ferroui-base 0.3.0

The base library of FerroUI: the property system, layout, input, styling, media, text, animation, data binding and the compositor
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use super::{
    FocusManager, IAccessKeyHandler, IMainMenu, InputElement, Key, KeyEventArgs, KeyModifiers, PointerPressedEventArgs,
};
use crate::interactivity::{RoutedEvent, RoutedEventArgs, RoutingStrategies};
use crate::reactive::IDisposable;
use crate::{
    ferro_property, ferro_routed_event, ferro_routed_event_args, AttachedProperty, FerroProperty, Ref,
    StyledPropertyOptions, Visual, WeakRef,
};
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::rc::{Rc, Weak};

type Element = Ref<InputElement>;

/// The inputs to an access key pressed event: raised on an element to ask
/// which element an access key should target.
#[derive(Clone)]
pub struct AccessKeyPressedEventArgs {
    base: RoutedEventArgs,
    target: Rc<RefCell<Option<Element>>>,
    key: String,
}

ferro_routed_event_args!(AccessKeyPressedEventArgs: RoutedEventArgs);

impl AccessKeyPressedEventArgs {
    /// Creates args for the given (normalized) access key.
    pub fn new(key: &str) -> Self {
        Self {
            base: RoutedEventArgs::with_event(AccessKeyHandler::access_key_pressed_event()),
            target: Rc::new(RefCell::new(None)),
            key: key.to_string(),
        }
    }

    /// The target for the access key.
    pub fn target(&self) -> Option<Element> {
        self.target.borrow().clone()
    }

    /// Sets the target for the access key.
    pub fn set_target(&self, value: Option<Element>) {
        let old = self.target.replace(value);
        drop(old);
    }

    /// The access key which was pressed.
    pub fn key(&self) -> &str {
        &self.key
    }
}

/// Information pertaining to when the access key associated with an element
/// is pressed.
#[derive(Clone)]
pub struct AccessKeyEventArgs {
    base: RoutedEventArgs,
    key: String,
    is_multiple: bool,
}

ferro_routed_event_args!(AccessKeyEventArgs: RoutedEventArgs);

impl AccessKeyEventArgs {
    /// Creates args for an access key. `is_multiple` tells whether the key
    /// is not unique among the registered elements.
    pub fn new(key: &str, is_multiple: bool) -> Self {
        Self {
            base: RoutedEventArgs::with_event(AccessKeyHandler::access_key_event()),
            key: key.to_string(),
            is_multiple,
        }
    }

    /// The key that was pressed which caused this event to fire.
    pub fn key(&self) -> &str {
        &self.key
    }

    /// Whether the key pressed is not unique among the registered elements.
    pub fn is_multiple(&self) -> bool {
        self.is_multiple
    }
}

/// An access key registered for an element.
pub struct AccessKeyRegistration {
    target: WeakRef<InputElement>,
    key: String,
}

impl AccessKeyRegistration {
    /// Creates a registration.
    pub fn new(key: String, target: WeakRef<InputElement>) -> Self {
        Self { target, key }
    }

    /// The (normalized) access key.
    pub fn key(&self) -> &str {
        &self.key
    }

    /// The registered element, if it is still alive.
    pub fn get_input_element(&self) -> Option<Element> {
        self.target.upgrade()
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ProcessKeyResult {
    NoMatch,
    MoreMatches,
    LastMatch,
}

/// Handles access keys for a window.
pub struct AccessKeyHandler {
    this: Weak<AccessKeyHandler>,
    /// The registered access keys.
    registrations: RefCell<Vec<AccessKeyRegistration>>,
    /// The window to which the handler belongs.
    owner: RefCell<Option<WeakRef<InputElement>>>,
    /// Whether access keys are currently being shown.
    showing_access_keys: Cell<bool>,
    /// Whether to ignore the Alt KeyUp event.
    ignore_alt_up: Cell<bool>,
    /// Whether the AltKey is down.
    alt_is_down: Cell<bool>,
    /// Element to restore following AltKey taking focus.
    restore_focus_element: RefCell<Option<WeakRef<InputElement>>>,
    /// The window's main menu.
    main_menu: RefCell<Option<Rc<dyn IMainMenu>>>,
    main_menu_closed: RefCell<Option<Rc<dyn IDisposable>>>,
}

crate::ferro_static_type!(AccessKeyHandler);

impl AccessKeyHandler {
    ferro_routed_event!(
        /// Defines the AccessKey event: raised on the element an access key
        /// targets.
        pub fn access_key_event() -> RoutedEvent<AccessKeyEventArgs> {
            RoutedEvent::register::<AccessKeyHandler, _>("AccessKey", RoutingStrategies::BUBBLE)
        }
    );

    ferro_routed_event!(
        /// Defines the AccessKeyPressed event: raised to find the target of
        /// an access key.
        pub fn access_key_pressed_event() -> RoutedEvent<AccessKeyPressedEventArgs> {
            RoutedEvent::register::<AccessKeyHandler, _>("AccessKeyPressed", RoutingStrategies::BUBBLE)
        }
    );

}

crate::ferro_properties! { impl AccessKeyHandler {
    ferro_property!(
        /// Defines the ShowAccessKey attached property: whether access key
        /// markers are shown in a window.
        pub fn show_access_key_property() -> AttachedProperty<bool> {
            FerroProperty::register_attached_with::<AccessKeyHandler, Visual, _>(
                "ShowAccessKey",
                StyledPropertyOptions::new(false).inherits(true),
            )
        }
    );
} }

impl AccessKeyHandler {
    /// Creates an access key handler without an owner.
    pub fn new() -> Rc<AccessKeyHandler> {
        Rc::new_cyclic(|this| AccessKeyHandler {
            this: this.clone(),
            registrations: RefCell::new(Vec::new()),
            owner: RefCell::new(None),
            showing_access_keys: Cell::new(false),
            ignore_alt_up: Cell::new(false),
            alt_is_down: Cell::new(false),
            restore_focus_element: RefCell::new(None),
            main_menu: RefCell::new(None),
            main_menu_closed: RefCell::new(None),
        })
    }

    /// The number of registered access keys, including registrations of
    /// elements that are no longer alive and have not been purged yet.
    pub fn registration_count(&self) -> usize {
        self.registrations.borrow().len()
    }

    fn owner(&self) -> Option<Element> {
        self.owner.borrow().as_ref().and_then(WeakRef::upgrade)
    }

    /// The registered access keys with the elements they target (the ones
    /// that are still alive). For handlers built on this one.
    pub fn registrations(&self) -> Vec<(String, Element)> {
        self.registrations
            .borrow()
            .iter()
            .filter_map(|registration| {
                registration.get_input_element().map(|element| (registration.key().to_owned(), element))
            })
            .collect()
    }

    /// Records the owner of the handler: the first part of
    /// [`IAccessKeyHandler::set_owner`].
    ///
    /// This method can only be called once.
    fn set_owner_without_handlers(&self, owner: &Element) {
        if self.owner.borrow().is_some() {
            panic!("AccessKeyHandler owner has already been set.");
        }

        *self.owner.borrow_mut() = Some(owner.downgrade());
    }

    /// Sets the owner of the handler as [`IAccessKeyHandler::set_owner`] does, with the key
    /// and pointer handlers attached, and then runs `on_set_owner`: the `OnSetOwner` virtual
    /// of the class, which does nothing in this class and which a handler built on this one
    /// overrides (the handler of a menu item attaches its text input handler there).
    ///
    /// This method can only be called once.
    pub fn set_owner_with(&self, owner: &Element, on_set_owner: impl FnOnce(&Element)) {
        IAccessKeyHandler::set_owner(self, owner);

        on_set_owner(owner);
    }

    fn set_show_access_keys(target: &Visual, value: bool) {
        target.set_value(Self::show_access_key_property(), value)
    }

    fn source_of(e: &RoutedEventArgs) -> Option<Element> {
        e.source().and_then(|source| source.downcast::<InputElement>().ok())
    }

    /// Called when a key is pressed in the owner window, before the event
    /// reaches the focused element.
    pub fn on_preview_key_down(&self, e: &KeyEventArgs) {
        let Some(owner) = self.owner() else { return };
        let source = Self::source_of(e);

        // If the event did not originate from within the owner, ignore all
        // keyboard events.
        let is_focus_within_owner = Self::is_focus_within_owner(&owner, source.as_ref());
        if !is_focus_within_owner {
            return;
        }

        if e.key == Key::LeftAlt || e.key == Key::RightAlt {
            self.alt_is_down.set(true);

            let main_menu = self.main_menu();
            if !main_menu.as_ref().is_some_and(|menu| menu.is_open()) {
                let focus_manager = source.as_ref().and_then(|source| FocusManager::get_focus_manager(source));

                // Save the currently focused input element.
                if let Some(focused_element) = focus_manager.and_then(|manager| manager.get_focused_element()) {
                    *self.restore_focus_element.borrow_mut() = Some(focused_element.downgrade());
                }

                // When Alt is pressed without a main menu, or with a closed
                // main menu, show access key markers in the window (i.e.
                // "_File").
                self.showing_access_keys.set(is_focus_within_owner);
                Self::set_show_access_keys(&owner, is_focus_within_owner);
            } else {
                // If the Alt key is pressed and the main menu is open, close
                // the main menu.
                self.close_menu(&owner);
                self.ignore_alt_up.set(true);

                let restore_element = self.restore_focus_element.borrow_mut().take().and_then(|weak| weak.upgrade());
                if let Some(restore_element) = restore_element {
                    restore_element.focus();
                }
            }
        } else if self.alt_is_down.get() {
            self.ignore_alt_up.set(true);
        }
    }

    /// Called when a key is pressed in the owner window.
    pub fn on_key_down(&self, e: &KeyEventArgs) {
        let Some(owner) = self.owner() else { return };
        let source = Self::source_of(e);

        // If the event did not originate from within the owner, ignore all
        // keyboard events.
        if !Self::is_focus_within_owner(&owner, source.as_ref()) {
            return;
        }

        if (!e.key_modifiers.contains(KeyModifiers::ALT) || e.key_modifiers.contains(KeyModifiers::CONTROL))
            && !self.main_menu().is_some_and(|menu| menu.is_open())
        {
            return;
        }

        e.set_handled(self.process_key(e.key_symbol.as_deref(), source.as_ref()));
    }

    /// Handles the Alt/F10 keys being released in the window.
    pub fn on_preview_key_up(&self, e: &KeyEventArgs) {
        if e.key == Key::LeftAlt || e.key == Key::RightAlt {
            self.alt_is_down.set(false);

            if self.ignore_alt_up.get() {
                self.ignore_alt_up.set(false);
            } else if self.showing_access_keys.get() {
                if let Some(main_menu) = self.main_menu() {
                    main_menu.open();
                }
            }
        }
    }

    /// Handles a pointer press in the window: hides the access key markers.
    pub fn on_preview_pointer_pressed(&self, _e: &PointerPressedEventArgs) {
        if self.showing_access_keys.get() {
            if let Some(owner) = self.owner() {
                Self::set_show_access_keys(&owner, false);
            }
        }
    }

    /// Closes the main menu and performs other bookeeping.
    fn close_menu(&self, owner: &Element) {
        if let Some(main_menu) = self.main_menu() {
            main_menu.close();
        }
        self.showing_access_keys.set(false);
        Self::set_show_access_keys(owner, false);
    }

    fn main_menu_closed(&self) {
        if let Some(owner) = self.owner() {
            Self::set_show_access_keys(&owner, false);
        }
    }

    /// Processes an access key: raises the AccessKey event on the element
    /// the key targets. Returns whether a target was found.
    pub fn process_key(&self, key: Option<&str>, element: Option<&Element>) -> bool {
        let Some(key) = key.filter(|key| !key.is_empty()) else { return false };
        let key = Self::normalize_key(key);
        let sender_target = Self::get_target_for_element(element, &key);

        // Find the possible targets matching the access key
        let targets = Self::sort_by_hierarchy(self.get_targets_for_key(&key, element, sender_target));
        Self::process_key_for_targets(&key, &targets) != ProcessKeyResult::NoMatch
    }

    fn normalize_key(key: &str) -> String {
        key.to_uppercase()
    }

    fn process_key_for_targets(key: &str, targets: &[Element]) -> ProcessKeyResult {
        if targets.is_empty() {
            return ProcessKeyResult::NoMatch;
        }

        let mut is_single_target = true;
        let mut last_was_focused = false;
        let mut effective_target: Option<&Element> = None;
        let mut chosen_index = 0;

        for (i, target) in targets.iter().enumerate() {
            if !Self::is_targetable(target) {
                continue;
            }

            if effective_target.is_none() {
                effective_target = Some(target);
                chosen_index = i;
            } else {
                if last_was_focused {
                    effective_target = Some(target);
                    chosen_index = i;
                }

                is_single_target = false;
            }

            last_was_focused = target.is_focused();
        }

        let Some(effective_target) = effective_target else { return ProcessKeyResult::NoMatch };

        let args = AccessKeyEventArgs::new(key, !is_single_target);
        effective_target.raise_event(&args);

        if chosen_index == targets.len() - 1 {
            ProcessKeyResult::LastMatch
        } else {
            ProcessKeyResult::MoreMatches
        }
    }

    /// Get the list of access key targets for the sender of the keyboard
    /// event. If sender is null, pretend key was pressed in the active
    /// window.
    fn get_targets_for_key(&self, key: &str, sender: Option<&Element>, sender_target: Option<Element>) -> Vec<Element> {
        let possible_elements = self.copy_matching_and_purge_dead(key);
        let mut final_targets = Vec::with_capacity(1);

        // Go through all the possible elements, find the interesting
        // candidates
        for element in &possible_elements {
            if Some(element) != sender {
                if !Self::is_targetable(element) {
                    continue;
                }

                if let Some(target) = Self::get_target_for_element(Some(element), key) {
                    final_targets.push(target);
                }
            } else if let Some(target) = &sender_target {
                // This is the same element that sent the event so it must be
                // in the same scope. Just add it to the final targets
                final_targets.push(target.clone());
            }
        }

        final_targets
    }

    fn is_targetable(element: &InputElement) -> bool {
        element.is_effectively_enabled() && element.is_effectively_visible()
    }

    fn copy_matching_and_purge_dead(&self, key: &str) -> Vec<Element> {
        let mut registrations = self.registrations.borrow_mut();
        let mut matches = Vec::with_capacity(registrations.len());

        // collect live elements with matching key and remove dead elements
        registrations.retain(|registration| match registration.get_input_element() {
            Some(input_element) => {
                if registration.key == key {
                    matches.push(input_element);
                }
                true
            }
            None => false,
        });

        matches
    }

    /// Returns targeting information for the given element: raises the
    /// AccessKeyPressed event on it and returns the target it names.
    fn get_target_for_element(element: Option<&Element>, key: &str) -> Option<Element> {
        let element = element?;

        let args = AccessKeyPressedEventArgs::new(key);
        element.raise_event(&args);
        args.target()
    }

    /// Checks if the focused element is a descendent of the owner.
    fn is_focus_within_owner(owner: &Element, source: Option<&Element>) -> bool {
        match source {
            Some(source) => source == owner || owner.is_visual_ancestor_of(source),
            None => false,
        }
    }

    /// Sorts the list of targets according to logical ancestors in the
    /// hierarchy so that child elements, for example tab item content, are
    /// processed before the next parent item, i.e. the next tab item.
    fn sort_by_hierarchy(targets: Vec<Element>) -> Vec<Element> {
        // bail out, if there are no targets to sort
        if targets.len() <= 1 {
            return targets;
        }

        let mut sorted: Vec<Element> = Vec::with_capacity(targets.len());
        let mut queue: VecDeque<Element> = targets.into();

        while let Some(element) = queue.pop_front() {
            // if the element was already added, do nothing
            if sorted.contains(&element) {
                continue;
            }

            // add the element itself
            sorted.push(element.clone());

            // add all descendants of the element
            sorted.extend(queue.iter().filter(|child| element.is_logical_ancestor_of(Some(child))).cloned());
        }

        sorted
    }
}

impl IAccessKeyHandler for AccessKeyHandler {
    fn main_menu(&self) -> Option<Rc<dyn IMainMenu>> {
        self.main_menu.borrow().clone()
    }

    fn set_main_menu(&self, value: Option<Rc<dyn IMainMenu>>) {
        let subscription = self.main_menu_closed.borrow_mut().take();
        if let Some(subscription) = subscription {
            subscription.dispose();
        }

        let old = self.main_menu.replace(value.clone());
        drop(old);

        if let Some(main_menu) = value {
            let this = self.this.clone();
            let subscription = main_menu.closed(Rc::new(move |_| {
                if let Some(this) = this.upgrade() {
                    this.main_menu_closed();
                }
            }));
            *self.main_menu_closed.borrow_mut() = Some(subscription);
        }
    }

    fn set_owner(&self, owner: &Element) {
        self.set_owner_without_handlers(owner);

        let handler = |f: fn(&AccessKeyHandler, &KeyEventArgs)| {
            let this = self.this.clone();
            move |_: &crate::interactivity::Interactive, e: &KeyEventArgs| {
                if let Some(this) = this.upgrade() {
                    f(&this, e);
                }
            }
        };

        owner.add_handler_with(
            InputElement::key_down_event(),
            handler(Self::on_preview_key_down),
            RoutingStrategies::TUNNEL,
            false,
        );
        owner.add_handler_with(
            InputElement::key_down_event(),
            handler(Self::on_key_down),
            RoutingStrategies::BUBBLE,
            false,
        );
        owner.add_handler_with(
            InputElement::key_up_event(),
            handler(Self::on_preview_key_up),
            RoutingStrategies::TUNNEL,
            false,
        );

        let this = self.this.clone();
        owner.add_handler_with(
            InputElement::pointer_pressed_event(),
            move |_, e| {
                if let Some(this) = this.upgrade() {
                    this.on_preview_pointer_pressed(e);
                }
            },
            RoutingStrategies::TUNNEL,
            false,
        );
    }

    fn register(&self, access_key: &str, element: &Element) {
        assert!(!access_key.is_empty(), "The access key cannot be empty.");

        let key = Self::normalize_key(access_key);
        let mut registrations = self.registrations.borrow_mut();

        // remove dead elements with matching key
        registrations.retain(|registration| !(registration.key == key && registration.get_input_element().is_none()));

        registrations.push(AccessKeyRegistration::new(key, element.downgrade()));
    }

    fn unregister(&self, element: &InputElement) {
        // remove element and all dead elements
        self.registrations.borrow_mut().retain(|registration| match registration.get_input_element() {
            Some(input_element) => !std::ptr::eq::<InputElement>(&*input_element, element),
            None => false,
        });
    }
}