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
use super::*;
impl Context {
/// Render a help bar showing keybinding hints.
///
/// `bindings` is a slice of `(key, action)` pairs. Keys are rendered in the
/// theme's primary color; actions in the dim text color. Pairs are separated
/// by a `·` character.
pub fn help(&mut self, bindings: &[(&str, &str)]) -> Response {
if bindings.is_empty() {
return Response::none();
}
self.skip_interaction_slot();
self.commands
.push(Command::BeginContainer(Box::new(BeginContainerArgs {
direction: Direction::Row,
gap: 2,
align: Align::Start,
align_self: None,
justify: Justify::Start,
border: None,
border_sides: BorderSides::all(),
border_style: Style::new().fg(self.theme.border),
bg_color: None,
padding: Padding::default(),
margin: Margin::default(),
constraints: Constraints::default(),
title: None,
grow: 0,
group_name: None,
})));
for (idx, (key, action)) in bindings.iter().enumerate() {
if idx > 0 {
self.styled("·", Style::new().fg(self.theme.text_dim));
}
self.styled(*key, Style::new().bold().fg(self.theme.primary));
self.styled(*action, Style::new().fg(self.theme.text_dim));
}
self.commands.push(Command::EndContainer);
self.rollback.last_text_idx = None;
Response::none()
}
/// Render a help bar with custom key/description colors.
pub fn help_colored(
&mut self,
bindings: &[(&str, &str)],
key_color: Color,
text_color: Color,
) -> Response {
if bindings.is_empty() {
return Response::none();
}
self.skip_interaction_slot();
self.commands
.push(Command::BeginContainer(Box::new(BeginContainerArgs {
direction: Direction::Row,
gap: 2,
align: Align::Start,
align_self: None,
justify: Justify::Start,
border: None,
border_sides: BorderSides::all(),
border_style: Style::new().fg(self.theme.border),
bg_color: None,
padding: Padding::default(),
margin: Margin::default(),
constraints: Constraints::default(),
title: None,
grow: 0,
group_name: None,
})));
for (idx, (key, action)) in bindings.iter().enumerate() {
if idx > 0 {
self.styled("·", Style::new().fg(text_color));
}
self.styled(*key, Style::new().bold().fg(key_color));
self.styled(*action, Style::new().fg(text_color));
}
self.commands.push(Command::EndContainer);
self.rollback.last_text_idx = None;
Response::none()
}
// ── events ───────────────────────────────────────────────────────
/// Check if a character key was pressed this frame.
///
/// Returns `true` if the key event has not been consumed by another widget.
pub fn key(&self, c: char) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, e)| {
!self.consumed[i]
&& matches!(e, Event::Key(k) if k.kind == KeyEventKind::Press && k.code == KeyCode::Char(c))
})
}
/// Check if a specific key code was pressed this frame.
///
/// Returns `true` if the key event has not been consumed by another widget.
/// Blocked when a modal/overlay is active and the caller is outside the overlay.
/// Use [`raw_key_code`](Self::raw_key_code) for global shortcuts that must work
/// regardless of modal/overlay state.
pub fn key_code(&self, code: KeyCode) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, e)| {
!self.consumed[i]
&& matches!(e, Event::Key(k) if k.kind == KeyEventKind::Press && k.code == code)
})
}
/// Check if a specific key code was pressed this frame, ignoring modal/overlay state.
///
/// Unlike [`key_code`](Self::key_code), this method bypasses the modal/overlay guard
/// so it works even when a modal or overlay is active. Use this for global shortcuts
/// (e.g. Esc to close a modal, Ctrl+Q to quit) that must always be reachable.
///
/// Returns `true` if the key event has not been consumed by another widget.
pub fn raw_key_code(&self, code: KeyCode) -> bool {
self.events.iter().enumerate().any(|(i, e)| {
!self.consumed[i]
&& matches!(e, Event::Key(k) if k.kind == KeyEventKind::Press && k.code == code)
})
}
/// Check if a character key was released this frame.
///
/// Returns `true` if the key release event has not been consumed by another widget.
pub fn key_release(&self, c: char) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, e)| {
!self.consumed[i]
&& matches!(e, Event::Key(k) if k.kind == KeyEventKind::Release && k.code == KeyCode::Char(c))
})
}
/// Check if a specific key code was released this frame.
///
/// Returns `true` if the key release event has not been consumed by another widget.
pub fn key_code_release(&self, code: KeyCode) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, e)| {
!self.consumed[i]
&& matches!(e, Event::Key(k) if k.kind == KeyEventKind::Release && k.code == code)
})
}
/// Check for a character key press and consume the event, preventing other
/// handlers from seeing it.
///
/// Returns `true` if the key was found unconsumed and is now consumed.
/// Unlike [`key()`](Self::key) which peeks without consuming, this claims
/// exclusive ownership of the event.
///
/// Call **after** widgets if you want widgets to have priority over your
/// handler, or **before** widgets to intercept first.
pub fn consume_key(&mut self, c: char) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
let index = self.available_key_presses().find_map(|(i, key)| {
if key.code == KeyCode::Char(c) {
Some(i)
} else {
None
}
});
if let Some(index) = index {
self.consume_indices([index]);
true
} else {
false
}
}
/// Check for a special key press and consume the event, preventing other
/// handlers from seeing it.
///
/// Returns `true` if the key was found unconsumed and is now consumed.
/// Unlike [`key_code()`](Self::key_code) which peeks without consuming,
/// this claims exclusive ownership of the event.
///
/// Call **after** widgets if you want widgets to have priority over your
/// handler, or **before** widgets to intercept first.
pub fn consume_key_code(&mut self, code: KeyCode) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
let index =
self.available_key_presses().find_map(
|(i, key)| {
if key.code == code {
Some(i)
} else {
None
}
},
);
if let Some(index) = index {
self.consume_indices([index]);
true
} else {
false
}
}
/// Check if a character key with specific modifiers was pressed this frame.
///
/// Returns `true` if the key event has not been consumed by another widget.
pub fn key_mod(&self, c: char, modifiers: KeyModifiers) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, e)| {
!self.consumed[i]
&& matches!(e, Event::Key(k) if k.kind == KeyEventKind::Press && k.code == KeyCode::Char(c) && k.modifiers.contains(modifiers))
})
}
/// Like [`key_mod`](Self::key_mod) but bypasses the modal/overlay guard.
pub fn raw_key_mod(&self, c: char, modifiers: KeyModifiers) -> bool {
self.events.iter().enumerate().any(|(i, e)| {
!self.consumed[i]
&& matches!(e, Event::Key(k) if k.kind == KeyEventKind::Press && k.code == KeyCode::Char(c) && k.modifiers.contains(modifiers))
})
}
/// Return the position of a left mouse button down event this frame, if any.
///
/// Returns `None` if no unconsumed mouse-down event occurred.
pub fn mouse_down(&self) -> Option<(u32, u32)> {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return None;
}
self.events.iter().enumerate().find_map(|(i, event)| {
if self.consumed[i] {
return None;
}
if let Event::Mouse(mouse) = event {
if matches!(mouse.kind, MouseKind::Down(MouseButton::Left)) {
return Some((mouse.x, mouse.y));
}
}
None
})
}
/// Return the position of a left mouse button drag event this frame, if any.
///
/// Returns `None` if no unconsumed drag event occurred. Drag events fire
/// while the left button is held and the cursor moves.
pub fn mouse_drag(&self) -> Option<(u32, u32)> {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return None;
}
self.events.iter().enumerate().find_map(|(i, event)| {
if self.consumed[i] {
return None;
}
if let Event::Mouse(mouse) = event {
if matches!(mouse.kind, MouseKind::Drag(MouseButton::Left)) {
return Some((mouse.x, mouse.y));
}
}
None
})
}
/// Return the position of a left mouse button release event this frame, if any.
///
/// Returns `None` if no unconsumed mouse-up event occurred.
pub fn mouse_up(&self) -> Option<(u32, u32)> {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return None;
}
self.events.iter().enumerate().find_map(|(i, event)| {
if self.consumed[i] {
return None;
}
if let Event::Mouse(mouse) = event {
if matches!(mouse.kind, MouseKind::Up(MouseButton::Left)) {
return Some((mouse.x, mouse.y));
}
}
None
})
}
/// Return the position of a mouse button down event for the specified button.
///
/// This is a generalized version of [`mouse_down`](Self::mouse_down) that
/// accepts any [`MouseButton`].
pub fn mouse_down_button(&self, button: MouseButton) -> Option<(u32, u32)> {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return None;
}
self.events.iter().enumerate().find_map(|(i, event)| {
if self.consumed[i] {
return None;
}
if let Event::Mouse(mouse) = event {
if matches!(&mouse.kind, MouseKind::Down(b) if *b == button) {
return Some((mouse.x, mouse.y));
}
}
None
})
}
/// Return the position of a mouse drag event for the specified button.
pub fn mouse_drag_button(&self, button: MouseButton) -> Option<(u32, u32)> {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return None;
}
self.events.iter().enumerate().find_map(|(i, event)| {
if self.consumed[i] {
return None;
}
if let Event::Mouse(mouse) = event {
if matches!(&mouse.kind, MouseKind::Drag(b) if *b == button) {
return Some((mouse.x, mouse.y));
}
}
None
})
}
/// Return the position of a mouse button release event for the specified button.
pub fn mouse_up_button(&self, button: MouseButton) -> Option<(u32, u32)> {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return None;
}
self.events.iter().enumerate().find_map(|(i, event)| {
if self.consumed[i] {
return None;
}
if let Event::Mouse(mouse) = event {
if matches!(&mouse.kind, MouseKind::Up(b) if *b == button) {
return Some((mouse.x, mouse.y));
}
}
None
})
}
/// Return the current mouse cursor position, if known.
///
/// The position is updated on every mouse move or click event. Returns
/// `None` until the first mouse event is received.
pub fn mouse_pos(&self) -> Option<(u32, u32)> {
self.mouse_pos
}
/// Return the first unconsumed paste event text, if any.
pub fn paste(&self) -> Option<&str> {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return None;
}
self.events.iter().enumerate().find_map(|(i, event)| {
if self.consumed[i] {
return None;
}
if let Event::Paste(ref text) = event {
return Some(text.as_str());
}
None
})
}
/// Check if an unconsumed scroll-up event occurred this frame.
pub fn scroll_up(&self) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, event)| {
!self.consumed[i]
&& matches!(event, Event::Mouse(mouse) if matches!(mouse.kind, MouseKind::ScrollUp))
})
}
/// Check if an unconsumed scroll-down event occurred this frame.
pub fn scroll_down(&self) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, event)| {
!self.consumed[i]
&& matches!(event, Event::Mouse(mouse) if matches!(mouse.kind, MouseKind::ScrollDown))
})
}
/// Check if an unconsumed scroll-left event occurred this frame.
pub fn scroll_left(&self) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, event)| {
!self.consumed[i]
&& matches!(event, Event::Mouse(mouse) if matches!(mouse.kind, MouseKind::ScrollLeft))
})
}
/// Check if an unconsumed scroll-right event occurred this frame.
pub fn scroll_right(&self) -> bool {
if (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0
{
return false;
}
self.events.iter().enumerate().any(|(i, event)| {
!self.consumed[i]
&& matches!(event, Event::Mouse(mouse) if matches!(mouse.kind, MouseKind::ScrollRight))
})
}
/// Iterate over unconsumed events this frame, respecting the modal guard.
///
/// Returns an empty iterator when a modal is active and the caller is not
/// inside an overlay. Use [`raw_events`](Self::raw_events) to bypass the
/// modal guard (e.g., for global hotkeys).
///
/// # Example
///
/// ```no_run
/// # slt::run(|ui: &mut slt::Context| {
/// for event in ui.events() {
/// if let slt::Event::Mouse(mouse) = event {
/// if matches!(mouse.kind, slt::MouseKind::Down(slt::MouseButton::Right)) {
/// // handle right-click
/// }
/// }
/// }
/// # });
/// ```
pub fn events(&self) -> impl Iterator<Item = &Event> {
let blocked = (self.rollback.modal_active || self.prev_modal_active)
&& self.rollback.overlay_depth == 0;
self.events.iter().enumerate().filter_map(move |(i, e)| {
if blocked || self.consumed[i] {
None
} else {
Some(e)
}
})
}
/// Iterate over all unconsumed events, bypassing the modal guard.
///
/// Use this for global shortcuts that must work even when a modal or
/// overlay is active. Prefer [`events`](Self::events) for normal use.
pub fn raw_events(&self) -> impl Iterator<Item = &Event> + '_ {
self.events
.iter()
.enumerate()
.filter_map(|(i, e)| if self.consumed[i] { None } else { Some(e) })
}
/// Signal the run loop to exit after this frame.
pub fn quit(&mut self) {
self.should_quit = true;
}
/// Copy text to the system clipboard via OSC 52.
///
/// Works transparently over SSH connections. The text is queued and
/// written to the terminal after the current frame renders.
///
/// Requires a terminal that supports OSC 52 (most modern terminals:
/// Ghostty, kitty, WezTerm, iTerm2, Windows Terminal).
pub fn copy_to_clipboard(&mut self, text: impl Into<String>) {
self.clipboard_text = Some(text.into());
}
/// Get the current theme.
pub fn theme(&self) -> &Theme {
&self.theme
}
/// Resolve a [`ThemeColor`] token against the current theme.
pub fn color(&self, token: ThemeColor) -> Color {
self.theme.resolve(token)
}
/// Get the current spacing scale from the theme.
pub fn spacing(&self) -> Spacing {
self.theme.spacing
}
/// Change the theme for subsequent rendering.
///
/// All widgets rendered after this call will use the new theme's colors.
pub fn set_theme(&mut self, theme: Theme) {
self.theme = theme;
}
/// Check if dark mode is active.
pub fn is_dark_mode(&self) -> bool {
self.rollback.dark_mode
}
/// Set dark mode. When true, dark_* style variants are applied.
pub fn set_dark_mode(&mut self, dark: bool) {
self.rollback.dark_mode = dark;
}
// ── info ─────────────────────────────────────────────────────────
/// Get the terminal width in cells.
pub fn width(&self) -> u32 {
self.area_width
}
/// Get the current terminal width breakpoint.
///
/// Returns a [`Breakpoint`] based on the terminal width:
/// - `Xs`: < 40 columns
/// - `Sm`: 40-79 columns
/// - `Md`: 80-119 columns
/// - `Lg`: 120-159 columns
/// - `Xl`: >= 160 columns
///
/// Use this for responsive layouts that adapt to terminal size:
/// ```no_run
/// # use slt::{Breakpoint, Context};
/// # slt::run(|ui: &mut Context| {
/// match ui.breakpoint() {
/// Breakpoint::Xs | Breakpoint::Sm => {
/// ui.col(|ui| { ui.text("Stacked layout"); });
/// }
/// _ => {
/// ui.row(|ui| { ui.text("Side-by-side layout"); });
/// }
/// }
/// # });
/// ```
pub fn breakpoint(&self) -> Breakpoint {
let w = self.area_width;
if w < 40 {
Breakpoint::Xs
} else if w < 80 {
Breakpoint::Sm
} else if w < 120 {
Breakpoint::Md
} else if w < 160 {
Breakpoint::Lg
} else {
Breakpoint::Xl
}
}
/// Get the terminal height in cells.
pub fn height(&self) -> u32 {
self.area_height
}
/// Get the current tick count (increments each frame).
///
/// Useful for animations and time-based logic. The tick starts at 0 and
/// increases by 1 on every rendered frame.
pub fn tick(&self) -> u64 {
self.tick
}
/// Return whether the layout debugger is enabled.
///
/// The debugger is toggled with F12 at runtime.
pub fn debug_enabled(&self) -> bool {
self.debug
}
}