glory_render/
focus_scope.rs1use crate::{
3 DescriptorView, Error, NodeId, SemanticId,
4 memory::{Memory, Subtree},
5};
6
7pub const MAX_FOCUS_SCOPES: usize = 1024;
8pub const MAX_FOCUS_SCOPE_NODES: usize = 4096;
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct FocusScopeConfig {
11 pub active: bool,
12 pub loop_tab: bool,
13 pub contain_focus: bool,
14 pub auto_focus: bool,
15 pub restore_focus: bool,
16 pub initial_focus: Option<SemanticId>,
17 pub return_focus: Option<SemanticId>,
18}
19impl Default for FocusScopeConfig {
20 fn default() -> Self {
21 Self {
22 active: true,
23 loop_tab: true,
24 contain_focus: true,
25 auto_focus: true,
26 restore_focus: true,
27 initial_focus: None,
28 return_focus: None,
29 }
30 }
31}
32pub(crate) fn declared(descriptor: DescriptorView<'_>) -> bool {
33 descriptor
34 .semantics
35 .as_ref()
36 .is_some_and(|semantics| semantics.focus_scope.is_some())
37}
38pub(crate) struct Movement {
39 old: Vec<NodeId>,
40 new: Vec<NodeId>,
41 size: usize,
42}
43impl Movement {
44 pub(crate) fn commit(self, tree: &mut Memory) {
45 for root in self.old {
46 *tree.focus_scopes.get_mut(&root).expect("old scope") -= self.size;
47 }
48 for root in self.new {
49 *tree.focus_scopes.get_mut(&root).expect("new scope") += self.size;
50 }
51 }
52}
53impl Memory {
54 pub fn focus_scopes(&self) -> usize {
55 self.focus_scopes.len()
56 }
57 pub fn focus_scope_roots(&self) -> impl Iterator<Item = NodeId> + '_ {
59 self.focus_scopes.keys().copied()
60 }
61 pub fn focus_scope_nodes(&self, root: NodeId) -> Result<Option<usize>, Error> {
62 self.node(root)?;
63 Ok(self.focus_scopes.get(&root).copied())
64 }
65 pub(crate) fn admit_focus_scopes(&self, additional: usize) -> Result<(), Error> {
66 if additional > MAX_FOCUS_SCOPES - self.focus_scopes.len() {
67 return Err(Error::InvalidProps("live FocusScope budget exceeded"));
68 }
69 Ok(())
70 }
71 pub(crate) fn scope_ancestors(&self, mut parent: Option<NodeId>) -> Result<Vec<NodeId>, Error> {
72 let mut scopes = Vec::new();
73 if self.focus_scopes.is_empty() {
74 return Ok(scopes);
75 }
76 while let Some(id) = parent {
77 let node = self.node(id)?;
78 if self.focus_scopes.contains_key(&id) {
79 scopes.push(id);
80 }
81 parent = node.parent;
82 }
83 Ok(scopes)
84 }
85 pub(crate) fn scope_subtree_size(&self, root: NodeId, limit: usize) -> Result<usize, Error> {
86 if let Some(size) = self.focus_scopes.get(&root) {
87 return if *size <= limit {
88 Ok(*size)
89 } else {
90 Err(Error::InvalidPlacement(
91 "restricted FocusScope subtree exceeds its live node budget",
92 ))
93 };
94 }
95 let mut pending = vec![root];
96 let mut size = 0usize;
97 while let Some(id) = pending.pop() {
98 if id != root
99 && let Some(nested) = self.focus_scopes.get(&id)
100 {
101 size = size
102 .checked_add(*nested)
103 .ok_or(Error::InvalidPlacement("FocusScope size overflow"))?;
104 } else {
105 let node = self.node(id)?;
106 size += 1;
107 if size + pending.len() + node.children.len() > limit {
108 return Err(Error::InvalidPlacement(
109 "restricted FocusScope subtree exceeds its live node budget",
110 ));
111 }
112 pending.extend(node.children.iter().copied());
113 }
114 if size > limit {
115 return Err(Error::InvalidPlacement(
116 "restricted FocusScope subtree exceeds its live node budget",
117 ));
118 }
119 }
120 Ok(size)
121 }
122 pub(crate) fn scope_movement(
123 &self,
124 id: NodeId,
125 parent: Option<NodeId>,
126 ) -> Result<Option<Movement>, Error> {
127 if self.focus_scopes.is_empty() {
128 return Ok(None);
129 }
130 let old = self.scope_ancestors(self.node(id)?.parent)?;
131 let new = self.scope_ancestors(parent)?;
132 let leaving = old
133 .iter()
134 .filter(|id| !new.contains(id))
135 .copied()
136 .collect::<Vec<_>>();
137 let entering = new
138 .iter()
139 .filter(|id| !old.contains(id))
140 .copied()
141 .collect::<Vec<_>>();
142 if leaving.is_empty() && entering.is_empty() {
143 return Ok(None);
144 }
145 let limit = entering
146 .iter()
147 .map(|id| MAX_FOCUS_SCOPE_NODES - self.focus_scopes[id])
148 .min()
149 .unwrap_or(MAX_FOCUS_SCOPE_NODES);
150 let size = self.scope_subtree_size(id, limit)?;
151 Ok(Some(Movement {
152 old: leaving,
153 new: entering,
154 size,
155 }))
156 }
157 pub(crate) fn validate_focus_child(
158 &self,
159 child: Subtree<'_>,
160 parent: Option<NodeId>,
161 ) -> Result<(), Error> {
162 let additional = match child {
163 Subtree::New(descriptor) => usize::from(declared(descriptor)),
164 Subtree::Plan(plan) => plan.focus_scopes(),
165 Subtree::Existing(_) => 0,
166 };
167 self.admit_focus_scopes(additional)?;
168 if self.focus_scopes.is_empty() {
169 return Ok(());
170 }
171 match child {
172 Subtree::Existing(id) => {
173 self.scope_movement(id, parent)?;
174 }
175 Subtree::New(_) | Subtree::Plan(_) => {
176 let size = match child {
177 Subtree::Plan(plan) => plan.nodes().len(),
178 _ => 1,
179 };
180 for root in self.scope_ancestors(parent)? {
181 if size > MAX_FOCUS_SCOPE_NODES - self.focus_scopes[&root] {
182 return Err(Error::InvalidPlacement(
183 "restricted FocusScope subtree exceeds its live node budget",
184 ));
185 }
186 }
187 }
188 }
189 Ok(())
190 }
191}