Rework presenter and MouseRegion to use Handler hashmap rather than individual fields

This commit is contained in:
K Simmons 2022-07-17 23:19:32 -07:00
parent 92bc1a6eef
commit 8add81350e
30 changed files with 616 additions and 421 deletions

View File

@ -3,7 +3,7 @@ use editor::Editor;
use futures::StreamExt; use futures::StreamExt;
use gpui::{ use gpui::{
actions, elements::*, platform::CursorStyle, Action, AppContext, Entity, ModelHandle, actions, elements::*, platform::CursorStyle, Action, AppContext, Entity, ModelHandle,
MutableAppContext, RenderContext, View, ViewContext, ViewHandle, MouseButton, MutableAppContext, RenderContext, View, ViewContext, ViewHandle,
}; };
use language::{LanguageRegistry, LanguageServerBinaryStatus}; use language::{LanguageRegistry, LanguageServerBinaryStatus};
use project::{LanguageServerProgress, Project}; use project::{LanguageServerProgress, Project};
@ -317,7 +317,9 @@ impl View for ActivityIndicator {
if let Some(action) = action { if let Some(action) = action {
element = element element = element
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| cx.dispatch_any_action(action.boxed_clone())); .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_any_action(action.boxed_clone())
});
} }
element.boxed() element.boxed()

View File

@ -2,7 +2,7 @@ use crate::ViewReleaseNotes;
use gpui::{ use gpui::{
elements::{Flex, MouseEventHandler, Padding, ParentElement, Svg, Text}, elements::{Flex, MouseEventHandler, Padding, ParentElement, Svg, Text},
platform::{AppVersion, CursorStyle}, platform::{AppVersion, CursorStyle},
Element, Entity, View, ViewContext, Element, Entity, MouseButton, View, ViewContext,
}; };
use menu::Cancel; use menu::Cancel;
use settings::Settings; use settings::Settings;
@ -62,7 +62,7 @@ impl View for UpdateNotification {
.boxed() .boxed()
}) })
.with_padding(Padding::uniform(5.)) .with_padding(Padding::uniform(5.))
.on_click(move |_, _, cx| cx.dispatch_action(Cancel)) .on_click(MouseButton::Left, move |_, cx| cx.dispatch_action(Cancel))
.aligned() .aligned()
.constrained() .constrained()
.with_height(cx.font_cache().line_height(theme.message.text.font_size)) .with_height(cx.font_cache().line_height(theme.message.text.font_size))
@ -84,7 +84,9 @@ impl View for UpdateNotification {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(|_, _, cx| cx.dispatch_action(ViewReleaseNotes)) .on_click(MouseButton::Left, |_, cx| {
cx.dispatch_action(ViewReleaseNotes)
})
.boxed() .boxed()
} }
} }

View File

@ -8,8 +8,8 @@ use gpui::{
elements::*, elements::*,
platform::CursorStyle, platform::CursorStyle,
views::{ItemType, Select, SelectStyle}, views::{ItemType, Select, SelectStyle},
AppContext, Entity, ModelHandle, MutableAppContext, RenderContext, Subscription, Task, View, AppContext, Entity, ModelHandle, MouseButton, MutableAppContext, RenderContext, Subscription,
ViewContext, ViewHandle, Task, View, ViewContext, ViewHandle,
}; };
use menu::Confirm; use menu::Confirm;
use postage::prelude::Stream; use postage::prelude::Stream;
@ -320,7 +320,7 @@ impl ChatPanel {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, 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

@ -13,8 +13,9 @@ use gpui::{
geometry::{rect::RectF, vector::vec2f}, geometry::{rect::RectF, vector::vec2f},
impl_actions, impl_internal_actions, impl_actions, impl_internal_actions,
platform::CursorStyle, platform::CursorStyle,
AppContext, ClipboardItem, Element, ElementBox, Entity, ModelHandle, MutableAppContext, AppContext, ClipboardItem, Element, ElementBox, Entity, ModelHandle, MouseButton,
RenderContext, Subscription, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle, MutableAppContext, RenderContext, Subscription, View, ViewContext, ViewHandle, WeakModelHandle,
WeakViewHandle,
}; };
use join_project_notification::JoinProjectNotification; use join_project_notification::JoinProjectNotification;
use menu::{Confirm, SelectNext, SelectPrev}; use menu::{Confirm, SelectNext, SelectPrev};
@ -310,7 +311,9 @@ 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(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ToggleExpanded(section))
})
.boxed() .boxed()
} }
@ -445,7 +448,7 @@ impl ContactsPanel {
Some( Some(
button button
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ToggleProjectOnline { cx.dispatch_action(ToggleProjectOnline {
project: Some(open_project.clone()), project: Some(open_project.clone()),
}) })
@ -499,7 +502,7 @@ impl ContactsPanel {
} else { } else {
CursorStyle::Arrow CursorStyle::Arrow
}) })
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, move |_, cx| {
if !is_host { if !is_host {
cx.dispatch_global_action(JoinProject { cx.dispatch_global_action(JoinProject {
contact: contact.clone(), contact: contact.clone(),
@ -563,7 +566,7 @@ impl ContactsPanel {
} else { } else {
button button
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, move |_, cx| {
let project = project_handle.upgrade(cx.deref_mut()); let project = project_handle.upgrade(cx.deref_mut());
cx.dispatch_action(ToggleProjectOnline { project }) cx.dispatch_action(ToggleProjectOnline { project })
}) })
@ -646,7 +649,7 @@ impl ContactsPanel {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(RespondToContactRequest { cx.dispatch_action(RespondToContactRequest {
user_id, user_id,
accept: false, accept: false,
@ -668,7 +671,7 @@ impl ContactsPanel {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(RespondToContactRequest { cx.dispatch_action(RespondToContactRequest {
user_id, user_id,
accept: true, accept: true,
@ -691,7 +694,9 @@ 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(MouseButton::Left, move |_, cx| {
cx.dispatch_action(RemoveContact(user_id))
})
.flex_float() .flex_float()
.boxed(), .boxed(),
); );
@ -1078,7 +1083,9 @@ 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(MouseButton::Left, |_, cx| {
cx.dispatch_action(contact_finder::Toggle)
})
.boxed(), .boxed(),
) )
.constrained() .constrained()
@ -1126,7 +1133,7 @@ impl View for ContactsPanel {
}, },
) )
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, move |_, cx| {
cx.write_to_clipboard(ClipboardItem::new( cx.write_to_clipboard(ClipboardItem::new(
info.url.to_string(), info.url.to_string(),
)); ));

View File

@ -3,7 +3,7 @@ use client::User;
use gpui::{ use gpui::{
elements::{Flex, Image, Label, MouseEventHandler, Padding, ParentElement, Text}, elements::{Flex, Image, Label, MouseEventHandler, Padding, ParentElement, Text},
platform::CursorStyle, platform::CursorStyle,
Action, Element, ElementBox, RenderContext, View, Action, Element, ElementBox, MouseButton, RenderContext, View,
}; };
use settings::Settings; use settings::Settings;
use std::sync::Arc; use std::sync::Arc;
@ -61,7 +61,9 @@ 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(MouseButton::Left, move |_, cx| {
cx.dispatch_any_action(dismiss_action.boxed_clone())
})
.aligned() .aligned()
.constrained() .constrained()
.with_height( .with_height(
@ -96,7 +98,9 @@ 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(MouseButton::Left, move |_, cx| {
cx.dispatch_any_action(action.boxed_clone())
})
.boxed() .boxed()
}, },
)) ))

View File

@ -1,7 +1,7 @@
use gpui::{ use gpui::{
elements::*, geometry::vector::Vector2F, impl_internal_actions, keymap, platform::CursorStyle, elements::*, geometry::vector::Vector2F, impl_internal_actions, keymap, platform::CursorStyle,
Action, AppContext, Axis, Entity, MutableAppContext, RenderContext, SizeConstraint, Action, AppContext, Axis, Entity, MouseButton, MutableAppContext, RenderContext,
Subscription, View, ViewContext, SizeConstraint, Subscription, View, ViewContext,
}; };
use menu::*; use menu::*;
use settings::Settings; use settings::Settings;
@ -337,7 +337,7 @@ impl ContextMenu {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(Clicked); cx.dispatch_action(Clicked);
cx.dispatch_any_action(action.boxed_clone()); cx.dispatch_any_action(action.boxed_clone());
}) })
@ -355,7 +355,7 @@ impl ContextMenu {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_mouse_down_out(|_, cx| cx.dispatch_action(Cancel)) .on_mouse_down_out(MouseButton::Left, |_, cx| cx.dispatch_action(Cancel))
.on_right_mouse_down_out(|_, cx| cx.dispatch_action(Cancel)) .on_mouse_down_out(MouseButton::Right, |_, cx| cx.dispatch_action(Cancel))
} }
} }

View File

@ -1,8 +1,8 @@
use collections::HashSet; use collections::HashSet;
use editor::{Editor, GoToNextDiagnostic}; use editor::{Editor, GoToNextDiagnostic};
use gpui::{ use gpui::{
elements::*, platform::CursorStyle, serde_json, Entity, ModelHandle, MutableAppContext, elements::*, platform::CursorStyle, serde_json, Entity, ModelHandle, MouseButton,
RenderContext, Subscription, View, ViewContext, ViewHandle, WeakViewHandle, MutableAppContext, RenderContext, Subscription, View, ViewContext, ViewHandle, WeakViewHandle,
}; };
use language::Diagnostic; use language::Diagnostic;
use project::Project; use project::Project;
@ -161,7 +161,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(MouseButton::Left, |_, cx| cx.dispatch_action(crate::Deploy))
.with_tooltip::<Summary, _>( .with_tooltip::<Summary, _>(
0, 0,
"Project Diagnostics".to_string(), "Project Diagnostics".to_string(),
@ -201,7 +201,9 @@ 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(MouseButton::Left, |_, cx| {
cx.dispatch_action(GoToNextDiagnostic)
})
.boxed(), .boxed(),
); );
} }

View File

@ -30,8 +30,8 @@ use gpui::{
impl_actions, impl_internal_actions, impl_actions, impl_internal_actions,
platform::CursorStyle, platform::CursorStyle,
text_layout, AppContext, AsyncAppContext, ClipboardItem, Element, ElementBox, Entity, text_layout, AppContext, AsyncAppContext, ClipboardItem, Element, ElementBox, Entity,
ModelHandle, MutableAppContext, RenderContext, Subscription, Task, View, ViewContext, ModelHandle, MouseButton, MutableAppContext, RenderContext, Subscription, Task, View,
ViewHandle, WeakViewHandle, ViewContext, ViewHandle, WeakViewHandle,
}; };
use highlight_matching_bracket::refresh_matching_bracket_highlights; use highlight_matching_bracket::refresh_matching_bracket_highlights;
use hover_popover::{hide_hover, HoverState}; use hover_popover::{hide_hover, HoverState};
@ -707,7 +707,7 @@ impl CompletionsMenu {
}, },
) )
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_mouse_down(move |_, cx| { .on_mouse_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ConfirmCompletion { cx.dispatch_action(ConfirmCompletion {
item_ix: Some(item_ix), item_ix: Some(item_ix),
}); });
@ -840,7 +840,7 @@ impl CodeActionsMenu {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_mouse_down(move |_, cx| { .on_mouse_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ConfirmCodeAction { cx.dispatch_action(ConfirmCodeAction {
item_ix: Some(item_ix), item_ix: Some(item_ix),
}); });
@ -2674,7 +2674,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(MouseButton::Left, |_, cx| {
cx.dispatch_action(ToggleCodeActions { cx.dispatch_action(ToggleCodeActions {
deployed_from_indicator: true, deployed_from_indicator: true,
}); });

View File

@ -25,7 +25,7 @@ use gpui::{
platform::CursorStyle, platform::CursorStyle,
text_layout::{self, Line, RunStyle, TextLayoutCache}, text_layout::{self, Line, RunStyle, TextLayoutCache},
AppContext, Axis, Border, CursorRegion, Element, ElementBox, Event, EventContext, KeyDownEvent, AppContext, Axis, Border, CursorRegion, Element, ElementBox, Event, EventContext, KeyDownEvent,
LayoutContext, ModifiersChangedEvent, MouseButton, MouseEvent, MouseMovedEvent, LayoutContext, ModifiersChangedEvent, MouseButton, MouseButtonEvent, MouseMovedEvent,
MutableAppContext, PaintContext, Quad, Scene, ScrollWheelEvent, SizeConstraint, ViewContext, MutableAppContext, PaintContext, Quad, Scene, ScrollWheelEvent, SizeConstraint, ViewContext,
WeakViewHandle, WeakViewHandle,
}; };
@ -968,7 +968,9 @@ impl EditorElement {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(move |_, _, cx| cx.dispatch_action(jump_action.clone())) .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(jump_action.clone())
})
.with_tooltip::<JumpIcon, _>( .with_tooltip::<JumpIcon, _>(
*key, *key,
"Jump to Buffer".to_string(), "Jump to Buffer".to_string(),
@ -1483,7 +1485,7 @@ impl Element for EditorElement {
} }
match event { match event {
Event::MouseDown(MouseEvent { Event::MouseDown(MouseButtonEvent {
button: MouseButton::Left, button: MouseButton::Left,
position, position,
cmd, cmd,
@ -1501,12 +1503,12 @@ impl Element for EditorElement {
paint, paint,
cx, cx,
), ),
Event::MouseDown(MouseEvent { Event::MouseDown(MouseButtonEvent {
button: MouseButton::Right, button: MouseButton::Right,
position, position,
.. ..
}) => self.mouse_right_down(*position, layout, paint, cx), }) => self.mouse_right_down(*position, layout, paint, cx),
Event::MouseUp(MouseEvent { Event::MouseUp(MouseButtonEvent {
button: MouseButton::Left, button: MouseButton::Left,
position, position,
.. ..

View File

@ -5401,7 +5401,7 @@ impl RefCounts {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{actions, elements::*, impl_actions, MouseButton, MouseEvent}; use crate::{actions, elements::*, impl_actions, MouseButton, MouseButtonEvent};
use serde::Deserialize; use serde::Deserialize;
use smol::future::poll_once; use smol::future::poll_once;
use std::{ use std::{
@ -5754,7 +5754,7 @@ mod tests {
let presenter = cx.presenters_and_platform_windows[&window_id].0.clone(); let presenter = cx.presenters_and_platform_windows[&window_id].0.clone();
// Ensure window's root element is in a valid lifecycle state. // Ensure window's root element is in a valid lifecycle state.
presenter.borrow_mut().dispatch_event( presenter.borrow_mut().dispatch_event(
Event::MouseDown(MouseEvent { Event::MouseDown(MouseButtonEvent {
position: Default::default(), position: Default::default(),
button: MouseButton::Left, button: MouseButton::Left,
ctrl: false, ctrl: false,

View File

@ -1,11 +1,11 @@
use crate::{ use crate::{
geometry::vector::Vector2F, CursorRegion, DebugContext, Element, ElementBox, Event, geometry::vector::Vector2F, CursorRegion, DebugContext, Element, ElementBox, Event,
EventContext, LayoutContext, MouseButton, MouseEvent, MouseRegion, NavigationDirection, EventContext, LayoutContext, MouseButton, MouseButtonEvent, MouseRegion, NavigationDirection,
PaintContext, SizeConstraint, PaintContext, SizeConstraint,
}; };
use pathfinder_geometry::rect::RectF; use pathfinder_geometry::rect::RectF;
use serde_json::json; use serde_json::json;
use std::{any::TypeId, rc::Rc}; use std::any::TypeId;
pub struct EventHandler { pub struct EventHandler {
child: ElementBox, child: ElementBox,
@ -82,19 +82,11 @@ impl Element for EventHandler {
bounds: visible_bounds, bounds: visible_bounds,
style: Default::default(), style: Default::default(),
}); });
cx.scene.push_mouse_region(MouseRegion { cx.scene.push_mouse_region(MouseRegion::handle_all(
view_id: cx.current_view_id(), cx.current_view_id(),
discriminant: Some(discriminant), Some(discriminant),
bounds: visible_bounds, visible_bounds,
hover: Some(Rc::new(|_, _, _| {})), ));
mouse_down: Some(Rc::new(|_, _| {})),
click: Some(Rc::new(|_, _, _| {})),
right_mouse_down: Some(Rc::new(|_, _| {})),
right_click: Some(Rc::new(|_, _, _| {})),
drag: Some(Rc::new(|_, _, _| {})),
mouse_down_out: Some(Rc::new(|_, _| {})),
right_mouse_down_out: Some(Rc::new(|_, _| {})),
});
cx.scene.pop_stacking_context(); cx.scene.pop_stacking_context();
} }
self.child.paint(bounds.origin(), visible_bounds, cx); self.child.paint(bounds.origin(), visible_bounds, cx);
@ -117,7 +109,7 @@ impl Element for EventHandler {
true true
} else { } else {
match event { match event {
Event::MouseDown(MouseEvent { Event::MouseDown(MouseButtonEvent {
button: MouseButton::Left, button: MouseButton::Left,
position, position,
.. ..
@ -129,7 +121,7 @@ impl Element for EventHandler {
} }
false false
} }
Event::MouseDown(MouseEvent { Event::MouseDown(MouseButtonEvent {
button: MouseButton::Right, button: MouseButton::Right,
position, position,
.. ..
@ -141,7 +133,7 @@ impl Element for EventHandler {
} }
false false
} }
Event::MouseDown(MouseEvent { Event::MouseDown(MouseButtonEvent {
button: MouseButton::Navigate(direction), button: MouseButton::Navigate(direction),
position, position,
.. ..

View File

@ -1,4 +1,4 @@
use std::{any::TypeId, rc::Rc}; use std::any::TypeId;
use super::Padding; use super::Padding;
use crate::{ use crate::{
@ -8,24 +8,16 @@ use crate::{
}, },
platform::CursorStyle, platform::CursorStyle,
scene::CursorRegion, scene::CursorRegion,
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseRegion, MouseState, DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseButton,
PaintContext, RenderContext, SizeConstraint, View, MouseButtonEvent, MouseMovedEvent, MouseRegion, MouseState, PaintContext, RenderContext,
SizeConstraint, View,
}; };
use serde_json::json; use serde_json::json;
pub struct MouseEventHandler { pub struct MouseEventHandler {
child: ElementBox, child: ElementBox,
tag: TypeId,
id: usize,
cursor_style: Option<CursorStyle>, cursor_style: Option<CursorStyle>,
mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, region: MouseRegion,
click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
right_mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
right_click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
right_mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
drag: Option<Rc<dyn Fn(Vector2F, Vector2F, &mut EventContext)>>,
hover: Option<Rc<dyn Fn(Vector2F, bool, &mut EventContext)>>,
padding: Padding, padding: Padding,
} }
@ -37,18 +29,9 @@ impl MouseEventHandler {
F: FnOnce(MouseState, &mut RenderContext<V>) -> ElementBox, F: FnOnce(MouseState, &mut RenderContext<V>) -> ElementBox,
{ {
Self { Self {
id,
tag: TypeId::of::<Tag>(),
child: render_child(cx.mouse_state::<Tag>(id), cx), child: render_child(cx.mouse_state::<Tag>(id), cx),
cursor_style: None, cursor_style: None,
mouse_down: None, region: MouseRegion::new(0, Some((TypeId::of::<Tag>(), id)), Default::default()),
click: None,
right_mouse_down: None,
right_click: None,
mouse_down_out: None,
right_mouse_down_out: None,
drag: None,
hover: None,
padding: Default::default(), padding: Default::default(),
} }
} }
@ -60,65 +43,45 @@ impl MouseEventHandler {
pub fn on_mouse_down( pub fn on_mouse_down(
mut self, mut self,
handler: impl Fn(Vector2F, &mut EventContext) + 'static, button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.mouse_down = Some(Rc::new(handler)); self.region = self.region.on_down(button, handler);
self self
} }
pub fn on_click( pub fn on_click(
mut self, mut self,
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static, button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.click = Some(Rc::new(handler)); self.region = self.region.on_click(button, handler);
self
}
pub fn on_right_mouse_down(
mut self,
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
) -> Self {
self.right_mouse_down = Some(Rc::new(handler));
self
}
pub fn on_right_click(
mut self,
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
) -> Self {
self.right_click = Some(Rc::new(handler));
self self
} }
pub fn on_mouse_down_out( pub fn on_mouse_down_out(
mut self, mut self,
handler: impl Fn(Vector2F, &mut EventContext) + 'static, button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.mouse_down_out = Some(Rc::new(handler)); self.region = self.region.on_down_out(button, handler);
self
}
pub fn on_right_mouse_down_out(
mut self,
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
) -> Self {
self.right_mouse_down_out = Some(Rc::new(handler));
self self
} }
pub fn on_drag( pub fn on_drag(
mut self, mut self,
handler: impl Fn(Vector2F, Vector2F, &mut EventContext) + 'static, button: MouseButton,
handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.drag = Some(Rc::new(handler)); self.region = self.region.on_drag(button, handler);
self self
} }
pub fn on_hover( pub fn on_hover(
mut self, mut self,
handler: impl Fn(Vector2F, bool, &mut EventContext) + 'static, handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
) -> Self { ) -> Self {
self.hover = Some(Rc::new(handler)); self.region = self.region.on_hover(handler);
self self
} }
@ -163,19 +126,9 @@ impl Element for MouseEventHandler {
}); });
} }
cx.scene.push_mouse_region(MouseRegion { self.region.view_id = cx.current_view_id();
view_id: cx.current_view_id(), self.region.bounds = hit_bounds;
discriminant: Some((self.tag, self.id)), cx.scene.push_mouse_region(self.region.clone());
bounds: hit_bounds,
hover: self.hover.clone(),
click: self.click.clone(),
mouse_down: self.mouse_down.clone(),
right_click: self.right_click.clone(),
right_mouse_down: self.right_mouse_down.clone(),
mouse_down_out: self.mouse_down_out.clone(),
right_mouse_down_out: self.right_mouse_down_out.clone(),
drag: self.drag.clone(),
});
self.child.paint(bounds.origin(), visible_bounds, cx); self.child.paint(bounds.origin(), visible_bounds, cx);
} }

View File

@ -6,8 +6,8 @@ use crate::{
fonts::TextStyle, fonts::TextStyle,
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json::json, json::json,
Action, Axis, ElementStateHandle, LayoutContext, PaintContext, RenderContext, SizeConstraint, Action, Axis, ElementStateHandle, LayoutContext, MouseMovedEvent, PaintContext, RenderContext,
Task, View, SizeConstraint, Task, View,
}; };
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
@ -91,7 +91,7 @@ impl Tooltip {
}; };
let child = let child =
MouseEventHandler::new::<MouseEventHandlerState<Tag>, _, _>(id, cx, |_, _| child) MouseEventHandler::new::<MouseEventHandlerState<Tag>, _, _>(id, cx, |_, _| child)
.on_hover(move |position, hover, cx| { .on_hover(move |hover, MouseMovedEvent { position, .. }, cx| {
let window_id = cx.window_id(); let window_id = cx.window_id();
if let Some(view_id) = cx.view_id() { if let Some(view_id) = cx.view_id() {
if hover { if hover {

View File

@ -21,20 +21,26 @@ pub struct ModifiersChangedEvent {
pub cmd: bool, pub cmd: bool,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Default)]
pub struct ScrollWheelEvent { pub struct ScrollWheelEvent {
pub position: Vector2F, pub position: Vector2F,
pub delta: Vector2F, pub delta: Vector2F,
pub precise: bool, pub precise: bool,
} }
#[derive(Copy, Clone, Debug)] #[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
pub enum NavigationDirection { pub enum NavigationDirection {
Back, Back,
Forward, Forward,
} }
#[derive(Copy, Clone, Debug)] impl Default for NavigationDirection {
fn default() -> Self {
Self::Back
}
}
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
pub enum MouseButton { pub enum MouseButton {
Left, Left,
Right, Right,
@ -42,8 +48,26 @@ pub enum MouseButton {
Navigate(NavigationDirection), Navigate(NavigationDirection),
} }
#[derive(Clone, Debug)] impl MouseButton {
pub struct MouseEvent { pub fn all() -> Vec<Self> {
vec![
MouseButton::Left,
MouseButton::Right,
MouseButton::Middle,
MouseButton::Navigate(NavigationDirection::Back),
MouseButton::Navigate(NavigationDirection::Forward),
]
}
}
impl Default for MouseButton {
fn default() -> Self {
Self::Left
}
}
#[derive(Clone, Debug, Default)]
pub struct MouseButtonEvent {
pub button: MouseButton, pub button: MouseButton,
pub position: Vector2F, pub position: Vector2F,
pub ctrl: bool, pub ctrl: bool,
@ -53,7 +77,7 @@ pub struct MouseEvent {
pub click_count: usize, pub click_count: usize,
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, Default)]
pub struct MouseMovedEvent { pub struct MouseMovedEvent {
pub position: Vector2F, pub position: Vector2F,
pub pressed_button: Option<MouseButton>, pub pressed_button: Option<MouseButton>,
@ -68,8 +92,8 @@ pub enum Event {
KeyDown(KeyDownEvent), KeyDown(KeyDownEvent),
KeyUp(KeyUpEvent), KeyUp(KeyUpEvent),
ModifiersChanged(ModifiersChangedEvent), ModifiersChanged(ModifiersChangedEvent),
MouseDown(MouseEvent), MouseDown(MouseButtonEvent),
MouseUp(MouseEvent), MouseUp(MouseButtonEvent),
MouseMoved(MouseMovedEvent), MouseMoved(MouseMovedEvent),
ScrollWheel(ScrollWheelEvent), ScrollWheel(ScrollWheelEvent),
} }

View File

@ -2,8 +2,8 @@ use crate::{
geometry::vector::vec2f, geometry::vector::vec2f,
keymap::Keystroke, keymap::Keystroke,
platform::{Event, NavigationDirection}, platform::{Event, NavigationDirection},
KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, MouseButton, MouseEvent, MouseMovedEvent, KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, MouseButton, MouseButtonEvent,
ScrollWheelEvent, MouseMovedEvent, ScrollWheelEvent,
}; };
use cocoa::{ use cocoa::{
appkit::{NSEvent, NSEventModifierFlags, NSEventType}, appkit::{NSEvent, NSEventModifierFlags, NSEventType},
@ -126,7 +126,7 @@ impl Event {
let modifiers = native_event.modifierFlags(); let modifiers = native_event.modifierFlags();
window_height.map(|window_height| { window_height.map(|window_height| {
Self::MouseDown(MouseEvent { Self::MouseDown(MouseButtonEvent {
button, button,
position: vec2f( position: vec2f(
native_event.locationInWindow().x as f32, native_event.locationInWindow().x as f32,
@ -155,7 +155,7 @@ impl Event {
window_height.map(|window_height| { window_height.map(|window_height| {
let modifiers = native_event.modifierFlags(); let modifiers = native_event.modifierFlags();
Self::MouseUp(MouseEvent { Self::MouseUp(MouseButtonEvent {
button, button,
position: vec2f( position: vec2f(
native_event.locationInWindow().x as f32, native_event.locationInWindow().x as f32,

View File

@ -6,7 +6,7 @@ use crate::{
}, },
keymap::Keystroke, keymap::Keystroke,
platform::{self, Event, WindowBounds, WindowContext}, platform::{self, Event, WindowBounds, WindowContext},
KeyDownEvent, ModifiersChangedEvent, MouseButton, MouseEvent, MouseMovedEvent, Scene, KeyDownEvent, ModifiersChangedEvent, MouseButton, MouseButtonEvent, MouseMovedEvent, Scene,
}; };
use block::ConcreteBlock; use block::ConcreteBlock;
use cocoa::{ use cocoa::{
@ -635,7 +635,7 @@ extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) {
)) ))
.detach(); .detach();
} }
Event::MouseUp(MouseEvent { Event::MouseUp(MouseButtonEvent {
button: MouseButton::Left, button: MouseButton::Left,
.. ..
}) => { }) => {

View File

@ -6,10 +6,10 @@ use crate::{
json::{self, ToJson}, json::{self, ToJson},
keymap::Keystroke, keymap::Keystroke,
platform::{CursorStyle, Event}, platform::{CursorStyle, Event},
scene::CursorRegion, scene::{CursorRegion, MouseRegionEvent},
text_layout::TextLayoutCache, text_layout::TextLayoutCache,
Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AssetCache, ElementBox, Entity, Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AssetCache, ElementBox, Entity,
FontSystem, ModelHandle, MouseButton, MouseEvent, MouseMovedEvent, MouseRegion, MouseRegionId, FontSystem, ModelHandle, MouseButtonEvent, MouseMovedEvent, MouseRegion, MouseRegionId,
ReadModel, ReadView, RenderContext, RenderParams, Scene, UpgradeModelHandle, UpgradeViewHandle, ReadModel, ReadView, RenderContext, RenderParams, Scene, UpgradeModelHandle, UpgradeViewHandle,
View, ViewHandle, WeakModelHandle, WeakViewHandle, View, ViewHandle, WeakModelHandle, WeakViewHandle,
}; };
@ -230,105 +230,58 @@ impl Presenter {
let mut mouse_down_out_handlers = Vec::new(); let mut mouse_down_out_handlers = Vec::new();
let mut mouse_down_region = None; let mut mouse_down_region = None;
let mut clicked_region = None; let mut clicked_region = None;
let mut right_mouse_down_region = None;
let mut right_clicked_region = None;
let mut dragged_region = None; let mut dragged_region = None;
match event { match &event {
Event::MouseDown(MouseEvent { Event::MouseDown(
position, e @ MouseButtonEvent {
button: MouseButton::Left, position, button, ..
.. },
}) => { ) => {
let mut hit = false; let mut hit = false;
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) {
if !hit { if !hit {
hit = true; hit = true;
invalidated_views.push(region.view_id); invalidated_views.push(region.view_id);
mouse_down_region = Some((region.clone(), position)); mouse_down_region =
Some((region.clone(), MouseRegionEvent::Down(e.clone())));
self.clicked_region = Some(region.clone()); self.clicked_region = Some(region.clone());
self.prev_drag_position = Some(position); self.prev_drag_position = Some(*position);
} }
} else if let Some(handler) = region.mouse_down_out.clone() { } else if let Some(handler) = region
mouse_down_out_handlers.push((handler, region.view_id, position)); .handlers
.get(&(MouseRegionEvent::down_out_disc(), Some(*button)))
.cloned()
{
mouse_down_out_handlers.push((
handler,
region.view_id,
MouseRegionEvent::DownOut(e.clone()),
));
} }
} }
} }
Event::MouseUp(MouseEvent { Event::MouseUp(e @ MouseButtonEvent { position, .. }) => {
position,
click_count,
button: MouseButton::Left,
..
}) => {
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); 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, MouseRegionEvent::Click(e.clone())));
} }
} }
} }
Event::MouseDown(MouseEvent { Event::MouseMoved(e @ MouseMovedEvent { position, .. }) => {
position,
button: MouseButton::Right,
..
}) => {
let mut hit = false;
for (region, _) in self.mouse_regions.iter().rev() {
if region.bounds.contains_point(position) {
if !hit {
hit = true;
invalidated_views.push(region.view_id);
right_mouse_down_region = Some((region.clone(), position));
self.right_clicked_region = Some(region.clone());
}
} else if let Some(handler) = region.right_mouse_down_out.clone() {
mouse_down_out_handlers.push((handler, region.view_id, position));
}
}
}
Event::MouseUp(MouseEvent {
position,
click_count,
button: MouseButton::Right,
..
}) => {
if let Some(region) = self.right_clicked_region.take() {
invalidated_views.push(region.view_id);
if region.bounds.contains_point(position) {
right_clicked_region = Some((region, position, click_count));
}
}
}
Event::MouseMoved(MouseMovedEvent {
pressed_button,
position,
shift,
ctrl,
alt,
cmd,
..
}) => {
if let Some(MouseButton::Left) = pressed_button {
if let Some((clicked_region, prev_drag_position)) = self if let Some((clicked_region, prev_drag_position)) = self
.clicked_region .clicked_region
.as_ref() .as_ref()
.zip(self.prev_drag_position.as_mut()) .zip(self.prev_drag_position.as_mut())
{ {
dragged_region = dragged_region = Some((
Some((clicked_region.clone(), *prev_drag_position, position)); clicked_region.clone(),
*prev_drag_position = position; MouseRegionEvent::Drag(*prev_drag_position, e.clone()),
} ));
*prev_drag_position = *position;
self.last_mouse_moved_event = Some(Event::MouseMoved(MouseMovedEvent {
position,
pressed_button: Some(MouseButton::Left),
shift,
ctrl,
alt,
cmd,
}));
} }
self.last_mouse_moved_event = Some(event.clone()); self.last_mouse_moved_event = Some(event.clone());
@ -339,51 +292,39 @@ impl Presenter {
let (mut handled, mut event_cx) = let (mut handled, mut event_cx) =
self.handle_hover_events(&event, &mut invalidated_views, cx); self.handle_hover_events(&event, &mut invalidated_views, cx);
for (handler, view_id, position) in mouse_down_out_handlers { for (handler, view_id, region_event) in mouse_down_out_handlers {
event_cx.with_current_view(view_id, |event_cx| handler(position, event_cx)) event_cx.with_current_view(view_id, |event_cx| handler(region_event, event_cx))
} }
if let Some((mouse_down_region, position)) = mouse_down_region { if let Some((mouse_down_region, region_event)) = mouse_down_region {
handled = true; handled = true;
if let Some(mouse_down_callback) = mouse_down_region.mouse_down { if let Some(mouse_down_callback) =
mouse_down_region.handlers.get(&region_event.handler_key())
{
event_cx.with_current_view(mouse_down_region.view_id, |event_cx| { event_cx.with_current_view(mouse_down_region.view_id, |event_cx| {
mouse_down_callback(position, event_cx); mouse_down_callback(region_event, event_cx);
}) })
} }
} }
if let Some((clicked_region, position, click_count)) = clicked_region { if let Some((clicked_region, region_event)) = clicked_region {
handled = true; handled = true;
if let Some(click_callback) = clicked_region.click { if let Some(click_callback) =
clicked_region.handlers.get(&region_event.handler_key())
{
event_cx.with_current_view(clicked_region.view_id, |event_cx| { event_cx.with_current_view(clicked_region.view_id, |event_cx| {
click_callback(position, click_count, event_cx); click_callback(region_event, event_cx);
}) })
} }
} }
if let Some((right_mouse_down_region, position)) = right_mouse_down_region { if let Some((dragged_region, region_event)) = dragged_region {
handled = true; handled = true;
if let Some(right_mouse_down_callback) = right_mouse_down_region.right_mouse_down { if let Some(drag_callback) =
event_cx.with_current_view(right_mouse_down_region.view_id, |event_cx| { dragged_region.handlers.get(&region_event.handler_key())
right_mouse_down_callback(position, event_cx); {
})
}
}
if let Some((right_clicked_region, position, click_count)) = right_clicked_region {
handled = true;
if let Some(right_click_callback) = right_clicked_region.right_click {
event_cx.with_current_view(right_clicked_region.view_id, |event_cx| {
right_click_callback(position, click_count, event_cx);
})
}
}
if let Some((dragged_region, prev_position, position)) = dragged_region {
handled = true;
if let Some(drag_callback) = dragged_region.drag {
event_cx.with_current_view(dragged_region.view_id, |event_cx| { event_cx.with_current_view(dragged_region.view_id, |event_cx| {
drag_callback(prev_position, position, event_cx); drag_callback(region_event, event_cx);
}) })
} }
} }
@ -420,14 +361,17 @@ impl Presenter {
invalidated_views: &mut Vec<usize>, invalidated_views: &mut Vec<usize>,
cx: &'a mut MutableAppContext, cx: &'a mut MutableAppContext,
) -> (bool, EventContext<'a>) { ) -> (bool, EventContext<'a>) {
let mut unhovered_regions = Vec::new(); let mut hover_regions = Vec::new();
let mut hovered_regions = Vec::new(); // let mut unhovered_regions = Vec::new();
// let mut hovered_regions = Vec::new();
if let Event::MouseMoved(MouseMovedEvent { if let Event::MouseMoved(
e @ MouseMovedEvent {
position, position,
pressed_button, pressed_button,
.. ..
}) = event },
) = event
{ {
if let None = pressed_button { if let None = pressed_button {
let mut style_to_assign = CursorStyle::Arrow; let mut style_to_assign = CursorStyle::Arrow;
@ -448,7 +392,10 @@ impl Presenter {
if let Some(region_id) = region.id() { if let Some(region_id) = region.id() {
if !self.hovered_region_ids.contains(&region_id) { if !self.hovered_region_ids.contains(&region_id) {
invalidated_views.push(region.view_id); invalidated_views.push(region.view_id);
hovered_regions.push((region.clone(), position)); hover_regions.push((
region.clone(),
MouseRegionEvent::Hover(true, e.clone()),
));
self.hovered_region_ids.insert(region_id); self.hovered_region_ids.insert(region_id);
} }
} }
@ -456,7 +403,10 @@ impl Presenter {
if let Some(region_id) = region.id() { if let Some(region_id) = region.id() {
if self.hovered_region_ids.contains(&region_id) { if self.hovered_region_ids.contains(&region_id) {
invalidated_views.push(region.view_id); invalidated_views.push(region.view_id);
unhovered_regions.push((region.clone(), position)); hover_regions.push((
region.clone(),
MouseRegionEvent::Hover(false, e.clone()),
));
self.hovered_region_ids.remove(&region_id); self.hovered_region_ids.remove(&region_id);
} }
} }
@ -468,20 +418,11 @@ impl Presenter {
let mut event_cx = self.build_event_context(cx); let mut event_cx = self.build_event_context(cx);
let mut handled = false; let mut handled = false;
for (unhovered_region, position) in unhovered_regions { for (hover_region, region_event) in hover_regions {
handled = true; handled = true;
if let Some(hover_callback) = unhovered_region.hover { if let Some(hover_callback) = hover_region.handlers.get(&region_event.handler_key()) {
event_cx.with_current_view(unhovered_region.view_id, |event_cx| { event_cx.with_current_view(hover_region.view_id, |event_cx| {
hover_callback(*position, false, event_cx); hover_callback(region_event, event_cx);
})
}
}
for (hovered_region, position) in hovered_regions {
handled = true;
if let Some(hover_callback) = hovered_region.hover {
event_cx.with_current_view(hovered_region.view_id, |event_cx| {
hover_callback(*position, true, event_cx);
}) })
} }
} }

View File

@ -1,6 +1,7 @@
use collections::HashMap;
use serde::Deserialize; use serde::Deserialize;
use serde_json::json; use serde_json::json;
use std::{any::TypeId, borrow::Cow, rc::Rc, sync::Arc}; use std::{any::TypeId, borrow::Cow, mem::Discriminant, rc::Rc, sync::Arc};
use crate::{ use crate::{
color::Color, color::Color,
@ -8,7 +9,7 @@ use crate::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
json::ToJson, json::ToJson,
platform::CursorStyle, platform::CursorStyle,
EventContext, ImageData, MouseEvent, MouseMovedEvent, ScrollWheelEvent, EventContext, ImageData, MouseButton, MouseButtonEvent, MouseMovedEvent, ScrollWheelEvent,
}; };
pub struct Scene { pub struct Scene {
@ -44,30 +45,247 @@ pub struct CursorRegion {
pub style: CursorStyle, pub style: CursorStyle,
} }
#[derive(Debug)]
pub enum MouseRegionEvent { pub enum MouseRegionEvent {
Moved(MouseMovedEvent), Move(MouseMovedEvent),
Hover(MouseEvent), Drag(Vector2F, MouseMovedEvent),
Down(MouseEvent), Hover(bool, MouseMovedEvent),
Up(MouseEvent), Down(MouseButtonEvent),
Click(MouseEvent), Up(MouseButtonEvent),
DownOut(MouseEvent), Click(MouseButtonEvent),
DownOut(MouseButtonEvent),
ScrollWheel(ScrollWheelEvent), ScrollWheel(ScrollWheelEvent),
} }
impl MouseRegionEvent {
pub fn move_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Move(Default::default()))
}
pub fn drag_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Drag(
Default::default(),
Default::default(),
))
}
pub fn hover_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Hover(
Default::default(),
Default::default(),
))
}
pub fn down_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Down(Default::default()))
}
pub fn up_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Up(Default::default()))
}
pub fn click_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::Click(Default::default()))
}
pub fn down_out_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::DownOut(Default::default()))
}
pub fn scroll_wheel_disc() -> Discriminant<MouseRegionEvent> {
std::mem::discriminant(&MouseRegionEvent::ScrollWheel(Default::default()))
}
pub fn handler_key(&self) -> (Discriminant<MouseRegionEvent>, Option<MouseButton>) {
match self {
MouseRegionEvent::Move(_) => (Self::move_disc(), None),
MouseRegionEvent::Drag(_, MouseMovedEvent { pressed_button, .. }) => {
(Self::drag_disc(), *pressed_button)
}
MouseRegionEvent::Hover(_, _) => (Self::hover_disc(), None),
MouseRegionEvent::Down(MouseButtonEvent { button, .. }) => {
(Self::down_disc(), Some(*button))
}
MouseRegionEvent::Up(MouseButtonEvent { button, .. }) => {
(Self::up_disc(), Some(*button))
}
MouseRegionEvent::Click(MouseButtonEvent { button, .. }) => {
(Self::click_disc(), Some(*button))
}
MouseRegionEvent::DownOut(MouseButtonEvent { button, .. }) => {
(Self::down_out_disc(), Some(*button))
}
MouseRegionEvent::ScrollWheel(_) => (Self::scroll_wheel_disc(), None),
}
}
}
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct MouseRegion { pub struct MouseRegion {
pub view_id: usize, pub view_id: usize,
pub discriminant: Option<(TypeId, usize)>, pub discriminant: Option<(TypeId, usize)>,
pub bounds: RectF, pub bounds: RectF,
pub handlers: HashMap<
(Discriminant<MouseRegionEvent>, Option<MouseButton>),
Rc<dyn Fn(MouseRegionEvent, &mut EventContext)>,
>,
}
pub hover: Option<Rc<dyn Fn(Vector2F, bool, &mut EventContext)>>, impl MouseRegion {
pub mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, pub fn new(view_id: usize, discriminant: Option<(TypeId, usize)>, bounds: RectF) -> Self {
pub click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>, Self {
pub right_mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, view_id,
pub right_click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>, discriminant,
pub drag: Option<Rc<dyn Fn(Vector2F, Vector2F, &mut EventContext)>>, bounds,
pub mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, handlers: Default::default(),
pub right_mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>, }
}
pub fn handle_all(
view_id: usize,
discriminant: Option<(TypeId, usize)>,
bounds: RectF,
) -> Self {
let mut handlers: HashMap<
(Discriminant<MouseRegionEvent>, Option<MouseButton>),
Rc<dyn Fn(MouseRegionEvent, &mut EventContext)>,
> = Default::default();
handlers.insert((MouseRegionEvent::move_disc(), None), Rc::new(|_, _| {}));
handlers.insert((MouseRegionEvent::hover_disc(), None), Rc::new(|_, _| {}));
for button in MouseButton::all() {
handlers.insert(
(MouseRegionEvent::drag_disc(), Some(button)),
Rc::new(|_, _| {}),
);
handlers.insert(
(MouseRegionEvent::down_disc(), Some(button)),
Rc::new(|_, _| {}),
);
handlers.insert(
(MouseRegionEvent::up_disc(), Some(button)),
Rc::new(|_, _| {}),
);
handlers.insert(
(MouseRegionEvent::click_disc(), Some(button)),
Rc::new(|_, _| {}),
);
handlers.insert(
(MouseRegionEvent::down_out_disc(), Some(button)),
Rc::new(|_, _| {}),
);
}
handlers.insert(
(MouseRegionEvent::scroll_wheel_disc(), None),
Rc::new(|_, _| {}),
);
Self {
view_id,
discriminant,
bounds,
handlers,
}
}
pub fn on_down(
mut self,
button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers.insert((MouseRegionEvent::down_disc(), Some(button)),
Rc::new(move |region_event, cx| {
if let MouseRegionEvent::Down(mouse_button_event) = region_event {
handler(mouse_button_event, cx);
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Down, found {:?}",
region_event);
}
}));
self
}
pub fn on_up(
mut self,
button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers.insert((MouseRegionEvent::up_disc(), Some(button)),
Rc::new(move |region_event, cx| {
if let MouseRegionEvent::Up(mouse_button_event) = region_event {
handler(mouse_button_event, cx);
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Up, found {:?}",
region_event);
}
}));
self
}
pub fn on_click(
mut self,
button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers.insert((MouseRegionEvent::click_disc(), Some(button)),
Rc::new(move |region_event, cx| {
if let MouseRegionEvent::Click(mouse_button_event) = region_event {
handler(mouse_button_event, cx);
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Click, found {:?}",
region_event);
}
}));
self
}
pub fn on_down_out(
mut self,
button: MouseButton,
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers.insert((MouseRegionEvent::down_out_disc(), Some(button)),
Rc::new(move |region_event, cx| {
if let MouseRegionEvent::DownOut(mouse_button_event) = region_event {
handler(mouse_button_event, cx);
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::DownOut, found {:?}",
region_event);
}
}));
self
}
pub fn on_drag(
mut self,
button: MouseButton,
handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers.insert((MouseRegionEvent::drag_disc(), Some(button)),
Rc::new(move |region_event, cx| {
if let MouseRegionEvent::Drag(prev_drag_position, mouse_moved_event) = region_event {
handler(prev_drag_position, mouse_moved_event, cx);
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Drag, found {:?}",
region_event);
}
}));
self
}
pub fn on_hover(
mut self,
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
) -> Self {
self.handlers.insert((MouseRegionEvent::hover_disc(), None),
Rc::new(move |region_event, cx| {
if let MouseRegionEvent::Hover(hover, mouse_moved_event) = region_event {
handler(hover, mouse_moved_event, cx);
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Hover, found {:?}",
region_event);
}
}));
self
}
} }
#[derive(Copy, Clone, Eq, PartialEq, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Hash)]

View File

@ -1,8 +1,8 @@
use serde::Deserialize; use serde::Deserialize;
use crate::{ use crate::{
actions, elements::*, impl_actions, AppContext, Entity, MutableAppContext, RenderContext, View, actions, elements::*, impl_actions, AppContext, Entity, MouseButton, MutableAppContext,
ViewContext, WeakViewHandle, RenderContext, View, ViewContext, WeakViewHandle,
}; };
pub struct Select { pub struct Select {
@ -119,7 +119,9 @@ impl View for Select {
.with_style(style.header) .with_style(style.header)
.boxed() .boxed()
}) })
.on_click(move |_, _, cx| cx.dispatch_action(ToggleSelect)) .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ToggleSelect)
})
.boxed(), .boxed(),
); );
if self.is_open { if self.is_open {
@ -151,7 +153,7 @@ impl View for Select {
) )
}, },
) )
.on_click(move |_, _, cx| { .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(SelectItem(ix)) cx.dispatch_action(SelectItem(ix))
}) })
.boxed() .boxed()

View File

@ -7,8 +7,8 @@ use gpui::{
geometry::vector::{vec2f, Vector2F}, geometry::vector::{vec2f, Vector2F},
keymap, keymap,
platform::CursorStyle, platform::CursorStyle,
AppContext, Axis, Element, ElementBox, Entity, MouseState, MutableAppContext, RenderContext, AppContext, Axis, Element, ElementBox, Entity, MouseButton, MouseState, MutableAppContext,
Task, View, ViewContext, ViewHandle, WeakViewHandle, RenderContext, Task, View, ViewContext, ViewHandle, WeakViewHandle,
}; };
use menu::{Cancel, Confirm, SelectFirst, SelectIndex, SelectLast, SelectNext, SelectPrev}; use menu::{Cancel, Confirm, SelectFirst, SelectIndex, SelectLast, SelectNext, SelectPrev};
use settings::Settings; use settings::Settings;
@ -90,7 +90,9 @@ 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(MouseButton::Left, move |_, cx| {
cx.dispatch_action(SelectIndex(ix))
})
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.boxed() .boxed()
})); }));

View File

@ -11,8 +11,9 @@ use gpui::{
geometry::vector::Vector2F, geometry::vector::Vector2F,
impl_internal_actions, keymap, impl_internal_actions, keymap,
platform::CursorStyle, platform::CursorStyle,
AppContext, ClipboardItem, Element, ElementBox, Entity, ModelHandle, MutableAppContext, AppContext, ClipboardItem, Element, ElementBox, Entity, ModelHandle, MouseButton,
PromptLevel, RenderContext, Task, View, ViewContext, ViewHandle, MouseButtonEvent, MutableAppContext, PromptLevel, RenderContext, Task, View, ViewContext,
ViewHandle,
}; };
use menu::{Confirm, SelectNext, SelectPrev}; use menu::{Confirm, SelectNext, SelectPrev};
use project::{Entry, EntryKind, Project, ProjectEntryId, ProjectPath, Worktree, WorktreeId}; use project::{Entry, EntryKind, Project, ProjectEntryId, ProjectPath, Worktree, WorktreeId};
@ -1054,7 +1055,9 @@ impl ProjectPanel {
.with_padding_left(padding) .with_padding_left(padding)
.boxed() .boxed()
}) })
.on_click(move |_, click_count, cx| { .on_click(
MouseButton::Left,
move |MouseButtonEvent { 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 {
@ -1063,10 +1066,14 @@ impl ProjectPanel {
change_focus: click_count > 1, change_focus: click_count > 1,
}) })
} }
}) },
.on_right_mouse_down(move |position, cx| { )
.on_mouse_down(
MouseButton::Right,
move |MouseButtonEvent { position, .. }, cx| {
cx.dispatch_action(DeployContextMenu { entry_id, position }) cx.dispatch_action(DeployContextMenu { entry_id, position })
}) },
)
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.boxed() .boxed()
} }
@ -1113,13 +1120,16 @@ impl View for ProjectPanel {
.expanded() .expanded()
.boxed() .boxed()
}) })
.on_right_mouse_down(move |position, cx| { .on_mouse_down(
MouseButton::Right,
move |MouseButtonEvent { position, .. }, cx| {
// When deploying the context menu anywhere below the last project entry, // When deploying the context menu anywhere below the last project entry,
// act as if the user clicked the root of the last worktree. // act as if the user clicked the root of the last worktree.
if let Some(entry_id) = last_worktree_root_id { if let Some(entry_id) = last_worktree_root_id {
cx.dispatch_action(DeployContextMenu { entry_id, position }) cx.dispatch_action(DeployContextMenu { entry_id, position })
} }
}) },
)
.boxed(), .boxed(),
) )
.with_child(ChildView::new(&self.context_menu).boxed()) .with_child(ChildView::new(&self.context_menu).boxed())

View File

@ -7,8 +7,8 @@ use collections::HashMap;
use editor::{Anchor, Autoscroll, Editor}; use editor::{Anchor, Autoscroll, Editor};
use gpui::{ use gpui::{
actions, elements::*, impl_actions, platform::CursorStyle, Action, AppContext, Entity, actions, elements::*, impl_actions, platform::CursorStyle, Action, AppContext, Entity,
MutableAppContext, RenderContext, Subscription, Task, View, ViewContext, ViewHandle, MouseButton, MutableAppContext, RenderContext, Subscription, Task, View, ViewContext,
WeakViewHandle, ViewHandle, WeakViewHandle,
}; };
use language::OffsetRangeExt; use language::OffsetRangeExt;
use project::search::SearchQuery; use project::search::SearchQuery;
@ -285,7 +285,9 @@ impl BufferSearchBar {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click(move |_, _, cx| cx.dispatch_any_action(option.to_toggle_action())) .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_any_action(option.to_toggle_action())
})
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_tooltip::<Self, _>( .with_tooltip::<Self, _>(
option as usize, option as usize,
@ -330,9 +332,9 @@ impl BufferSearchBar {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click({ .on_click(MouseButton::Left, {
let action = action.boxed_clone(); let action = action.boxed_clone();
move |_, _, cx| cx.dispatch_any_action(action.boxed_clone()) move |_, cx| cx.dispatch_any_action(action.boxed_clone())
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_tooltip::<NavButton, _>( .with_tooltip::<NavButton, _>(

View File

@ -7,8 +7,8 @@ use collections::HashMap;
use editor::{Anchor, Autoscroll, Editor, MultiBuffer, SelectAll, MAX_TAB_TITLE_LEN}; use editor::{Anchor, Autoscroll, Editor, MultiBuffer, SelectAll, MAX_TAB_TITLE_LEN};
use gpui::{ use gpui::{
actions, elements::*, platform::CursorStyle, Action, AppContext, ElementBox, Entity, actions, elements::*, platform::CursorStyle, Action, AppContext, ElementBox, Entity,
ModelContext, ModelHandle, MutableAppContext, RenderContext, Subscription, Task, View, ModelContext, ModelHandle, MouseButton, MutableAppContext, RenderContext, Subscription, Task,
ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle,
}; };
use menu::Confirm; use menu::Confirm;
use project::{search::SearchQuery, Project}; use project::{search::SearchQuery, Project};
@ -735,9 +735,9 @@ impl ProjectSearchBar {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click({ .on_click(MouseButton::Left, {
let action = action.boxed_clone(); let action = action.boxed_clone();
move |_, _, cx| cx.dispatch_any_action(action.boxed_clone()) move |_, cx| cx.dispatch_any_action(action.boxed_clone())
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_tooltip::<NavButton, _>( .with_tooltip::<NavButton, _>(
@ -770,7 +770,9 @@ impl ProjectSearchBar {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click(move |_, _, cx| cx.dispatch_any_action(option.to_toggle_action())) .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_any_action(option.to_toggle_action())
})
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_tooltip::<Self, _>( .with_tooltip::<Self, _>(
option as usize, option as usize,

View File

@ -20,8 +20,8 @@ use gpui::{
}, },
json::json, json::json,
text_layout::{Line, RunStyle}, text_layout::{Line, RunStyle},
Event, FontCache, KeyDownEvent, MouseRegion, PaintContext, Quad, ScrollWheelEvent, Event, FontCache, KeyDownEvent, MouseButton, MouseButtonEvent, MouseMovedEvent, MouseRegion,
SizeConstraint, TextLayoutCache, WeakModelHandle, PaintContext, Quad, ScrollWheelEvent, SizeConstraint, TextLayoutCache, WeakModelHandle,
}; };
use itertools::Itertools; use itertools::Itertools;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
@ -29,7 +29,7 @@ use settings::Settings;
use theme::TerminalStyle; use theme::TerminalStyle;
use util::ResultExt; use util::ResultExt;
use std::{cmp::min, ops::Range, rc::Rc, sync::Arc}; use std::{cmp::min, ops::Range, sync::Arc};
use std::{fmt::Debug, ops::Sub}; use std::{fmt::Debug, ops::Sub};
use crate::{color_translation::convert_color, connection::TerminalConnection, ZedListener}; use crate::{color_translation::convert_color, connection::TerminalConnection, ZedListener};
@ -594,23 +594,34 @@ fn attach_mouse_handlers(
let drag_mutex = terminal_mutex.clone(); let drag_mutex = terminal_mutex.clone();
let mouse_down_mutex = terminal_mutex.clone(); let mouse_down_mutex = terminal_mutex.clone();
cx.scene.push_mouse_region(MouseRegion { cx.scene.push_mouse_region(
view_id, MouseRegion::new(view_id, None, visible_bounds)
mouse_down: Some(Rc::new(move |pos, _| { .on_down(
MouseButton::Left,
move |MouseButtonEvent { position, .. }, _| {
let mut term = mouse_down_mutex.lock(); let mut term = mouse_down_mutex.lock();
let (point, side) = mouse_to_cell_data( let (point, side) = mouse_to_cell_data(
pos, position,
origin, origin,
cur_size, cur_size,
term.renderable_content().display_offset, term.renderable_content().display_offset,
); );
term.selection = Some(Selection::new(SelectionType::Simple, point, side)) term.selection = Some(Selection::new(SelectionType::Simple, point, side))
})), },
click: Some(Rc::new(move |pos, click_count, cx| { )
.on_click(
MouseButton::Left,
move |MouseButtonEvent {
position,
click_count,
..
},
cx| {
let mut term = click_mutex.lock(); let mut term = click_mutex.lock();
let (point, side) = mouse_to_cell_data( let (point, side) = mouse_to_cell_data(
pos, position,
origin, origin,
cur_size, cur_size,
term.renderable_content().display_offset, term.renderable_content().display_offset,
@ -624,19 +635,21 @@ fn attach_mouse_handlers(
_ => None, _ => None,
}; };
let selection = let selection = selection_type
selection_type.map(|selection_type| Selection::new(selection_type, point, side)); .map(|selection_type| Selection::new(selection_type, point, side));
term.selection = selection; term.selection = selection;
cx.focus_parent_view(); cx.focus_parent_view();
cx.notify(); cx.notify();
})), },
bounds: visible_bounds, )
drag: Some(Rc::new(move |_delta, pos, cx| { .on_drag(
MouseButton::Left,
move |_, MouseMovedEvent { position, .. }, cx| {
let mut term = drag_mutex.lock(); let mut term = drag_mutex.lock();
let (point, side) = mouse_to_cell_data( let (point, side) = mouse_to_cell_data(
pos, position,
origin, origin,
cur_size, cur_size,
term.renderable_content().display_offset, term.renderable_content().display_offset,
@ -648,9 +661,9 @@ fn attach_mouse_handlers(
} }
cx.notify(); cx.notify();
})), },
..Default::default() ),
}); );
} }
///Copied (with modifications) from alacritty/src/input.rs > Processor::cell_side() ///Copied (with modifications) from alacritty/src/input.rs > Processor::cell_side()

View File

@ -13,8 +13,9 @@ use gpui::{
}, },
impl_actions, impl_internal_actions, impl_actions, impl_internal_actions,
platform::{CursorStyle, NavigationDirection}, platform::{CursorStyle, NavigationDirection},
AppContext, AsyncAppContext, Entity, ModelHandle, MutableAppContext, PromptLevel, Quad, AppContext, AsyncAppContext, Entity, ModelHandle, MouseButton, MouseButtonEvent,
RenderContext, Task, View, ViewContext, ViewHandle, WeakViewHandle, MutableAppContext, PromptLevel, Quad, RenderContext, Task, View, ViewContext, ViewHandle,
WeakViewHandle,
}; };
use project::{Project, ProjectEntryId, ProjectPath}; use project::{Project, ProjectEntryId, ProjectPath};
use serde::Deserialize; use serde::Deserialize;
@ -955,9 +956,9 @@ impl Pane {
) )
.with_padding(Padding::uniform(4.)) .with_padding(Padding::uniform(4.))
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click({ .on_click(MouseButton::Left, {
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(),
@ -978,7 +979,7 @@ impl Pane {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_mouse_down(move |_, cx| { .on_mouse_down(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ActivateItem(ix)); cx.dispatch_action(ActivateItem(ix));
}) })
.boxed() .boxed()
@ -1079,9 +1080,12 @@ impl View for Pane {
}, },
) )
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_mouse_down(|position, cx| { .on_mouse_down(
MouseButton::Left,
|MouseButtonEvent { position, .. }, cx| {
cx.dispatch_action(DeploySplitMenu { position }); cx.dispatch_action(DeploySplitMenu { position });
}) },
)
.boxed(), .boxed(),
) )
.constrained() .constrained()

View File

@ -1,7 +1,7 @@
use crate::StatusItemView; use crate::StatusItemView;
use gpui::{ use gpui::{
elements::*, impl_actions, platform::CursorStyle, AnyViewHandle, AppContext, Entity, elements::*, impl_actions, platform::CursorStyle, AnyViewHandle, AppContext, Entity,
RenderContext, Subscription, View, ViewContext, ViewHandle, MouseButton, MouseMovedEvent, RenderContext, Subscription, View, ViewContext, ViewHandle,
}; };
use serde::Deserialize; use serde::Deserialize;
use settings::Settings; use settings::Settings;
@ -187,8 +187,15 @@ impl Sidebar {
..Default::default() ..Default::default()
}) })
.with_cursor_style(CursorStyle::ResizeLeftRight) .with_cursor_style(CursorStyle::ResizeLeftRight)
.on_mouse_down(|_, _| {}) // This prevents the mouse down event from being propagated elsewhere .on_mouse_down(MouseButton::Left, |_, _| {}) // This prevents the mouse down event from being propagated elsewhere
.on_drag(move |old_position, new_position, cx| { .on_drag(
MouseButton::Left,
move |old_position,
MouseMovedEvent {
position: new_position,
..
},
cx| {
let delta = new_position.x() - old_position.x(); let delta = new_position.x() - old_position.x();
let prev_width = *actual_width.borrow(); let prev_width = *actual_width.borrow();
*custom_width.borrow_mut() = 0f32 *custom_width.borrow_mut() = 0f32
@ -199,7 +206,8 @@ impl Sidebar {
.round(); .round();
cx.notify(); cx.notify();
}) },
)
.boxed() .boxed()
} }
} }
@ -314,9 +322,9 @@ impl View for SidebarButtons {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click({ .on_click(MouseButton::Left, {
let action = action.clone(); let action = action.clone();
move |_, _, cx| cx.dispatch_action(action.clone()) move |_, cx| cx.dispatch_action(action.clone())
}) })
.with_tooltip::<Self, _>( .with_tooltip::<Self, _>(
ix, ix,

View File

@ -1,7 +1,7 @@
use crate::{ItemHandle, Pane}; use crate::{ItemHandle, Pane};
use gpui::{ use gpui::{
elements::*, platform::CursorStyle, Action, AnyViewHandle, AppContext, ElementBox, Entity, elements::*, platform::CursorStyle, Action, AnyViewHandle, AppContext, ElementBox, Entity,
MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle, MouseButton, MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle,
}; };
use settings::Settings; use settings::Settings;
@ -191,7 +191,9 @@ fn nav_button<A: Action + Clone>(
} else { } else {
CursorStyle::default() CursorStyle::default()
}) })
.on_click(move |_, _, cx| cx.dispatch_action(action.clone())) .on_click(MouseButton::Left, move |_, cx| {
cx.dispatch_action(action.clone())
})
.with_tooltip::<A, _>( .with_tooltip::<A, _>(
0, 0,
action_name.to_string(), action_name.to_string(),

View File

@ -21,8 +21,8 @@ use gpui::{
json::{self, ToJson}, json::{self, ToJson},
platform::{CursorStyle, WindowOptions}, platform::{CursorStyle, WindowOptions},
AnyModelHandle, AnyViewHandle, AppContext, AsyncAppContext, Border, Entity, ImageData, AnyModelHandle, AnyViewHandle, AppContext, AsyncAppContext, Border, Entity, ImageData,
ModelContext, ModelHandle, MutableAppContext, PathPromptOptions, PromptLevel, RenderContext, ModelContext, ModelHandle, MouseButton, MutableAppContext, PathPromptOptions, PromptLevel,
Task, View, ViewContext, ViewHandle, WeakViewHandle, RenderContext, Task, View, ViewContext, ViewHandle, WeakViewHandle,
}; };
use language::LanguageRegistry; use language::LanguageRegistry;
use log::error; use log::error;
@ -1980,7 +1980,7 @@ impl Workspace {
.with_style(style.container) .with_style(style.container)
.boxed() .boxed()
}) })
.on_click(|_, _, cx| cx.dispatch_action(Authenticate)) .on_click(MouseButton::Left, |_, cx| cx.dispatch_action(Authenticate))
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.aligned() .aligned()
.boxed(), .boxed(),
@ -2031,7 +2031,9 @@ impl Workspace {
if let Some((peer_id, peer_github_login)) = peer { if let Some((peer_id, peer_github_login)) = peer {
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(MouseButton::Left, move |_, cx| {
cx.dispatch_action(ToggleFollow(peer_id))
})
.with_tooltip::<ToggleFollow, _>( .with_tooltip::<ToggleFollow, _>(
peer_id.0 as usize, peer_id.0 as usize,
if is_followed { if is_followed {

View File

@ -2,7 +2,7 @@ use crate::OpenBrowser;
use gpui::{ use gpui::{
elements::{MouseEventHandler, Text}, elements::{MouseEventHandler, Text},
platform::CursorStyle, platform::CursorStyle,
Element, Entity, RenderContext, View, Element, Entity, MouseButton, RenderContext, View,
}; };
use settings::Settings; use settings::Settings;
use workspace::StatusItemView; use workspace::StatusItemView;
@ -32,7 +32,7 @@ impl View for FeedbackLink {
.boxed() .boxed()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(|_, _, cx| { .on_click(MouseButton::Left, |_, cx| {
cx.dispatch_action(OpenBrowser { cx.dispatch_action(OpenBrowser {
url: NEW_ISSUE_URL.into(), url: NEW_ISSUE_URL.into(),
}) })

View File

@ -5,6 +5,7 @@
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "styles",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {