kael_ui 0.2.0

Professional shadcn-inspired UI component library for Kael. 100+ accessible components for building beautiful, performant desktop applications.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Search input component - Specialized search with filters and advanced capabilities.

use crate::{
    components::{
        icon::Icon,
        icon_source::IconSource,
        input::Input,
        input_state::{InputEvent, InputState},
    },
    theme::use_theme,
};
use kael::{prelude::FluentBuilder as _, InteractiveElement, *};
use std::rc::Rc;

#[derive(Clone, Debug, PartialEq)]
pub struct SearchFilter {
    pub label: SharedString,
    pub active: bool,
    pub value: SharedString,
}

impl SearchFilter {
    pub fn new(label: impl Into<SharedString>, value: impl Into<SharedString>) -> Self {
        Self {
            label: label.into(),
            active: false,
            value: value.into(),
        }
    }

    pub fn active(mut self, active: bool) -> Self {
        self.active = active;
        self
    }
}

pub struct SearchInputState {
    input: Entity<InputState>,
    filters: Vec<SearchFilter>,
    case_sensitive: bool,
    use_regex: bool,
    loading: bool,
    results_count: Option<usize>,
    on_search: Option<Rc<dyn Fn(&str, &mut App)>>,
    on_filter_toggle: Option<Rc<dyn Fn(usize, &mut App)>>,
}

impl SearchInputState {
    pub fn new(cx: &mut Context<Self>) -> Self {
        let input = cx.new(|cx| InputState::new(cx).placeholder("Search..."));

        Self {
            input,
            filters: Vec::new(),
            case_sensitive: false,
            use_regex: false,
            loading: false,
            results_count: None,
            on_search: None,
            on_filter_toggle: None,
        }
    }

    pub fn placeholder(
        self,
        placeholder: impl Into<SharedString>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        self.input.update(cx, |input, cx| {
            input.set_placeholder(placeholder, window, cx);
        });
        self
    }

    pub fn set_filters(&mut self, filters: Vec<SearchFilter>, cx: &mut Context<Self>) {
        self.filters = filters;
        cx.notify();
    }

    pub fn toggle_filter(&mut self, index: usize, cx: &mut Context<Self>) {
        if let Some(filter) = self.filters.get_mut(index) {
            filter.active = !filter.active;
            if let Some(handler) = &self.on_filter_toggle {
                handler(index, cx);
            }
            cx.notify();
        }
    }

    pub fn set_case_sensitive(&mut self, case_sensitive: bool, cx: &mut Context<Self>) {
        self.case_sensitive = case_sensitive;
        cx.notify();
    }

    pub fn toggle_case_sensitive(&mut self, cx: &mut Context<Self>) {
        self.case_sensitive = !self.case_sensitive;
        cx.notify();
    }

    pub fn set_use_regex(&mut self, use_regex: bool, cx: &mut Context<Self>) {
        self.use_regex = use_regex;
        cx.notify();
    }

    pub fn toggle_use_regex(&mut self, cx: &mut Context<Self>) {
        self.use_regex = !self.use_regex;
        cx.notify();
    }

    pub fn set_loading(&mut self, loading: bool, cx: &mut Context<Self>) {
        self.loading = loading;
        cx.notify();
    }

    pub fn set_results_count(&mut self, count: Option<usize>, cx: &mut Context<Self>) {
        self.results_count = count;
        cx.notify();
    }

    pub fn clear(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.input.update(cx, |input, cx| {
            input.set_value("", window, cx);
        });
        self.results_count = None;
        cx.notify();
    }

    pub fn query(&self, cx: &App) -> String {
        self.input.read(cx).content().to_string()
    }

    pub fn active_filters(&self) -> Vec<&SearchFilter> {
        self.filters.iter().filter(|f| f.active).collect()
    }

    pub fn case_sensitive(&self) -> bool {
        self.case_sensitive
    }

    pub fn use_regex(&self) -> bool {
        self.use_regex
    }
}

pub struct SearchInput {
    state: Entity<SearchInputState>,
    style: StyleRefinement,
}

impl SearchInput {
    pub fn new(cx: &mut Context<Self>) -> Self {
        let state = cx.new(SearchInputState::new);

        let state_clone = state.clone();
        let input_entity = state.read(cx).input.clone();
        cx.subscribe(&input_entity, move |_this, _input, event, cx| {
            if let InputEvent::Change = event {
                let query = state_clone.read(cx).input.read(cx).content().to_string();
                let handler = state_clone.read(cx).on_search.clone();
                if let Some(handler) = handler {
                    cx.defer(move |cx| {
                        handler(&query, cx);
                    });
                }
            }
        })
        .detach();

        Self {
            state,
            style: StyleRefinement::default(),
        }
    }

    pub fn placeholder(
        self,
        placeholder: impl Into<SharedString>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        self.state.update(cx, |state, cx| {
            state.input.update(cx, |input, cx| {
                input.set_placeholder(placeholder, window, cx);
            });
        });
        self
    }

    pub fn filters(self, filters: Vec<SearchFilter>, cx: &mut Context<Self>) -> Self {
        self.state.update(cx, |state, cx| {
            state.set_filters(filters, cx);
        });
        self
    }

    pub fn on_search<F>(self, handler: F, cx: &mut Context<Self>) -> Self
    where
        F: Fn(&str, &mut App) + 'static,
    {
        self.state.update(cx, |state, _cx| {
            state.on_search = Some(Rc::new(handler));
        });
        self
    }

    pub fn on_filter_toggle<F>(self, handler: F, cx: &mut Context<Self>) -> Self
    where
        F: Fn(usize, &mut App) + 'static,
    {
        self.state.update(cx, |state, _cx| {
            state.on_filter_toggle = Some(Rc::new(handler));
        });
        self
    }

    pub fn state(&self) -> &Entity<SearchInputState> {
        &self.state
    }
}

impl Styled for SearchInput {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Render for SearchInput {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = use_theme();
        let state = self.state.read(cx);
        let has_query = !state.input.read(cx).content().is_empty();
        let user_style = self.style.clone();

        div()
            .flex()
            .flex_col()
            .gap(px(8.0))
            // Main search input row
            .child(
                div()
                    .flex()
                    .items_center()
                    .gap(px(8.0))
                    .px(px(12.0))
                    .py(px(8.0))
                    .bg(theme.tokens.input)
                    .border_1()
                    .border_color(theme.tokens.border)
                    .rounded(theme.tokens.radius_md)
                    .child(
                        Icon::new(if state.loading {
                            IconSource::Named("loader".into())
                        } else {
                            IconSource::Named("search".into())
                        })
                        .size(px(16.0))
                        .color(theme.tokens.muted_foreground),
                    )
                    .child(div().flex_1().child(Input::new(&state.input)))
                    .when_some(state.results_count, |parent_div, count| {
                        parent_div.child(
                            div()
                                .px(px(8.0))
                                .py(px(2.0))
                                .rounded(theme.tokens.radius_sm)
                                .bg(theme.tokens.muted)
                                .text_size(px(12.0))
                                .text_color(theme.tokens.muted_foreground)
                                .child(format!("{} results", count)),
                        )
                    })
                    .child(
                        div()
                            .px(px(8.0))
                            .py(px(4.0))
                            .rounded(theme.tokens.radius_sm)
                            .cursor(CursorStyle::PointingHand)
                            .when(state.case_sensitive, |div| div.bg(theme.tokens.accent))
                            .when(!state.case_sensitive, |div| {
                                div.hover(|style| style.bg(theme.tokens.muted))
                            })
                            .on_mouse_down(
                                MouseButton::Left,
                                cx.listener(|this, _event, _window, cx| {
                                    this.state.update(cx, |state, cx| {
                                        state.toggle_case_sensitive(cx);
                                    });
                                }),
                            )
                            .child(
                                div()
                                    .text_size(px(12.0))
                                    .font_weight(FontWeight::SEMIBOLD)
                                    .text_color(if state.case_sensitive {
                                        theme.tokens.accent_foreground
                                    } else {
                                        theme.tokens.muted_foreground
                                    })
                                    .child("Aa"),
                            ),
                    )
                    .child(
                        div()
                            .px(px(8.0))
                            .py(px(4.0))
                            .rounded(theme.tokens.radius_sm)
                            .cursor(CursorStyle::PointingHand)
                            .when(state.use_regex, |div| div.bg(theme.tokens.accent))
                            .when(!state.use_regex, |div| {
                                div.hover(|style| style.bg(theme.tokens.muted))
                            })
                            .on_mouse_down(
                                MouseButton::Left,
                                cx.listener(|this, _event, _window, cx| {
                                    this.state.update(cx, |state, cx| {
                                        state.toggle_use_regex(cx);
                                    });
                                }),
                            )
                            .child(
                                div()
                                    .text_size(px(12.0))
                                    .font_weight(FontWeight::SEMIBOLD)
                                    .text_color(if state.use_regex {
                                        theme.tokens.accent_foreground
                                    } else {
                                        theme.tokens.muted_foreground
                                    })
                                    .child(".*"),
                            ),
                    )
                    .when(has_query, |parent_div| {
                        parent_div.child(
                            div()
                                .p(px(4.0))
                                .rounded(theme.tokens.radius_sm)
                                .cursor(CursorStyle::PointingHand)
                                .hover(|style| style.bg(theme.tokens.muted))
                                .on_mouse_down(
                                    MouseButton::Left,
                                    cx.listener(|this, _event, window, cx| {
                                        this.state.update(cx, |state, cx| {
                                            state.clear(window, cx);
                                        });
                                    }),
                                )
                                .child(
                                    Icon::new(IconSource::Named("x".into()))
                                        .size(px(14.0))
                                        .color(theme.tokens.muted_foreground),
                                ),
                        )
                    }),
            )
            .when(!state.filters.is_empty(), |parent_div| {
                parent_div.child(
                    div()
                        .flex()
                        .items_center()
                        .gap(px(8.0))
                        .flex_wrap()
                        .children(state.filters.iter().enumerate().map(|(idx, filter)| {
                            let filter = filter.clone();
                            div()
                                .px(px(8.0))
                                .py(px(4.0))
                                .rounded(theme.tokens.radius_sm)
                                .border_1()
                                .border_color(if filter.active {
                                    theme.tokens.accent
                                } else {
                                    theme.tokens.border
                                })
                                .bg(if filter.active {
                                    theme.tokens.accent.opacity(0.1)
                                } else {
                                    theme.tokens.background
                                })
                                .cursor(CursorStyle::PointingHand)
                                .hover(|style| style.bg(theme.tokens.muted))
                                .on_mouse_down(
                                    MouseButton::Left,
                                    cx.listener(move |this, _event, _window, cx| {
                                        this.state.update(cx, |state, cx| {
                                            state.toggle_filter(idx, cx);
                                        });
                                    }),
                                )
                                .child(
                                    div()
                                        .text_size(px(12.0))
                                        .text_color(if filter.active {
                                            theme.tokens.accent
                                        } else {
                                            theme.tokens.foreground
                                        })
                                        .child(filter.label.clone()),
                                )
                                .into_any_element()
                        })),
                )
            })
            .map(|this| {
                let mut div = this;
                div.style().refine(&user_style);
                div
            })
    }
}