more event wip

This commit is contained in:
K Simmons 2022-07-22 14:00:38 -07:00
parent 53add253b6
commit a650c146f1
4 changed files with 175 additions and 97 deletions

View File

@ -51,6 +51,15 @@ impl MouseEventHandler {
self self
} }
pub fn on_up(
mut self,
button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers = self.handlers.on_up(button, handler);
self
}
pub fn on_click( pub fn on_click(
mut self, mut self,
button: MouseButton, button: MouseButton,
@ -69,6 +78,15 @@ impl MouseEventHandler {
self self
} }
pub fn on_up_out(
mut self,
button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers = self.handlers.on_up(button, handler);
self
}
pub fn on_drag( pub fn on_drag(
mut self, mut self,
button: MouseButton, button: MouseButton,
@ -78,6 +96,15 @@ impl MouseEventHandler {
self self
} }
pub fn on_drag_over(
mut self,
button: MouseButton,
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers = self.handlers.on_drag_over(button, handler);
self
}
pub fn on_hover( pub fn on_hover(
mut self, mut self,
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static, handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,

View File

@ -234,10 +234,7 @@ impl Presenter {
pub fn dispatch_event(&mut self, event: Event, cx: &mut MutableAppContext) -> bool { pub fn dispatch_event(&mut self, event: Event, cx: &mut MutableAppContext) -> bool {
if let Some(root_view_id) = cx.root_view_id(self.window_id) { if let Some(root_view_id) = cx.root_view_id(self.window_id) {
let mut invalidated_views = Vec::new(); let mut invalidated_views = Vec::new();
let mut mouse_down_out_handlers = Vec::new(); let mut events_to_send = Vec::new();
let mut mouse_down_region = None;
let mut clicked_region = None;
let mut dragged_region = None;
match &event { match &event {
Event::MouseDown( Event::MouseDown(
@ -248,32 +245,42 @@ impl Presenter {
let mut hit = false; 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) {
if !hit { if !std::mem::replace(&mut hit, true) {
hit = true;
invalidated_views.push(region.view_id); invalidated_views.push(region.view_id);
mouse_down_region = events_to_send
Some((region.clone(), MouseRegionEvent::Down(e.clone()))); .push((region.clone(), MouseRegionEvent::Down(e.clone())));
self.clicked_region = Some(region.clone()); self.clicked_region = Some(region.clone());
self.prev_drag_position = Some(*position); self.prev_drag_position = Some(*position);
} }
} else if let Some(handler) = region } else {
.handlers events_to_send
.get(&(MouseRegionEvent::down_out_disc(), Some(*button))) .push((region.clone(), MouseRegionEvent::DownOut(e.clone())));
{
mouse_down_out_handlers.push((
handler,
region.view_id,
MouseRegionEvent::DownOut(e.clone()),
));
} }
} }
} }
Event::MouseUp(e @ MouseButtonEvent { position, .. }) => { Event::MouseUp(
e @ MouseButtonEvent {
position, button, ..
},
) => {
let mut hit = false;
for (region, _) in self.mouse_regions.iter().rev() {
if region.bounds.contains_point(*position) {
if !std::mem::replace(&mut hit, true) {
invalidated_views.push(region.view_id);
events_to_send
.push((region.clone(), MouseRegionEvent::Up(e.clone())));
}
} else {
events_to_send
.push((region.clone(), MouseRegionEvent::UpOut(e.clone())));
}
}
self.prev_drag_position.take(); self.prev_drag_position.take();
if let Some(region) = self.clicked_region.take() { if let Some(region) = self.clicked_region.take() {
invalidated_views.push(region.view_id); invalidated_views.push(region.view_id);
if region.bounds.contains_point(*position) { if region.bounds.contains_point(*position) {
clicked_region = Some((region, MouseRegionEvent::Click(e.clone()))); events_to_send.push((region, MouseRegionEvent::Click(e.clone())));
} }
} }
} }
@ -283,7 +290,7 @@ impl Presenter {
.as_ref() .as_ref()
.zip(self.prev_drag_position.as_mut()) .zip(self.prev_drag_position.as_mut())
{ {
dragged_region = Some(( events_to_send.push((
clicked_region.clone(), clicked_region.clone(),
MouseRegionEvent::Drag(*prev_drag_position, *e), MouseRegionEvent::Drag(*prev_drag_position, *e),
)); ));
@ -298,39 +305,11 @@ impl Presenter {
let (mut handled, mut event_cx) = let (mut handled, mut event_cx) =
self.handle_hover_events(&event, &mut invalidated_views, cx); self.handle_hover_events(&event, &mut invalidated_views, cx);
for (handler, view_id, region_event) in mouse_down_out_handlers { for (region, event) in events_to_send {
event_cx.with_current_view(view_id, |event_cx| handler(region_event, event_cx))
}
if let Some((mouse_down_region, region_event)) = mouse_down_region {
handled = true; handled = true;
if let Some(mouse_down_callback) = if let Some(callback) = region.handlers.get(&event.handler_key()) {
mouse_down_region.handlers.get(&region_event.handler_key()) event_cx.with_current_view(region.view_id, |event_cx| {
{ callback(event, event_cx);
event_cx.with_current_view(mouse_down_region.view_id, |event_cx| {
mouse_down_callback(region_event, event_cx);
})
}
}
if let Some((clicked_region, region_event)) = clicked_region {
handled = true;
if let Some(click_callback) =
clicked_region.handlers.get(&region_event.handler_key())
{
event_cx.with_current_view(clicked_region.view_id, |event_cx| {
click_callback(region_event, event_cx);
})
}
}
if let Some((dragged_region, region_event)) = dragged_region {
handled = true;
if let Some(drag_callback) =
dragged_region.handlers.get(&region_event.handler_key())
{
event_cx.with_current_view(dragged_region.view_id, |event_cx| {
drag_callback(region_event, event_cx);
}) })
} }
} }
@ -366,38 +345,44 @@ impl Presenter {
}, },
) = event ) = event
{ {
if pressed_button.is_none() { let mut style_to_assign = CursorStyle::Arrow;
let mut style_to_assign = CursorStyle::Arrow; for region in self.cursor_regions.iter().rev() {
for region in self.cursor_regions.iter().rev() { if region.bounds.contains_point(*position) {
if region.bounds.contains_point(*position) { style_to_assign = region.style;
style_to_assign = region.style; break;
break;
}
} }
cx.platform().set_cursor_style(style_to_assign); }
cx.platform().set_cursor_style(style_to_assign);
let mut hover_depth = None; let mut hover_depth = None;
for (region, depth) in self.mouse_regions.iter().rev() { for (region, depth) in self.mouse_regions.iter().rev() {
if region.bounds.contains_point(*position) if region.bounds.contains_point(*position)
&& hover_depth.map_or(true, |hover_depth| hover_depth == *depth) && hover_depth.map_or(true, |hover_depth| hover_depth == *depth)
{ {
hover_depth = Some(*depth); hover_depth = Some(*depth);
if let Some(region_id) = region.id() { if let Some(region_id) = region.id() {
if !self.hovered_region_ids.contains(&region_id) { if !self.hovered_region_ids.contains(&region_id) {
invalidated_views.push(region.view_id);
hover_regions
.push((region.clone(), MouseRegionEvent::Hover(true, *e)));
self.hovered_region_ids.insert(region_id);
}
}
} else if let Some(region_id) = region.id() {
if self.hovered_region_ids.contains(&region_id) {
invalidated_views.push(region.view_id); invalidated_views.push(region.view_id);
hover_regions let region_event = if let Some(pressed_button) = pressed_button {
.push((region.clone(), MouseRegionEvent::Hover(false, *e))); MouseRegionEvent::DragOver(true, e.clone())
self.hovered_region_ids.remove(&region_id); } else {
MouseRegionEvent::Hover(true, e.clone())
};
hover_regions.push((region.clone(), region_event));
self.hovered_region_ids.insert(region_id);
} }
} }
} else if let Some(region_id) = region.id() {
if self.hovered_region_ids.contains(&region_id) {
invalidated_views.push(region.view_id);
let region_event = if let Some(pressed_button) = pressed_button {
MouseRegionEvent::DragOver(false, e.clone())
} else {
MouseRegionEvent::Hover(false, e.clone())
};
hover_regions.push((region.clone(), region_event));
self.hovered_region_ids.remove(&region_id);
}
} }
} }
} }
@ -407,9 +392,9 @@ impl Presenter {
for (hover_region, region_event) in hover_regions { for (hover_region, region_event) in hover_regions {
handled = true; handled = true;
if let Some(hover_callback) = hover_region.handlers.get(&region_event.handler_key()) { if let Some(callback) = hover_region.handlers.get(&region_event.handler_key()) {
event_cx.with_current_view(hover_region.view_id, |event_cx| { event_cx.with_current_view(hover_region.view_id, |event_cx| {
hover_callback(region_event, event_cx); callback(region_event, event_cx);
}) })
} }
} }

View File

@ -1,4 +1,8 @@
use std::{any::TypeId, mem::Discriminant, rc::Rc}; use std::{
any::TypeId,
mem::{discriminant, Discriminant},
rc::Rc,
};
use collections::HashMap; use collections::HashMap;
use pathfinder_geometry::{rect::RectF, vector::Vector2F}; use pathfinder_geometry::{rect::RectF, vector::Vector2F};
@ -81,6 +85,15 @@ impl MouseRegion {
self self
} }
pub fn on_up_out(
mut self,
button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers = self.handlers.on_up_out(button, handler);
self
}
pub fn on_drag( pub fn on_drag(
mut self, mut self,
button: MouseButton, button: MouseButton,
@ -90,6 +103,15 @@ impl MouseRegion {
self self
} }
pub fn on_drag_over(
mut self,
button: MouseButton,
handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers = self.handlers.on_drag_over(button, handler);
self
}
pub fn on_hover( pub fn on_hover(
mut self, mut self,
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static, handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
@ -129,6 +151,10 @@ impl HandlerSet {
(MouseRegionEvent::drag_disc(), Some(button)), (MouseRegionEvent::drag_disc(), Some(button)),
Rc::new(|_, _| {}), Rc::new(|_, _| {}),
); );
set.insert(
(MouseRegionEvent::drag_over_disc(), Some(button)),
Rc::new(|_, _| {}),
);
set.insert( set.insert(
(MouseRegionEvent::down_disc(), Some(button)), (MouseRegionEvent::down_disc(), Some(button)),
Rc::new(|_, _| {}), Rc::new(|_, _| {}),
@ -145,6 +171,10 @@ impl HandlerSet {
(MouseRegionEvent::down_out_disc(), Some(button)), (MouseRegionEvent::down_out_disc(), Some(button)),
Rc::new(|_, _| {}), Rc::new(|_, _| {}),
); );
set.insert(
(MouseRegionEvent::up_out_disc(), Some(button)),
Rc::new(|_, _| {}),
);
} }
set.insert( set.insert(
(MouseRegionEvent::scroll_wheel_disc(), None), (MouseRegionEvent::scroll_wheel_disc(), None),
@ -233,6 +263,24 @@ impl HandlerSet {
self self
} }
pub fn on_up_out(
mut self,
button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self {
self.set.insert((MouseRegionEvent::up_out_disc(), Some(button)),
Rc::new(move |region_event, cx| {
if let MouseRegionEvent::UpOut(mouse_button_event) = region_event {
handler(mouse_button_event, cx);
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::UpOut, found {:?}",
region_event);
}
}));
self
}
pub fn on_drag( pub fn on_drag(
mut self, mut self,
button: MouseButton, button: MouseButton,
@ -251,14 +299,32 @@ impl HandlerSet {
self self
} }
pub fn on_drag_over(
mut self,
button: MouseButton,
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
) -> Self {
self.set.insert((MouseRegionEvent::drag_over_disc(), Some(button)),
Rc::new(move |region_event, cx| {
if let MouseRegionEvent::DragOver(started, mouse_moved_event) = region_event {
handler(started, mouse_moved_event, cx);
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::DragOver, found {:?}",
region_event);
}
}));
self
}
pub fn on_hover( pub fn on_hover(
mut self, mut self,
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static, handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.set.insert((MouseRegionEvent::hover_disc(), None), self.set.insert((MouseRegionEvent::hover_disc(), None),
Rc::new(move |region_event, cx| { Rc::new(move |region_event, cx| {
if let MouseRegionEvent::Hover(hover, mouse_moved_event) = region_event { if let MouseRegionEvent::Hover(started, mouse_moved_event) = region_event {
handler(hover, mouse_moved_event, cx); handler(started, mouse_moved_event, cx);
} else { } else {
panic!( panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Hover, found {:?}", "Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Hover, found {:?}",
@ -273,7 +339,7 @@ impl HandlerSet {
pub enum MouseRegionEvent { pub enum MouseRegionEvent {
Move(MouseMovedEvent), Move(MouseMovedEvent),
Drag(Vector2F, MouseMovedEvent), Drag(Vector2F, MouseMovedEvent),
DragOver(Vector2F, MouseMovedEvent), DragOver(bool, MouseMovedEvent),
Hover(bool, MouseMovedEvent), Hover(bool, MouseMovedEvent),
Down(MouseButtonEvent), Down(MouseButtonEvent),
Up(MouseButtonEvent), Up(MouseButtonEvent),
@ -285,40 +351,40 @@ pub enum MouseRegionEvent {
impl MouseRegionEvent { impl MouseRegionEvent {
pub fn move_disc() -> Discriminant<MouseRegionEvent> { pub fn move_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Move(Default::default())) discriminant(&MouseRegionEvent::Move(Default::default()))
} }
pub fn drag_disc() -> Discriminant<MouseRegionEvent> { pub fn drag_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Drag( discriminant(&MouseRegionEvent::Drag(
Default::default(), Default::default(),
Default::default(), Default::default(),
)) ))
} }
pub fn drag_over_disc() -> Discriminant<MouseRegionEvent> { pub fn drag_over_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::DragOver( discriminant(&MouseRegionEvent::DragOver(
Default::default(), Default::default(),
Default::default(), Default::default(),
)) ))
} }
pub fn hover_disc() -> Discriminant<MouseRegionEvent> { pub fn hover_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Hover( discriminant(&MouseRegionEvent::Hover(
Default::default(), Default::default(),
Default::default(), Default::default(),
)) ))
} }
pub fn down_disc() -> Discriminant<MouseRegionEvent> { pub fn down_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Down(Default::default())) discriminant(&MouseRegionEvent::Down(Default::default()))
} }
pub fn up_disc() -> Discriminant<MouseRegionEvent> { pub fn up_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Up(Default::default())) discriminant(&MouseRegionEvent::Up(Default::default()))
} }
pub fn up_out_disc() -> Discriminant<MouseRegionEvent> { pub fn up_out_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::UpOut(Default::default())) discriminant(&MouseRegionEvent::UpOut(Default::default()))
} }
pub fn click_disc() -> Discriminant<MouseRegionEvent> { pub fn click_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Click(Default::default())) discriminant(&MouseRegionEvent::Click(Default::default()))
} }
pub fn down_out_disc() -> Discriminant<MouseRegionEvent> { pub fn down_out_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::DownOut(Default::default())) discriminant(&MouseRegionEvent::DownOut(Default::default()))
} }
pub fn scroll_wheel_disc() -> Discriminant<MouseRegionEvent> { pub fn scroll_wheel_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::ScrollWheel(Default::default())) std::mem::discriminant(&MouseRegionEvent::ScrollWheel(Default::default()))

View File

@ -1025,13 +1025,13 @@ impl Pane {
.on_drag(MouseButton::Left, |_, cx| { .on_drag(MouseButton::Left, |_, cx| {
cx.global::<DragAndDrop>().dragging(some view handle) cx.global::<DragAndDrop>().dragging(some view handle)
}) })
.on_mouse_up_out(MouseButton::Left, |_, cx| { .on_up_out(MouseButton::Left, |_, cx| {
cx.global::<DragAndDrop>().stopped_dragging(some view handle) cx.global::<DragAndDrop>().stopped_dragging(some view handle)
}) })
.on_drag_over(MouseButton::Left, |started, _, cx| { .on_drag_over(MouseButton::Left, |started, _, _, cx| {
if started { if started {
if let Some(tab) = cx.global::<DragAndDrop>().current_dragged::<Tab>() { if let Some(tab) = cx.global::<DragAndDrop>().current_dragged::<Tab>() {
cx.dispatch_action(ReceivingTab) cx.dispatch_action(ReceivingTab);
} }
} }
}) })