Dismiss context menu when (right-)mousing down outside of it
This commit is contained in:
parent
fb26f8195b
commit
e7ab61d125
2 changed files with 62 additions and 40 deletions
|
@ -237,14 +237,16 @@ impl ContextMenu {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_menu(&self, cx: &mut RenderContext<Self>) -> impl Element {
|
fn render_menu(&self, cx: &mut RenderContext<Self>) -> impl Element {
|
||||||
enum Tag {}
|
enum Menu {}
|
||||||
|
enum MenuItem {}
|
||||||
let style = cx.global::<Settings>().theme.context_menu.clone();
|
let style = cx.global::<Settings>().theme.context_menu.clone();
|
||||||
|
MouseEventHandler::new::<Menu, _, _>(0, cx, |_, cx| {
|
||||||
Flex::column()
|
Flex::column()
|
||||||
.with_children(self.items.iter().enumerate().map(|(ix, item)| {
|
.with_children(self.items.iter().enumerate().map(|(ix, item)| {
|
||||||
match item {
|
match item {
|
||||||
ContextMenuItem::Item { label, action } => {
|
ContextMenuItem::Item { label, action } => {
|
||||||
let action = action.boxed_clone();
|
let action = action.boxed_clone();
|
||||||
MouseEventHandler::new::<Tag, _, _>(ix, cx, |state, _| {
|
MouseEventHandler::new::<MenuItem, _, _>(ix, cx, |state, _| {
|
||||||
let style =
|
let style =
|
||||||
style.item.style_for(state, Some(ix) == self.selected_index);
|
style.item.style_for(state, Some(ix) == self.selected_index);
|
||||||
Flex::row()
|
Flex::row()
|
||||||
|
@ -281,5 +283,9 @@ impl ContextMenu {
|
||||||
}))
|
}))
|
||||||
.contained()
|
.contained()
|
||||||
.with_style(style.container)
|
.with_style(style.container)
|
||||||
|
.boxed()
|
||||||
|
})
|
||||||
|
.on_mouse_down_out(|_, cx| cx.dispatch_action(Cancel))
|
||||||
|
.on_right_mouse_down_out(|_, cx| cx.dispatch_action(Cancel))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,6 +88,22 @@ impl MouseEventHandler {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn on_mouse_down_out(
|
||||||
|
mut self,
|
||||||
|
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||||
|
) -> Self {
|
||||||
|
self.mouse_down_out = Some(Rc::new(handler));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_right_mouse_down_out(
|
||||||
|
mut self,
|
||||||
|
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||||
|
) -> Self {
|
||||||
|
self.right_mouse_down_out = Some(Rc::new(handler));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn on_drag(mut self, handler: impl Fn(Vector2F, &mut EventContext) + 'static) -> Self {
|
pub fn on_drag(mut self, handler: impl Fn(Vector2F, &mut EventContext) + 'static) -> Self {
|
||||||
self.drag = Some(Rc::new(handler));
|
self.drag = Some(Rc::new(handler));
|
||||||
self
|
self
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue