Move terminal out of compositor

gui
Blaž Hrastnik 3 years ago
parent 57d4a9ba21
commit e0f9d86f49
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640

@ -11,7 +11,7 @@ use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
#[cfg(feature = "lsp")] #[cfg(feature = "lsp")]
use serde_json::json; use serde_json::json;
use helix_view::{align_view, editor::ConfigEvent, theme, Align, Editor}; use helix_view::{align_view, editor::ConfigEvent, graphics::Rect, theme, Align, Editor};
use crate::{ use crate::{
args::Args, args::Args,
@ -43,8 +43,12 @@ use {
#[cfg(windows)] #[cfg(windows)]
type Signals = futures_util::stream::Empty<()>; type Signals = futures_util::stream::Empty<()>;
use tui::backend::{Backend, CrosstermBackend};
type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
pub struct Application { pub struct Application {
compositor: Compositor, compositor: Compositor,
terminal: Terminal,
editor: Editor, editor: Editor,
config: Arc<ArcSwap<Config>>, config: Arc<ArcSwap<Config>>,
@ -121,10 +125,13 @@ 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()?; let backend = CrosstermBackend::new(stdout());
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| {
@ -195,6 +202,7 @@ impl Application {
let app = Self { let app = Self {
compositor, compositor,
terminal,
editor, editor,
config, config,
@ -213,12 +221,28 @@ impl Application {
} }
fn render(&mut self) { fn render(&mut self) {
let mut cx = crate::compositor::Context { let area = self
editor: &mut self.editor, .terminal
jobs: &mut self.jobs, .autoresize()
.expect("Unable to determine terminal size");
// if the terminal size suddenly changed, we need to trigger a resize
self.editor.resize(area.clip_bottom(1)); // -1 from bottom for commandline
let surface = self.terminal.current_buffer_mut();
// let area = *surface.area();
let mut render_cx = crate::compositor::RenderContext {
editor: &self.editor,
surface,
scroll: None,
}; };
self.compositor.render(area, &mut render_cx);
self.compositor.render(&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) { pub async fn event_loop(&mut self) {
@ -349,8 +373,8 @@ impl Application {
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.compositor.size();
self.compositor.resize(width, height); self.compositor.resize(area);
self.render(); self.render();
} }
_ => unreachable!(), _ => unreachable!(),
@ -381,7 +405,13 @@ impl Application {
// Handle key events // Handle key events
let should_redraw = match event { let should_redraw = match event {
Some(Ok(Event::Resize(width, height))) => { Some(Ok(Event::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)
@ -711,10 +741,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!(stdout, terminal::EnterAlternateScreen)?; execute!(stdout, terminal::EnterAlternateScreen)?;
@ -727,10 +756,8 @@ impl Application {
fn restore_term(&mut self) -> Result<(), Error> { fn restore_term(&mut self) -> Result<(), Error> {
use helix_view::graphics::CursorKind; use helix_view::graphics::CursorKind;
use tui::backend::Backend; if self.terminal.cursor_kind() == CursorKind::Hidden {
if self.compositor.terminal.cursor_kind() == CursorKind::Hidden { self.terminal
self.compositor
.terminal
.backend_mut() .backend_mut()
.show_cursor(CursorKind::Block) .show_cursor(CursorKind::Block)
.ok(); .ok();

@ -67,42 +67,28 @@ pub trait Component: Any + AnyComponent {
} }
} }
use anyhow::Error;
use std::io::stdout;
use tui::backend::CrosstermBackend;
type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
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() -> Result<Self, Error> { pub fn new(area: Rect) -> Self {
let backend = CrosstermBackend::new(stdout()); Self {
let terminal = Terminal::new(backend)?;
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>) {
@ -162,33 +148,10 @@ impl Compositor {
consumed consumed
} }
pub fn render(&mut self, cx: &mut Context) { pub fn render(&mut self, area: Rect, cx: &mut RenderContext) {
let area = self
.terminal
.autoresize()
.expect("Unable to determine terminal size");
// if the terminal size suddenly changed, we need to trigger a resize
cx.editor.resize(area.clip_bottom(1)); // -1 from bottom for commandline
let surface = self.terminal.current_buffer_mut();
// let area = *surface.area();
let mut render_cx = RenderContext {
editor: cx.editor,
surface,
scroll: None,
};
for layer in &mut self.layers { for layer in &mut self.layers {
layer.render(area, &mut render_cx); layer.render(area, 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