Skip to main content

rab/tui/components/
rc_ref_cell_component.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3
4use crossterm::event::KeyEvent;
5
6use crate::tui::Component;
7
8/// A Component wrapper around `Rc<RefCell<dyn Component>>` for shared ownership.
9/// Allows App to hold a `Weak<RefCell<dyn Component>>` for in-place updates.
10pub struct RcRefCellComponent(pub Rc<RefCell<dyn Component>>);
11
12impl Clone for RcRefCellComponent {
13    fn clone(&self) -> Self {
14        Self(self.0.clone())
15    }
16}
17
18impl Component for RcRefCellComponent {
19    fn render(&mut self, width: usize) -> Vec<String> {
20        self.0.borrow_mut().render(width)
21    }
22
23    fn handle_input(&mut self, key: &KeyEvent) -> bool {
24        self.0.borrow_mut().handle_input(key)
25    }
26
27    fn set_expanded(&mut self, expanded: bool) {
28        self.0.borrow_mut().set_expanded(expanded);
29    }
30
31    fn set_hide_thinking(&mut self, hide: bool) {
32        self.0.borrow_mut().set_hide_thinking(hide);
33    }
34
35    fn invalidate(&mut self) {
36        self.0.borrow_mut().invalidate();
37    }
38}