xrack-runtime 0.6.0

Runtime types, properties, models, and controls for generated XRack UI
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use crate::Property;
use std::{cell::RefCell, fmt, ops::Range, rc::Rc};

/// Identity of a focus target in one component instance.
#[derive(Clone, Default)]
pub struct FocusHandle(Rc<FocusSignals>);

/// Reactive physical press state for one observed push button.
#[derive(Clone, Default)]
#[doc(hidden)]
pub struct ButtonPressHandle(Rc<Property<bool>>);

impl fmt::Debug for ButtonPressHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ButtonPressHandle").finish_non_exhaustive()
    }
}

impl PartialEq for ButtonPressHandle {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl ButtonPressHandle {
    pub fn pressed(&self) -> bool {
        self.0.get(|| false)
    }

    pub fn publish(&self, pressed: bool) {
        self.0.set_if_changed(pressed);
    }
}

#[derive(Default)]
struct FocusSignals {
    direct: Property<bool>,
    within: Property<bool>,
    text: Option<Box<TextSelectionSignals>>,
    commands: Option<Box<TextCommandSignals>>,
}

#[derive(Default)]
struct TextSelectionSignals {
    offsets: [Property<i64>; 3],
    selected: Property<Rc<str>>,
}

#[derive(Default)]
struct TextCommandSignals {
    undo: Property<bool>,
    redo: Property<bool>,
    cut: Property<bool>,
    copy: Property<bool>,
}

/// Availability of commands on the active text editor.
#[doc(hidden)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TextCommandAvailability {
    pub undo: bool,
    pub redo: bool,
    pub cut: bool,
    pub copy: bool,
}

impl fmt::Debug for FocusHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FocusHandle").finish_non_exhaustive()
    }
}

impl PartialEq for FocusHandle {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FocusAction {
    Focus,
    Clear,
    Text(TextCommand),
    EditTableCell { row: i64, column: i64 },
    CancelTableEdit,
}

/// A programmatic edit using the same selection, validation and history as keyboard input.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextCommand {
    SelectAll,
    ClearSelection,
    SetSelection { anchor: i64, focus: i64 },
    Undo,
    Redo,
    Cut,
    Copy,
    Paste,
}

/// Commands lowered from XR focus and editor methods.
#[doc(hidden)]
#[derive(Clone, Copy, Debug)]
pub enum FocusCommandMethod {
    Focus,
    Clear,
    SelectAll,
    ClearSelection,
    SetSelection,
    Undo,
    Redo,
    Cut,
    Copy,
    Paste,
    EditTableCell,
    CancelTableEdit,
}

impl FocusCommandMethod {
    pub fn parse(name: &str) -> Option<Self> {
        Some(match name {
            "focus" => Self::Focus,
            "clear" => Self::Clear,
            "select-all" => Self::SelectAll,
            "clear-selection" => Self::ClearSelection,
            "set-selection-offsets" => Self::SetSelection,
            "undo" => Self::Undo,
            "redo" => Self::Redo,
            "cut" => Self::Cut,
            "copy" => Self::Copy,
            "paste" => Self::Paste,
            "edit-cell" => Self::EditTableCell,
            "cancel-edit" => Self::CancelTableEdit,
            _ => return None,
        })
    }

    pub fn action(self, operands: &[i64]) -> Option<FocusAction> {
        let text = match (self, operands) {
            (Self::Focus, []) => return Some(FocusAction::Focus),
            (Self::Clear, []) => return Some(FocusAction::Clear),
            (Self::SelectAll, []) => TextCommand::SelectAll,
            (Self::ClearSelection, []) => TextCommand::ClearSelection,
            (Self::SetSelection, &[anchor, focus]) => TextCommand::SetSelection { anchor, focus },
            (Self::Undo, []) => TextCommand::Undo,
            (Self::Redo, []) => TextCommand::Redo,
            (Self::Cut, []) => TextCommand::Cut,
            (Self::Copy, []) => TextCommand::Copy,
            (Self::Paste, []) => TextCommand::Paste,
            (Self::EditTableCell, &[row, column]) => {
                return Some(FocusAction::EditTableCell { row, column });
            }
            (Self::CancelTableEdit, []) => return Some(FocusAction::CancelTableEdit),
            _ => return None,
        };
        Some(FocusAction::Text(text))
    }
}

#[doc(hidden)]
pub fn is_focus_handler(name: &str) -> bool {
    FocusHandler::ALL
        .iter()
        .any(|handler| handler.name() == name)
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FocusReason {
    #[default]
    Programmatic,
    TabNavigation,
    PointerClick,
    PopupActivation,
    WindowActivation,
    Accessibility,
    LayoutChange,
    KeyboardNavigation,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FocusEvent {
    Gained(FocusReason),
    Lost(FocusReason),
    Within { focused: bool, reason: FocusReason },
}

#[derive(Clone, Copy, Debug)]
#[doc(hidden)]
pub enum FocusHandler {
    Gained,
    Lost,
    Changed,
    Within,
}

impl FocusHandler {
    pub const ALL: [Self; 4] = [Self::Gained, Self::Lost, Self::Changed, Self::Within];

    pub fn name(self) -> &'static str {
        match self {
            Self::Gained => "focus-gained",
            Self::Lost => "focus-lost",
            Self::Changed => "focus-changed-event",
            Self::Within => "focus-within-changed",
        }
    }
}

impl FocusEvent {
    #[doc(hidden)]
    pub fn is_loss(self) -> bool {
        matches!(self, Self::Lost(_) | Self::Within { focused: false, .. })
    }

    #[doc(hidden)]
    pub fn try_deliver<E>(
        self,
        mut notify: impl FnMut(FocusHandler, bool, FocusReason) -> Result<bool, E>,
    ) -> Result<bool, E> {
        let (handler, focused, reason) = match self {
            Self::Gained(reason) => (FocusHandler::Gained, true, reason),
            Self::Lost(reason) => (FocusHandler::Lost, false, reason),
            Self::Within { focused, reason } => {
                return notify(FocusHandler::Within, focused, reason);
            }
        };
        let handled = notify(handler, focused, reason)?;
        Ok(notify(FocusHandler::Changed, focused, reason)? || handled)
    }

    #[doc(hidden)]
    pub fn deliver(self, mut notify: impl FnMut(FocusHandler, bool, FocusReason) -> bool) -> bool {
        self.try_deliver(|handler, focused, reason| {
            Ok::<_, std::convert::Infallible>(notify(handler, focused, reason))
        })
        .unwrap_or_else(|error| match error {})
    }
}

pub struct FocusRequest {
    pub target: FocusHandle,
    pub action: FocusAction,
}

/// A window's commands, drained against the scene prepared after the callback.
#[derive(Clone, Default)]
pub struct FocusRequests(Rc<RefCell<Vec<FocusRequest>>>);

thread_local! {
    static ACTIVE: RefCell<Option<FocusRequests>> = const { RefCell::new(None) };
}

pub struct FocusContext(Option<FocusRequests>);

impl FocusRequests {
    pub fn enter(&self) -> FocusContext {
        FocusContext(ACTIVE.with(|active| active.replace(Some(self.clone()))))
    }

    pub fn drain(&self) -> Vec<FocusRequest> {
        std::mem::take(&mut *self.0.borrow_mut())
    }

    pub fn is_empty(&self) -> bool {
        self.0.borrow().is_empty()
    }
}

impl Drop for FocusContext {
    fn drop(&mut self) {
        ACTIVE.with(|active| active.replace(self.0.take()));
    }
}

impl FocusHandle {
    #[doc(hidden)]
    pub fn with_text_selection() -> Self {
        Self::with_text_state(true, false)
    }

    #[doc(hidden)]
    pub fn with_text_state(selection: bool, commands: bool) -> Self {
        Self(Rc::new(FocusSignals {
            text: selection.then(Box::default),
            commands: commands.then(Box::default),
            ..FocusSignals::default()
        }))
    }

    #[doc(hidden)]
    pub fn observes_text_commands(&self) -> bool {
        self.0.commands.is_some()
    }

    #[doc(hidden)]
    pub fn publish_text_commands(&self, commands: TextCommandAvailability) {
        if let Some(signals) = &self.0.commands {
            signals.undo.set_if_changed(commands.undo);
            signals.redo.set_if_changed(commands.redo);
            signals.cut.set_if_changed(commands.cut);
            signals.copy.set_if_changed(commands.copy);
        }
    }

    #[doc(hidden)]
    pub fn clear_text_state(&self) {
        self.publish_text_selection(0, 0..0, "");
        self.publish_text_commands(TextCommandAvailability::default());
    }

    pub fn can_undo(&self) -> bool {
        self.0
            .commands
            .as_ref()
            .is_some_and(|signals| signals.undo.get(|| false))
    }

    pub fn can_redo(&self) -> bool {
        self.0
            .commands
            .as_ref()
            .is_some_and(|signals| signals.redo.get(|| false))
    }

    pub fn can_cut(&self) -> bool {
        self.0
            .commands
            .as_ref()
            .is_some_and(|signals| signals.cut.get(|| false))
    }

    pub fn can_copy(&self) -> bool {
        self.0
            .commands
            .as_ref()
            .is_some_and(|signals| signals.copy.get(|| false))
    }

    #[doc(hidden)]
    pub fn observes_text_selection(&self) -> bool {
        self.0.text.is_some()
    }

    #[doc(hidden)]
    pub fn publish_text_selection(&self, caret: usize, selection: Range<usize>, text: &str) {
        if let Some(signals) = &self.0.text {
            for (offset, value) in
                signals
                    .offsets
                    .iter()
                    .zip([caret, selection.start, selection.end])
            {
                offset.set_if_changed(value as i64);
            }
            if signals.selected.explicit_value().as_deref() != Some(text) {
                signals.selected.set(Rc::from(text));
            }
        }
    }

    fn text_offset(&self, index: usize) -> i64 {
        self.0
            .text
            .as_ref()
            .map_or(0, |signals| signals.offsets[index].get(|| 0))
    }

    pub fn cursor_offset(&self) -> i64 {
        self.text_offset(0)
    }

    pub fn selection_start_offset(&self) -> i64 {
        self.text_offset(1)
    }

    pub fn selection_end_offset(&self) -> i64 {
        self.text_offset(2)
    }

    pub fn selected_text(&self) -> String {
        self.0.text.as_ref().map_or_else(String::new, |signals| {
            signals.selected.get(|| Rc::from("")).to_string()
        })
    }

    pub fn has_focus(&self) -> bool {
        self.0.direct.get(|| false)
    }

    pub fn focus_within(&self) -> bool {
        self.0.within.get(|| false)
    }

    #[doc(hidden)]
    pub fn publish(&self, direct: bool, within: bool) {
        self.0.direct.set_if_changed(direct);
        self.0.within.set_if_changed(within);
    }

    pub fn request(&self, action: FocusAction) {
        ACTIVE.with(|active| {
            if let Some(requests) = &*active.borrow() {
                requests.0.borrow_mut().push(FocusRequest {
                    target: self.clone(),
                    action,
                });
            }
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn selection_start_and_selected_text_track_independently() {
        let handle = FocusHandle::with_text_selection();
        let start = Property::default();
        let selected = Property::default();
        handle.publish_text_selection(3, 1..3, "bc");
        assert_eq!(start.get(|| handle.selection_start_offset()), 1);
        assert_eq!(selected.get(|| handle.selected_text()), "bc");

        handle.publish_text_selection(4, 1..4, "bcd");
        assert!(!start.is_dirty());
        assert!(selected.is_dirty());
        assert_eq!(selected.get(|| handle.selected_text()), "bcd");

        handle.publish_text_selection(1, 1..4, "bcd");
        assert!(!start.is_dirty());
        assert!(!selected.is_dirty());
    }

    #[test]
    fn direct_and_within_focus_have_independent_dependencies() {
        let handle = FocusHandle::default();
        let direct = Property::default();
        let within = Property::default();
        assert!(!direct.get(|| handle.has_focus()));
        assert!(!within.get(|| handle.focus_within()));
        handle.publish(false, true);
        assert!(!direct.is_dirty());
        assert!(within.is_dirty());
        assert!(within.get(|| handle.focus_within()));
        handle.publish(true, true);
        assert!(direct.is_dirty());
        assert!(!within.is_dirty());
    }
}