Align tooltip based on the available window space

Antonio Scandurra created

Change summary

crates/editor/src/element.rs        |  2 +-
crates/gpui/src/elements/tooltip.rs | 11 +++++++++--
crates/gpui/src/presenter.rs        |  5 ++++-
3 files changed, 14 insertions(+), 4 deletions(-)

Detailed changes

crates/editor/src/element.rs 🔗

@@ -1619,7 +1619,7 @@ mod tests {
 
         // Don't panic.
         let bounds = RectF::new(Default::default(), size);
-        let mut paint_cx = presenter.build_paint_context(&mut scene, cx);
+        let mut paint_cx = presenter.build_paint_context(&mut scene, bounds.size(), cx);
         element.paint(bounds, bounds, &mut state, &mut paint_cx);
     }
 }

crates/gpui/src/elements/tooltip.rs 🔗

@@ -101,9 +101,16 @@ impl Element for Tooltip {
         self.child.paint(bounds.origin(), visible_bounds, cx);
         if let Some(tooltip) = self.tooltip.as_mut() {
             let origin = self.state.read(cx).position.get();
-            let size = tooltip.size();
+            let mut bounds = RectF::new(origin, tooltip.size());
+            if bounds.lower_right().x() > cx.window_size.x() {
+                bounds.set_origin_x(bounds.origin_x() - bounds.width());
+            }
+            if bounds.lower_right().y() > cx.window_size.y() {
+                bounds.set_origin_y(bounds.origin_y() - bounds.height());
+            }
+
             cx.scene.push_stacking_context(None);
-            tooltip.paint(origin, RectF::new(origin, size), cx);
+            tooltip.paint(bounds.origin(), bounds, cx);
             cx.scene.pop_stacking_context();
         }
     }

crates/gpui/src/presenter.rs 🔗

@@ -148,7 +148,7 @@ impl Presenter {
 
         if let Some(root_view_id) = cx.root_view_id(self.window_id) {
             self.layout(window_size, refreshing, cx);
-            let mut paint_cx = self.build_paint_context(&mut scene, cx);
+            let mut paint_cx = self.build_paint_context(&mut scene, window_size, cx);
             paint_cx.paint(
                 root_view_id,
                 Vector2F::zero(),
@@ -205,10 +205,12 @@ impl Presenter {
     pub fn build_paint_context<'a>(
         &'a mut self,
         scene: &'a mut Scene,
+        window_size: Vector2F,
         cx: &'a mut MutableAppContext,
     ) -> PaintContext {
         PaintContext {
             scene,
+            window_size,
             font_cache: &self.font_cache,
             text_layout_cache: &self.text_layout_cache,
             rendered_views: &mut self.rendered_views,
@@ -592,6 +594,7 @@ impl<'a> UpgradeViewHandle for LayoutContext<'a> {
 pub struct PaintContext<'a> {
     rendered_views: &'a mut HashMap<usize, ElementBox>,
     view_stack: Vec<usize>,
+    pub window_size: Vector2F,
     pub scene: &'a mut Scene,
     pub font_cache: &'a FontCache,
     pub text_layout_cache: &'a TextLayoutCache,