jackdaw 0.3.1

A 3D level editor built with Bevy
Documentation
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
//! Project Files panel: a file tree view with live filesystem watching.

use std::path::{Path, PathBuf};
use std::sync::{Mutex, mpsc};

use bevy::prelude::*;
use jackdaw_feathers::{
    file_browser,
    icons::{Icon, IconFont},
    tokens,
};
use jackdaw_widgets::tree_view::{
    TreeChildrenPopulated, TreeNodeExpandToggle, TreeNodeExpanded, TreeRowChildren, TreeRowContent,
    TreeRowLabel,
};

// EditorEntity not needed for project file nodes

pub struct ProjectFilesPlugin;

impl Plugin for ProjectFilesPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<ProjectFilesState>()
            .add_systems(OnEnter(crate::AppState::Editor), setup_project_files)
            .add_systems(
                Update,
                (check_project_watcher, refresh_project_tree)
                    .run_if(in_state(crate::AppState::Editor)),
            )
            .add_observer(handle_directory_expand);
    }
}

/// State for the project files panel.
#[derive(Resource, Default)]
pub struct ProjectFilesState {
    pub root_directory: PathBuf,
    pub needs_refresh: bool,
    pub initialized: bool,
}

/// Marker on the project files tree container.
#[derive(Component)]
pub struct ProjectFilesTree;

/// Component on tree nodes representing a filesystem path.
#[derive(Component)]
pub struct ProjectFileNode(pub PathBuf);

/// Marker for directory nodes (have expandable children).
#[derive(Component)]
pub struct ProjectFileIsDir;

/// File watcher resource for the project root.
#[derive(Resource)]
struct ProjectFileWatcher {
    _watcher: notify::RecommendedWatcher,
    receiver: Mutex<mpsc::Receiver<()>>,
}

/// Initial setup: read project root and set up file watcher.
fn setup_project_files(
    project_root: Option<Res<crate::project::ProjectRoot>>,
    mut state: ResMut<ProjectFilesState>,
    mut commands: Commands,
) {
    let root = project_root
        .map(|p| p.root.clone())
        .unwrap_or_else(|| std::env::current_dir().unwrap_or_default());

    state.root_directory = root.clone();
    state.needs_refresh = true;
    state.initialized = false;

    // Set up file watcher
    let (tx, rx) = mpsc::channel();
    let watcher = notify::recommended_watcher(move |res: Result<notify::Event, notify::Error>| {
        if let Ok(event) = res {
            use notify::EventKind;
            if matches!(
                event.kind,
                EventKind::Create(_)
                    | EventKind::Remove(_)
                    | EventKind::Modify(notify::event::ModifyKind::Name(_))
            ) {
                let _ = tx.send(());
            }
        }
    });
    if let Ok(mut w) = watcher {
        use notify::Watcher;
        if w.watch(&root, notify::RecursiveMode::Recursive).is_ok() {
            commands.insert_resource(ProjectFileWatcher {
                _watcher: w,
                receiver: Mutex::new(rx),
            });
        }
    }
}

/// Poll the file watcher for changes.
fn check_project_watcher(
    watcher: Option<Res<ProjectFileWatcher>>,
    mut state: ResMut<ProjectFilesState>,
) {
    let Some(watcher) = watcher else { return };
    let Ok(rx) = watcher.receiver.lock() else {
        return;
    };
    if rx.try_recv().is_ok() {
        // Drain any additional pending events
        while rx.try_recv().is_ok() {}
        state.needs_refresh = true;
    }
}

/// Rebuild the root-level tree when needs_refresh is set.
fn refresh_project_tree(
    mut state: ResMut<ProjectFilesState>,
    tree_query: Query<(Entity, Option<&Children>), With<ProjectFilesTree>>,
    mut commands: Commands,
    icon_font: Option<Res<IconFont>>,
) {
    if !state.needs_refresh {
        return;
    }
    state.needs_refresh = false;

    let Ok((tree_entity, existing_children)) = tree_query.single() else {
        return;
    };

    // Clear existing children
    if let Some(children) = existing_children {
        for child in children.iter() {
            commands.entity(child).despawn();
        }
    }

    let Some(icon_font) = icon_font else { return };

    // Scan root directory
    let root = &state.root_directory;
    if !root.is_dir() {
        return;
    }

    let mut entries = scan_directory(root);
    entries.sort_by(|a, b| {
        // Directories first, then alphabetical
        b.1.cmp(&a.1).then_with(|| {
            a.0.file_name()
                .unwrap_or_default()
                .to_ascii_lowercase()
                .cmp(&b.0.file_name().unwrap_or_default().to_ascii_lowercase())
        })
    });

    for (path, is_dir) in entries {
        spawn_file_tree_row(&mut commands, tree_entity, &path, is_dir, &icon_font.0);
    }

    state.initialized = true;
}

/// Handle directory expansion: lazily populate children.
fn handle_directory_expand(
    event: On<bevy::picking::events::Pointer<bevy::picking::events::Click>>,
    toggle_query: Query<&ChildOf, With<TreeNodeExpandToggle>>,
    content_query: Query<&ChildOf, With<TreeRowContent>>,
    mut tree_nodes: Query<(
        &mut TreeNodeExpanded,
        &mut TreeChildrenPopulated,
        &Children,
        &ProjectFileNode,
    )>,
    children_containers: Query<Entity, With<TreeRowChildren>>,
    mut commands: Commands,
    icon_font: Option<Res<IconFont>>,
    file_dirs: Query<(), With<ProjectFileIsDir>>,
) {
    let clicked = event.event_target();

    // Walk up: click target → TreeRowContent → TreeNode
    // Check if this is a toggle click
    let tree_node_entity = if let Ok(toggle_parent) = toggle_query.get(clicked) {
        // Clicked on the expand toggle itself
        let content_entity = toggle_parent.parent();
        if let Ok(content_parent) = content_query.get(content_entity) {
            content_parent.parent()
        } else {
            return;
        }
    } else if let Ok(content_parent) = content_query.get(clicked) {
        // Clicked on the content row
        content_parent.parent()
    } else {
        return;
    };

    // Only handle directory nodes
    if file_dirs.get(tree_node_entity).is_err() {
        return;
    }

    let Ok((mut expanded, mut populated, children, file_node)) =
        tree_nodes.get_mut(tree_node_entity)
    else {
        return;
    };

    // Toggle expanded state
    expanded.0 = !expanded.0;

    // Find the TreeRowChildren container
    let Some(children_entity) = children
        .iter()
        .find(|c| children_containers.get(*c).is_ok())
    else {
        return;
    };

    if expanded.0 && !populated.0 {
        // First expansion: scan and populate children
        populated.0 = true;

        let Some(icon_font) = icon_font else { return };
        let dir_path = &file_node.0;

        let mut entries = scan_directory(dir_path);
        entries.sort_by(|a, b| {
            b.1.cmp(&a.1).then_with(|| {
                a.0.file_name()
                    .unwrap_or_default()
                    .to_ascii_lowercase()
                    .cmp(&b.0.file_name().unwrap_or_default().to_ascii_lowercase())
            })
        });

        for (path, is_dir) in entries {
            spawn_file_tree_row(&mut commands, children_entity, &path, is_dir, &icon_font.0);
        }
    }
}

/// Scan a directory and return (path, is_directory) entries.
fn scan_directory(dir: &Path) -> Vec<(PathBuf, bool)> {
    let Ok(read_dir) = std::fs::read_dir(dir) else {
        return Vec::new();
    };

    read_dir
        .filter_map(|entry| {
            let entry = entry.ok()?;
            let path = entry.path();
            let is_dir = path.is_dir();
            // Skip hidden files/directories (starting with .)
            let name = path.file_name()?.to_string_lossy().to_string();
            if name.starts_with('.') {
                return None;
            }
            // Skip target directory
            if name == "target" {
                return None;
            }
            Some((path, is_dir))
        })
        .collect()
}

/// Spawn a single file/directory tree row.
fn spawn_file_tree_row(
    commands: &mut Commands,
    parent: Entity,
    path: &Path,
    is_dir: bool,
    icon_font: &Handle<Font>,
) {
    let file_name = path
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_default();

    // Create the tree node entity
    let node_entity = commands
        .spawn((
            // Use the node entity itself as the "source" since we don't have scene entities
            ProjectFileNode(path.to_path_buf()),
            TreeNodeExpanded(false),
            TreeChildrenPopulated(false),
            Node {
                flex_direction: FlexDirection::Column,
                width: Val::Percent(100.0),
                ..Default::default()
            },
            ChildOf(parent),
        ))
        .id();

    // Note: We intentionally do NOT add TreeNode(self) here. TreeNode is a
    // relationship component that would warn about self-referencing. Project file
    // nodes use ProjectFileNode instead of TreeNode for identification.

    if is_dir {
        commands.entity(node_entity).insert(ProjectFileIsDir);
    }

    // Clickable row content
    let content = commands
        .spawn((
            TreeRowContent,
            Node {
                flex_direction: FlexDirection::Row,
                align_items: AlignItems::Center,
                padding: UiRect::axes(Val::Px(tokens::SPACING_SM), Val::Px(tokens::SPACING_XS)),
                column_gap: Val::Px(tokens::SPACING_SM),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                width: Val::Percent(100.0),
                ..Default::default()
            },
            ChildOf(node_entity),
        ))
        .id();

    // Hover effects
    commands.entity(content).observe(
        |hover: On<Pointer<Over>>, mut bg: Query<&mut BackgroundColor>| {
            if let Ok(mut bg) = bg.get_mut(hover.event_target()) {
                bg.0 = tokens::HOVER_BG;
            }
        },
    );
    commands.entity(content).observe(
        |out: On<Pointer<Out>>, mut bg: Query<&mut BackgroundColor>| {
            if let Ok(mut bg) = bg.get_mut(out.event_target()) {
                bg.0 = Color::NONE;
            }
        },
    );

    if is_dir {
        // Expand toggle (chevron)
        let _ = commands
            .spawn((
                TreeNodeExpandToggle,
                Text::new(String::from(Icon::ChevronRight.unicode())),
                TextFont {
                    font: icon_font.clone(),
                    font_size: tokens::ICON_SM,
                    ..Default::default()
                },
                TextColor(tokens::TEXT_SECONDARY),
                Node {
                    width: Val::Px(15.0),
                    flex_shrink: 0.0,
                    ..Default::default()
                },
                ChildOf(content),
            ))
            .id();

        // Directory label (no icon, just text)
        commands.spawn((
            TreeRowLabel,
            Text::new(file_name),
            TextFont {
                font_size: tokens::TEXT_SIZE,
                ..Default::default()
            },
            TextColor(tokens::TEXT_PRIMARY),
            ChildOf(content),
        ));

        // Children container (initially hidden)
        commands.spawn((
            TreeRowChildren,
            Node {
                flex_direction: FlexDirection::Column,
                padding: UiRect::left(Val::Px(16.0)),
                margin: UiRect::left(Val::Px(tokens::SPACING_SM)),
                border: UiRect::left(Val::Px(1.0)),
                width: Val::Percent(100.0),
                display: Display::None,
                ..Default::default()
            },
            BorderColor::all(tokens::CONNECTION_LINE),
            ChildOf(node_entity),
        ));

        // Toggle expand/collapse on click
        let node_for_click = node_entity;
        commands.entity(content).observe(
            move |_: On<Pointer<Click>>,
                  mut expanded_query: Query<&mut TreeNodeExpanded>,
                  children_query: Query<&Children>,
                  children_containers: Query<Entity, With<TreeRowChildren>>,
                  mut node_query: Query<&mut Node>,
                  toggle_texts: Query<&Children, With<TreeRowContent>>,
                  toggle_markers: Query<Entity, With<TreeNodeExpandToggle>>,
                  mut text_query: Query<&mut Text>| {
                let Ok(mut expanded) = expanded_query.get_mut(node_for_click) else {
                    return;
                };
                expanded.0 = !expanded.0;
                let is_expanded = expanded.0;

                // Toggle children visibility
                if let Ok(children) = children_query.get(node_for_click) {
                    for child in children.iter() {
                        if children_containers.get(child).is_ok() {
                            if let Ok(mut node) = node_query.get_mut(child) {
                                node.display = if is_expanded {
                                    Display::Flex
                                } else {
                                    Display::None
                                };
                            }
                        }
                    }
                }

                // Update chevron icon
                if let Ok(content_children) = toggle_texts.get(node_for_click) {
                    // Find the TreeRowContent, then its children
                    for cc in content_children.iter() {
                        if let Ok(content_kids) = children_query.get(cc) {
                            for kid in content_kids.iter() {
                                if toggle_markers.get(kid).is_ok() {
                                    if let Ok(mut text) = text_query.get_mut(kid) {
                                        text.0 = String::from(if is_expanded {
                                            Icon::ChevronDown.unicode()
                                        } else {
                                            Icon::ChevronRight.unicode()
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            },
        );
    } else {
        // File icon based on extension
        let icon = file_browser::file_icon(&file_name);

        commands.spawn((
            Text::new(String::from(icon.unicode())),
            TextFont {
                font: icon_font.clone(),
                font_size: tokens::ICON_SM,
                ..Default::default()
            },
            TextColor(tokens::FILE_ICON_COLOR),
            Node {
                width: Val::Px(15.0),
                flex_shrink: 0.0,
                ..Default::default()
            },
            ChildOf(content),
        ));

        // File label
        commands.spawn((
            TreeRowLabel,
            Text::new(file_name),
            TextFont {
                font_size: tokens::TEXT_SIZE,
                ..Default::default()
            },
            TextColor(tokens::TEXT_PRIMARY),
            ChildOf(content),
        ));
    }
}