reratui 1.1.0

A modern, reactive TUI framework for Rust with React-inspired hooks and components, powered by ratatui
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
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
//! Mouse event hooks for handling mouse input in components.
//!
//! This module provides fiber-based mouse hooks that integrate with the
//! fiber event system for proper React-like semantics.
//!
//! # Example
//!
//! ```rust,ignore
//! use reratui_fiber::hooks::{use_mouse, use_mouse_click, use_mouse_hover};
//! use crossterm::event::MouseButton;
//! use ratatui::layout::Rect;
//!
//! #[component]
//! fn MyComponent() -> Element {
//!     // Handle all mouse events
//!     use_mouse(|mouse_event| {
//!         println!("Mouse at: ({}, {})", mouse_event.column, mouse_event.row);
//!     });
//!
//!     // Handle only click events
//!     use_mouse_click(|button, x, y| {
//!         println!("Clicked {:?} at ({}, {})", button, x, y);
//!     });
//!
//!     // Track hover state over an area
//!     let button_area = Rect::new(10, 5, 20, 3);
//!     let is_hovering = use_mouse_hover(button_area);
//!
//!     rsx! { <Text text={format!("Hovering: {}", is_hovering)} /> }
//! }
//! ```

use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
use ratatui::layout::Rect;
use std::time::{Duration, Instant};

use super::effect_event::use_effect_event;
use super::event::use_event;
use super::r#ref::use_ref;
use super::state::use_state;

/// A hook that handles mouse events with a stable callback.
///
/// This hook uses `use_effect_event` internally to ensure the callback always
/// sees the latest captured values while maintaining a stable identity.
///
/// # Type Parameters
///
/// * `F` - A function that takes a `MouseEvent` and returns nothing
///
/// # Arguments
///
/// * `handler` - A callback function that will be invoked when a mouse event occurs
///
/// # Examples
///
/// ```rust,ignore
/// use reratui_fiber::hooks::use_mouse;
/// use reratui_fiber::hooks::use_state;
/// use crossterm::event::{MouseEventKind, MouseButton};
///
/// // Track mouse clicks
/// let (click_count, set_click_count) = use_state(|| 0);
///
/// use_mouse(move |mouse_event| {
///     if matches!(mouse_event.kind, MouseEventKind::Down(MouseButton::Left)) {
///         println!("Mouse clicked at: ({}, {})", mouse_event.column, mouse_event.row);
///         set_click_count.update(|c| c + 1);
///     }
/// });
/// ```
///
/// # Note
///
/// - The callback always sees the latest state values (via effect event pattern)
/// - Each mouse event is only processed once per component
/// - The callback has a stable identity across renders
/// - Only mouse events trigger the callback (keyboard, resize, etc. are ignored)
/// - Mouse capture must be enabled in the terminal
pub fn use_mouse<F>(handler: F)
where
    F: Fn(MouseEvent) + Send + Sync + 'static,
{
    // Create a stable callback using effect event pattern
    let stable_handler = use_effect_event(move |mouse_event: MouseEvent| {
        handler(mouse_event);
    });

    // Check for mouse events
    if let Some(Event::Mouse(mouse_event)) = use_event() {
        // Emit the event to the stable handler
        stable_handler.call(mouse_event);
    }
}

/// A hook that handles mouse click events only (filters out movement and drag).
///
/// This is a convenience wrapper around `use_mouse` that only triggers the callback
/// when a mouse button is clicked (pressed down), ignoring movement, drag, and scroll events.
///
/// # Type Parameters
///
/// * `F` - A function that takes `(MouseButton, u16, u16)` (button, column, row) and returns nothing
///
/// # Arguments
///
/// * `handler` - A callback function that will be invoked when a mouse button is clicked
///
/// # Examples
///
/// ```rust,ignore
/// use reratui_fiber::hooks::use_mouse_click;
/// use crossterm::event::MouseButton;
///
/// // Track left clicks only
/// use_mouse_click(move |button, x, y| {
///     if button == MouseButton::Left {
///         println!("Left click at ({}, {})", x, y);
///     }
/// });
/// ```
///
/// # Note
///
/// - Only triggers on `MouseEventKind::Down` events
/// - Filters out movement, drag, scroll, and button release events
/// - The callback always sees the latest state values (via effect event pattern)
/// - The callback has a stable identity across renders
pub fn use_mouse_click<F>(handler: F)
where
    F: Fn(MouseButton, u16, u16) + Send + Sync + 'static,
{
    use_mouse(move |mouse_event| {
        // Only handle click (down) events
        if let MouseEventKind::Down(button) = mouse_event.kind {
            handler(button, mouse_event.column, mouse_event.row);
        }
    });
}

/// Information about a drag operation
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DragInfo {
    /// The mouse button being used for dragging
    pub button: Option<MouseButton>,
    /// Starting position (column, row)
    pub start: (u16, u16),
    /// Current position (column, row)
    pub current: (u16, u16),
    /// Whether the drag is currently active
    pub is_dragging: bool,
    /// Whether the drag just started
    pub is_start: bool,
    /// Whether the drag just ended
    pub is_end: bool,
}

/// Hook for tracking mouse drag operations.
///
/// Returns a tuple containing the current drag state and a reset function.
/// The reset function can be used to clear the drag state and reset tracking.
///
/// This hook automatically updates the drag state based on mouse events from the current event context.
///
/// # Returns
///
/// A tuple `(DragInfo, impl Fn())` where:
/// - First element is the current drag information
/// - Second element is a reset function to clear the drag state
///
/// # Examples
///
/// ```rust,ignore
/// use reratui_fiber::hooks::use_mouse_drag;
///
/// let (drag_info, reset_drag) = use_mouse_drag();
///
/// if drag_info.is_start {
///     println!("Drag started at {:?}", drag_info.start);
/// } else if drag_info.is_dragging {
///     println!("Dragging from {:?} to {:?}", drag_info.start, drag_info.current);
/// } else if drag_info.is_end {
///     println!("Drag ended at {:?}", drag_info.current);
/// }
///
/// // Reset drag state if needed
/// if some_condition {
///     reset_drag();
/// }
/// ```
///
/// # Note
///
/// - Tracks drag start (button down), drag movement, and drag end (button up)
/// - The drag state persists across renders until the drag ends or is reset
/// - `is_dragging` is `true` during the entire drag operation
/// - `is_start` is only `true` on the first frame of the drag
/// - `is_end` is only `true` on the last frame of the drag
pub fn use_mouse_drag() -> (DragInfo, impl Fn() + Clone) {
    let (drag_info, set_drag_info) = use_state(DragInfo::default);
    let drag_state = use_ref(|| None::<(MouseButton, u16, u16)>);

    let set_info_clone = set_drag_info;
    let state_clone = drag_state.clone();

    use_mouse(move |mouse_event| {
        match mouse_event.kind {
            MouseEventKind::Down(button) => {
                // Start drag
                state_clone.set(Some((button, mouse_event.column, mouse_event.row)));
                set_info_clone.set(DragInfo {
                    button: Some(button),
                    start: (mouse_event.column, mouse_event.row),
                    current: (mouse_event.column, mouse_event.row),
                    is_dragging: true,
                    is_start: true,
                    is_end: false,
                });
            }
            MouseEventKind::Drag(button) => {
                // Continue drag
                if let Some((drag_button, start_x, start_y)) = state_clone.get() {
                    if button == drag_button {
                        set_info_clone.set(DragInfo {
                            button: Some(button),
                            start: (start_x, start_y),
                            current: (mouse_event.column, mouse_event.row),
                            is_dragging: true,
                            is_start: false,
                            is_end: false,
                        });
                    }
                }
            }
            MouseEventKind::Up(button) => {
                // End drag
                if let Some((drag_button, start_x, start_y)) = state_clone.get() {
                    if button == drag_button {
                        set_info_clone.set(DragInfo {
                            button: Some(button),
                            start: (start_x, start_y),
                            current: (mouse_event.column, mouse_event.row),
                            is_dragging: false,
                            is_start: false,
                            is_end: true,
                        });
                        state_clone.set(None);
                    }
                }
            }
            _ => {}
        }
    });

    let reset = {
        let set_info = set_drag_info;
        let state = drag_state.clone();
        move || {
            set_info.set(DragInfo::default());
            state.set(None);
        }
    };

    (drag_info, reset)
}

/// A hook that detects double-click events with configurable timing.
///
/// This hook detects when a mouse button is clicked twice within a specified
/// time window (default 500ms).
///
/// # Type Parameters
///
/// * `F` - A function that takes `(MouseButton, u16, u16)` (button, column, row) and returns nothing
///
/// # Arguments
///
/// * `max_delay` - Maximum time between clicks to be considered a double-click
/// * `handler` - A callback function that will be invoked when a double-click is detected
///
/// # Examples
///
/// ```rust,ignore
/// use reratui_fiber::hooks::use_double_click;
/// use std::time::Duration;
///
/// // Detect double-clicks with 500ms window
/// use_double_click(Duration::from_millis(500), move |button, x, y| {
///     println!("Double-click at ({}, {})", x, y);
/// });
/// ```
///
/// # Note
///
/// - Default timing window is 500ms (typical for most UIs)
/// - Only triggers on the second click of a double-click
/// - Uses `use_ref` internally to track click timing without re-renders
/// - The callback always sees the latest state values (via effect event pattern)
/// - The callback has a stable identity across renders
pub fn use_double_click<F>(max_delay: Duration, handler: F)
where
    F: Fn(MouseButton, u16, u16) + Send + Sync + 'static,
{
    // Track last click: Option<(button, x, y, time)>
    let last_click = use_ref(|| None::<(MouseButton, u16, u16, Instant)>);

    use_mouse(move |mouse_event| {
        if let MouseEventKind::Down(button) = mouse_event.kind {
            let now = Instant::now();
            let current_pos = (mouse_event.column, mouse_event.row);

            if let Some((last_button, last_x, last_y, last_time)) = last_click.get() {
                // Check if this is a double-click
                let time_diff = now.duration_since(last_time);
                let same_button = button == last_button;
                let same_position = current_pos == (last_x, last_y);

                if same_button && same_position && time_diff <= max_delay {
                    // Double-click detected!
                    handler(button, mouse_event.column, mouse_event.row);
                    // Reset to prevent triple-click from triggering another double-click
                    last_click.set(None);
                    return;
                }
            }

            // Store this click for potential double-click
            last_click.set(Some((button, mouse_event.column, mouse_event.row, now)));
        }
    });
}

/// A hook that tracks the current mouse position.
///
/// Returns a tuple `(x, y)` representing the current mouse coordinates.
/// The position is updated whenever any mouse event occurs (move, click, scroll, etc.).
///
/// # Returns
///
/// A tuple `(u16, u16)` where:
/// - First element is the column (x-coordinate)
/// - Second element is the row (y-coordinate)
///
/// # Examples
///
/// ```rust,ignore
/// use reratui_fiber::hooks::use_mouse_position;
///
/// let (x, y) = use_mouse_position();
/// println!("Mouse is at position: ({}, {})", x, y);
/// ```
///
/// # Note
///
/// - The position starts at (0, 0) until the first mouse event
/// - Mouse capture must be enabled in the terminal
/// - The hook updates on any mouse event, including movement, clicks, and scrolling
pub fn use_mouse_position() -> (u16, u16) {
    let (position, set_position) = use_state(|| (0u16, 0u16));

    use_mouse({
        move |mouse_event| {
            let new_pos = (mouse_event.column, mouse_event.row);
            if new_pos != position {
                set_position.set(new_pos);
            }
        }
    });

    position
}

/// A hook that detects if the mouse is hovering over a specific rectangular area.
///
/// Returns `true` if the mouse cursor is currently within the specified area bounds,
/// `false` otherwise. The hover state is updated on any mouse event.
///
/// # Arguments
///
/// * `area` - A `Rect` defining the rectangular area to monitor for hover events.
///   The area is defined by its `x`, `y`, `width`, and `height` properties.
///
/// # Returns
///
/// A boolean indicating whether the mouse is currently hovering over the area.
///
/// # Examples
///
/// ```rust,ignore
/// use reratui_fiber::hooks::use_mouse_hover;
/// use ratatui::layout::Rect;
///
/// let button_area = Rect::new(10, 5, 20, 3);
/// let is_hovering = use_mouse_hover(button_area);
///
/// if is_hovering {
///     println!("Mouse is hovering over the button!");
/// }
/// ```
///
/// # Note
///
/// - The hover detection is inclusive of the area boundaries
/// - Mouse position (x, y) is considered inside if:
///   - `x >= area.x && x < area.x + area.width`
///   - `y >= area.y && y < area.y + area.height`
/// - The hook updates on any mouse event (movement, clicks, scrolling)
/// - Mouse capture must be enabled in the terminal
pub fn use_mouse_hover(area: Rect) -> bool {
    let (is_hovering, set_hovering) = use_state(|| false);

    use_mouse({
        move |mouse_event| {
            let is_inside = mouse_event.column >= area.x
                && mouse_event.column < area.x + area.width
                && mouse_event.row >= area.y
                && mouse_event.row < area.y + area.height;

            if is_inside != is_hovering {
                set_hovering.set(is_inside);
            }
        }
    });

    is_hovering
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::{clear_current_event, set_current_event};
    use crate::fiber_tree::{FiberTree, clear_fiber_tree, set_fiber_tree, with_fiber_tree_mut};
    use crossterm::event::KeyModifiers;
    use once_cell::sync::Lazy;
    use parking_lot::Mutex;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicI32, Ordering};

    /// Test mutex to ensure tests run sequentially since they share global state
    static TEST_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

    fn setup_test_fiber() -> crate::fiber::FiberId {
        let mut tree = FiberTree::new();
        let fiber_id = tree.mount(None, None);
        tree.begin_render(fiber_id);
        set_fiber_tree(tree);
        fiber_id
    }

    fn cleanup_test() {
        with_fiber_tree_mut(|tree| {
            tree.end_render();
        });
        clear_fiber_tree();
        clear_current_event();
        crate::scheduler::batch::clear_state_batch();
    }

    fn create_mouse_event(kind: MouseEventKind, column: u16, row: u16) -> Event {
        Event::Mouse(MouseEvent {
            kind,
            column,
            row,
            modifiers: KeyModifiers::NONE,
        })
    }

    #[test]
    fn test_use_mouse_receives_mouse_event() {
        let _lock = TEST_MUTEX.lock();
        cleanup_test();
        let _fiber_id = setup_test_fiber();

        let call_count = Arc::new(AtomicI32::new(0));
        let call_count_clone = call_count.clone();

        // Set up a mouse event
        let event = create_mouse_event(MouseEventKind::Down(MouseButton::Left), 10, 20);
        set_current_event(Some(Arc::new(event)));

        // Use the mouse hook
        use_mouse(move |mouse_event| {
            assert_eq!(mouse_event.column, 10);
            assert_eq!(mouse_event.row, 20);
            call_count_clone.fetch_add(1, Ordering::SeqCst);
        });

        assert_eq!(call_count.load(Ordering::SeqCst), 1);

        cleanup_test();
    }

    #[test]
    fn test_use_mouse_ignores_non_mouse_events() {
        let _lock = TEST_MUTEX.lock();
        cleanup_test();
        let _fiber_id = setup_test_fiber();

        let call_count = Arc::new(AtomicI32::new(0));
        let call_count_clone = call_count.clone();

        // Set up a key event (not a mouse event)
        let event = Event::Key(crossterm::event::KeyEvent::new(
            crossterm::event::KeyCode::Char('a'),
            KeyModifiers::NONE,
        ));
        set_current_event(Some(Arc::new(event)));

        // Use the mouse hook
        use_mouse(move |_mouse_event| {
            call_count_clone.fetch_add(1, Ordering::SeqCst);
        });

        // Should not be called for key events
        assert_eq!(call_count.load(Ordering::SeqCst), 0);

        cleanup_test();
    }

    #[test]
    fn test_use_mouse_click_only_handles_down() {
        let _lock = TEST_MUTEX.lock();
        cleanup_test();
        let fiber_id = setup_test_fiber();

        let call_count = Arc::new(AtomicI32::new(0));

        // Test with Down event
        let down_event = create_mouse_event(MouseEventKind::Down(MouseButton::Left), 10, 20);
        set_current_event(Some(Arc::new(down_event)));

        let call_count_clone = call_count.clone();
        use_mouse_click(move |button, x, y| {
            assert_eq!(button, MouseButton::Left);
            assert_eq!(x, 10);
            assert_eq!(y, 20);
            call_count_clone.fetch_add(1, Ordering::SeqCst);
        });

        assert_eq!(call_count.load(Ordering::SeqCst), 1);

        // Simulate re-render for move event
        with_fiber_tree_mut(|tree| {
            tree.end_render();
            tree.begin_render(fiber_id);
        });
        clear_current_event();

        // Test with Move event
        let move_event = create_mouse_event(MouseEventKind::Moved, 15, 25);
        set_current_event(Some(Arc::new(move_event)));

        let call_count_clone2 = call_count.clone();
        use_mouse_click(move |_, _, _| {
            call_count_clone2.fetch_add(1, Ordering::SeqCst);
        });

        // Should still be 1 (move event ignored)
        assert_eq!(call_count.load(Ordering::SeqCst), 1);

        cleanup_test();
    }

    #[test]
    fn test_use_mouse_no_event() {
        let _lock = TEST_MUTEX.lock();
        cleanup_test();
        let _fiber_id = setup_test_fiber();

        let call_count = Arc::new(AtomicI32::new(0));
        let call_count_clone = call_count.clone();

        // No event set
        clear_current_event();

        use_mouse(move |_| {
            call_count_clone.fetch_add(1, Ordering::SeqCst);
        });

        // Should not be called
        assert_eq!(call_count.load(Ordering::SeqCst), 0);

        cleanup_test();
    }

    #[test]
    fn test_use_mouse_hover_inside_area() {
        let _lock = TEST_MUTEX.lock();
        cleanup_test();
        let fiber_id = setup_test_fiber();

        // Set up a mouse event inside the area
        let event = create_mouse_event(MouseEventKind::Moved, 15, 7);
        set_current_event(Some(Arc::new(event)));

        let area = Rect::new(10, 5, 20, 10);
        // First render - state is initialized to false, event triggers update
        let is_hovering = use_mouse_hover(area);
        // Initial state is false (state updates are batched)
        assert!(!is_hovering);

        // Apply the batch and re-render
        with_fiber_tree_mut(|tree| {
            tree.end_render();
            crate::scheduler::batch::with_state_batch_mut(|batch| {
                batch.end_batch(tree);
            });
            tree.begin_render(fiber_id);
        });
        clear_current_event();

        // Second render - state should now be true
        let is_hovering = use_mouse_hover(area);
        assert!(is_hovering);

        cleanup_test();
    }

    #[test]
    fn test_use_mouse_hover_outside_area() {
        let _lock = TEST_MUTEX.lock();
        cleanup_test();
        let _fiber_id = setup_test_fiber();

        // Set up a mouse event outside the area
        let event = create_mouse_event(MouseEventKind::Moved, 5, 3);
        set_current_event(Some(Arc::new(event)));

        let area = Rect::new(10, 5, 20, 10);
        let is_hovering = use_mouse_hover(area);

        // Initial state is false, and event is outside area, so no update queued
        assert!(!is_hovering);

        cleanup_test();
    }

    #[test]
    fn test_use_mouse_position() {
        let _lock = TEST_MUTEX.lock();
        cleanup_test();
        let fiber_id = setup_test_fiber();

        // Set up a mouse event
        let event = create_mouse_event(MouseEventKind::Moved, 42, 24);
        set_current_event(Some(Arc::new(event)));

        // First render - state is initialized to (0, 0), event triggers update
        let (x, y) = use_mouse_position();
        // Initial state is (0, 0) (state updates are batched)
        assert_eq!(x, 0);
        assert_eq!(y, 0);

        // Apply the batch and re-render
        with_fiber_tree_mut(|tree| {
            tree.end_render();
            crate::scheduler::batch::with_state_batch_mut(|batch| {
                batch.end_batch(tree);
            });
            tree.begin_render(fiber_id);
        });
        clear_current_event();

        // Second render - state should now be (42, 24)
        let (x, y) = use_mouse_position();
        assert_eq!(x, 42);
        assert_eq!(y, 24);

        cleanup_test();
    }

    #[test]
    fn test_use_mouse_position_default() {
        let _lock = TEST_MUTEX.lock();
        cleanup_test();
        let _fiber_id = setup_test_fiber();

        // No event set
        clear_current_event();

        let (x, y) = use_mouse_position();

        // Should return default (0, 0)
        assert_eq!(x, 0);
        assert_eq!(y, 0);

        cleanup_test();
    }

    #[test]
    fn test_drag_info_default() {
        let info = DragInfo::default();
        assert_eq!(info.button, None);
        assert_eq!(info.start, (0, 0));
        assert_eq!(info.current, (0, 0));
        assert!(!info.is_dragging);
        assert!(!info.is_start);
        assert!(!info.is_end);
    }
}