Switch MouseEventHandler to use MouseRegions

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2022-05-26 20:00:01 -06:00
parent 50edcb06dd
commit 893f15ddab
26 changed files with 150 additions and 195 deletions

View file

@ -270,7 +270,7 @@ impl View for AutoUpdateIndicator {
) )
.boxed() .boxed()
}) })
.on_click(|_, cx| cx.dispatch_action(DismissErrorMessage)) .on_click(|_, _, cx| cx.dispatch_action(DismissErrorMessage))
.boxed() .boxed()
} }
AutoUpdateStatus::Idle => Empty::new().boxed(), AutoUpdateStatus::Idle => Empty::new().boxed(),

View file

@ -320,7 +320,7 @@ impl ChatPanel {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| { .on_click(move |_, _, cx| {
let rpc = rpc.clone(); let rpc = rpc.clone();
let this = this.clone(); let this = this.clone();
cx.spawn(|mut cx| async move { cx.spawn(|mut cx| async move {

View file

@ -1,9 +1,9 @@
use fuzzy::{StringMatch, StringMatchCandidate}; use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{ use gpui::{
actions, actions,
elements::{ChildView, Flex, Label, MouseState, ParentElement}, elements::{ChildView, Flex, Label, ParentElement},
keymap::Keystroke, keymap::Keystroke,
Action, Element, Entity, MutableAppContext, View, ViewContext, ViewHandle, Action, Element, Entity, MouseState, MutableAppContext, View, ViewContext, ViewHandle,
}; };
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use settings::Settings; use settings::Settings;
@ -203,7 +203,7 @@ impl PickerDelegate for CommandPalette {
fn render_match( fn render_match(
&self, &self,
ix: usize, ix: usize,
mouse_state: &MouseState, mouse_state: MouseState,
selected: bool, selected: bool,
cx: &gpui::AppContext, cx: &gpui::AppContext,
) -> gpui::ElementBox { ) -> gpui::ElementBox {

View file

@ -1,7 +1,7 @@
use client::{ContactRequestStatus, User, UserStore}; use client::{ContactRequestStatus, User, UserStore};
use gpui::{ use gpui::{
actions, elements::*, Entity, ModelHandle, MutableAppContext, RenderContext, Task, View, actions, elements::*, Entity, ModelHandle, MouseState, MutableAppContext, RenderContext, Task,
ViewContext, ViewHandle, View, ViewContext, ViewHandle,
}; };
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use settings::Settings; use settings::Settings;
@ -105,7 +105,7 @@ impl PickerDelegate for ContactFinder {
fn render_match( fn render_match(
&self, &self,
ix: usize, ix: usize,
mouse_state: &MouseState, mouse_state: MouseState,
selected: bool, selected: bool,
cx: &gpui::AppContext, cx: &gpui::AppContext,
) -> ElementBox { ) -> ElementBox {

View file

@ -259,7 +259,7 @@ impl ContactsPanel {
) -> ElementBox { ) -> ElementBox {
enum Header {} enum Header {}
let header_style = theme.header_row.style_for(&Default::default(), is_selected); let header_style = theme.header_row.style_for(Default::default(), is_selected);
let text = match section { let text = match section {
Section::Requests => "Requests", Section::Requests => "Requests",
Section::Online => "Online", Section::Online => "Online",
@ -299,7 +299,7 @@ impl ContactsPanel {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| cx.dispatch_action(ToggleExpanded(section))) .on_click(move |_, _, cx| cx.dispatch_action(ToggleExpanded(section)))
.boxed() .boxed()
} }
@ -331,11 +331,7 @@ impl ContactsPanel {
.constrained() .constrained()
.with_height(theme.row_height) .with_height(theme.row_height)
.contained() .contained()
.with_style( .with_style(*theme.contact_row.style_for(Default::default(), is_selected))
*theme
.contact_row
.style_for(&Default::default(), is_selected),
)
.boxed() .boxed()
} }
@ -442,7 +438,7 @@ impl ContactsPanel {
} else { } else {
CursorStyle::Arrow CursorStyle::Arrow
}) })
.on_click(move |_, cx| { .on_click(move |_, _, cx| {
if !is_host { if !is_host {
cx.dispatch_global_action(JoinProject { cx.dispatch_global_action(JoinProject {
contact: contact.clone(), contact: contact.clone(),
@ -504,7 +500,7 @@ impl ContactsPanel {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| { .on_click(move |_, _, cx| {
cx.dispatch_action(RespondToContactRequest { cx.dispatch_action(RespondToContactRequest {
user_id, user_id,
accept: false, accept: false,
@ -526,7 +522,7 @@ impl ContactsPanel {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| { .on_click(move |_, _, cx| {
cx.dispatch_action(RespondToContactRequest { cx.dispatch_action(RespondToContactRequest {
user_id, user_id,
accept: true, accept: true,
@ -549,7 +545,7 @@ impl ContactsPanel {
}) })
.with_padding(Padding::uniform(2.)) .with_padding(Padding::uniform(2.))
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| cx.dispatch_action(RemoveContact(user_id))) .on_click(move |_, _, cx| cx.dispatch_action(RemoveContact(user_id)))
.flex_float() .flex_float()
.boxed(), .boxed(),
); );
@ -558,11 +554,7 @@ impl ContactsPanel {
row.constrained() row.constrained()
.with_height(theme.row_height) .with_height(theme.row_height)
.contained() .contained()
.with_style( .with_style(*theme.contact_row.style_for(Default::default(), is_selected))
*theme
.contact_row
.style_for(&Default::default(), is_selected),
)
.boxed() .boxed()
} }
@ -862,7 +854,7 @@ impl View for ContactsPanel {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(|_, cx| cx.dispatch_action(contact_finder::Toggle)) .on_click(|_, _, cx| cx.dispatch_action(contact_finder::Toggle))
.boxed(), .boxed(),
) )
.constrained() .constrained()
@ -910,7 +902,7 @@ impl View for ContactsPanel {
}, },
) )
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| { .on_click(move |_, _, cx| {
cx.write_to_clipboard(ClipboardItem::new( cx.write_to_clipboard(ClipboardItem::new(
info.url.to_string(), info.url.to_string(),
)); ));

View file

@ -61,7 +61,7 @@ pub fn render_user_notification<V: View, A: Action + Clone>(
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_padding(Padding::uniform(5.)) .with_padding(Padding::uniform(5.))
.on_click(move |_, cx| cx.dispatch_any_action(dismiss_action.boxed_clone())) .on_click(move |_, _, cx| cx.dispatch_any_action(dismiss_action.boxed_clone()))
.aligned() .aligned()
.constrained() .constrained()
.with_height( .with_height(
@ -76,13 +76,10 @@ pub fn render_user_notification<V: View, A: Action + Clone>(
.named("contact notification header"), .named("contact notification header"),
) )
.with_children(body.map(|body| { .with_children(body.map(|body| {
Label::new( Label::new(body.to_string(), theme.body_message.text.clone())
body.to_string(), .contained()
theme.body_message.text.clone(), .with_style(theme.body_message.container)
) .boxed()
.contained()
.with_style(theme.body_message.container)
.boxed()
})) }))
.with_children(if buttons.is_empty() { .with_children(if buttons.is_empty() {
None None
@ -99,7 +96,7 @@ pub fn render_user_notification<V: View, A: Action + Clone>(
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| cx.dispatch_any_action(action.boxed_clone())) .on_click(move |_, _, cx| cx.dispatch_any_action(action.boxed_clone()))
.boxed() .boxed()
}, },
)) ))

View file

@ -159,7 +159,7 @@ impl View for DiagnosticIndicator {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(|_, cx| cx.dispatch_action(crate::Deploy)) .on_click(|_, _, cx| cx.dispatch_action(crate::Deploy))
.aligned() .aligned()
.boxed(), .boxed(),
); );
@ -192,7 +192,7 @@ impl View for DiagnosticIndicator {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(|_, cx| cx.dispatch_action(GoToNextDiagnostic)) .on_click(|_, _, cx| cx.dispatch_action(GoToNextDiagnostic))
.boxed(), .boxed(),
); );
} }

View file

@ -679,7 +679,7 @@ impl CompletionsMenu {
}, },
) )
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_mouse_down(move |cx| { .on_mouse_down(move |_, cx| {
cx.dispatch_action(ConfirmCompletion { cx.dispatch_action(ConfirmCompletion {
item_ix: Some(item_ix), item_ix: Some(item_ix),
}); });
@ -812,7 +812,7 @@ impl CodeActionsMenu {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_mouse_down(move |cx| { .on_mouse_down(move |_, cx| {
cx.dispatch_action(ConfirmCodeAction { cx.dispatch_action(ConfirmCodeAction {
item_ix: Some(item_ix), item_ix: Some(item_ix),
}); });
@ -2603,7 +2603,7 @@ impl Editor {
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_padding(Padding::uniform(3.)) .with_padding(Padding::uniform(3.))
.on_mouse_down(|cx| { .on_mouse_down(|_, cx| {
cx.dispatch_action(ToggleCodeActions { cx.dispatch_action(ToggleCodeActions {
deployed_from_indicator: true, deployed_from_indicator: true,
}); });

View file

@ -1,7 +1,7 @@
use fuzzy::PathMatch; use fuzzy::PathMatch;
use gpui::{ use gpui::{
actions, elements::*, AppContext, Entity, ModelHandle, MutableAppContext, RenderContext, Task, actions, elements::*, AppContext, Entity, ModelHandle, MouseState, MutableAppContext,
View, ViewContext, ViewHandle, RenderContext, Task, View, ViewContext, ViewHandle,
}; };
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use project::{Project, ProjectPath, WorktreeId}; use project::{Project, ProjectPath, WorktreeId};
@ -226,7 +226,7 @@ impl PickerDelegate for FileFinder {
fn render_match( fn render_match(
&self, &self,
ix: usize, ix: usize,
mouse_state: &MouseState, mouse_state: MouseState,
selected: bool, selected: bool,
cx: &AppContext, cx: &AppContext,
) -> ElementBox { ) -> ElementBox {

View file

@ -460,7 +460,7 @@ impl TestAppContext {
view_id: handle.id(), view_id: handle.id(),
view_type: PhantomData, view_type: PhantomData,
titlebar_height: 0., titlebar_height: 0.,
hovered_region_id: None, hovered_region_ids: Default::default(),
clicked_region_id: None, clicked_region_id: None,
refreshing: false, refreshing: false,
}; };
@ -1080,7 +1080,7 @@ impl MutableAppContext {
window_id, window_id,
view_id, view_id,
titlebar_height, titlebar_height,
hovered_region_id: None, hovered_region_ids: Default::default(),
clicked_region_id: None, clicked_region_id: None,
refreshing: false, refreshing: false,
}) })
@ -3402,7 +3402,7 @@ pub struct RenderParams {
pub window_id: usize, pub window_id: usize,
pub view_id: usize, pub view_id: usize,
pub titlebar_height: f32, pub titlebar_height: f32,
pub hovered_region_id: Option<MouseRegionId>, pub hovered_region_ids: HashSet<MouseRegionId>,
pub clicked_region_id: Option<MouseRegionId>, pub clicked_region_id: Option<MouseRegionId>,
pub refreshing: bool, pub refreshing: bool,
} }
@ -3411,14 +3411,14 @@ pub struct RenderContext<'a, T: View> {
pub(crate) window_id: usize, pub(crate) window_id: usize,
pub(crate) view_id: usize, pub(crate) view_id: usize,
pub(crate) view_type: PhantomData<T>, pub(crate) view_type: PhantomData<T>,
pub(crate) hovered_region_id: Option<MouseRegionId>, pub(crate) hovered_region_ids: HashSet<MouseRegionId>,
pub(crate) clicked_region_id: Option<MouseRegionId>, pub(crate) clicked_region_id: Option<MouseRegionId>,
pub app: &'a mut MutableAppContext, pub app: &'a mut MutableAppContext,
pub titlebar_height: f32, pub titlebar_height: f32,
pub refreshing: bool, pub refreshing: bool,
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy, Default)]
pub struct MouseState { pub struct MouseState {
pub hovered: bool, pub hovered: bool,
pub clicked: bool, pub clicked: bool,
@ -3432,7 +3432,7 @@ impl<'a, V: View> RenderContext<'a, V> {
view_id: params.view_id, view_id: params.view_id,
view_type: PhantomData, view_type: PhantomData,
titlebar_height: params.titlebar_height, titlebar_height: params.titlebar_height,
hovered_region_id: params.hovered_region_id, hovered_region_ids: params.hovered_region_ids.clone(),
clicked_region_id: params.clicked_region_id, clicked_region_id: params.clicked_region_id,
refreshing: params.refreshing, refreshing: params.refreshing,
} }
@ -3447,14 +3447,14 @@ impl<'a, V: View> RenderContext<'a, V> {
} }
pub fn mouse_state<Tag: 'static>(&self, region_id: usize) -> MouseState { pub fn mouse_state<Tag: 'static>(&self, region_id: usize) -> MouseState {
let region_id = Some(MouseRegionId { let region_id = MouseRegionId {
view_id: self.view_id, view_id: self.view_id,
tag: TypeId::of::<Tag>(), tag: TypeId::of::<Tag>(),
region_id, region_id,
}); };
MouseState { MouseState {
hovered: self.hovered_region_id == region_id, hovered: self.hovered_region_ids.contains(&region_id),
clicked: self.clicked_region_id == region_id, clicked: self.clicked_region_id == Some(region_id),
} }
} }

View file

@ -1,3 +1,5 @@
use std::{any::TypeId, rc::Rc};
use super::Padding; use super::Padding;
use crate::{ use crate::{
geometry::{ geometry::{
@ -6,40 +8,33 @@ use crate::{
}, },
platform::CursorStyle, platform::CursorStyle,
scene::CursorRegion, scene::CursorRegion,
DebugContext, Element, ElementBox, ElementStateHandle, Event, EventContext, LayoutContext, DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseRegion, MouseState,
PaintContext, RenderContext, SizeConstraint, View, PaintContext, RenderContext, SizeConstraint, View,
}; };
use serde_json::json; use serde_json::json;
pub struct MouseEventHandler { pub struct MouseEventHandler {
state: ElementStateHandle<MouseState>,
child: ElementBox, child: ElementBox,
tag: TypeId,
id: usize,
cursor_style: Option<CursorStyle>, cursor_style: Option<CursorStyle>,
mouse_down_handler: Option<Box<dyn FnMut(&mut EventContext)>>, mouse_down_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
click_handler: Option<Box<dyn FnMut(usize, &mut EventContext)>>, click_handler: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
drag_handler: Option<Box<dyn FnMut(Vector2F, &mut EventContext)>>, drag_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
padding: Padding, padding: Padding,
} }
#[derive(Default)]
pub struct MouseState {
pub hovered: bool,
pub clicked: bool,
prev_drag_position: Option<Vector2F>,
}
impl MouseEventHandler { impl MouseEventHandler {
pub fn new<Tag, V, F>(id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self pub fn new<Tag, V, F>(id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self
where where
Tag: 'static, Tag: 'static,
V: View, V: View,
F: FnOnce(&MouseState, &mut RenderContext<V>) -> ElementBox, F: FnOnce(MouseState, &mut RenderContext<V>) -> ElementBox,
{ {
let state_handle = cx.element_state::<Tag, _>(id);
let child = state_handle.update(cx, |state, cx| render_child(state, cx));
Self { Self {
state: state_handle, id,
child, tag: TypeId::of::<Tag>(),
child: render_child(cx.mouse_state::<Tag>(id), cx),
cursor_style: None, cursor_style: None,
mouse_down_handler: None, mouse_down_handler: None,
click_handler: None, click_handler: None,
@ -53,18 +48,24 @@ impl MouseEventHandler {
self self
} }
pub fn on_mouse_down(mut self, handler: impl FnMut(&mut EventContext) + 'static) -> Self { pub fn on_mouse_down(
self.mouse_down_handler = Some(Box::new(handler)); mut self,
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
) -> Self {
self.mouse_down_handler = Some(Rc::new(handler));
self self
} }
pub fn on_click(mut self, handler: impl FnMut(usize, &mut EventContext) + 'static) -> Self { pub fn on_click(
self.click_handler = Some(Box::new(handler)); mut self,
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
) -> Self {
self.click_handler = Some(Rc::new(handler));
self self
} }
pub fn on_drag(mut self, handler: impl FnMut(Vector2F, &mut EventContext) + 'static) -> Self { pub fn on_drag(mut self, handler: impl Fn(Vector2F, &mut EventContext) + 'static) -> Self {
self.drag_handler = Some(Box::new(handler)); self.drag_handler = Some(Rc::new(handler));
self self
} }
@ -107,6 +108,18 @@ impl Element for MouseEventHandler {
style, style,
}); });
} }
cx.scene.push_mouse_region(MouseRegion {
view_id: cx.current_view_id(),
tag: self.tag,
region_id: self.id,
bounds,
hover: None,
click: self.click_handler.clone(),
mouse_down: self.mouse_down_handler.clone(),
drag: self.drag_handler.clone(),
});
self.child.paint(bounds.origin(), visible_bounds, cx); self.child.paint(bounds.origin(), visible_bounds, cx);
} }
@ -114,81 +127,12 @@ impl Element for MouseEventHandler {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
visible_bounds: RectF, _: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
let hit_bounds = self.hit_bounds(visible_bounds); self.child.dispatch_event(event, cx)
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();
let handled_in_child = self.child.dispatch_event(event, cx);
self.state.update(cx, |state, cx| match event {
Event::MouseMoved {
position,
left_mouse_down,
} => {
if !left_mouse_down {
let mouse_in = hit_bounds.contains_point(*position);
if state.hovered != mouse_in {
state.hovered = mouse_in;
cx.notify();
return true;
}
}
handled_in_child
}
Event::LeftMouseDown { position, .. } => {
if !handled_in_child && hit_bounds.contains_point(*position) {
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
}
}
Event::LeftMouseUp {
position,
click_count,
..
} => {
state.prev_drag_position = None;
if !handled_in_child && state.clicked {
state.clicked = false;
cx.notify();
if let Some(handler) = click_handler {
if hit_bounds.contains_point(*position) {
handler(*click_count, cx);
}
}
true
} else {
handled_in_child
}
}
Event::LeftMouseDragged { position, .. } => {
if !handled_in_child && state.clicked {
let prev_drag_position = state.prev_drag_position.replace(*position);
if let Some((handler, prev_position)) = drag_handler.zip(prev_drag_position) {
let delta = *position - prev_position;
if !delta.is_zero() {
(handler)(delta, cx);
}
}
true
} else {
handled_in_child
}
}
_ => handled_in_child,
})
} }
fn debug( fn debug(

View file

@ -31,7 +31,7 @@ pub struct Presenter {
text_layout_cache: TextLayoutCache, text_layout_cache: TextLayoutCache,
asset_cache: Arc<AssetCache>, asset_cache: Arc<AssetCache>,
last_mouse_moved_event: Option<Event>, last_mouse_moved_event: Option<Event>,
hovered_region_id: Option<MouseRegionId>, hovered_region_ids: HashSet<MouseRegionId>,
clicked_region: Option<MouseRegion>, clicked_region: Option<MouseRegion>,
prev_drag_position: Option<Vector2F>, prev_drag_position: Option<Vector2F>,
titlebar_height: f32, titlebar_height: f32,
@ -56,7 +56,7 @@ impl Presenter {
text_layout_cache, text_layout_cache,
asset_cache, asset_cache,
last_mouse_moved_event: None, last_mouse_moved_event: None,
hovered_region_id: None, hovered_region_ids: Default::default(),
clicked_region: None, clicked_region: None,
prev_drag_position: None, prev_drag_position: None,
titlebar_height, titlebar_height,
@ -100,7 +100,7 @@ impl Presenter {
window_id: self.window_id, window_id: self.window_id,
view_id: *view_id, view_id: *view_id,
titlebar_height: self.titlebar_height, titlebar_height: self.titlebar_height,
hovered_region_id: self.hovered_region_id, hovered_region_ids: self.hovered_region_ids.clone(),
clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id), clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id),
refreshing: false, refreshing: false,
}) })
@ -118,7 +118,7 @@ impl Presenter {
window_id: self.window_id, window_id: self.window_id,
view_id: *view_id, view_id: *view_id,
titlebar_height: self.titlebar_height, titlebar_height: self.titlebar_height,
hovered_region_id: self.hovered_region_id, hovered_region_ids: self.hovered_region_ids.clone(),
clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id), clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id),
refreshing: true, refreshing: true,
}) })
@ -181,7 +181,7 @@ impl Presenter {
asset_cache: &self.asset_cache, asset_cache: &self.asset_cache,
view_stack: Vec::new(), view_stack: Vec::new(),
refreshing, refreshing,
hovered_region_id: self.hovered_region_id, hovered_region_ids: self.hovered_region_ids.clone(),
clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id), clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id),
titlebar_height: self.titlebar_height, titlebar_height: self.titlebar_height,
app: cx, app: cx,
@ -198,14 +198,16 @@ impl Presenter {
font_cache: &self.font_cache, font_cache: &self.font_cache,
text_layout_cache: &self.text_layout_cache, text_layout_cache: &self.text_layout_cache,
rendered_views: &mut self.rendered_views, rendered_views: &mut self.rendered_views,
view_stack: Vec::new(),
app: cx, app: cx,
} }
} }
pub fn dispatch_event(&mut self, event: Event, cx: &mut MutableAppContext) { pub fn dispatch_event(&mut self, event: Event, cx: &mut MutableAppContext) {
if let Some(root_view_id) = cx.root_view_id(self.window_id) { if let Some(root_view_id) = cx.root_view_id(self.window_id) {
let mut unhovered_region = None; let mut invalidated_views = Vec::new();
let mut hovered_region = None; let mut hovered_regions = Vec::new();
let mut unhovered_regions = Vec::new();
let mut clicked_region = None; let mut clicked_region = None;
let mut dragged_region = None; let mut dragged_region = None;
@ -213,6 +215,7 @@ impl Presenter {
Event::LeftMouseDown { position, .. } => { Event::LeftMouseDown { position, .. } => {
for region in self.mouse_regions.iter().rev() { for region in self.mouse_regions.iter().rev() {
if region.bounds.contains_point(position) { if region.bounds.contains_point(position) {
invalidated_views.push(region.view_id);
self.clicked_region = Some(region.clone()); self.clicked_region = Some(region.clone());
self.prev_drag_position = Some(position); self.prev_drag_position = Some(position);
break; break;
@ -226,6 +229,7 @@ impl Presenter {
} => { } => {
self.prev_drag_position.take(); self.prev_drag_position.take();
if let Some(region) = self.clicked_region.take() { if let Some(region) = self.clicked_region.take() {
invalidated_views.push(region.view_id);
if region.bounds.contains_point(position) { if region.bounds.contains_point(position) {
clicked_region = Some((region, position, click_count)); clicked_region = Some((region, position, click_count));
} }
@ -248,13 +252,18 @@ impl Presenter {
cx.platform().set_cursor_style(style_to_assign); cx.platform().set_cursor_style(style_to_assign);
for region in self.mouse_regions.iter().rev() { for region in self.mouse_regions.iter().rev() {
let region_id = region.id();
if region.bounds.contains_point(position) { if region.bounds.contains_point(position) {
if hovered_region.is_none() { if !self.hovered_region_ids.contains(&region_id) {
hovered_region = Some(region.clone()); invalidated_views.push(region.view_id);
hovered_regions.push(region.clone());
self.hovered_region_ids.insert(region_id);
} }
} else { } else {
if self.hovered_region_id == Some(region.id()) { if self.hovered_region_ids.contains(&region_id) {
unhovered_region = Some(region.clone()) invalidated_views.push(region.view_id);
unhovered_regions.push(region.clone());
self.hovered_region_ids.remove(&region_id);
} }
} }
} }
@ -279,10 +288,8 @@ impl Presenter {
_ => {} _ => {}
} }
self.hovered_region_id = hovered_region.as_ref().map(MouseRegion::id);
let mut event_cx = self.build_event_context(cx); let mut event_cx = self.build_event_context(cx);
if let Some(unhovered_region) = unhovered_region { for unhovered_region in unhovered_regions {
if let Some(hover_callback) = unhovered_region.hover { if let Some(hover_callback) = unhovered_region.hover {
event_cx.with_current_view(unhovered_region.view_id, |event_cx| { event_cx.with_current_view(unhovered_region.view_id, |event_cx| {
hover_callback(false, event_cx); hover_callback(false, event_cx);
@ -290,7 +297,7 @@ impl Presenter {
} }
} }
if let Some(hovered_region) = hovered_region { for hovered_region in hovered_regions {
if let Some(hover_callback) = hovered_region.hover { if let Some(hover_callback) = hovered_region.hover {
event_cx.with_current_view(hovered_region.view_id, |event_cx| { event_cx.with_current_view(hovered_region.view_id, |event_cx| {
hover_callback(true, event_cx); hover_callback(true, event_cx);
@ -316,7 +323,7 @@ impl Presenter {
event_cx.dispatch_event(root_view_id, &event); event_cx.dispatch_event(root_view_id, &event);
let invalidated_views = event_cx.invalidated_views; invalidated_views.extend(event_cx.invalidated_views);
let dispatch_directives = event_cx.dispatched_actions; let dispatch_directives = event_cx.dispatched_actions;
for view_id in invalidated_views { for view_id in invalidated_views {
@ -376,7 +383,7 @@ pub struct LayoutContext<'a> {
pub app: &'a mut MutableAppContext, pub app: &'a mut MutableAppContext,
pub refreshing: bool, pub refreshing: bool,
titlebar_height: f32, titlebar_height: f32,
hovered_region_id: Option<MouseRegionId>, hovered_region_ids: HashSet<MouseRegionId>,
clicked_region_id: Option<MouseRegionId>, clicked_region_id: Option<MouseRegionId>,
} }
@ -405,7 +412,7 @@ impl<'a> LayoutContext<'a> {
view_id: handle.id(), view_id: handle.id(),
view_type: PhantomData, view_type: PhantomData,
titlebar_height: self.titlebar_height, titlebar_height: self.titlebar_height,
hovered_region_id: self.hovered_region_id, hovered_region_ids: self.hovered_region_ids.clone(),
clicked_region_id: self.clicked_region_id, clicked_region_id: self.clicked_region_id,
refreshing: self.refreshing, refreshing: self.refreshing,
}; };
@ -469,6 +476,7 @@ impl<'a> UpgradeViewHandle for LayoutContext<'a> {
pub struct PaintContext<'a> { pub struct PaintContext<'a> {
rendered_views: &'a mut HashMap<usize, ElementBox>, rendered_views: &'a mut HashMap<usize, ElementBox>,
view_stack: Vec<usize>,
pub scene: &'a mut Scene, pub scene: &'a mut Scene,
pub font_cache: &'a FontCache, pub font_cache: &'a FontCache,
pub text_layout_cache: &'a TextLayoutCache, pub text_layout_cache: &'a TextLayoutCache,
@ -478,10 +486,16 @@ pub struct PaintContext<'a> {
impl<'a> PaintContext<'a> { impl<'a> PaintContext<'a> {
fn paint(&mut self, view_id: usize, origin: Vector2F, visible_bounds: RectF) { fn paint(&mut self, view_id: usize, origin: Vector2F, visible_bounds: RectF) {
if let Some(mut tree) = self.rendered_views.remove(&view_id) { if let Some(mut tree) = self.rendered_views.remove(&view_id) {
self.view_stack.push(view_id);
tree.paint(origin, visible_bounds, self); tree.paint(origin, visible_bounds, self);
self.rendered_views.insert(view_id, tree); self.rendered_views.insert(view_id, tree);
self.view_stack.pop();
} }
} }
pub fn current_view_id(&self) -> usize {
*self.view_stack.last().unwrap()
}
} }
impl<'a> Deref for PaintContext<'a> { impl<'a> Deref for PaintContext<'a> {

View file

@ -50,11 +50,12 @@ pub struct MouseRegion {
pub region_id: usize, pub region_id: usize,
pub bounds: RectF, pub bounds: RectF,
pub hover: Option<Rc<dyn Fn(bool, &mut EventContext)>>, pub hover: Option<Rc<dyn Fn(bool, &mut EventContext)>>,
pub mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
pub click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>, pub click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
pub drag: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, pub drag: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
} }
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct MouseRegionId { pub struct MouseRegionId {
pub view_id: usize, pub view_id: usize,
pub tag: TypeId, pub tag: TypeId,
@ -242,6 +243,10 @@ impl Scene {
self.active_layer().push_cursor_region(region); self.active_layer().push_cursor_region(region);
} }
pub fn push_mouse_region(&mut self, region: MouseRegion) {
self.active_layer().push_mouse_region(region);
}
pub fn push_image(&mut self, image: Image) { pub fn push_image(&mut self, image: Image) {
self.active_layer().push_image(image) self.active_layer().push_image(image)
} }

View file

@ -119,7 +119,7 @@ impl View for Select {
.with_style(style.header) .with_style(style.header)
.boxed() .boxed()
}) })
.on_click(move |_, cx| cx.dispatch_action(ToggleSelect)) .on_click(move |_, _, cx| cx.dispatch_action(ToggleSelect))
.boxed(), .boxed(),
); );
if self.is_open { if self.is_open {
@ -151,7 +151,9 @@ impl View for Select {
) )
}, },
) )
.on_click(move |_, cx| cx.dispatch_action(SelectItem(ix))) .on_click(move |_, _, cx| {
cx.dispatch_action(SelectItem(ix))
})
.boxed() .boxed()
})) }))
}, },

View file

@ -4,8 +4,8 @@ use editor::{
}; };
use fuzzy::StringMatch; use fuzzy::StringMatch;
use gpui::{ use gpui::{
actions, elements::*, geometry::vector::Vector2F, AppContext, Entity, MutableAppContext, actions, elements::*, geometry::vector::Vector2F, AppContext, Entity, MouseState,
RenderContext, Task, View, ViewContext, ViewHandle, MutableAppContext, RenderContext, Task, View, ViewContext, ViewHandle,
}; };
use language::Outline; use language::Outline;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
@ -231,7 +231,7 @@ impl PickerDelegate for OutlineView {
fn render_match( fn render_match(
&self, &self,
ix: usize, ix: usize,
mouse_state: &MouseState, mouse_state: MouseState,
selected: bool, selected: bool,
cx: &AppContext, cx: &AppContext,
) -> ElementBox { ) -> ElementBox {

View file

@ -1,14 +1,14 @@
use editor::Editor; use editor::Editor;
use gpui::{ use gpui::{
elements::{ elements::{
ChildView, Flex, Label, MouseEventHandler, MouseState, ParentElement, ScrollTarget, ChildView, Flex, Label, MouseEventHandler, ParentElement, ScrollTarget, UniformList,
UniformList, UniformListState, UniformListState,
}, },
geometry::vector::{vec2f, Vector2F}, geometry::vector::{vec2f, Vector2F},
keymap, keymap,
platform::CursorStyle, platform::CursorStyle,
AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, Task, View, AppContext, Axis, Element, ElementBox, Entity, MouseState, MutableAppContext, RenderContext,
ViewContext, ViewHandle, WeakViewHandle, Task, View, ViewContext, ViewHandle, WeakViewHandle,
}; };
use settings::Settings; use settings::Settings;
use std::cmp; use std::cmp;
@ -34,7 +34,7 @@ pub trait PickerDelegate: View {
fn render_match( fn render_match(
&self, &self,
ix: usize, ix: usize,
state: &MouseState, state: MouseState,
selected: bool, selected: bool,
cx: &AppContext, cx: &AppContext,
) -> ElementBox; ) -> ElementBox;
@ -92,7 +92,7 @@ impl<D: PickerDelegate> View for Picker<D> {
.read(cx) .read(cx)
.render_match(ix, state, ix == selected_ix, cx) .render_match(ix, state, ix == selected_ix, cx)
}) })
.on_mouse_down(move |cx| cx.dispatch_action(SelectIndex(ix))) .on_mouse_down(move |_, cx| cx.dispatch_action(SelectIndex(ix)))
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.boxed() .boxed()
})); }));

View file

@ -839,7 +839,7 @@ impl ProjectPanel {
.with_padding_left(padding) .with_padding_left(padding)
.boxed() .boxed()
}) })
.on_click(move |click_count, cx| { .on_click(move |_, click_count, cx| {
if kind == EntryKind::Dir { if kind == EntryKind::Dir {
cx.dispatch_action(ToggleExpanded(entry_id)) cx.dispatch_action(ToggleExpanded(entry_id))
} else { } else {

View file

@ -3,8 +3,8 @@ use editor::{
}; };
use fuzzy::{StringMatch, StringMatchCandidate}; use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{ use gpui::{
actions, elements::*, AppContext, Entity, ModelHandle, MutableAppContext, RenderContext, Task, actions, elements::*, AppContext, Entity, ModelHandle, MouseState, MutableAppContext,
View, ViewContext, ViewHandle, RenderContext, Task, View, ViewContext, ViewHandle,
}; };
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
@ -221,7 +221,7 @@ impl PickerDelegate for ProjectSymbolsView {
fn render_match( fn render_match(
&self, &self,
ix: usize, ix: usize,
mouse_state: &MouseState, mouse_state: MouseState,
selected: bool, selected: bool,
cx: &AppContext, cx: &AppContext,
) -> ElementBox { ) -> ElementBox {

View file

@ -290,7 +290,7 @@ impl BufferSearchBar {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click(move |_, cx| cx.dispatch_action(ToggleSearchOption(search_option))) .on_click(move |_, _, cx| cx.dispatch_action(ToggleSearchOption(search_option)))
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.boxed() .boxed()
} }
@ -314,7 +314,7 @@ impl BufferSearchBar {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click(move |_, cx| match direction { .on_click(move |_, _, cx| match direction {
Direction::Prev => cx.dispatch_action(SelectPrevMatch), Direction::Prev => cx.dispatch_action(SelectPrevMatch),
Direction::Next => cx.dispatch_action(SelectNextMatch), Direction::Next => cx.dispatch_action(SelectNextMatch),
}) })

View file

@ -672,7 +672,7 @@ impl ProjectSearchBar {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click(move |_, cx| match direction { .on_click(move |_, _, cx| match direction {
Direction::Prev => cx.dispatch_action(SelectPrevMatch), Direction::Prev => cx.dispatch_action(SelectPrevMatch),
Direction::Next => cx.dispatch_action(SelectNextMatch), Direction::Next => cx.dispatch_action(SelectNextMatch),
}) })
@ -699,7 +699,7 @@ impl ProjectSearchBar {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click(move |_, cx| cx.dispatch_action(ToggleSearchOption(option))) .on_click(move |_, _, cx| cx.dispatch_action(ToggleSearchOption(option)))
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.boxed() .boxed()
} }

View file

@ -2,9 +2,9 @@ mod theme_registry;
use gpui::{ use gpui::{
color::Color, color::Color,
elements::{ContainerStyle, ImageStyle, LabelStyle, MouseState}, elements::{ContainerStyle, ImageStyle, LabelStyle},
fonts::{HighlightStyle, TextStyle}, fonts::{HighlightStyle, TextStyle},
Border, Border, MouseState,
}; };
use serde::{de::DeserializeOwned, Deserialize}; use serde::{de::DeserializeOwned, Deserialize};
use serde_json::Value; use serde_json::Value;
@ -488,7 +488,7 @@ pub struct Interactive<T> {
} }
impl<T> Interactive<T> { impl<T> Interactive<T> {
pub fn style_for(&self, state: &MouseState, active: bool) -> &T { pub fn style_for(&self, state: MouseState, active: bool) -> &T {
if active { if active {
if state.hovered { if state.hovered {
self.active_hover self.active_hover

View file

@ -1,6 +1,6 @@
use fuzzy::{match_strings, StringMatch, StringMatchCandidate}; use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
use gpui::{ use gpui::{
actions, elements::*, AppContext, Element, ElementBox, Entity, MutableAppContext, actions, elements::*, AppContext, Element, ElementBox, Entity, MouseState, MutableAppContext,
RenderContext, View, ViewContext, ViewHandle, RenderContext, View, ViewContext, ViewHandle,
}; };
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
@ -213,7 +213,7 @@ impl PickerDelegate for ThemeSelector {
fn render_match( fn render_match(
&self, &self,
ix: usize, ix: usize,
mouse_state: &MouseState, mouse_state: MouseState,
selected: bool, selected: bool,
cx: &AppContext, cx: &AppContext,
) -> ElementBox { ) -> ElementBox {

View file

@ -168,7 +168,8 @@ impl View for LspStatus {
self.failed.join(", "), self.failed.join(", "),
if self.failed.len() > 1 { "s" } else { "" } if self.failed.len() > 1 { "s" } else { "" }
); );
handler = Some(|_, cx: &mut EventContext| cx.dispatch_action(DismissErrorMessage)); handler =
Some(|_, _, cx: &mut EventContext| cx.dispatch_action(DismissErrorMessage));
} else { } else {
return Empty::new().boxed(); return Empty::new().boxed();
} }

View file

@ -788,7 +788,7 @@ impl Pane {
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click({ .on_click({
let pane = pane.clone(); let pane = pane.clone();
move |_, cx| { move |_, _, cx| {
cx.dispatch_action(CloseItem { cx.dispatch_action(CloseItem {
item_id, item_id,
pane: pane.clone(), pane: pane.clone(),

View file

@ -293,7 +293,7 @@ impl View for SidebarButtons {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| { .on_click(move |_, _, cx| {
cx.dispatch_action(ToggleSidebarItem { cx.dispatch_action(ToggleSidebarItem {
side, side,
item_index: ix, item_index: ix,

View file

@ -1730,7 +1730,7 @@ impl Workspace {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click(|_, cx| cx.dispatch_action(Authenticate)) .on_click(|_, _, cx| cx.dispatch_action(Authenticate))
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.aligned() .aligned()
.boxed(), .boxed(),
@ -1781,7 +1781,7 @@ impl Workspace {
if let Some(peer_id) = peer_id { if let Some(peer_id) = peer_id {
MouseEventHandler::new::<ToggleFollow, _, _>(replica_id.into(), cx, move |_, _| content) MouseEventHandler::new::<ToggleFollow, _, _>(replica_id.into(), cx, move |_, _| content)
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, cx| cx.dispatch_action(ToggleFollow(peer_id))) .on_click(move |_, _, cx| cx.dispatch_action(ToggleFollow(peer_id)))
.boxed() .boxed()
} else { } else {
content content