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

This commit is contained in:
K Simmons 2022-10-21 15:53:43 -07:00
parent ba35536664
commit 70e2951e35
9 changed files with 221 additions and 187 deletions

View file

@ -125,7 +125,7 @@ impl<V: View> DragAndDrop<V> {
cx.defer(|cx| { cx.defer(|cx| {
cx.update_global::<Self, _, _>(|this, cx| this.stop_dragging(cx)); cx.update_global::<Self, _, _>(|this, cx| this.stop_dragging(cx));
}); });
cx.propogate_event(); cx.propagate_event();
}) })
.on_up_out(MouseButton::Left, |_, cx| { .on_up_out(MouseButton::Left, |_, cx| {
cx.defer(|cx| { cx.defer(|cx| {

View file

@ -137,7 +137,7 @@ impl EditorElement {
gutter_bounds, gutter_bounds,
cx, cx,
) { ) {
cx.propogate_event(); cx.propagate_event();
} }
} }
}) })
@ -150,7 +150,7 @@ impl EditorElement {
text_bounds, text_bounds,
cx, cx,
) { ) {
cx.propogate_event(); cx.propagate_event();
} }
} }
}) })
@ -167,7 +167,7 @@ impl EditorElement {
text_bounds, text_bounds,
cx, cx,
) { ) {
cx.propogate_event() cx.propagate_event()
} }
} }
}) })
@ -182,7 +182,7 @@ impl EditorElement {
text_bounds, text_bounds,
cx, cx,
) { ) {
cx.propogate_event() cx.propagate_event()
} }
} }
}) })
@ -190,7 +190,7 @@ impl EditorElement {
let position_map = position_map.clone(); let position_map = position_map.clone();
move |e, cx| { move |e, cx| {
if !Self::mouse_moved(e.platform_event, &position_map, text_bounds, cx) { if !Self::mouse_moved(e.platform_event, &position_map, text_bounds, cx) {
cx.propogate_event() cx.propagate_event()
} }
} }
}) })
@ -199,7 +199,7 @@ impl EditorElement {
move |e, cx| { move |e, cx| {
if !Self::scroll(e.position, e.delta, e.precise, &position_map, bounds, cx) if !Self::scroll(e.position, e.delta, e.precise, &position_map, bounds, cx)
{ {
cx.propogate_event() cx.propagate_event()
} }
} }
}), }),

View file

@ -277,7 +277,7 @@ impl Element for Flex {
cx.notify(); cx.notify();
} else { } else {
cx.propogate_event(); cx.propagate_event();
} }
} }
}) })

View file

@ -23,10 +23,14 @@ pub struct MouseEventHandler<Tag: 'static> {
hoverable: bool, hoverable: bool,
notify_on_hover: bool, notify_on_hover: bool,
notify_on_click: bool, notify_on_click: bool,
above: bool,
padding: Padding, padding: Padding,
_tag: PhantomData<Tag>, _tag: PhantomData<Tag>,
} }
// MouseEventHandler::new
// MouseEventHandler::above
impl<Tag> MouseEventHandler<Tag> { impl<Tag> MouseEventHandler<Tag> {
pub fn new<V, F>(region_id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self pub fn new<V, F>(region_id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self
where where
@ -45,11 +49,22 @@ impl<Tag> MouseEventHandler<Tag> {
notify_on_hover, notify_on_hover,
notify_on_click, notify_on_click,
hoverable: true, hoverable: true,
above: false,
padding: Default::default(), padding: Default::default(),
_tag: PhantomData, _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 { pub fn with_cursor_style(mut self, cursor: CursorStyle) -> Self {
self.cursor_style = Some(cursor); self.cursor_style = Some(cursor);
self self
@ -149,6 +164,29 @@ impl<Tag> MouseEventHandler<Tag> {
) )
.round_out() .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> { impl<Tag> Element for MouseEventHandler<Tag> {
@ -170,30 +208,16 @@ impl<Tag> Element for MouseEventHandler<Tag> {
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
cx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default(); if self.above {
let hit_bounds = self.hit_bounds(visible_bounds);
self.child.paint(bounds.origin(), visible_bounds, cx); self.child.paint(bounds.origin(), visible_bounds, cx);
cx.paint_stacking_context(None, |cx| { cx.paint_layer(None, |cx| {
if let Some(style) = self.cursor_style { self.paint_regions(bounds, visible_bounds, cx);
cx.scene.push_cursor_region(CursorRegion {
bounds: hit_bounds,
style,
}); });
} else {
self.paint_regions(bounds, visible_bounds, cx);
self.child.paint(bounds.origin(), visible_bounds, cx);
} }
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),
);
});
} }
fn rect_for_text_range( fn rect_for_text_range(

View file

@ -204,8 +204,7 @@ impl Element for Overlay {
OverlayFitMode::None => {} OverlayFitMode::None => {}
} }
cx.scene.push_stacking_context(None); cx.paint_stacking_context(None, |cx| {
if self.hoverable { if self.hoverable {
enum OverlayHoverCapture {} enum OverlayHoverCapture {}
// Block hovers in lower stacking contexts // Block hovers in lower stacking contexts
@ -222,7 +221,7 @@ impl Element for Overlay {
RectF::new(Vector2F::zero(), cx.window_size), RectF::new(Vector2F::zero(), cx.window_size),
cx, cx,
); );
cx.scene.pop_stacking_context(); });
} }
fn rect_for_text_range( fn rect_for_text_range(

View file

@ -304,7 +304,7 @@ impl Element for UniformList {
}, },
cx| { cx| {
if !Self::scroll(state.clone(), position, delta, precise, scroll_max, cx) { if !Self::scroll(state.clone(), position, delta, precise, scroll_max, cx) {
cx.propogate_event(); cx.propagate_event();
} }
} }
}), }),

View file

@ -485,9 +485,7 @@ impl Presenter {
event_cx.handled = true; event_cx.handled = true;
event_cx.with_current_view(valid_region.id().view_id(), { event_cx.with_current_view(valid_region.id().view_id(), {
let region_event = mouse_event.clone(); let region_event = mouse_event.clone();
|cx| { |cx| callback(region_event, cx)
callback(region_event, cx);
}
}); });
} }
@ -804,7 +802,7 @@ impl<'a> EventContext<'a> {
self.notify_count self.notify_count
} }
pub fn propogate_event(&mut self) { pub fn propagate_event(&mut self) {
self.handled = false; self.handled = false;
} }
} }

View file

@ -1,7 +1,7 @@
use collections::HashMap; use collections::HashMap;
use gpui::{ use gpui::{
actions, actions,
elements::{ChildView, Container, Empty, MouseEventHandler, Side, Svg}, elements::{ChildView, Container, Empty, MouseEventHandler, ParentElement, Side, Stack, Svg},
impl_internal_actions, Border, CursorStyle, Element, ElementBox, Entity, MouseButton, impl_internal_actions, Border, CursorStyle, Element, ElementBox, Entity, MouseButton,
MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle, MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle,
}; };
@ -308,14 +308,13 @@ impl Dock {
DockAnchor::Expanded => { DockAnchor::Expanded => {
enum ExpandedDockWash {} enum ExpandedDockWash {}
enum ExpandedDockPane {} enum ExpandedDockPane {}
Container::new( Stack::new()
MouseEventHandler::<ExpandedDockWash>::new(0, cx, |_state, cx| { .with_child(
MouseEventHandler::<ExpandedDockPane>::new(0, cx, |_state, cx| { // Render wash under the dock which when clicked hides it
ChildView::new(&self.pane, cx).boxed() MouseEventHandler::<ExpandedDockWash>::new(0, cx, |_, _| {
}) Empty::new()
.capture_all()
.contained() .contained()
.with_style(style.maximized) .with_background_color(style.wash_color)
.boxed() .boxed()
}) })
.capture_all() .capture_all()
@ -325,7 +324,17 @@ impl Dock {
.with_cursor_style(CursorStyle::Arrow) .with_cursor_style(CursorStyle::Arrow)
.boxed(), .boxed(),
) )
.with_background_color(style.wash_color) .with_child(
MouseEventHandler::<ExpandedDockPane>::new(0, cx, |_state, cx| {
ChildView::new(&self.pane, cx).boxed()
})
// Make sure all events directly under the dock pane
// are captured
.capture_all()
.contained()
.with_style(style.maximized)
.boxed(),
)
.boxed() .boxed()
} }
}) })
@ -391,7 +400,7 @@ impl View for ToggleDockButton {
.on_up(MouseButton::Left, move |_, cx| { .on_up(MouseButton::Left, move |_, cx| {
let dock_pane = workspace.read(cx.app).dock_pane(); let dock_pane = workspace.read(cx.app).dock_pane();
let drop_index = dock_pane.read(cx.app).items_len() + 1; let drop_index = dock_pane.read(cx.app).items_len() + 1;
Pane::handle_dropped_item(&dock_pane.downgrade(), drop_index, cx); Pane::handle_dropped_item(&dock_pane.downgrade(), drop_index, false, cx);
}); });
if dock_position.is_visible() { if dock_position.is_visible() {

View file

@ -1061,7 +1061,6 @@ impl Pane {
enum Tab {} enum Tab {}
enum Filler {} enum Filler {}
let pane = cx.handle(); let pane = cx.handle();
MouseEventHandler::<Tabs>::new(0, cx, |_, cx| {
let autoscroll = if mem::take(&mut self.autoscroll) { let autoscroll = if mem::take(&mut self.autoscroll) {
Some(self.active_item_index) Some(self.active_item_index)
} else { } else {
@ -1082,7 +1081,7 @@ impl Pane {
let tab_active = ix == self.active_item_index; let tab_active = ix == self.active_item_index;
row.add_child({ row.add_child({
MouseEventHandler::<Tab>::new(ix, cx, { MouseEventHandler::<Tab>::above(ix, cx, {
let item = item.clone(); let item = item.clone();
let pane = pane.clone(); let pane = pane.clone();
let detail = detail.clone(); let detail = detail.clone();
@ -1090,8 +1089,7 @@ impl Pane {
let theme = cx.global::<Settings>().theme.clone(); let theme = cx.global::<Settings>().theme.clone();
move |mouse_state, cx| { move |mouse_state, cx| {
let tab_style = let tab_style = theme.workspace.tab_bar.tab_style(pane_active, tab_active);
theme.workspace.tab_bar.tab_style(pane_active, tab_active);
let hovered = mouse_state.hovered(); let hovered = mouse_state.hovered();
Self::render_tab( Self::render_tab(
&item, &item,
@ -1112,6 +1110,7 @@ impl Pane {
}) })
.on_down(MouseButton::Left, move |_, cx| { .on_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ActivateItem(ix)); cx.dispatch_action(ActivateItem(ix));
cx.propagate_event();
}) })
.on_click(MouseButton::Middle, { .on_click(MouseButton::Middle, {
let item = item.clone(); let item = item.clone();
@ -1125,7 +1124,7 @@ impl Pane {
}) })
.on_up(MouseButton::Left, { .on_up(MouseButton::Left, {
let pane = pane.clone(); let pane = pane.clone();
move |_, cx: &mut EventContext| Pane::handle_dropped_item(&pane, ix, cx) move |_, cx: &mut EventContext| Pane::handle_dropped_item(&pane, ix, true, cx)
}) })
.as_draggable( .as_draggable(
DraggedItem { DraggedItem {
@ -1165,23 +1164,20 @@ impl Pane {
.with_style(filler_style.container) .with_style(filler_style.container)
.with_border(filler_style.container.border); .with_border(filler_style.container.border);
if let Some(overlay) = if let Some(overlay) = Self::tab_overlay_color(mouse_state.hovered(), &theme, cx) {
Self::tab_overlay_color(mouse_state.hovered(), &theme, cx)
{
filler = filler.with_overlay_color(overlay); filler = filler.with_overlay_color(overlay);
} }
filler.boxed() filler.boxed()
}) })
.on_up(MouseButton::Left, move |_, cx| {
Pane::handle_dropped_item(&pane, filler_index, true, cx)
})
.flex(1., true) .flex(1., true)
.named("filler"), .named("filler"),
); );
row.boxed() row
})
.on_up(MouseButton::Left, move |_, cx| {
Pane::handle_dropped_item(&pane, filler_index, cx)
})
} }
fn tab_details(&self, cx: &AppContext) -> Vec<usize> { fn tab_details(&self, cx: &AppContext) -> Vec<usize> {
@ -1305,7 +1301,6 @@ impl Pane {
}) })
} }
}) })
.on_click(MouseButton::Middle, |_, cx| cx.propogate_event())
.named("close-tab-icon") .named("close-tab-icon")
} else { } else {
Empty::new().boxed() Empty::new().boxed()
@ -1325,19 +1320,26 @@ impl Pane {
tab.constrained().with_height(tab_style.height).boxed() tab.constrained().with_height(tab_style.height).boxed()
} }
pub fn handle_dropped_item(pane: &WeakViewHandle<Pane>, index: usize, cx: &mut EventContext) { pub fn handle_dropped_item(
pane: &WeakViewHandle<Pane>,
index: usize,
allow_same_pane: bool,
cx: &mut EventContext,
) {
if let Some((_, dragged_item)) = cx if let Some((_, dragged_item)) = cx
.global::<DragAndDrop<Workspace>>() .global::<DragAndDrop<Workspace>>()
.currently_dragged::<DraggedItem>(cx.window_id) .currently_dragged::<DraggedItem>(cx.window_id)
{ {
if pane != &dragged_item.pane || allow_same_pane {
cx.dispatch_action(MoveItem { cx.dispatch_action(MoveItem {
item_id: dragged_item.item.id(), item_id: dragged_item.item.id(),
from: dragged_item.pane.clone(), from: dragged_item.pane.clone(),
to: pane.clone(), to: pane.clone(),
destination_index: index, destination_index: index,
}) })
}
} else { } else {
cx.propogate_event(); cx.propagate_event();
} }
} }
@ -1448,7 +1450,7 @@ impl View for Pane {
}) })
.with_child({ .with_child({
let drop_index = self.active_item_index + 1; let drop_index = self.active_item_index + 1;
MouseEventHandler::<PaneContentTabDropTarget>::new( MouseEventHandler::<PaneContentTabDropTarget>::above(
0, 0,
cx, cx,
|_, cx| { |_, cx| {
@ -1469,7 +1471,7 @@ impl View for Pane {
.on_up(MouseButton::Left, { .on_up(MouseButton::Left, {
let pane = cx.handle(); let pane = cx.handle();
move |_, cx: &mut EventContext| { move |_, cx: &mut EventContext| {
Pane::handle_dropped_item(&pane, drop_index, cx) Pane::handle_dropped_item(&pane, drop_index, false, cx)
} }
}) })
.flex(1., true) .flex(1., true)
@ -1491,7 +1493,9 @@ impl View for Pane {
}) })
.on_up(MouseButton::Left, { .on_up(MouseButton::Left, {
let pane = this.clone(); let pane = this.clone();
move |_, cx: &mut EventContext| Pane::handle_dropped_item(&pane, 0, cx) move |_, cx: &mut EventContext| {
Pane::handle_dropped_item(&pane, 0, true, cx)
}
}) })
.boxed() .boxed()
} }