WIP: Add click out event to fix context menus
This commit is contained in:
parent
6c53653831
commit
18dd3102bf
4 changed files with 75 additions and 13 deletions
|
@ -301,18 +301,23 @@ impl ContextMenu {
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) {
|
) {
|
||||||
let mut items = items.into_iter().peekable();
|
let mut items = items.into_iter().peekable();
|
||||||
if items.peek().is_some() {
|
dbg!(self.visible);
|
||||||
self.items = items.collect();
|
if (self.visible) {
|
||||||
self.anchor_position = anchor_position;
|
|
||||||
self.anchor_corner = anchor_corner;
|
|
||||||
self.visible = true;
|
|
||||||
self.show_count += 1;
|
|
||||||
if !cx.is_self_focused() {
|
|
||||||
self.previously_focused_view_id = cx.focused_view_id();
|
|
||||||
}
|
|
||||||
cx.focus_self();
|
|
||||||
} else {
|
|
||||||
self.visible = false;
|
self.visible = false;
|
||||||
|
} else {
|
||||||
|
if items.peek().is_some() {
|
||||||
|
self.items = items.collect();
|
||||||
|
self.anchor_position = anchor_position;
|
||||||
|
self.anchor_corner = anchor_corner;
|
||||||
|
self.visible = true;
|
||||||
|
self.show_count += 1;
|
||||||
|
if !cx.is_self_focused() {
|
||||||
|
self.previously_focused_view_id = cx.focused_view_id();
|
||||||
|
}
|
||||||
|
cx.focus_self();
|
||||||
|
} else {
|
||||||
|
self.visible = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
},
|
},
|
||||||
scene::{
|
scene::{
|
||||||
CursorRegion, MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover,
|
CursorRegion, MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover,
|
||||||
MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, Scene,
|
MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, Scene, MouseClickOut,
|
||||||
},
|
},
|
||||||
text_layout::TextLayoutCache,
|
text_layout::TextLayoutCache,
|
||||||
util::post_inc,
|
util::post_inc,
|
||||||
|
@ -524,6 +524,10 @@ impl<'a> WindowContext<'a> {
|
||||||
region: Default::default(),
|
region: Default::default(),
|
||||||
platform_event: e.clone(),
|
platform_event: e.clone(),
|
||||||
}));
|
}));
|
||||||
|
mouse_events.push(MouseEvent::ClickOut(MouseClickOut {
|
||||||
|
region: Default::default(),
|
||||||
|
platform_event: e.clone(),
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
Event::MouseMoved(
|
Event::MouseMoved(
|
||||||
|
|
|
@ -99,6 +99,20 @@ impl Deref for MouseClick {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct MouseClickOut {
|
||||||
|
pub region: RectF,
|
||||||
|
pub platform_event: MouseButtonEvent,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for MouseClickOut {
|
||||||
|
type Target = MouseButtonEvent;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.platform_event
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct MouseDownOut {
|
pub struct MouseDownOut {
|
||||||
pub region: RectF,
|
pub region: RectF,
|
||||||
|
@ -150,6 +164,7 @@ pub enum MouseEvent {
|
||||||
Down(MouseDown),
|
Down(MouseDown),
|
||||||
Up(MouseUp),
|
Up(MouseUp),
|
||||||
Click(MouseClick),
|
Click(MouseClick),
|
||||||
|
ClickOut(MouseClickOut),
|
||||||
DownOut(MouseDownOut),
|
DownOut(MouseDownOut),
|
||||||
UpOut(MouseUpOut),
|
UpOut(MouseUpOut),
|
||||||
ScrollWheel(MouseScrollWheel),
|
ScrollWheel(MouseScrollWheel),
|
||||||
|
@ -165,6 +180,7 @@ impl MouseEvent {
|
||||||
MouseEvent::Down(r) => r.region = region,
|
MouseEvent::Down(r) => r.region = region,
|
||||||
MouseEvent::Up(r) => r.region = region,
|
MouseEvent::Up(r) => r.region = region,
|
||||||
MouseEvent::Click(r) => r.region = region,
|
MouseEvent::Click(r) => r.region = region,
|
||||||
|
MouseEvent::ClickOut(r) => r.region = region,
|
||||||
MouseEvent::DownOut(r) => r.region = region,
|
MouseEvent::DownOut(r) => r.region = region,
|
||||||
MouseEvent::UpOut(r) => r.region = region,
|
MouseEvent::UpOut(r) => r.region = region,
|
||||||
MouseEvent::ScrollWheel(r) => r.region = region,
|
MouseEvent::ScrollWheel(r) => r.region = region,
|
||||||
|
@ -182,6 +198,7 @@ impl MouseEvent {
|
||||||
MouseEvent::Down(_) => true,
|
MouseEvent::Down(_) => true,
|
||||||
MouseEvent::Up(_) => true,
|
MouseEvent::Up(_) => true,
|
||||||
MouseEvent::Click(_) => true,
|
MouseEvent::Click(_) => true,
|
||||||
|
MouseEvent::ClickOut(_) => true,
|
||||||
MouseEvent::DownOut(_) => false,
|
MouseEvent::DownOut(_) => false,
|
||||||
MouseEvent::UpOut(_) => false,
|
MouseEvent::UpOut(_) => false,
|
||||||
MouseEvent::ScrollWheel(_) => true,
|
MouseEvent::ScrollWheel(_) => true,
|
||||||
|
@ -222,6 +239,10 @@ impl MouseEvent {
|
||||||
discriminant(&MouseEvent::Click(Default::default()))
|
discriminant(&MouseEvent::Click(Default::default()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn click_out_disc() -> Discriminant<MouseEvent> {
|
||||||
|
discriminant(&MouseEvent::ClickOut(Default::default()))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn down_out_disc() -> Discriminant<MouseEvent> {
|
pub fn down_out_disc() -> Discriminant<MouseEvent> {
|
||||||
discriminant(&MouseEvent::DownOut(Default::default()))
|
discriminant(&MouseEvent::DownOut(Default::default()))
|
||||||
}
|
}
|
||||||
|
@ -239,6 +260,7 @@ impl MouseEvent {
|
||||||
MouseEvent::Down(e) => HandlerKey::new(Self::down_disc(), Some(e.button)),
|
MouseEvent::Down(e) => HandlerKey::new(Self::down_disc(), Some(e.button)),
|
||||||
MouseEvent::Up(e) => HandlerKey::new(Self::up_disc(), Some(e.button)),
|
MouseEvent::Up(e) => HandlerKey::new(Self::up_disc(), Some(e.button)),
|
||||||
MouseEvent::Click(e) => HandlerKey::new(Self::click_disc(), Some(e.button)),
|
MouseEvent::Click(e) => HandlerKey::new(Self::click_disc(), Some(e.button)),
|
||||||
|
MouseEvent::ClickOut(e) => HandlerKey::new(Self::click_out_disc(), Some(e.button)),
|
||||||
MouseEvent::UpOut(e) => HandlerKey::new(Self::up_out_disc(), Some(e.button)),
|
MouseEvent::UpOut(e) => HandlerKey::new(Self::up_out_disc(), Some(e.button)),
|
||||||
MouseEvent::DownOut(e) => HandlerKey::new(Self::down_out_disc(), Some(e.button)),
|
MouseEvent::DownOut(e) => HandlerKey::new(Self::down_out_disc(), Some(e.button)),
|
||||||
MouseEvent::ScrollWheel(_) => HandlerKey::new(Self::scroll_wheel_disc(), None),
|
MouseEvent::ScrollWheel(_) => HandlerKey::new(Self::scroll_wheel_disc(), None),
|
||||||
|
|
|
@ -14,7 +14,7 @@ use super::{
|
||||||
MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover, MouseMove, MouseUp,
|
MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover, MouseMove, MouseUp,
|
||||||
MouseUpOut,
|
MouseUpOut,
|
||||||
},
|
},
|
||||||
MouseMoveOut, MouseScrollWheel,
|
MouseMoveOut, MouseScrollWheel, MouseClickOut,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -89,6 +89,15 @@ impl MouseRegion {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn on_click_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
|
||||||
|
where
|
||||||
|
V: View,
|
||||||
|
F: Fn(MouseClickOut, &mut V, &mut EventContext<V>) + 'static,
|
||||||
|
{
|
||||||
|
self.handlers = self.handlers.on_click(button, handler);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn on_down_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
|
pub fn on_down_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
|
||||||
where
|
where
|
||||||
V: View,
|
V: View,
|
||||||
|
@ -405,6 +414,28 @@ impl HandlerSet {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn on_click_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
|
||||||
|
where
|
||||||
|
V: View,
|
||||||
|
F: Fn(MouseClickOut, &mut V, &mut EventContext<V>) + 'static,
|
||||||
|
{
|
||||||
|
self.insert(MouseEvent::click_out_disc(), Some(button),
|
||||||
|
Rc::new(move |region_event, view, cx, view_id| {
|
||||||
|
if let MouseEvent::ClickOut(e) = region_event {
|
||||||
|
let view = view.downcast_mut().unwrap();
|
||||||
|
let mut cx = ViewContext::mutable(cx, view_id);
|
||||||
|
let mut cx = EventContext::new(&mut cx);
|
||||||
|
handler(e, view, &mut cx);
|
||||||
|
cx.handled
|
||||||
|
} else {
|
||||||
|
panic!(
|
||||||
|
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::ClickOut, found {:?}",
|
||||||
|
region_event);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn on_down_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
|
pub fn on_down_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
|
||||||
where
|
where
|
||||||
V: View,
|
V: View,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue