1use super::*;
4
5impl AppRuntime {
6 pub fn hit_test_input(
8 blueprint: &NodeBlueprint,
9 layout: &LayoutNode,
10 px: f32,
11 py: f32,
12 ) -> Option<InputHitTarget> {
13 let mut candidates = Vec::new();
14 let mut counter = 0;
15 Self::collect_input_candidates(blueprint, layout, &mut counter, &mut candidates);
16
17 for (target, border_box) in candidates.into_iter().rev() {
18 if border_box.contains_point(px, py) {
19 return Some(target);
20 }
21 }
22 None
23 }
24
25 pub(crate) fn collect_input_candidates(
26 blueprint: &NodeBlueprint,
27 layout: &LayoutNode,
28 counter: &mut usize,
29 out: &mut Vec<(InputHitTarget, strata_layout::Rect)>,
30 ) {
31 Self::collect_input_candidates_guarded(blueprint, layout, counter, out, 0);
32 }
33
34 pub(crate) fn collect_input_candidates_guarded(
35 blueprint: &NodeBlueprint,
36 layout: &LayoutNode,
37 counter: &mut usize,
38 out: &mut Vec<(InputHitTarget, strata_layout::Rect)>,
39 depth: usize,
40 ) {
41 if depth >= 256 {
42 return;
43 }
44 match blueprint {
45 NodeBlueprint::Element {
46 tag,
47 classes,
48 attributes,
49 children,
50 ..
51 } => {
52 let is_input = matches!(tag, Tag::Input | Tag::Textarea);
53 if is_input {
54 let this_idx = *counter;
55 *counter += 1;
56
57 let mut current_value = String::new();
58 let mut input_id = None;
59 let mut on_input = None;
60 let mut on_change = None;
61 let mut on_submit = None;
62 let mut on_focus = None;
63 let mut on_blur = None;
64
65 for attr in attributes {
66 match attr {
67 strata_dom::Attribute::Property { key, value } => {
68 if key == "value" {
69 current_value = value.clone();
70 } else if key == "id" {
71 input_id = Some(value.clone());
72 }
73 }
74 strata_dom::Attribute::EventListener { event, handler } => {
75 match event {
76 strata_dom::EventType::Input => {
77 on_input = Some(handler.clone())
78 }
79 strata_dom::EventType::Change => {
80 on_change = Some(handler.clone())
81 }
82 strata_dom::EventType::Submit => {
83 on_submit = Some(handler.clone())
84 }
85 strata_dom::EventType::Focus => {
86 on_focus = Some(handler.clone())
87 }
88 strata_dom::EventType::Blur => on_blur = Some(handler.clone()),
89 _ => {}
90 }
91 }
92 _ => {}
93 }
94 }
95
96 let is_mono = strata_layout::is_monospace_family(layout.style.font_family);
97 out.push((
98 InputHitTarget {
99 input_index: this_idx,
100 input_id,
101 entity_id: if layout.entity_id.is_valid() {
102 Some(layout.entity_id)
103 } else {
104 None
105 },
106 current_value,
107 font_size: layout.style.font_size,
108 line_height: layout.style.line_height,
109 is_mono,
110 font_family: layout.style.font_family,
111 content_box_x: layout.geometry.content_box.x,
112 content_box_y: layout.geometry.content_box.y,
113 content_box_width: layout.geometry.content_box.width,
114 on_input,
115 on_change,
116 on_submit,
117 on_focus,
118 on_blur,
119 },
120 layout.geometry.border_box,
121 ));
122 }
123
124 let start_idx = out.len();
125 let mut flat = Vec::new();
126 for c in children {
127 Self::flatten_blueprints(c, &mut flat);
128 }
129
130 for (&child_bp, child_layout) in flat.iter().zip(layout.children.iter()) {
131 Self::collect_input_candidates_guarded(
132 child_bp,
133 child_layout,
134 counter,
135 out,
136 depth + 1,
137 );
138 }
139
140 if !is_input && out.len() > start_idx {
142 let has_cursor_text = classes
143 .iter()
144 .any(|c| c.split_whitespace().any(|tok| tok == "cursor-text"))
145 || layout.style.cursor == strata_style::types::Cursor::Text
146 || matches!(tag, Tag::Label);
147 if has_cursor_text {
148 let child_target = out[start_idx].0.clone();
149 out.insert(start_idx, (child_target, layout.geometry.border_box));
150 }
151 }
152 }
153 NodeBlueprint::Fragment(children) => {
154 for c in children {
155 let mut flat = Vec::new();
156 Self::flatten_blueprints(c, &mut flat);
157 for (&child_bp, child_layout) in flat.iter().zip(layout.children.iter()) {
158 Self::collect_input_candidates_guarded(
159 child_bp,
160 child_layout,
161 counter,
162 out,
163 depth + 1,
164 );
165 }
166 }
167 }
168 _ => {}
169 }
170 }
171
172 pub fn find_input_layout<'a>(
174 blueprint: &NodeBlueprint,
175 layout: &'a LayoutNode,
176 target_index: usize,
177 target_id: Option<&str>,
178 counter: &mut usize,
179 ) -> Option<&'a LayoutNode> {
180 Self::find_input_layout_guarded(blueprint, layout, target_index, target_id, counter, 0)
181 }
182
183 pub(crate) fn find_input_layout_guarded<'a>(
184 blueprint: &NodeBlueprint,
185 layout: &'a LayoutNode,
186 target_index: usize,
187 target_id: Option<&str>,
188 counter: &mut usize,
189 depth: usize,
190 ) -> Option<&'a LayoutNode> {
191 if depth >= 256 {
192 return None;
193 }
194 match blueprint {
195 NodeBlueprint::Element {
196 tag,
197 attributes,
198 children,
199 ..
200 } => {
201 let is_input = matches!(tag, Tag::Input | Tag::Textarea);
202 if is_input {
203 let this_idx = *counter;
204 *counter += 1;
205
206 let mut matches_id = false;
207 if let Some(tid) = target_id {
208 for attr in attributes {
209 if let strata_dom::Attribute::Property { key, value } = attr {
210 if key == "id" && value == tid {
211 matches_id = true;
212 break;
213 }
214 }
215 }
216 }
217
218 let is_match = if target_id.is_some() {
219 matches_id
220 } else {
221 this_idx == target_index
222 };
223
224 if is_match {
225 return Some(layout);
226 }
227 }
228
229 let mut flat = Vec::new();
230 for c in children {
231 Self::flatten_blueprints(c, &mut flat);
232 }
233
234 for (&child_bp, child_layout) in flat.iter().zip(layout.children.iter()) {
235 if let Some(found) = Self::find_input_layout_guarded(
236 child_bp,
237 child_layout,
238 target_index,
239 target_id,
240 counter,
241 depth + 1,
242 ) {
243 return Some(found);
244 }
245 }
246
247 None
248 }
249 NodeBlueprint::Fragment(children) => {
250 for c in children {
251 let mut flat = Vec::new();
252 Self::flatten_blueprints(c, &mut flat);
253 for (&child_bp, child_layout) in flat.iter().zip(layout.children.iter()) {
254 if let Some(found) = Self::find_input_layout_guarded(
255 child_bp,
256 child_layout,
257 target_index,
258 target_id,
259 counter,
260 depth + 1,
261 ) {
262 return Some(found);
263 }
264 }
265 }
266 None
267 }
268 _ => None,
269 }
270 }
271
272 pub fn find_input_layout_mut<'a>(
274 blueprint: &NodeBlueprint,
275 layout: &'a mut LayoutNode,
276 target_index: usize,
277 target_id: Option<&str>,
278 counter: &mut usize,
279 ) -> Option<&'a mut LayoutNode> {
280 Self::find_input_layout_mut_guarded(blueprint, layout, target_index, target_id, counter, 0)
281 }
282
283 pub(crate) fn find_input_layout_mut_guarded<'a>(
284 blueprint: &NodeBlueprint,
285 layout: &'a mut LayoutNode,
286 target_index: usize,
287 target_id: Option<&str>,
288 counter: &mut usize,
289 depth: usize,
290 ) -> Option<&'a mut LayoutNode> {
291 if depth >= 256 {
292 return None;
293 }
294 match blueprint {
295 NodeBlueprint::Element {
296 tag,
297 attributes,
298 children,
299 ..
300 } => {
301 let is_input = matches!(tag, Tag::Input | Tag::Textarea);
302 if is_input {
303 let this_idx = *counter;
304 *counter += 1;
305
306 let mut matches_id = false;
307 if let Some(tid) = target_id {
308 for attr in attributes {
309 if let strata_dom::Attribute::Property { key, value } = attr {
310 if key == "id" && value == tid {
311 matches_id = true;
312 break;
313 }
314 }
315 }
316 }
317
318 let is_match = if target_id.is_some() {
319 matches_id
320 } else {
321 this_idx == target_index
322 };
323
324 if is_match {
325 return Some(layout);
326 }
327 }
328
329 let mut flat = Vec::new();
330 for c in children {
331 Self::flatten_blueprints(c, &mut flat);
332 }
333
334 for (&child_bp, child_layout) in flat.iter().zip(layout.children.iter_mut()) {
335 if let Some(found) = Self::find_input_layout_mut_guarded(
336 child_bp,
337 child_layout,
338 target_index,
339 target_id,
340 counter,
341 depth + 1,
342 ) {
343 return Some(found);
344 }
345 }
346
347 None
348 }
349 NodeBlueprint::Fragment(children) => {
350 let mut flat = Vec::new();
351 for c in children {
352 Self::flatten_blueprints(c, &mut flat);
353 }
354 for (&child_bp, child_layout) in flat.iter().zip(layout.children.iter_mut()) {
355 if let Some(found) = Self::find_input_layout_mut_guarded(
356 child_bp,
357 child_layout,
358 target_index,
359 target_id,
360 counter,
361 depth + 1,
362 ) {
363 return Some(found);
364 }
365 }
366 None
367 }
368 _ => None,
369 }
370 }
371
372 pub fn find_input_blueprint<'a>(
374 blueprint: &'a NodeBlueprint,
375 target_index: usize,
376 target_id: Option<&str>,
377 counter: &mut usize,
378 ) -> Option<&'a NodeBlueprint> {
379 Self::find_input_blueprint_guarded(blueprint, target_index, target_id, counter, 0)
380 }
381
382 pub(crate) fn find_input_blueprint_guarded<'a>(
383 blueprint: &'a NodeBlueprint,
384 target_index: usize,
385 target_id: Option<&str>,
386 counter: &mut usize,
387 depth: usize,
388 ) -> Option<&'a NodeBlueprint> {
389 if depth >= 256 {
390 return None;
391 }
392 match blueprint {
393 NodeBlueprint::Element {
394 tag,
395 attributes,
396 children,
397 ..
398 } => {
399 let is_input = matches!(tag, Tag::Input | Tag::Textarea);
400 if is_input {
401 let this_idx = *counter;
402 *counter += 1;
403
404 let mut matches_id = false;
405 if let Some(tid) = target_id {
406 for attr in attributes {
407 if let strata_dom::Attribute::Property { key, value } = attr {
408 if key == "id" && value == tid {
409 matches_id = true;
410 break;
411 }
412 }
413 }
414 }
415
416 let is_match = if target_id.is_some() {
417 matches_id
418 } else {
419 this_idx == target_index
420 };
421
422 if is_match {
423 return Some(blueprint);
424 }
425 }
426
427 let mut flat = Vec::new();
428 for c in children {
429 Self::flatten_blueprints(c, &mut flat);
430 }
431
432 for child_bp in flat {
433 if let Some(found) = Self::find_input_blueprint_guarded(
434 child_bp,
435 target_index,
436 target_id,
437 counter,
438 depth + 1,
439 ) {
440 return Some(found);
441 }
442 }
443
444 None
445 }
446 NodeBlueprint::Fragment(children) => {
447 for c in children {
448 let mut flat = Vec::new();
449 Self::flatten_blueprints(c, &mut flat);
450 for child_bp in flat {
451 if let Some(found) = Self::find_input_blueprint_guarded(
452 child_bp,
453 target_index,
454 target_id,
455 counter,
456 depth + 1,
457 ) {
458 return Some(found);
459 }
460 }
461 }
462 None
463 }
464 _ => None,
465 }
466 }
467}