Change mouse_event_handler to use HandlerSet
This commit is contained in:
parent
ec25fa9260
commit
deeefed7eb
2 changed files with 26 additions and 12 deletions
|
@ -7,7 +7,7 @@ use crate::{
|
||||||
vector::{vec2f, Vector2F},
|
vector::{vec2f, Vector2F},
|
||||||
},
|
},
|
||||||
platform::CursorStyle,
|
platform::CursorStyle,
|
||||||
scene::CursorRegion,
|
scene::{CursorRegion, HandlerSet},
|
||||||
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseButton,
|
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseButton,
|
||||||
MouseButtonEvent, MouseMovedEvent, MouseRegion, MouseState, PaintContext, RenderContext,
|
MouseButtonEvent, MouseMovedEvent, MouseRegion, MouseState, PaintContext, RenderContext,
|
||||||
SizeConstraint, View,
|
SizeConstraint, View,
|
||||||
|
@ -16,8 +16,9 @@ use serde_json::json;
|
||||||
|
|
||||||
pub struct MouseEventHandler {
|
pub struct MouseEventHandler {
|
||||||
child: ElementBox,
|
child: ElementBox,
|
||||||
|
discriminant: (TypeId, usize),
|
||||||
cursor_style: Option<CursorStyle>,
|
cursor_style: Option<CursorStyle>,
|
||||||
region: MouseRegion,
|
handlers: HandlerSet,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +32,8 @@ impl MouseEventHandler {
|
||||||
Self {
|
Self {
|
||||||
child: render_child(cx.mouse_state::<Tag>(id), cx),
|
child: render_child(cx.mouse_state::<Tag>(id), cx),
|
||||||
cursor_style: None,
|
cursor_style: None,
|
||||||
region: MouseRegion::new(0, Some((TypeId::of::<Tag>(), id)), Default::default()),
|
discriminant: (TypeId::of::<Tag>(), id),
|
||||||
|
handlers: Default::default(),
|
||||||
padding: Default::default(),
|
padding: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +48,7 @@ impl MouseEventHandler {
|
||||||
button: MouseButton,
|
button: MouseButton,
|
||||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.region = self.region.on_down(button, handler);
|
self.handlers = self.handlers.on_down(button, handler);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +57,7 @@ impl MouseEventHandler {
|
||||||
button: MouseButton,
|
button: MouseButton,
|
||||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.region = self.region.on_click(button, handler);
|
self.handlers = self.handlers.on_click(button, handler);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +66,7 @@ impl MouseEventHandler {
|
||||||
button: MouseButton,
|
button: MouseButton,
|
||||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.region = self.region.on_down_out(button, handler);
|
self.handlers = self.handlers.on_down_out(button, handler);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +75,7 @@ impl MouseEventHandler {
|
||||||
button: MouseButton,
|
button: MouseButton,
|
||||||
handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
|
handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.region = self.region.on_drag(button, handler);
|
self.handlers = self.handlers.on_drag(button, handler);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +83,7 @@ impl MouseEventHandler {
|
||||||
mut self,
|
mut self,
|
||||||
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
|
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.region = self.region.on_hover(handler);
|
self.handlers = self.handlers.on_hover(handler);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,9 +128,12 @@ impl Element for MouseEventHandler {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.region.view_id = cx.current_view_id();
|
cx.scene.push_mouse_region(MouseRegion::from_handlers(
|
||||||
self.region.bounds = hit_bounds;
|
cx.current_view_id(),
|
||||||
cx.scene.push_mouse_region(self.region.clone());
|
Some(self.discriminant.clone()),
|
||||||
|
hit_bounds,
|
||||||
|
self.handlers.clone(),
|
||||||
|
));
|
||||||
|
|
||||||
self.child.paint(bounds.origin(), visible_bounds, cx);
|
self.child.paint(bounds.origin(), visible_bounds, cx);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,11 +15,20 @@ pub struct MouseRegion {
|
||||||
|
|
||||||
impl MouseRegion {
|
impl MouseRegion {
|
||||||
pub fn new(view_id: usize, discriminant: Option<(TypeId, usize)>, bounds: RectF) -> Self {
|
pub fn new(view_id: usize, discriminant: Option<(TypeId, usize)>, bounds: RectF) -> Self {
|
||||||
|
Self::from_handlers(view_id, discriminant, bounds, Default::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_handlers(
|
||||||
|
view_id: usize,
|
||||||
|
discriminant: Option<(TypeId, usize)>,
|
||||||
|
bounds: RectF,
|
||||||
|
handlers: HandlerSet,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
view_id,
|
view_id,
|
||||||
discriminant,
|
discriminant,
|
||||||
bounds,
|
bounds,
|
||||||
handlers: Default::default(),
|
handlers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue