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