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.
64            if node.style.is_hidden() || node.kind == WidgetKind::ToolbarItem || inner.is_hidden_page(id) {
65                return;
66            }
67            if matches!(
68                node.kind,
69                WidgetKind::Button
70                    | WidgetKind::ToggleButton
71                    | WidgetKind::MenuButton
72                    | WidgetKind::TextInput
73                    | WidgetKind::PasswordInput
74                    | WidgetKind::SearchInput
75                    | WidgetKind::TextArea
76                    | WidgetKind::Checkbox
77                    | WidgetKind::Switch
78                    | WidgetKind::Select
79                    | WidgetKind::RadioGroup
80                    | WidgetKind::Slider
81                    | WidgetKind::NumberInput
82                    | WidgetKind::List
83                    | WidgetKind::Table
84                    | WidgetKind::Sidebar
85                    | WidgetKind::Tabs
86            ) || (node.kind == WidgetKind::GpuSurface && crate::find_prop!(node.props, TakesInput) == Some(true))
87            {
88                out.push((node.tab_index, id));
89            }
90            // A window's sidebar comes first: it's on the leading side, and
91            // picks what the content shows.
92            let mut children = node.native_children.clone();
93            children.sort_by_key(|c| inner.nodes[c].kind != WidgetKind::Sidebar);
94            for child in children {
95                walk(inner, child, out);
96            }
97        }
98        let mut entries = Vec::new();
99        if self.nodes.contains_key(&window) {
100            walk(self, window, &mut entries);
101        }
102        // Stable sort: explicit indices first (ascending), tree order otherwise.
103        entries.sort_by_key(|(index, _)| index.unwrap_or(u32::MAX));
104        entries.into_iter().map(|(_, id)| id).collect()
105    }
106
107    pub(super) fn sync_focus_orders(&mut self) {
108        for window in self.windows.clone() {
109            let order = self.focus_order(window);
110            if self.focus_orders.get(&window) != Some(&order) {
111                self.focus_orders.insert(window, order.clone());
112                self.pending.push(Command::SetFocusOrder { window, order });
113            }
114        }
115    }
116}