From ea8b5016f783e24f54f9179dc7d9abc19b6dcf51 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 9 Jun 2022 13:21:27 +0200 Subject: [PATCH] Snap overlay's bottom/right edge to same window's edge on overflow --- crates/context_menu/src/context_menu.rs | 2 +- crates/editor/src/element.rs | 7 +++++++ crates/gpui/src/elements/overlay.rs | 20 +++++++++++--------- crates/gpui/src/elements/tooltip.rs | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/crates/context_menu/src/context_menu.rs b/crates/context_menu/src/context_menu.rs index 9a37e97d8c55913ff8e055c78b5ac4613d90afa7..6915bd775335b4fd933e5bf61be9a10c6d1e6e4f 100644 --- a/crates/context_menu/src/context_menu.rs +++ b/crates/context_menu/src/context_menu.rs @@ -94,7 +94,7 @@ impl View for ContextMenu { Overlay::new(expanded_menu) .hoverable(true) - .align_to_fit(true) + .move_to_fit(true) .with_abs_position(self.position) .boxed() } diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index d73569a03f8f0f9e3c35b4e9277ad9dacce088c6..a62a75debcbc1da50bebe0347a8983a7c6080359 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -491,8 +491,15 @@ impl EditorElement { let x = cursor_row_layout.x_for_index(position.column() as usize) - scroll_left; let y = (position.row() + 1) as f32 * layout.line_height - scroll_top; let mut list_origin = content_origin + vec2f(x, y); + let list_width = context_menu.size().x(); let list_height = context_menu.size().y(); + // Snap the right edge of the list to the right edge of the window if + // its horizontal bounds overflow. + if list_origin.x() + list_width > cx.window_size.x() { + list_origin.set_x((cx.window_size.x() - list_width).max(0.)); + } + if list_origin.y() + list_height > bounds.max_y() { list_origin.set_y(list_origin.y() - layout.line_height - list_height); } diff --git a/crates/gpui/src/elements/overlay.rs b/crates/gpui/src/elements/overlay.rs index 6010cfcd6af239a3da6e76a8e585cfe35efd49aa..1843b65c10a10dcf7818c113e3ac2bc2f6aeebdf 100644 --- a/crates/gpui/src/elements/overlay.rs +++ b/crates/gpui/src/elements/overlay.rs @@ -10,7 +10,7 @@ use crate::{ pub struct Overlay { child: ElementBox, abs_position: Option, - align_to_fit: bool, + move_to_fit: bool, hoverable: bool, } @@ -19,7 +19,7 @@ impl Overlay { Self { child, abs_position: None, - align_to_fit: false, + move_to_fit: false, hoverable: false, } } @@ -29,8 +29,8 @@ impl Overlay { self } - pub fn align_to_fit(mut self, align_to_fit: bool) -> Self { - self.align_to_fit = align_to_fit; + pub fn move_to_fit(mut self, align_to_fit: bool) -> Self { + self.move_to_fit = align_to_fit; self } @@ -76,15 +76,17 @@ impl Element for Overlay { }); } - if self.align_to_fit { - // Align overlay to the left if its bounds overflow the window width. + if self.move_to_fit { + // Snap the right edge of the overlay to the right edge of the window if + // its horizontal bounds overflow. if bounds.lower_right().x() > cx.window_size.x() { - bounds.set_origin_x(bounds.origin_x() - bounds.width()); + bounds.set_origin_x((cx.window_size.x() - bounds.width()).max(0.)); } - // Align overlay to the top if its bounds overflow the window height. + // Snap the bottom edge of the overlay to the bottom edge of the window if + // its vertical bounds overflow. if bounds.lower_right().y() > cx.window_size.y() { - bounds.set_origin_y(bounds.origin_y() - bounds.height()); + bounds.set_origin_y((cx.window_size.y() - bounds.height()).max(0.)); } } diff --git a/crates/gpui/src/elements/tooltip.rs b/crates/gpui/src/elements/tooltip.rs index a1b91a9faa203f453921f9e8c7a86703e2901d49..252d81e46d4f653f3a25780c104cb79cfe074cc9 100644 --- a/crates/gpui/src/elements/tooltip.rs +++ b/crates/gpui/src/elements/tooltip.rs @@ -79,7 +79,7 @@ impl Tooltip { }) .boxed(), ) - .align_to_fit(true) + .move_to_fit(true) .with_abs_position(state.position.get()) .boxed(), )