1use gpui::{
8 Anchor, AnyElement, App, Bounds, Decorations, Display, Edges, Element, GlobalElementId,
9 Half as _, HitboxBehavior, InspectorElementId, LayoutId, ParentElement, Pixels, Point,
10 Position, Size, Style, Window, point, px,
11};
12
13use crate::{LayoutDirection, Placement};
14use std::{cell::Cell, rc::Rc};
15
16#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
18pub enum Align {
19 Start,
21 #[default]
23 Center,
24 End,
26}
27
28#[derive(Clone, Copy, Debug, PartialEq)]
30enum Strategy {
31 Corner {
35 anchor: Anchor,
36 position: Point<Pixels>,
37 },
38 Side {
41 trigger_bounds: Bounds<Pixels>,
42 placement: Option<Placement>,
43 align: Align,
44 offset: Pixels,
45 },
46}
47
48impl Align {
49 fn horizontal(self, direction: LayoutDirection) -> Self {
52 match (self, direction) {
53 (Align::Start, LayoutDirection::Rtl) => Align::End,
54 (Align::End, LayoutDirection::Rtl) => Align::Start,
55 (align, _) => align,
56 }
57 }
58}
59
60#[derive(Clone, Copy, Debug, PartialEq)]
62pub struct ResolvedPosition {
63 pub bounds: Bounds<Pixels>,
65 pub placement: Option<Placement>,
68}
69
70pub struct Positioner {
76 strategy: Strategy,
77 corner_position: Option<Rc<Cell<Point<Pixels>>>>,
78 on_position: Option<Box<dyn Fn(ResolvedPosition)>>,
79 margin: Pixels,
80 boundary: Option<Bounds<Pixels>>,
81 occlude: bool,
82 children: Vec<AnyElement>,
83}
84
85#[doc(hidden)]
86pub struct PositionerState {
87 child_layout_ids: Vec<LayoutId>,
88}
89
90impl Positioner {
91 pub fn side(trigger_bounds: Bounds<Pixels>) -> Self {
93 Self {
94 strategy: Strategy::Side {
95 trigger_bounds,
96 placement: None,
97 align: Align::Center,
98 offset: px(0.),
99 },
100 corner_position: None,
101 on_position: None,
102 margin: px(4.),
103 boundary: None,
104 occlude: false,
105 children: Vec::new(),
106 }
107 }
108
109 pub fn corner(anchor: Anchor, position: Point<Pixels>) -> Self {
115 Self {
116 strategy: Strategy::Corner { anchor, position },
117 corner_position: None,
118 on_position: None,
119 margin: px(4.),
120 boundary: None,
121 occlude: false,
122 children: Vec::new(),
123 }
124 }
125
126 pub fn position(mut self, position: Point<Pixels>) -> Self {
131 if let Strategy::Corner {
132 position: current, ..
133 } = &mut self.strategy
134 {
135 *current = position;
136 }
137 self
138 }
139
140 pub fn placement(mut self, placement: Placement) -> Self {
142 if let Strategy::Side {
143 placement: slot, ..
144 } = &mut self.strategy
145 {
146 *slot = Some(placement);
147 }
148 self
149 }
150
151 pub fn align(mut self, align: Align) -> Self {
154 if let Strategy::Side { align: slot, .. } = &mut self.strategy {
155 *slot = align;
156 }
157 self
158 }
159
160 pub fn offset(mut self, offset: Pixels) -> Self {
163 if let Strategy::Side { offset: slot, .. } = &mut self.strategy {
164 *slot = offset;
165 }
166 self
167 }
168
169 pub(crate) fn tracked_corner_position(mut self, position: Rc<Cell<Point<Pixels>>>) -> Self {
171 self.corner_position = Some(position);
172 self
173 }
174
175 pub fn on_position(mut self, callback: impl Fn(ResolvedPosition) + 'static) -> Self {
177 self.on_position = Some(Box::new(callback));
178 self
179 }
180
181 pub fn occlude(mut self) -> Self {
188 self.occlude = true;
189 self
190 }
191
192 pub fn margin(mut self, margin: Pixels) -> Self {
194 self.margin = margin;
195 self
196 }
197
198 pub fn boundary(mut self, boundary: Bounds<Pixels>) -> Self {
202 self.boundary = Some(boundary);
203 self
204 }
205}
206
207fn boundary_margin(
210 margin: Edges<Pixels>,
211 boundary: Bounds<Pixels>,
212 viewport_size: Size<Pixels>,
213) -> Edges<Pixels> {
214 Edges {
215 top: margin.top.max(boundary.top()),
216 right: margin.right.max(viewport_size.width - boundary.right()),
217 bottom: margin.bottom.max(viewport_size.height - boundary.bottom()),
218 left: margin.left.max(boundary.left()),
219 }
220}
221
222fn frame_insets(decorations: Decorations, client_inset: Pixels) -> Edges<Pixels> {
233 match decorations {
234 Decorations::Server => Edges::default(),
235 Decorations::Client { tiling } => Edges {
236 top: if tiling.top { px(0.) } else { client_inset },
237 right: if tiling.right { px(0.) } else { client_inset },
238 bottom: if tiling.bottom { px(0.) } else { client_inset },
239 left: if tiling.left { px(0.) } else { client_inset },
240 },
241 }
242}
243
244#[cfg(test)]
250fn resolve(
251 strategy: Strategy,
252 popup_size: Size<Pixels>,
253 viewport_size: Size<Pixels>,
254 margin: Edges<Pixels>,
255) -> ResolvedPosition {
256 resolve_in(
257 strategy,
258 popup_size,
259 viewport_size,
260 margin,
261 LayoutDirection::Ltr,
262 )
263}
264
265fn resolve_in(
268 strategy: Strategy,
269 popup_size: Size<Pixels>,
270 viewport_size: Size<Pixels>,
271 margin: Edges<Pixels>,
272 direction: LayoutDirection,
273) -> ResolvedPosition {
274 match strategy {
275 Strategy::Corner { anchor, position } => ResolvedPosition {
276 bounds: clamp(
277 Bounds::from_anchor_and_size(anchor, position, popup_size),
278 viewport_size,
279 margin,
280 ),
281 placement: None,
282 },
283 Strategy::Side {
284 trigger_bounds,
285 placement,
286 align,
287 offset,
288 } => {
289 let placement =
290 resolve_placement(trigger_bounds, popup_size, viewport_size, margin, placement);
291 let origin = side_origin(
292 trigger_bounds,
293 popup_size,
294 placement,
295 align,
296 offset,
297 direction,
298 );
299 ResolvedPosition {
300 bounds: clamp(Bounds::new(origin, popup_size), viewport_size, margin),
301 placement: Some(placement),
302 }
303 }
304 }
305}
306
307fn resolve_placement(
308 trigger_bounds: Bounds<Pixels>,
309 popup_size: Size<Pixels>,
310 viewport_size: Size<Pixels>,
311 margin: Edges<Pixels>,
312 preferred: Option<Placement>,
313) -> Placement {
314 let right_limit = (viewport_size.width - margin.right).max(margin.left);
315 let bottom_limit = (viewport_size.height - margin.bottom).max(margin.top);
316 let available_left = (trigger_bounds.left() - margin.left).max(px(0.));
317 let available_right = (right_limit - trigger_bounds.right()).max(px(0.));
318 let available_above = (trigger_bounds.top() - margin.top).max(px(0.));
319 let available_below = (bottom_limit - trigger_bounds.bottom()).max(px(0.));
320
321 match preferred {
322 Some(Placement::Right) if popup_size.width <= available_right => Placement::Right,
323 Some(Placement::Right) if popup_size.width <= available_left => Placement::Left,
324 Some(Placement::Right) if available_right >= available_left => Placement::Right,
325 Some(Placement::Right) => Placement::Left,
326 Some(Placement::Left) if popup_size.width <= available_left => Placement::Left,
327 Some(Placement::Left) if popup_size.width <= available_right => Placement::Right,
328 Some(Placement::Left) if available_left >= available_right => Placement::Left,
329 Some(Placement::Left) => Placement::Right,
330 Some(Placement::Bottom) if popup_size.height <= available_below => Placement::Bottom,
331 Some(Placement::Bottom) if popup_size.height <= available_above => Placement::Top,
332 Some(Placement::Bottom) if available_below >= available_above => Placement::Bottom,
333 Some(Placement::Bottom) => Placement::Top,
334 Some(Placement::Top) | None if popup_size.height <= available_above => Placement::Top,
335 Some(Placement::Top) | None if popup_size.height <= available_below => Placement::Bottom,
336 Some(Placement::Top) | None if available_below >= available_above => Placement::Bottom,
337 Some(Placement::Top) | None => Placement::Top,
338 }
339}
340
341fn side_origin(
342 trigger_bounds: Bounds<Pixels>,
343 popup_size: Size<Pixels>,
344 placement: Placement,
345 align: Align,
346 offset: Pixels,
347 direction: LayoutDirection,
348) -> Point<Pixels> {
349 let aligned_x = match align.horizontal(direction) {
350 Align::Start => trigger_bounds.left(),
351 Align::Center => trigger_bounds.center().x - popup_size.width.half(),
352 Align::End => trigger_bounds.right() - popup_size.width,
353 };
354 let aligned_y = match align {
355 Align::Start => trigger_bounds.top(),
356 Align::Center => trigger_bounds.center().y - popup_size.height.half(),
357 Align::End => trigger_bounds.bottom() - popup_size.height,
358 };
359
360 match placement {
361 Placement::Top => point(aligned_x, trigger_bounds.top() - popup_size.height - offset),
362 Placement::Bottom => point(aligned_x, trigger_bounds.bottom() + offset),
363 Placement::Left => point(trigger_bounds.left() - popup_size.width - offset, aligned_y),
364 Placement::Right => point(trigger_bounds.right() + offset, aligned_y),
365 }
366}
367
368fn clamp(
369 mut bounds: Bounds<Pixels>,
370 viewport_size: Size<Pixels>,
371 margin: Edges<Pixels>,
372) -> Bounds<Pixels> {
373 let right_limit = (viewport_size.width - margin.right).max(margin.left);
374 let bottom_limit = (viewport_size.height - margin.bottom).max(margin.top);
375
376 if bounds.right() > right_limit {
377 bounds.origin.x -= bounds.right() - right_limit;
378 }
379 if bounds.left() < margin.left {
380 bounds.origin.x = margin.left;
381 }
382 if bounds.bottom() > bottom_limit {
383 bounds.origin.y -= bounds.bottom() - bottom_limit;
384 }
385 if bounds.top() < margin.top {
386 bounds.origin.y = margin.top;
387 }
388
389 bounds
390}
391
392impl ParentElement for Positioner {
393 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
394 self.children.extend(elements);
395 }
396}
397
398impl Element for Positioner {
399 type RequestLayoutState = PositionerState;
400 type PrepaintState = ();
401
402 fn id(&self) -> Option<gpui::ElementId> {
403 None
404 }
405
406 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
407 None
408 }
409
410 fn request_layout(
411 &mut self,
412 _: Option<&GlobalElementId>,
413 _: Option<&InspectorElementId>,
414 window: &mut Window,
415 cx: &mut App,
416 ) -> (LayoutId, Self::RequestLayoutState) {
417 let child_layout_ids = self
418 .children
419 .iter_mut()
420 .map(|child| child.request_layout(window, cx))
421 .collect::<Vec<_>>();
422 let layout_id = window.request_layout(
423 Style {
424 position: Position::Absolute,
425 display: Display::Flex,
426 ..Style::default()
427 },
428 child_layout_ids.iter().copied(),
429 cx,
430 );
431
432 (layout_id, PositionerState { child_layout_ids })
433 }
434
435 fn prepaint(
436 &mut self,
437 _: Option<&GlobalElementId>,
438 _: Option<&InspectorElementId>,
439 bounds: Bounds<Pixels>,
440 request_layout: &mut Self::RequestLayoutState,
441 window: &mut Window,
442 cx: &mut App,
443 ) {
444 if request_layout.child_layout_ids.is_empty() {
445 return;
446 }
447
448 let mut child_min = point(Pixels::MAX, Pixels::MAX);
449 let mut child_max = Point::default();
450 for child_layout_id in &request_layout.child_layout_ids {
451 let child_bounds = window.layout_bounds(*child_layout_id);
452 child_min = child_min.min(&child_bounds.origin);
453 child_max = child_max.max(&child_bounds.bottom_right());
454 }
455
456 let popup_size = (child_max - child_min).into();
457 let frame = frame_insets(
458 window.window_decorations(),
459 window.client_inset().unwrap_or(px(0.)),
460 );
461 let mut strategy = self.strategy;
462 if let (Strategy::Corner { position, .. }, Some(tracked)) =
463 (&mut strategy, &self.corner_position)
464 {
465 *position = tracked.get();
466 }
467 let viewport_size = window.viewport_size();
468 let margin = frame.map(|inset| *inset + self.margin);
469 let position = resolve_in(
470 strategy,
471 popup_size,
472 viewport_size,
473 match self.boundary {
474 Some(boundary) => boundary_margin(margin, boundary, viewport_size),
475 None => margin,
476 },
477 LayoutDirection::global(cx),
478 );
479 if let Some(callback) = &self.on_position {
480 callback(position);
481 }
482 if self.occlude {
485 window.insert_hitbox(position.bounds, HitboxBehavior::BlockMouse);
486 }
487
488 let offset = position.bounds.origin - bounds.origin;
489 let offset = point(offset.x.round(), offset.y.round());
490
491 window.with_element_offset(offset, |window| {
492 for child in &mut self.children {
493 child.prepaint(window, cx);
494 }
495 });
496 }
497
498 fn paint(
499 &mut self,
500 _: Option<&GlobalElementId>,
501 _: Option<&InspectorElementId>,
502 _: Bounds<Pixels>,
503 _: &mut Self::RequestLayoutState,
504 _: &mut Self::PrepaintState,
505 window: &mut Window,
506 cx: &mut App,
507 ) {
508 for child in &mut self.children {
509 child.paint(window, cx);
510 }
511 }
512}
513
514#[cfg(test)]
515mod tests {
516 use super::*;
517 use gpui::Tiling;
518
519 const MARGIN: Pixels = px(4.);
520
521 fn trigger(x: f32, y: f32, w: f32, h: f32) -> Bounds<Pixels> {
522 Bounds::new(point(px(x), px(y)), Size::new(px(w), px(h)))
523 }
524
525 fn viewport() -> Size<Pixels> {
526 Size::new(px(500.), px(400.))
527 }
528
529 fn side(
530 trigger_bounds: Bounds<Pixels>,
531 placement: Option<Placement>,
532 align: Align,
533 popup: Size<Pixels>,
534 ) -> ResolvedPosition {
535 resolve(
536 Strategy::Side {
537 trigger_bounds,
538 placement,
539 align,
540 offset: px(0.),
541 },
542 popup,
543 viewport(),
544 Edges::all(MARGIN),
545 )
546 }
547
548 #[test]
549 fn prefers_the_requested_side_when_it_fits() {
550 let position = side(
551 trigger(200., 200., 40., 20.),
552 Some(Placement::Top),
553 Align::Center,
554 Size::new(px(80.), px(30.)),
555 );
556
557 assert_eq!(position.placement, Some(Placement::Top));
558 assert_eq!(position.bounds.bottom(), px(200.));
559 }
560
561 #[test]
562 fn flips_to_the_opposite_side_when_the_preferred_side_does_not_fit() {
563 let position = side(
564 trigger(200., 10., 40., 20.),
565 Some(Placement::Top),
566 Align::Center,
567 Size::new(px(80.), px(60.)),
568 );
569
570 assert_eq!(position.placement, Some(Placement::Bottom));
571 assert_eq!(position.bounds.top(), px(30.));
572 }
573
574 #[test]
575 fn clamps_into_the_viewport_while_keeping_the_flipped_side() {
576 let position = side(
577 trigger(480., 200., 40., 20.),
578 Some(Placement::Bottom),
579 Align::Center,
580 Size::new(px(120.), px(30.)),
581 );
582
583 assert_eq!(position.placement, Some(Placement::Bottom));
584 assert_eq!(position.bounds.right(), viewport().width - MARGIN);
585 }
586
587 #[test]
588 fn alignment_selects_the_leading_center_or_trailing_edge() {
589 let trigger_bounds = trigger(200., 200., 100., 20.);
590 let popup = Size::new(px(40.), px(30.));
591
592 let start = side(trigger_bounds, Some(Placement::Bottom), Align::Start, popup);
593 let center = side(
594 trigger_bounds,
595 Some(Placement::Bottom),
596 Align::Center,
597 popup,
598 );
599 let end = side(trigger_bounds, Some(Placement::Bottom), Align::End, popup);
600
601 assert_eq!(start.bounds.left(), px(200.));
602 assert_eq!(center.bounds.left(), px(230.));
603 assert_eq!(end.bounds.left(), px(260.));
604 }
605
606 #[test]
607 fn rtl_aligns_start_with_the_right_edge_of_the_trigger() {
608 let strategy = |align| Strategy::Side {
609 trigger_bounds: trigger(200., 200., 100., 20.),
610 placement: Some(Placement::Bottom),
611 align,
612 offset: px(0.),
613 };
614 let popup = Size::new(px(40.), px(30.));
615 let resolve_rtl = |align| {
616 resolve_in(
617 strategy(align),
618 popup,
619 viewport(),
620 Edges::all(MARGIN),
621 LayoutDirection::Rtl,
622 )
623 };
624
625 assert_eq!(resolve_rtl(Align::Start).bounds.right(), px(300.));
626 assert_eq!(resolve_rtl(Align::Center).bounds.left(), px(230.));
627 assert_eq!(resolve_rtl(Align::End).bounds.left(), px(200.));
628 }
629
630 #[test]
631 fn side_offset_adds_a_gap_between_trigger_and_popup() {
632 let position = resolve(
633 Strategy::Side {
634 trigger_bounds: trigger(200., 200., 40., 20.),
635 placement: Some(Placement::Bottom),
636 align: Align::Center,
637 offset: px(8.),
638 },
639 Size::new(px(40.), px(30.)),
640 viewport(),
641 Edges::all(MARGIN),
642 );
643
644 assert_eq!(position.bounds.top(), px(228.));
645 }
646
647 #[test]
648 fn corner_positioning_places_the_named_corner_and_never_reports_a_side() {
649 let position = resolve(
650 Strategy::Corner {
651 anchor: Anchor::TopLeft,
652 position: point(px(100.), px(100.)),
653 },
654 Size::new(px(40.), px(30.)),
655 viewport(),
656 Edges::all(MARGIN),
657 );
658
659 assert_eq!(position.placement, None);
660 assert_eq!(position.bounds.origin, point(px(100.), px(100.)));
661 }
662
663 #[test]
664 fn corner_positioning_clamps_but_does_not_flip() {
665 let position = resolve(
666 Strategy::Corner {
667 anchor: Anchor::TopLeft,
668 position: point(px(480.), px(390.)),
669 },
670 Size::new(px(40.), px(30.)),
671 viewport(),
672 Edges::all(MARGIN),
673 );
674
675 assert_eq!(position.placement, None);
676 assert_eq!(position.bounds.right(), viewport().width - MARGIN);
677 assert_eq!(position.bounds.bottom(), viewport().height - MARGIN);
678 }
679
680 #[test]
683 fn a_boundary_keeps_the_popup_inside_it() {
684 let boundary = trigger(100., 0., 300., 400.);
685 let margin = boundary_margin(Edges::all(MARGIN), boundary, viewport());
686 let corner = |x| Strategy::Corner {
687 anchor: Anchor::TopRight,
688 position: point(px(x), px(100.)),
689 };
690 let popup = Size::new(px(200.), px(30.));
691
692 let window_only = resolve(corner(250.), popup, viewport(), Edges::all(MARGIN));
693 assert_eq!(window_only.bounds.left(), px(50.));
694
695 let bounded = resolve(corner(250.), popup, viewport(), margin);
696 assert_eq!(bounded.bounds.left(), px(100.));
697
698 let side = resolve(
699 Strategy::Side {
700 trigger_bounds: trigger(360., 200., 30., 20.),
701 placement: Some(Placement::Right),
702 align: Align::Center,
703 offset: px(0.),
704 },
705 Size::new(px(80.), px(30.)),
706 viewport(),
707 margin,
708 );
709 assert_eq!(side.placement, Some(Placement::Left));
710 assert!(side.bounds.right() <= px(400.));
711 }
712
713 #[test]
714 fn a_boundary_past_the_window_edge_keeps_the_window_margin() {
715 let boundary = trigger(-50., -50., 600., 500.);
716 assert_eq!(
717 boundary_margin(Edges::all(MARGIN), boundary, viewport()),
718 Edges::all(MARGIN)
719 );
720 }
721
722 #[test]
723 fn corner_position_can_be_updated_without_rebuilding_the_positioner() {
724 let positioner = Positioner::corner(Anchor::TopLeft, point(px(10.), px(20.)))
725 .position(point(px(30.), px(40.)));
726
727 assert!(matches!(
728 positioner.strategy,
729 Strategy::Corner {
730 position,
731 ..
732 } if position == point(px(30.), px(40.))
733 ));
734 }
735
736 #[test]
739 fn frame_insets_apply_the_client_inset_only_on_untiled_edges() {
740 let inset = px(20.);
741
742 assert_eq!(
743 frame_insets(Decorations::Server, inset),
744 Edges::default(),
745 "a server-decorated window has no frame to keep clear of"
746 );
747 assert_eq!(
748 frame_insets(
749 Decorations::Client {
750 tiling: Tiling::tiled()
751 },
752 inset
753 ),
754 Edges::default(),
755 "a window tiled on every side draws no shadow padding"
756 );
757 assert_eq!(
758 frame_insets(
759 Decorations::Client {
760 tiling: Tiling {
761 top: false,
762 left: true,
763 right: false,
764 bottom: true,
765 }
766 },
767 inset
768 ),
769 Edges {
770 top: inset,
771 right: inset,
772 bottom: px(0.),
773 left: px(0.),
774 }
775 );
776 }
777
778 #[test]
782 fn clamping_keeps_only_the_margin_on_a_tiled_edge() {
783 let popup = Size::new(px(120.), px(30.));
784 let corner = Strategy::Corner {
786 anchor: Anchor::TopRight,
787 position: point(viewport().width - px(10.), px(100.)),
788 };
789 let margin = Edges::all(MARGIN);
790 let inset = px(20.);
791
792 let tiled = resolve(corner, popup, viewport(), margin);
793 assert_eq!(tiled.bounds.right(), viewport().width - px(10.));
794
795 let untiled = resolve(
796 corner,
797 popup,
798 viewport(),
799 margin.map(|margin| *margin + inset),
800 );
801 assert_eq!(untiled.bounds.right(), viewport().width - MARGIN - inset);
802 }
803
804 const WINDOW_MARGIN: Pixels = px(4.);
808
809 fn tooltip_placement(
810 trigger_bounds: Bounds<Pixels>,
811 popup: Size<Pixels>,
812 viewport: Size<Pixels>,
813 margin: Pixels,
814 placement: Option<Placement>,
815 ) -> (Bounds<Pixels>, Placement) {
816 let resolved = resolve(
817 Strategy::Side {
818 trigger_bounds,
819 placement,
820 align: Align::Center,
821 offset: px(0.),
822 },
823 popup,
824 viewport,
825 Edges::all(margin),
826 );
827 (resolved.bounds, resolved.placement.unwrap())
828 }
829
830 fn bounds(x: f32, y: f32, width: f32, height: f32) -> Bounds<Pixels> {
831 Bounds::new(point(px(x), px(y)), Size::new(px(width), px(height)))
832 }
833
834 fn size_px(width: f32, height: f32) -> Size<Pixels> {
835 Size::new(px(width), px(height))
836 }
837
838 #[test]
839 fn prefers_above_when_space_allows() {
840 let trigger = bounds(100., 80., 80., 24.);
841 let position = tooltip_placement(
842 trigger,
843 size_px(120., 30.),
844 size_px(300., 200.),
845 WINDOW_MARGIN,
846 None,
847 );
848 assert_eq!(position.1, Placement::Top);
849 assert_eq!(position.0.origin, point(px(80.), px(50.)));
850 }
851 #[test]
852 fn flips_and_clamps_on_each_axis() {
853 let top = tooltip_placement(
854 bounds(24., 4., 120., 32.),
855 size_px(240., 32.),
856 size_px(520., 260.),
857 WINDOW_MARGIN,
858 None,
859 );
860 assert_eq!(top.1, Placement::Bottom);
861
862 let right = tooltip_placement(
863 bounds(260., 60., 32., 32.),
864 size_px(120., 30.),
865 size_px(300., 200.),
866 WINDOW_MARGIN,
867 Some(Placement::Right),
868 );
869 assert_eq!(right.1, Placement::Left);
870
871 let left_edge = tooltip_placement(
872 bounds(4., 80., 24., 24.),
873 size_px(120., 30.),
874 size_px(300., 200.),
875 WINDOW_MARGIN,
876 None,
877 );
878 assert_eq!(left_edge.0.left(), WINDOW_MARGIN);
879 }
880 #[test]
881 fn places_tooltip_to_the_right() {
882 let trigger = bounds(20., 60., 32., 32.);
883 let position = tooltip_placement(
884 trigger,
885 size_px(120., 30.),
886 size_px(300., 200.),
887 WINDOW_MARGIN,
888 Some(Placement::Right),
889 );
890 assert_eq!(position.1, Placement::Right);
891 assert_eq!(position.0.left(), trigger.right());
892 assert_eq!(position.0.center().y, trigger.center().y);
893 }
894 #[test]
895 fn right_placement_clamps_vertical_edges() {
896 let trigger = bounds(20., 2., 32., 20.);
897 let position = tooltip_placement(
898 trigger,
899 size_px(120., 40.),
900 size_px(300., 200.),
901 WINDOW_MARGIN,
902 Some(Placement::Right),
903 );
904 assert_eq!(position.1, Placement::Right);
905 assert_eq!(position.0.top(), WINDOW_MARGIN);
906 assert_eq!(position.0.left(), trigger.right());
907 }
908 #[test]
909 fn uses_larger_side_when_neither_vertical_side_fits() {
910 let position = tooltip_placement(
911 bounds(120., 20., 40., 20.),
912 size_px(160., 120.),
913 size_px(300., 100.),
914 WINDOW_MARGIN,
915 None,
916 );
917 assert_eq!(position.1, Placement::Bottom);
918 assert_eq!(position.0.top(), WINDOW_MARGIN);
919 assert_eq!(position.0.left(), px(60.));
920 }
921}