revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Event handling and keyboard input for TUI applications
//!
//! This module provides comprehensive event handling including keyboard, mouse,
//! drag-and-drop, gestures, focus management, and custom events.
//!
//! # Core Event Types
//!
//! | Event | Description | Use Case |
//! |-------|-------------|----------|
//! | [`KeyEvent`] | Keyboard input | All keyboard interaction |
//! | [`MouseEvent`] | Mouse input | Clicks, scrolls, movement |
//! | [`Event`] | Unified event type | Main event loop handling |
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use revue::prelude::*;
//!
//! struct MyApp;
//!
//! impl MyApp {
//!     fn handle_event(&mut self, event: &Event) -> bool {
//!         match event {
//!             Event::Key(key) => self.handle_key(key),
//!             Event::Mouse(mouse) => self.handle_mouse(mouse),
//!             Event::Resize(width, height) => self.handle_resize(*width, *height),
//!             Event::Tick => self.update_animation(),
//!         }
//!     }
//! }
//! ```
//!
//! # Keyboard Input
//!
//! ```rust,ignore
//! use revue::event::{Key, KeyEvent, Modifiers};
//!
//! fn handle_key(key: &KeyEvent) {
//!     match key.key {
//!         Key::Char('c') if key.ctrl => println!("Ctrl+C pressed"),
//!         Key::Char('q') => println!("Quit"),
//!         Key::Up => println!("Arrow up"),
//!         Key::Enter => println!("Enter"),
//!         _ => {}
//!     }
//! }
//! ```
//!
//! # Mouse Input
//!
//! ```rust,ignore
//! use revue::event::{MouseEvent, MouseEventKind, MouseButton};
//!
//! fn handle_mouse(mouse: &MouseEvent) {
//!     match mouse.kind {
//!         MouseEventKind::Down(MouseButton::Left) => {
//!             println!("Click at {}, {}", mouse.x, mouse.y);
//!         }
//!         MouseEventKind::ScrollUp => {
//!             println!("Scroll up");
//!         }
//!         _ => {}
//!     }
//! }
//! ```
//!
//! # Drag and Drop
//!
//! ```rust,ignore
//! use revue::event::{start_drag, DragData, DragId};
//!
//! // Start dragging
//! let drag_id = start_drag(
//!     DragId::new(1),
//!     DragData::new("my_data")
//! );
//!
//! // Check if dragging
//! if is_dragging(drag_id) {
//!     // Update drag position
//! }
//!
//! // End drag with result
//! end_drag(drag_id);
//! ```
//!
//! # Focus Management
//!
//! ```rust,ignore
//! use revue::event::{FocusManager, Direction};
//!
//! let mut focus = FocusManager::new();
//!
//! // Add widgets to focus system
//! focus.register("input1");
//! focus.register("input2");
//!
//! // Navigate focus
//! focus.move_focus(Direction::Forward);  // Tab
//! focus.move_focus(Direction::Backward); // Shift+Tab
//! ```
//!
//! # Gestures
//!
//! ```rust,ignore
//! use revue::event::{Gesture, SwipeGesture, TapGesture};
//!
//! // Tap gesture
//! let tap = TapGesture::new()
//!     .on_tap(|point| println!("Tap at {:?}", point));
//!
//! // Swipe gesture
//! let swipe = SwipeGesture::new()
//!     .on_swipe(|direction| println!("Swiped {:?}", direction));
//! ```
//!
//! # Custom Events
//!
//! ```rust,ignore
//! use revue::event::{CustomEvent, EventDispatcher};
//!
//! #[derive(Debug, Clone)]
//! pub struct MyEvent {
//!     pub data: String,
//! }
//!
//! impl CustomEvent for MyEvent {
//!     fn id(&self) -> &'static str { "my_event" }
//! }
//!
//! // Dispatch custom event
//! dispatcher.dispatch(MyEvent { data: "Hello".to_string() });
//! ```

pub mod click;
pub mod custom;
pub mod drag;
mod focus;
pub mod gesture;
mod handler;
pub mod ime;
mod keymap;
mod reader;

pub use click::{ClickDetector, ClickType};
pub use custom::{
    AppEvent, CustomEvent, CustomEventBus, CustomHandlerId, DispatchPhase, DispatchResult,
    ErrorEvent, EventDispatcher, EventEnvelope, EventId, EventMeta, EventPriority, EventRecord,
    EventResponse, HandlerOptions, NavigateEvent, StateChangeEvent,
};
pub use drag::{
    cancel_drag, drag_context, end_drag, is_dragging, start_drag, update_drag_position,
    DragContext, DragData, DragId, DragState, DropResult, DropTarget,
};
pub use focus::{Direction, FocusManager, FocusTrap, FocusTrapConfig, WidgetId};
pub use gesture::{
    DragGesture, Gesture, GestureConfig, GestureRecognizer, GestureState, LongPressGesture,
    PinchDirection, PinchGesture, SwipeDirection, SwipeGesture, TapGesture,
};
pub use handler::{EventContext, EventHandler, EventPhase, HandlerId};
pub use ime::{
    Candidate, CompositionEvent, CompositionState, CompositionStyle, ImeConfig, ImeState,
    PreeditSegment, PreeditString,
};
pub use keymap::{Key, KeyBinding, KeyMap};
pub use reader::EventReader;

/// Mouse button types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseButton {
    /// Left mouse button
    Left,
    /// Right mouse button
    Right,
    /// Middle mouse button (scroll wheel click)
    Middle,
}

/// Mouse event kind
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseEventKind {
    /// Button pressed down
    Down(MouseButton),
    /// Button released
    Up(MouseButton),
    /// Mouse dragged while button held
    Drag(MouseButton),
    /// Mouse moved (no button pressed)
    Move,
    /// Scroll wheel down
    ScrollDown,
    /// Scroll wheel up
    ScrollUp,
    /// Scroll wheel left (horizontal)
    ScrollLeft,
    /// Scroll wheel right (horizontal)
    ScrollRight,
}

/// Mouse event with position and modifiers
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MouseEvent {
    /// X coordinate (column)
    pub x: u16,
    /// Y coordinate (row)
    pub y: u16,
    /// Kind of mouse event
    pub kind: MouseEventKind,
    /// Control modifier held
    pub ctrl: bool,
    /// Alt modifier held
    pub alt: bool,
    /// Shift modifier held
    pub shift: bool,
}

impl MouseEvent {
    /// Create a new mouse event
    pub fn new(x: u16, y: u16, kind: MouseEventKind) -> Self {
        Self {
            x,
            y,
            kind,
            ctrl: false,
            alt: false,
            shift: false,
        }
    }

    /// Check if this is a left click (down)
    pub fn is_left_click(&self) -> bool {
        matches!(self.kind, MouseEventKind::Down(MouseButton::Left))
    }

    /// Check if this is a right click (down)
    pub fn is_right_click(&self) -> bool {
        matches!(self.kind, MouseEventKind::Down(MouseButton::Right))
    }

    /// Check if this is a scroll event
    pub fn is_scroll(&self) -> bool {
        matches!(
            self.kind,
            MouseEventKind::ScrollDown
                | MouseEventKind::ScrollUp
                | MouseEventKind::ScrollLeft
                | MouseEventKind::ScrollRight
        )
    }
}

/// Application event
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
    /// Keyboard event
    Key(KeyEvent),
    /// Mouse event
    Mouse(MouseEvent),
    /// Terminal resize event
    Resize(u16, u16),
    /// Tick event (for animations, updates)
    Tick,
    /// Terminal gained focus
    FocusGained,
    /// Terminal lost focus
    FocusLost,
    /// Pasted text (requires bracketed paste mode)
    Paste(String),
}

/// Key press event
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyEvent {
    /// The key that was pressed
    pub key: Key,
    /// Control modifier
    pub ctrl: bool,
    /// Alt modifier
    pub alt: bool,
    /// Shift modifier
    pub shift: bool,
}

impl KeyEvent {
    /// Create a new key event
    pub fn new(key: Key) -> Self {
        Self {
            key,
            ctrl: false,
            alt: false,
            shift: false,
        }
    }

    /// Create with control modifier
    pub fn ctrl(key: Key) -> Self {
        Self {
            key,
            ctrl: true,
            alt: false,
            shift: false,
        }
    }

    /// Create with alt modifier
    pub fn alt(key: Key) -> Self {
        Self {
            key,
            ctrl: false,
            alt: true,
            shift: false,
        }
    }

    /// Check if this is Ctrl+C
    pub fn is_ctrl_c(&self) -> bool {
        self.ctrl && self.key == Key::Char('c')
    }

    /// Check if this is Escape
    pub fn is_escape(&self) -> bool {
        self.key == Key::Escape
    }

    /// Check if this is Enter
    pub fn is_enter(&self) -> bool {
        self.key == Key::Enter
    }

    /// Check if this is Tab
    pub fn is_tab(&self) -> bool {
        self.key == Key::Tab && !self.shift
    }

    /// Check if this is Shift+Tab
    pub fn is_shift_tab(&self) -> bool {
        self.key == Key::Tab && self.shift
    }

    /// Convert to KeyBinding for keymap lookup
    pub fn to_binding(&self) -> KeyBinding {
        KeyBinding {
            key: self.key,
            ctrl: self.ctrl,
            alt: self.alt,
            shift: self.shift,
        }
    }
}

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

    #[test]
    fn test_mouse_event_new() {
        let event = MouseEvent::new(10, 20, MouseEventKind::Down(MouseButton::Left));
        assert_eq!(event.x, 10);
        assert_eq!(event.y, 20);
        assert!(!event.ctrl);
        assert!(!event.alt);
        assert!(!event.shift);
    }

    #[test]
    fn test_mouse_event_is_left_click() {
        let event = MouseEvent::new(0, 0, MouseEventKind::Down(MouseButton::Left));
        assert!(event.is_left_click());

        let event = MouseEvent::new(0, 0, MouseEventKind::Down(MouseButton::Right));
        assert!(!event.is_left_click());

        let event = MouseEvent::new(0, 0, MouseEventKind::Up(MouseButton::Left));
        assert!(!event.is_left_click());
    }

    #[test]
    fn test_mouse_event_is_right_click() {
        let event = MouseEvent::new(0, 0, MouseEventKind::Down(MouseButton::Right));
        assert!(event.is_right_click());

        let event = MouseEvent::new(0, 0, MouseEventKind::Down(MouseButton::Left));
        assert!(!event.is_right_click());
    }

    #[test]
    fn test_mouse_event_is_scroll() {
        assert!(MouseEvent::new(0, 0, MouseEventKind::ScrollUp).is_scroll());
        assert!(MouseEvent::new(0, 0, MouseEventKind::ScrollDown).is_scroll());
        assert!(MouseEvent::new(0, 0, MouseEventKind::ScrollLeft).is_scroll());
        assert!(MouseEvent::new(0, 0, MouseEventKind::ScrollRight).is_scroll());
        assert!(!MouseEvent::new(0, 0, MouseEventKind::Move).is_scroll());
    }

    #[test]
    fn test_key_event_new() {
        let event = KeyEvent::new(Key::Char('a'));
        assert_eq!(event.key, Key::Char('a'));
        assert!(!event.ctrl);
        assert!(!event.alt);
        assert!(!event.shift);
    }

    #[test]
    fn test_key_event_ctrl() {
        let event = KeyEvent::ctrl(Key::Char('c'));
        assert!(event.ctrl);
        assert!(!event.alt);
        assert!(!event.shift);
    }

    #[test]
    fn test_key_event_alt() {
        let event = KeyEvent::alt(Key::Char('x'));
        assert!(!event.ctrl);
        assert!(event.alt);
        assert!(!event.shift);
    }

    #[test]
    fn test_key_event_is_ctrl_c() {
        let event = KeyEvent::ctrl(Key::Char('c'));
        assert!(event.is_ctrl_c());

        let event = KeyEvent::new(Key::Char('c'));
        assert!(!event.is_ctrl_c());
    }

    #[test]
    fn test_key_event_is_escape() {
        let event = KeyEvent::new(Key::Escape);
        assert!(event.is_escape());

        let event = KeyEvent::new(Key::Enter);
        assert!(!event.is_escape());
    }

    #[test]
    fn test_key_event_is_enter() {
        let event = KeyEvent::new(Key::Enter);
        assert!(event.is_enter());

        let event = KeyEvent::new(Key::Tab);
        assert!(!event.is_enter());
    }

    #[test]
    fn test_key_event_is_tab() {
        let event = KeyEvent::new(Key::Tab);
        assert!(event.is_tab());

        let event = KeyEvent {
            key: Key::Tab,
            ctrl: false,
            alt: false,
            shift: true,
        };
        assert!(!event.is_tab());
    }

    #[test]
    fn test_key_event_is_shift_tab() {
        let event = KeyEvent {
            key: Key::Tab,
            ctrl: false,
            alt: false,
            shift: true,
        };
        assert!(event.is_shift_tab());

        let event = KeyEvent::new(Key::Tab);
        assert!(!event.is_shift_tab());
    }

    #[test]
    fn test_key_event_to_binding() {
        let event = KeyEvent {
            key: Key::Char('a'),
            ctrl: true,
            alt: false,
            shift: true,
        };
        let binding = event.to_binding();
        assert_eq!(binding.key, Key::Char('a'));
        assert!(binding.ctrl);
        assert!(!binding.alt);
        assert!(binding.shift);
    }

    #[test]
    fn test_event_enum_variants() {
        let key_event = Event::Key(KeyEvent::new(Key::Char('a')));
        let mouse_event = Event::Mouse(MouseEvent::new(0, 0, MouseEventKind::Move));
        let resize_event = Event::Resize(80, 24);
        let tick_event = Event::Tick;
        let focus_gained = Event::FocusGained;
        let focus_lost = Event::FocusLost;
        let paste_event = Event::Paste("test".to_string());

        // Just verify they all compile - variants exist
        match key_event {
            Event::Key(_) => {}
            _ => panic!("Expected Key event"),
        }
        match mouse_event {
            Event::Mouse(_) => {}
            _ => panic!("Expected Mouse event"),
        }
        match resize_event {
            Event::Resize(_, _) => {}
            _ => panic!("Expected Resize event"),
        }
        match tick_event {
            Event::Tick => {}
            _ => panic!("Expected Tick event"),
        }
        match focus_gained {
            Event::FocusGained => {}
            _ => panic!("Expected FocusGained event"),
        }
        match focus_lost {
            Event::FocusLost => {}
            _ => panic!("Expected FocusLost event"),
        }
        match paste_event {
            Event::Paste(_) => {}
            _ => panic!("Expected Paste event"),
        }
    }

    #[test]
    fn test_mouse_button_variants() {
        let _left = MouseButton::Left;
        let _right = MouseButton::Right;
        let _middle = MouseButton::Middle;
    }

    #[test]
    fn test_mouse_event_kind_variants() {
        let _down = MouseEventKind::Down(MouseButton::Left);
        let _up = MouseEventKind::Up(MouseButton::Right);
        let _drag = MouseEventKind::Drag(MouseButton::Middle);
        let _move = MouseEventKind::Move;
        let _scroll_down = MouseEventKind::ScrollDown;
        let _scroll_up = MouseEventKind::ScrollUp;
        let _scroll_left = MouseEventKind::ScrollLeft;
        let _scroll_right = MouseEventKind::ScrollRight;
    }

    #[test]
    fn test_mouse_event_with_modifiers() {
        let event = MouseEvent {
            x: 10,
            y: 20,
            kind: MouseEventKind::Down(MouseButton::Left),
            ctrl: true,
            alt: false,
            shift: true,
        };
        assert!(event.ctrl);
        assert!(event.shift);
        assert!(!event.alt);
    }
}