add mouse region handler bool for adding the handler above the child

This commit is contained in:
K Simmons
2022-10-24 23:30:35 -07:00
parent ba35536664
commit 70e2951e35
9 changed files with 221 additions and 187 deletions
+1 -1
View File
@@ -277,7 +277,7 @@ impl Element for Flex {
cx.notify();
} else {
cx.propogate_event();
cx.propagate_event();
}
}
})
+47 -23
View File
@@ -23,10 +23,14 @@ pub struct MouseEventHandler<Tag: 'static> {
hoverable: bool,
notify_on_hover: bool,
notify_on_click: bool,
above: bool,
padding: Padding,
_tag: PhantomData<Tag>,
}
// MouseEventHandler::new
// MouseEventHandler::above
impl<Tag> MouseEventHandler<Tag> {
pub fn new<V, F>(region_id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self
where
@@ -45,11 +49,22 @@ impl<Tag> MouseEventHandler<Tag> {
notify_on_hover,
notify_on_click,
hoverable: true,
above: false,
padding: Default::default(),
_tag: PhantomData,
}
}
pub fn above<V, F>(region_id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self
where
V: View,
F: FnOnce(&mut MouseState, &mut RenderContext<V>) -> ElementBox,
{
let mut handler = Self::new(region_id, cx, render_child);
handler.above = true;
handler
}
pub fn with_cursor_style(mut self, cursor: CursorStyle) -> Self {
self.cursor_style = Some(cursor);
self
@@ -149,6 +164,29 @@ impl<Tag> MouseEventHandler<Tag> {
)
.round_out()
}
fn paint_regions(&self, bounds: RectF, visible_bounds: RectF, cx: &mut PaintContext) {
let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default();
let hit_bounds = self.hit_bounds(visible_bounds);
if let Some(style) = self.cursor_style {
cx.scene.push_cursor_region(CursorRegion {
bounds: hit_bounds,
style,
});
}
cx.scene.push_mouse_region(
MouseRegion::from_handlers::<Tag>(
cx.current_view_id(),
self.region_id,
hit_bounds,
self.handlers.clone(),
)
.with_hoverable(self.hoverable)
.with_notify_on_hover(self.notify_on_hover)
.with_notify_on_click(self.notify_on_click),
);
}
}
impl<Tag> Element for MouseEventHandler<Tag> {
@@ -170,30 +208,16 @@ impl<Tag> Element for MouseEventHandler<Tag> {
_: &mut Self::LayoutState,
cx: &mut PaintContext,
) -> Self::PaintState {
let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default();
let hit_bounds = self.hit_bounds(visible_bounds);
if self.above {
self.child.paint(bounds.origin(), visible_bounds, cx);
self.child.paint(bounds.origin(), visible_bounds, cx);
cx.paint_stacking_context(None, |cx| {
if let Some(style) = self.cursor_style {
cx.scene.push_cursor_region(CursorRegion {
bounds: hit_bounds,
style,
});
}
cx.scene.push_mouse_region(
MouseRegion::from_handlers::<Tag>(
cx.current_view_id(),
self.region_id,
hit_bounds,
self.handlers.clone(),
)
.with_hoverable(self.hoverable)
.with_notify_on_hover(self.notify_on_hover)
.with_notify_on_click(self.notify_on_click),
);
});
cx.paint_layer(None, |cx| {
self.paint_regions(bounds, visible_bounds, cx);
});
} else {
self.paint_regions(bounds, visible_bounds, cx);
self.child.paint(bounds.origin(), visible_bounds, cx);
}
}
fn rect_for_text_range(
+17 -18
View File
@@ -204,25 +204,24 @@ impl Element for Overlay {
OverlayFitMode::None => {}
}
cx.scene.push_stacking_context(None);
cx.paint_stacking_context(None, |cx| {
if self.hoverable {
enum OverlayHoverCapture {}
// Block hovers in lower stacking contexts
cx.scene
.push_mouse_region(MouseRegion::new::<OverlayHoverCapture>(
cx.current_view_id(),
cx.current_view_id(),
bounds,
));
}
if self.hoverable {
enum OverlayHoverCapture {}
// Block hovers in lower stacking contexts
cx.scene
.push_mouse_region(MouseRegion::new::<OverlayHoverCapture>(
cx.current_view_id(),
cx.current_view_id(),
bounds,
));
}
self.child.paint(
bounds.origin(),
RectF::new(Vector2F::zero(), cx.window_size),
cx,
);
cx.scene.pop_stacking_context();
self.child.paint(
bounds.origin(),
RectF::new(Vector2F::zero(), cx.window_size),
cx,
);
});
}
fn rect_for_text_range(
+1 -1
View File
@@ -304,7 +304,7 @@ impl Element for UniformList {
},
cx| {
if !Self::scroll(state.clone(), position, delta, precise, scroll_max, cx) {
cx.propogate_event();
cx.propagate_event();
}
}
}),