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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
//! Main outliner widget implementation.
//!
//! This module provides the [`Outliner`] widget, which renders an interactive
//! hierarchical tree view with support for expansion, selection, editing, and
//! custom actions.
use crate::{
drag_drop::{calculate_drop_position, validate_drop, DragDropVisuals},
response::{DropEvent, OutlinerResponse},
state::OutlinerState,
style::Style,
traits::{ActionIcon, DropPosition, OutlinerActions, OutlinerNode},
};
/// The main outliner widget for rendering hierarchical tree structures.
///
/// This widget provides a complete tree view with support for:
/// - Hierarchical display with indentation
/// - Expand/collapse functionality for collection nodes
/// - Node selection and editing (rename)
/// - Action icons (visibility, lock, selection, custom)
/// - Keyboard navigation and shortcuts
///
/// # Examples
///
/// ```ignore
/// use egui_arbor::Outliner;
///
/// let response = Outliner::new("my_outliner")
/// .show(ui, &nodes, &mut actions);
///
/// if let Some(id) = response.selected() {
/// println!("Selected: {:?}", id);
/// }
/// ```
pub struct Outliner {
/// Unique identifier for this outliner instance.
id: egui::Id,
/// Visual styling configuration.
style: Style,
/// Visual configuration for drag-drop operations.
drag_drop_visuals: DragDropVisuals,
}
impl Outliner {
/// Creates a new outliner widget with default styling.
///
/// # Arguments
///
/// * `id` - A unique identifier for this outliner instance. This is used for
/// state persistence across frames.
///
/// # Examples
///
/// ```
/// use egui_arbor::Outliner;
///
/// let outliner = Outliner::new("my_outliner");
/// ```
pub fn new(id: impl Into<egui::Id>) -> Self {
Self {
id: id.into(),
style: Style::default(),
drag_drop_visuals: DragDropVisuals::default(),
}
}
/// Sets a custom style for this outliner.
///
/// # Arguments
///
/// * `style` - The style configuration to use
///
/// # Examples
///
/// ```
/// use egui_arbor::{Outliner, Style};
///
/// let outliner = Outliner::new("my_outliner")
/// .with_style(Style::default().with_indent(20.0));
/// ```
pub fn with_style(mut self, style: Style) -> Self {
self.style = style;
self
}
/// Sets custom drag-drop visuals for this outliner.
///
/// # Arguments
///
/// * `visuals` - The drag-drop visual configuration to use
///
/// # Examples
///
/// ```
/// use egui_arbor::{Outliner, DragDropVisuals};
///
/// let outliner = Outliner::new("my_outliner")
/// .with_drag_drop_visuals(DragDropVisuals::default());
/// ```
pub fn with_drag_drop_visuals(mut self, visuals: DragDropVisuals) -> Self {
self.drag_drop_visuals = visuals;
self
}
/// Renders the outliner widget and returns the response.
///
/// This is the main entry point for using the outliner. It renders all nodes
/// in the hierarchy and handles user interactions.
///
/// # Type Parameters
///
/// * `N` - The node type implementing [`OutlinerNode`]
/// * `A` - The actions handler implementing [`OutlinerActions<N>`]
///
/// # Arguments
///
/// * `ui` - The egui UI context to render into
/// * `nodes` - The root-level nodes to display
/// * `actions` - The actions handler for responding to user interactions
///
/// # Returns
///
/// An [`OutlinerResponse`] containing information about events that occurred
/// during rendering.
///
/// # Examples
///
/// ```ignore
/// let response = outliner.show(ui, &nodes, &mut actions);
///
/// if let Some(id) = response.selected() {
/// println!("Node selected: {:?}", id);
/// }
/// ```
pub fn show<N, A>(
self,
ui: &mut egui::Ui,
nodes: &[N],
actions: &mut A,
) -> OutlinerResponse<N::Id>
where
N: OutlinerNode,
N::Id: 'static,
A: OutlinerActions<N>,
{
// Load state from previous frame
let mut state = OutlinerState::load(ui.ctx(), self.id);
// Collect all visible node IDs in order for range selection
let mut visible_nodes = Vec::new();
Self::collect_visible_node_ids(nodes, &state, &mut visible_nodes);
// Render within a scroll area and capture the inner response
let scroll_output = egui::ScrollArea::vertical()
.auto_shrink([false, false])
.show(ui, |ui| {
// Track node rectangles for box selection
let mut node_rects: Vec<(N::Id, egui::Rect)> = Vec::new();
// Collect all currently selected nodes for potential multi-drag
let selected_nodes: Vec<N::Id> = visible_nodes.iter()
.filter(|id| actions.is_selected(id))
.cloned()
.collect();
// Create the outliner response wrapper
let mut outliner_response = OutlinerResponse::new(
ui.allocate_response(egui::vec2(ui.available_width(), 0.0), egui::Sense::hover())
);
// Render all root nodes
for node in nodes {
self.render_node(ui, node, 0, nodes, &mut state, actions, &mut outliner_response, &visible_nodes, &mut node_rects, &selected_nodes);
}
// Handle box selection in the background
let available_rect = ui.available_rect_before_wrap();
let bg_response = ui.allocate_rect(available_rect, egui::Sense::click_and_drag());
// Check if we're starting a box selection (clicking in empty space)
if bg_response.drag_started()
&& let Some(start_pos) = ui.ctx().pointer_interact_pos() {
// Only start box selection if not clicking on any node
let clicking_on_node = node_rects.iter().any(|(_, rect)| rect.contains(start_pos));
if !clicking_on_node {
state.start_box_selection(start_pos);
}
}
// Draw and update box selection
if let Some(box_sel) = state.box_selection()
&& let Some(current_pos) = ui.ctx().pointer_interact_pos() {
// Draw selection box
let min_x = box_sel.start_pos.x.min(current_pos.x);
let max_x = box_sel.start_pos.x.max(current_pos.x);
let min_y = box_sel.start_pos.y.min(current_pos.y);
let max_y = box_sel.start_pos.y.max(current_pos.y);
let selection_rect = egui::Rect::from_min_max(
egui::pos2(min_x, min_y),
egui::pos2(max_x, max_y),
);
// Draw the selection box
ui.painter().rect_stroke(
selection_rect,
0.0,
egui::Stroke::new(1.0, egui::Color32::from_rgb(100, 150, 255)),
egui::epaint::StrokeKind::Outside,
);
ui.painter().rect_filled(
selection_rect,
0.0,
egui::Color32::from_rgba_premultiplied(100, 150, 255, 30),
);
// Update selection based on box
if bg_response.dragged() {
let ctrl_or_cmd_pressed = ui.input(|i| i.modifiers.command || i.modifiers.ctrl);
// If not holding ctrl/cmd, deselect all first
if !ctrl_or_cmd_pressed {
for id in &visible_nodes {
if actions.is_selected(id) {
actions.on_select(id, false);
}
}
}
// Select nodes that intersect with the box
for (node_id, node_rect) in &node_rects {
if selection_rect.intersects(*node_rect) {
actions.on_select(node_id, true);
}
}
outliner_response.changed = true;
}
}
if bg_response.drag_stopped() {
state.end_box_selection();
}
outliner_response
});
// Store state for next frame
state.store(ui.ctx(), self.id);
scroll_output.inner
}
/// Collects all visible node IDs in order (depth-first traversal).
///
/// This is used for shift-click range selection.
fn collect_visible_node_ids<N>(
nodes: &[N],
state: &OutlinerState<N::Id>,
result: &mut Vec<N::Id>,
) where
N: OutlinerNode,
{
for node in nodes {
result.push(node.id());
if node.is_collection() && state.is_expanded(&node.id()) {
Self::collect_visible_node_ids(node.children(), state, result);
}
}
}
/// Renders a single node and its children recursively.
///
/// This method handles the complete rendering of a node including:
/// - Indentation based on depth
/// - Expand/collapse arrow (for collections)
/// - Node icon (if provided)
/// - Node label (clickable, editable)
/// - Action icons
/// - Recursive rendering of children (if expanded)
#[allow(clippy::too_many_arguments)]
fn render_node<N, A>(
&self,
ui: &mut egui::Ui,
node: &N,
depth: usize,
all_nodes: &[N],
state: &mut OutlinerState<N::Id>,
actions: &mut A,
response: &mut OutlinerResponse<N::Id>,
visible_nodes: &[N::Id],
node_rects: &mut Vec<(N::Id, egui::Rect)>,
selected_nodes: &[N::Id],
) where
N: OutlinerNode,
A: OutlinerActions<N>,
{
let node_id = node.id();
let is_collection = node.is_collection();
let is_expanded = state.is_expanded(&node_id);
let is_editing = state.is_editing(&node_id);
let is_selected = actions.is_selected(&node_id);
// Check drag-drop state
let is_dragging = state.drag_drop().is_dragging_node(&node_id);
let is_hover_target = state.drag_drop().is_hover_target(&node_id);
let drop_position = state.drag_drop().current_drop_position();
// Start horizontal layout for this row
let row_output = ui.horizontal(|ui| {
// Calculate space needed for action icons upfront
let num_action_icons = node.action_icons().len();
let icons_width = num_action_icons as f32 * (self.style.action_icon_size + self.style.icon_spacing);
// Add indentation
ui.add_space(depth as f32 * self.style.indent);
// Render expand/collapse arrow for collections
if is_collection {
let expand_response = self.render_expand_icon(ui, is_expanded);
if expand_response.clicked() {
state.toggle_expanded(&node_id);
response.changed = true;
}
} else {
// Add spacing to align with non-collection nodes
ui.add_space(self.style.expand_icon_size + self.style.icon_spacing);
}
// Render node icon (placeholder for now)
if node.icon().is_some() {
ui.label("📄");
ui.add_space(self.style.icon_spacing);
}
// Render node label (or text edit if editing)
let label_response = self.render_node_label(
ui,
node,
is_editing,
is_selected,
icons_width,
state,
actions,
response,
);
// Handle label interactions
if !is_editing {
if label_response.clicked() {
// Check for modifier keys
let shift_pressed = ui.input(|i| i.modifiers.shift);
let ctrl_or_cmd_pressed = ui.input(|i| i.modifiers.command || i.modifiers.ctrl);
if shift_pressed && state.last_selected().is_some() {
// Shift-click: select range
let last_id = state.last_selected().unwrap();
if let (Some(start_idx), Some(end_idx)) = (
visible_nodes.iter().position(|id| id == last_id),
visible_nodes.iter().position(|id| id == &node_id),
) {
let (min_idx, max_idx) = if start_idx < end_idx {
(start_idx, end_idx)
} else {
(end_idx, start_idx)
};
// Select all nodes in range
for id in &visible_nodes[min_idx..=max_idx] {
actions.on_select(id, true);
}
}
response.changed = true;
} else if ctrl_or_cmd_pressed {
// Ctrl/Cmd-click: toggle selection without clearing others
let new_selection = !is_selected;
actions.on_select(&node_id, new_selection);
if new_selection {
state.set_last_selected(Some(node_id.clone()));
}
response.selected = Some(node_id.clone());
response.changed = true;
} else {
// Normal click: clear other selections and select this one
// First, deselect all nodes
for id in visible_nodes {
if actions.is_selected(id) {
actions.on_select(id, false);
}
}
// Then select this node
actions.on_select(&node_id, true);
state.set_last_selected(Some(node_id.clone()));
response.selected = Some(node_id.clone());
response.changed = true;
}
}
if label_response.double_clicked() {
state.start_editing(node_id.clone(), node.name().to_string());
response.double_clicked = Some(node_id.clone());
response.changed = true;
}
if label_response.secondary_clicked() {
response.context_menu = Some(node_id.clone());
}
}
// Render action icons (right-aligned)
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
self.render_action_icons(ui, node, actions);
});
// Return the label response so we can use it for drag detection
label_response
});
let row_rect = row_output.response.rect;
let label_response = row_output.inner;
// Store the node rectangle for box selection
node_rects.push((node_id.clone(), row_rect));
// Use the label response for drag detection
let drag_response = label_response;
// Handle drag-drop interactions
if !is_editing {
// Detect drag start
if drag_response.drag_started() {
state.drag_drop_mut().start_drag(node_id.clone());
response.drag_started = Some(node_id.clone());
// Collect all selected nodes for multi-drag
// If the dragged node is selected, include all selected nodes
// Otherwise, just drag this single node
let dragging_nodes = if actions.is_selected(&node_id) {
selected_nodes.to_vec()
} else {
vec![node_id.clone()]
};
state.set_dragging_nodes(dragging_nodes.clone());
response.dragging_nodes = dragging_nodes;
response.changed = true;
}
// Handle hover for drop target detection
if state.drag_drop().is_dragging() && !is_dragging {
// Check if cursor is hovering over this row
if let Some(cursor_pos) = ui.ctx().pointer_hover_pos()
&& row_rect.contains(cursor_pos) {
let position = calculate_drop_position(
cursor_pos.y,
row_rect,
is_collection,
);
// Validate the drop
if let Some(source_id) = state.drag_drop().dragging_id() {
let is_valid = validate_drop(
source_id,
&node_id,
position,
node,
|target, source| Self::is_descendant_of_impl(all_nodes, target, source),
);
if is_valid {
state.drag_drop_mut().update_hover(node_id.clone(), position);
} else {
state.drag_drop_mut().clear_hover();
}
}
}
}
// Handle drop
if state.drag_drop().is_dragging() && drag_response.drag_stopped() {
if let Some((source_id, target_id, position)) = state.drag_drop_mut().end_drag() {
// Invoke the on_move callback
actions.on_move(&source_id, &target_id, position);
// Get the dragging nodes and add them to the response
response.dragging_nodes = state.dragging_nodes().to_vec();
// Record the drop event in the response
response.drop_event = Some(DropEvent::new(source_id, target_id, position));
response.changed = true;
// Clear dragging nodes after drop
state.clear_dragging_nodes();
} else {
state.drag_drop_mut().cancel_drag();
state.clear_dragging_nodes();
}
}
}
// Draw visual feedback for drag-drop
if is_dragging {
self.drag_drop_visuals.draw_drag_source(ui.painter(), row_rect);
}
if is_hover_target
&& let Some(position) = drop_position {
match position {
DropPosition::Before | DropPosition::After => {
self.drag_drop_visuals.draw_drop_line(ui.painter(), row_rect, position);
}
DropPosition::Inside => {
self.drag_drop_visuals.draw_drop_highlight(ui.painter(), row_rect);
}
}
}
// Render children if this is an expanded collection
if is_collection && is_expanded {
for child in node.children() {
self.render_node(ui, child, depth + 1, all_nodes, state, actions, response, visible_nodes, node_rects, selected_nodes);
}
}
}
/// Helper function to check if target_id is a descendant of source_id.
///
/// This is used to prevent circular dependencies in drag-drop operations.
fn is_descendant_of_impl<N>(all_nodes: &[N], target_id: &N::Id, source_id: &N::Id) -> bool
where
N: OutlinerNode,
{
// Find the source node
if let Some(source_node) = Self::find_node_by_id_impl(all_nodes, source_id) {
return Self::contains_descendant_impl(source_node, target_id);
}
false
}
/// Helper function to find a node by its ID.
fn find_node_by_id_impl<'a, N>(nodes: &'a [N], id: &N::Id) -> Option<&'a N>
where
N: OutlinerNode,
{
for node in nodes {
if node.id() == *id {
return Some(node);
}
if let Some(found) = Self::find_node_by_id_impl(node.children(), id) {
return Some(found);
}
}
None
}
/// Helper function to check if a node contains a descendant with the given ID.
fn contains_descendant_impl<N>(node: &N, target_id: &N::Id) -> bool
where
N: OutlinerNode,
{
for child in node.children() {
if child.id() == *target_id {
return true;
}
if Self::contains_descendant_impl(child, target_id) {
return true;
}
}
false
}
/// Renders the expand/collapse arrow icon.
///
/// Returns the response from the arrow button/label.
fn render_expand_icon(&self, ui: &mut egui::Ui, is_expanded: bool) -> egui::Response {
let icon_text = if is_expanded {
self.style.expand_icon_style.expanded_str()
} else {
self.style.expand_icon_style.collapsed_str()
};
let (rect, response) = ui.allocate_exact_size(
egui::vec2(self.style.expand_icon_size, self.style.row_height),
egui::Sense::click(),
);
if ui.is_rect_visible(rect) {
let visuals = ui.style().interact(&response);
let text_color = visuals.text_color();
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
icon_text,
egui::FontId::proportional(self.style.expand_icon_size),
text_color,
);
}
response
}
/// Renders the node label, either as a selectable label or text edit.
///
/// Returns the response from the label or text edit.
#[allow(clippy::too_many_arguments)]
fn render_node_label<N, A>(
&self,
ui: &mut egui::Ui,
node: &N,
is_editing: bool,
is_selected: bool,
icons_width: f32,
state: &mut OutlinerState<N::Id>,
actions: &mut A,
response: &mut OutlinerResponse<N::Id>,
) -> egui::Response
where
N: OutlinerNode,
A: OutlinerActions<N>,
{
if is_editing {
// Render text edit for renaming
let text_edit_response = ui.text_edit_singleline(state.editing_text_mut());
// Check for Enter key to confirm
if ui.input(|i| i.key_pressed(egui::Key::Enter)) {
let text = state.editing_text().to_string();
actions.on_rename(&node.id(), text.clone());
state.stop_editing();
response.renamed = Some((node.id(), text));
response.changed = true;
}
// Check for Escape key to cancel
if ui.input(|i| i.key_pressed(egui::Key::Escape)) {
state.stop_editing();
response.changed = true;
}
// Auto-focus the text edit
text_edit_response.request_focus();
text_edit_response
} else {
// Render selectable label
let label_text = node.name();
// Create a custom selectable label with our styling
// Include drag sensing so we can detect drag operations on the label
// Reserve space for action icons to prevent layout shifts
let available_width = ui.available_width();
let label_width = (available_width - icons_width - 10.0).max(50.0);
let (rect, label_response) = ui.allocate_exact_size(
egui::vec2(label_width, self.style.row_height),
egui::Sense::click_and_drag(),
);
if ui.is_rect_visible(rect) {
let visuals = ui.style().interact(&label_response);
// Draw background if selected or hovered
if is_selected {
let bg_color = self.style.selection_color
.unwrap_or_else(|| ui.visuals().selection.bg_fill);
ui.painter().rect_filled(rect, 2.0, bg_color);
} else if label_response.hovered() {
let bg_color = self.style.hover_color
.unwrap_or_else(|| ui.visuals().widgets.hovered.bg_fill);
ui.painter().rect_filled(rect, 2.0, bg_color);
}
// Draw text
let text_color = if is_selected {
ui.visuals().selection.stroke.color
} else {
visuals.text_color()
};
ui.painter().text(
rect.left_center() + egui::vec2(4.0, 0.0),
egui::Align2::LEFT_CENTER,
label_text,
egui::FontId::proportional(self.style.row_height * 0.8),
text_color,
);
}
label_response
}
}
/// Collects all descendant node IDs recursively.
///
/// This helper method traverses the tree starting from the given node
/// and collects all descendant IDs into a vector.
fn collect_descendant_ids<N>(node: &N) -> Vec<N::Id>
where
N: OutlinerNode,
{
let mut ids = Vec::new();
for child in node.children() {
ids.push(child.id());
ids.extend(Self::collect_descendant_ids(child));
}
ids
}
/// Renders the action icons for a node.
///
/// Icons are rendered right-to-left in the order they appear in the
/// node's action_icons() list.
fn render_action_icons<N, A>(&self, ui: &mut egui::Ui, node: &N, actions: &mut A)
where
N: OutlinerNode,
A: OutlinerActions<N>,
{
let node_id = node.id();
let is_collection = node.is_collection();
for action_icon in node.action_icons().iter().rev() {
match action_icon {
ActionIcon::Visibility => {
let is_visible = actions.is_visible(&node_id);
let icon_text = if is_visible { "👁" } else { "🚫" };
let (rect, icon_response) = ui.allocate_exact_size(
egui::vec2(self.style.action_icon_size, self.style.row_height),
egui::Sense::click(),
);
if ui.is_rect_visible(rect) {
let visuals = ui.style().interact(&icon_response);
let text_color = if is_visible {
visuals.text_color()
} else {
visuals.text_color().gamma_multiply(0.5)
};
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
icon_text,
egui::FontId::proportional(self.style.action_icon_size * 0.8),
text_color,
);
}
// Handle click to toggle visibility
if icon_response.clicked() {
// Determine the new visibility state (opposite of current)
let new_visibility = !is_visible;
// Set the parent to the new state
if new_visibility != is_visible {
actions.on_visibility_toggle(&node_id);
}
// If this is a collection, set all children to the same state
if is_collection {
for child_id in Self::collect_descendant_ids(node) {
let child_visible = actions.is_visible(&child_id);
// Only toggle if the child's state differs from target
if child_visible != new_visibility {
actions.on_visibility_toggle(&child_id);
}
}
}
}
}
ActionIcon::Lock => {
let is_locked = actions.is_locked(&node_id);
let icon_text = if is_locked { "🔒" } else { "🔓" };
let (rect, icon_response) = ui.allocate_exact_size(
egui::vec2(self.style.action_icon_size, self.style.row_height),
egui::Sense::click(),
);
if ui.is_rect_visible(rect) {
let visuals = ui.style().interact(&icon_response);
let text_color = if is_locked {
visuals.text_color()
} else {
visuals.text_color().gamma_multiply(0.5)
};
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
icon_text,
egui::FontId::proportional(self.style.action_icon_size * 0.8),
text_color,
);
}
// Handle click to toggle lock state
if icon_response.clicked() {
actions.on_lock_toggle(&node_id);
// If this is a collection, apply to all children
if is_collection {
for child_id in Self::collect_descendant_ids(node) {
actions.on_lock_toggle(&child_id);
}
}
}
}
ActionIcon::Selection => {
let is_selected = actions.is_selected(&node_id);
let icon_text = if is_selected { "☑" } else { "☐" };
let (rect, icon_response) = ui.allocate_exact_size(
egui::vec2(self.style.action_icon_size, self.style.row_height),
egui::Sense::click(),
);
if ui.is_rect_visible(rect) {
let visuals = ui.style().interact(&icon_response);
let text_color = if is_selected {
visuals.text_color()
} else {
visuals.text_color().gamma_multiply(0.5)
};
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
icon_text,
egui::FontId::proportional(self.style.action_icon_size * 0.8),
text_color,
);
}
// Handle click to toggle selection
if icon_response.clicked() {
// Determine the new selection state based on current state
let current_state = actions.is_selected(&node_id);
let new_state = !current_state;
// Apply the new state to the parent
actions.on_select(&node_id, new_state);
// If this is a collection, apply the same state to all children
if is_collection {
for child_id in Self::collect_descendant_ids(node) {
actions.on_select(&child_id, new_state);
}
}
}
}
ActionIcon::Custom { icon, tooltip } => {
let (rect, icon_response) = ui.allocate_exact_size(
egui::vec2(self.style.action_icon_size, self.style.row_height),
egui::Sense::click(),
);
if ui.is_rect_visible(rect) {
let visuals = ui.style().interact(&icon_response);
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
icon.as_str(),
egui::FontId::proportional(self.style.action_icon_size * 0.8),
visuals.text_color(),
);
}
// Handle click for custom action
let clicked = icon_response.clicked();
// Add tooltip if provided (consumes the response)
let _icon_response = if let Some(tip) = tooltip {
icon_response.on_hover_text(tip)
} else {
icon_response
};
if clicked {
actions.on_custom_action(&node_id, icon.as_str());
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::{OutlinerNode, OutlinerActions, IconType, ActionIcon};
use std::collections::{HashSet, HashMap};
// Mock node for testing
#[derive(Debug, Clone, PartialEq)]
struct TestNode {
id: u64,
name: String,
is_collection: bool,
children: Vec<TestNode>,
}
impl OutlinerNode for TestNode {
type Id = u64;
fn id(&self) -> Self::Id {
self.id
}
fn name(&self) -> &str {
&self.name
}
fn is_collection(&self) -> bool {
self.is_collection
}
fn children(&self) -> &[Self] {
&self.children
}
fn children_mut(&mut self) -> &mut Vec<Self> {
&mut self.children
}
fn icon(&self) -> Option<IconType> {
if self.is_collection {
Some(IconType::Collection)
} else {
Some(IconType::Entity)
}
}
fn action_icons(&self) -> Vec<ActionIcon> {
vec![ActionIcon::Visibility, ActionIcon::Lock, ActionIcon::Selection]
}
}
impl TestNode {
fn new(id: u64, name: &str, is_collection: bool) -> Self {
Self {
id,
name: name.to_string(),
is_collection,
children: Vec::new(),
}
}
fn with_children(mut self, children: Vec<TestNode>) -> Self {
self.children = children;
self
}
}
// Mock actions handler for testing
struct TestActions {
selected: HashSet<u64>,
visible: HashSet<u64>,
locked: HashSet<u64>,
renamed: HashMap<u64, String>,
moved: Vec<(u64, u64, DropPosition)>,
custom_actions: Vec<(u64, String)>,
}
impl TestActions {
fn new() -> Self {
Self {
selected: HashSet::new(),
visible: HashSet::new(),
locked: HashSet::new(),
renamed: HashMap::new(),
moved: Vec::new(),
custom_actions: Vec::new(),
}
}
}
impl OutlinerActions<TestNode> for TestActions {
fn on_rename(&mut self, id: &u64, new_name: String) {
self.renamed.insert(*id, new_name);
}
fn on_move(&mut self, id: &u64, target: &u64, position: DropPosition) {
self.moved.push((*id, *target, position));
}
fn on_select(&mut self, id: &u64, selected: bool) {
if selected {
self.selected.insert(*id);
} else {
self.selected.remove(id);
}
}
fn is_selected(&self, id: &u64) -> bool {
self.selected.contains(id)
}
fn is_visible(&self, id: &u64) -> bool {
self.visible.contains(id)
}
fn is_locked(&self, id: &u64) -> bool {
self.locked.contains(id)
}
fn on_visibility_toggle(&mut self, id: &u64) {
if self.visible.contains(id) {
self.visible.remove(id);
} else {
self.visible.insert(*id);
}
}
fn on_lock_toggle(&mut self, id: &u64) {
if self.locked.contains(id) {
self.locked.remove(id);
} else {
self.locked.insert(*id);
}
}
fn on_selection_toggle(&mut self, id: &u64) {
let is_selected = self.is_selected(id);
self.on_select(id, !is_selected);
}
fn on_custom_action(&mut self, id: &u64, icon: &str) {
self.custom_actions.push((*id, icon.to_string()));
}
}
#[test]
fn test_collect_visible_node_ids_flat() {
let nodes = vec![
TestNode::new(1, "Node1", false),
TestNode::new(2, "Node2", false),
TestNode::new(3, "Node3", false),
];
let state = OutlinerState::<u64>::default();
let mut result = Vec::new();
Outliner::collect_visible_node_ids(&nodes, &state, &mut result);
assert_eq!(result, vec![1, 2, 3]);
}
#[test]
fn test_collect_visible_node_ids_with_collapsed_children() {
let nodes = vec![
TestNode::new(1, "Node1", true).with_children(vec![
TestNode::new(2, "Child1", false),
TestNode::new(3, "Child2", false),
]),
];
let state = OutlinerState::<u64>::default();
let mut result = Vec::new();
Outliner::collect_visible_node_ids(&nodes, &state, &mut result);
// Only parent should be visible when collapsed
assert_eq!(result, vec![1]);
}
#[test]
fn test_collect_visible_node_ids_with_expanded_children() {
let nodes = vec![
TestNode::new(1, "Node1", true).with_children(vec![
TestNode::new(2, "Child1", false),
TestNode::new(3, "Child2", false),
]),
];
let mut state = OutlinerState::<u64>::default();
state.set_expanded(&1, true);
let mut result = Vec::new();
Outliner::collect_visible_node_ids(&nodes, &state, &mut result);
// Parent and children should be visible when expanded
assert_eq!(result, vec![1, 2, 3]);
}
#[test]
fn test_collect_visible_node_ids_nested() {
let nodes = vec![
TestNode::new(1, "Node1", true).with_children(vec![
TestNode::new(2, "Child1", true).with_children(vec![
TestNode::new(3, "GrandChild1", false),
]),
]),
];
let mut state = OutlinerState::<u64>::default();
state.set_expanded(&1, true);
state.set_expanded(&2, true);
let mut result = Vec::new();
Outliner::collect_visible_node_ids(&nodes, &state, &mut result);
assert_eq!(result, vec![1, 2, 3]);
}
#[test]
fn test_find_node_by_id_root_level() {
let nodes = vec![
TestNode::new(1, "Node1", false),
TestNode::new(2, "Node2", false),
];
let found = Outliner::find_node_by_id_impl(&nodes, &1);
assert!(found.is_some());
assert_eq!(found.unwrap().id(), 1);
let found = Outliner::find_node_by_id_impl(&nodes, &2);
assert!(found.is_some());
assert_eq!(found.unwrap().id(), 2);
}
#[test]
fn test_find_node_by_id_nested() {
let nodes = vec![
TestNode::new(1, "Node1", true).with_children(vec![
TestNode::new(2, "Child1", false),
TestNode::new(3, "Child2", true).with_children(vec![
TestNode::new(4, "GrandChild1", false),
]),
]),
];
let found = Outliner::find_node_by_id_impl(&nodes, &4);
assert!(found.is_some());
assert_eq!(found.unwrap().id(), 4);
}
#[test]
fn test_find_node_by_id_not_found() {
let nodes = vec![
TestNode::new(1, "Node1", false),
];
let found = Outliner::find_node_by_id_impl(&nodes, &999);
assert!(found.is_none());
}
#[test]
fn test_contains_descendant_direct_child() {
let node = TestNode::new(1, "Parent", true).with_children(vec![
TestNode::new(2, "Child", false),
]);
assert!(Outliner::contains_descendant_impl(&node, &2));
assert!(!Outliner::contains_descendant_impl(&node, &999));
}
#[test]
fn test_contains_descendant_nested() {
let node = TestNode::new(1, "Parent", true).with_children(vec![
TestNode::new(2, "Child", true).with_children(vec![
TestNode::new(3, "GrandChild", false),
]),
]);
assert!(Outliner::contains_descendant_impl(&node, &2));
assert!(Outliner::contains_descendant_impl(&node, &3));
assert!(!Outliner::contains_descendant_impl(&node, &1));
}
#[test]
fn test_is_descendant_of_impl() {
let nodes = vec![
TestNode::new(1, "Parent", true).with_children(vec![
TestNode::new(2, "Child", true).with_children(vec![
TestNode::new(3, "GrandChild", false),
]),
]),
];
// Node 2 is a descendant of node 1
assert!(Outliner::is_descendant_of_impl(&nodes, &2, &1));
// Node 3 is a descendant of node 1
assert!(Outliner::is_descendant_of_impl(&nodes, &3, &1));
// Node 3 is a descendant of node 2
assert!(Outliner::is_descendant_of_impl(&nodes, &3, &2));
// Node 1 is not a descendant of node 2
assert!(!Outliner::is_descendant_of_impl(&nodes, &1, &2));
}
#[test]
fn test_collect_descendant_ids() {
let node = TestNode::new(1, "Parent", true).with_children(vec![
TestNode::new(2, "Child1", false),
TestNode::new(3, "Child2", true).with_children(vec![
TestNode::new(4, "GrandChild", false),
]),
]);
let ids = Outliner::collect_descendant_ids(&node);
assert_eq!(ids.len(), 3);
assert!(ids.contains(&2));
assert!(ids.contains(&3));
assert!(ids.contains(&4));
}
#[test]
fn test_collect_descendant_ids_empty() {
let node = TestNode::new(1, "Leaf", false);
let ids = Outliner::collect_descendant_ids(&node);
assert!(ids.is_empty());
}
#[test]
fn test_outliner_new() {
let outliner = Outliner::new("test_outliner");
// Just verify it can be created
assert_eq!(outliner.id, egui::Id::new("test_outliner"));
}
#[test]
fn test_outliner_with_style() {
let style = Style::default().with_indent(30.0);
let outliner = Outliner::new("test").with_style(style.clone());
assert_eq!(outliner.style.indent, 30.0);
}
#[test]
fn test_outliner_with_drag_drop_visuals() {
let visuals = DragDropVisuals::default();
let outliner = Outliner::new("test").with_drag_drop_visuals(visuals);
// Just verify it can be created with custom visuals
assert_eq!(outliner.drag_drop_visuals.drop_line_thickness, 2.0);
}
}