Add mouse down out handlers

These will fire whenever the left/right mouse button is pressed down outside a specific region. I'll use these to cancel the context menu in the next commit.
This commit is contained in:
Nathan Sobo 2022-05-27 12:56:44 -06:00
parent 9909fc529b
commit 44c8ee5709
3 changed files with 52 additions and 29 deletions

View file

@ -18,11 +18,13 @@ pub struct MouseEventHandler {
tag: TypeId, tag: TypeId,
id: usize, id: usize,
cursor_style: Option<CursorStyle>, cursor_style: Option<CursorStyle>,
mouse_down_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
click_handler: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>, click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
right_mouse_down_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, right_mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
right_click_handler: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>, right_click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
drag_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
right_mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
drag: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
padding: Padding, padding: Padding,
} }
@ -38,11 +40,13 @@ impl MouseEventHandler {
tag: TypeId::of::<Tag>(), tag: TypeId::of::<Tag>(),
child: render_child(cx.mouse_state::<Tag>(id), cx), child: render_child(cx.mouse_state::<Tag>(id), cx),
cursor_style: None, cursor_style: None,
mouse_down_handler: None, mouse_down: None,
click_handler: None, click: None,
right_mouse_down_handler: None, right_mouse_down: None,
right_click_handler: None, right_click: None,
drag_handler: None, mouse_down_out: None,
right_mouse_down_out: None,
drag: None,
padding: Default::default(), padding: Default::default(),
} }
} }
@ -56,7 +60,7 @@ impl MouseEventHandler {
mut self, mut self,
handler: impl Fn(Vector2F, &mut EventContext) + 'static, handler: impl Fn(Vector2F, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.mouse_down_handler = Some(Rc::new(handler)); self.mouse_down = Some(Rc::new(handler));
self self
} }
@ -64,7 +68,7 @@ impl MouseEventHandler {
mut self, mut self,
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static, handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.click_handler = Some(Rc::new(handler)); self.click = Some(Rc::new(handler));
self self
} }
@ -72,7 +76,7 @@ impl MouseEventHandler {
mut self, mut self,
handler: impl Fn(Vector2F, &mut EventContext) + 'static, handler: impl Fn(Vector2F, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.right_mouse_down_handler = Some(Rc::new(handler)); self.right_mouse_down = Some(Rc::new(handler));
self self
} }
@ -80,12 +84,12 @@ impl MouseEventHandler {
mut self, mut self,
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static, handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.right_click_handler = Some(Rc::new(handler)); self.right_click = Some(Rc::new(handler));
self 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_handler = Some(Rc::new(handler)); self.drag = Some(Rc::new(handler));
self self
} }
@ -134,11 +138,13 @@ impl Element for MouseEventHandler {
discriminant: Some((self.tag, self.id)), discriminant: Some((self.tag, self.id)),
bounds: self.hit_bounds(bounds), bounds: self.hit_bounds(bounds),
hover: None, hover: None,
click: self.click_handler.clone(), click: self.click.clone(),
mouse_down: self.mouse_down_handler.clone(), mouse_down: self.mouse_down.clone(),
right_click: self.right_click_handler.clone(), right_click: self.right_click.clone(),
right_mouse_down: self.right_mouse_down_handler.clone(), right_mouse_down: self.right_mouse_down.clone(),
drag: self.drag_handler.clone(), mouse_down_out: self.mouse_down_out.clone(),
right_mouse_down_out: self.right_mouse_down_out.clone(),
drag: self.drag.clone(),
}); });
self.child.paint(bounds.origin(), visible_bounds, cx); self.child.paint(bounds.origin(), visible_bounds, cx);

View file

@ -222,6 +222,7 @@ impl Presenter {
let mut invalidated_views = Vec::new(); let mut invalidated_views = Vec::new();
let mut hovered_regions = Vec::new(); let mut hovered_regions = Vec::new();
let mut unhovered_regions = Vec::new(); let mut unhovered_regions = Vec::new();
let mut mouse_down_out_handlers = Vec::new();
let mut mouse_down_region = None; let mut mouse_down_region = None;
let mut clicked_region = None; let mut clicked_region = None;
let mut right_mouse_down_region = None; let mut right_mouse_down_region = None;
@ -230,13 +231,18 @@ impl Presenter {
match event { match event {
Event::LeftMouseDown { position, .. } => { Event::LeftMouseDown { position, .. } => {
let mut hit = false;
for (region, _) in self.mouse_regions.iter().rev() { for (region, _) in self.mouse_regions.iter().rev() {
if region.bounds.contains_point(position) { if region.bounds.contains_point(position) {
invalidated_views.push(region.view_id); if !hit {
mouse_down_region = Some((region.clone(), position)); hit = true;
self.clicked_region = Some(region.clone()); invalidated_views.push(region.view_id);
self.prev_drag_position = Some(position); mouse_down_region = Some((region.clone(), position));
break; self.clicked_region = Some(region.clone());
self.prev_drag_position = Some(position);
}
} else if let Some(handler) = region.mouse_down_out.clone() {
mouse_down_out_handlers.push((handler, region.view_id, position));
} }
} }
} }
@ -254,12 +260,17 @@ impl Presenter {
} }
} }
Event::RightMouseDown { position, .. } => { Event::RightMouseDown { position, .. } => {
let mut hit = false;
for (region, _) in self.mouse_regions.iter().rev() { for (region, _) in self.mouse_regions.iter().rev() {
if region.bounds.contains_point(position) { if region.bounds.contains_point(position) {
invalidated_views.push(region.view_id); if !hit {
right_mouse_down_region = Some((region.clone(), position)); hit = true;
self.right_clicked_region = Some(region.clone()); invalidated_views.push(region.view_id);
break; right_mouse_down_region = Some((region.clone(), position));
self.right_clicked_region = Some(region.clone());
}
} else if let Some(handler) = region.right_mouse_down_out.clone() {
mouse_down_out_handlers.push((handler, region.view_id, position));
} }
} }
} }
@ -355,6 +366,10 @@ impl Presenter {
} }
} }
for (handler, view_id, position) in mouse_down_out_handlers {
event_cx.with_current_view(view_id, |event_cx| handler(position, event_cx))
}
if let Some((mouse_down_region, position)) = mouse_down_region { if let Some((mouse_down_region, position)) = mouse_down_region {
if let Some(mouse_down_callback) = mouse_down_region.mouse_down { if let Some(mouse_down_callback) = mouse_down_region.mouse_down {
handled = true; handled = true;

View file

@ -54,6 +54,8 @@ pub struct MouseRegion {
pub right_mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, pub right_mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
pub right_click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>, pub right_click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
pub drag: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, pub drag: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
pub mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
pub right_mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
} }
#[derive(Copy, Clone, Eq, PartialEq, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Hash)]