gpui_component/searchable_list/
item.rs1use gpui::ColorExt as _;
2use gpui::{
3 AnyElement, App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce,
4 StyleRefinement, Styled, Window, prelude::FluentBuilder,
5};
6
7use crate::{
8 ActiveTheme, Disableable, Icon, IconName, Selectable, Sizable, Size, StyleSized, StyledExt,
9 h_flex,
10};
11
12#[derive(IntoElement)]
19pub struct SearchableListItemElement {
20 id: ElementId,
21 size: Size,
22 style: StyleRefinement,
23 selected: bool,
25 checked: bool,
27 disabled: bool,
28 children: Vec<AnyElement>,
29 check_icon: Option<Icon>,
31}
32
33impl SearchableListItemElement {
34 pub fn new(ix: usize) -> Self {
35 Self {
36 id: ("searchable-list-item", ix).into(),
37 size: Size::default(),
38 style: StyleRefinement::default(),
39 selected: false,
40 checked: false,
41 disabled: false,
42 children: Vec::new(),
43 check_icon: Some(Icon::new(IconName::Check)),
44 }
45 }
46
47 pub fn checked(mut self, checked: bool) -> Self {
49 self.checked = checked;
50 self
51 }
52
53 pub fn check_icon(mut self, icon: impl Into<Icon>) -> Self {
55 self.check_icon = Some(icon.into());
56 self
57 }
58}
59
60impl ParentElement for SearchableListItemElement {
61 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
62 self.children.extend(elements);
63 }
64}
65
66impl Disableable for SearchableListItemElement {
67 fn disabled(mut self, disabled: bool) -> Self {
68 self.disabled = disabled;
69 self
70 }
71}
72
73impl Selectable for SearchableListItemElement {
74 fn selected(mut self, selected: bool) -> Self {
75 self.selected = selected;
76 self
77 }
78
79 fn is_selected(&self) -> bool {
80 self.selected
81 }
82}
83
84impl Sizable for SearchableListItemElement {
85 fn with_size(mut self, size: impl Into<Size>) -> Self {
86 self.size = size.into();
87 self
88 }
89}
90
91impl Styled for SearchableListItemElement {
92 fn style(&mut self) -> &mut StyleRefinement {
93 &mut self.style
94 }
95}
96
97impl RenderOnce for SearchableListItemElement {
98 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
99 h_flex()
100 .id(self.id)
101 .relative()
102 .gap_x_1()
103 .py_1()
104 .px_2()
105 .rounded(cx.theme().radius)
106 .text_base()
107 .text_color(cx.theme().foreground)
108 .items_center()
109 .justify_between()
110 .input_text_size(self.size)
111 .list_size(self.size)
112 .refine_style(&self.style)
113 .when(!self.disabled, |this| {
114 this.when(!self.selected, |this| {
115 this.hover(|this| this.bg(cx.theme().accent.opacity(0.7)))
116 })
117 })
118 .when(self.selected, |this| this.bg(cx.theme().tokens.accent))
119 .when(self.disabled, |this| {
120 this.cursor_not_allowed()
121 .text_color(cx.theme().muted_foreground)
122 })
123 .child(
124 h_flex()
125 .w_full()
126 .items_center()
127 .justify_between()
128 .gap_x_1()
129 .child(h_flex().w_full().items_center().children(self.children))
130 .when_some(self.check_icon, |this, icon| {
131 this.child(
132 icon.xsmall()
133 .text_color(cx.theme().foreground)
134 .when(!self.checked, |this| this.invisible()),
135 )
136 }),
137 )
138 }
139}