Skip to main content

cranpose_ui/
focus_order.rs

1use cranpose_core::NodeId;
2use cranpose_foundation::{SemanticsConfiguration, SemanticsWidgetRole};
3use cranpose_ui_graphics::Rect;
4
5use crate::layout::{LayoutBox, LayoutTree};
6
7/// One focus target, with the bounds layout gave it.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct FocusEntry {
10    /// Id of the layout node that takes focus.
11    pub node_id: NodeId,
12    /// Bounds of the node in root coordinates, in logical pixels.
13    pub rect: Rect,
14}
15
16impl FocusEntry {
17    /// Returns the center point of [`Self::rect`] in root coordinates, in logical pixels.
18    pub fn center(&self) -> (f32, f32) {
19        (
20            self.rect.x + self.rect.width * 0.5,
21            self.rect.y + self.rect.height * 0.5,
22        )
23    }
24}
25
26/// Replaces the focus order a later [`crate::FocusManager`] move reads. The
27/// order belongs to the current [`crate::AppContext`]. Keyboard navigation
28/// publishes the order from the latest layout before choosing a target.
29pub fn set_focus_order(entries: Vec<FocusEntry>) {
30    crate::render_state::with_focus_dispatch(|state| state.set_focus_order(entries));
31}
32
33/// Asks for focus to move one step in `direction` once the event at hand is
34/// handled. A keyboard action runs inside its key event, before the app
35/// shell knows the layout's focus order; the shell publishes it and moves
36/// afterwards, as it does for Tab.
37pub(crate) fn request_focus_move(direction: crate::FocusDirection) {
38    crate::render_state::with_focus_dispatch(|state| state.request_move(direction));
39}
40
41/// Takes the move a keyboard action asked for, which the app shell makes
42/// after each key and keyboard action event.
43pub fn take_requested_focus_move() -> Option<crate::FocusDirection> {
44    crate::render_state::with_focus_dispatch(
45        crate::focus_dispatch::FocusInvalidationState::take_requested_move,
46    )
47}
48
49/// Reads the published focus order.
50pub fn with_focus_order<T>(reader: impl FnOnce(&[FocusEntry]) -> T) -> T {
51    crate::render_state::with_focus_dispatch(|state| state.with_focus_order(reader))
52}
53
54/// How many focus targets the last published order holds.
55pub fn focus_order_len() -> usize {
56    with_focus_order(<[FocusEntry]>::len)
57}
58
59/// Walks `tree` in the order the layout pass placed it and keeps the nodes
60/// that registered a focus target and take space on screen.
61pub fn collect_focus_order(tree: &LayoutTree) -> Vec<FocusEntry> {
62    let mut entries = Vec::new();
63    collect_from_box(focus_root(tree.root()), &mut entries);
64    entries
65}
66
67fn collect_from_box(layout_box: &LayoutBox, entries: &mut Vec<FocusEntry>) {
68    let config = layout_box.node_data.semantics();
69    if config.is_some_and(|config| config.hidden) {
70        return;
71    }
72    if crate::focus_dispatch::has_focus_target(layout_box.node_id)
73        && takes_space(layout_box.rect)
74        && config.is_none_or(|config| config.enabled)
75    {
76        entries.push(FocusEntry {
77            node_id: layout_box.node_id,
78            rect: layout_box.rect,
79        });
80    }
81    for child in &layout_box.children {
82        collect_from_box(child, entries);
83    }
84}
85
86pub(crate) fn takes_space(rect: Rect) -> bool {
87    rect.width > 0.0
88        && rect.height > 0.0
89        && rect.x.is_finite()
90        && rect.y.is_finite()
91        && rect.width.is_finite()
92        && rect.height.is_finite()
93}
94
95/// The focus targets under one node, in the order layout gave them. An arrow
96/// key inside a selectable group moves among these and no others.
97pub fn collect_focus_order_under(tree: &LayoutTree, node_id: NodeId) -> Vec<FocusEntry> {
98    let mut entries = Vec::new();
99    if let Some(layout_box) = find_box(focus_root(tree.root()), node_id) {
100        collect_from_box(layout_box, &mut entries);
101    }
102    entries
103}
104
105pub(crate) fn focus_root(root: &LayoutBox) -> &LayoutBox {
106    top_modal(root).unwrap_or(root)
107}
108
109fn top_modal(layout_box: &LayoutBox) -> Option<&LayoutBox> {
110    let config = layout_box.node_data.semantics();
111    if config.is_some_and(|config| config.hidden) {
112        return None;
113    }
114    layout_box
115        .children
116        .iter()
117        .rev()
118        .find_map(top_modal)
119        .or_else(|| {
120            (takes_space(layout_box.rect) && config.is_some_and(|config| config.is_modal))
121                .then_some(layout_box)
122        })
123}
124
125pub(crate) fn is_selectable_group(config: &SemanticsConfiguration) -> bool {
126    config.selectable_group
127        || matches!(
128            config.role,
129            Some(
130                SemanticsWidgetRole::Menu
131                    | SemanticsWidgetRole::RadioGroup
132                    | SemanticsWidgetRole::TabBar
133            )
134        )
135}
136
137pub(crate) fn find_box(layout_box: &LayoutBox, node_id: NodeId) -> Option<&LayoutBox> {
138    if layout_box.node_id == node_id {
139        return Some(layout_box);
140    }
141    layout_box
142        .children
143        .iter()
144        .find_map(|child| find_box(child, node_id))
145}