Improved id for Switch. (#167)

This commit is contained in:
Jason Lee 2024-08-19 15:53:38 +08:00 committed by GitHub
parent f05d2ebbd2
commit b3bee1db3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 26 deletions

View file

@ -1,7 +1,7 @@
use gpui::{ use gpui::{
div, prelude::FluentBuilder as _, AnyElement, ClickEvent, Div, ElementId, InteractiveElement, div, prelude::FluentBuilder as _, AnyElement, ClickEvent, Div, ElementId, InteractiveElement,
IntoElement, MouseButton, MouseDownEvent, MouseMoveEvent, ParentElement, RenderOnce, IntoElement, MouseMoveEvent, ParentElement, RenderOnce, SharedString, Stateful,
SharedString, Stateful, StatefulInteractiveElement as _, Styled, WindowContext, StatefulInteractiveElement as _, Styled, WindowContext,
}; };
use smallvec::SmallVec; use smallvec::SmallVec;
@ -17,7 +17,6 @@ pub struct ListItem {
group_id: Option<SharedString>, group_id: Option<SharedString>,
on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>, on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
on_mouse_enter: Option<Box<dyn Fn(&MouseMoveEvent, &mut WindowContext) + 'static>>, on_mouse_enter: Option<Box<dyn Fn(&MouseMoveEvent, &mut WindowContext) + 'static>>,
on_secondary_mouse_down: Option<Box<dyn Fn(&MouseDownEvent, &mut WindowContext) + 'static>>,
suffix: Option<Box<dyn Fn(&mut WindowContext) -> AnyElement + 'static>>, suffix: Option<Box<dyn Fn(&mut WindowContext) -> AnyElement + 'static>>,
children: SmallVec<[AnyElement; 2]>, children: SmallVec<[AnyElement; 2]>,
} }
@ -30,7 +29,6 @@ impl ListItem {
selected: false, selected: false,
confirmed: false, confirmed: false,
on_click: None, on_click: None,
on_secondary_mouse_down: None,
on_mouse_enter: None, on_mouse_enter: None,
check_icon: None, check_icon: None,
suffix: None, suffix: None,
@ -83,14 +81,6 @@ impl ListItem {
self self
} }
pub fn on_secondary_mouse_down(
mut self,
handler: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
) -> Self {
self.on_secondary_mouse_down = Some(Box::new(handler));
self
}
pub fn on_mouse_enter( pub fn on_mouse_enter(
mut self, mut self,
handler: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static, handler: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static,
@ -147,14 +137,6 @@ impl RenderOnce for ListItem {
.when(!is_active && !self.disabled, |this| { .when(!is_active && !self.disabled, |this| {
this.hover(|this| this.bg(cx.theme().list_hover)) this.hover(|this| this.bg(cx.theme().list_hover))
}) })
// Right click
.when_some(self.on_secondary_mouse_down, |this, on_mouse_down| {
if !self.disabled {
this.on_mouse_down(MouseButton::Right, move |ev, cx| (on_mouse_down)(ev, cx))
} else {
this
}
})
// Mouse enter // Mouse enter
.when_some(self.on_mouse_enter, |this, on_mouse_enter| { .when_some(self.on_mouse_enter, |this, on_mouse_enter| {
if !self.disabled { if !self.disabled {

View file

@ -26,7 +26,7 @@ impl LabelSide {
#[derive(IntoElement)] #[derive(IntoElement)]
pub struct Switch { pub struct Switch {
id: SharedString, id: ElementId,
base: Stateful<Div>, base: Stateful<Div>,
checked: bool, checked: bool,
disabled: bool, disabled: bool,
@ -37,9 +37,8 @@ pub struct Switch {
} }
impl Switch { impl Switch {
pub fn new(id: impl Into<SharedString>) -> Self { pub fn new(id: impl Into<ElementId>) -> Self {
let id: SharedString = id.into(); let id: ElementId = id.into();
Self { Self {
id: id.clone(), id: id.clone(),
base: div().id(id), base: div().id(id),
@ -90,7 +89,6 @@ impl Disableable for Switch {
impl RenderOnce for Switch { impl RenderOnce for Switch {
fn render(self, cx: &mut gpui::WindowContext) -> impl IntoElement { fn render(self, cx: &mut gpui::WindowContext) -> impl IntoElement {
let theme = cx.theme(); let theme = cx.theme();
let group_id = format!("switch_group_{:?}", self.id);
let checked = self.checked; let checked = self.checked;
let (bg, toggle_bg) = match self.checked { let (bg, toggle_bg) = match self.checked {
@ -115,7 +113,6 @@ impl RenderOnce for Switch {
h_flex() h_flex()
.id(self.id) .id(self.id)
.group(group_id)
.items_center() .items_center()
.gap_2() .gap_2()
.when(self.label_side.left(), |this| this.flex_row_reverse()) .when(self.label_side.left(), |this| this.flex_row_reverse())