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
|
// Each component declares it's own size constraints and gets fitted based on it's parent.
// Q: how does this work with popups?
// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
use crossterm::event::Event;
use helix_core::Position;
use smol::Executor;
use tui::buffer::Buffer as Surface;
use tui::layout::Rect;
pub type Callback = Box<dyn FnOnce(&mut Compositor, &mut Editor)>;
// --> EventResult should have a callback that takes a context with methods like .popup(),
// .prompt() etc. That way we can abstract it from the renderer.
// Q: How does this interact with popups where we need to be able to specify the rendering of the
// popup?
// A: It could just take a textarea.
//
// If Compositor was specified in the callback that's then problematic because of
// Cursive-inspired
pub enum EventResult {
Ignored,
Consumed(Option<Callback>),
}
use helix_view::{Editor, View};
pub struct Context<'a> {
pub editor: &'a mut Editor,
pub executor: &'static smol::Executor<'static>,
pub scroll: Option<usize>,
}
pub trait Component {
/// Process input events, return true if handled.
fn handle_event(&mut self, event: Event, ctx: &mut Context) -> EventResult {
EventResult::Ignored
}
// , args: ()
/// Should redraw? Useful for saving redraw cycles if we know component didn't change.
fn should_update(&self) -> bool {
true
}
/// Render the component onto the provided surface.
fn render(&self, area: Rect, frame: &mut Surface, ctx: &mut Context);
fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
None
}
/// May be used by the parent component to compute the child area.
/// viewport is the maximum allowed area, and the child should stay within those bounds.
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
// TODO: the compositor should trigger this on push_layer too so that we can use it as an
// initializer there too.
//
// TODO: for scrolling, the scroll wrapper should place a size + offset on the Context
// that way render can use it
None
}
}
use anyhow::Error;
use std::io::stdout;
use tui::backend::CrosstermBackend;
type Terminal = crate::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
pub struct Compositor {
layers: Vec<Box<dyn Component>>,
terminal: Terminal,
}
impl Compositor {
pub fn new() -> Result<Self, Error> {
let backend = CrosstermBackend::new(stdout());
let mut terminal = Terminal::new(backend)?;
Ok(Self {
layers: Vec::new(),
terminal,
})
}
pub fn size(&self) -> Rect {
self.terminal.size().expect("couldn't get terminal size")
}
pub fn resize(&mut self, width: u16, height: u16) {
self.terminal
.resize(Rect::new(0, 0, width, height))
.expect("Unable to resize terminal")
}
pub fn push(&mut self, layer: Box<dyn Component>) {
self.layers.push(layer);
}
pub fn pop(&mut self) {
self.layers.pop();
}
pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool {
// propagate events through the layers until we either find a layer that consumes it or we
// run out of layers (event bubbling)
for layer in self.layers.iter_mut().rev() {
match layer.handle_event(event, cx) {
EventResult::Consumed(Some(callback)) => {
callback(self, cx.editor);
return true;
}
EventResult::Consumed(None) => return true,
EventResult::Ignored => false,
};
}
false
}
pub fn render(&mut self, cx: &mut Context) {
let area = self.size();
let surface = self.terminal.current_buffer_mut();
for layer in &self.layers {
layer.render(area, surface, cx)
}
let pos = self
.cursor_position(area, cx.editor)
.map(|pos| (pos.col as u16, pos.row as u16));
self.terminal.draw(pos);
}
pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
for layer in self.layers.iter().rev() {
if let Some(pos) = layer.cursor_position(area, editor) {
return Some(pos);
}
}
None
}
}
|