jackdaw_feathers 0.4.1

Internal crate for the jackdaw editor
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use bevy::{feathers::theme::ThemedText, prelude::*, ui_widgets::observe};
use bevy_monitors::prelude::{MonitorSelf, Mutation, NotifyChanged};
use jackdaw_widgets::tree_view::{
    EntityCategory, TreeChildrenPopulated, TreeFocused, TreeNode, TreeNodeExpandToggle,
    TreeNodeExpanded, TreeRowChildren, TreeRowClicked, TreeRowContent, TreeRowDot, TreeRowDropped,
    TreeRowDroppedOnRoot, TreeRowLabel, TreeRowSelected, TreeRowStartRename,
    TreeRowVisibilityToggle, TreeRowVisibilityToggled, TreeView,
};

use lucide_icons::Icon;

use crate::tokens;

pub const ROW_BG: Color = Color::NONE;
const INDENT_WIDTH: f32 = 16.0;
const TOGGLE_WIDTH: f32 = 18.0;
const DOT_COLUMN_WIDTH: f32 = 14.0;

/// Parameters for tree row icon font rendering
#[derive(Clone)]
pub struct TreeRowStyle {
    pub icon_font: Handle<Font>,
}

/// Returns the display color for an entity category.
pub fn category_color(category: EntityCategory) -> Color {
    match category {
        EntityCategory::Camera => tokens::CATEGORY_CAMERA,
        EntityCategory::Light => tokens::CATEGORY_LIGHT,
        EntityCategory::Mesh => tokens::CATEGORY_MESH,
        EntityCategory::Scene => tokens::CATEGORY_SCENE,
        EntityCategory::Entity => tokens::CATEGORY_ENTITY,
    }
}

/// Creates a tree row bundle for displaying an entity in the hierarchy
pub fn tree_row(
    label: &str,
    has_children: bool,
    selected: bool,
    source: Entity,
    category: EntityCategory,
    style: &TreeRowStyle,
) -> impl Bundle {
    (
        TreeNode(source),
        TreeNodeExpanded(false),
        TreeChildrenPopulated(false),
        MonitorSelf,
        NotifyChanged::<TreeNodeExpanded>::default(),
        Node {
            flex_direction: FlexDirection::Column,
            width: percent(100),
            ..default()
        },
        children![
            // The clickable row content
            tree_row_content(label, has_children, selected, source, category, style),
            // Container for child rows (initially empty, populated lazily)
            (
                TreeRowChildren,
                Node {
                    flex_direction: FlexDirection::Column,
                    padding: UiRect::left(px(INDENT_WIDTH)),
                    margin: UiRect::left(px(tokens::SPACING_SM)),
                    border: UiRect::left(px(1.0)),
                    width: percent(100),
                    display: Display::None,
                    ..default()
                },
                BorderColor::all(tokens::CONNECTION_LINE),
            )
        ],
        // React to TreeNodeExpanded changes: toggle children visibility + chevron icon
        observe(
            |mutation: On<Mutation<TreeNodeExpanded>>,
             expanded_query: Query<(&TreeNodeExpanded, &Children)>,
             children_container: Query<Entity, With<TreeRowChildren>>,
             content_query: Query<&Children, With<TreeRowContent>>,
             toggle_query: Query<&Children, With<TreeNodeExpandToggle>>,
             mut node_query: Query<&mut Node>,
             mut text_query: Query<&mut Text>| {
                let entity = mutation.event_target();
                let Ok((expanded, children)) = expanded_query.get(entity) else {
                    return;
                };

                for child in children.iter() {
                    // Toggle TreeRowChildren display
                    if children_container.contains(child)
                        && let Ok(mut node) = node_query.get_mut(child)
                    {
                        node.display = if expanded.0 {
                            Display::Flex
                        } else {
                            Display::None
                        };
                    }

                    // Update chevron: TreeRowContent -> TreeNodeExpandToggle -> Text
                    if let Ok(content_children) = content_query.get(child) {
                        for cc in content_children.iter() {
                            if let Ok(toggle_children) = toggle_query.get(cc) {
                                for tc in toggle_children.iter() {
                                    if let Ok(mut text) = text_query.get_mut(tc) {
                                        text.0 = if expanded.0 {
                                            Icon::ChevronDown.unicode().to_string()
                                        } else {
                                            Icon::ChevronRight.unicode().to_string()
                                        };
                                    }
                                }
                            }
                        }
                    }
                }
            },
        ),
    )
}

fn tree_row_content(
    label: &str,
    has_children: bool,
    selected: bool,
    source: Entity,
    category: EntityCategory,
    style: &TreeRowStyle,
) -> impl Bundle {
    let bg = if selected {
        tokens::SELECTED_BG
    } else {
        ROW_BG
    };
    let border = if selected {
        tokens::SELECTED_BORDER
    } else {
        Color::NONE
    };

    (
        TreeRowContent,
        Node {
            flex_direction: FlexDirection::Row,
            align_items: AlignItems::Center,
            padding: UiRect::axes(px(tokens::SPACING_SM), px(tokens::SPACING_XS)),
            border: UiRect::all(px(1.0)),
            border_radius: BorderRadius::all(px(6.0)),
            width: percent(100),
            ..default()
        },
        BackgroundColor(bg),
        BorderColor::all(border),
        children![
            // Expand toggle (chevron)
            expand_toggle(has_children, &style.icon_font),
            // Category icon
            category_dot(category, &style.icon_font),
            // Label
            (
                TreeRowLabel,
                Text::new(label),
                TextFont {
                    font_size: tokens::FONT_MD,
                    ..default()
                },
                Node {
                    flex_grow: 1.0,
                    ..default()
                },
                ThemedText,
            ),
            // Visibility toggle (eye icon)
            visibility_toggle(source, &style.icon_font)
        ],
        // Click handler for selection (left-click only)
        observe(move |mut click: On<Pointer<Click>>, mut commands: Commands| {
            if click.event.button != PointerButton::Primary {
                return;
            }
            click.propagate(false);
            commands.trigger(TreeRowClicked {
                entity: click.event_target(),
                source_entity: source,
            });
        }),
        // Hover effects (skip selected rows)
        observe(
            |hover: On<Pointer<Over>>,
             mut bg_query: Query<
                &mut BackgroundColor,
                (With<TreeRowContent>, Without<TreeRowSelected>),
            >| {
                if let Ok(mut bg) = bg_query.get_mut(hover.event_target()) {
                    bg.0 = tokens::HOVER_BG;
                }
            },
        ),
        observe(
            |out: On<Pointer<Out>>,
             mut bg_query: Query<
                &mut BackgroundColor,
                (With<TreeRowContent>, Without<TreeRowSelected>),
            >| {
                if let Ok(mut bg) = bg_query.get_mut(out.event_target()) {
                    bg.0 = ROW_BG;
                }
            },
        ),
        // Drag-and-drop: highlight drop target with border accent
        observe(
            |mut drag_enter: On<Pointer<DragEnter>>,
             mut query: Query<(&mut BackgroundColor, &mut Node), With<TreeRowContent>>| {
                drag_enter.propagate(false);
                if let Ok((mut bg, mut node)) = query.get_mut(drag_enter.event_target()) {
                    bg.0 = tokens::DROP_TARGET_BG;
                    node.border = UiRect::left(px(3.0));
                }
            },
        ),
        observe(
            |mut drag_leave: On<Pointer<DragLeave>>,
             mut query: Query<(&mut BackgroundColor, &mut Node), With<TreeRowContent>>,
             selected: Query<(), With<TreeRowSelected>>| {
                drag_leave.propagate(false);
                if let Ok((mut bg, mut node)) = query.get_mut(drag_leave.event_target()) {
                    bg.0 = if selected.contains(drag_leave.event_target()) {
                        tokens::SELECTED_BG
                    } else {
                        ROW_BG
                    };
                    node.border = UiRect::all(px(1.0));
                }
            },
        ),
        // Drag-and-drop: resolve source entities and fire TreeRowDropped
        observe(
            |mut drag_drop: On<Pointer<DragDrop>>,
             mut commands: Commands,
             parent_query: Query<&ChildOf>,
             tree_nodes: Query<&TreeNode>,
             mut query: Query<(&mut BackgroundColor, &mut Node), With<TreeRowContent>>,
             selected_query: Query<(), With<TreeRowSelected>>| {
                drag_drop.propagate(false);
                let target_content = drag_drop.event_target();

                // Revert drop target styling
                if let Ok((mut bg, mut node)) = query.get_mut(target_content) {
                    bg.0 = if selected_query.contains(target_content) {
                        tokens::SELECTED_BG
                    } else {
                        ROW_BG
                    };
                    node.border = UiRect::all(px(1.0));
                }

                // Resolve both target and dragged to their scene source entities
                let Ok(&ChildOf(target_tree_row)) = parent_query.get(target_content) else {
                    return;
                };
                let Ok(target_node) = tree_nodes.get(target_tree_row) else {
                    return;
                };
                let Some(dragged_source) =
                    find_source_entity(drag_drop.dropped, &parent_query, &tree_nodes)
                else {
                    return;
                };

                commands.trigger(TreeRowDropped {
                    entity: target_content,
                    dragged_source,
                    target_source: target_node.0,
                });
            },
        ),
    )
}

fn expand_toggle(has_children: bool, icon_font: &Handle<Font>) -> impl Bundle {
    let (text, font) = if has_children {
        (
            String::from(Icon::ChevronRight.unicode()),
            icon_font.clone(),
        )
    } else {
        (String::from(" "), Handle::default())
    };

    (
        TreeNodeExpandToggle,
        Node {
            width: px(TOGGLE_WIDTH),
            justify_content: JustifyContent::Center,
            ..default()
        },
        children![(
            Text::new(text),
            TextFont {
                font,
                font_size: tokens::FONT_SM,
                ..default()
            },
            TextColor(tokens::TEXT_SECONDARY),
        )],
        observe(
            |mut click: On<Pointer<Click>>,
             mut commands: Commands,
             parent_query: Query<&ChildOf>,
             tree_node_query: Query<(Entity, &TreeNodeExpanded)>| {
                if click.event.button != PointerButton::Primary {
                    return;
                }
                click.propagate(false);
                // Walk up ChildOf chain to find the nearest TreeNode ancestor
                let mut current = click.event_target();
                for _ in 0..4 {
                    if let Ok((entity, expanded)) = tree_node_query.get(current) {
                        commands
                            .entity(entity)
                            .insert(TreeNodeExpanded(!expanded.0));
                        return;
                    }
                    let Ok(&ChildOf(parent)) = parent_query.get(current) else {
                        return;
                    };
                    current = parent;
                }
            },
        ),
    )
}

/// Eye icon for toggling entity visibility.
fn visibility_toggle(source: Entity, icon_font: &Handle<Font>) -> impl Bundle {
    (
        TreeRowVisibilityToggle,
        Node {
            width: px(18.0),
            height: px(18.0),
            justify_content: JustifyContent::Center,
            align_items: AlignItems::Center,
            ..default()
        },
        children![(
            Text::new(String::from(Icon::Eye.unicode())),
            TextFont {
                font: icon_font.clone(),
                font_size: tokens::FONT_SM,
                ..default()
            },
            TextColor(tokens::TEXT_SECONDARY.with_alpha(0.4)),
        )],
        observe(
            move |mut click: On<Pointer<Click>>, mut commands: Commands| {
                if click.event.button != PointerButton::Primary {
                    return;
                }
                click.propagate(false);
                commands.trigger(TreeRowVisibilityToggled {
                    entity: click.event_target(),
                    source_entity: source,
                });
            },
        ),
        observe(
            |hover: On<Pointer<Over>>,
             children_query: Query<&Children>,
             mut text_color: Query<&mut TextColor>| {
                let entity = hover.event_target();
                if let Ok(children) = children_query.get(entity) {
                    for child in children.iter() {
                        if let Ok(mut color) = text_color.get_mut(child) {
                            color.0 = tokens::TEXT_SECONDARY;
                        }
                    }
                }
            },
        ),
        observe(
            |out: On<Pointer<Out>>,
             children_query: Query<&Children>,
             mut text_color: Query<&mut TextColor>| {
                let entity = out.event_target();
                if let Ok(children) = children_query.get(entity) {
                    for child in children.iter() {
                        if let Ok(mut color) = text_color.get_mut(child) {
                            color.0 = tokens::TEXT_SECONDARY.with_alpha(0.4);
                        }
                    }
                }
            },
        ),
    )
}

/// Icon indicating entity category (matches Figma reference).
fn category_dot(category: EntityCategory, icon_font: &Handle<Font>) -> impl Bundle {
    let color = category_color(category);
    let icon_char = match category {
        EntityCategory::Camera => Icon::Video,
        EntityCategory::Light => Icon::Lightbulb,
        EntityCategory::Mesh | EntityCategory::Scene => Icon::Box,
        EntityCategory::Entity => Icon::Dot,
    };
    (
        TreeRowDot,
        Node {
            width: px(DOT_COLUMN_WIDTH),
            height: px(DOT_COLUMN_WIDTH),
            justify_content: JustifyContent::Center,
            align_items: AlignItems::Center,
            ..default()
        },
        children![(
            Text::new(String::from(icon_char.unicode())),
            TextFont {
                font: icon_font.clone(),
                font_size: 12.0,
                ..default()
            },
            TextColor(color),
        )],
    )
}

/// Walk up the [`ChildOf`] chain from any deeply-nested UI entity until we find
/// a [`TreeNode`] ancestor, then return its source entity.
fn find_source_entity(
    entity: Entity,
    parents: &Query<&ChildOf>,
    tree_nodes: &Query<&TreeNode>,
) -> Option<Entity> {
    let mut current = entity;
    for _ in 0..8 {
        if let Ok(node) = tree_nodes.get(current) {
            return Some(node.0);
        }
        let Ok(&ChildOf(parent)) = parents.get(current) else {
            break;
        };
        current = parent;
    }
    None
}

/// Returns observers for the root tree container to handle deparenting (drop-to-root).
pub fn tree_container_drop_observers() -> impl Bundle {
    (
        observe(
            |mut drag_enter: On<Pointer<DragEnter>>, mut bg_query: Query<&mut BackgroundColor>| {
                drag_enter.propagate(false);
                if let Ok(mut bg) = bg_query.get_mut(drag_enter.event_target()) {
                    bg.0 = tokens::CONTAINER_DROP_TARGET_BG;
                }
            },
        ),
        observe(
            |mut drag_leave: On<Pointer<DragLeave>>, mut bg_query: Query<&mut BackgroundColor>| {
                drag_leave.propagate(false);
                if let Ok(mut bg) = bg_query.get_mut(drag_leave.event_target()) {
                    bg.0 = Color::NONE;
                }
            },
        ),
        observe(
            |mut drag_drop: On<Pointer<DragDrop>>,
             mut commands: Commands,
             parent_query: Query<&ChildOf>,
             tree_nodes: Query<&TreeNode>,
             mut bg_query: Query<&mut BackgroundColor>| {
                drag_drop.propagate(false);
                let container = drag_drop.event_target();

                // Revert background
                if let Ok(mut bg) = bg_query.get_mut(container) {
                    bg.0 = Color::NONE;
                }

                // Resolve the dragged entity to its scene source
                let Some(dragged_source) =
                    find_source_entity(drag_drop.dropped, &parent_query, &tree_nodes)
                else {
                    return;
                };

                commands.trigger(TreeRowDroppedOnRoot {
                    entity: container,
                    dragged_source,
                });
            },
        ),
    )
}

/// Keyboard navigation for tree views: arrow keys, Enter, F2, Delete
pub fn tree_keyboard_navigation(
    keyboard: Res<ButtonInput<KeyCode>>,
    mut focused: ResMut<TreeFocused>,
    tree_view: Query<&Children, With<TreeView>>,
    tree_nodes: Query<(Entity, &TreeNodeExpanded, &Children), With<TreeNode>>,
    tree_row_children: Query<&Children, With<TreeRowChildren>>,
    tree_row_contents: Query<Entity, With<TreeRowContent>>,
    node_query: Query<&Node>,
    mut commands: Commands,
    tree_node_query: Query<&TreeNode>,
    input_focus: Res<bevy::input_focus::InputFocus>,
) {
    // Skip tree keyboard navigation when a text input is focused
    // to avoid Enter/arrow keys interfering with text editing.
    if input_focus.0.is_some() {
        return;
    }
    // Collect all visible tree rows in order
    let visible_rows =
        collect_visible_rows(&tree_view, &tree_nodes, &tree_row_children, &node_query);

    if visible_rows.is_empty() {
        return;
    }

    let current_idx = focused
        .0
        .and_then(|f| visible_rows.iter().position(|&e| e == f));

    if keyboard.just_pressed(KeyCode::ArrowDown) {
        let next = match current_idx {
            Some(i) if i + 1 < visible_rows.len() => Some(visible_rows[i + 1]),
            None if !visible_rows.is_empty() => Some(visible_rows[0]),
            _ => focused.0,
        };
        focused.0 = next;
    }

    if keyboard.just_pressed(KeyCode::ArrowUp) {
        let prev = match current_idx {
            Some(i) if i > 0 => Some(visible_rows[i - 1]),
            None if !visible_rows.is_empty() => Some(*visible_rows.last().unwrap()),
            _ => focused.0,
        };
        focused.0 = prev;
    }

    if keyboard.just_pressed(KeyCode::ArrowLeft)
        && let Some(focused_entity) = focused.0
        && let Ok((entity, expanded, _)) = tree_nodes.get(focused_entity)
        && expanded.0
    {
        // Collapse the node
        commands.entity(entity).insert(TreeNodeExpanded(false));
    }
    // If already collapsed, could move to parent, but skipping for now.

    if keyboard.just_pressed(KeyCode::ArrowRight)
        && let Some(focused_entity) = focused.0
        && let Ok((entity, expanded, children)) = tree_nodes.get(focused_entity)
    {
        let has_children = children.iter().any(|c| tree_row_children.contains(c));
        if has_children && !expanded.0 {
            // Expand the node
            commands.entity(entity).insert(TreeNodeExpanded(true));
        }
    }

    // Enter/Space: select focused node
    if (keyboard.just_pressed(KeyCode::Enter) || keyboard.just_pressed(KeyCode::Space))
        && let Some(focused_entity) = focused.0
        && let Ok(tree_node) = tree_node_query.get(focused_entity)
    {
        // Find the TreeRowContent child to use as event target
        if let Ok((_, _, children)) = tree_nodes.get(focused_entity) {
            for child in children.iter() {
                if tree_row_contents.contains(child) {
                    commands.trigger(TreeRowClicked {
                        entity: child,
                        source_entity: tree_node.0,
                    });
                    break;
                }
            }
        }
    }

    // F2: start inline rename
    if keyboard.just_pressed(KeyCode::F2)
        && let Some(focused_entity) = focused.0
        && let Ok(tree_node) = tree_node_query.get(focused_entity)
    {
        commands.trigger(TreeRowStartRename {
            entity: focused_entity,
            source_entity: tree_node.0,
        });
    }
}

/// Collect all visible tree row entities in depth-first order
fn collect_visible_rows(
    tree_view: &Query<&Children, With<TreeView>>,
    tree_nodes: &Query<(Entity, &TreeNodeExpanded, &Children), With<TreeNode>>,
    tree_row_children: &Query<&Children, With<TreeRowChildren>>,
    node_query: &Query<&Node>,
) -> Vec<Entity> {
    let mut result = Vec::new();

    for view_children in tree_view.iter() {
        for child in view_children.iter() {
            collect_visible_rows_recursive(
                child,
                tree_nodes,
                tree_row_children,
                node_query,
                &mut result,
            );
        }
    }

    result
}

fn collect_visible_rows_recursive(
    entity: Entity,
    tree_nodes: &Query<(Entity, &TreeNodeExpanded, &Children), With<TreeNode>>,
    tree_row_children: &Query<&Children, With<TreeRowChildren>>,
    node_query: &Query<&Node>,
    result: &mut Vec<Entity>,
) {
    let Ok((_, expanded, children)) = tree_nodes.get(entity) else {
        return;
    };

    // Check if this node is visible (Display::Flex or default)
    if let Ok(node) = node_query.get(entity)
        && node.display == Display::None
    {
        return;
    }

    result.push(entity);

    if expanded.0 {
        // Find TreeRowChildren container and recurse into its children
        for child in children.iter() {
            if let Ok(row_children) = tree_row_children.get(child) {
                for grandchild in row_children.iter() {
                    collect_visible_rows_recursive(
                        grandchild,
                        tree_nodes,
                        tree_row_children,
                        node_query,
                        result,
                    );
                }
            }
        }
    }
}