fc_ui/capabilities/controls/
search_input.rs1use crate::capabilities::controls::input::Input;
4use crate::capabilities::controls::input_state::{InputEvent, InputState};
5use crate::capabilities::foundation::theme::use_theme;
6use crate::capabilities::primitives::icon::Icon;
7use crate::capabilities::primitives::icon_source::IconSource;
8use gpui::{prelude::FluentBuilder as _, InteractiveElement, *};
9use std::rc::Rc;
10
11#[derive(Clone, Debug, PartialEq)]
12pub struct SearchFilter {
13 pub label: SharedString,
14 pub active: bool,
15 pub value: SharedString,
16}
17
18impl SearchFilter {
19 pub fn new(label: impl Into<SharedString>, value: impl Into<SharedString>) -> Self {
20 Self {
21 label: label.into(),
22 active: false,
23 value: value.into(),
24 }
25 }
26
27 pub fn active(mut self, active: bool) -> Self {
28 self.active = active;
29 self
30 }
31}
32
33pub struct SearchInputState {
34 input: Entity<InputState>,
35 filters: Vec<SearchFilter>,
36 case_sensitive: bool,
37 use_regex: bool,
38 loading: bool,
39 results_count: Option<usize>,
40 on_search: Option<Rc<dyn Fn(&str, &mut App)>>,
41 on_filter_toggle: Option<Rc<dyn Fn(usize, &mut App)>>,
42}
43
44impl SearchInputState {
45 pub fn new(cx: &mut Context<Self>) -> Self {
46 let input = cx.new(|cx| InputState::new(cx).placeholder("Search..."));
47
48 Self {
49 input,
50 filters: Vec::new(),
51 case_sensitive: false,
52 use_regex: false,
53 loading: false,
54 results_count: None,
55 on_search: None,
56 on_filter_toggle: None,
57 }
58 }
59
60 pub fn placeholder(
61 self,
62 placeholder: impl Into<SharedString>,
63 window: &mut Window,
64 cx: &mut Context<Self>,
65 ) -> Self {
66 self.input.update(cx, |input, cx| {
67 input.set_placeholder(placeholder, window, cx);
68 });
69 self
70 }
71
72 pub fn set_filters(&mut self, filters: Vec<SearchFilter>, cx: &mut Context<Self>) {
73 self.filters = filters;
74 cx.notify();
75 }
76
77 pub fn toggle_filter(&mut self, index: usize, cx: &mut Context<Self>) {
78 if let Some(filter) = self.filters.get_mut(index) {
79 filter.active = !filter.active;
80 if let Some(handler) = &self.on_filter_toggle {
81 handler(index, cx);
82 }
83 cx.notify();
84 }
85 }
86
87 pub fn set_case_sensitive(&mut self, case_sensitive: bool, cx: &mut Context<Self>) {
88 self.case_sensitive = case_sensitive;
89 cx.notify();
90 }
91
92 pub fn toggle_case_sensitive(&mut self, cx: &mut Context<Self>) {
93 self.case_sensitive = !self.case_sensitive;
94 cx.notify();
95 }
96
97 pub fn set_use_regex(&mut self, use_regex: bool, cx: &mut Context<Self>) {
98 self.use_regex = use_regex;
99 cx.notify();
100 }
101
102 pub fn toggle_use_regex(&mut self, cx: &mut Context<Self>) {
103 self.use_regex = !self.use_regex;
104 cx.notify();
105 }
106
107 pub fn set_loading(&mut self, loading: bool, cx: &mut Context<Self>) {
108 self.loading = loading;
109 cx.notify();
110 }
111
112 pub fn set_results_count(&mut self, count: Option<usize>, cx: &mut Context<Self>) {
113 self.results_count = count;
114 cx.notify();
115 }
116
117 pub fn clear(&mut self, window: &mut Window, cx: &mut Context<Self>) {
118 self.input.update(cx, |input, cx| {
119 input.set_value("", window, cx);
120 });
121 self.results_count = None;
122 cx.notify();
123 }
124
125 pub fn query(&self, cx: &App) -> String {
126 self.input.read(cx).content().to_string()
127 }
128
129 pub fn active_filters(&self) -> Vec<&SearchFilter> {
130 self.filters.iter().filter(|f| f.active).collect()
131 }
132
133 pub fn case_sensitive(&self) -> bool {
134 self.case_sensitive
135 }
136
137 pub fn use_regex(&self) -> bool {
138 self.use_regex
139 }
140}
141
142pub struct SearchInput {
143 state: Entity<SearchInputState>,
144 style: StyleRefinement,
145}
146
147impl SearchInput {
148 pub fn new(cx: &mut Context<Self>) -> Self {
149 let state = cx.new(SearchInputState::new);
150
151 let state_weak = state.downgrade();
152 let input_entity = state.read(cx).input.clone();
153 cx.subscribe(&input_entity, move |_this, _input, event, cx| {
154 if let InputEvent::Change = event {
155 let Some(state) = state_weak.upgrade() else {
156 return;
157 };
158 let query = state.read(cx).input.read(cx).content().to_string();
159 let handler = state.read(cx).on_search.clone();
160 if let Some(handler) = handler {
161 cx.defer(move |cx| {
162 handler(&query, cx);
163 });
164 }
165 }
166 })
167 .detach();
168
169 Self {
170 state,
171 style: StyleRefinement::default(),
172 }
173 }
174
175 pub fn placeholder(
176 self,
177 placeholder: impl Into<SharedString>,
178 window: &mut Window,
179 cx: &mut Context<Self>,
180 ) -> Self {
181 self.state.update(cx, |state, cx| {
182 state.input.update(cx, |input, cx| {
183 input.set_placeholder(placeholder, window, cx);
184 });
185 });
186 self
187 }
188
189 pub fn filters(self, filters: Vec<SearchFilter>, cx: &mut Context<Self>) -> Self {
190 self.state.update(cx, |state, cx| {
191 state.set_filters(filters, cx);
192 });
193 self
194 }
195
196 pub fn on_search<F>(self, handler: F, cx: &mut Context<Self>) -> Self
197 where
198 F: Fn(&str, &mut App) + 'static,
199 {
200 self.state.update(cx, |state, _cx| {
201 state.on_search = Some(Rc::new(handler));
202 });
203 self
204 }
205
206 pub fn on_filter_toggle<F>(self, handler: F, cx: &mut Context<Self>) -> Self
207 where
208 F: Fn(usize, &mut App) + 'static,
209 {
210 self.state.update(cx, |state, _cx| {
211 state.on_filter_toggle = Some(Rc::new(handler));
212 });
213 self
214 }
215
216 pub fn state(&self) -> &Entity<SearchInputState> {
217 &self.state
218 }
219}
220
221impl Styled for SearchInput {
222 fn style(&mut self) -> &mut StyleRefinement {
223 &mut self.style
224 }
225}
226
227impl Render for SearchInput {
228 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
229 let theme = use_theme(cx);
230 let state = self.state.read(cx);
231 let has_query = !state.input.read(cx).content().is_empty();
232 let user_style = self.style.clone();
233
234 div()
235 .flex()
236 .flex_col()
237 .gap(px(8.0))
238 .child(
240 div()
241 .flex()
242 .items_center()
243 .gap(px(8.0))
244 .px(px(12.0))
245 .py(px(8.0))
246 .bg(theme.tokens.input)
247 .border_1()
248 .border_color(theme.tokens.border)
249 .rounded(theme.tokens.radius_md)
250 .child(
251 Icon::new(if state.loading {
252 IconSource::Named("loader".into())
253 } else {
254 IconSource::Named("search".into())
255 })
256 .size(px(16.0))
257 .color(theme.tokens.muted_foreground),
258 )
259 .child(div().flex_1().child(Input::new(&state.input)))
260 .when_some(state.results_count, |parent_div, count| {
261 parent_div.child(
262 div()
263 .px(px(8.0))
264 .py(px(2.0))
265 .rounded(theme.tokens.radius_sm)
266 .bg(theme.tokens.muted)
267 .text_size(px(12.0))
268 .text_color(theme.tokens.muted_foreground)
269 .child(format!("{} results", count)),
270 )
271 })
272 .child(
273 div()
274 .px(px(8.0))
275 .py(px(4.0))
276 .rounded(theme.tokens.radius_sm)
277 .cursor(CursorStyle::PointingHand)
278 .when(state.case_sensitive, |div| div.bg(theme.tokens.accent))
279 .when(!state.case_sensitive, |div| {
280 div.hover(|style| style.bg(theme.tokens.muted))
281 })
282 .on_mouse_down(
283 MouseButton::Left,
284 cx.listener(|this, _event, _window, cx| {
285 this.state.update(cx, |state, cx| {
286 state.toggle_case_sensitive(cx);
287 });
288 }),
289 )
290 .child(
291 div()
292 .text_size(px(12.0))
293 .font_weight(FontWeight::SEMIBOLD)
294 .text_color(if state.case_sensitive {
295 theme.tokens.accent_foreground
296 } else {
297 theme.tokens.muted_foreground
298 })
299 .child("Aa"),
300 ),
301 )
302 .child(
303 div()
304 .px(px(8.0))
305 .py(px(4.0))
306 .rounded(theme.tokens.radius_sm)
307 .cursor(CursorStyle::PointingHand)
308 .when(state.use_regex, |div| div.bg(theme.tokens.accent))
309 .when(!state.use_regex, |div| {
310 div.hover(|style| style.bg(theme.tokens.muted))
311 })
312 .on_mouse_down(
313 MouseButton::Left,
314 cx.listener(|this, _event, _window, cx| {
315 this.state.update(cx, |state, cx| {
316 state.toggle_use_regex(cx);
317 });
318 }),
319 )
320 .child(
321 div()
322 .text_size(px(12.0))
323 .font_weight(FontWeight::SEMIBOLD)
324 .text_color(if state.use_regex {
325 theme.tokens.accent_foreground
326 } else {
327 theme.tokens.muted_foreground
328 })
329 .child(".*"),
330 ),
331 )
332 .when(has_query, |parent_div| {
333 parent_div.child(
334 div()
335 .p(px(4.0))
336 .rounded(theme.tokens.radius_sm)
337 .cursor(CursorStyle::PointingHand)
338 .hover(|style| style.bg(theme.tokens.muted))
339 .on_mouse_down(
340 MouseButton::Left,
341 cx.listener(|this, _event, window, cx| {
342 this.state.update(cx, |state, cx| {
343 state.clear(window, cx);
344 });
345 }),
346 )
347 .child(
348 Icon::new(IconSource::Named("x".into()))
349 .size(px(14.0))
350 .color(theme.tokens.muted_foreground),
351 ),
352 )
353 }),
354 )
355 .when(!state.filters.is_empty(), |parent_div| {
356 parent_div.child(
357 div()
358 .flex()
359 .items_center()
360 .gap(px(8.0))
361 .flex_wrap()
362 .children(state.filters.iter().enumerate().map(|(idx, filter)| {
363 let filter = filter.clone();
364 div()
365 .px(px(8.0))
366 .py(px(4.0))
367 .rounded(theme.tokens.radius_sm)
368 .border_1()
369 .border_color(if filter.active {
370 theme.tokens.accent
371 } else {
372 theme.tokens.border
373 })
374 .bg(if filter.active {
375 theme.tokens.accent.opacity(0.1)
376 } else {
377 theme.tokens.background
378 })
379 .cursor(CursorStyle::PointingHand)
380 .hover(|style| style.bg(theme.tokens.muted))
381 .on_mouse_down(
382 MouseButton::Left,
383 cx.listener(move |this, _event, _window, cx| {
384 this.state.update(cx, |state, cx| {
385 state.toggle_filter(idx, cx);
386 });
387 }),
388 )
389 .child(
390 div()
391 .text_size(px(12.0))
392 .text_color(if filter.active {
393 theme.tokens.accent
394 } else {
395 theme.tokens.foreground
396 })
397 .child(filter.label.clone()),
398 )
399 .into_any_element()
400 })),
401 )
402 })
403 .map(|this| {
404 let mut div = this;
405 div.style().refine(&user_style);
406 div
407 })
408 }
409}