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
//! Input Demo
//!
//! This example demonstrates the keyboard and mouse input capabilities of MinUI,
//! displaying events in a structured panel on the screen.
use minui::MouseButton;
use minui::input::ClickTracker;
use minui::prelude::*;
use std::collections::VecDeque;
use std::time::Duration;
// NOTE: `TextBlock` currently doesn't treat `\n` as hard line breaks in its wrapping logic
// (it wraps based on whitespace). For input demo, we want one event per line.
// Until `TextBlock` becomes newline-aware, render log as a vertical `Container` of `Label`s.
const MAX_EVENTS: usize = 12;
struct InputDemoState {
event_log: VecDeque<String>,
mouse_pos: (u16, u16),
click_tracker: ClickTracker,
}
fn main() -> minui::Result<()> {
let initial_state = InputDemoState {
event_log: {
let mut log = VecDeque::new();
log.push_back("Welcome to MinUI Input Demo!".to_string());
log.push_back("Try typing, moving mouse, clicking...".to_string());
log.push_back("Double-click quickly to see double-click detection!".to_string());
log.push_back("Press 'q' to quit".to_string());
log
},
mouse_pos: (0, 0),
click_tracker: ClickTracker::new(),
};
let mut app = App::new(initial_state)?.with_frame_rate(Duration::from_millis(16));
app.run(
|state, event| {
// Return false to exit (supports modifier-aware and legacy events).
if let Event::KeyWithModifiers(k) = event {
if matches!(k.key, KeyKind::Char('q')) {
return false;
}
}
if matches!(event, Event::Character('q')) {
return false;
}
// Handle events and update state
match event {
// Prefer modifier-aware keyboard events (the keyboard handler may emit these for most keys).
Event::KeyWithModifiers(k) => match k.key {
KeyKind::Char(c) => {
state.event_log.push_back(format!(
"Key: '{}' (mods: shift={}, ctrl={}, alt={}, super={})",
c, k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Up => {
state.event_log.push_back(format!(
"Key: ↑ Up (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Down => {
state.event_log.push_back(format!(
"Key: ↓ Down (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Left => {
state.event_log.push_back(format!(
"Key: ← Left (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Right => {
state.event_log.push_back(format!(
"Key: → Right (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Enter => {
state.event_log.push_back(format!(
"Key: ⏎ Enter (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Escape => {
state.event_log.push_back(format!(
"Key: Escape (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Backspace => {
state.event_log.push_back(format!(
"Key: ⌫ Backspace (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Delete => {
state.event_log.push_back(format!(
"Key: ⌦ Delete (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Tab => {
state.event_log.push_back(format!(
"Key: Tab (mods: shift={}, ctrl={}, alt={}, super={})",
k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
KeyKind::Function(n) => {
state.event_log.push_back(format!(
"Key: F{} (mods: shift={}, ctrl={}, alt={}, super={})",
n, k.mods.shift, k.mods.ctrl, k.mods.alt, k.mods.super_key
));
}
},
// Legacy fallback keyboard events (some backends may still emit these).
Event::Character(c) => {
state.event_log.push_back(format!("Key: '{}'", c));
}
Event::Paste(text) => {
// Keep the log readable for large pastes
let preview: String = text.chars().take(60).collect();
if text.chars().count() > 60 {
state.event_log.push_back(format!(
"Paste: \"{}…\" ({} chars)",
preview,
text.chars().count()
));
} else {
state.event_log.push_back(format!("Paste: \"{}\"", preview));
}
}
Event::KeyUp => {
state.event_log.push_back("Key: ↑ Up".to_string());
}
Event::KeyDown => {
state.event_log.push_back("Key: ↓ Down".to_string());
}
Event::KeyLeft => {
state.event_log.push_back("Key: ← Left".to_string());
}
Event::KeyRight => {
state.event_log.push_back("Key: → Right".to_string());
}
Event::Enter => {
state.event_log.push_back("Key: ⏎ Enter".to_string());
}
Event::Escape => {
state.event_log.push_back("Key: Escape".to_string());
}
Event::Backspace => {
state.event_log.push_back("Key: ⌫ Backspace".to_string());
}
Event::Delete => {
state.event_log.push_back("Key: ⌦ Delete".to_string());
}
Event::FunctionKey(n) => {
state.event_log.push_back(format!("Key: F{}", n));
}
// Handle mouse events
Event::MouseMove { x, y } => {
state.mouse_pos = (x, y);
// Only log occasional moves to avoid spam
if x % 3 == 0 && y % 3 == 0 {
state
.event_log
.push_back(format!("Mouse: Moved to ({}, {})", x, y));
}
}
Event::MouseClick { x, y, button } => {
state.mouse_pos = (x, y);
let button_name = match button {
MouseButton::Left => "Left",
MouseButton::Right => "Right",
MouseButton::Middle => "Middle",
MouseButton::Other(_) => "Other",
};
// Check for double-click
if state.click_tracker.is_double_click(x, y) {
state.event_log.push_back(format!(
"Mouse: DOUBLE-CLICK! {} button at ({}, {})",
button_name, x, y
));
} else {
state
.event_log
.push_back(format!("Mouse: {} click at ({}, {})", button_name, x, y));
}
}
Event::MouseDrag { x, y, button } => {
state.mouse_pos = (x, y);
let button_name = match button {
MouseButton::Left => "Left",
MouseButton::Right => "Right",
MouseButton::Middle => "Middle",
MouseButton::Other(_) => "Other",
};
state
.event_log
.push_back(format!("Mouse: {} drag to ({}, {})", button_name, x, y));
}
Event::MouseScroll { delta } => {
let direction = if delta > 0 { "up" } else { "down" };
state
.event_log
.push_back(format!("Mouse: Scroll {} ({})", direction, delta));
}
Event::MouseScrollHorizontal { delta } => {
let direction = if delta > 0 { "right" } else { "left" };
state
.event_log
.push_back(format!("Mouse: Scroll {} ({})", direction, delta));
}
Event::MouseRelease { x, y, button } => {
state.mouse_pos = (x, y);
let button_name = match button {
MouseButton::Left => "Left",
MouseButton::Right => "Right",
MouseButton::Middle => "Middle",
MouseButton::Other(_) => "Other",
};
state
.event_log
.push_back(format!("Mouse: {} release at ({}, {})", button_name, x, y));
}
Event::Resize { width, height } => {
state
.event_log
.push_back(format!("Terminal: Resized to {}x{}", width, height));
}
_ => {}
}
// Keep the log at reasonable size
if state.event_log.len() > MAX_EVENTS {
state.event_log.pop_front();
}
true
},
|state, window| {
let (term_width, term_height) = window.get_size();
// Create a container to display the events.
//
// Panel has been absorbed into Container: use borders + title + padding, and put
// content widgets inside as children.
// NOTE: `ContainerPadding` is the name exported by the prelude for Container's padding type.
// (The underlying type in `container.rs` is `Padding`.)
use minui::widgets::ContainerPadding;
let panel_x: u16 = 2u16;
let panel_y: u16 = 1u16;
let panel_w: u16 = term_width.saturating_sub(4u16);
let panel_h: u16 = term_height.saturating_sub(4u16);
// Render the log as stacked labels so each event appears on its own line.
// We display the newest entries at the top (reverse chronological).
let mut log_container = Container::vertical().with_row_gap(Gap::Pixels(0u16));
if state.event_log.is_empty() {
log_container = log_container.add_child(Label::new("No events yet..."));
} else {
for line in state.event_log.iter().rev().take(MAX_EVENTS) {
log_container = log_container.add_child(Label::new(line.clone()));
}
}
let panel = Container::new()
.with_position_and_size(panel_x, panel_y, panel_w, panel_h)
.with_border()
.with_border_chars(BorderChars::double_line())
.with_border_color(ColorPair::new(Color::Cyan, Color::Black))
.with_title("MinUI Input Demo")
.with_title_alignment(TitleAlignment::Center)
.with_padding(ContainerPadding::uniform(1u16))
.add_child(log_container);
panel.draw(window)?;
// Draw mouse position info at bottom
let mouse_info = format!("Mouse: ({}, {})", state.mouse_pos.0, state.mouse_pos.1);
let info_y = term_height.saturating_sub(2);
window.write_str_colored(
info_y,
2,
&mouse_info,
ColorPair::new(Color::Cyan, Color::Transparent),
)?;
// Draw instructions at the very bottom
let help_text = "Press 'q' to quit | Try typing, clicking, scrolling!";
let help_x = (term_width.saturating_sub(help_text.len() as u16)) / 2;
let help_y = term_height.saturating_sub(1);
window.write_str_colored(
help_y,
help_x,
help_text,
ColorPair::new(Color::DarkGray, Color::Transparent),
)?;
window.flush()?;
Ok(())
},
)?;
Ok(())
}