Move terminal out of compositor

pull/4678/head
Blaž Hrastnik 2 years ago
parent 00d7b18097
commit 264a455c18
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640

@ -10,11 +10,13 @@ use helix_view::{
align_view, align_view,
document::DocumentSavedEventResult, document::DocumentSavedEventResult,
editor::{ConfigEvent, EditorEvent}, editor::{ConfigEvent, EditorEvent},
graphics::Rect,
theme, theme,
tree::Layout, tree::Layout,
Align, Editor, Align, Editor,
}; };
use serde_json::json; use serde_json::json;
use tui::backend::Backend;
use crate::{ use crate::{
args::Args, args::Args,
@ -53,8 +55,18 @@ type Signals = futures_util::stream::Empty<()>;
const LSP_DEADLINE: Duration = Duration::from_millis(16); 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<CrosstermBackend<std::io::Stdout>>;
#[cfg(feature = "integration")]
type Terminal = tui::terminal::Terminal<TestBackend>;
pub struct Application { pub struct Application {
compositor: Compositor, compositor: Compositor,
terminal: Terminal,
pub editor: Editor, pub editor: Editor,
config: Arc<ArcSwap<Config>>, config: Arc<ArcSwap<Config>>,
@ -143,10 +155,18 @@ impl Application {
let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf)); 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 config = Arc::new(ArcSwap::from_pointee(config));
let mut editor = Editor::new( let mut editor = Editor::new(
compositor.size(), area,
theme_loader.clone(), theme_loader.clone(),
syn_loader.clone(), syn_loader.clone(),
Box::new(Map::new(Arc::clone(&config), |config: &Config| { Box::new(Map::new(Arc::clone(&config), |config: &Config| {
@ -233,6 +253,7 @@ impl Application {
let app = Self { let app = Self {
compositor, compositor,
terminal,
editor, editor,
config, config,
@ -254,15 +275,26 @@ impl Application {
#[cfg(not(feature = "integration"))] #[cfg(not(feature = "integration"))]
fn render(&mut self) { fn render(&mut self) {
let compositor = &mut self.compositor;
let mut cx = crate::compositor::Context { let mut cx = crate::compositor::Context {
editor: &mut self.editor, editor: &mut self.editor,
jobs: &mut self.jobs, jobs: &mut self.jobs,
scroll: None, 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<S>(&mut self, input_stream: &mut S) pub async fn event_loop<S>(&mut self, input_stream: &mut S)
@ -392,17 +424,24 @@ impl Application {
#[cfg(not(windows))] #[cfg(not(windows))]
pub async fn handle_signals(&mut self, signal: i32) { pub async fn handle_signals(&mut self, signal: i32) {
use helix_view::graphics::Rect;
match signal { match signal {
signal::SIGTSTP => { signal::SIGTSTP => {
// restore cursor
use helix_view::graphics::CursorKind;
self.terminal
.backend_mut()
.show_cursor(CursorKind::Block)
.ok();
restore_term().unwrap(); restore_term().unwrap();
low_level::emulate_default_handler(signal::SIGTSTP).unwrap(); low_level::emulate_default_handler(signal::SIGTSTP).unwrap();
} }
signal::SIGCONT => { signal::SIGCONT => {
self.claim_term().await.unwrap(); self.claim_term().await.unwrap();
// redraw the terminal // redraw the terminal
let Rect { width, height, .. } = self.compositor.size(); let area = self.terminal.size().expect("couldn't get terminal size");
self.compositor.resize(width, height); self.compositor.resize(area);
self.terminal.clear().expect("couldn't clear terminal");
self.render(); self.render();
} }
signal::SIGUSR1 => { signal::SIGUSR1 => {
@ -539,7 +578,14 @@ impl Application {
// Handle key events // Handle key events
let should_redraw = match event.unwrap() { let should_redraw = match event.unwrap() {
CrosstermEvent::Resize(width, height) => { 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 self.compositor
.handle_event(&Event::Resize(width, height), &mut cx) .handle_event(&Event::Resize(width, height), &mut cx)
} }
@ -923,10 +969,9 @@ impl Application {
async fn claim_term(&mut self) -> Result<(), Error> { async fn claim_term(&mut self) -> Result<(), Error> {
use helix_view::graphics::CursorKind; use helix_view::graphics::CursorKind;
use tui::backend::Backend;
terminal::enable_raw_mode()?; terminal::enable_raw_mode()?;
if self.compositor.terminal.cursor_kind() == CursorKind::Hidden { if self.terminal.cursor_kind() == CursorKind::Hidden {
self.compositor.terminal.backend_mut().hide_cursor().ok(); self.terminal.backend_mut().hide_cursor().ok();
} }
let mut stdout = stdout(); let mut stdout = stdout();
execute!( execute!(
@ -962,14 +1007,12 @@ impl Application {
let close_errs = self.close().await; let close_errs = self.close().await;
// restore cursor
use helix_view::graphics::CursorKind; use helix_view::graphics::CursorKind;
use tui::backend::Backend; self.terminal
self.compositor
.terminal
.backend_mut() .backend_mut()
.show_cursor(CursorKind::Block) .show_cursor(CursorKind::Block)
.ok(); .ok();
restore_term()?; restore_term()?;
for err in close_errs { for err in close_errs {

@ -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<CrosstermBackend<std::io::Stdout>>;
#[cfg(feature = "integration")]
type Terminal = tui::terminal::Terminal<TestBackend>;
pub struct Compositor { pub struct Compositor {
layers: Vec<Box<dyn Component>>, layers: Vec<Box<dyn Component>>,
pub terminal: Terminal,
area: Rect, area: Rect,
pub(crate) last_picker: Option<Box<dyn Component>>, pub(crate) last_picker: Option<Box<dyn Component>>,
} }
impl Compositor { impl Compositor {
pub fn new() -> anyhow::Result<Self> { pub fn new(area: Rect) -> Self {
#[cfg(not(feature = "integration"))] Self {
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 {
layers: Vec::new(), layers: Vec::new(),
area, area,
terminal,
last_picker: None, last_picker: None,
}) }
} }
pub fn size(&self) -> Rect { pub fn size(&self) -> Rect {
self.area self.area
} }
pub fn resize(&mut self, width: u16, height: u16) { pub fn resize(&mut self, area: Rect) {
self.terminal self.area = area;
.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 push(&mut self, mut layer: Box<dyn Component>) { pub fn push(&mut self, mut layer: Box<dyn Component>) {
@ -192,25 +164,10 @@ impl Compositor {
consumed consumed
} }
pub fn render(&mut self, cx: &mut Context) { pub fn render(&mut self, area: Rect, surface: &mut Surface, 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();
for layer in &mut self.layers { for layer in &mut self.layers {
layer.render(area, surface, cx); 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<Position>, CursorKind) { pub fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {

Loading…
Cancel
Save