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

@ -222,6 +222,7 @@ impl Presenter {
let mut invalidated_views = Vec::new();
let mut hovered_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 clicked_region = None;
let mut right_mouse_down_region = None;
@ -230,13 +231,18 @@ impl Presenter {
match event {
Event::LeftMouseDown { position, .. } => {
let mut hit = false;
for (region, _) in self.mouse_regions.iter().rev() {
if region.bounds.contains_point(position) {
invalidated_views.push(region.view_id);
mouse_down_region = Some((region.clone(), position));
self.clicked_region = Some(region.clone());
self.prev_drag_position = Some(position);
break;
if !hit {
hit = true;
invalidated_views.push(region.view_id);
mouse_down_region = Some((region.clone(), position));
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, .. } => {
let mut hit = false;
for (region, _) in self.mouse_regions.iter().rev() {
if region.bounds.contains_point(position) {
invalidated_views.push(region.view_id);
right_mouse_down_region = Some((region.clone(), position));
self.right_clicked_region = Some(region.clone());
break;
if !hit {
hit = true;
invalidated_views.push(region.view_id);
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_callback) = mouse_down_region.mouse_down {
handled = true;