From 264a455c18f765b04c31a613ccc336dd893c4cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Tue, 29 Mar 2022 10:11:44 +0900 Subject: [PATCH] Move terminal out of compositor --- helix-term/src/application.rs | 75 +++++++++++++++++++++++++++-------- helix-term/src/compositor.rs | 55 +++---------------------- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 19eb2e5c1..433104dcc 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -10,11 +10,13 @@ use helix_view::{ align_view, document::DocumentSavedEventResult, editor::{ConfigEvent, EditorEvent}, + graphics::Rect, theme, tree::Layout, Align, Editor, }; use serde_json::json; +use tui::backend::Backend; use crate::{ args::Args, @@ -53,8 +55,18 @@ type Signals = futures_util::stream::Empty<()>; const LSP_DEADLINE: Duration = Duration::from_millis(16); +#[cfg(not(feature = "integration"))] +use tui::backend::CrosstermBackend; + +#[cfg(not(feature = "integration"))] +type Terminal = tui::terminal::Terminal>; + +#[cfg(feature = "integration")] +type Terminal = tui::terminal::Terminal; + pub struct Application { compositor: Compositor, + terminal: Terminal, pub editor: Editor, config: Arc>, @@ -143,10 +155,18 @@ impl Application { let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf)); - let mut compositor = Compositor::new().context("build compositor")?; + #[cfg(not(feature = "integration"))] + let backend = CrosstermBackend::new(stdout()); + + #[cfg(feature = "integration")] + let backend = TestBackend::new(120, 150); + + let terminal = Terminal::new(backend)?; + let area = terminal.size().expect("couldn't get terminal size"); + let mut compositor = Compositor::new(area); let config = Arc::new(ArcSwap::from_pointee(config)); let mut editor = Editor::new( - compositor.size(), + area, theme_loader.clone(), syn_loader.clone(), Box::new(Map::new(Arc::clone(&config), |config: &Config| { @@ -233,6 +253,7 @@ impl Application { let app = Self { compositor, + terminal, editor, config, @@ -254,15 +275,26 @@ impl Application { #[cfg(not(feature = "integration"))] fn render(&mut self) { - let compositor = &mut self.compositor; - let mut cx = crate::compositor::Context { editor: &mut self.editor, jobs: &mut self.jobs, scroll: None, }; - compositor.render(&mut cx); + let area = self + .terminal + .autoresize() + .expect("Unable to determine terminal size"); + + // TODO: need to recalculate view tree if necessary + + let surface = self.terminal.current_buffer_mut(); + + self.compositor.render(area, surface, &mut cx); + + let (pos, kind) = self.compositor.cursor(area, &self.editor); + let pos = pos.map(|pos| (pos.col as u16, pos.row as u16)); + self.terminal.draw(pos, kind).unwrap(); } pub async fn event_loop(&mut self, input_stream: &mut S) @@ -392,17 +424,24 @@ impl Application { #[cfg(not(windows))] pub async fn handle_signals(&mut self, signal: i32) { - use helix_view::graphics::Rect; match signal { signal::SIGTSTP => { + // restore cursor + use helix_view::graphics::CursorKind; + self.terminal + .backend_mut() + .show_cursor(CursorKind::Block) + .ok(); restore_term().unwrap(); low_level::emulate_default_handler(signal::SIGTSTP).unwrap(); } signal::SIGCONT => { self.claim_term().await.unwrap(); // redraw the terminal - let Rect { width, height, .. } = self.compositor.size(); - self.compositor.resize(width, height); + let area = self.terminal.size().expect("couldn't get terminal size"); + self.compositor.resize(area); + self.terminal.clear().expect("couldn't clear terminal"); + self.render(); } signal::SIGUSR1 => { @@ -539,7 +578,14 @@ impl Application { // Handle key events let should_redraw = match event.unwrap() { CrosstermEvent::Resize(width, height) => { - self.compositor.resize(width, height); + self.terminal + .resize(Rect::new(0, 0, width, height)) + .expect("Unable to resize terminal"); + + let area = self.terminal.size().expect("couldn't get terminal size"); + + self.compositor.resize(area); + self.compositor .handle_event(&Event::Resize(width, height), &mut cx) } @@ -923,10 +969,9 @@ impl Application { async fn claim_term(&mut self) -> Result<(), Error> { use helix_view::graphics::CursorKind; - use tui::backend::Backend; terminal::enable_raw_mode()?; - if self.compositor.terminal.cursor_kind() == CursorKind::Hidden { - self.compositor.terminal.backend_mut().hide_cursor().ok(); + if self.terminal.cursor_kind() == CursorKind::Hidden { + self.terminal.backend_mut().hide_cursor().ok(); } let mut stdout = stdout(); execute!( @@ -962,14 +1007,12 @@ impl Application { let close_errs = self.close().await; + // restore cursor use helix_view::graphics::CursorKind; - use tui::backend::Backend; - self.compositor - .terminal + self.terminal .backend_mut() .show_cursor(CursorKind::Block) .ok(); - restore_term()?; for err in close_errs { diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index 1452d31f9..bbcd21810 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -75,56 +75,28 @@ pub trait Component: Any + AnyComponent { } } -use anyhow::Context as AnyhowContext; - -#[cfg(not(feature = "integration"))] -use tui::backend::CrosstermBackend; - -#[cfg(not(feature = "integration"))] -use std::io::stdout; - -#[cfg(not(feature = "integration"))] -type Terminal = tui::terminal::Terminal>; - -#[cfg(feature = "integration")] -type Terminal = tui::terminal::Terminal; - pub struct Compositor { layers: Vec>, - pub terminal: Terminal, area: Rect, pub(crate) last_picker: Option>, } impl Compositor { - pub fn new() -> anyhow::Result { - #[cfg(not(feature = "integration"))] - let backend = CrosstermBackend::new(stdout()); - - #[cfg(feature = "integration")] - let backend = TestBackend::new(120, 150); - - let terminal = Terminal::new(backend).context("build terminal")?; - let area = terminal.size().expect("couldn't get terminal size"); - Ok(Self { + pub fn new(area: Rect) -> Self { + Self { layers: Vec::new(), area, - terminal, last_picker: None, - }) + } } pub fn size(&self) -> Rect { self.area } - pub fn resize(&mut self, width: u16, height: u16) { - self.terminal - .resize(Rect::new(0, 0, width, height)) - .expect("Unable to resize terminal"); - - self.area = self.terminal.size().expect("couldn't get terminal size"); + pub fn resize(&mut self, area: Rect) { + self.area = area; } pub fn push(&mut self, mut layer: Box) { @@ -192,25 +164,10 @@ impl Compositor { consumed } - pub fn render(&mut self, cx: &mut Context) { - self.terminal - .autoresize() - .expect("Unable to determine terminal size"); - - // TODO: need to recalculate view tree if necessary - - let surface = self.terminal.current_buffer_mut(); - - let area = *surface.area(); - + pub fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { for layer in &mut self.layers { layer.render(area, surface, cx); } - - let (pos, kind) = self.cursor(area, cx.editor); - let pos = pos.map(|pos| (pos.col as u16, pos.row as u16)); - - self.terminal.draw(pos, kind).unwrap(); } pub fn cursor(&self, area: Rect, editor: &Editor) -> (Option, CursorKind) {