1#[cfg(feature = "frame-log")]
3use log;
4
5use crate::widget::EventContext;
6
7use super::*;
8
9impl ElementTree {
10 pub fn hit_test(&self, position: Point) -> Option<ElementId> {
40 #[cfg(feature = "frame-log")]
41 log::trace!("[FRAME-EVENT] hit_test at {:?}", position);
42
43 for entry in self.overlays.iter().rev() {
45 if let Some(el) = self.elements.get(entry.root_element) {
46 let off = entry.layer_offset(self);
47 if let Some(hit) = el.render_object.borrow().hit_test(position, off) {
48 #[cfg(feature = "frame-log")]
49 log::trace!("[FRAME-EVENT] hit_test → overlay hit={:?}", hit);
50 return Some(hit);
51 }
52 }
53 }
54 let result = self.root_render_object()?.borrow().hit_test(position, Point::ZERO);
56 #[cfg(feature = "frame-log")]
57 log::trace!("[FRAME-EVENT] hit_test → main tree hit={:?}", result);
58 result
59 }
60
61 pub fn dispatch_event(&mut self, event: &WidgetEvent, position: Point) {
79 match event {
80 WidgetEvent::PointerDown(_) => {
81 let hit_id = self.hit_test(position);
82 self.notify_click_outside(hit_id);
86 if !self.pointer_down_chain.is_empty() {
88 self.cancel_pointer_chain(None);
89 }
90 let Some(id) = hit_id else {
91 self.clear_focus();
93 return;
94 };
95 self.pointer_capture = Some(id);
97 self.gesture_winner = None;
98 if let Some(focus_target) = self.nearest_focusable_ancestor(id) {
102 self.request_focus(focus_target);
103 }
104 let _ = self.dispatch_bubbling(id, event, position);
105 self.resolve_gesture_arena();
106 }
107 WidgetEvent::PointerMove(_) | WidgetEvent::PointerUp(_) => {
108 if let WidgetEvent::PointerMove(_) = event {
111 let current_hit = self.hit_test(position);
112 if current_hit != self.last_hit_target {
113 if let Some(old) = self.last_hit_target.take() {
114 if self.is_active(old) {
115 let _ = self.dispatch_bubbling(old, &WidgetEvent::PointerLeave, position);
116 }
117 }
118 if let Some(new_id) = current_hit {
119 let _ = self.dispatch_bubbling(new_id, &WidgetEvent::PointerEnter, position);
120 }
121 self.last_hit_target = current_hit;
122 }
123 }
124 if self.pointer_capture.is_some() && !self.pointer_capture.is_some_and(|id| self.is_active(id)) {
126 let winner = self.gesture_winner.filter(|&w| self.is_active(w));
127 self.pointer_capture = None;
128 self.cancel_pointer_chain(winner);
129 }
130 if self.pointer_capture.is_none() {
136 if let Some(w) = self.gesture_winner.filter(|&w| self.is_active(w)) {
137 self.pointer_capture = Some(w);
138 }
139 }
140 if let Some(id) = self.pointer_capture {
141 if let Some(w) = self.gesture_winner.filter(|&w| self.is_active(w)) {
143 let local = self.local_position(w, position);
144 let _ = self.deliver_event(w, event, local);
145 } else {
146 let handled_by = self.dispatch_bubbling(id, event, position);
147 if matches!(event, WidgetEvent::PointerUp(_)) {
148 if let Some(handler) = handled_by {
150 self.cancel_pointer_chain(Some(handler));
151 }
152 }
153 self.resolve_gesture_arena();
154 }
155 } else {
156 if let Some(id) = self.hit_test(position) {
159 if matches!(event, WidgetEvent::PointerMove(_)) {
160 let _ = self.dispatch_bubbling_hover(id, event, position);
163 } else {
164 let local = self.local_position(id, position);
166 let _ = self.deliver_event(id, event, local);
167 }
168 }
169 }
170 if matches!(event, WidgetEvent::PointerUp(_)) {
171 self.pointer_capture = None;
173 self.gesture_winner = None;
174 self.pointer_down_chain.clear();
175 }
176 }
177 WidgetEvent::PointerLeave | WidgetEvent::PointerEnter => {
178 if let Some(id) = self.hit_test(position) {
182 match event {
183 WidgetEvent::PointerEnter => self.last_hit_target = Some(id),
184 WidgetEvent::PointerLeave => self.last_hit_target = None,
185 _ => {}
186 }
187 let _ = self.dispatch_bubbling(id, event, position);
188 }
189 }
190 WidgetEvent::PointerCancel => {
191 self.cancel_pointer_chain(None);
193 self.pointer_capture = None;
194 self.gesture_winner = None;
195 self.pointer_down_chain.clear();
196 }
197 WidgetEvent::Scroll(_, _) => {
198 if let Some(target) = self.find_scroll_target(position) {
200 let local = self.local_position(target, position);
201 let _ = self.deliver_event(target, event, local);
202 }
203 }
204 _ => {
205 if let Some(id) = self.hit_test(position) {
207 let local = self.local_position(id, position);
208 let _ = self.deliver_event(id, event, local);
209 }
210 }
211 }
212 }
213
214 pub fn dispatch_event_to(&mut self, id: ElementId, event: &WidgetEvent) -> bool {
221 if !self.is_active(id) {
222 return false;
223 }
224 self.deliver_event(id, event, Point::ZERO);
225 true
226 }
227
228 pub fn reveal(&mut self, id: ElementId, target_root: Rect) -> bool {
234 let mut chain = Vec::new();
235 let mut current = Some(id);
236 while let Some(eid) = current {
237 chain.push(eid);
238 current = self.elements.get(eid).and_then(|element| element.parent);
239 }
240 for eid in chain {
241 let Some(origin) = self.element_origin(eid) else { continue };
242 let local =
243 Rect::new(target_root.x - origin.x, target_root.y - origin.y, target_root.width, target_root.height);
244 let handled = self
245 .elements
246 .get_mut(eid)
247 .and_then(|element| element.state.as_mut())
248 .is_some_and(|state| state.scroll_into_view(local));
249 if handled {
250 self.mark_element_dirty_with_semantics(eid, false, true);
252 return true;
253 }
254 }
255 false
256 }
257
258 pub(crate) fn dispatch_bubbling(
264 &mut self,
265 id: ElementId,
266 event: &WidgetEvent,
267 position: Point,
268 ) -> Option<ElementId> {
269 self.dispatch_bubbling_inner(id, event, position, false)
270 }
271
272 fn dispatch_bubbling_hover(&mut self, id: ElementId, event: &WidgetEvent, position: Point) -> Option<ElementId> {
279 self.dispatch_bubbling_inner(id, event, position, true)
280 }
281
282 fn dispatch_bubbling_inner(
283 &mut self,
284 id: ElementId,
285 event: &WidgetEvent,
286 position: Point,
287 stop_at_boundary: bool,
288 ) -> Option<ElementId> {
289 let mut chain = std::mem::take(&mut self.event_chain);
291 chain.clear();
292 let mut cur = Some(id);
293 while let Some(cid) = cur {
294 if let Some(el) = self.elements.get(cid) {
295 if cid == id || el.state.is_some() {
296 chain.push(cid);
297 }
298 let boundary = cid != id && el.widget.0.is_repaint_boundary();
299 cur = if stop_at_boundary && boundary { None } else { el.parent };
300 } else {
301 break;
302 }
303 }
304 if matches!(event, WidgetEvent::PointerDown(_)) {
305 self.pointer_down_chain.clear();
306 self.pointer_down_chain.extend_from_slice(&chain);
307 }
308 let mut handled_by = None;
309 for &cid in &chain {
310 let local = self.local_position(cid, position);
311 if self.deliver_event(cid, event, local) {
312 handled_by = Some(cid);
313 break;
314 }
315 }
316 self.event_chain = chain;
317 handled_by
318 }
319
320 pub(crate) fn find_scroll_target(&self, position: Point) -> Option<ElementId> {
335 if let Some(hit_id) = self.hit_test(position) {
337 let mut cur = Some(hit_id);
338 while let Some(cid) = cur {
339 if let Some(el) = self.elements.get(cid) {
340 if el.widget.0.accepts_scroll() {
342 return Some(cid);
343 }
344 cur = el.parent;
345 } else {
346 break;
347 }
348 }
349 }
350 let origins = self.origin_map();
354 let mut best: Option<(ElementId, usize)> = None;
355 for (eid, el) in self.elements.iter() {
356 if !el.active {
357 continue;
358 }
359 if !el.widget.0.accepts_scroll() {
361 continue;
362 }
363 let Some(&(origin, depth)) = origins.get(&eid) else { continue };
364 if el.render_object.borrow().hit_test(position, origin).is_some() && best.map_or(true, |(_, d)| depth > d) {
366 best = Some((eid, depth));
367 }
368 }
369 best.map(|(eid, _)| eid)
370 }
371
372 pub(crate) fn is_in_overlay_subtree(&self, element_id: ElementId, overlay_root: ElementId) -> bool {
375 let mut cur = Some(element_id);
376 while let Some(cid) = cur {
377 if cid == overlay_root {
378 return true;
379 }
380 match self.elements.get(cid) {
381 Some(el) => cur = el.parent,
382 None => return false,
383 }
384 }
385 false
386 }
387
388 pub(crate) fn notify_click_outside(&mut self, hit_target: Option<ElementId>) {
395 use crate::widget::overlay_host::OverlayEvent;
396 let mut to_notify: Vec<ElementId> = Vec::new();
401 for entry in self.overlays.iter() {
402 let inside =
403 hit_target.map(|hit_id| self.is_in_overlay_subtree(hit_id, entry.root_element)).unwrap_or(false);
404 if !inside {
405 to_notify.push(entry.root_element);
406 }
407 }
408 for &root in &to_notify {
411 if let Some(el) = self.elements.get_mut(root) {
412 if let Some(state_dyn) = el.state.as_mut() {
413 if let Some(aware) = state_dyn.as_overlay_aware() {
414 aware.on_overlay_event(OverlayEvent::ClickOutside);
415 }
416 }
417 }
418 }
419 }
420
421 pub(crate) fn resolve_gesture_arena(&mut self) {
424 if self.gesture_winner.is_some() {
425 return;
426 }
427 let Some(cap) = self.pointer_capture else { return };
428 let mut chain = std::mem::take(&mut self.event_chain);
430 chain.clear();
431 let mut cur = Some(cap);
432 while let Some(cid) = cur {
433 if let Some(el) = self.elements.get(cid) {
434 if el.state.is_some() {
435 chain.push(cid);
436 }
437 cur = el.parent;
438 } else {
439 break;
440 }
441 }
442 let mut winner = None;
444 for &cid in &chain {
445 let accepted = self
446 .elements
447 .get(cid)
448 .and_then(|el| el.state.as_ref())
449 .map(|s| s.gesture_state() == Some(GestureState::Accepted))
450 .unwrap_or(false);
451 if accepted {
452 winner = Some(cid);
453 break;
454 }
455 }
456 if let Some(w) = winner {
457 self.gesture_winner = Some(w);
458 self.cancel_pointer_chain(Some(w));
460 }
461 self.event_chain = chain;
462 }
463
464 fn cancel_pointer_chain(&mut self, except: Option<ElementId>) {
473 let chain = std::mem::take(&mut self.pointer_down_chain);
474 for &id in &chain {
475 if Some(id) == except || !self.is_active(id) {
476 continue;
477 }
478 if let Some(state) = self.elements.get_mut(id).and_then(|el| el.state.as_mut()) {
479 state.reject_gestures();
480 }
481 let _ = self.deliver_event(id, &WidgetEvent::PointerCancel, Point::ZERO);
482 }
483 self.pointer_down_chain = chain.into_iter().filter(|id| Some(*id) == except).collect();
484 }
485
486 pub fn pointer_leave_window(&mut self) {
490 if let Some(old) = self.last_hit_target.take() {
491 if self.is_active(old) {
492 let _ = self.dispatch_bubbling(old, &WidgetEvent::PointerLeave, Point::ZERO);
493 }
494 }
495 self.cancel_pointer_chain(None);
496 self.pointer_capture = None;
497 self.gesture_winner = None;
498 self.pointer_down_chain.clear();
499 }
500
501 pub(crate) fn local_position(&self, id: ElementId, position: Point) -> Point {
505 match self.element_origin(id) {
506 Some(o) => Point::new(position.x - o.x, position.y - o.y),
507 None => position,
508 }
509 }
510
511 pub(crate) fn deliver_event(&mut self, id: ElementId, event: &WidgetEvent, local_pos: Point) -> bool {
516 let layout_affecting = match event {
520 WidgetEvent::PointerMove(_) | WidgetEvent::PointerEnter | WidgetEvent::PointerLeave => {
521 self.pointer_capture.is_some()
522 }
523 _ => true,
524 };
525 let semantics_affecting = match event {
528 WidgetEvent::PointerEnter | WidgetEvent::PointerLeave => false,
529 WidgetEvent::PointerMove(_) => self.pointer_capture.is_some(),
530 _ => true,
531 };
532 self.mark_element_dirty_with_semantics(id, layout_affecting, semantics_affecting);
534 let mut ctx = EventContext::default();
539 let request_build = {
540 let el = self.elements.get_mut(id).expect("element exists");
541 if let Some(state) = el.state.as_mut() {
542 state.handle_event_ctx(event, local_pos, &mut ctx);
544 state.take_needs_build()
545 } else {
546 el.widget.0.handle_event_ctx(event, local_pos, &mut ctx);
548 false
549 }
550 };
551 if request_build {
552 self.build_requests.push(id);
553 }
554 ctx.is_handled()
555 }
556
557 pub fn focused_element(&self) -> Option<ElementId> {
563 self.focused
564 }
565
566 pub(crate) fn is_active(&self, id: ElementId) -> bool {
568 self.elements.get(id).map(|e| e.active).unwrap_or(false)
569 }
570
571 pub fn request_focus(&mut self, id: ElementId) {
576 if self.focused == Some(id) || self.elements.get(id).is_none() {
577 return; }
579 let old = self.focused;
580 if let Some(old) = old {
582 self.deliver_event(old, &WidgetEvent::FocusLost, Point::ZERO);
583 }
584 self.focused = Some(id);
585 self.deliver_event(id, &WidgetEvent::FocusGained, Point::ZERO);
586 self.reconcile_focus_within();
590 }
591
592 pub fn clear_focus(&mut self) {
594 if let Some(old) = self.focused.take() {
595 if self.elements.get(old).is_some() {
596 self.deliver_event(old, &WidgetEvent::FocusLost, Point::ZERO);
597 }
598 self.reconcile_focus_within();
599 }
600 }
601
602 pub(crate) fn reconcile_focus_within(&mut self) {
608 let desired = self.focused.map(|id| self.ancestor_ids(id)).unwrap_or_default();
609 let old = std::mem::take(&mut self.focus_within_ancestors);
610 let leave: Vec<ElementId> =
611 old.iter().copied().filter(|id| !desired.contains(id) && self.is_active(*id)).collect();
612 let enter: Vec<ElementId> =
613 desired.iter().copied().filter(|id| !old.contains(id) && self.is_active(*id)).collect();
614 for id in leave {
615 self.deliver_focus_within(id, false);
616 }
617 for id in enter {
618 self.deliver_focus_within(id, true);
619 }
620 self.focus_within_ancestors = desired;
621 }
622
623 fn deliver_focus_within(&mut self, id: ElementId, within: bool) {
628 let request_build = {
629 let Some(el) = self.elements.get_mut(id) else { return };
630 if let Some(state) = el.state.as_mut() {
631 state.handle_event(&WidgetEvent::FocusWithin(within), Point::ZERO);
632 state.take_needs_build()
633 } else {
634 el.widget.0.handle_event(&WidgetEvent::FocusWithin(within), Point::ZERO);
635 false
636 }
637 };
638 if request_build {
639 self.build_requests.push(id);
640 }
641 }
642
643 fn ancestor_ids(&self, id: ElementId) -> Vec<ElementId> {
645 let mut out = Vec::new();
646 let mut cursor = self.elements.get(id).and_then(|el| el.parent);
647 while let Some(pid) = cursor {
648 out.push(pid);
649 cursor = self.elements.get(pid).and_then(|el| el.parent);
650 }
651 out
652 }
653
654 fn nearest_focusable_ancestor(&self, id: ElementId) -> Option<ElementId> {
656 let mut cur = Some(id);
657 while let Some(cid) = cur {
658 let el = self.elements.get(cid)?;
659 if el.widget.0.is_focusable() {
660 return Some(cid);
661 }
662 cur = el.parent;
663 }
664 None
665 }
666}
667
668impl ElementTree {
669 pub fn dispatch_key_event(&mut self, event: &WidgetEvent) {
678 if let WidgetEvent::KeyDown(key, _mods) = event {
679 match key {
680 KeyCode::Tab | KeyCode::BackTab => {
681 let captured = self
683 .focused
684 .and_then(|id| self.elements.get(id))
685 .map(|el| el.widget.0.captures_tab())
686 .unwrap_or(false);
687 if !captured {
688 if matches!(key, KeyCode::Tab) {
689 self.focus_next();
690 } else {
691 self.focus_previous();
692 }
693 return;
694 }
695 }
696 KeyCode::Escape
697 if self.close_topmost_overlay() => {
700 return;
701 }
702 _ => {}
703 }
704 }
705 if let WidgetEvent::KeyDown(key, mods) = event {
708 if let Some(root) = self.overlays.last().map(|e| e.root_element) {
709 let handled = self
710 .elements
711 .get_mut(root)
712 .and_then(|el| el.state.as_mut())
713 .is_some_and(|state| state.handle_overlay_key(*key, *mods));
714 if handled {
715 self.mark_element_layout_dirty(root);
716 return;
717 }
718 }
719 }
720 if !self.focused.is_some_and(|id| self.is_active(id)) {
722 self.clear_focus();
723 }
724 if let Some(fid) = self.focused {
726 self.deliver_event(fid, event, Point::ZERO);
727 }
728 }
729
730 pub(crate) fn collect_focusable(&self) -> Vec<ElementId> {
739 let revision = self.semantics_revision();
742 if let Some((cached_revision, chain)) = self.focus_chain_cache.borrow().as_ref() {
743 if *cached_revision == revision {
744 return chain.clone();
745 }
746 }
747
748 let mut out = Vec::new();
749 if let Some(entry) = self.overlays.iter().rev().find(|e| e.focus.is_modal()) {
750 self.collect_focusable_recursive(entry.root_element, &mut out);
751 } else {
752 if let Some(root) = self.root {
753 self.collect_focusable_recursive(root, &mut out);
754 }
755 for entry in &self.overlays {
756 self.collect_focusable_recursive(entry.root_element, &mut out);
757 }
758 }
759 *self.focus_chain_cache.borrow_mut() = Some((revision, out.clone()));
760 out
761 }
762
763 pub(crate) fn collect_focusable_recursive(&self, id: ElementId, out: &mut Vec<ElementId>) {
764 self.collect_focusable_visible(id, true, out);
765 }
766
767 fn collect_focusable_visible(&self, id: ElementId, visible: bool, out: &mut Vec<ElementId>) {
770 if !visible {
771 return;
772 }
773 let Some(el) = self.elements.get(id) else { return };
774 if !el.active {
775 return;
776 }
777 if el.widget.0.is_focusable() {
778 out.push(id);
779 }
780 let flags = el.widget.0.visible_children();
781 for (index, &child) in el.children.iter().enumerate() {
782 if flags.as_ref().and_then(|f| f.get(index)) == Some(&false) {
783 continue;
784 }
785 self.collect_focusable_visible(child, true, out);
786 }
787 }
788
789 pub fn focus_next(&mut self) {
791 let chain = self.collect_focusable();
792 if chain.is_empty() {
793 self.clear_focus();
794 return;
795 }
796 let next = match self.focused {
797 Some(cur) if chain.contains(&cur) => {
798 let i = chain.iter().position(|&x| x == cur).unwrap();
799 chain[(i + 1) % chain.len()]
800 }
801 _ => chain[0],
803 };
804 self.request_focus(next);
805 }
806
807 pub fn focus_previous(&mut self) {
809 let chain = self.collect_focusable();
810 if chain.is_empty() {
811 self.clear_focus();
812 return;
813 }
814 let prev = match self.focused {
815 Some(cur) if chain.contains(&cur) => {
816 let i = chain.iter().position(|&x| x == cur).unwrap();
817 chain[(i + chain.len() - 1) % chain.len()]
818 }
819 _ => chain[chain.len() - 1],
820 };
821 self.request_focus(prev);
822 }
823
824 pub fn focused_wants_ime(&self) -> bool {
828 self.focused.and_then(|id| self.elements.get(id)).map(|el| el.widget.0.wants_ime()).unwrap_or(false)
829 }
830
831 pub fn element_origin(&self, id: ElementId) -> Option<Point> {
839 self.origin_map().get(&id).map(|(origin, _)| *origin)
840 }
841
842 pub fn process_overlay_actions(&mut self, now_ms: u64) {
848 self.process_overlay_intents(now_ms);
849 }
851
852 pub fn close_topmost_overlay(&mut self) -> bool {
862 let Some(entry) = self.overlays.last() else { return false };
863 let handle = entry.handle;
864 let requester = entry.requester_id;
865 self.hide_overlay(handle);
866 if self.is_active(requester) {
868 if let Some(el) = self.elements.get_mut(requester) {
869 if let Some(state_box) = el.state.as_mut() {
870 if let Some(oa) = state_box.as_overlay_aware() {
871 oa.on_overlay_force_closed(handle);
872 }
873 }
874 }
875 self.mark_element_layout_dirty(requester);
876 }
877 true
878 }
879
880 pub(crate) fn ensure_root_layer_laid_out(&mut self) {
902 let root_layer_id = match self.layer_tree.root {
903 Some(r) => r,
904 None => return,
905 };
906
907 #[cfg(feature = "frame-log")]
908 log::info!("[FRAME-OVERLAY] ensure_root_layer_laid_out begin, root_layer={:?}", root_layer_id);
909
910 let mut all_layers_dfs: Vec<LayerId> = Vec::new();
913 {
914 let mut stack: Vec<LayerId> =
916 self.layer_tree.layers.get(root_layer_id).map(|l| l.children.clone()).unwrap_or_default();
917 while let Some(lid) = stack.pop() {
918 all_layers_dfs.push(lid);
919 if let Some(layer) = self.layer_tree.layers.get(lid) {
920 for &child in &layer.children {
921 stack.push(child);
922 }
923 }
924 }
925 }
926
927 #[cfg(feature = "frame-log")]
928 log::info!("[FRAME-OVERLAY] ensure_root_layer_laid_out DFS layers={:?}", all_layers_dfs);
929
930 for &lid in &all_layers_dfs {
931 let root_el = match self.layer_tree.layers.get(lid).map(|l| l.root_element) {
932 Some(e) => e,
933 None => continue,
934 };
935 let active = self.elements.get(root_el).map(|e| e.active).unwrap_or(false);
937 if !active {
938 continue;
939 }
940
941 self.refresh_layer_subtree(root_el);
943
944 {
947 let el = match self.elements.get_mut(root_el) {
948 Some(e) => e,
949 None => continue,
950 };
951 el.render_object.layout_cached(
952 crate::render_object::BoxConstraints::UNBOUNDED,
953 &mut crate::render_object::LayoutContext,
954 );
955
956 #[cfg(feature = "frame-log")]
957 {
958 let ro_size = el.render_object.borrow().content_size();
959 log::info!(
960 "[FRAME-OVERLAY] ensure_root layout layer={:?} root_el={:?} ro_size={:?}",
961 lid,
962 root_el,
963 ro_size
964 );
965 }
966 }
967 }
968 }
969
970 pub(crate) fn compute_tooltip_target_rect(&self, eid: ElementId) -> crate::Rect {
981 let size =
986 self.elements.get(eid).map(|el| el.render_object.borrow().content_size()).unwrap_or(crate::Size::ZERO);
987
988 if let Some(origin) = self.element_origin(eid) {
989 debug_assert!(
990 size.width >= 0.5 && size.height >= 0.5,
991 "compute_tooltip_target_rect found eid={:?} but content_size is ZERO! \
992 width={}, height={}. Component likely forgot to override content_size().",
993 eid,
994 size.width,
995 size.height,
996 );
997 let rect = crate::Rect::new(origin.x, origin.y, size.width, size.height);
998 #[cfg(feature = "frame-log")]
999 log::info!("[FRAME-OVERLAY] target_rect eid={:?} origin={:?} size={:?} rect={:?}", eid, origin, size, rect);
1000 return rect;
1001 }
1002
1003 let fallback = self.elements.get(eid).map(|el| {
1005 let layer_off = el
1006 .layer
1007 .map(|lid| {
1008 let mut off = Point::ZERO;
1009 let mut cur = Some(lid);
1010 while let Some(layer) = cur.and_then(|id| self.layer_tree.layers.get(id)) {
1011 off = off + layer.offset;
1012 cur = layer.parent;
1013 }
1014 off
1015 })
1016 .unwrap_or(Point::ZERO);
1017 crate::Rect::new(
1018 layer_off.x + el.layout_offset.x,
1019 layer_off.y + el.layout_offset.y,
1020 size.width,
1021 size.height,
1022 )
1023 });
1024 let rect = fallback.unwrap_or(crate::Rect::ZERO);
1025
1026 #[cfg(feature = "frame-log")]
1027 log::warn!("[FRAME-OVERLAY] target_rect eid={:?} not located via element tree; fallback={:?}", eid, rect);
1028
1029 rect
1030 }
1031}
1032
1033pub(crate) fn compute_tooltip_position(
1035 target_rect: crate::Rect,
1036 bubble_size: crate::Size,
1037 placement: crate::widget::overlay_host::AnchorPlacement,
1038 gap: f32,
1039 offset: Point,
1040) -> (f32, f32) {
1041 use crate::widget::overlay_host::AnchorPlacement as AP;
1042 let tx = target_rect.x;
1043 let ty = target_rect.y;
1044 let tw = target_rect.width;
1045 let th = target_rect.height;
1046 let bw = bubble_size.width;
1047 let bh = bubble_size.height;
1048 let (bx, by) = match placement {
1049 AP::TopLeft => (tx, ty - bh - gap),
1050 AP::Top => (tx + tw * 0.5 - bw * 0.5, ty - bh - gap),
1051 AP::TopRight => (tx + tw - bw, ty - bh - gap),
1052 AP::Left => (tx - bw - gap, ty + th * 0.5 - bh * 0.5),
1053 AP::Right => (tx + tw + gap, ty + th * 0.5 - bh * 0.5),
1054 AP::BottomLeft => (tx, ty + th + gap),
1055 AP::Bottom => (tx + tw * 0.5 - bw * 0.5, ty + th + gap),
1056 AP::BottomRight => (tx + tw - bw, ty + th + gap),
1057 AP::InsideTopRight => (tx + tw - bw, ty + gap),
1059 };
1060 (bx + offset.x, by + offset.y)
1061}