Align context menu to fit within the window bounds

This commit is contained in:
Antonio Scandurra 2022-06-02 09:47:06 +02:00
parent f1964cf2a0
commit 701e2090cd
3 changed files with 58 additions and 37 deletions

View file

@ -92,6 +92,8 @@ impl View for ContextMenu {
.boxed(); .boxed();
Overlay::new(expanded_menu) Overlay::new(expanded_menu)
.hoverable(true)
.align_to_fit(true)
.with_abs_position(self.position) .with_abs_position(self.position)
.boxed() .boxed()
} }

View file

@ -10,6 +10,8 @@ use crate::{
pub struct Overlay { pub struct Overlay {
child: ElementBox, child: ElementBox,
abs_position: Option<Vector2F>, abs_position: Option<Vector2F>,
align_to_fit: bool,
hoverable: bool,
} }
impl Overlay { impl Overlay {
@ -17,6 +19,8 @@ impl Overlay {
Self { Self {
child, child,
abs_position: None, abs_position: None,
align_to_fit: false,
hoverable: false,
} }
} }
@ -24,6 +28,16 @@ impl Overlay {
self.abs_position = Some(position); self.abs_position = Some(position);
self self
} }
pub fn align_to_fit(mut self, align_to_fit: bool) -> Self {
self.align_to_fit = align_to_fit;
self
}
pub fn hoverable(mut self, hoverable: bool) -> Self {
self.hoverable = hoverable;
self
}
} }
impl Element for Overlay { impl Element for Overlay {
@ -51,15 +65,30 @@ impl Element for Overlay {
size: &mut Self::LayoutState, size: &mut Self::LayoutState,
cx: &mut PaintContext, cx: &mut PaintContext,
) { ) {
let origin = self.abs_position.unwrap_or(bounds.origin()); let mut bounds = RectF::new(self.abs_position.unwrap_or(bounds.origin()), *size);
let visible_bounds = RectF::new(origin, *size);
cx.scene.push_stacking_context(None); cx.scene.push_stacking_context(None);
cx.scene.push_mouse_region(MouseRegion {
view_id: cx.current_view_id(), if self.hoverable {
bounds: visible_bounds, cx.scene.push_mouse_region(MouseRegion {
..Default::default() view_id: cx.current_view_id(),
}); bounds,
self.child.paint(origin, visible_bounds, cx); ..Default::default()
});
}
if self.align_to_fit {
// Align overlay to the left if its bounds overflow the window width.
if bounds.lower_right().x() > cx.window_size.x() {
bounds.set_origin_x(bounds.origin_x() - bounds.width());
}
// Align overlay to the top if its bounds overflow the window height.
if bounds.lower_right().y() > cx.window_size.y() {
bounds.set_origin_y(bounds.origin_y() - bounds.height());
}
}
self.child.paint(bounds.origin(), bounds, cx);
cx.scene.pop_stacking_context(); cx.scene.pop_stacking_context();
} }

View file

@ -1,6 +1,6 @@
use super::{ use super::{
ContainerStyle, Element, ElementBox, Flex, KeystrokeLabel, MouseEventHandler, ParentElement, ContainerStyle, Element, ElementBox, Flex, KeystrokeLabel, MouseEventHandler, Overlay,
Text, ParentElement, Text,
}; };
use crate::{ use crate::{
fonts::TextStyle, fonts::TextStyle,
@ -21,7 +21,7 @@ const DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(500);
pub struct Tooltip { pub struct Tooltip {
child: ElementBox, child: ElementBox,
tooltip: Option<ElementBox>, tooltip: Option<ElementBox>,
state: ElementStateHandle<Rc<TooltipState>>, _state: ElementStateHandle<Rc<TooltipState>>,
} }
#[derive(Default)] #[derive(Default)]
@ -68,15 +68,20 @@ impl Tooltip {
) )
.boxed(); .boxed();
Some( Some(
Self::render_tooltip(text, style, action, false) Overlay::new(
.constrained() Self::render_tooltip(text, style, action, false)
.dynamically(move |constraint, cx| { .constrained()
SizeConstraint::strict_along( .dynamically(move |constraint, cx| {
Axis::Vertical, SizeConstraint::strict_along(
collapsed_tooltip.layout(constraint, cx).y(), Axis::Vertical,
) collapsed_tooltip.layout(constraint, cx).y(),
}) )
.boxed(), })
.boxed(),
)
.align_to_fit(true)
.with_abs_position(state.position.get())
.boxed(),
) )
} else { } else {
None None
@ -111,7 +116,7 @@ impl Tooltip {
Self { Self {
child, child,
tooltip, tooltip,
state: state_handle, _state: state_handle,
} }
} }
@ -171,22 +176,7 @@ impl Element for Tooltip {
) { ) {
self.child.paint(bounds.origin(), visible_bounds, cx); self.child.paint(bounds.origin(), visible_bounds, cx);
if let Some(tooltip) = self.tooltip.as_mut() { if let Some(tooltip) = self.tooltip.as_mut() {
let origin = self.state.read(cx).position.get(); tooltip.paint(bounds.origin(), visible_bounds, cx);
let mut bounds = RectF::new(origin, tooltip.size());
// Align tooltip to the left if its bounds overflow the window width.
if bounds.lower_right().x() > cx.window_size.x() {
bounds.set_origin_x(bounds.origin_x() - bounds.width());
}
// Align tooltip to the top if its bounds overflow the window height.
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(bounds.origin(), bounds, cx);
cx.scene.pop_stacking_context();
} }
} }