lv-tui 0.4.0

A reactive TUI framework for Rust
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
use crate::backend::{CrosstermBackend, TerminalBackend};
use crate::buffer::Buffer;
use crate::component::Component;
use crate::dirty::Dirty;
use std::cell::RefCell;
use std::time::Instant;

use crate::event::{Command, Event, EventPhase, Key, KeyEvent};
use crate::geom::Rect;
use crate::geom::Pos;
use crate::node::{Node, NodeId};
use crate::Result;

thread_local! {
    pub static COMMAND_QUEUE: RefCell<Vec<Command>> = RefCell::new(Vec::new());
    pub static DEBUG_MODE: RefCell<bool> = RefCell::new(false);
    pub static FORCE_FULL_PAINT: RefCell<bool> = RefCell::new(false);
    pub static TIMER_QUEUE: RefCell<TimerQueue> = RefCell::new(TimerQueue { requests: Vec::new(), next_id: 0 });
    /// Worker cancel flags, keyed by WorkerId.
    pub static WORKER_REGISTRY: RefCell<WorkerRegistry> = RefCell::new(WorkerRegistry { flags: std::collections::HashMap::new(), next_id: 0 });
}

/// Tracks active workers and their cancel flags.
pub struct WorkerRegistry {
    pub flags: std::collections::HashMap<crate::event::WorkerId, std::sync::Arc<std::sync::atomic::AtomicBool>>,
    pub next_id: u64,
}

/// Accumulates timer requests from component event handlers.
pub struct TimerQueue {
    pub requests: Vec<TimerRequest>,
    pub next_id: u64,
}

// ── Timer types ──────────────────────────────────────────────────

/// An action that a component can request on a timer.
pub enum TimerAction {
    /// Register a one-shot timer.
    SetOneShot { id: u64, duration_ms: u64 },
    /// Register a periodic timer.
    SetInterval { id: u64, interval_ms: u64 },
    /// Cancel a timer by id.
    Cancel { id: u64 },
}

/// A pending timer request from a component's event handler.
pub struct TimerRequest {
    pub action: TimerAction,
    pub owner: NodeId,
}

/// A live timer managed by the Runtime.
pub struct TimerEntry {
    pub id: u64,
    /// `None` for one-shot (removed after firing).
    pub interval_ms: Option<u64>,
    pub next_fire: Instant,
    pub owner: NodeId,
}

/// The application runtime.
pub struct Runtime<B: TerminalBackend = CrosstermBackend> {
    pub(crate) root: Node,
    pub(crate) backend: B,
    pub(crate) front: Buffer,
    pub(crate) back: Buffer,
    pub(crate) root_rect: Rect,
    pub(crate) quit: bool,
    pub(crate) full_paint: bool,
    pub(crate) dirty: Dirty,
    pub(crate) focused_id: Option<NodeId>,
    pub(crate) tick_rate: std::time::Duration,
    pub(crate) task_rx: std::sync::mpsc::Receiver<String>,
    pub(crate) task_tx: std::sync::mpsc::Sender<String>,
    /// Active timers.
    pub(crate) timers: Vec<TimerEntry>,
}

const ZERO_RECT: Rect = Rect {
    x: 0,
    y: 0,
    width: 0,
    height: 0,
};

impl<B: TerminalBackend> Runtime<B> {
    /// Creates a new runtime wrapping the given root component and backend.
    pub fn new(root: impl Component + 'static, backend: B) -> Result<Self> {
        let (tx, rx) = std::sync::mpsc::channel::<String>();
        Ok(Self {
            root: Node::root(root),
            backend,
            front: Buffer::new(crate::geom::Size { width: 1, height: 1 }),
            back: Buffer::new(crate::geom::Size { width: 1, height: 1 }),
            root_rect: ZERO_RECT,
            quit: false,
            full_paint: false,
            dirty: Dirty::NONE,
            focused_id: None,
            tick_rate: std::time::Duration::from_millis(250),
            task_rx: rx,
            task_tx: tx,
            timers: Vec::new(),
        })
    }

    /// Builder: sets the tick interval (default 250ms).
    pub fn tick_rate(mut self, rate: std::time::Duration) -> Self {
        self.tick_rate = rate;
        self
    }

    /// Processes one iteration of the event loop.
    pub fn step(&mut self) -> Result<()> {
        if self.quit {
            return Ok(());
        }

        // 处理全局命令队列
        let commands: Vec<Command> = COMMAND_QUEUE.with(|q| q.borrow_mut().drain(..).collect());
        for cmd in commands {
            match cmd {
                Command::Quit => self.quit = true,
                Command::FocusNext => self.focus_next(false),
                Command::FocusPrev => self.focus_next(true),
                Command::Custom(_) => {}
            }
        }

        // 检查任务完成
        while let Ok(result) = self.task_rx.try_recv() {
            if let Some(rest) = result.strip_prefix("__worker_done__") {
                // New format: __worker_done__<id>__<payload>
                if let Some((id_str, payload)) = rest.split_once("__") {
                    if let Ok(id) = id_str.parse::<u64>() {
                        self.dispatch_event(Event::WorkerDone(
                            crate::event::WorkerId(id), payload.to_string()))?;
                    }
                }
            } else if let Some(rest) = result.strip_prefix("__worker_cancelled__") {
                if let Ok(id) = rest.parse::<u64>() {
                    self.dispatch_event(Event::WorkerDone(
                        crate::event::WorkerId(id), String::new()))?;
                }
            } else {
                // Old format: plain string from cx.spawn()
                self.dispatch_event(Event::TaskComplete(result))?;
            }
            if self.dirty.contains(Dirty::PAINT) {
                self.paint_frame()?;
                self.dirty = Dirty::NONE;
            }
        }

        // 处理过期的定时器
        let now = Instant::now();
        let mut expired: Vec<(u64, Option<u64>, NodeId)> = Vec::new(); // (id, interval_ms, owner)
        let mut i = 0;
        while i < self.timers.len() {
            if now >= self.timers[i].next_fire {
                let entry = &self.timers[i];
                expired.push((entry.id, entry.interval_ms, entry.owner));
                self.timers.remove(i);
            } else {
                i += 1;
            }
        }

        for (id, interval_ms, owner) in expired {
            self.dispatch_to_target(owner, &Event::Timer(id));

            if self.dirty.intersects(Dirty::LAYOUT | Dirty::TREE) {
                self.layout_children()?;
            }
            if self.dirty.contains(Dirty::PAINT) {
                self.paint_frame()?;
                self.dirty = Dirty::NONE;
            }

            // Re-add periodic timer for next interval
            if let Some(interval) = interval_ms {
                self.timers.push(TimerEntry {
                    id,
                    interval_ms: Some(interval),
                    next_fire: now + std::time::Duration::from_millis(interval),
                    owner,
                });
            }
        }

        // Calculate poll timeout — use smallest of tick_rate and next timer fire
        let poll_timeout = if self.task_rx.try_recv().is_ok() {
            std::time::Duration::from_millis(0)
        } else {
            let next_timer = self.timers.iter().map(|t| {
                let remaining = t.next_fire.duration_since(now);
                if remaining.as_millis() > 0 {
                    remaining
                } else {
                    std::time::Duration::from_millis(0)
                }
            }).min();
            match next_timer {
                Some(t) if t < self.tick_rate => t,
                _ => self.tick_rate,
            }
        };

        let event = match self.backend.poll_event(poll_timeout)? {
            Some(e) => e,
            None => Event::Tick,
        };
        self.dispatch_event(event)?;

        // Process timer requests collected during event dispatch
        self.process_timer_requests();

        if self.dirty.intersects(Dirty::LAYOUT | Dirty::TREE) {
            self.layout_children()?;
        }

        if self.dirty.contains(Dirty::PAINT) {
            self.paint_frame()?;
            self.dirty = Dirty::NONE;
        }

        Ok(())
    }

    /// Drains TIMER_QUEUE and applies set/interval/cancel requests.
    pub(crate) fn process_timer_requests(&mut self) {
        let requests: Vec<TimerRequest> = TIMER_QUEUE.with(|q| q.borrow_mut().requests.drain(..).collect());
        let now = Instant::now();
        for req in requests {
            match req.action {
                TimerAction::SetOneShot { id, duration_ms } => {
                    self.timers.push(TimerEntry {
                        id,
                        interval_ms: None,
                        next_fire: now + std::time::Duration::from_millis(duration_ms),
                        owner: req.owner,
                    });
                }
                TimerAction::SetInterval { id, interval_ms } => {
                    self.timers.push(TimerEntry {
                        id,
                        interval_ms: Some(interval_ms),
                        next_fire: now + std::time::Duration::from_millis(interval_ms),
                        owner: req.owner,
                    });
                }
                TimerAction::Cancel { id } => {
                    self.timers.retain(|t| t.id != id);
                }
            }
        }
    }

    /// Starts the event loop. Blocks until the application exits.
    pub fn run(&mut self) -> Result<()> {
        self.backend.enter()?;
        self.initial_layout_and_paint()?;
        // Process timer requests from mount() lifecycle
        self.process_timer_requests();

        while !self.quit {
            self.step()?;
        }

        // 卸载生命周期
        self.root.unmount(&mut self.dirty, &mut self.quit, Some(self.task_tx.clone()));
        self.backend.leave()?;
        Ok(())
    }

    pub fn initial_layout_and_paint(&mut self) -> Result<()> {
        let size = self.backend.size()?;

        self.root_rect = Rect {
            x: 0,
            y: 0,
            width: size.width,
            height: size.height,
        };

        self.front = Buffer::new(size);
        self.back = Buffer::new(size);

        self.full_paint = true;
        self.dirty = Dirty::LAYOUT | Dirty::PAINT;

        self.layout_children()?;

        // 挂载生命周期
        self.root.mount(&mut self.dirty, &mut self.quit, Some(self.task_tx.clone()));

        // 启动时自动聚焦第一个可聚焦组件
        let mut ids = Vec::new();
        self.root.collect_focusable(&mut ids);
        if !ids.is_empty() {
            self.focused_id = Some(ids[0]);
        }

        self.paint_frame()?;
        self.dirty = Dirty::NONE;

        Ok(())
    }

    fn dispatch_event(&mut self, event: Event) -> Result<()> {
        match event {
            Event::Resize(size) => {
                let new_rect = Rect {
                    x: 0,
                    y: 0,
                    width: size.width,
                    height: size.height,
                };

                if new_rect != self.root_rect {
                    self.root_rect = new_rect;
                    self.front.resize(size);
                    self.back.resize(size);
                    self.full_paint = true;
                    self.dirty = Dirty::LAYOUT | Dirty::PAINT;
                }
            }
            Event::Key(KeyEvent { key: Key::Tab, modifiers }) => {
                self.focus_next(modifiers.shift);
            }
            Event::Mouse(mouse_event) => {
                let pos = Pos {
                    x: mouse_event.x,
                    y: mouse_event.y,
                };
                if let Some(id) = self.root.hit_test(pos) {
                    self.dispatch_to_target(id, &Event::Mouse(mouse_event));
                }
            }
            Event::Focus | Event::Blur => {
                // 焦点事件由 set_focus 内发送
            }
            other => {
                // 键盘等事件发给焦点组件(有焦点时)或 root
                let target = self.focused_id.unwrap_or(NodeId::ROOT);
                self.dispatch_to_target(target, &other);
            }
        }

        Ok(())
    }

    pub(crate) fn focus_next(&mut self, backwards: bool) {
        let mut ids = Vec::new();
        self.root.collect_focusable(&mut ids);

        if ids.is_empty() {
            return;
        }

        let old_id = self.focused_id;

        let new_idx = match old_id {
            None => {
                if backwards {
                    ids.len() - 1
                } else {
                    0
                }
            }
            Some(old) => {
                if let Some(pos) = ids.iter().position(|id| *id == old) {
                    if backwards {
                        if pos == 0 {
                            ids.len() - 1
                        } else {
                            pos - 1
                        }
                    } else if pos + 1 >= ids.len() {
                        0
                    } else {
                        pos + 1
                    }
                } else {
                    0
                }
            }
        };

        let new_id = ids[new_idx];
        self.set_focus(new_id);
    }

    /// 三阶段事件分发:capture → target → bubble
    fn dispatch_to_target(&mut self, target_id: NodeId, event: &Event) {
        // 找到从 root 到 target 的完整路径
        let (path, effective_target) = match self.root.find_path_to(target_id) {
                Some(p) => (p, target_id),
                None => (vec![NodeId::ROOT], NodeId::ROOT),
            };

        let mut stopped = false;

        // Capture: root → target 的祖先(正序,不含 target 自身)
        for &node_id in &path[..path.len().saturating_sub(1)] {
            if stopped {
                break;
            }
            self.root.send_event(
                node_id,
                event,
                &mut self.dirty,
                &mut self.quit,
                EventPhase::Capture,
                &mut stopped,
                Some(self.task_tx.clone()),
            );
        }

        // Target: 目标自身
        if !stopped {
            self.root.send_event(
                effective_target,
                event,
                &mut self.dirty,
                &mut self.quit,
                EventPhase::Target,
                &mut stopped,
                Some(self.task_tx.clone()),
            );
        }

        // Bubble: target 的祖先 → root(逆序,不含 target)
        for &node_id in path[..path.len().saturating_sub(1)].iter().rev() {
            if stopped {
                break;
            }
            self.root.send_event(
                node_id,
                event,
                &mut self.dirty,
                &mut self.quit,
                EventPhase::Bubble,
                &mut stopped,
                Some(self.task_tx.clone()),
            );
        }
    }

    fn set_focus(&mut self, new_id: NodeId) {
        let old_id = self.focused_id;

        // 发送 Blur 给旧焦点(直接目标,不冒泡)
        if let Some(old) = old_id {
            if old != new_id {
                let mut stopped = false;
                self.root.send_event(
                    old,
                    &Event::Blur,
                    &mut self.dirty,
                    &mut self.quit,
                    EventPhase::Target,
                    &mut stopped,
                Some(self.task_tx.clone()),
                );
            }
        }

        self.focused_id = Some(new_id);

        // 发送 Focus 给新焦点
        if old_id != Some(new_id) {
            let mut stopped = false;
            self.root.send_event(
                new_id,
                &Event::Focus,
                &mut self.dirty,
                &mut self.quit,
                EventPhase::Target,
                &mut stopped,
                Some(self.task_tx.clone()),
            );
        }

        self.dirty |= Dirty::PAINT;
    }

    pub(crate) fn layout_children(&mut self) -> Result<()> {
        self.root.layout(self.root_rect);
        Ok(())
    }

    pub(crate) fn paint_frame(&mut self) -> Result<()> {
        self.back.clear();
        self.root.render_with_clip(&mut self.back, self.focused_id, None);

        // 全局 debug 渲染
        if DEBUG_MODE.with(|d| *d.borrow()) {
            self.root.debug_render(&mut self.back, self.focused_id);
        }

        if self.full_paint || FORCE_FULL_PAINT.with(|f| f.replace(false)) {
            let ops = self.back.all_ops();
            self.backend.flush(&ops)?;
            self.full_paint = false;
        } else {
            let ops = self.front.diff(&self.back);
            self.backend.flush(&ops)?;
        }

        std::mem::swap(&mut self.front, &mut self.back);

        Ok(())
    }
}