1use super::*;
4
5impl AppRuntime {
6 pub fn current_cursor(&self) -> strata_platform::window::CursorIcon {
8 self.current_cursor
9 }
10
11 pub fn dispatch_pointer_down_action(&mut self, px: f32, py: f32) -> PointerDownAction {
14 self.dispatch_pointer_down_action_with_modifiers(
15 px,
16 py,
17 strata_platform::Modifiers::default(),
18 )
19 }
20
21 pub fn dispatch_pointer_down(&mut self, px: f32, py: f32) -> bool {
24 self.dispatch_pointer_down_action(px, py).is_handled()
25 }
26
27 pub fn dispatch_pointer_down_with_modifiers(
31 &mut self,
32 px: f32,
33 py: f32,
34 modifiers: strata_platform::Modifiers,
35 ) -> bool {
36 self.dispatch_pointer_down_action_with_modifiers(px, py, modifiers)
37 .is_handled()
38 }
39
40 pub fn dispatch_pointer_down_action_with_modifiers(
43 &mut self,
44 px: f32,
45 py: f32,
46 modifiers: strata_platform::Modifiers,
47 ) -> PointerDownAction {
48 self.dispatch_pointer_down_action_with_button_and_modifiers(px, py, 0, modifiers)
49 }
50
51 pub fn dispatch_pointer_down_action_with_button_and_modifiers(
55 &mut self,
56 px: f32,
57 py: f32,
58 button: i16,
59 modifiers: strata_platform::Modifiers,
60 ) -> PointerDownAction {
61 if Self::hit_test_property(
64 &self.root_blueprint,
65 &self.layout_root,
66 px,
67 py,
68 "data-keep-focus",
69 )
70 .is_some()
71 {
72 self.drag_selection_active = false;
73 return PointerDownAction::Ignored;
74 }
75
76 let mut input_handled = false;
77 if button == 0 {
78 if let Some(target) =
79 Self::hit_test_input(&self.root_blueprint, &self.layout_root, px, py)
80 {
81 let target_key = target
82 .input_id
83 .clone()
84 .unwrap_or_else(|| format!("input:{}", target.input_index));
85 let text_val = if let Some(ref active) = self.active_focus {
86 let current_key = active
87 .input_id
88 .clone()
89 .unwrap_or_else(|| format!("input:{}", active.input_index));
90 if current_key == target_key {
91 active.state.text.get()
92 } else {
93 target.current_value.clone()
94 }
95 } else {
96 target.current_value.clone()
97 };
98 let is_password = self.input_is_password(
99 target.input_index,
100 target.input_id.as_deref(),
101 target.entity_id,
102 );
103 let measure_val = if is_password {
104 Self::mask_password_text(&text_val)
105 } else {
106 text_val
107 };
108 let active_scroll_x = self
109 .active_focus
110 .as_ref()
111 .and_then(|active| {
112 let current_key = active
113 .input_id
114 .clone()
115 .unwrap_or_else(|| format!("input:{}", active.input_index));
116 if current_key == target_key {
117 Some(active.state.scroll_x.get())
118 } else {
119 None
120 }
121 })
122 .unwrap_or(0.0);
123 let content_box_width = target.content_box_width;
124 let rel_x = if px < target.content_box_x {
125 (active_scroll_x + (px - target.content_box_x)).max(0.0)
126 } else if content_box_width > 0.0 && px > target.content_box_x + content_box_width
127 {
128 100000.0
129 } else {
130 (px - target.content_box_x + active_scroll_x).max(0.0)
131 };
132 let char_idx = Self::calculate_exact_char_index_at_offset(
133 &measure_val,
134 rel_x,
135 target.font_size,
136 target.line_height,
137 target.is_mono,
138 target.font_family,
139 );
140
141 let now = std::time::Instant::now();
142 let click_count = if let Some((last_time, last_x, last_y, count)) =
143 self.last_click_info
144 {
145 if now.duration_since(last_time).as_millis() < 400
146 && (px - last_x).abs() < 5.0
147 && (py - last_y).abs() < 5.0
148 {
149 count + 1
150 } else {
151 1
152 }
153 } else {
154 1
155 };
156 self.last_click_info = Some((now, px, py, click_count));
157 self.reset_caret_blink();
158
159 let target_key = target
160 .input_id
161 .clone()
162 .unwrap_or_else(|| format!("input:{}", target.input_index));
163
164 if let Some(ref mut active) = self.active_focus {
166 let current_key = active
167 .input_id
168 .clone()
169 .unwrap_or_else(|| format!("input:{}", active.input_index));
170 if current_key == target_key {
171 if modifiers.shift {
172 active.state.update_selection_drag(char_idx);
174 self.drag_selection_active = true;
175 } else if click_count == 2 {
176 if is_password {
177 active.state.select_all();
178 } else {
179 active.state.select_word_at_index(char_idx);
180 }
181 self.drag_selection_active = false;
182 } else if click_count == 3 {
183 active.state.select_line_at_index(char_idx);
184 self.drag_selection_active = false;
185 } else if click_count >= 4 {
186 active.state.select_all();
187 self.drag_selection_active = false;
188 } else {
189 active.state.cursor_index.set(char_idx);
190 active.state.selection_anchor.set(Some(char_idx));
191 self.drag_selection_active = true;
192 }
193 self.sync_active_input_layout();
194 return PointerDownAction::CaretMoved;
195 }
196
197 self.input_states.insert(current_key, active.state.clone());
199 if let Some(ref blur_cb) = active.on_blur {
200 blur_cb(strata_dom::Event::new(strata_dom::EventType::Blur));
201 }
202 }
203
204 let state = self.input_states.remove(&target_key).unwrap_or_else(|| {
206 strata_dom::text_input::TextInputState::with_text(&target.current_value)
207 });
208
209 if state.text.get().is_empty() && !target.current_value.is_empty() {
211 state.text.set(target.current_value.clone());
212 }
213
214 if click_count == 2 {
215 if is_password {
216 state.select_all();
217 } else {
218 state.select_word_at_index(char_idx);
219 }
220 self.drag_selection_active = false;
221 } else if click_count == 3 {
222 state.select_line_at_index(char_idx);
223 self.drag_selection_active = false;
224 } else if click_count >= 4 {
225 state.select_all();
226 self.drag_selection_active = false;
227 } else {
228 state.cursor_index.set(char_idx);
229 state.selection_anchor.set(Some(char_idx));
230 self.drag_selection_active = true;
231 }
232 state.is_focused.set(true);
233
234 if let Some(ref focus_cb) = target.on_focus {
235 focus_cb(strata_dom::Event::new(strata_dom::EventType::Focus));
236 }
237
238 self.active_focus = Some(ActiveInput {
239 input_index: target.input_index,
240 input_id: target.input_id,
241 entity_id: target.entity_id,
242 state,
243 on_input: target.on_input,
244 on_change: target.on_change,
245 on_submit: target.on_submit,
246 on_blur: target.on_blur,
247 });
248 self.sync_active_input_layout();
249 input_handled = true;
250 } else if let Some(active) = self.active_focus.take() {
251 active.state.scroll_x.set(0.0);
252 let key = active
253 .input_id
254 .clone()
255 .unwrap_or_else(|| format!("input:{}", active.input_index));
256 self.input_states.insert(key, active.state.clone());
257 self.drag_selection_active = false;
258 self.last_click_info = None;
259 if let Some(ref blur_cb) = active.on_blur {
260 blur_cb(strata_dom::Event::new(strata_dom::EventType::Blur));
261 }
262 self.sync_active_input_layout();
263 input_handled = true;
264 } else {
265 self.drag_selection_active = false;
266 }
267 }
268
269 let pre_open_outside_ids = strata_dom::hooks::snapshot_open_click_outside_ids();
271 let mut inside_click_stopped = self.point_hits_non_backdrop_top_layer_portal(px, py);
272 self.pointer_capture_target = None;
273 let mut dom_handled = false;
274 let mut captured_target: Option<(Vec<usize>, bool)> = None;
275 if let Some((handler, target_box, eid)) = Self::hit_test_event_with_box_and_entity(
276 &self.root_blueprint,
277 &self.layout_root,
278 px,
279 py,
280 strata_dom::EventType::PointerDown,
281 ) {
282 let target_box_copy = *target_box;
283 let ev = self.create_mouse_event(
284 strata_dom::EventType::PointerDown,
285 px,
286 py,
287 button,
288 1,
289 Some(&target_box_copy),
290 );
291 let capture_req = ev.pointer_capture_requested.clone();
292 let stopped_cell = ev.propagation_stopped.clone();
293 let prevented_cell = ev.default_prevented.clone();
294 handler(ev);
295 dom_handled = true;
296 if stopped_cell.get() || prevented_cell.get() {
297 inside_click_stopped = true;
298 }
299 if let Some(target_path) = self.layout_root.find_path_by_entity_id(eid) {
300 match capture_req.get() {
301 Some(true) => captured_target = Some((target_path, true)),
302 Some(false) => {}
303 None => captured_target = Some((target_path, false)),
304 }
305 }
306 }
307 if let Some((handler, target_box, eid)) = Self::hit_test_event_with_box_and_entity(
308 &self.root_blueprint,
309 &self.layout_root,
310 px,
311 py,
312 strata_dom::EventType::MouseDown,
313 ) {
314 let target_box_copy = *target_box;
315 let ev = self.create_mouse_event(
316 strata_dom::EventType::MouseDown,
317 px,
318 py,
319 button,
320 1,
321 Some(&target_box_copy),
322 );
323 let capture_req = ev.pointer_capture_requested.clone();
324 let stopped_cell = ev.propagation_stopped.clone();
325 let prevented_cell = ev.default_prevented.clone();
326 handler(ev);
327 dom_handled = true;
328 if stopped_cell.get() || prevented_cell.get() {
329 inside_click_stopped = true;
330 }
331 if let Some(target_path) = self.layout_root.find_path_by_entity_id(eid) {
332 match capture_req.get() {
333 Some(true) => captured_target = Some((target_path, true)),
334 Some(false) => captured_target = None,
335 None if captured_target.is_none() => {
336 captured_target = Some((target_path, false));
337 }
338 None => {}
339 }
340 }
341 }
342 self.pointer_capture_target = captured_target;
343
344 if button == 0 {
345 self.active_dom_drag = self.find_draggable_path_at(px, py).map(|source_path| {
346 ActiveDomDrag {
347 source_path,
348 start_pos: (px, py),
349 started: false,
350 last_over_path: None,
351 data_transfer: Default::default(),
352 }
353 });
354 }
355
356 if !pre_open_outside_ids.is_empty() && !inside_click_stopped {
357 if strata_dom::hooks::dispatch_click_outside_for_ids(&pre_open_outside_ids) {
358 dom_handled = true;
359 }
360 }
361
362 let focus_changed = if button == 0 {
363 let focus_path = self.find_focus_path_at(px, py);
364 self.set_focus_path(focus_path)
365 } else {
366 false
367 };
368
369 if dom_handled {
370 PointerDownAction::DomListenerInvoked
371 } else if input_handled || focus_changed {
372 PointerDownAction::FocusChanged
373 } else {
374 PointerDownAction::Ignored
375 }
376 }
377
378 pub fn dispatch_pointer_move_action(&mut self, px: f32, py: f32) -> PointerMoveAction {
380 let mut selection_dragged = false;
381 if self.drag_selection_active {
382 let is_password = self
383 .active_focus
384 .as_ref()
385 .is_some_and(|a| self.active_input_is_password(a));
386 if let Some(ref mut active) = self.active_focus {
387 let mut current_idx = 0;
388 let target_idx = active.input_index;
389 let target_id = active.input_id.as_deref();
390
391 let layout_by_entity = if let Some(eid) = active.entity_id {
392 self.layout_root.find_by_entity_id(eid)
393 } else {
394 None
395 };
396 let layout_opt = layout_by_entity.or_else(|| {
397 Self::find_input_layout(
398 &self.root_blueprint,
399 &self.layout_root,
400 target_idx,
401 target_id,
402 &mut current_idx,
403 )
404 });
405
406 if let Some(layout) = layout_opt {
407 let scroll_x = active.state.scroll_x.get();
408 let rel_x = (px - layout.geometry.content_box.x + scroll_x).max(0.0);
409 let is_mono = strata_layout::is_monospace_family(layout.style.font_family);
410 let raw_text = active.state.text.get();
411 let measure_text = if is_password {
412 Self::mask_password_text(&raw_text)
413 } else {
414 raw_text
415 };
416 let char_idx = Self::calculate_exact_char_index_at_offset(
417 &measure_text,
418 rel_x,
419 layout.style.font_size,
420 layout.style.line_height,
421 is_mono,
422 layout.style.font_family,
423 );
424 active.state.cursor_index.set(char_idx);
425 self.reset_caret_blink();
426 self.sync_active_input_layout();
427 self.mark_paint_dirty();
428 selection_dragged = true;
429 }
430 }
431 }
432
433 let (movement_x, movement_y) = if let Some((wx, wy)) = self.pending_cursor_warp {
434 if (px - wx).abs() <= 2.5 && (py - wy).abs() <= 2.5 {
435 self.pending_cursor_warp = None;
436 self.last_pointer_pos = Some((px, py));
437 (0.0, 0.0)
438 } else {
439 self.pending_cursor_warp = None;
440 let delta = self
441 .last_pointer_pos
442 .map(|(lx, ly)| ((px - lx) as f64, (py - ly) as f64))
443 .unwrap_or((0.0, 0.0));
444 self.last_pointer_pos = Some((px, py));
445 delta
446 }
447 } else {
448 let delta = self
449 .last_pointer_pos
450 .map(|(lx, ly)| ((px - lx) as f64, (py - ly) as f64))
451 .unwrap_or((0.0, 0.0));
452 self.last_pointer_pos = Some((px, py));
453 delta
454 };
455
456 let mut dom_handled = false;
457 let mut requested_warp: Option<(f64, f64)> = None;
458 let capture = self.pointer_capture_target.clone();
459 for ev_type in [
460 strata_dom::EventType::PointerMove,
461 strata_dom::EventType::MouseMove,
462 ] {
463 match capture {
464 Some((ref cap_path, true)) => {
465 if let Some((handler, target_box, _)) = Self::find_event_at_or_above_path(
466 &self.root_blueprint,
467 &self.layout_root,
468 cap_path,
469 ev_type,
470 ) {
471 let target_box_copy = *target_box;
472 let mut ev =
473 self.create_mouse_event(ev_type, px, py, 0, 0, Some(&target_box_copy));
474 ev.movement_x = movement_x;
475 ev.movement_y = movement_y;
476 let capture_req = ev.pointer_capture_requested.clone();
477 let warp_req = ev.cursor_warp_requested.clone();
478 handler(ev);
479 dom_handled = true;
480 if capture_req.get() == Some(false) {
481 self.pointer_capture_target = None;
482 }
483 if let Some(pos) = warp_req.get() {
484 requested_warp = Some(pos);
485 }
486 }
487 }
488 Some((ref cap_path, false)) => {
489 let mut hit_eid = None;
490 if let Some((handler, target_box, eid)) =
491 Self::hit_test_event_with_box_and_entity(
492 &self.root_blueprint,
493 &self.layout_root,
494 px,
495 py,
496 ev_type,
497 )
498 {
499 hit_eid = Some(eid);
500 let mut ev =
501 self.create_mouse_event(ev_type, px, py, 0, 0, Some(target_box));
502 ev.movement_x = movement_x;
503 ev.movement_y = movement_y;
504 let warp_req = ev.cursor_warp_requested.clone();
505 handler(ev);
506 dom_handled = true;
507 if let Some(pos) = warp_req.get() {
508 requested_warp = Some(pos);
509 }
510 }
511 if let Some((handler, target_box, cap_eid)) = Self::find_event_at_or_above_path(
512 &self.root_blueprint,
513 &self.layout_root,
514 cap_path,
515 ev_type,
516 ) {
517 if hit_eid != Some(cap_eid) {
518 let target_box_copy = *target_box;
519 let mut ev = self.create_mouse_event(
520 ev_type,
521 px,
522 py,
523 0,
524 0,
525 Some(&target_box_copy),
526 );
527 ev.movement_x = movement_x;
528 ev.movement_y = movement_y;
529 let warp_req = ev.cursor_warp_requested.clone();
530 handler(ev);
531 dom_handled = true;
532 if let Some(pos) = warp_req.get() {
533 requested_warp = Some(pos);
534 }
535 }
536 }
537 }
538 None => {
539 if let Some((handler, target_box)) = Self::hit_test_event_with_box(
540 &self.root_blueprint,
541 &self.layout_root,
542 px,
543 py,
544 ev_type,
545 ) {
546 let mut ev =
547 self.create_mouse_event(ev_type, px, py, 0, 0, Some(target_box));
548 ev.movement_x = movement_x;
549 ev.movement_y = movement_y;
550 let warp_req = ev.cursor_warp_requested.clone();
551 handler(ev);
552 dom_handled = true;
553 if let Some(pos) = warp_req.get() {
554 requested_warp = Some(pos);
555 }
556 }
557 }
558 }
559 }
560 if let Some((wx, wy)) = requested_warp {
561 self.warp_cursor(wx as f32, wy as f32);
562 }
563
564 if let Some(mut drag) = self.active_dom_drag.take() {
565 let mut cancel_drag = false;
566 if !drag.started {
567 let (sx, sy) = drag.start_pos;
568 if (px - sx).abs() > 3.0 || (py - sy).abs() > 3.0 {
569 drag.started = true;
570 let (invoked, prevented) = self.dispatch_drag_event_at_path(
571 &drag.source_path,
572 strata_dom::EventType::DragStart,
573 sx,
574 sy,
575 &drag.data_transfer,
576 );
577 if invoked {
578 dom_handled = true;
579 }
580 if prevented {
581 cancel_drag = true;
582 }
583 }
584 }
585
586 if drag.started && !cancel_drag {
587 let (drag_invoked, _) = self.dispatch_drag_event_at_path(
588 &drag.source_path,
589 strata_dom::EventType::Drag,
590 px,
591 py,
592 &drag.data_transfer,
593 );
594 if drag_invoked {
595 dom_handled = true;
596 }
597
598 let over_path = self.find_drop_target_path_at(px, py);
599 if over_path != drag.last_over_path {
600 if let Some(ref old_path) = drag.last_over_path {
601 let (leave_invoked, _) = self.dispatch_drag_event_at_path(
602 old_path,
603 strata_dom::EventType::DragLeave,
604 px,
605 py,
606 &drag.data_transfer,
607 );
608 if leave_invoked {
609 dom_handled = true;
610 }
611 }
612 if let Some(ref new_path) = over_path {
613 let (enter_invoked, _) = self.dispatch_drag_event_at_path(
614 new_path,
615 strata_dom::EventType::DragEnter,
616 px,
617 py,
618 &drag.data_transfer,
619 );
620 if enter_invoked {
621 dom_handled = true;
622 }
623 }
624 drag.last_over_path = over_path.clone();
625 }
626
627 if let Some(ref cur_over) = over_path {
628 let (over_invoked, _) = self.dispatch_drag_event_at_path(
629 cur_over,
630 strata_dom::EventType::DragOver,
631 px,
632 py,
633 &drag.data_transfer,
634 );
635 if over_invoked {
636 dom_handled = true;
637 }
638 }
639 }
640
641 if !cancel_drag {
642 self.active_dom_drag = Some(drag);
643 }
644 }
645
646 if dom_handled {
647 PointerMoveAction::DomListenerInvoked
648 } else if selection_dragged {
649 PointerMoveAction::SelectionDragged
650 } else {
651 PointerMoveAction::Ignored
652 }
653 }
654
655 pub fn dispatch_pointer_move(&mut self, px: f32, py: f32) -> bool {
657 self.dispatch_pointer_move_action(px, py).is_handled()
658 }
659
660 pub fn dispatch_pointer_up_action(&mut self, px: f32, py: f32) -> PointerUpAction {
662 self.dispatch_pointer_up_action_with_button(px, py, 0)
663 }
664
665 pub fn dispatch_pointer_up_action_with_button(
667 &mut self,
668 px: f32,
669 py: f32,
670 button: i16,
671 ) -> PointerUpAction {
672 let mut selection_ended = false;
673 if button == 0 && self.drag_selection_active {
674 self.drag_selection_active = false;
675 if let Some(ref mut active) = self.active_focus {
676 if let Some(anchor) = active.state.selection_anchor.get() {
677 if anchor == active.state.cursor_index.get() {
678 active.state.selection_anchor.set(None);
679 }
680 }
681 }
682 selection_ended = true;
683 }
684
685 let capture = self.pointer_capture_target.take();
686 let mut dom_handled = false;
687 for ev_type in [
688 strata_dom::EventType::PointerUp,
689 strata_dom::EventType::MouseUp,
690 ] {
691 match capture {
692 Some((ref cap_path, true)) => {
693 if let Some((handler, target_box, _)) = Self::find_event_at_or_above_path(
694 &self.root_blueprint,
695 &self.layout_root,
696 cap_path,
697 ev_type,
698 ) {
699 let ev =
700 self.create_mouse_event(ev_type, px, py, button, 1, Some(target_box));
701 handler(ev);
702 dom_handled = true;
703 }
704 }
705 Some((ref cap_path, false)) => {
706 let mut hit_eid = None;
707 if let Some((handler, target_box, eid)) =
708 Self::hit_test_event_with_box_and_entity(
709 &self.root_blueprint,
710 &self.layout_root,
711 px,
712 py,
713 ev_type,
714 )
715 {
716 hit_eid = Some(eid);
717 let ev =
718 self.create_mouse_event(ev_type, px, py, button, 1, Some(target_box));
719 handler(ev);
720 dom_handled = true;
721 }
722 if let Some((handler, target_box, cap_eid)) = Self::find_event_at_or_above_path(
723 &self.root_blueprint,
724 &self.layout_root,
725 cap_path,
726 ev_type,
727 ) {
728 if hit_eid != Some(cap_eid) {
729 let ev = self.create_mouse_event(
730 ev_type,
731 px,
732 py,
733 button,
734 1,
735 Some(target_box),
736 );
737 handler(ev);
738 dom_handled = true;
739 }
740 }
741 }
742 None => {
743 if let Some((handler, target_box)) = Self::hit_test_event_with_box(
744 &self.root_blueprint,
745 &self.layout_root,
746 px,
747 py,
748 ev_type,
749 ) {
750 let ev =
751 self.create_mouse_event(ev_type, px, py, button, 1, Some(target_box));
752 handler(ev);
753 dom_handled = true;
754 }
755 }
756 }
757 }
758
759 if button == 0 {
760 if let Some(drag) = self.active_dom_drag.take() {
761 if drag.started {
762 let drop_path = self.find_drop_target_path_at(px, py).or(drag.last_over_path);
763 let mut drop_dispatched = false;
764 if let Some(ref target_path) = drop_path {
765 let (invoked, _) = self.dispatch_drag_event_at_path(
766 target_path,
767 strata_dom::EventType::Drop,
768 px,
769 py,
770 &drag.data_transfer,
771 );
772 if invoked {
773 drop_dispatched = true;
774 dom_handled = true;
775 }
776 }
777 if !drop_dispatched {
778 if let Some((handler, target_box)) = Self::hit_test_event_with_box(
779 &self.root_blueprint,
780 &self.layout_root,
781 px,
782 py,
783 strata_dom::EventType::Drop,
784 ) {
785 let target_box_copy = *target_box;
786 let mut ev = self.create_mouse_event(
787 strata_dom::EventType::Drop,
788 px,
789 py,
790 0,
791 1,
792 Some(&target_box_copy),
793 );
794 ev.data_transfer = std::rc::Rc::clone(&drag.data_transfer);
795 ev.data = drag
796 .data_transfer
797 .borrow()
798 .get("text/plain")
799 .cloned()
800 .or_else(|| drag.data_transfer.borrow().values().next().cloned());
801 handler(ev);
802 dom_handled = true;
803 }
804 }
805
806 let (end_invoked, _) = self.dispatch_drag_event_at_path(
807 &drag.source_path,
808 strata_dom::EventType::DragEnd,
809 px,
810 py,
811 &drag.data_transfer,
812 );
813 if end_invoked {
814 dom_handled = true;
815 }
816 }
817 }
818 }
819
820 if dom_handled {
821 PointerUpAction::DomListenerInvoked
822 } else if selection_ended {
823 PointerUpAction::SelectionEnded
824 } else {
825 PointerUpAction::Ignored
826 }
827 }
828
829 pub fn dispatch_pointer_up(&mut self, px: f32, py: f32) -> bool {
831 self.dispatch_pointer_up_action(px, py).is_handled()
832 }
833
834 pub(crate) fn find_focus_path_at(&self, px: f32, py: f32) -> Option<Vec<usize>> {
836 let hit_path = Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, px, py)?;
837 let mut cur = &self.root_blueprint;
838 let is_focusable = |bp: &NodeBlueprint| -> bool {
839 if let NodeBlueprint::Element {
840 tag, attributes, ..
841 } = bp
842 {
843 let tag_lower = Self::tag_as_str(tag).to_ascii_lowercase();
844 if matches!(
845 tag_lower.as_str(),
846 "button" | "input" | "textarea" | "select" | "a"
847 ) {
848 return true;
849 }
850 for attr in attributes {
851 match attr {
852 strata_dom::Attribute::EventListener { event, .. }
853 if *event == strata_dom::EventType::KeyDown
854 || *event == strata_dom::EventType::KeyUp =>
855 {
856 return true;
857 }
858 strata_dom::Attribute::Property { key, .. }
859 if key == "tabindex" || key == "role" =>
860 {
861 return true;
862 }
863 _ => {}
864 }
865 }
866 }
867 false
868 };
869 let mut best: Option<Vec<usize>> = if is_focusable(cur) {
870 Some(Vec::new())
871 } else {
872 None
873 };
874 let mut prefix = Vec::with_capacity(hit_path.len());
875 for &idx in &hit_path {
876 let mut flat = Vec::new();
877 match cur {
878 NodeBlueprint::Element { children, .. } | NodeBlueprint::Fragment(children) => {
879 for c in children {
880 Self::flatten_blueprints(c, &mut flat);
881 }
882 }
883 _ => break,
884 }
885 if idx >= flat.len() {
886 break;
887 }
888 cur = flat[idx];
889 prefix.push(idx);
890 if is_focusable(cur) {
891 best = Some(prefix.clone());
892 }
893 }
894 best
895 }
896
897 fn keycode_to_dom_key(key: strata_platform::KeyCode, shift: bool) -> String {
898 use strata_platform::KeyCode;
899 match key {
900 KeyCode::ArrowDown => "ArrowDown".to_string(),
901 KeyCode::ArrowUp => "ArrowUp".to_string(),
902 KeyCode::ArrowLeft => "ArrowLeft".to_string(),
903 KeyCode::ArrowRight => "ArrowRight".to_string(),
904 KeyCode::Home => "Home".to_string(),
905 KeyCode::End => "End".to_string(),
906 KeyCode::PageUp => "PageUp".to_string(),
907 KeyCode::PageDown => "PageDown".to_string(),
908 KeyCode::Enter => "Enter".to_string(),
909 KeyCode::Escape => "Escape".to_string(),
910 KeyCode::Tab => "Tab".to_string(),
911 KeyCode::Space => " ".to_string(),
912 KeyCode::Backspace => "Backspace".to_string(),
913 KeyCode::Delete => "Delete".to_string(),
914 KeyCode::Digit0 => "0".to_string(),
915 KeyCode::Digit1 => "1".to_string(),
916 KeyCode::Digit2 => "2".to_string(),
917 KeyCode::Digit3 => "3".to_string(),
918 KeyCode::Digit4 => "4".to_string(),
919 KeyCode::Digit5 => "5".to_string(),
920 KeyCode::Digit6 => "6".to_string(),
921 KeyCode::Digit7 => "7".to_string(),
922 KeyCode::Digit8 => "8".to_string(),
923 KeyCode::Digit9 => "9".to_string(),
924 other => {
925 let dbg = format!("{:?}", other);
926 if let Some(letter) = dbg.strip_prefix("Key") {
927 if shift {
928 letter.to_string()
929 } else {
930 letter.to_ascii_lowercase()
931 }
932 } else {
933 dbg
934 }
935 }
936 }
937 }
938
939 fn dispatch_global_shortcut_for_key(
940 key: strata_platform::KeyCode,
941 modifiers: strata_platform::Modifiers,
942 ) -> bool {
943 let dom_key = Self::keycode_to_dom_key(key, false);
944 strata_dom::hooks::dispatch_global_keydown(
945 &dom_key,
946 modifiers.ctrl,
947 modifiers.shift,
948 modifiers.alt,
949 modifiers.meta,
950 )
951 }
952
953 fn dispatch_dom_key_event(
954 &self,
955 event_type: strata_dom::EventType,
956 key: strata_platform::KeyCode,
957 modifiers: strata_platform::Modifiers,
958 ) -> (bool, bool) {
959 let Some(ref path) = self.current_focus_path else {
960 return (false, false);
961 };
962 let mut chain: Vec<&NodeBlueprint> = Vec::with_capacity(path.len() + 1);
963 let mut cur = &self.root_blueprint;
964 chain.push(cur);
965 for &idx in path {
966 let mut flat = Vec::new();
967 match cur {
968 NodeBlueprint::Element { children, .. } | NodeBlueprint::Fragment(children) => {
969 for c in children {
970 Self::flatten_blueprints(c, &mut flat);
971 }
972 }
973 _ => break,
974 }
975 if idx >= flat.len() {
976 break;
977 }
978 cur = flat[idx];
979 chain.push(cur);
980 }
981
982 let dom_key = Self::keycode_to_dom_key(key, modifiers.shift);
983 let mut ev = strata_dom::Event::new(event_type);
984 ev.key = dom_key;
985 ev.ctrl_key = modifiers.ctrl;
986 ev.shift_key = modifiers.shift;
987 ev.alt_key = modifiers.alt;
988 ev.meta_key = modifiers.meta;
989
990 let mut handled = false;
991 let mut default_prevented = false;
992 for bp in chain.into_iter().rev() {
993 if let NodeBlueprint::Element { attributes, .. } = bp {
994 for attr in attributes {
995 if let strata_dom::Attribute::EventListener { event, handler } = attr {
996 if *event == event_type {
997 let call_ev = ev.clone();
998 let prevented_cell = call_ev.default_prevented.clone();
999 let stopped_cell = call_ev.propagation_stopped.clone();
1000 handler(call_ev);
1001 handled = true;
1002 if prevented_cell.get() {
1003 default_prevented = true;
1004 }
1005 if stopped_cell.get() {
1006 return (true, true);
1007 }
1008 }
1009 }
1010 }
1011 }
1012 }
1013 (handled, default_prevented)
1014 }
1015
1016 pub fn dispatch_key_event(&mut self, event: &PlatformEvent) -> KeyDispatchResult {
1018 if self.active_focus.is_none() {
1019 match event {
1020 PlatformEvent::KeyDown { key, modifiers } => {
1021 let (dom_handled, dom_suppressed) = self.dispatch_dom_key_event(
1022 strata_dom::EventType::KeyDown,
1023 *key,
1024 *modifiers,
1025 );
1026 if dom_suppressed {
1027 return KeyDispatchResult::NeedsRebuild;
1028 }
1029 if Self::dispatch_global_shortcut_for_key(*key, *modifiers) || dom_handled {
1030 return KeyDispatchResult::NeedsRebuild;
1031 }
1032 let (px, py) = self.last_mouse_pos;
1033 let scrolled = match key {
1034 strata_platform::KeyCode::ArrowDown => {
1035 self.dispatch_scroll(px, py, 0.0, 40.0)
1036 }
1037 strata_platform::KeyCode::ArrowUp => {
1038 self.dispatch_scroll(px, py, 0.0, -40.0)
1039 }
1040 strata_platform::KeyCode::PageDown => {
1041 self.dispatch_scroll(px, py, 0.0, 400.0)
1042 }
1043 strata_platform::KeyCode::PageUp => {
1044 self.dispatch_scroll(px, py, 0.0, -400.0)
1045 }
1046 strata_platform::KeyCode::Home => self.scroll_to_top(px, py),
1047 strata_platform::KeyCode::End => self.scroll_to_bottom(px, py),
1048 _ => false,
1049 };
1050 if scrolled {
1051 return KeyDispatchResult::RedrawOnly;
1052 }
1053 }
1054 PlatformEvent::KeyUp { key, modifiers } => {
1055 if self
1056 .dispatch_dom_key_event(strata_dom::EventType::KeyUp, *key, *modifiers)
1057 .0
1058 {
1059 return KeyDispatchResult::NeedsRebuild;
1060 }
1061 }
1062 _ => {}
1063 }
1064 return KeyDispatchResult::Ignored;
1065 }
1066
1067 match event {
1068 PlatformEvent::ImeCommit(text) => {
1069 if !text.is_empty() {
1070 self.reset_caret_blink();
1071 let active = self.active_focus.as_mut().unwrap();
1072 active.state.insert_text(text);
1073 let val = active.state.text.get();
1074 if let Some(ref on_input) = active.on_input {
1075 on_input(strata_dom::Event::with_value(
1076 strata_dom::EventType::Input,
1077 val,
1078 ));
1079 }
1080 self.sync_active_input_layout();
1081 KeyDispatchResult::NeedsRebuild
1082 } else {
1083 KeyDispatchResult::Ignored
1084 }
1085 }
1086 PlatformEvent::ImePreedit { .. } => KeyDispatchResult::RedrawOnly,
1087 PlatformEvent::ReceivedCharacter(ch) => {
1088 if !ch.is_control() {
1089 self.reset_caret_blink();
1090 let active = self.active_focus.as_mut().unwrap();
1091 active.state.insert_text(&ch.to_string());
1092 let val = active.state.text.get();
1093 if let Some(ref on_input) = active.on_input {
1094 on_input(strata_dom::Event::with_value(
1095 strata_dom::EventType::Input,
1096 val,
1097 ));
1098 }
1099 self.sync_active_input_layout();
1100 KeyDispatchResult::NeedsRebuild
1101 } else {
1102 KeyDispatchResult::Ignored
1103 }
1104 }
1105 PlatformEvent::KeyDown { key, modifiers } => {
1106 let mut text_changed = false;
1107 let mut nav_or_sel = false;
1108 let mut submit = false;
1109 let mut blur = false;
1110
1111 let is_cmd = modifiers.ctrl || modifiers.meta;
1112 if is_cmd && !modifiers.alt {
1113 let cmd = match key {
1114 strata_platform::KeyCode::KeyA => Some(EditCommand::SelectAll),
1115 strata_platform::KeyCode::KeyC => Some(EditCommand::Copy),
1116 strata_platform::KeyCode::KeyX => Some(EditCommand::Cut),
1117 strata_platform::KeyCode::KeyV => Some(EditCommand::Paste),
1118 strata_platform::KeyCode::KeyZ if modifiers.shift => {
1119 Some(EditCommand::Redo)
1120 }
1121 strata_platform::KeyCode::KeyZ => Some(EditCommand::Undo),
1122 strata_platform::KeyCode::KeyY => Some(EditCommand::Redo),
1123 _ => None,
1124 };
1125 if let Some(cmd) = cmd {
1126 let res = self.apply_edit_command(cmd);
1127 self.sync_active_input_layout();
1128 if res {
1129 return match cmd {
1130 EditCommand::SelectAll | EditCommand::Copy => {
1131 self.mark_paint_dirty();
1132 KeyDispatchResult::RedrawOnly
1133 }
1134 _ => KeyDispatchResult::NeedsRebuild,
1135 };
1136 } else {
1137 return KeyDispatchResult::Ignored;
1138 }
1139 }
1140 }
1141
1142 let is_global_candidate = is_cmd
1143 || modifiers.alt
1144 || matches!(
1145 key,
1146 strata_platform::KeyCode::Escape
1147 | strata_platform::KeyCode::F1
1148 | strata_platform::KeyCode::F2
1149 | strata_platform::KeyCode::F3
1150 | strata_platform::KeyCode::F4
1151 | strata_platform::KeyCode::F5
1152 | strata_platform::KeyCode::F6
1153 | strata_platform::KeyCode::F7
1154 | strata_platform::KeyCode::F8
1155 | strata_platform::KeyCode::F9
1156 | strata_platform::KeyCode::F10
1157 | strata_platform::KeyCode::F11
1158 | strata_platform::KeyCode::F12
1159 );
1160 if is_global_candidate && Self::dispatch_global_shortcut_for_key(*key, *modifiers) {
1161 return KeyDispatchResult::NeedsRebuild;
1162 }
1163
1164 let active = self.active_focus.as_mut().unwrap();
1165
1166 if !text_changed && !nav_or_sel {
1167 match key {
1168 strata_platform::KeyCode::Backspace => {
1169 if is_cmd {
1170 active.state.delete_word_backward();
1171 } else {
1172 active.state.delete_backward();
1173 }
1174 text_changed = true;
1175 }
1176 strata_platform::KeyCode::Delete => {
1177 if is_cmd {
1178 active.state.delete_word_forward();
1179 } else {
1180 active.state.delete_forward();
1181 }
1182 text_changed = true;
1183 }
1184 strata_platform::KeyCode::ArrowLeft => {
1185 active.state.move_left(modifiers.shift, is_cmd);
1186 nav_or_sel = true;
1187 }
1188 strata_platform::KeyCode::ArrowRight => {
1189 active.state.move_right(modifiers.shift, is_cmd);
1190 nav_or_sel = true;
1191 }
1192 strata_platform::KeyCode::Home => {
1193 active.state.move_home(modifiers.shift);
1194 nav_or_sel = true;
1195 }
1196 strata_platform::KeyCode::End => {
1197 active.state.move_end(modifiers.shift);
1198 nav_or_sel = true;
1199 }
1200 strata_platform::KeyCode::Enter => {
1201 submit = true;
1202 }
1203 strata_platform::KeyCode::Escape => {
1204 blur = true;
1205 }
1206 _ => {}
1207 }
1208 }
1209
1210 if text_changed || nav_or_sel {
1211 self.caret_blink_epoch = std::time::Instant::now();
1212 }
1213
1214 if text_changed {
1215 let val = active.state.text.get();
1216 if let Some(ref on_input) = active.on_input {
1217 on_input(strata_dom::Event::with_value(
1218 strata_dom::EventType::Input,
1219 val,
1220 ));
1221 }
1222 self.sync_active_input_layout();
1223 return KeyDispatchResult::NeedsRebuild;
1224 }
1225
1226 if nav_or_sel {
1227 self.sync_active_input_layout();
1228 self.mark_paint_dirty();
1229 return KeyDispatchResult::RedrawOnly;
1230 }
1231
1232 if submit {
1233 let val = active.state.text.get();
1234 if let Some(ref on_submit) = active.on_submit {
1235 on_submit(strata_dom::Event::with_value(
1236 strata_dom::EventType::Submit,
1237 val.clone(),
1238 ));
1239 }
1240 if let Some(ref on_change) = active.on_change {
1241 on_change(strata_dom::Event::with_value(
1242 strata_dom::EventType::Change,
1243 val,
1244 ));
1245 }
1246 return KeyDispatchResult::NeedsRebuild;
1247 }
1248
1249 if blur {
1250 active.state.scroll_x.set(0.0);
1251 if let Some(ref on_blur) = active.on_blur {
1252 on_blur(strata_dom::Event::new(strata_dom::EventType::Blur));
1253 }
1254 self.active_focus = None;
1255 return KeyDispatchResult::NeedsRebuild;
1256 }
1257
1258 if Self::dispatch_global_shortcut_for_key(*key, *modifiers) {
1259 return KeyDispatchResult::NeedsRebuild;
1260 }
1261
1262 KeyDispatchResult::Ignored
1263 }
1264 _ => KeyDispatchResult::Ignored,
1265 }
1266 }
1267
1268 pub fn handle_key_event(&mut self, event: &PlatformEvent) -> bool {
1271 self.dispatch_key_event(event).is_handled()
1272 }
1273
1274 pub fn create_mouse_event(
1278 &self,
1279 event_type: strata_dom::EventType,
1280 px: f32,
1281 py: f32,
1282 button: i16,
1283 detail: i32,
1284 target_box: Option<&strata_layout::Rect>,
1285 ) -> strata_dom::Event {
1286 let mut ev = strata_dom::Event::new(event_type);
1287 ev.client_x = px as f64;
1288 ev.client_y = py as f64;
1289 ev.page_x = px as f64;
1290 ev.page_y = py as f64;
1291 ev.screen_x = px as f64;
1292 ev.screen_y = py as f64;
1293 ev.button = button;
1294 ev.buttons = match button {
1295 0 => 1,
1296 1 => 4,
1297 2 => 2,
1298 _ => 0,
1299 };
1300 ev.detail = detail;
1301 if let Some(tb) = target_box {
1302 ev.offset_x = (px - tb.x) as f64;
1303 ev.offset_y = (py - tb.y) as f64;
1304 ev.width = tb.width as f64;
1305 ev.height = tb.height as f64;
1306 } else {
1307 ev.offset_x = px as f64;
1308 ev.offset_y = py as f64;
1309 }
1310 #[cfg(target_os = "linux")]
1311 {
1312 let mods = strata_platform::gtk_current_modifiers();
1313 ev.shift_key = mods.shift;
1314 ev.ctrl_key = mods.ctrl;
1315 ev.alt_key = mods.alt;
1316 ev.meta_key = mods.meta;
1317 }
1318 ev
1319 }
1320
1321 pub fn target_is_retained_canvas3d(&self, px: f32, py: f32) -> bool {
1324 if self.tracked_retained_3d.is_empty() {
1325 return false;
1326 }
1327 let path_is_canvas = |path: &[usize]| -> bool {
1328 let mut current = &self.root_blueprint;
1329 if let NodeBlueprint::Element { tag, .. } = current {
1330 if matches!(tag, Tag::Canvas | Tag::Custom("Canvas3D")) {
1331 return true;
1332 }
1333 }
1334 for &idx in path {
1335 let mut flat = Vec::new();
1336 match current {
1337 NodeBlueprint::Element { children, .. }
1338 | NodeBlueprint::Fragment(children) => {
1339 for child in children {
1340 Self::flatten_blueprints(child, &mut flat);
1341 }
1342 }
1343 _ => return false,
1344 }
1345 if idx >= flat.len() {
1346 return false;
1347 }
1348 current = flat[idx];
1349 if let NodeBlueprint::Element { tag, .. } = current {
1350 if matches!(tag, Tag::Canvas | Tag::Custom("Canvas3D")) {
1351 return true;
1352 }
1353 }
1354 }
1355 false
1356 };
1357 if let Some((ref cap_path, _)) = self.pointer_capture_target {
1358 if path_is_canvas(cap_path) {
1359 return true;
1360 }
1361 }
1362 Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, px, py)
1363 .as_deref()
1364 .is_some_and(path_is_canvas)
1365 }
1366
1367 pub fn dispatch_click(&self, px: f32, py: f32) -> bool {
1370 self.dispatch_mouse_click(px, py, 0, 1)
1371 }
1372
1373 pub fn dispatch_mouse_click(&self, px: f32, py: f32, button: i16, detail: i32) -> bool {
1375 let ev_type = if button == 0 {
1376 strata_dom::EventType::Click
1377 } else {
1378 strata_dom::EventType::AuxClick
1379 };
1380
1381 if let Some((handler, target_box)) =
1382 Self::hit_test_event_with_box(&self.root_blueprint, &self.layout_root, px, py, ev_type)
1383 {
1384 let ev = self.create_mouse_event(ev_type, px, py, button, detail, Some(target_box));
1385 handler(ev);
1386 true
1387 } else if button != 0 {
1388 if let Some((handler, target_box)) = Self::hit_test_event_with_box(
1390 &self.root_blueprint,
1391 &self.layout_root,
1392 px,
1393 py,
1394 strata_dom::EventType::Click,
1395 ) {
1396 let ev = self.create_mouse_event(
1397 strata_dom::EventType::Click,
1398 px,
1399 py,
1400 button,
1401 detail,
1402 Some(target_box),
1403 );
1404 handler(ev);
1405 true
1406 } else {
1407 false
1408 }
1409 } else {
1410 false
1411 }
1412 }
1413
1414 pub fn dispatch_dblclick(&self, px: f32, py: f32) -> bool {
1416 if let Some((handler, target_box)) = Self::hit_test_event_with_box(
1417 &self.root_blueprint,
1418 &self.layout_root,
1419 px,
1420 py,
1421 strata_dom::EventType::DblClick,
1422 ) {
1423 let ev = self.create_mouse_event(
1424 strata_dom::EventType::DblClick,
1425 px,
1426 py,
1427 0,
1428 2,
1429 Some(target_box),
1430 );
1431 handler(ev);
1432 true
1433 } else {
1434 false
1435 }
1436 }
1437
1438 pub fn dispatch_context_menu(&self, px: f32, py: f32) -> bool {
1442 if let Some((handler, target_box)) = Self::hit_test_event_with_box(
1443 &self.root_blueprint,
1444 &self.layout_root,
1445 px,
1446 py,
1447 strata_dom::EventType::ContextMenu,
1448 ) {
1449 let ev = self.create_mouse_event(
1450 strata_dom::EventType::ContextMenu,
1451 px,
1452 py,
1453 2,
1454 1,
1455 Some(target_box),
1456 );
1457 handler(ev);
1458 true
1459 } else {
1460 false
1461 }
1462 }
1463
1464 pub fn begin_window_drag_candidate(&mut self, px: f32, py: f32) -> bool {
1468 let region = Self::hit_test_property(
1469 &self.root_blueprint,
1470 &self.layout_root,
1471 px,
1472 py,
1473 "data-app-region",
1474 );
1475 let is_drag = region.as_deref() == Some("drag")
1476 || self
1477 .csd_controller
1478 .hit_test(self.viewport_width, self.viewport_height, px, py)
1479 == strata_platform::CsdHitZone::Titlebar;
1480
1481 if !is_drag {
1482 self.pending_window_drag = None;
1483 return false;
1484 }
1485 let now = std::time::Instant::now();
1486 if let Some((t, lx, ly)) = self.last_drag_click.take() {
1487 if now.duration_since(t).as_millis() < 400
1488 && (px - lx).abs() < 5.0
1489 && (py - ly).abs() < 5.0
1490 {
1491 self.pending_window_drag = None;
1493 return true;
1494 }
1495 }
1496 self.last_drag_click = Some((now, px, py));
1497 self.pending_window_drag = Some((px, py));
1498 false
1499 }
1500
1501 pub fn poll_window_drag(&mut self, px: f32, py: f32) -> bool {
1504 if let Some((sx, sy)) = self.pending_window_drag {
1505 if (px - sx).abs() > 4.0 || (py - sy).abs() > 4.0 {
1506 self.pending_window_drag = None;
1507 self.last_drag_click = None;
1508 #[cfg(target_os = "linux")]
1509 {
1510 let was_max = strata_platform::gtk_is_maximized();
1511 strata_platform::gtk_begin_window_drag(sx / self.viewport_width.max(1.0), sy);
1512 let now_max = strata_platform::gtk_is_maximized();
1513 return was_max != now_max;
1514 }
1515 #[cfg(not(target_os = "linux"))]
1516 {
1517 return true;
1518 }
1519 }
1520 }
1521 false
1522 }
1523
1524 pub(crate) fn find_draggable_path_at(&self, px: f32, py: f32) -> Option<Vec<usize>> {
1526 let hit_path = Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, px, py)?;
1527 for len in (0..=hit_path.len()).rev() {
1528 let prefix = &hit_path[..len];
1529 let Some(NodeBlueprint::Element { attributes, .. }) =
1530 Self::find_blueprint_at_path(&self.root_blueprint, prefix)
1531 else {
1532 continue;
1533 };
1534 let mut explicit_draggable: Option<bool> = None;
1535 let mut has_drag_listener = false;
1536 for attr in attributes {
1537 match attr {
1538 strata_dom::Attribute::Property { key, value } if key == "draggable" => {
1539 explicit_draggable = Some(!value.eq_ignore_ascii_case("false"));
1540 }
1541 strata_dom::Attribute::EventListener { event, .. }
1542 if matches!(
1543 event,
1544 strata_dom::EventType::DragStart | strata_dom::EventType::Drag
1545 ) =>
1546 {
1547 has_drag_listener = true;
1548 }
1549 _ => {}
1550 }
1551 }
1552 if explicit_draggable == Some(true)
1553 || (explicit_draggable.is_none() && has_drag_listener)
1554 {
1555 return Some(prefix.to_vec());
1556 }
1557 }
1558 None
1559 }
1560
1561 pub(crate) fn find_drop_target_path_at(&self, px: f32, py: f32) -> Option<Vec<usize>> {
1565 let hit_path = Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, px, py)?;
1566 for len in (0..=hit_path.len()).rev() {
1567 let prefix = &hit_path[..len];
1568 let Some(NodeBlueprint::Element { attributes, .. }) =
1569 Self::find_blueprint_at_path(&self.root_blueprint, prefix)
1570 else {
1571 continue;
1572 };
1573 let is_drop_zone = attributes.iter().any(|attr| {
1574 matches!(
1575 attr,
1576 strata_dom::Attribute::EventListener {
1577 event: strata_dom::EventType::DragEnter
1578 | strata_dom::EventType::DragOver
1579 | strata_dom::EventType::DragLeave
1580 | strata_dom::EventType::Drop,
1581 ..
1582 }
1583 )
1584 });
1585 if is_drop_zone {
1586 return Some(prefix.to_vec());
1587 }
1588 }
1589 Some(hit_path)
1590 }
1591
1592 pub(crate) fn dispatch_drag_event_at_path(
1595 &self,
1596 path: &[usize],
1597 event_type: strata_dom::EventType,
1598 px: f32,
1599 py: f32,
1600 data_transfer: &std::rc::Rc<
1601 std::cell::RefCell<std::collections::HashMap<String, String>>,
1602 >,
1603 ) -> (bool, bool) {
1604 if let Some((handler, target_box, _)) = Self::find_event_at_or_above_path(
1605 &self.root_blueprint,
1606 &self.layout_root,
1607 path,
1608 event_type,
1609 ) {
1610 let target_box_copy = *target_box;
1611 let mut ev =
1612 self.create_mouse_event(event_type, px, py, 0, 1, Some(&target_box_copy));
1613 ev.data_transfer = std::rc::Rc::clone(data_transfer);
1614 ev.data = data_transfer
1615 .borrow()
1616 .get("text/plain")
1617 .cloned()
1618 .or_else(|| data_transfer.borrow().values().next().cloned());
1619 let prevented_cell = ev.default_prevented.clone();
1620 handler(ev);
1621 return (true, prevented_cell.get());
1622 }
1623 (false, false)
1624 }
1625
1626 pub(crate) fn find_first_event_listener<'a>(
1627 blueprint: &'a NodeBlueprint,
1628 layout: &'a LayoutNode,
1629 event_type: strata_dom::EventType,
1630 ) -> Option<(
1631 std::rc::Rc<dyn Fn(strata_dom::Event)>,
1632 &'a strata_layout::Rect,
1633 )> {
1634 if let NodeBlueprint::Element { attributes, .. } = blueprint {
1635 for attr in attributes {
1636 if let strata_dom::Attribute::EventListener { event, handler } = attr {
1637 if *event == event_type {
1638 return Some((handler.clone(), &layout.geometry.border_box));
1639 }
1640 }
1641 }
1642 }
1643 for (_idx, child_bp, child_layout) in Self::paired_blueprint_children(blueprint, layout) {
1644 if let Some(found) =
1645 Self::find_first_event_listener(child_bp, child_layout, event_type)
1646 {
1647 return Some(found);
1648 }
1649 }
1650 None
1651 }
1652
1653 pub fn dispatch_file_drop(&mut self, paths: Vec<std::path::PathBuf>) -> bool {
1656 if paths.is_empty() {
1657 return false;
1658 }
1659 let (px, py) = self.last_mouse_pos;
1660 let hit = Self::hit_test_event_with_box(
1661 &self.root_blueprint,
1662 &self.layout_root,
1663 px,
1664 py,
1665 strata_dom::EventType::Drop,
1666 )
1667 .or_else(|| {
1668 Self::find_first_event_listener(
1669 &self.root_blueprint,
1670 &self.layout_root,
1671 strata_dom::EventType::Drop,
1672 )
1673 });
1674
1675 if let Some((handler, target_box)) = hit {
1676 let target_box_copy = *target_box;
1677 let mut ev = self.create_mouse_event(
1678 strata_dom::EventType::Drop,
1679 px,
1680 py,
1681 0,
1682 1,
1683 Some(&target_box_copy),
1684 );
1685 if let Some(first) = paths.first() {
1686 let first_str = first.to_string_lossy().into_owned();
1687 ev.data = Some(first_str.clone());
1688 ev.set_data("text/uri-list", first_str.clone());
1689 ev.set_data("text/plain", first_str);
1690 }
1691 ev.files = paths;
1692 handler(ev);
1693 return true;
1694 }
1695 false
1696 }
1697}