masonry_core 0.5.0

Traits and types of the Masonry toolkit.
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
// Copyright 2019 the Xilem Authors and the Druid Authors
// SPDX-License-Identifier: Apache-2.0

//! Events.

use dpi::LogicalPosition;
use kurbo::Rect;
use ui_events::keyboard::{Code, Key, KeyState, KeyboardEvent};

use crate::core::TimerKey;
use crate::dpi::PhysicalSize;
use crate::util::Duration;

// --- MARK: TYPES

/// A global event.
#[derive(Debug, Clone, PartialEq)]
pub enum WindowEvent {
    /// The window's DPI factor changed.
    Rescale(f64),
    /// The window was resized.
    Resize(PhysicalSize<u32>),
    /// The animation frame requested by this window must run.
    AnimFrame(Duration),
    /// The accessibility tree must be updated when rendering the app.
    EnableAccessTree,
    /// The accessibility tree is no longer updated when rendering the app.
    DisableAccessTree,
}

/// A text-related event.
#[derive(Debug, Clone, PartialEq)]
pub enum TextEvent {
    /// A keyboard event.
    Keyboard(KeyboardEvent),
    /// An IME event.
    Ime(Ime),
    /// The window took or lost focus.
    WindowFocusChange(bool),
    // TODO - Handle rich text copy-pasting
    /// The user pasted content in.
    ClipboardPaste(String),
}

/// An accessibility event.
///
/// This is generated by `accesskit` back-ends when the user interacts with
/// the app using their platform's accessibility APIs.
///
/// For instance, these events are often used by visually impaired users to navigate
/// your app without a mouse.
#[derive(Debug, Clone, PartialEq)]
pub struct AccessEvent {
    /// The action that was performed.
    pub action: accesskit::Action,
    /// Additional data associated with the action.
    pub data: Option<accesskit::ActionData>,
}

/// The light/dark mode of the window.
#[derive(Debug, Clone, PartialEq)]
pub enum WindowTheme {
    /// Light mode.
    Light,
    /// Dark mode.
    Dark,
}

/// Changes to widget state.
///
/// Unlike [`PointerEvent`](ui_events::pointer::PointerEvent)s,
/// [`TextEvent`]s and [`AccessEvent`]s, [`Update`] events
/// are generated by Masonry, and may occur at different times during a given pass of
/// the event loop.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum Update {
    /// Sent to a `Widget` when it is added to the widget tree. This should be
    /// the first message that each widget receives.
    ///
    /// Widgets should handle this event in order to do any initial setup that cannot
    /// be done when constructing the widget.
    WidgetAdded,

    /// Called when the [disabled] state of the widget is changed.
    ///
    /// See also [`is_disabled`] and [`set_disabled`].
    ///
    /// [disabled]: crate::doc::masonry_concepts#disabled
    /// [`is_disabled`]: crate::core::EventCtx::is_disabled
    /// [`set_disabled`]: crate::core::EventCtx::set_disabled
    DisabledChanged(bool),

    /// Called when the [stashed] state of the widget is changed.
    ///
    /// See also [`is_stashed`] and [`set_stashed`].
    ///
    /// [stashed]: crate::doc::masonry_concepts#stashed
    /// [`is_stashed`]: crate::core::EventCtx::is_stashed
    /// [`set_stashed`]: crate::core::EventCtx::set_stashed
    StashedChanged(bool),

    /// Called when a descendant widget requests to be scrolled to.
    ///
    /// The included [`Rect`] is in the receiving widget's content-box coordinate space.
    ///
    /// See also [`EventCtx::request_scroll_to_this`] and [`EventCtx::request_scroll_to`].
    ///
    /// [`EventCtx::request_scroll_to_this`]: crate::core::EventCtx::request_scroll_to_this
    /// [`EventCtx::request_scroll_to`]: crate::core::EventCtx::request_scroll_to
    RequestPanToChild(Rect),

    /// Called when the [hovered] status of the current widget changes.
    ///
    /// [hovered]: crate::doc::masonry_concepts#widget-status
    HoveredChanged(bool),

    /// Called when the [hovered] status of the current widget or a descendant changes.
    ///
    /// This is sent before [`Update::HoveredChanged`].
    ///
    /// [hovered]: crate::doc::masonry_concepts#widget-status
    ChildHoveredChanged(bool),

    /// Called when the current timer for this key fires.
    TimerFired(TimerKey),

    /// Called when a pointer stays long enough on the current widget to trigger a
    /// tooltip timer.
    ///
    /// This will only be sent to widgets which return true in [`Widget::accepts_tooltips()`].
    ///
    /// [`Widget::accepts_tooltips()`]: crate::core::Widget::accepts_tooltips
    TooltipRequested(LogicalPosition<f64>),

    /// Called when the [active] status of the current widget changes.
    ///
    /// [active]: crate::doc::masonry_concepts#widget-status
    ActiveChanged(bool),

    /// Called when the [active] status of the current widget or a descendant changes.
    ///
    /// This is sent before [`Update::ActiveChanged`].
    ///
    /// [active]: crate::doc::masonry_concepts#widget-status
    ChildActiveChanged(bool),

    /// Called when the [focused] status of the current widget changes.
    ///
    /// [focused]: crate::doc::masonry_concepts#text-focus
    FocusChanged(bool),

    /// Called when the [focused] status of the current widget or a descendant changes.
    ///
    /// This is sent before [`Update::FocusChanged`].
    ///
    /// [focused]: crate::doc::masonry_concepts#text-focus
    ChildFocusChanged(bool),

    /// Called when available fonts have changed.
    ///
    /// Widgets that directly use fonts should recompute their font selection.
    FontsChanged,
}

/// An enum for specifying whether an event was handled.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Handled {
    /// An event was already handled, and shouldn't be propagated to other event handlers.
    Yes,
    /// An event has not yet been handled.
    No,
}

// --- MARK: IMPLS

impl TextEvent {
    /// Constructor for keyboard events with [`KeyState::Down`].
    ///
    /// The key code will be marked as [`Code::Unidentified`].
    pub fn key_down(key: Key) -> Self {
        Self::Keyboard(KeyboardEvent {
            state: KeyState::Down,
            key,
            code: Code::Unidentified,
            ..Default::default()
        })
    }

    /// Constructor for keyboard events with [`KeyState::Up`].
    ///
    /// The key code will be marked as [`Code::Unidentified`].
    pub fn key_up(key: Key) -> Self {
        Self::Keyboard(KeyboardEvent {
            state: KeyState::Up,
            key,
            code: Code::Unidentified,
            ..Default::default()
        })
    }

    /// Constructor for [`Ime::Preedit`] events.
    ///
    /// This is mostly useful for testing.
    pub fn preedit(text: String) -> Self {
        Self::Ime(Ime::Preedit(text, None))
    }

    /// Constructor for [`Ime::Preedit`] events.
    ///
    /// This is mostly useful for testing.
    ///
    /// **selected** is the part of the preedit text that should be selected.
    ///
    /// # Panics
    ///
    /// If **selected** isn't a substring of **text**.
    pub fn preedit_with_cursor(text: String, selected: String) -> Self {
        let Some(offset) = text.find(&selected) else {
            panic!("Error building Preedit event: '{selected}' not found in '{text}'");
        };
        let span = (offset, selected.len());
        Self::Ime(Ime::Preedit(text, Some(span)))
    }

    /// Short name of the event, for debug logging.
    pub fn short_name(&self) -> &'static str {
        match self {
            Self::Keyboard(KeyboardEvent { repeat: true, .. }) => "Keyboard(repeat)",
            Self::Keyboard(..) => "Keyboard",
            Self::Ime(Ime::Disabled) => "Ime::Disabled",
            Self::Ime(Ime::Enabled) => "Ime::Enabled",
            Self::Ime(Ime::Commit(_)) => "Ime::Commit",
            Self::Ime(Ime::Preedit(s, _)) if s.is_empty() => "Ime::Preedit(\"\")",
            Self::Ime(Ime::Preedit(_, _)) => "Ime::Preedit(\"...\")",
            Self::WindowFocusChange(_) => "WindowFocusChange",
            Self::ClipboardPaste(_) => "ClipboardPaste",
        }
    }
}

impl AccessEvent {
    /// Short name of the event, for debug logging.
    ///
    /// Returns the enum variant name.
    pub fn short_name(&self) -> &'static str {
        match self.action {
            accesskit::Action::Click => "Click",
            accesskit::Action::Focus => "Focus",
            accesskit::Action::Blur => "Blur",
            accesskit::Action::Collapse => "Collapse",
            accesskit::Action::Expand => "Expand",
            accesskit::Action::CustomAction => "CustomAction",
            accesskit::Action::Decrement => "Decrement",
            accesskit::Action::Increment => "Increment",
            accesskit::Action::HideTooltip => "HideTooltip",
            accesskit::Action::ShowTooltip => "ShowTooltip",
            accesskit::Action::ReplaceSelectedText => "ReplaceSelectedText",
            accesskit::Action::ScrollDown => "ScrollDown",
            accesskit::Action::ScrollLeft => "ScrollLeft",
            accesskit::Action::ScrollRight => "ScrollRight",
            accesskit::Action::ScrollUp => "ScrollUp",
            accesskit::Action::ScrollIntoView => "ScrollIntoView",
            accesskit::Action::ScrollToPoint => "ScrollToPoint",
            accesskit::Action::SetScrollOffset => "SetScrollOffset",
            accesskit::Action::SetTextSelection => "SetTextSelection",
            accesskit::Action::SetSequentialFocusNavigationStartingPoint => {
                "SetSequentialFocusNavigationStartingPoint"
            }
            accesskit::Action::SetValue => "SetValue",
            accesskit::Action::ShowContextMenu => "ShowContextMenu",
        }
    }
}

impl Update {
    /// Short name of the event, for debug logging.
    ///
    /// Returns the enum variant name.
    pub fn short_name(&self) -> &str {
        match self {
            Self::WidgetAdded => "WidgetAdded",
            Self::DisabledChanged(false) => "DisabledChanged(false)",
            Self::StashedChanged(false) => "StashedChanged(false)",
            Self::HoveredChanged(false) => "HoveredChanged(false)",
            Self::ChildHoveredChanged(false) => "ChildHoveredChanged(false)",
            Self::ActiveChanged(false) => "ActiveChanged(false)",
            Self::ChildActiveChanged(false) => "ChildActiveChanged(false)",
            Self::FocusChanged(false) => "FocusChanged(false)",
            Self::ChildFocusChanged(false) => "ChildFocusChanged(false)",
            Self::TooltipRequested(_) => "TooltipRequested(_)",
            Self::DisabledChanged(true) => "DisabledChanged(true)",
            Self::StashedChanged(true) => "StashedChanged(true)",
            Self::HoveredChanged(true) => "HoveredChanged(true)",
            Self::ChildHoveredChanged(true) => "ChildHoveredChanged(true)",
            Self::ActiveChanged(true) => "ActiveChanged(true)",
            Self::ChildActiveChanged(true) => "ChildActiveChanged(true)",
            Self::FocusChanged(true) => "FocusChanged(true)",
            Self::ChildFocusChanged(true) => "ChildFocusChanged(true)",
            Self::RequestPanToChild(_) => "RequestPanToChild(_)",
            Self::TimerFired(_) => "TimerFired(_)",
            Self::FontsChanged => "FontsChanged",
        }
    }
}

impl Handled {
    /// Has the event been handled yet?
    pub fn is_handled(self) -> bool {
        self == Self::Yes
    }
}

impl From<bool> for Handled {
    /// Returns `Handled::Yes` if `handled` is true, and `Handled::No` otherwise.
    fn from(handled: bool) -> Self {
        if handled { Self::Yes } else { Self::No }
    }
}

// --- MARK: WINIT EXPIES

/// Describes [input method](https://en.wikipedia.org/wiki/Input_method) events.
///
/// Mirrors [`winit::event::Ime`](https://docs.rs/winit/latest/x86_64-unknown-linux-gnu/winit/event/enum.Ime.html).
///
/// This is also called a "composition event".
///
/// Most keypresses using a latin-like keyboard layout simply generate a [`TextEvent::Keyboard`].
/// However, one couldn't possibly have a key for every single unicode character that the user
/// might want to type — so the solution operating systems employ is to allow the user to type these
/// using _a sequence of keypresses_ instead.
///
/// A prominent example of this is accents - many keyboard layouts allow you to first click the
/// "accent key", and then the character you want to apply the accent to. In this case, some
/// platforms will generate the following event sequence:
///
/// ```ignore
/// // Press "`" key
/// Ime::Preedit("`", Some((0, 0)))
/// // Press "E" key
/// Ime::Preedit("", None) // Synthetic event generated by winit to clear preedit.
/// Ime::Commit("é")
/// ```
///
/// Additionally, certain input devices are configured to display a candidate box that allow the
/// user to select the desired character interactively. (To properly position this box, you must use
/// [`RenderRootSignal::ImeMoved`](crate::app::RenderRootSignal::ImeMoved).)
///
/// An example of a keyboard layout which uses candidate boxes is pinyin. On a latin keyboard the
/// following event sequence could be obtained:
///
/// ```ignore
/// // Press "A" key
/// Ime::Preedit("a", Some((1, 1)))
/// // Press "B" key
/// Ime::Preedit("a b", Some((3, 3)))
/// // Press left arrow key
/// Ime::Preedit("a b", Some((1, 1)))
/// // Press space key
/// Ime::Preedit("啊b", Some((3, 3)))
/// // Press space key
/// Ime::Preedit("", None) // Synthetic event generated by winit to clear preedit.
/// Ime::Commit("啊不")
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
//#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Ime {
    /// Notifies when the IME was enabled.
    ///
    /// After getting this event you could receive [`Preedit`][Self::Preedit] and
    /// [`Commit`][Self::Commit] events. You should also start performing IME related requests
    /// like [`RenderRootSignal::ImeMoved`](crate::app::RenderRootSignal::ImeMoved).
    Enabled,

    /// Notifies when a new composing text should be set at the cursor position.
    ///
    /// The value represents a pair of the preedit string and the cursor begin position and end
    /// position. When it's `None`, the cursor should be hidden. When `String` is an empty string
    /// this indicates that preedit was cleared.
    ///
    /// The cursor position is byte-wise indexed.
    Preedit(String, Option<(usize, usize)>),

    /// Notifies when text should be inserted into the editor widget.
    ///
    /// Right before this event winit will send empty [`Self::Preedit`] event.
    Commit(String),

    /// Notifies when the IME was disabled.
    ///
    /// After receiving this event you won't get any more [`Preedit`][Self::Preedit] or
    /// [`Commit`][Self::Commit] events until the next [`Enabled`][Self::Enabled] event.
    Disabled,
}

/// Defines the orientation that a window resize will be performed.
///
/// Mirrors [`winit::window::ResizeDirection`](https://docs.rs/winit/latest/x86_64-unknown-linux-gnu/winit/window/enum.ResizeDirection.html).
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[expect(missing_docs, reason = "Copied from winit")]
pub enum ResizeDirection {
    East,
    North,
    NorthEast,
    NorthWest,
    South,
    SouthEast,
    SouthWest,
    West,
}