uzor 1.0.4

Core UI engine — geometry, interaction, input state
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
//! Drag-and-drop state machine for panel system
//!
//! Implements the complete drag-and-drop lifecycle:
//! 1. `start_drag()` - Initiate drag on mouse down + threshold
//! 2. `update_hover()` - Update target as mouse moves
//! 3. `complete_drop()` - Finalize on mouse up
//! 4. `cancel_drag()` - Cancel on ESC or invalid drop
//!
//! # Lock System
//!
//! Uses egui_dock-inspired lock system to prevent rapid changes:
//! - **Unlocked**: Changes allowed
//! - **SoftLock**: Releasable after 200ms timeout
//! - **HardLock**: Absolute lock during critical operations
//!
//! # Example
//!
//! ```
//! use uzor_panels::{DragDropState, LeafId, PanelRect, DropZone};
//!
//! let mut state = DragDropState::new();
//!
//! // Start drag
//! let rect = PanelRect::new(0.0, 0.0, 100.0, 100.0);
//! state.start_drag(LeafId(1), (10.0, 10.0), rect);
//!
//! // Update hover target
//! let preview = PanelRect::new(100.0, 0.0, 50.0, 100.0);
//! state.update_hover(LeafId(2), DropZone::Right, preview);
//!
//! // Complete drop
//! if let Some((source, target, zone)) = state.complete_drop() {
//!     println!("Drop panel {:?} onto {:?} at {:?}", source, target, zone);
//! }
//! ```

use std::time::{Duration, Instant};
use super::{LeafId, PanelRect, DropZone};

// =============================================================================
// Drag State Types
// =============================================================================

/// Source of drag operation
#[derive(Clone, Debug)]
pub struct DragSource {
    /// Panel being dragged
    pub panel_id: LeafId,
    /// Original rectangle
    pub source_rect: PanelRect,
    /// Mouse offset from panel origin (for preview positioning)
    pub offset: (f32, f32),
}

/// Target of hover during drag
#[derive(Clone, Debug)]
pub struct HoverTarget {
    /// Container being hovered
    pub container_id: LeafId,
    /// Drop zone within container
    pub zone: DropZone,
    /// Preview rectangle (where panel will be placed)
    pub preview_rect: PanelRect,
}

/// Lock state to prevent rapid changes during drag
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum LockState {
    /// No lock - changes allowed
    Unlocked,
    /// Soft lock - releasable after timeout (200ms)
    SoftLock,
    /// Hard lock - absolute lock during critical operations
    HardLock,
}

// =============================================================================
// Drag-and-Drop State Machine
// =============================================================================

/// Main drag-and-drop state machine
///
/// Manages the complete drag-and-drop lifecycle:
/// 1. `start_drag()` - Initiate drag on mouse down + threshold
/// 2. `update_hover()` - Update target as mouse moves
/// 3. `complete_drop()` - Finalize on mouse up
/// 4. `cancel_drag()` - Cancel on ESC or invalid drop
pub struct DragDropState {
    drag_source: Option<DragSource>,
    hover_target: Option<HoverTarget>,
    lock: LockState,
    lock_time: Option<Instant>,
}

impl DragDropState {
    /// Create new drag-and-drop state
    pub fn new() -> Self {
        Self {
            drag_source: None,
            hover_target: None,
            lock: LockState::Unlocked,
            lock_time: None,
        }
    }

    /// Start drag operation
    ///
    /// # Arguments
    /// - `panel_id`: Panel being dragged
    /// - `mouse_pos`: Current mouse position (absolute)
    /// - `rect`: Panel rectangle
    pub fn start_drag(&mut self, panel_id: LeafId, mouse_pos: (f32, f32), rect: PanelRect) {
        self.drag_source = Some(DragSource {
            panel_id,
            source_rect: rect,
            offset: (mouse_pos.0 - rect.x, mouse_pos.1 - rect.y),
        });
    }

    /// Update hover target during drag
    ///
    /// Respects lock state - will not update if locked.
    pub fn update_hover(&mut self, container_id: LeafId, zone: DropZone, preview: PanelRect) {
        // Check lock
        if self.is_locked() {
            return;
        }

        self.hover_target = Some(HoverTarget {
            container_id,
            zone,
            preview_rect: preview,
        });
    }

    /// Clear hover target (mouse left container)
    pub fn clear_hover(&mut self) {
        self.hover_target = None;
    }

    /// Complete drop operation on mouse up
    ///
    /// Returns (source_panel_id, target_container_id, drop_zone) if successful.
    /// Sets soft lock to prevent rapid re-drops.
    pub fn complete_drop(&mut self) -> Option<(LeafId, LeafId, DropZone)> {
        if self.is_locked() {
            return None;
        }

        let source = self.drag_source.take()?;
        let target = self.hover_target.take()?;

        // Set soft lock (prevent rapid re-drops)
        self.lock = LockState::SoftLock;
        self.lock_time = Some(Instant::now());

        Some((source.panel_id, target.container_id, target.zone))
    }

    /// Cancel drag operation (ESC or invalid drop)
    pub fn cancel_drag(&mut self) {
        self.drag_source = None;
        self.hover_target = None;
        self.lock = LockState::Unlocked;
        self.lock_time = None;
    }

    /// Check if drag is active
    pub fn is_dragging(&self) -> bool {
        self.drag_source.is_some()
    }

    /// Check if locked (any lock state except Unlocked)
    pub fn is_locked(&self) -> bool {
        match self.lock {
            LockState::Unlocked => false,
            LockState::HardLock => true,
            LockState::SoftLock => {
                // Check timeout (200ms)
                if let Some(lock_time) = self.lock_time {
                    lock_time.elapsed() < Duration::from_millis(200)
                } else {
                    false
                }
            }
        }
    }

    /// Update lock state (call every frame)
    ///
    /// Releases soft lock after timeout expires.
    pub fn update(&mut self) {
        if self.lock == LockState::SoftLock {
            if let Some(lock_time) = self.lock_time {
                if lock_time.elapsed() >= Duration::from_millis(200) {
                    self.lock = LockState::Unlocked;
                    self.lock_time = None;
                }
            }
        }
    }

    /// Get current drag source (if dragging)
    pub fn drag_source(&self) -> Option<&DragSource> {
        self.drag_source.as_ref()
    }

    /// Get current hover target (if hovering)
    pub fn hover_target(&self) -> Option<&HoverTarget> {
        self.hover_target.as_ref()
    }

    /// Set hard lock (for critical operations)
    pub fn set_hard_lock(&mut self) {
        self.lock = LockState::HardLock;
        self.lock_time = Some(Instant::now());
    }

    /// Release lock (force unlock)
    pub fn unlock(&mut self) {
        self.lock = LockState::Unlocked;
        self.lock_time = None;
    }
}

impl Default for DragDropState {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Panel Drag State (application-level)
// =============================================================================

/// Drag state for panel being dragged by header
///
/// This is a higher-level state used by the application layer to track
/// panel drag operations. It's separate from DragDropState which handles
/// the core state machine.
#[derive(Clone, Debug)]
pub struct PanelDragState {
    /// Panel being dragged
    pub dragged_leaf_id: LeafId,
    /// Current mouse position (panel-local coords)
    pub current_x: f32,
    pub current_y: f32,
    /// Target panel under cursor (if any)
    pub target_leaf_id: Option<LeafId>,
    /// Current drop zone
    pub drop_zone: Option<DropZone>,
    /// True if this is a window-level edge drop (vs leaf-level)
    pub is_window_edge: bool,
}

impl PanelDragState {
    /// Create new panel drag state
    pub fn new(dragged_leaf_id: LeafId, x: f32, y: f32) -> Self {
        Self {
            dragged_leaf_id,
            current_x: x,
            current_y: y,
            target_leaf_id: None,
            drop_zone: None,
            is_window_edge: false,
        }
    }

    /// Update mouse position
    pub fn update_position(&mut self, x: f32, y: f32) {
        self.current_x = x;
        self.current_y = y;
    }

    /// Update target and drop zone
    pub fn update_target(&mut self, target: LeafId, zone: DropZone, is_window_edge: bool) {
        self.target_leaf_id = Some(target);
        self.drop_zone = Some(zone);
        self.is_window_edge = is_window_edge;
    }

    /// Clear target (mouse left valid drop area)
    pub fn clear_target(&mut self) {
        self.target_leaf_id = None;
        self.drop_zone = None;
        self.is_window_edge = false;
    }

    /// Check if there's a valid drop target
    pub fn has_target(&self) -> bool {
        self.target_leaf_id.is_some() && self.drop_zone.is_some()
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_drag_drop_lifecycle() {
        let mut state = DragDropState::new();

        assert!(!state.is_dragging());

        // Start drag
        let rect = PanelRect::new(0.0, 0.0, 100.0, 100.0);
        state.start_drag(LeafId(1), (10.0, 10.0), rect);
        assert!(state.is_dragging());

        // Update hover
        let preview = PanelRect::new(100.0, 0.0, 50.0, 100.0);
        state.update_hover(LeafId(2), DropZone::Right, preview);
        assert!(state.hover_target().is_some());

        // Complete drop
        let result = state.complete_drop();
        assert!(result.is_some());
        assert!(!state.is_dragging());
        assert!(state.is_locked()); // Soft lock after drop
    }

    #[test]
    fn test_lock_prevents_hover_update() {
        let mut state = DragDropState::new();

        // Start drag and complete to enter soft lock
        let rect = PanelRect::new(0.0, 0.0, 100.0, 100.0);
        state.start_drag(LeafId(1), (10.0, 10.0), rect);
        state.update_hover(LeafId(2), DropZone::Center, rect);
        state.complete_drop();

        // Try to start new drag while locked
        assert!(state.is_locked());
        state.start_drag(LeafId(3), (20.0, 20.0), rect);
        state.update_hover(LeafId(4), DropZone::Left, rect);

        // Hover should not update due to lock
        // But drag source should exist (start_drag doesn't check lock)
        assert!(state.drag_source().is_some());
    }

    #[test]
    fn test_cancel_drag() {
        let mut state = DragDropState::new();
        let rect = PanelRect::new(0.0, 0.0, 100.0, 100.0);

        state.start_drag(LeafId(1), (10.0, 10.0), rect);
        assert!(state.is_dragging());

        state.cancel_drag();
        assert!(!state.is_dragging());
        assert!(!state.is_locked());
    }

    #[test]
    fn test_panel_drag_state() {
        let mut state = PanelDragState::new(LeafId(1), 10.0, 20.0);

        assert_eq!(state.dragged_leaf_id, LeafId(1));
        assert_eq!(state.current_x, 10.0);
        assert_eq!(state.current_y, 20.0);
        assert!(!state.has_target());

        // Update position
        state.update_position(30.0, 40.0);
        assert_eq!(state.current_x, 30.0);
        assert_eq!(state.current_y, 40.0);

        // Update target
        state.update_target(LeafId(2), DropZone::Center, false);
        assert!(state.has_target());
        assert_eq!(state.target_leaf_id, Some(LeafId(2)));
        assert_eq!(state.drop_zone, Some(DropZone::Center));
        assert!(!state.is_window_edge);

        // Clear target
        state.clear_target();
        assert!(!state.has_target());
        assert_eq!(state.target_leaf_id, None);
        assert_eq!(state.drop_zone, None);
    }

    #[test]
    fn test_hard_lock() {
        let mut state = DragDropState::new();

        state.set_hard_lock();
        assert!(state.is_locked());
        assert_eq!(state.lock, LockState::HardLock);

        // Hard lock doesn't release on update
        state.update();
        assert!(state.is_locked());

        // Must explicitly unlock
        state.unlock();
        assert!(!state.is_locked());
    }

    #[test]
    fn test_soft_lock_timeout() {
        let mut state = DragDropState::new();
        let rect = PanelRect::new(0.0, 0.0, 100.0, 100.0);

        // Complete drop to enter soft lock
        state.start_drag(LeafId(1), (10.0, 10.0), rect);
        state.update_hover(LeafId(2), DropZone::Center, rect);
        state.complete_drop();

        assert!(state.is_locked());
        assert_eq!(state.lock, LockState::SoftLock);

        // Soft lock should still be active immediately
        state.update();
        assert!(state.is_locked());

        // Note: Can't test timeout in unit test without sleeping
        // In real usage, after 200ms the lock would release
    }
}