From 0d4839b973c7b5f246156d40c7db57248e5cb8ba Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 28 Nov 2023 21:14:17 -0700 Subject: [PATCH] use the right click event for buttons --- crates/search2/src/search_bar.rs | 6 +++--- crates/ui2/src/components/button.rs | 15 +++++---------- crates/ui2/src/components/disclosure.rs | 4 ++-- crates/ui2/src/components/icon_button.rs | 18 +++++++----------- crates/ui2/src/components/list.rs | 4 ++-- 5 files changed, 19 insertions(+), 28 deletions(-) diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index 1a7456f41c..f5a9a8c8f7 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -1,4 +1,4 @@ -use gpui::{IntoElement, MouseDownEvent, WindowContext}; +use gpui::{ClickEvent, IntoElement, WindowContext}; use ui::{Button, ButtonVariant, IconButton}; use crate::mode::SearchMode; @@ -6,7 +6,7 @@ use crate::mode::SearchMode; pub(super) fn render_nav_button( icon: ui::Icon, _active: bool, - on_click: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, + on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static, ) -> impl IntoElement { // let tooltip_style = cx.theme().tooltip.clone(); // let cursor_style = if active { @@ -21,7 +21,7 @@ pub(super) fn render_nav_button( pub(crate) fn render_search_mode_button( mode: SearchMode, is_active: bool, - on_click: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, + on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static, ) -> Button { let button_variant = if is_active { ButtonVariant::Filled diff --git a/crates/ui2/src/components/button.rs b/crates/ui2/src/components/button.rs index 02902a4b64..fbe5b951fa 100644 --- a/crates/ui2/src/components/button.rs +++ b/crates/ui2/src/components/button.rs @@ -1,9 +1,7 @@ -use std::rc::Rc; - use gpui::{ - DefiniteLength, Div, Hsla, IntoElement, MouseButton, MouseDownEvent, - StatefulInteractiveElement, WindowContext, + ClickEvent, DefiniteLength, Div, Hsla, IntoElement, StatefulInteractiveElement, WindowContext, }; +use std::rc::Rc; use crate::prelude::*; use crate::{h_stack, Color, Icon, IconButton, IconElement, Label, LineHeightStyle}; @@ -67,7 +65,7 @@ impl ButtonVariant { #[derive(IntoElement)] pub struct Button { disabled: bool, - click_handler: Option>, + click_handler: Option>, icon: Option, icon_position: Option, label: SharedString, @@ -118,7 +116,7 @@ impl RenderOnce for Button { } if let Some(click_handler) = self.click_handler.clone() { - button = button.on_mouse_down(MouseButton::Left, move |event, cx| { + button = button.on_click(move |event, cx| { click_handler(event, cx); }); } @@ -168,10 +166,7 @@ impl Button { self } - pub fn on_click( - mut self, - handler: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, - ) -> Self { + pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self { self.click_handler = Some(Rc::new(handler)); self } diff --git a/crates/ui2/src/components/disclosure.rs b/crates/ui2/src/components/disclosure.rs index e0d7b1c519..6206a2edd8 100644 --- a/crates/ui2/src/components/disclosure.rs +++ b/crates/ui2/src/components/disclosure.rs @@ -1,12 +1,12 @@ use std::rc::Rc; -use gpui::{div, Element, IntoElement, MouseDownEvent, ParentElement, WindowContext}; +use gpui::{div, ClickEvent, Element, IntoElement, ParentElement, WindowContext}; use crate::{Color, Icon, IconButton, IconSize, Toggle}; pub fn disclosure_control( toggle: Toggle, - on_toggle: Option>, + on_toggle: Option>, ) -> impl Element { match (toggle.is_toggleable(), toggle.is_toggled()) { (false, _) => div(), diff --git a/crates/ui2/src/components/icon_button.rs b/crates/ui2/src/components/icon_button.rs index 2ccda3ea0e..cdaec6a770 100644 --- a/crates/ui2/src/components/icon_button.rs +++ b/crates/ui2/src/components/icon_button.rs @@ -1,5 +1,5 @@ use crate::{h_stack, prelude::*, Icon, IconElement, IconSize}; -use gpui::{prelude::*, Action, AnyView, Div, MouseButton, MouseDownEvent, Stateful}; +use gpui::{prelude::*, Action, AnyView, ClickEvent, Div, Stateful}; #[derive(IntoElement)] pub struct IconButton { @@ -11,7 +11,7 @@ pub struct IconButton { state: InteractionState, selected: bool, tooltip: Option AnyView + 'static>>, - on_mouse_down: Option>, + on_click: Option>, } impl RenderOnce for IconButton { @@ -57,9 +57,8 @@ impl RenderOnce for IconButton { .color(icon_color), ); - if let Some(click_handler) = self.on_mouse_down { - button = button.on_mouse_down(MouseButton::Left, move |event, cx| { - cx.stop_propagation(); + if let Some(click_handler) = self.on_click { + button = button.on_click(move |event, cx| { click_handler(event, cx); }) } @@ -85,7 +84,7 @@ impl IconButton { state: InteractionState::default(), selected: false, tooltip: None, - on_mouse_down: None, + on_click: None, } } @@ -124,11 +123,8 @@ impl IconButton { self } - pub fn on_click( - mut self, - handler: impl 'static + Fn(&MouseDownEvent, &mut WindowContext), - ) -> Self { - self.on_mouse_down = Some(Box::new(handler)); + pub fn on_click(mut self, handler: impl 'static + Fn(&ClickEvent, &mut WindowContext)) -> Self { + self.on_click = Some(Box::new(handler)); self } diff --git a/crates/ui2/src/components/list.rs b/crates/ui2/src/components/list.rs index f3e75f272d..aa61c8333e 100644 --- a/crates/ui2/src/components/list.rs +++ b/crates/ui2/src/components/list.rs @@ -177,7 +177,7 @@ pub struct ListItem { toggle: Toggle, inset: bool, on_click: Option>, - on_toggle: Option>, + on_toggle: Option>, on_secondary_mouse_down: Option>, children: SmallVec<[AnyElement; 2]>, } @@ -234,7 +234,7 @@ impl ListItem { pub fn on_toggle( mut self, - on_toggle: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, + on_toggle: impl Fn(&ClickEvent, &mut WindowContext) + 'static, ) -> Self { self.on_toggle = Some(Rc::new(on_toggle)); self