Merge pull request #1090 from zed-industries/context-menu-polish

Context menu polish
This commit is contained in:
Antonio Scandurra 2022-06-02 10:06:44 +02:00 committed by GitHub
commit c34b30e739
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 39 deletions

View file

@ -50,6 +50,7 @@ impl ContextMenuItem {
} }
pub struct ContextMenu { pub struct ContextMenu {
show_count: usize,
position: Vector2F, position: Vector2F,
items: Vec<ContextMenuItem>, items: Vec<ContextMenuItem>,
selected_index: Option<usize>, selected_index: Option<usize>,
@ -92,6 +93,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()
} }
@ -104,6 +107,7 @@ impl View for ContextMenu {
impl ContextMenu { impl ContextMenu {
pub fn new(cx: &mut ViewContext<Self>) -> Self { pub fn new(cx: &mut ViewContext<Self>) -> Self {
Self { Self {
show_count: 0,
position: Default::default(), position: Default::default(),
items: Default::default(), items: Default::default(),
selected_index: Default::default(), selected_index: Default::default(),
@ -141,8 +145,9 @@ impl ContextMenu {
fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) { fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
self.reset(cx); self.reset(cx);
cx.defer(|this, cx| { let show_count = self.show_count;
if cx.handle().is_focused(cx) { cx.defer(move |this, cx| {
if cx.handle().is_focused(cx) && this.show_count == show_count {
let window_id = cx.window_id(); let window_id = cx.window_id();
(**cx).focus(window_id, this.previously_focused_view_id.take()); (**cx).focus(window_id, this.previously_focused_view_id.take());
} }
@ -210,6 +215,7 @@ impl ContextMenu {
self.items = items.collect(); self.items = items.collect();
self.position = position; self.position = position;
self.visible = true; self.visible = true;
self.show_count += 1;
if !cx.is_self_focused() { if !cx.is_self_focused() {
self.previously_focused_view_id = cx.focused_view_id(cx.window_id()); self.previously_focused_view_id = cx.focused_view_id(cx.window_id());
} }

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();
} }
} }