Skip to main content

slt/context/
core.rs

1use super::*;
2
3/// The main rendering context passed to your closure each frame.
4///
5/// Provides all methods for building UI: text, containers, widgets, and event
6/// handling. You receive a `&mut Context` on every frame and describe what to
7/// render by calling its methods. SLT collects those calls, lays them out with
8/// flexbox, diffs against the previous frame, and flushes only changed cells.
9///
10/// # Example
11///
12/// ```no_run
13/// slt::run(|ui: &mut slt::Context| {
14///     if ui.key('q') { ui.quit(); }
15///     ui.text("Hello, world!").bold();
16/// });
17/// ```
18pub struct Context {
19    pub(crate) commands: Vec<Command>,
20    pub(crate) events: Vec<Event>,
21    pub(crate) consumed: Vec<bool>,
22    pub(crate) should_quit: bool,
23    pub(crate) area_width: u32,
24    pub(crate) area_height: u32,
25    pub(crate) tick: u64,
26    pub(crate) focus_index: usize,
27    pub(crate) hook_states: Vec<Box<dyn std::any::Any>>,
28    pub(crate) prev_focus_count: usize,
29    pub(crate) prev_modal_focus_start: usize,
30    pub(crate) prev_modal_focus_count: usize,
31    pub(crate) prev_scroll_infos: Vec<(u32, u32)>,
32    pub(crate) prev_scroll_rects: Vec<Rect>,
33    pub(crate) prev_hit_map: Vec<Rect>,
34    pub(crate) prev_group_rects: Vec<(std::sync::Arc<str>, Rect)>,
35    pub(crate) prev_focus_groups: Vec<Option<std::sync::Arc<str>>>,
36    pub(crate) _prev_focus_rects: Vec<(usize, Rect)>,
37    pub(crate) mouse_pos: Option<(u32, u32)>,
38    pub(crate) click_pos: Option<(u32, u32)>,
39    pub(crate) prev_modal_active: bool,
40    pub(crate) clipboard_text: Option<String>,
41    pub(crate) debug: bool,
42    pub(crate) theme: Theme,
43    pub(crate) is_real_terminal: bool,
44    pub(crate) deferred_draws: Vec<Option<RawDrawCallback>>,
45    pub(crate) rollback: ContextRollbackState,
46    pub(crate) scroll_lines_per_event: u32,
47    pub(crate) screen_hook_map: std::collections::HashMap<String, (usize, usize)>,
48    pub(crate) widget_theme: WidgetTheme,
49}
50
51type RawDrawCallback = Box<dyn FnOnce(&mut crate::buffer::Buffer, Rect)>;
52
53#[derive(Debug, Clone)]
54pub(crate) struct PendingTooltip {
55    pub anchor_rect: Rect,
56    pub lines: Vec<String>,
57}
58
59#[derive(Clone)]
60pub(crate) struct ContextRollbackState {
61    pub(crate) last_text_idx: Option<usize>,
62    pub(crate) focus_count: usize,
63    pub(crate) interaction_count: usize,
64    pub(crate) scroll_count: usize,
65    pub(crate) group_count: usize,
66    pub(crate) group_stack: Vec<String>,
67    pub(crate) overlay_depth: usize,
68    pub(crate) modal_active: bool,
69    pub(crate) modal_focus_start: usize,
70    pub(crate) modal_focus_count: usize,
71    pub(crate) hook_cursor: usize,
72    pub(crate) dark_mode: bool,
73    pub(crate) notification_queue: Vec<(String, ToastLevel, u64)>,
74    pub(crate) pending_tooltips: Vec<PendingTooltip>,
75    pub(crate) text_color_stack: Vec<Option<Color>>,
76}
77
78pub(super) struct ContextCheckpoint {
79    commands_len: usize,
80    hook_states_len: usize,
81    deferred_draws_len: usize,
82    rollback: ContextRollbackState,
83}
84
85impl ContextCheckpoint {
86    pub(super) fn capture(ctx: &Context) -> Self {
87        Self {
88            commands_len: ctx.commands.len(),
89            hook_states_len: ctx.hook_states.len(),
90            deferred_draws_len: ctx.deferred_draws.len(),
91            rollback: ctx.rollback.clone(),
92        }
93    }
94
95    pub(super) fn restore(&self, ctx: &mut Context) {
96        ctx.commands.truncate(self.commands_len);
97        ctx.hook_states.truncate(self.hook_states_len);
98        ctx.deferred_draws.truncate(self.deferred_draws_len);
99        ctx.rollback = self.rollback.clone();
100    }
101}