cranpose_ui/
focus_order.rs1use cranpose_core::NodeId;
2use cranpose_foundation::{SemanticsConfiguration, SemanticsWidgetRole};
3use cranpose_ui_graphics::Rect;
4
5use crate::layout::{LayoutBox, LayoutTree};
6
7#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct FocusEntry {
10 pub node_id: NodeId,
12 pub rect: Rect,
14}
15
16impl FocusEntry {
17 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
26pub fn set_focus_order(entries: Vec<FocusEntry>) {
30 crate::render_state::with_focus_dispatch(|state| state.set_focus_order(entries));
31}
32
33pub(crate) fn request_focus_move(direction: crate::FocusDirection) {
38 crate::render_state::with_focus_dispatch(|state| state.request_move(direction));
39}
40
41pub 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
49pub 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
54pub fn focus_order_len() -> usize {
56 with_focus_order(<[FocusEntry]>::len)
57}
58
59pub 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
95pub 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}