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
//! Event context for sharing events between components
//!
//! This module provides a context for sharing events between components,
//! allowing child components to access the current event without having to
//! pass it through props.
use Event;
use ;
use Lazy;
use debug;
use cratewith_hook_context;
/// Structure to track an event and whether it has been processed
pub
/// Global storage for the current event
pub static CURRENT_EVENT: = new;
/// Sets the current event in the global storage
///
/// This function should be called by the App when an event is received.
///
/// # Arguments
///
/// * `event` - The event to set in the context
/// Gets the current event from the context
///
/// This function should be called by components to access the current event.
/// Each component can only access the event once per event cycle.
///
/// # Returns
///
/// * `Option<Arc<Event>>` - The current event, or None if no event is available or already processed
pub
/// Marks the current event as processed by the specified component
///
/// # Arguments
///
/// * `component_id` - The ID of the component that processed the event
/// A React-style hook that returns the current terminal event being processed
///
/// This hook allows components to handle terminal events like keyboard, mouse, and resize events
/// in their render methods. Each hook instance can access an event exactly once per render cycle.
///
/// # Event Types
///
/// Handles all Crossterm event types:
/// - `Event::Key` - Keyboard events with modifiers (Ctrl, Alt, Shift)
/// - `Event::Mouse` - Mouse clicks, drags, scrolls, and movement
/// - `Event::Resize` - Terminal window resize events
/// - `Event::FocusGained`/`Event::FocusLost` - Terminal focus events
/// - `Event::Paste` - Paste events
///
/// # Usage Patterns
///
/// 1. Keyboard Event Handling:
/// ```rust,no_run
/// # use reratui_hooks::event::use_event;
/// # use crossterm::event::{Event, KeyCode, KeyModifiers};
/// // In a component context:
/// if let Some(Event::Key(key)) = use_event() {
/// match key.code {
/// KeyCode::Char('q') if key.modifiers.contains(KeyModifiers::CONTROL) => {
/// // Handle Ctrl+Q
/// }
/// KeyCode::Enter => {
/// // Handle Enter key
/// }
/// _ => {}
/// }
/// }
/// ```
///
/// 2. Mouse Event Handling:
/// ```rust,no_run
/// # use reratui_hooks::event::use_event;
/// # use crossterm::event::{Event, MouseEventKind, MouseButton};
/// // In a component context:
/// if let Some(Event::Mouse(mouse)) = use_event() {
/// match mouse.kind {
/// MouseEventKind::Down(MouseButton::Left) => {
/// // Handle left click at (mouse.column, mouse.row)
/// }
/// MouseEventKind::Drag(MouseButton::Left) => {
/// // Handle drag
/// }
/// _ => {}
/// }
/// }
/// ```
///
/// 3. Resize Event Handling:
/// ```rust,no_run
/// # use reratui_hooks::event::use_event;
/// # use crossterm::event::Event;
/// // In a component context:
/// if let Some(Event::Resize(width, height)) = use_event() {
/// // Handle terminal resize
/// }
/// ```
///
/// # Integration with Other Hooks
///
/// Works seamlessly with other hooks like `use_state`:
/// ```rust,no_run
/// # use reratui_hooks::event::use_event;
/// # use reratui_hooks::state::use_state;
/// # use crossterm::event::Event;
/// // In a component context:
/// let (key_count, set_key_count) = use_state(|| 0);
/// if let Some(Event::Key(_)) = use_event() {
/// set_key_count.update(|prev| prev + 1);
/// }
/// ```
///
/// # Note
///
/// - Events are consumed when accessed - each event can only be handled once per hook
/// - Multiple components can use this hook independently
/// - Events are cleared at the start of each render cycle
/// - Use with `use_state` for tracking event-related state
///
/// # Returns
///
/// * `Option<Event>` - The current event if available and not yet processed by this hook,
/// or None if no event is available or already processed