From 93b578294c50c9fab31d77a659d7faf0d20221b3 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 28 Jul 2025 18:38:39 +0800 Subject: [PATCH] list: Update ListItem and DropdownListItem style. (#1095) ## Break Change - The `Selectable` trait has been removed `element_id` method, this is necessary. - The `item` of `List` now must impl `Selectable` trait. ## List > NOTE: The default ListItem is no rounded and paddings. image ## Dropdown image --- crates/story/src/drawer_story.rs | 4 - crates/story/src/list_story.rs | 34 +++-- crates/ui/src/button/button.rs | 13 +- crates/ui/src/button/dropdown_button.rs | 4 - crates/ui/src/checkbox.rs | 4 - crates/ui/src/dropdown.rs | 119 ++++++++++++++-- crates/ui/src/list/list.rs | 178 ++++++++++++------------ crates/ui/src/list/list_item.rs | 54 +++++-- crates/ui/src/menu/menu_item.rs | 4 - crates/ui/src/menu/popup_menu.rs | 6 +- crates/ui/src/popover.rs | 3 +- crates/ui/src/sidebar/footer.rs | 20 +-- crates/ui/src/sidebar/header.rs | 19 +-- crates/ui/src/styled.rs | 12 +- crates/ui/src/tab/tab.rs | 4 - 15 files changed, 304 insertions(+), 174 deletions(-) diff --git a/crates/story/src/drawer_story.rs b/crates/story/src/drawer_story.rs index ea9c2159..f758d664 100644 --- a/crates/story/src/drawer_story.rs +++ b/crates/story/src/drawer_story.rs @@ -71,15 +71,11 @@ impl ListDelegate for ListItemDeletegate { _: &mut Context>, ) -> Option { let confirmed = Some(ix) == self.confirmed_index; - let selected = Some(ix) == self.selected_index; if let Some(item) = self.matches.get(ix) { let list_item = ListItem::new(("item", ix)) .check_icon(IconName::Check) .confirmed(confirmed) - .selected(selected) - .py_1() - .px_3() .child( h_flex() .items_center() diff --git a/crates/story/src/list_story.rs b/crates/story/src/list_story.rs index e81f4e97..06c78511 100644 --- a/crates/story/src/list_story.rs +++ b/crates/story/src/list_story.rs @@ -2,9 +2,9 @@ use std::time::Duration; use fake::Fake; use gpui::{ - actions, div, px, App, AppContext, Context, ElementId, Entity, FocusHandle, Focusable, - InteractiveElement, IntoElement, ParentElement, Render, RenderOnce, SharedString, Styled, - Subscription, Task, Timer, Window, + actions, div, prelude::FluentBuilder as _, px, App, AppContext, Context, Edges, ElementId, + Entity, FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render, + RenderOnce, SharedString, Styled, Subscription, Task, Timer, Window, }; use gpui_component::{ @@ -13,7 +13,7 @@ use gpui_component::{ h_flex, label::Label, list::{List, ListDelegate, ListEvent, ListItem}, - v_flex, ActiveTheme, Sizable, + v_flex, ActiveTheme, Selectable, Sizable, }; actions!(list_story, [SelectedCompany]); @@ -65,6 +65,17 @@ impl CompanyListItem { } } +impl Selectable for CompanyListItem { + fn selected(mut self, selected: bool) -> Self { + self.selected = selected; + self + } + + fn is_selected(&self) -> bool { + self.selected + } +} + impl RenderOnce for CompanyListItem { fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { let text_color = if self.selected { @@ -88,10 +99,16 @@ impl RenderOnce for CompanyListItem { }; self.base - .px_3() + .px_2() .py_1() .overflow_x_hidden() .bg(bg_color) + .border_1() + .border_color(bg_color) + .when(self.selected, |this| { + this.border_color(cx.theme().list_active_border) + }) + .rounded(cx.theme().radius) .child( h_flex() .items_center() @@ -283,10 +300,9 @@ impl ListStory { eof: false, }; - let company_list = cx.new(|cx| List::new(delegate, window, cx)); - // company_list.update(cx, |list, cx| { - // list.set_selected_index(Some(3), cx); - // }); + let company_list = + cx.new(|cx| List::new(delegate, window, cx).paddings(Edges::all(px(8.)))); + let _subscriptions = vec![ cx.subscribe(&company_list, |_, _, ev: &ListEvent, _| match ev { diff --git a/crates/ui/src/button/button.rs b/crates/ui/src/button/button.rs index ab775589..c2b04d4f 100644 --- a/crates/ui/src/button/button.rs +++ b/crates/ui/src/button/button.rs @@ -7,7 +7,7 @@ use crate::{ use gpui::{ div, prelude::FluentBuilder as _, relative, Action, AnyElement, App, ClickEvent, Corners, Div, Edges, ElementId, Hsla, InteractiveElement, Interactivity, IntoElement, MouseButton, - ParentElement, Pixels, RenderOnce, SharedString, StatefulInteractiveElement as _, + ParentElement, Pixels, RenderOnce, SharedString, Stateful, StatefulInteractiveElement as _, StyleRefinement, Styled, Window, }; @@ -181,9 +181,8 @@ impl ButtonVariant { /// A Button element. #[derive(IntoElement)] pub struct Button { - base: Div, + base: Stateful
, style: StyleRefinement, - id: ElementId, icon: Option, label: Option, children: Vec, @@ -215,9 +214,8 @@ impl From