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