Skip to main content

mitsuami_core/ui/
focus.rs

1//! Keyboard focus: the Tab order and focus requests.
2
3use crate::command::Command;
4use crate::widget::{NodeId, WidgetKind};
5
6use super::{Inner, Ui};
7
8impl Ui {
9    /// Moves a control ahead in the Tab order: controls with a tab index
10    /// come first, lowest index first, then everything else in tree order.
11    pub fn set_tab_index(&self, id: NodeId, index: Option<u32>) {
12        let mut inner = self.inner.borrow_mut();
13        if let Some(node) = inner.nodes.get_mut(&id) {
14            node.tab_index = index;
15            inner.focus_dirty = true;
16        }
17        drop(inner);
18        self.changed();
19    }
20
21    /// The control that has keyboard focus in `window`, if any.
22    pub fn focused(&self, window: NodeId) -> Option<NodeId> {
23        self.inner.borrow().focused.get(&window).copied()
24    }
25
26    /// The keyboard order of a window's focusable controls, as sent to the
27    /// backend.
28    pub fn focus_order(&self, window: NodeId) -> Vec<NodeId> {
29        self.inner.borrow().focus_order(window)
30    }
31
32    /// Gives the control keyboard focus, at the next commit.
33    pub fn focus(&self, id: NodeId) {
34        self.inner.borrow_mut().pending_focus.push(id);
35        self.changed();
36    }
37
38    /// Focuses a text field or text area and selects these grapheme
39    /// clusters of its text, at the next commit; a range past its end is
40    /// cut to it. Other controls are only focused.
41    pub fn select_text(&self, id: NodeId, range: std::ops::Range<usize>) {
42        let mut inner = self.inner.borrow_mut();
43        inner.pending_focus.push(id);
44        let text = inner.nodes.get(&id).is_some_and(|n| {
45            matches!(
46                n.kind,
47                WidgetKind::TextInput | WidgetKind::PasswordInput | WidgetKind::SearchInput | WidgetKind::TextArea
48            )
49        });
50        if text {
51            inner.pending_selections.push((id, range));
52        }
53        drop(inner);
54        self.changed();
55    }
56}
57
58impl Inner {
59    /// Focusable controls in reading (tree) order, explicit tab indices first.
60    fn focus_order(&self, window: NodeId) -> Vec<NodeId> {
61        fn walk(inner: &Inner, id: NodeId, out: &mut Vec<(Option<u32>, NodeId)>) {
62            let node = &inner.nodes[&id];
63            // Whether Tab reaches the toolbar is the platform's call, and a
64            // popover takes focus as its platform's does.
65            if node.style.is_hidden()
66                || matches!(node.kind, WidgetKind::ToolbarItem | WidgetKind::Popover)
67                || inner.is_hidden_page(id)
68            {
69                return;
70            }
71            if matches!(
72                node.kind,
73                WidgetKind::Button
74                    | WidgetKind::ToggleButton
75                    | WidgetKind::MenuButton
76                    | WidgetKind::SplitButton
77                    | WidgetKind::TextInput
78                    | WidgetKind::PasswordInput
79                    | WidgetKind::SearchInput
80                    | WidgetKind::TextArea
81                    | WidgetKind::Checkbox
82                    | WidgetKind::Switch
83                    | WidgetKind::Select
84                    | WidgetKind::RadioGroup
85                    | WidgetKind::Slider
86                    | WidgetKind::NumberInput
87                    | WidgetKind::ColorWell
88                    | WidgetKind::List
89                    | WidgetKind::Table
90                    | WidgetKind::Tree
91                    | WidgetKind::Sidebar
92                    | WidgetKind::Tabs
93                    | WidgetKind::DocumentTabs
94            ) || (node.kind == WidgetKind::GpuSurface && crate::find_prop!(node.props, TakesInput) == Some(true))
95            {
96                out.push((node.tab_index, id));
97            }
98            // A window's sidebar comes first: it's on the leading side, and
99            // picks what the content shows.
100            let mut children = node.native_children.clone();
101            children.sort_by_key(|c| inner.nodes[c].kind != WidgetKind::Sidebar);
102            for child in children {
103                walk(inner, child, out);
104            }
105        }
106        let mut entries = Vec::new();
107        if self.nodes.contains_key(&window) {
108            walk(self, window, &mut entries);
109        }
110        // Stable sort: explicit indices first (ascending), tree order otherwise.
111        entries.sort_by_key(|(index, _)| index.unwrap_or(u32::MAX));
112        entries.into_iter().map(|(_, id)| id).collect()
113    }
114
115    pub(super) fn sync_focus_orders(&mut self) {
116        for window in self.windows.clone() {
117            let order = self.focus_order(window);
118            if self.focus_orders.get(&window) != Some(&order) {
119                self.focus_orders.insert(window, order.clone());
120                self.pending.push(Command::SetFocusOrder { window, order });
121            }
122        }
123    }
124}