Toggle sidebar items on mouse down instead of on click

This commit is contained in:
Antonio Scandurra 2021-09-02 10:20:58 +02:00
parent 1d697df1bc
commit 38dc023942
2 changed files with 12 additions and 1 deletions

View file

@ -12,6 +12,7 @@ pub struct MouseEventHandler {
state: ElementStateHandle<MouseState>,
child: ElementBox,
cursor_style: Option<CursorStyle>,
mouse_down_handler: Option<Box<dyn FnMut(&mut EventContext)>>,
click_handler: Option<Box<dyn FnMut(&mut EventContext)>>,
drag_handler: Option<Box<dyn FnMut(Vector2F, &mut EventContext)>>,
}
@ -38,6 +39,7 @@ impl MouseEventHandler {
state: state_handle,
child,
cursor_style: None,
mouse_down_handler: None,
click_handler: None,
drag_handler: None,
}
@ -48,6 +50,11 @@ impl MouseEventHandler {
self
}
pub fn on_mouse_down(mut self, handler: impl FnMut(&mut EventContext) + 'static) -> Self {
self.mouse_down_handler = Some(Box::new(handler));
self
}
pub fn on_click(mut self, handler: impl FnMut(&mut EventContext) + 'static) -> Self {
self.click_handler = Some(Box::new(handler));
self
@ -89,6 +96,7 @@ impl Element for MouseEventHandler {
cx: &mut EventContext,
) -> bool {
let cursor_style = self.cursor_style;
let mouse_down_handler = self.mouse_down_handler.as_mut();
let click_handler = self.click_handler.as_mut();
let drag_handler = self.drag_handler.as_mut();
@ -124,6 +132,9 @@ impl Element for MouseEventHandler {
state.clicked = true;
state.prev_drag_position = Some(*position);
cx.notify();
if let Some(handler) = mouse_down_handler {
handler(cx);
}
true
} else {
handled_in_child

View file

@ -91,7 +91,7 @@ impl Sidebar {
.boxed()
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(move |cx| {
.on_mouse_down(move |cx| {
cx.dispatch_action(ToggleSidebarItem(ToggleArg { side, item_index }))
})
.boxed()