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_layer(None, |cx| {
self.paint_regions(bounds, visible_bounds, cx);
cx.paint_stacking_context(None, |cx| { });
if let Some(style) = self.cursor_style { } else {
cx.scene.push_cursor_region(CursorRegion { self.paint_regions(bounds, visible_bounds, cx);
bounds: hit_bounds, self.child.paint(bounds.origin(), visible_bounds, cx);
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),
);
});
} }
fn rect_for_text_range( fn rect_for_text_range(

View file

@ -204,25 +204,24 @@ impl Element for Overlay {
OverlayFitMode::None => {} 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 { self.child.paint(
enum OverlayHoverCapture {} bounds.origin(),
// Block hovers in lower stacking contexts RectF::new(Vector2F::zero(), cx.window_size),
cx.scene cx,
.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();
} }
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,25 +308,34 @@ 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(
// Render wash under the dock which when clicked hides it
MouseEventHandler::<ExpandedDockWash>::new(0, cx, |_, _| {
Empty::new()
.contained()
.with_background_color(style.wash_color)
.boxed()
})
.capture_all()
.on_down(MouseButton::Left, |_, cx| {
cx.dispatch_action(HideDock);
})
.with_cursor_style(CursorStyle::Arrow)
.boxed(),
)
.with_child(
MouseEventHandler::<ExpandedDockPane>::new(0, cx, |_state, cx| { MouseEventHandler::<ExpandedDockPane>::new(0, cx, |_state, cx| {
ChildView::new(&self.pane, cx).boxed() ChildView::new(&self.pane, cx).boxed()
}) })
// Make sure all events directly under the dock pane
// are captured
.capture_all() .capture_all()
.contained() .contained()
.with_style(style.maximized) .with_style(style.maximized)
.boxed() .boxed(),
}) )
.capture_all() .boxed()
.on_down(MouseButton::Left, |_, cx| {
cx.dispatch_action(HideDock);
})
.with_cursor_style(CursorStyle::Arrow)
.boxed(),
)
.with_background_color(style.wash_color)
.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,127 +1061,123 @@ 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 { None
None };
};
let pane_active = self.is_active; let pane_active = self.is_active;
let mut row = Flex::row().scrollable::<Tabs, _>(1, autoscroll, cx); let mut row = Flex::row().scrollable::<Tabs, _>(1, autoscroll, cx);
for (ix, (item, detail)) in self for (ix, (item, detail)) in self
.items .items
.iter() .iter()
.cloned() .cloned()
.zip(self.tab_details(cx)) .zip(self.tab_details(cx))
.enumerate() .enumerate()
{ {
let detail = if detail == 0 { None } else { Some(detail) }; let detail = if detail == 0 { None } else { Some(detail) };
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();
let theme = cx.global::<Settings>().theme.clone();
move |mouse_state, cx| {
let tab_style = theme.workspace.tab_bar.tab_style(pane_active, tab_active);
let hovered = mouse_state.hovered();
Self::render_tab(
&item,
pane,
ix == 0,
detail,
hovered,
Self::tab_overlay_color(hovered, theme.as_ref(), cx),
tab_style,
cx,
)
}
})
.with_cursor_style(if pane_active && tab_active {
CursorStyle::Arrow
} else {
CursorStyle::PointingHand
})
.on_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ActivateItem(ix));
cx.propagate_event();
})
.on_click(MouseButton::Middle, {
let item = item.clone();
let pane = pane.clone();
move |_, cx: &mut EventContext| {
cx.dispatch_action(CloseItem {
item_id: item.id(),
pane: pane.clone(),
})
}
})
.on_up(MouseButton::Left, {
let pane = pane.clone();
move |_, cx: &mut EventContext| Pane::handle_dropped_item(&pane, ix, true, cx)
})
.as_draggable(
DraggedItem {
item,
pane: pane.clone(),
},
{
let theme = cx.global::<Settings>().theme.clone(); let theme = cx.global::<Settings>().theme.clone();
move |mouse_state, cx| { let detail = detail.clone();
let tab_style = move |dragged_item, cx: &mut RenderContext<Workspace>| {
theme.workspace.tab_bar.tab_style(pane_active, tab_active); let tab_style = &theme.workspace.tab_bar.dragged_tab;
let hovered = mouse_state.hovered();
Self::render_tab( Self::render_tab(
&item, &dragged_item.item,
pane, dragged_item.pane.clone(),
ix == 0, false,
detail, detail,
hovered, false,
Self::tab_overlay_color(hovered, theme.as_ref(), cx), None,
tab_style, &tab_style,
cx, cx,
) )
} }
}) },
.with_cursor_style(if pane_active && tab_active { )
CursorStyle::Arrow .boxed()
} else { })
CursorStyle::PointingHand }
})
.on_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ActivateItem(ix));
})
.on_click(MouseButton::Middle, {
let item = item.clone();
let pane = pane.clone();
move |_, cx: &mut EventContext| {
cx.dispatch_action(CloseItem {
item_id: item.id(),
pane: pane.clone(),
})
}
})
.on_up(MouseButton::Left, {
let pane = pane.clone();
move |_, cx: &mut EventContext| Pane::handle_dropped_item(&pane, ix, cx)
})
.as_draggable(
DraggedItem {
item,
pane: pane.clone(),
},
{
let theme = cx.global::<Settings>().theme.clone();
let detail = detail.clone(); // Use the inactive tab style along with the current pane's active status to decide how to render
move |dragged_item, cx: &mut RenderContext<Workspace>| { // the filler
let tab_style = &theme.workspace.tab_bar.dragged_tab; let filler_style = theme.workspace.tab_bar.tab_style(pane_active, false);
Self::render_tab( row.add_child(
&dragged_item.item, MouseEventHandler::<Filler>::new(0, cx, |mouse_state, cx| {
dragged_item.pane.clone(), let mut filler = Empty::new()
false, .contained()
detail, .with_style(filler_style.container)
false, .with_border(filler_style.container.border);
None,
&tab_style,
cx,
)
}
},
)
.boxed()
})
}
// Use the inactive tab style along with the current pane's active status to decide how to render if let Some(overlay) = Self::tab_overlay_color(mouse_state.hovered(), &theme, cx) {
// the filler filler = filler.with_overlay_color(overlay);
let filler_style = theme.workspace.tab_bar.tab_style(pane_active, false); }
row.add_child(
MouseEventHandler::<Filler>::new(0, cx, |mouse_state, cx| {
let mut filler = Empty::new()
.contained()
.with_style(filler_style.container)
.with_border(filler_style.container.border);
if let Some(overlay) = filler.boxed()
Self::tab_overlay_color(mouse_state.hovered(), &theme, cx) })
{ .on_up(MouseButton::Left, move |_, cx| {
filler = filler.with_overlay_color(overlay); Pane::handle_dropped_item(&pane, filler_index, true, cx)
} })
.flex(1., true)
.named("filler"),
);
filler.boxed() row
})
.flex(1., true)
.named("filler"),
);
row.boxed()
})
.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)
{ {
cx.dispatch_action(MoveItem { if pane != &dragged_item.pane || allow_same_pane {
item_id: dragged_item.item.id(), cx.dispatch_action(MoveItem {
from: dragged_item.pane.clone(), item_id: dragged_item.item.id(),
to: pane.clone(), from: dragged_item.pane.clone(),
destination_index: index, to: pane.clone(),
}) 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()
} }