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    /// Brings the window `id` is in (or the window `id` is) forward, at
39    /// the next commit.
40    pub fn present(&self, id: NodeId) {
41        self.inner.borrow_mut().pending_present.push(id);
42        self.changed();
43    }
44
45    /// Focuses a text field or text area and selects these grapheme
46    /// clusters of its text, at the next commit; a range past its end is
47    /// cut to it. Other controls are only focused.
48    pub fn select_text(&self, id: NodeId, range: std::ops::Range<usize>) {
49        let mut inner = self.inner.borrow_mut();
50        inner.pending_focus.push(id);
51        let text = inner.nodes.get(&id).is_some_and(|n| {
52            matches!(
53                n.kind,
54                WidgetKind::TextInput | WidgetKind::PasswordInput | WidgetKind::SearchInput | WidgetKind::TextArea
55            )
56        });
57        if text {
58            inner.pending_selections.push((id, range));
59        }
60        drop(inner);
61        self.changed();
62    }
63}
64
65impl Inner {
66    /// Focusable controls in reading (tree) order, explicit tab indices first.
67    fn focus_order(&self, window: NodeId) -> Vec<NodeId> {
68        fn walk(inner: &Inner, id: NodeId, out: &mut Vec<(Option<u32>, NodeId)>) {
69            let node = &inner.nodes[&id];
70            // Whether Tab reaches the toolbar is the platform's call, and a
71            // popover takes focus as its platform's does.
72            if node.style.is_hidden()
73                || matches!(node.kind, WidgetKind::ToolbarItem | WidgetKind::Popover)
74                || inner.is_hidden_page(id)
75            {
76                return;
77            }
78            if matches!(
79                node.kind,
80                WidgetKind::Button
81                    | WidgetKind::ToggleButton
82                    | WidgetKind::MenuButton
83                    | WidgetKind::SplitButton
84                    | WidgetKind::TextInput
85                    | WidgetKind::PasswordInput
86                    | WidgetKind::SearchInput
87                    | WidgetKind::TextArea
88                    | WidgetKind::Checkbox
89                    | WidgetKind::Switch
90                    | WidgetKind::Select
91                    | WidgetKind::RadioGroup
92                    | WidgetKind::Slider
93                    | WidgetKind::NumberInput
94                    | WidgetKind::ColorWell
95                    | WidgetKind::List
96                    | WidgetKind::Table
97                    | WidgetKind::Tree
98                    | WidgetKind::Sidebar
99                    | WidgetKind::Tabs
100                    | WidgetKind::DocumentTabs
101            ) || (node.kind == WidgetKind::GpuSurface && crate::find_prop!(node.props, TakesInput) == Some(true))
102            {
103                out.push((node.tab_index, id));
104            }
105            // A window's sidebar comes first: it's on the leading side, and
106            // picks what the content shows.
107            let mut children = node.native_children.clone();
108            children.sort_by_key(|c| inner.nodes[c].kind != WidgetKind::Sidebar);
109            for child in children {
110                walk(inner, child, out);
111            }
112        }
113        let mut entries = Vec::new();
114        if self.nodes.contains_key(&window) {
115            walk(self, window, &mut entries);
116        }
117        // Stable sort: explicit indices first (ascending), tree order otherwise.
118        entries.sort_by_key(|(index, _)| index.unwrap_or(u32::MAX));
119        entries.into_iter().map(|(_, id)| id).collect()
120    }
121
122    pub(super) fn sync_focus_orders(&mut self) {
123        for window in self.windows.clone() {
124            let order = self.focus_order(window);
125            if self.focus_orders.get(&window) != Some(&order) {
126                self.focus_orders.insert(window, order.clone());
127                self.pending.push(Command::SetFocusOrder { window, order });
128            }
129        }
130    }
131}