Skip to main content

logicaffeine_web/ui/components/
file_browser.rs

1//! File browser sidebar with tree navigation.
2//!
3//! Displays a hierarchical file tree for the Studio's virtual file system.
4//! Supports collapsible directories, file selection, and responsive layout.
5//!
6//! # Props
7//!
8//! - `tree` - Root [`FileNode`] containing the file tree
9//! - `selected_path` - Currently selected file path
10//! - `on_select` - Callback when a file is selected
11//! - `on_toggle_dir` - Callback when a directory is expanded/collapsed
12//! - `on_new_file` - New file, where the host keeps the reader's files; without it the header offers none
13//!
14//! # File Icons
15//!
16//! Icons are chosen by file extension:
17//! - `.logic` files: ∀ (forall symbol)
18//! - `.logos` files: λ (lambda symbol)
19//! - `.math` files: π (pi symbol)
20//! - Other files: document icon
21
22use crate::ui::components::icon::{Icon, IconSize, IconVariant};
23use crate::ui::components::tree::{Tree, TreeNode};
24use crate::ui::state::FileNode;
25use dioxus::prelude::*;
26
27const FILE_BROWSER_STYLE: &str = r#"
28.file-browser {
29    display: flex;
30    flex-direction: column;
31    height: 100%;
32    width: 100%;
33    background: var(--studio-surface, #12161c);
34    overflow: hidden;
35}
36
37.file-browser-header {
38    display: flex;
39    justify-content: space-between;
40    align-items: center;
41    padding: 0 14px;
42    height: 52px;
43    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
44    background: rgba(255, 255, 255, 0.02);
45    flex-shrink: 0;
46}
47
48.file-browser-heading {
49    display: flex;
50    align-items: baseline;
51    gap: 8px;
52    min-width: 0;
53}
54
55/* A fact about the files beside their title, in the theme's secondary text. */
56.file-browser-note {
57    font-size: 12px;
58    font-weight: 400;
59    color: var(--studio-text-secondary);
60    white-space: nowrap;
61    overflow: hidden;
62    text-overflow: ellipsis;
63}
64
65.file-browser-actions {
66    display: flex;
67    gap: 4px;
68}
69
70.file-browser-btn {
71    padding: 4px 8px;
72    border: none;
73    background: transparent;
74    color: rgba(255, 255, 255, 0.5);
75    font-size: 14px;
76    cursor: pointer;
77    border-radius: 4px;
78    transition: all 0.15s ease;
79}
80
81.file-browser-btn:hover:not(:disabled) {
82    background: rgba(255, 255, 255, 0.08);
83    color: rgba(255, 255, 255, 0.9);
84}
85
86/* New file waits while the reader's files load: it keeps its place, dimmed, and says why. */
87.file-browser-btn:disabled {
88    opacity: 0.5;
89    cursor: default;
90}
91
92/* The file tree is the Studio's tree (`components::tree`): it takes the sidebar's room and scrolls. */
93.file-tree {
94    flex: 1;
95    overflow: auto;
96}
97
98.file-tree-item .icon {
99    flex: none;
100    width: 16px;
101    margin-right: 6px;
102    display: flex;
103    align-items: center;
104    justify-content: center;
105    font-size: 13px;
106    opacity: 0.7;
107}
108
109.file-tree-item .name {
110    flex: 1;
111    min-width: 0;
112    overflow: hidden;
113    text-overflow: ellipsis;
114    white-space: nowrap;
115}
116
117.file-tree-empty {
118    padding: 20px;
119    text-align: center;
120    color: rgba(255, 255, 255, 0.4);
121    font-size: 13px;
122}
123
124/* Mobile: fill the wrapper, which handles positioning */
125@media (max-width: 768px) {
126    .file-browser {
127        width: 100%;
128        min-width: 100%;
129        max-width: 100%;
130    }
131
132    .file-tree-item {
133        color: rgba(255, 255, 255, 0.9);
134        -webkit-text-fill-color: rgba(255, 255, 255, 0.9);
135    }
136
137    .file-tree-item.selected {
138        color: #00d4ff;
139        -webkit-text-fill-color: #00d4ff;
140    }
141
142    .file-tree-item .name {
143        color: inherit;
144        -webkit-text-fill-color: inherit;
145    }
146}
147"#;
148
149#[component]
150pub fn FileBrowser(
151    tree: FileNode,
152    selected_path: Option<String>,
153    on_select: EventHandler<String>,
154    on_toggle_dir: EventHandler<String>,
155    /// New file, where the host keeps the reader's files. A host that keeps none (a frame on another
156    /// page) gives none, and the header offers no New file.
157    #[props(default)]
158    on_new_file: Option<EventHandler<()>>,
159    #[props(default = false)] show_private_mode: bool,
160    /// Why New file cannot make a file yet, such as the reader's files still loading. While there
161    /// is a reason, the button stays in place, disabled, and its tooltip gives the reason.
162    #[props(default)]
163    new_file_waits: Option<String>,
164) -> Element {
165    rsx! {
166        style { "{FILE_BROWSER_STYLE}" }
167
168        aside { class: "file-browser",
169            // Header
170            div { class: "file-browser-header",
171                div { class: "file-browser-heading",
172                    span { class: "file-browser-title panel-title", "Files" }
173                    // Without OPFS the files live in IndexedDB for this session only.
174                    if show_private_mode {
175                        span {
176                            class: "file-browser-note",
177                            title: "Temporary storage: files last for this browser session",
178                            "Private mode"
179                        }
180                    }
181                }
182                div { class: "file-browser-actions",
183                    if let Some(new_file) = on_new_file {
184                        button {
185                            class: "file-browser-btn",
186                            disabled: new_file_waits.is_some(),
187                            onclick: move |_| new_file.call(()),
188                            title: new_file_waits.clone().unwrap_or_else(|| "New file".to_string()),
189                            "+"
190                        }
191                    }
192                }
193            }
194
195            // File tree
196            Tree { kind: "file-tree", label: "Files",
197                if tree.children.is_empty() {
198                    div { class: "file-tree-empty",
199                        "No files yet"
200                    }
201                } else {
202                    for (i, child) in tree.children.iter().enumerate() {
203                        FileTreeNode {
204                            key: "{child.path}",
205                            node: child.clone(),
206                            selected_path: selected_path.clone(),
207                            depth: 0,
208                            last: i + 1 == tree.children.len(),
209                            stop: i == 0 && !selected_path.as_deref().is_some_and(|path| draws(&tree, path)),
210                            on_select: on_select.clone(),
211                            on_toggle_dir: on_toggle_dir.clone(),
212                        }
213                    }
214                }
215            }
216        }
217    }
218}
219
220/// Whether the tree draws the row of `path`: a node of it whose folders are all open.
221fn draws(node: &FileNode, path: &str) -> bool {
222    node.children.iter().any(|child| child.path == path || (child.is_directory && child.expanded && draws(child, path)))
223}
224
225#[component]
226fn FileTreeNode(
227    node: FileNode,
228    selected_path: Option<String>,
229    depth: usize,
230    /// The last of its siblings: its tree line stops at its own branch.
231    last: bool,
232    /// The tree's tab stop, since the tree draws no selected row.
233    #[props(default)]
234    stop: bool,
235    on_select: EventHandler<String>,
236    on_toggle_dir: EventHandler<String>,
237) -> Element {
238    let icon_variant = if node.is_directory {
239        if node.expanded {
240            IconVariant::FolderOpen
241        } else {
242            IconVariant::Folder
243        }
244    } else {
245        IconVariant::File
246    };
247
248    // File-specific display text for logic/math files
249    let file_symbol: Option<&'static str> = if !node.is_directory {
250        match node.name.rsplit('.').next() {
251            Some("logic") => Some("∀"),
252            Some("logos") => Some("λ"),
253            Some("math") => Some("π"),
254            _ => None,
255        }
256    } else {
257        None
258    };
259
260    let path = node.path.clone();
261    let is_directory = node.is_directory;
262    let count = node.children.len();
263
264    rsx! {
265        TreeNode {
266            depth,
267            last,
268            expandable: is_directory,
269            open: node.expanded,
270            selected: selected_path.as_ref() == Some(&node.path),
271            stop,
272            title: node.path.clone(),
273            on_press: Some(EventHandler::new(move |_| {
274                if is_directory {
275                    on_toggle_dir.call(path.clone());
276                } else {
277                    on_select.call(path.clone());
278                }
279            })),
280            row: rsx! {
281                span { class: "icon",
282                    if let Some(sym) = file_symbol {
283                        span { "{sym}" }
284                    } else {
285                        Icon { variant: icon_variant, size: IconSize::Small }
286                    }
287                }
288                span { class: "name", "{node.name}" }
289            },
290            for (i, child) in node.children.iter().enumerate() {
291                FileTreeNode {
292                    key: "{child.path}",
293                    node: child.clone(),
294                    selected_path: selected_path.clone(),
295                    depth: depth + 1,
296                    last: i + 1 == count,
297                    on_select: on_select.clone(),
298                    on_toggle_dir: on_toggle_dir.clone(),
299                }
300            }
301        }
302    }
303}