Skip to main content

ferroui_base/input/
keyboard_navigation.rs

1use super::{InputElement, KeyboardNavigationMode};
2use crate::{ferro_property, AttachedProperty, FerroProperty, Ref, StyledElement};
3
4/// Defines attached properties that control keyboard navigation behaviour
5/// for a container.
6pub struct KeyboardNavigation;
7
8crate::ferro_static_type!(KeyboardNavigation);
9
10crate::ferro_properties! { impl KeyboardNavigation {
11    ferro_property!(
12        /// Defines the `TabIndex` attached property.
13        pub fn tab_index_property() -> AttachedProperty<i32> {
14            FerroProperty::register_attached::<KeyboardNavigation, StyledElement, _>("TabIndex", i32::MAX)
15        }
16    );
17
18    ferro_property!(
19        /// Defines the `TabNavigation` attached property.
20        ///
21        /// It defines how pressing the Tab key causes focus to be navigated
22        /// between the children of the container.
23        pub fn tab_navigation_property() -> AttachedProperty<KeyboardNavigationMode> {
24            FerroProperty::register_attached::<KeyboardNavigation, InputElement, _>(
25                "TabNavigation",
26                KeyboardNavigationMode::Continue,
27            )
28        }
29    );
30
31    ferro_property!(
32        /// Defines the `TabOnceActiveElement` attached property.
33        ///
34        /// When focus enters a container which has its `TabNavigation`
35        /// attached property set to `Once`, this property defines to which
36        /// child the focus should move.
37        pub fn tab_once_active_element_property() -> AttachedProperty<Option<Ref<InputElement>>> {
38            FerroProperty::register_attached::<KeyboardNavigation, InputElement, _>("TabOnceActiveElement", None)
39        }
40    );
41
42    ferro_property!(
43        /// Defines the `IsTabStop` attached property.
44        ///
45        /// It determines if the control can receive focus by tab navigation.
46        pub fn is_tab_stop_property() -> AttachedProperty<bool> {
47            FerroProperty::register_attached::<KeyboardNavigation, InputElement, _>("IsTabStop", true)
48        }
49    );
50} }
51
52impl KeyboardNavigation {
53    /// Gets the `TabIndex` attached property for an element.
54    pub fn get_tab_index(element: &InputElement) -> i32 {
55        element.get_value(Self::tab_index_property())
56    }
57
58    /// Sets the `TabIndex` attached property for an element.
59    pub fn set_tab_index(element: &InputElement, value: i32) {
60        element.set_value(Self::tab_index_property(), value)
61    }
62
63    /// Gets the `TabNavigation` attached property for a container.
64    pub fn get_tab_navigation(element: &InputElement) -> KeyboardNavigationMode {
65        element.get_value(Self::tab_navigation_property())
66    }
67
68    /// Sets the `TabNavigation` attached property for a container.
69    pub fn set_tab_navigation(element: &InputElement, value: KeyboardNavigationMode) {
70        element.set_value(Self::tab_navigation_property(), value)
71    }
72
73    /// Gets the `TabOnceActiveElement` attached property for a container.
74    pub fn get_tab_once_active_element(element: &InputElement) -> Option<Ref<InputElement>> {
75        element.get_value(Self::tab_once_active_element_property())
76    }
77
78    /// Sets the `TabOnceActiveElement` attached property for a container.
79    pub fn set_tab_once_active_element(element: &InputElement, value: Option<Ref<InputElement>>) {
80        element.set_value(Self::tab_once_active_element_property(), value)
81    }
82
83    /// Sets the `IsTabStop` attached property for an element.
84    pub fn set_is_tab_stop(element: &InputElement, value: bool) {
85        element.set_value(Self::is_tab_stop_property(), value)
86    }
87
88    /// Gets the `IsTabStop` attached property for an element.
89    pub fn get_is_tab_stop(element: &InputElement) -> bool {
90        element.get_value(Self::is_tab_stop_property())
91    }
92}