2020-12-06 03:53:58 +01:00
|
|
|
// 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>)
|
2021-06-20 21:31:45 +02:00
|
|
|
use helix_core::Position;
|
2021-06-25 05:58:15 +02:00
|
|
|
use helix_view::graphics::{CursorKind, Rect};
|
2020-12-06 03:53:58 +01:00
|
|
|
|
|
|
|
use crossterm::event::Event;
|
2022-04-27 06:07:59 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "integration")]
|
|
|
|
use tui::backend::TestBackend;
|
2021-06-25 05:58:15 +02:00
|
|
|
use tui::buffer::Buffer as Surface;
|
2020-12-06 03:53:58 +01:00
|
|
|
|
2021-12-12 13:16:48 +01:00
|
|
|
pub type Callback = Box<dyn FnOnce(&mut Compositor, &mut Context)>;
|
2020-12-07 07:18:37 +01:00
|
|
|
|
|
|
|
// Cursive-inspired
|
2020-12-11 10:25:09 +01:00
|
|
|
pub enum EventResult {
|
2022-02-23 04:46:12 +01:00
|
|
|
Ignored(Option<Callback>),
|
2020-12-07 07:18:37 +01:00
|
|
|
Consumed(Option<Callback>),
|
|
|
|
}
|
|
|
|
|
2021-05-09 11:02:31 +02:00
|
|
|
use helix_view::Editor;
|
2021-03-08 09:00:32 +01:00
|
|
|
|
2021-06-28 14:48:38 +02:00
|
|
|
use crate::job::Jobs;
|
2021-03-26 08:02:13 +01:00
|
|
|
|
2020-12-13 04:23:50 +01:00
|
|
|
pub struct Context<'a> {
|
2020-12-11 10:25:09 +01:00
|
|
|
pub editor: &'a mut Editor,
|
2021-03-08 09:32:13 +01:00
|
|
|
pub scroll: Option<usize>,
|
2021-06-28 14:48:38 +02:00
|
|
|
pub jobs: &'a mut Jobs,
|
2020-12-11 10:25:09 +01:00
|
|
|
}
|
|
|
|
|
2021-04-05 11:23:37 +02:00
|
|
|
pub trait Component: Any + AnyComponent {
|
2020-12-06 03:53:58 +01:00
|
|
|
/// Process input events, return true if handled.
|
2021-07-01 20:57:12 +02:00
|
|
|
fn handle_event(&mut self, _event: Event, _ctx: &mut Context) -> EventResult {
|
2022-02-23 04:46:12 +01:00
|
|
|
EventResult::Ignored(None)
|
2021-03-02 10:24:24 +01:00
|
|
|
}
|
2020-12-06 03:53:58 +01:00
|
|
|
// , args: ()
|
|
|
|
|
|
|
|
/// Should redraw? Useful for saving redraw cycles if we know component didn't change.
|
|
|
|
fn should_update(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:00:32 +01:00
|
|
|
/// Render the component onto the provided surface.
|
2021-08-12 09:00:42 +02:00
|
|
|
fn render(&mut self, area: Rect, frame: &mut Surface, ctx: &mut Context);
|
2020-12-13 05:29:34 +01:00
|
|
|
|
2021-06-15 07:03:56 +02:00
|
|
|
/// Get cursor position and cursor kind.
|
2021-07-01 20:57:12 +02:00
|
|
|
fn cursor(&self, _area: Rect, _ctx: &Editor) -> (Option<Position>, CursorKind) {
|
2021-06-15 07:03:56 +02:00
|
|
|
(None, CursorKind::Hidden)
|
2020-12-13 05:29:34 +01:00
|
|
|
}
|
2021-03-02 10:24:24 +01:00
|
|
|
|
2021-03-08 09:00:32 +01:00
|
|
|
/// 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.
|
2021-12-10 11:23:34 +01:00
|
|
|
///
|
|
|
|
/// The returned size might be larger than the viewport if the child is too big to fit.
|
|
|
|
/// In this case the parent can use the values to calculate scroll.
|
2021-07-01 20:57:12 +02:00
|
|
|
fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> {
|
2021-03-02 10:24:24 +01:00
|
|
|
None
|
|
|
|
}
|
2021-04-05 11:23:37 +02:00
|
|
|
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
std::any::type_name::<Self>()
|
|
|
|
}
|
2021-12-08 08:11:18 +01:00
|
|
|
|
|
|
|
fn id(&self) -> Option<&'static str> {
|
|
|
|
None
|
|
|
|
}
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|
|
|
|
|
2022-04-27 01:18:20 +02:00
|
|
|
use anyhow::Context as AnyhowContext;
|
2022-04-27 06:07:59 +02:00
|
|
|
use tui::backend::Backend;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "integration"))]
|
|
|
|
use tui::backend::CrosstermBackend;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "integration"))]
|
2021-03-08 09:00:32 +01:00
|
|
|
use std::io::stdout;
|
2022-04-27 06:07:59 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "integration"))]
|
2021-05-09 10:52:55 +02:00
|
|
|
type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
|
2020-12-06 03:53:58 +01:00
|
|
|
|
2022-04-27 06:07:59 +02:00
|
|
|
#[cfg(feature = "integration")]
|
|
|
|
type Terminal = tui::terminal::Terminal<TestBackend>;
|
|
|
|
|
2020-12-11 10:25:09 +01:00
|
|
|
pub struct Compositor {
|
2020-12-06 03:53:58 +01:00
|
|
|
layers: Vec<Box<dyn Component>>,
|
2021-03-08 09:00:32 +01:00
|
|
|
terminal: Terminal,
|
2021-07-18 06:24:07 +02:00
|
|
|
|
|
|
|
pub(crate) last_picker: Option<Box<dyn Component>>,
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Compositor {
|
2022-04-27 01:18:20 +02:00
|
|
|
pub fn new() -> anyhow::Result<Self> {
|
2022-04-27 06:07:59 +02:00
|
|
|
#[cfg(not(feature = "integration"))]
|
2021-03-08 09:00:32 +01:00
|
|
|
let backend = CrosstermBackend::new(stdout());
|
2022-04-27 06:07:59 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "integration")]
|
|
|
|
let backend = TestBackend::new(120, 150);
|
|
|
|
|
2022-04-27 01:18:20 +02:00
|
|
|
let terminal = Terminal::new(backend).context("build terminal")?;
|
2021-03-08 09:00:32 +01:00
|
|
|
Ok(Self {
|
|
|
|
layers: Vec::new(),
|
|
|
|
terminal,
|
2021-07-18 06:24:07 +02:00
|
|
|
last_picker: None,
|
2021-03-08 09:00:32 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|
|
|
|
|
2021-07-17 16:47:08 +02:00
|
|
|
pub fn save_cursor(&mut self) {
|
|
|
|
if self.terminal.cursor_kind() == CursorKind::Hidden {
|
|
|
|
self.terminal
|
|
|
|
.backend_mut()
|
|
|
|
.show_cursor(CursorKind::Block)
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_cursor(&mut self) {
|
|
|
|
if self.terminal.cursor_kind() == CursorKind::Hidden {
|
|
|
|
self.terminal.backend_mut().hide_cursor().ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 04:06:40 +01:00
|
|
|
pub fn push(&mut self, mut layer: Box<dyn Component>) {
|
|
|
|
let size = self.size();
|
|
|
|
// trigger required_size on init
|
|
|
|
layer.required_size((size.width, size.height));
|
2020-12-06 03:53:58 +01:00
|
|
|
self.layers.push(layer);
|
|
|
|
}
|
|
|
|
|
2022-02-08 04:31:24 +01:00
|
|
|
/// Replace a component that has the given `id` with the new layer and if
|
|
|
|
/// no component is found, push the layer normally.
|
2022-03-03 02:14:50 +01:00
|
|
|
pub fn replace_or_push<T: Component>(&mut self, id: &'static str, layer: T) {
|
2022-02-08 04:31:24 +01:00
|
|
|
if let Some(component) = self.find_id(id) {
|
|
|
|
*component = layer;
|
|
|
|
} else {
|
2022-03-03 02:14:50 +01:00
|
|
|
self.push(Box::new(layer))
|
2022-02-08 04:31:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 06:24:07 +02:00
|
|
|
pub fn pop(&mut self) -> Option<Box<dyn Component>> {
|
|
|
|
self.layers.pop()
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 04:28:24 +02:00
|
|
|
pub fn remove(&mut self, id: &'static str) -> Option<Box<dyn Component>> {
|
|
|
|
let idx = self
|
|
|
|
.layers
|
|
|
|
.iter()
|
|
|
|
.position(|layer| layer.id() == Some(id))?;
|
|
|
|
Some(self.layers.remove(idx))
|
|
|
|
}
|
|
|
|
|
2020-12-11 10:25:09 +01:00
|
|
|
pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool {
|
2021-12-12 13:16:48 +01:00
|
|
|
// If it is a key event and a macro is being recorded, push the key event to the recording.
|
|
|
|
if let (Event::Key(key), Some((_, keys))) = (event, &mut cx.editor.macro_recording) {
|
|
|
|
keys.push(key.into());
|
|
|
|
}
|
|
|
|
|
2022-02-23 04:46:12 +01:00
|
|
|
let mut callbacks = Vec::new();
|
|
|
|
let mut consumed = false;
|
|
|
|
|
2021-02-09 07:40:30 +01:00
|
|
|
// 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) {
|
2020-12-11 10:25:09 +01:00
|
|
|
EventResult::Consumed(Some(callback)) => {
|
2022-02-23 04:46:12 +01:00
|
|
|
callbacks.push(callback);
|
|
|
|
consumed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
EventResult::Consumed(None) => {
|
|
|
|
consumed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
EventResult::Ignored(Some(callback)) => {
|
|
|
|
callbacks.push(callback);
|
2020-12-11 10:25:09 +01:00
|
|
|
}
|
2022-02-23 04:46:12 +01:00
|
|
|
EventResult::Ignored(None) => {}
|
2020-12-11 10:25:09 +01:00
|
|
|
};
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|
2022-02-23 04:46:12 +01:00
|
|
|
|
|
|
|
for callback in callbacks {
|
|
|
|
callback(self, cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
consumed
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-08 09:00:32 +01:00
|
|
|
pub fn render(&mut self, cx: &mut Context) {
|
2021-07-01 20:57:12 +02:00
|
|
|
self.terminal
|
2021-06-03 03:28:49 +02:00
|
|
|
.autoresize()
|
|
|
|
.expect("Unable to determine terminal size");
|
|
|
|
|
|
|
|
// TODO: need to recalculate view tree if necessary
|
2021-06-02 15:40:08 +02:00
|
|
|
|
2021-03-08 09:00:32 +01:00
|
|
|
let surface = self.terminal.current_buffer_mut();
|
|
|
|
|
2021-06-03 03:31:14 +02:00
|
|
|
let area = *surface.area();
|
2021-06-03 03:28:49 +02:00
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
for layer in &mut self.layers {
|
|
|
|
layer.render(area, surface, cx);
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|
2021-03-08 09:00:32 +01:00
|
|
|
|
2021-06-15 07:03:56 +02:00
|
|
|
let (pos, kind) = self.cursor(area, cx.editor);
|
|
|
|
let pos = pos.map(|pos| (pos.col as u16, pos.row as u16));
|
2021-03-08 09:00:32 +01:00
|
|
|
|
2021-07-01 21:08:00 +02:00
|
|
|
self.terminal.draw(pos, kind).unwrap();
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|
2020-12-13 05:29:34 +01:00
|
|
|
|
2021-06-15 07:03:56 +02:00
|
|
|
pub fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
|
2020-12-13 05:29:34 +01:00
|
|
|
for layer in self.layers.iter().rev() {
|
2021-06-15 07:03:56 +02:00
|
|
|
if let (Some(pos), kind) = layer.cursor(area, editor) {
|
|
|
|
return (Some(pos), kind);
|
2020-12-13 05:29:34 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-15 07:03:56 +02:00
|
|
|
(None, CursorKind::Hidden)
|
2020-12-13 05:29:34 +01:00
|
|
|
}
|
2021-04-05 11:23:37 +02:00
|
|
|
|
2021-10-15 10:36:39 +02:00
|
|
|
pub fn has_component(&self, type_name: &str) -> bool {
|
|
|
|
self.layers
|
|
|
|
.iter()
|
|
|
|
.any(|component| component.type_name() == type_name)
|
|
|
|
}
|
|
|
|
|
2021-11-18 03:08:47 +01:00
|
|
|
pub fn find<T: 'static>(&mut self) -> Option<&mut T> {
|
|
|
|
let type_name = std::any::type_name::<T>();
|
2021-04-05 11:23:37 +02:00
|
|
|
self.layers
|
|
|
|
.iter_mut()
|
|
|
|
.find(|component| component.type_name() == type_name)
|
2021-11-18 03:08:47 +01:00
|
|
|
.and_then(|component| component.as_any_mut().downcast_mut())
|
2021-12-08 08:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_id<T: 'static>(&mut self, id: &'static str) -> Option<&mut T> {
|
|
|
|
self.layers
|
|
|
|
.iter_mut()
|
2022-03-03 02:14:50 +01:00
|
|
|
.find(|component| component.id() == Some(id))
|
2021-12-08 08:11:18 +01:00
|
|
|
.and_then(|component| component.as_any_mut().downcast_mut())
|
2021-04-05 11:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// View casting, taken straight from Cursive
|
|
|
|
|
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
/// A view that can be downcasted to its concrete type.
|
|
|
|
///
|
|
|
|
/// This trait is automatically implemented for any `T: Component`.
|
|
|
|
pub trait AnyComponent {
|
|
|
|
/// Downcast self to a `Any`.
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
|
|
|
|
/// Downcast self to a mutable `Any`.
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
|
|
|
|
|
|
|
/// Returns a boxed any from a boxed self.
|
|
|
|
///
|
|
|
|
/// Can be used before `Box::downcast()`.
|
2021-06-25 08:20:06 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use helix_term::{ui::Text, compositor::Component};
|
2021-10-22 05:46:51 +02:00
|
|
|
/// let boxed: Box<dyn Component> = Box::new(Text::new("text".to_string()));
|
2021-06-25 08:20:06 +02:00
|
|
|
/// let text: Box<Text> = boxed.as_boxed_any().downcast().unwrap();
|
|
|
|
/// ```
|
2021-04-05 11:23:37 +02:00
|
|
|
fn as_boxed_any(self: Box<Self>) -> Box<dyn Any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Component> AnyComponent for T {
|
|
|
|
/// Downcast self to a `Any`.
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Downcast self to a mutable `Any`.
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_boxed_any(self: Box<Self>) -> Box<dyn Any> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl dyn AnyComponent {
|
|
|
|
/// Attempts to downcast `self` to a concrete type.
|
|
|
|
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
|
|
|
|
self.as_any().downcast_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to downcast `self` to a concrete type.
|
|
|
|
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
|
|
|
|
self.as_any_mut().downcast_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to downcast `Box<Self>` to a concrete type.
|
|
|
|
pub fn downcast<T: Any>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
|
|
|
|
// Do the check here + unwrap, so the error
|
|
|
|
// value is `Self` and not `dyn Any`.
|
|
|
|
if self.as_any().is::<T>() {
|
|
|
|
Ok(self.as_boxed_any().downcast().unwrap())
|
|
|
|
} else {
|
|
|
|
Err(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if this view is of type `T`.
|
|
|
|
pub fn is<T: Any>(&mut self) -> bool {
|
|
|
|
self.as_any().is::<T>()
|
|
|
|
}
|
2020-12-06 03:53:58 +01:00
|
|
|
}
|