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::{
div, prelude::FluentBuilder as _, AnyElement, ClickEvent, Div, ElementId, InteractiveElement,
IntoElement, MouseButton, MouseDownEvent, MouseMoveEvent, ParentElement, RenderOnce,
SharedString, Stateful, StatefulInteractiveElement as _, Styled, WindowContext,
IntoElement, MouseMoveEvent, ParentElement, RenderOnce, SharedString, Stateful,
StatefulInteractiveElement as _, Styled, WindowContext,
};
use smallvec::SmallVec;
@ -17,7 +17,6 @@ pub struct ListItem {
group_id: Option<SharedString>,
on_click: Option<Box<dyn Fn(&ClickEvent, &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>>,
children: SmallVec<[AnyElement; 2]>,
}
@ -30,7 +29,6 @@ impl ListItem {
selected: false,
confirmed: false,
on_click: None,
on_secondary_mouse_down: None,
on_mouse_enter: None,
check_icon: None,
suffix: None,
@ -83,14 +81,6 @@ impl ListItem {
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(
mut self,
handler: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static,
@ -147,14 +137,6 @@ impl RenderOnce for ListItem {
.when(!is_active && !self.disabled, |this| {
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
.when_some(self.on_mouse_enter, |this, on_mouse_enter| {
if !self.disabled {

View file

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