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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use crate::components::icon::Icon;
use crate::components::icon_source::IconSource;
use crate::theme::use_theme;
use kael::{prelude::FluentBuilder as _, *};
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FileNodeKind {
    File,
    Directory,
    Symlink,
}

#[derive(Clone, Debug)]
pub struct FileNode {
    pub path: PathBuf,
    pub name: String,
    pub kind: FileNodeKind,
    pub children: Vec<FileNode>,
    pub size: Option<u64>,
    pub modified: Option<String>,
    pub is_hidden: bool,
    pub has_unloaded_children: bool,
}

impl FileNode {
    pub fn file(path: impl Into<PathBuf>) -> Self {
        let path = path.into();
        let name = path
            .file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_default();
        Self {
            path,
            name,
            kind: FileNodeKind::File,
            children: Vec::new(),
            size: None,
            modified: None,
            is_hidden: false,
            has_unloaded_children: false,
        }
    }

    pub fn directory(path: impl Into<PathBuf>) -> Self {
        let path = path.into();
        let name = path
            .file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_default();
        Self {
            path,
            name,
            kind: FileNodeKind::Directory,
            children: Vec::new(),
            size: None,
            modified: None,
            is_hidden: false,
            has_unloaded_children: false,
        }
    }

    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    pub fn with_children(mut self, children: Vec<FileNode>) -> Self {
        self.children = children;
        self
    }

    pub fn with_size(mut self, size: u64) -> Self {
        self.size = Some(size);
        self
    }

    pub fn with_modified(mut self, modified: impl Into<String>) -> Self {
        self.modified = Some(modified.into());
        self
    }

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

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

    pub fn is_directory(&self) -> bool {
        self.kind == FileNodeKind::Directory
    }

    pub fn extension(&self) -> Option<&str> {
        self.path.extension().and_then(|e| e.to_str())
    }

    pub fn file_icon(&self, is_expanded: bool) -> IconSource {
        match self.kind {
            FileNodeKind::Directory => {
                if is_expanded {
                    IconSource::Named("folder-open".into())
                } else {
                    IconSource::Named("folder".into())
                }
            }
            FileNodeKind::Symlink => IconSource::Named("link".into()),
            FileNodeKind::File => match self.extension() {
                Some("json") | Some("yaml") | Some("yml") | Some("toml") | Some("xml") => {
                    IconSource::Named("file-json".into())
                }
                Some("md") | Some("txt") | Some("doc") | Some("docx") | Some("pdf") => {
                    IconSource::Named("file-text".into())
                }
                Some("sh") | Some("bash") | Some("zsh") => IconSource::Named("hash".into()),
                Some("png") | Some("jpg") | Some("jpeg") | Some("gif") | Some("svg")
                | Some("ico") | Some("webp") => IconSource::Named("image".into()),
                Some("mp3") | Some("wav") | Some("ogg") | Some("flac") => {
                    IconSource::Named("music".into())
                }
                Some("mp4") | Some("mov") | Some("avi") | Some("webm") => {
                    IconSource::Named("video".into())
                }
                Some("zip") | Some("tar") | Some("gz") | Some("rar") | Some("7z") => {
                    IconSource::Named("archive".into())
                }
                _ => IconSource::Named("file-code".into()),
            },
        }
    }

    pub fn file_icon_color(&self, theme: &crate::theme::Theme) -> Hsla {
        match self.kind {
            FileNodeKind::Directory => rgb(0x60a5fa).into(),
            FileNodeKind::Symlink => theme.tokens.muted_foreground,
            FileNodeKind::File => match self.extension() {
                Some("json") | Some("yaml") | Some("yml") | Some("toml") | Some("xml") => {
                    rgb(0xfbbf24).into()
                }
                Some("md") | Some("txt") | Some("doc") | Some("docx") | Some("pdf") => {
                    rgb(0xa78bfa).into()
                }
                Some("sh") | Some("bash") | Some("zsh") => rgb(0x4ade80).into(),
                Some("png") | Some("jpg") | Some("jpeg") | Some("gif") | Some("svg")
                | Some("ico") | Some("webp") => rgb(0x22c55e).into(),
                Some("mp3") | Some("wav") | Some("ogg") | Some("flac") => rgb(0xf472b6).into(),
                Some("mp4") | Some("mov") | Some("avi") | Some("webm") => rgb(0xf472b6).into(),
                Some("zip") | Some("tar") | Some("gz") | Some("rar") | Some("7z") => {
                    rgb(0xfbbf24).into()
                }
                _ => rgb(0x9ca3af).into(),
            },
        }
    }
}

#[derive(Clone)]
struct FlatFileNode {
    node: FileNode,
    level: usize,
}

fn sort_file_nodes(nodes: &mut [FileNode]) {
    nodes.sort_by(|a, b| match (a.is_directory(), b.is_directory()) {
        (true, false) => std::cmp::Ordering::Less,
        (false, true) => std::cmp::Ordering::Greater,
        _ => a.name.to_lowercase().cmp(&b.name.to_lowercase()),
    });
    for node in nodes.iter_mut() {
        if !node.children.is_empty() {
            sort_file_nodes(&mut node.children);
        }
    }
}

fn flatten_file_tree(
    nodes: &[FileNode],
    expanded_paths: &HashSet<PathBuf>,
    level: usize,
    show_hidden: bool,
) -> Vec<FlatFileNode> {
    let mut flat = Vec::new();

    for node in nodes {
        if node.is_hidden && !show_hidden {
            continue;
        }

        flat.push(FlatFileNode {
            node: node.clone(),
            level,
        });

        let has_children = !node.children.is_empty() || node.has_unloaded_children;
        if has_children && expanded_paths.contains(&node.path) {
            let children =
                flatten_file_tree(&node.children, expanded_paths, level + 1, show_hidden);
            flat.extend(children);
        }
    }

    flat
}

fn format_size(size: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if size >= GB {
        format!("{:.1} GB", size as f64 / GB as f64)
    } else if size >= MB {
        format!("{:.1} MB", size as f64 / MB as f64)
    } else if size >= KB {
        format!("{:.1} KB", size as f64 / KB as f64)
    } else {
        format!("{} B", size)
    }
}

const ROW_HEIGHT: f32 = 28.0;

#[derive(IntoElement)]
pub struct FileTree {
    nodes: Vec<FileNode>,
    selected_path: Option<PathBuf>,
    expanded_paths: Vec<PathBuf>,
    show_hidden: bool,
    show_file_size: bool,
    on_select: Option<Arc<dyn Fn(&PathBuf, &mut Window, &mut App) + Send + Sync>>,
    on_open: Option<Arc<dyn Fn(&PathBuf, &mut Window, &mut App) + Send + Sync>>,
    on_toggle: Option<Arc<dyn Fn(&PathBuf, bool, &mut Window, &mut App) + Send + Sync>>,
    on_context_menu:
        Option<Arc<dyn Fn(&PathBuf, Point<Pixels>, &mut Window, &mut App) + Send + Sync>>,
    style: StyleRefinement,
}

impl FileTree {
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            selected_path: None,
            expanded_paths: Vec::new(),
            show_hidden: false,
            show_file_size: false,
            on_select: None,
            on_open: None,
            on_toggle: None,
            on_context_menu: None,
            style: StyleRefinement::default(),
        }
    }

    pub fn nodes(mut self, mut nodes: Vec<FileNode>) -> Self {
        sort_file_nodes(&mut nodes);
        self.nodes = nodes;
        self
    }

    pub fn selected_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.selected_path = Some(path.into());
        self
    }

    pub fn expanded_paths(mut self, paths: Vec<PathBuf>) -> Self {
        self.expanded_paths = paths;
        self
    }

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

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

    pub fn on_select<F>(mut self, handler: F) -> Self
    where
        F: Fn(&PathBuf, &mut Window, &mut App) + Send + Sync + 'static,
    {
        self.on_select = Some(Arc::new(handler));
        self
    }

    pub fn on_open<F>(mut self, handler: F) -> Self
    where
        F: Fn(&PathBuf, &mut Window, &mut App) + Send + Sync + 'static,
    {
        self.on_open = Some(Arc::new(handler));
        self
    }

    pub fn on_toggle<F>(mut self, handler: F) -> Self
    where
        F: Fn(&PathBuf, bool, &mut Window, &mut App) + Send + Sync + 'static,
    {
        self.on_toggle = Some(Arc::new(handler));
        self
    }

    pub fn on_context_menu<F>(mut self, handler: F) -> Self
    where
        F: Fn(&PathBuf, Point<Pixels>, &mut Window, &mut App) + Send + Sync + 'static,
    {
        self.on_context_menu = Some(Arc::new(handler));
        self
    }
}

impl Default for FileTree {
    fn default() -> Self {
        Self::new()
    }
}

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

impl RenderOnce for FileTree {
    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
        let theme = use_theme();
        let user_style = self.style;

        let expanded_set: HashSet<PathBuf> = self.expanded_paths.into_iter().collect();
        let flat_nodes = flatten_file_tree(&self.nodes, &expanded_set, 0, self.show_hidden);

        let selected_path = self.selected_path;
        let on_select = self.on_select;
        let on_open = self.on_open;
        let on_toggle = self.on_toggle;
        let on_context_menu = self.on_context_menu;
        let show_file_size = self.show_file_size;

        div()
            .flex()
            .flex_col()
            .w_full()
            .bg(kael::transparent_black())
            .map(|mut this| {
                this.style().refine(&user_style);
                this
            })
            .children(flat_nodes.into_iter().map(|flat_node| {
                let is_selected = selected_path.as_ref() == Some(&flat_node.node.path);
                let is_expanded = expanded_set.contains(&flat_node.node.path);
                let has_children =
                    !flat_node.node.children.is_empty() || flat_node.node.has_unloaded_children;
                let indent = px((flat_node.level as f32) * 16.0);
                let node = flat_node.node;
                let path = node.path.clone();

                let icon_color = node.file_icon_color(&theme);
                let node_icon = node.file_icon(is_expanded);

                div()
                    .id(SharedString::from(path.to_string_lossy().to_string()))
                    .w_full()
                    .h(px(ROW_HEIGHT))
                    .flex()
                    .items_center()
                    .mx(px(8.0))
                    .px(px(8.0))
                    .pl(indent + px(8.0))
                    .rounded(px(8.0))
                    .cursor_pointer()
                    .bg(if is_selected {
                        theme.tokens.accent
                    } else {
                        kael::transparent_black()
                    })
                    .text_color(if is_selected {
                        theme.tokens.accent_foreground
                    } else if node.is_hidden {
                        theme.tokens.muted_foreground
                    } else {
                        theme.tokens.foreground
                    })
                    .when(!is_selected, |d| {
                        d.hover(|s| s.bg(theme.tokens.accent.opacity(0.5)))
                    })
                    .on_click({
                        let path = path.clone();
                        let on_select = on_select.clone();
                        let on_toggle = on_toggle.clone();
                        let on_open = on_open.clone();
                        let is_dir = node.is_directory();

                        move |event, window, cx| {
                            if let Some(ref handler) = on_select {
                                handler(&path, window, cx);
                            }

                            if is_dir {
                                if let Some(ref handler) = on_toggle {
                                    handler(&path, !is_expanded, window, cx);
                                }
                            } else if event.click_count() == 2 {
                                if let Some(ref handler) = on_open {
                                    handler(&path, window, cx);
                                }
                            }
                        }
                    })
                    .on_mouse_down(MouseButton::Right, {
                        let path = path.clone();
                        let on_context_menu = on_context_menu.clone();

                        move |event, window, cx| {
                            if let Some(ref handler) = on_context_menu {
                                handler(&path, event.position, window, cx);
                            }
                        }
                    })
                    .child(
                        div()
                            .flex()
                            .items_center()
                            .gap(px(6.0))
                            .flex_1()
                            .child(
                                div()
                                    .w(px(16.0))
                                    .h(px(16.0))
                                    .flex()
                                    .items_center()
                                    .justify_center()
                                    .when(has_children, |d| {
                                        d.child(
                                            Icon::new(if is_expanded {
                                                "chevron-down"
                                            } else {
                                                "chevron-right"
                                            })
                                            .size(px(12.0))
                                            .color(theme.tokens.muted_foreground),
                                        )
                                    }),
                            )
                            .child(Icon::new(node_icon).size(px(16.0)).color(if is_selected {
                                theme.tokens.accent_foreground
                            } else {
                                icon_color
                            }))
                            .child(
                                div()
                                    .flex_1()
                                    .text_size(px(13.0))
                                    .font_family(theme.tokens.font_family.clone())
                                    .when(node.is_hidden, |d| d.opacity(0.6))
                                    .child(node.name.clone()),
                            )
                            .when(
                                show_file_size && node.size.is_some() && !node.is_directory(),
                                |d| {
                                    d.child(
                                        div()
                                            .text_size(px(11.0))
                                            .text_color(theme.tokens.muted_foreground)
                                            .child(format_size(node.size.unwrap())),
                                    )
                                },
                            ),
                    )
            }))
    }
}