mitsuami-widgets 2.5.2

Built-in widgets for mitsuami
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Images, icons, files' icons and GPU surfaces.

use std::rc::Rc;

use mitsuami_core::{
    Color, Cursor, Element, ElementBuilder, FileLookup, ImageFit, ImageSource, NodeId, Pixels, Prop, Rect,
    SurfaceHandle, SurfaceInput, SurfaceSize, Tweak, Ui, UiEvent, VectorIcon, View, WidgetKind,
};
use mitsuami_reactive::{IntoValue, Signal, Value};

/// A picture, in the platform's image view: from a file, or from pixels
/// in memory. It's as large as the image (in points: pixels over their
/// scale) unless the layout sizes it; then `fit` says how it fills the
/// frame, or the platform does as it does by default. Its label is its
/// accessible name; without one it's decorative.
///
/// ```ignore
/// Image::file("logo.png").label("2ksbox")
/// Image::new(move || ImageSource::Pixels(frame.get())).label("Preview")
/// ```
pub struct Image(Element);

widget!(Image);

impl Image {
    pub fn new(source: impl IntoValue<ImageSource>) -> Image {
        let mut element = Element::new(WidgetKind::Image);
        element.prop(source.into_value(), Prop::Image);
        Image(element)
    }

    /// An image file, read by the platform.
    pub fn file(path: impl Into<std::path::PathBuf>) -> Image {
        Image::new(ImageSource::File(path.into()))
    }

    /// Pixels in memory.
    pub fn pixels(pixels: impl IntoValue<Pixels>) -> Image {
        let pixels = pixels.into_value();
        Image::new(match pixels {
            Value::Static(p) => Value::Static(ImageSource::Pixels(p)),
            dynamic => Value::Dynamic(Rc::new(move || ImageSource::Pixels(dynamic.get()))),
        })
    }

    /// Its accessible name: what the picture shows.
    pub fn label(mut self, label: impl IntoValue<String>) -> Image {
        self.0.prop(label.into_value(), Prop::Label);
        self
    }

    /// How it fills a frame of another size than its own.
    pub fn fit(mut self, fit: impl IntoValue<ImageFit>) -> Image {
        self.0.prop(fit.into_value(), Prop::ImageFit);
        self
    }

    /// Raw platform settings, past the semantic ones: see [`Tweak`].
    pub fn native(mut self, tweak: Tweak<Image>) -> Image {
        tweak.apply(&mut self.0);
        self
    }
}

/// An icon from the platform's own set, by its name there: an SF Symbol
/// on macOS, a themed icon's name on Linux (Adwaita's symbolic ones on
/// GNOME, Breeze's on KDE), a Segoe Fluent Icons glyph on Windows. Names
/// differ, so pick one per platform with `platform!`. Symbolic icons are
/// drawn in the colour the platform gives icons, which follows dark mode.
/// It's as large as the platform makes icons unless
/// `icon_size` says otherwise. Its label is its accessible name; without
/// one it's decorative.
///
/// ```ignore
/// Icon::new(platform! {
///     macos => "trash",
///     gtk => "user-trash-symbolic",
///     kde => "edit-delete",
///     windows => "\u{E74D}",
/// })
/// ```
pub struct Icon(Element);

widget!(Icon);

impl Icon {
    pub fn new(name: impl IntoValue<String>) -> Icon {
        let mut element = Element::new(WidgetKind::Icon);
        element.prop(name.into_value(), Prop::Icon);
        Icon(element)
    }

    /// The app's own icon, drawn from path data: see [`VectorIcon`]. It
    /// takes the size and colour a named icon takes, and shows in place of
    /// one. `Icon::new("")` makes an icon without a name.
    pub fn vector(mut self, icon: impl IntoValue<VectorIcon>) -> Icon {
        self.0.prop(crate::some(icon.into_value()), Prop::VectorIcon);
        self
    }

    /// Its accessible name: what the icon stands for.
    pub fn label(mut self, label: impl IntoValue<String>) -> Icon {
        self.0.prop(label.into_value(), Prop::Label);
        self
    }

    /// Its colour, in place of the one the platform gives icons: a
    /// semantic one (`Accent`, `SecondaryLabel`, `Error`, …) follows the
    /// appearance; `Rgba` is fixed. Symbolic icons take it (SF Symbols,
    /// Adwaita's and Breeze's `-symbolic` icons, Segoe Fluent glyphs);
    /// icons in full colour keep theirs.
    pub fn color(mut self, color: impl IntoValue<Color>) -> Icon {
        self.0.prop(color.into_value(), Prop::TextColor);
        self
    }

    /// How big it is, in points: an SF Symbol's point size, as a font's
    /// (the symbol's own shape sets its frame), a square's side elsewhere.
    pub fn icon_size(mut self, points: impl IntoValue<f32>) -> Icon {
        self.0.prop(points.into_value(), Prop::IconSize);
        self
    }

    /// Raw platform settings, past the semantic ones: see [`Tweak`].
    pub fn native(mut self, tweak: Tweak<Icon>) -> Icon {
        tweak.apply(&mut self.0);
        self
    }
}

/// A file's or folder's own icon, as the platform's file manager shows
/// it: an app's own, a folder's custom one, a document's by its type (the
/// icon theme's on Linux). It's a square as big as the platform's small
/// icons (16 points) unless `icon_size` says otherwise. With `thumbnail`,
/// a preview of what's in the file (a picture's, a document's first page)
/// where the platform makes one. The file is read when it's set, unless
/// `lookup` says to go by its name alone. Its label is its accessible
/// name; without one it's decorative, as it usually sits beside the
/// file's name.
///
/// ```ignore
/// FileIcon::new(&entry.path).icon_size(32.0).thumbnail(true)
/// FileIcon::new(&member.name).lookup(FileLookup::FileName)
/// ```
pub struct FileIcon(Element);

widget!(FileIcon);

impl FileIcon {
    pub fn new(path: impl IntoValue<std::path::PathBuf>) -> FileIcon {
        let mut element = Element::new(WidgetKind::FileIcon);
        element.prop(path.into_value(), Prop::File);
        FileIcon(element)
    }

    /// Its accessible name, where no text beside it names the file.
    pub fn label(mut self, label: impl IntoValue<String>) -> FileIcon {
        self.0.prop(label.into_value(), Prop::Label);
        self
    }

    /// Its square's side, in points.
    pub fn icon_size(mut self, points: impl IntoValue<f32>) -> FileIcon {
        self.0.prop(points.into_value(), Prop::IconSize);
        self
    }

    /// Shows a preview of what's in the file where the platform makes one
    /// (macOS, Windows, and GNOME's cached ones), its icon until then.
    pub fn thumbnail(mut self, thumbnail: impl IntoValue<bool>) -> FileIcon {
        self.0.prop(thumbnail.into_value(), Prop::Thumbnail);
        self
    }

    /// Where its icon comes from: the file on disk (the default), or only
    /// its name, for a file that isn't there (nothing is read, and
    /// `thumbnail` is ignored).
    pub fn lookup(mut self, lookup: impl IntoValue<FileLookup>) -> FileIcon {
        self.0.prop(lookup.into_value(), Prop::FileLookup);
        self
    }

    /// Raw platform settings, past the semantic ones: see [`Tweak`].
    pub fn native(mut self, tweak: Tweak<FileIcon>) -> FileIcon {
        tweak.apply(&mut self.0);
        self
    }
}

/// A surface the app draws on with its own GPU API (wgpu, Vulkan, Metal,
/// Direct3D), at its own pace and on its own thread, as it would draw on a
/// window of its own: an `NSView` backed by a `CAMetalLayer` on AppKit.
/// It has no natural size; the layout sizes it.
///
/// `on_ready` gets its [`SurfaceHandle`] once the native surface exists,
/// and `on_resize` its size in pixels whenever that or its scale changes;
/// the handle's `size()` has it too, for a render thread. The handle keeps
/// the native surface alive after the widget is gone, so drop it (and the
/// GPU surface made on it) when the app is done presenting.
///
/// The surface sits above the window's own content, as the platform
/// layers it. Its label is its accessible name.
///
/// With `on_input` it takes keys and the pointer: a click or Tab focuses
/// it, and it gets every key the window doesn't take first for its menus'
/// shortcuts, Tab too (Control+Tab leaves it on AppKit and GTK, as it
/// leaves a text view). `pointer_lock` hides and holds the cursor and
/// reports its moves; `keyboard_grab` takes the system's shortcuts and
/// the app's own as well. The platform ends both when the window stops
/// being the active one (the grab also when the surface loses focus), and
/// sets their signals back to `false`: the app locks again, say, on the
/// next click. `cursor` is the cursor over it: the platform's, none, or
/// the app's own image.
///
/// ```ignore
/// GpuSurface::new()
///     .label("Machine")
///     .on_ready(move |surface| renderer.send(Message::Surface(surface)))
///     .on_resize(move |size| renderer.send(Message::Resize(size)))
///     .on_input(move |input| machine.send(Message::Input(input)))
///     .pointer_lock(captured)
///     .keyboard_grab(captured)
/// ```
pub struct GpuSurface(Element);

widget!(GpuSurface);

impl Default for GpuSurface {
    fn default() -> GpuSurface {
        GpuSurface::new()
    }
}

impl GpuSurface {
    pub fn new() -> GpuSurface {
        GpuSurface(Element::new(WidgetKind::GpuSurface))
    }

    /// Its accessible name: what the app draws there.
    pub fn label(mut self, label: impl IntoValue<String>) -> GpuSurface {
        self.0.prop(label.into_value(), Prop::Label);
        self
    }

    /// The window it's in became the active one, or stopped being it. The surface keeps its
    /// focus through this: a caret it paints should stop anyway, as a text field's does.
    pub fn on_window_active(mut self, handler: impl Fn(bool) + 'static) -> GpuSurface {
        self.0.on(move |event| {
            if let UiEvent::WindowActiveChanged(active) = event {
                handler(*active);
            }
        });
        self
    }

    /// The native surface exists: make the GPU surface on it. Called once.
    pub fn on_ready(mut self, handler: impl Fn(SurfaceHandle) + 'static) -> GpuSurface {
        self.0.on(move |event| {
            if let UiEvent::SurfaceReady(surface) = event {
                handler(surface.clone());
            }
        });
        self
    }

    /// Its size in pixels, or its scale, changed: configure the GPU
    /// surface for it.
    pub fn on_resize(mut self, handler: impl Fn(SurfaceSize) + 'static) -> GpuSurface {
        self.0.on(move |event| {
            if let UiEvent::SurfaceResized(size) = event {
                handler(*size);
            }
        });
        self
    }

    /// Takes keys and the pointer, and reports them: a key down or up, the
    /// pointer's moves, buttons and scrolling over it, and while it's
    /// locked, how far it moved. Keys down when it loses focus, or its
    /// window stops being the active one, are reported released then.
    pub fn on_input(mut self, handler: impl Fn(SurfaceInput) + 'static) -> GpuSurface {
        self.0.prop(Value::Static(true), Prop::TakesInput);
        self.0.on(move |event| {
            if let UiEvent::SurfaceInput(input) = event {
                handler(input.clone());
            }
        });
        self
    }

    /// Hides and holds the cursor while `locked` is true, reporting its
    /// moves as `SurfaceInput::Motion`, and before the host's acceleration
    /// as `SurfaceInput::RawMotion`. The platform ends it when the
    /// window stops being the active one, and `locked` goes back to false.
    pub fn pointer_lock(mut self, locked: Signal<bool>) -> GpuSurface {
        self.0.prop(locked.into_value(), Prop::PointerLock);
        self.0.on(move |event| {
            if *event == UiEvent::PointerLockEnded {
                locked.set(false);
            }
        });
        self
    }

    /// The pointer's cursor over it, while it isn't locked: the
    /// platform's own, none, or an image of the app's (the one a machine
    /// gives its pointer, say).
    pub fn cursor(mut self, cursor: impl IntoValue<Cursor>) -> GpuSurface {
        self.0.prop(cursor.into_value(), Prop::Cursor);
        self
    }

    /// Takes text, as a text field does, while `on` is true: keys report
    /// what they type (`SurfaceInput::Key`'s `text`), and the platform's
    /// input methods are on while it has focus, committing as
    /// `SurfaceInput::Text`. With `on_input`, which reports them.
    pub fn text_input(mut self, on: impl IntoValue<bool>) -> GpuSurface {
        self.0.prop(on.into_value(), Prop::TextInput);
        self
    }

    /// Whether the platform's input methods are on while it takes text,
    /// as they are unless `on` is false: off, keys still report what they
    /// type (layouts, dead keys, AltGr), but nothing composes. For a modal
    /// editor, whose keys outside insert mode are commands.
    pub fn input_method(mut self, on: impl IntoValue<bool>) -> GpuSurface {
        self.0.prop(on.into_value(), Prop::InputMethod);
        self
    }

    /// Where the caret is, in points from its top left, while it takes
    /// text: the input method's candidate window opens by it.
    pub fn text_cursor(mut self, caret: impl IntoValue<Option<Rect>>) -> GpuSurface {
        self.0.prop(caret.into_value(), Prop::TextCursor);
        self
    }

    /// Lets the window's backdrop show through it while `on` is true,
    /// where what the app presents isn't opaque: AppKit puts its
    /// behind-window vibrancy under it, and WinUI's windows have Mica
    /// already; GTK and Kirigami have no backdrop, so the window's own
    /// background shows. The app presents with alpha, premultiplied as
    /// the compositor reads it (wgpu's `PostMultiplied` mode on Metal, a
    /// composition swapchain with `PreMultiplied` on Direct3D 12).
    pub fn transparent(mut self, on: impl IntoValue<bool>) -> GpuSurface {
        self.0.prop(on.into_value(), Prop::Transparent);
        self
    }

    /// Takes every key while `grabbed` is true, the system's shortcuts
    /// (as far as the platform lets an app) and the window's own too, and
    /// focuses the surface. The platform ends it when the surface or its
    /// window loses focus, and `grabbed` goes back to false.
    pub fn keyboard_grab(mut self, grabbed: Signal<bool>) -> GpuSurface {
        self.0.prop(grabbed.into_value(), Prop::KeyboardGrab);
        self.0.on(move |event| {
            if *event == UiEvent::KeyboardGrabEnded {
                grabbed.set(false);
            }
        });
        self
    }
}

impl Image {
    /// `<Image source=ImageSource::File("logo.png".into()) label="Logo"/>`
    #[doc(hidden)]
    pub fn __tag() -> Image {
        Image(Element::new(WidgetKind::Image))
    }

    #[doc(hidden)]
    pub fn source(mut self, source: impl IntoValue<ImageSource>) -> Image {
        self.0.prop(source.into_value(), Prop::Image);
        self
    }
}

impl Icon {
    /// `<Icon name="trash" label="Delete"/>`
    #[doc(hidden)]
    pub fn __tag() -> Icon {
        Icon(Element::new(WidgetKind::Icon))
    }

    #[doc(hidden)]
    pub fn name(mut self, name: impl IntoValue<String>) -> Icon {
        self.0.prop(name.into_value(), Prop::Icon);
        self
    }
}

impl FileIcon {
    /// `<FileIcon path=entry.path icon_size=32.0/>`
    #[doc(hidden)]
    pub fn __tag() -> FileIcon {
        FileIcon(Element::new(WidgetKind::FileIcon))
    }

    #[doc(hidden)]
    pub fn path(mut self, path: impl IntoValue<std::path::PathBuf>) -> FileIcon {
        self.0.prop(path.into_value(), Prop::File);
        self
    }
}

impl GpuSurface {
    /// `<GpuSurface label="Machine" @ready=… @resize=… @input=… pointer_lock=captured/>`
    #[doc(hidden)]
    pub fn __tag() -> GpuSurface {
        GpuSurface::new()
    }
}