egui-arbor 0.2.1

A tree outliner widget for egui - hierarchical data visualization and editing
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
//! Drag-and-drop functionality for the outliner widget.
//!
//! This module provides types and utilities for implementing drag-and-drop
//! operations in the outliner, including state tracking, drop validation,
//! and visual feedback.

use crate::traits::{DropPosition, OutlinerNode};
use std::hash::Hash;

/// Tracks the current drag-and-drop state for the outliner.
///
/// This structure maintains information about ongoing drag operations,
/// including which node is being dragged and potential drop targets.
#[derive(Debug, Clone)]
pub struct DragDropState<Id>
where
    Id: Hash + Eq + Clone,
{
    /// The ID of the node currently being dragged, if any.
    pub dragging: Option<Id>,

    /// The ID of the node currently being hovered over as a potential drop target.
    pub hover_target: Option<Id>,

    /// The position where the dragged node would be dropped relative to the hover target.
    pub drop_position: Option<DropPosition>,
}

impl<Id> Default for DragDropState<Id>
where
    Id: Hash + Eq + Clone,
{
    fn default() -> Self {
        Self {
            dragging: None,
            hover_target: None,
            drop_position: None,
        }
    }
}

impl<Id> DragDropState<Id>
where
    Id: Hash + Eq + Clone,
{
    /// Creates a new drag-drop state with no active operations.
    pub fn new() -> Self {
        Self::default()
    }

    /// Starts dragging a node.
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the node being dragged
    pub fn start_drag(&mut self, id: Id) {
        self.dragging = Some(id);
        self.hover_target = None;
        self.drop_position = None;
    }

    /// Updates the hover target and drop position.
    ///
    /// # Arguments
    ///
    /// * `target` - The ID of the node being hovered over
    /// * `position` - The position where the drop would occur
    pub fn update_hover(&mut self, target: Id, position: DropPosition) {
        self.hover_target = Some(target);
        self.drop_position = Some(position);
    }

    /// Clears the hover state.
    pub fn clear_hover(&mut self) {
        self.hover_target = None;
        self.drop_position = None;
    }

    /// Ends the drag operation and returns the drop information if valid.
    ///
    /// # Returns
    ///
    /// A tuple of `(source_id, target_id, position)` if a valid drop occurred,
    /// or `None` if the drag was cancelled or invalid.
    pub fn end_drag(&mut self) -> Option<(Id, Id, DropPosition)> {
        let result = if let (Some(source), Some(target), Some(position)) =
            (&self.dragging, &self.hover_target, &self.drop_position)
        {
            Some((source.clone(), target.clone(), *position))
        } else {
            None
        };

        self.dragging = None;
        self.hover_target = None;
        self.drop_position = None;

        result
    }

    /// Cancels the current drag operation.
    pub fn cancel_drag(&mut self) {
        self.dragging = None;
        self.hover_target = None;
        self.drop_position = None;
    }

    /// Returns whether a drag operation is currently active.
    pub fn is_dragging(&self) -> bool {
        self.dragging.is_some()
    }

    /// Returns the ID of the node being dragged, if any.
    pub fn dragging_id(&self) -> Option<&Id> {
        self.dragging.as_ref()
    }

    /// Returns whether the given node is currently being dragged.
    pub fn is_dragging_node(&self, id: &Id) -> bool {
        self.dragging.as_ref() == Some(id)
    }

    /// Returns whether the given node is the current hover target.
    pub fn is_hover_target(&self, id: &Id) -> bool {
        self.hover_target.as_ref() == Some(id)
    }

    /// Returns the current drop position, if any.
    pub fn current_drop_position(&self) -> Option<DropPosition> {
        self.drop_position
    }
}

/// Validates whether a drop operation is allowed.
///
/// This function checks if dropping `source` onto `target` at the given `position`
/// would create a valid hierarchy without circular dependencies.
///
/// # Arguments
///
/// * `source_id` - The ID of the node being dragged
/// * `target_id` - The ID of the potential drop target
/// * `position` - Where the source would be placed relative to the target
/// * `target_node` - The target node (used to check if it's a collection for Inside drops)
/// * `is_descendant` - A function that checks if the first ID is a descendant of the second
///
/// # Returns
///
/// `true` if the drop is valid, `false` otherwise.
pub fn validate_drop<N, F>(
    source_id: &N::Id,
    target_id: &N::Id,
    position: DropPosition,
    target_node: &N,
    is_descendant: F,
) -> bool
where
    N: OutlinerNode,
    F: Fn(&N::Id, &N::Id) -> bool,
{
    // Can't drop a node onto itself
    if source_id == target_id {
        return false;
    }

    // Can't drop a parent into its own descendant (would create a cycle)
    if is_descendant(target_id, source_id) {
        return false;
    }

    // For Inside drops, target must be a collection
    if position == DropPosition::Inside && !target_node.is_collection() {
        return false;
    }

    true
}

/// Determines the drop position based on the cursor position within a node's rect.
///
/// This function divides the node's vertical space into three zones:
/// - Top 25%: Before
/// - Middle 50%: Inside (if the node is a collection)
/// - Bottom 25%: After
///
/// # Arguments
///
/// * `cursor_y` - The Y coordinate of the cursor
/// * `rect` - The rectangle of the node being hovered over
/// * `is_collection` - Whether the target node can accept children
///
/// # Returns
///
/// The appropriate [`DropPosition`] based on cursor location.
pub fn calculate_drop_position(
    cursor_y: f32,
    rect: egui::Rect,
    is_collection: bool,
) -> DropPosition {
    let relative_y = (cursor_y - rect.top()) / rect.height();

    if relative_y < 0.25 {
        DropPosition::Before
    } else if relative_y > 0.75 {
        DropPosition::After
    } else if is_collection {
        DropPosition::Inside
    } else {
        // For non-collections in the middle zone, prefer After
        DropPosition::After
    }
}

/// Visual feedback configuration for drag-drop operations.
#[derive(Debug, Clone)]
pub struct DragDropVisuals {
    /// Color for the drop indicator line (Before/After positions).
    pub drop_line_color: egui::Color32,

    /// Thickness of the drop indicator line.
    pub drop_line_thickness: f32,

    /// Color for highlighting the drop target (Inside position).
    pub drop_target_color: egui::Color32,

    /// Color for the dragged node while dragging.
    pub drag_source_color: egui::Color32,

    /// Opacity multiplier for invalid drop targets.
    pub invalid_target_opacity: f32,
}

impl Default for DragDropVisuals {
    fn default() -> Self {
        Self {
            drop_line_color: egui::Color32::from_rgb(100, 150, 255),
            drop_line_thickness: 2.0,
            drop_target_color: egui::Color32::from_rgba_unmultiplied(100, 150, 255, 50),
            drag_source_color: egui::Color32::from_rgba_unmultiplied(100, 150, 255, 100),
            invalid_target_opacity: 0.3,
        }
    }
}

impl DragDropVisuals {
    /// Draws a drop indicator line at the specified position.
    ///
    /// # Arguments
    ///
    /// * `painter` - The egui painter to draw with
    /// * `rect` - The rectangle of the target node
    /// * `position` - Whether to draw the line before or after the node
    pub fn draw_drop_line(
        &self,
        painter: &egui::Painter,
        rect: egui::Rect,
        position: DropPosition,
    ) {
        let y = match position {
            DropPosition::Before => rect.top(),
            DropPosition::After => rect.bottom(),
            DropPosition::Inside => return, // Inside uses highlight instead
        };

        let start = egui::pos2(rect.left(), y);
        let end = egui::pos2(rect.right(), y);

        painter.line_segment(
            [start, end],
            egui::Stroke::new(self.drop_line_thickness, self.drop_line_color),
        );
    }

    /// Draws a highlight for an Inside drop target.
    ///
    /// # Arguments
    ///
    /// * `painter` - The egui painter to draw with
    /// * `rect` - The rectangle of the target node
    pub fn draw_drop_highlight(&self, painter: &egui::Painter, rect: egui::Rect) {
        painter.rect_filled(rect, 2.0, self.drop_target_color);
    }

    /// Draws visual feedback for the node being dragged.
    ///
    /// # Arguments
    ///
    /// * `painter` - The egui painter to draw with
    /// * `rect` - The rectangle of the dragged node
    pub fn draw_drag_source(&self, painter: &egui::Painter, rect: egui::Rect) {
        painter.rect_filled(rect, 2.0, self.drag_source_color);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::{OutlinerNode, IconType, ActionIcon};

    // 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]
        }
    }

    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
        }
    }

    #[test]
    fn test_drag_drop_state_default() {
        let state = DragDropState::<u64>::default();
        assert!(!state.is_dragging());
        assert_eq!(state.dragging_id(), None);
        assert_eq!(state.current_drop_position(), None);
    }

    #[test]
    fn test_drag_drop_state_new() {
        let state = DragDropState::<u64>::new();
        assert!(!state.is_dragging());
        assert_eq!(state.dragging_id(), None);
    }

    #[test]
    fn test_start_drag() {
        let mut state = DragDropState::<u64>::new();
        state.start_drag(42);
        
        assert!(state.is_dragging());
        assert_eq!(state.dragging_id(), Some(&42));
        assert!(state.is_dragging_node(&42));
        assert!(!state.is_dragging_node(&99));
    }

    #[test]
    fn test_update_hover() {
        let mut state = DragDropState::<u64>::new();
        state.start_drag(1);
        state.update_hover(2, DropPosition::Before);
        
        assert!(state.is_hover_target(&2));
        assert!(!state.is_hover_target(&1));
        assert_eq!(state.current_drop_position(), Some(DropPosition::Before));
    }

    #[test]
    fn test_clear_hover() {
        let mut state = DragDropState::<u64>::new();
        state.start_drag(1);
        state.update_hover(2, DropPosition::After);
        state.clear_hover();
        
        assert!(!state.is_hover_target(&2));
        assert_eq!(state.current_drop_position(), None);
        assert!(state.is_dragging()); // Drag should still be active
    }

    #[test]
    fn test_end_drag_with_valid_drop() {
        let mut state = DragDropState::<u64>::new();
        state.start_drag(1);
        state.update_hover(2, DropPosition::Inside);
        
        let result = state.end_drag();
        assert_eq!(result, Some((1, 2, DropPosition::Inside)));
        assert!(!state.is_dragging());
    }

    #[test]
    fn test_end_drag_without_hover() {
        let mut state = DragDropState::<u64>::new();
        state.start_drag(1);
        
        let result = state.end_drag();
        assert_eq!(result, None);
        assert!(!state.is_dragging());
    }

    #[test]
    fn test_cancel_drag() {
        let mut state = DragDropState::<u64>::new();
        state.start_drag(1);
        state.update_hover(2, DropPosition::Before);
        state.cancel_drag();
        
        assert!(!state.is_dragging());
        assert_eq!(state.dragging_id(), None);
        assert_eq!(state.current_drop_position(), None);
    }

    #[test]
    fn test_validate_drop_same_node() {
        let node = TestNode::new(1, "Node1", false);
        let is_descendant = |_: &u64, _: &u64| false;
        
        // Cannot drop a node onto itself
        assert!(!validate_drop::<TestNode, _>(
            &1,
            &1,
            DropPosition::Before,
            &node,
            is_descendant
        ));
    }

    #[test]
    fn test_validate_drop_into_descendant() {
        let node = TestNode::new(2, "Node2", true);
        let is_descendant = |target: &u64, source: &u64| {
            // Simulate node 2 being a descendant of node 1
            *target == 2 && *source == 1
        };
        
        // Cannot drop a parent into its own descendant
        assert!(!validate_drop::<TestNode, _>(
            &1,
            &2,
            DropPosition::Inside,
            &node,
            is_descendant
        ));
    }

    #[test]
    fn test_validate_drop_inside_non_collection() {
        let node = TestNode::new(2, "Node2", false);
        let is_descendant = |_: &u64, _: &u64| false;
        
        // Cannot drop inside a non-collection node
        assert!(!validate_drop::<TestNode, _>(
            &1,
            &2,
            DropPosition::Inside,
            &node,
            is_descendant
        ));
    }

    #[test]
    fn test_validate_drop_valid_before() {
        let node = TestNode::new(2, "Node2", false);
        let is_descendant = |_: &u64, _: &u64| false;
        
        // Valid drop before a node
        assert!(validate_drop::<TestNode, _>(
            &1,
            &2,
            DropPosition::Before,
            &node,
            is_descendant
        ));
    }

    #[test]
    fn test_validate_drop_valid_after() {
        let node = TestNode::new(2, "Node2", true);
        let is_descendant = |_: &u64, _: &u64| false;
        
        // Valid drop after a node
        assert!(validate_drop::<TestNode, _>(
            &1,
            &2,
            DropPosition::After,
            &node,
            is_descendant
        ));
    }

    #[test]
    fn test_validate_drop_valid_inside_collection() {
        let node = TestNode::new(2, "Node2", true);
        let is_descendant = |_: &u64, _: &u64| false;
        
        // Valid drop inside a collection
        assert!(validate_drop::<TestNode, _>(
            &1,
            &2,
            DropPosition::Inside,
            &node,
            is_descendant
        ));
    }

    #[test]
    fn test_calculate_drop_position_before() {
        let rect = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(100.0, 40.0));
        
        // Top 25% should be Before
        let position = calculate_drop_position(5.0, rect, true);
        assert_eq!(position, DropPosition::Before);
    }

    #[test]
    fn test_calculate_drop_position_after() {
        let rect = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(100.0, 40.0));
        
        // Bottom 25% should be After
        let position = calculate_drop_position(35.0, rect, true);
        assert_eq!(position, DropPosition::After);
    }

    #[test]
    fn test_calculate_drop_position_inside_collection() {
        let rect = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(100.0, 40.0));
        
        // Middle 50% should be Inside for collections
        let position = calculate_drop_position(20.0, rect, true);
        assert_eq!(position, DropPosition::Inside);
    }

    #[test]
    fn test_calculate_drop_position_middle_non_collection() {
        let rect = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(100.0, 40.0));
        
        // Middle 50% should be After for non-collections
        let position = calculate_drop_position(20.0, rect, false);
        assert_eq!(position, DropPosition::After);
    }

    #[test]
    fn test_drag_drop_visuals_default() {
        let visuals = DragDropVisuals::default();
        assert_eq!(visuals.drop_line_thickness, 2.0);
        assert!(visuals.invalid_target_opacity > 0.0 && visuals.invalid_target_opacity < 1.0);
    }

    #[test]
    fn test_multiple_drag_operations() {
        let mut state = DragDropState::<u64>::new();
        
        // First drag
        state.start_drag(1);
        assert!(state.is_dragging_node(&1));
        state.end_drag();
        assert!(!state.is_dragging());
        
        // Second drag
        state.start_drag(2);
        assert!(state.is_dragging_node(&2));
        assert!(!state.is_dragging_node(&1));
        state.cancel_drag();
        assert!(!state.is_dragging());
    }

    #[test]
    fn test_hover_updates_during_drag() {
        let mut state = DragDropState::<u64>::new();
        state.start_drag(1);
        
        // Update hover multiple times
        state.update_hover(2, DropPosition::Before);
        assert!(state.is_hover_target(&2));
        assert_eq!(state.current_drop_position(), Some(DropPosition::Before));
        
        state.update_hover(3, DropPosition::After);
        assert!(!state.is_hover_target(&2));
        assert!(state.is_hover_target(&3));
        assert_eq!(state.current_drop_position(), Some(DropPosition::After));
        
        state.update_hover(4, DropPosition::Inside);
        assert!(state.is_hover_target(&4));
        assert_eq!(state.current_drop_position(), Some(DropPosition::Inside));
    }
}