mirror of
https://github.com/ilyakooo0/helix.git
synced 2024-12-02 06:45:45 +03:00
ui: Move terminal into compositor, redo required_size hints.
This commit is contained in:
parent
05aa0d6991
commit
5ea610c41d
@ -23,23 +23,19 @@ use crossterm::{
|
|||||||
execute, terminal,
|
execute, terminal,
|
||||||
};
|
};
|
||||||
|
|
||||||
use tui::{backend::CrosstermBackend, layout::Rect};
|
use tui::layout::Rect;
|
||||||
|
|
||||||
type Terminal = crate::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
|
|
||||||
|
|
||||||
pub struct Application {
|
pub struct Application {
|
||||||
compositor: Compositor,
|
compositor: Compositor,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
terminal: Terminal,
|
|
||||||
|
|
||||||
executor: &'static smol::Executor<'static>,
|
executor: &'static smol::Executor<'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Application {
|
impl Application {
|
||||||
pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
|
pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
|
||||||
let backend = CrosstermBackend::new(stdout());
|
let mut compositor = Compositor::new()?;
|
||||||
let mut terminal = Terminal::new(backend)?;
|
let size = compositor.size();
|
||||||
let size = terminal.size()?;
|
|
||||||
let mut editor = Editor::new(size);
|
let mut editor = Editor::new(size);
|
||||||
|
|
||||||
let files = args.values_of_t::<PathBuf>("files").unwrap();
|
let files = args.values_of_t::<PathBuf>("files").unwrap();
|
||||||
@ -47,12 +43,10 @@ impl Application {
|
|||||||
editor.open(file, executor)?;
|
editor.open(file, executor)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut compositor = Compositor::new();
|
|
||||||
compositor.push(Box::new(ui::EditorView::new()));
|
compositor.push(Box::new(ui::EditorView::new()));
|
||||||
|
|
||||||
let mut app = Self {
|
let mut app = Self {
|
||||||
editor,
|
editor,
|
||||||
terminal,
|
|
||||||
compositor,
|
compositor,
|
||||||
|
|
||||||
executor,
|
executor,
|
||||||
@ -64,17 +58,11 @@ impl Application {
|
|||||||
fn render(&mut self) {
|
fn render(&mut self) {
|
||||||
let executor = &self.executor;
|
let executor = &self.executor;
|
||||||
let editor = &mut self.editor;
|
let editor = &mut self.editor;
|
||||||
let compositor = &self.compositor;
|
let compositor = &mut self.compositor;
|
||||||
|
|
||||||
let mut cx = crate::compositor::Context { editor, executor };
|
let mut cx = crate::compositor::Context { editor, executor };
|
||||||
let area = self.terminal.size().unwrap();
|
|
||||||
|
|
||||||
compositor.render(area, self.terminal.current_buffer_mut(), &mut cx);
|
compositor.render(&mut cx);
|
||||||
let pos = compositor
|
|
||||||
.cursor_position(area, &editor)
|
|
||||||
.map(|pos| (pos.col as u16, pos.row as u16));
|
|
||||||
|
|
||||||
self.terminal.draw(pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn event_loop(&mut self) {
|
pub async fn event_loop(&mut self) {
|
||||||
@ -107,7 +95,7 @@ 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.terminal.resize(Rect::new(0, 0, width, height));
|
self.compositor.resize(width, height);
|
||||||
|
|
||||||
self.compositor
|
self.compositor
|
||||||
.handle_event(Event::Resize(width, height), &mut cx)
|
.handle_event(Event::Resize(width, height), &mut cx)
|
||||||
|
@ -39,9 +39,12 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Push a new component onto the compositor.
|
/// Push a new component onto the compositor.
|
||||||
pub fn push_layer(&mut self, component: Box<dyn crate::compositor::Component>) {
|
pub fn push_layer(&mut self, mut component: Box<dyn crate::compositor::Component>) {
|
||||||
self.callback = Some(Box::new(
|
self.callback = Some(Box::new(
|
||||||
|compositor: &mut Compositor, editor: &mut Editor| {
|
|compositor: &mut Compositor, editor: &mut Editor| {
|
||||||
|
let size = compositor.size();
|
||||||
|
// trigger required_size on init
|
||||||
|
component.required_size((size.width, size.height));
|
||||||
compositor.push(component);
|
compositor.push(component);
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
|
@ -1,14 +1,3 @@
|
|||||||
// Features:
|
|
||||||
// Tracks currently focused component which receives all input
|
|
||||||
// Event loop is external as opposed to cursive-rs
|
|
||||||
// Calls render on the component and translates screen coords to local component coords
|
|
||||||
//
|
|
||||||
// TODO:
|
|
||||||
// Q: where is the Application state stored? do we store it into an external static var?
|
|
||||||
// A: probably makes sense to initialize the editor into a `static Lazy<>` global var.
|
|
||||||
//
|
|
||||||
// Q: how do we composit nested structures? There should be sub-components/views
|
|
||||||
//
|
|
||||||
// Each component declares it's own size constraints and gets fitted based on it's parent.
|
// Each component declares it's own size constraints and gets fitted based on it's parent.
|
||||||
// Q: how does this work with popups?
|
// Q: how does this work with popups?
|
||||||
// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
|
// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
|
||||||
@ -36,7 +25,7 @@ pub enum EventResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
use helix_view::{Editor, View};
|
use helix_view::{Editor, View};
|
||||||
// shared with commands.rs
|
|
||||||
pub struct Context<'a> {
|
pub struct Context<'a> {
|
||||||
pub editor: &'a mut Editor,
|
pub editor: &'a mut Editor,
|
||||||
pub executor: &'static smol::Executor<'static>,
|
pub executor: &'static smol::Executor<'static>,
|
||||||
@ -54,46 +43,53 @@ pub trait Component {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Render the component onto the provided surface.
|
||||||
fn render(&self, area: Rect, frame: &mut Surface, ctx: &mut Context);
|
fn render(&self, area: Rect, frame: &mut Surface, ctx: &mut Context);
|
||||||
|
|
||||||
fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
|
fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
|
/// 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
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For v1:
|
use anyhow::Error;
|
||||||
// Child views are something each view needs to handle on it's own for now, positioning and sizing
|
use std::io::stdout;
|
||||||
// options, focus tracking. In practice this is simple: we only will need special solving for
|
use tui::backend::CrosstermBackend;
|
||||||
// splits etc
|
type Terminal = crate::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
|
||||||
|
|
||||||
// impl Editor {
|
|
||||||
// fn render(&mut self, surface: &mut Surface, args: ()) {
|
|
||||||
// // compute x, y, w, h rects for sub-views!
|
|
||||||
// // get surface area
|
|
||||||
// // get constraints for textarea, statusbar
|
|
||||||
// // -> cassowary-rs
|
|
||||||
|
|
||||||
// // first render textarea
|
|
||||||
// // then render statusbar
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// usecases to consider:
|
|
||||||
// - a single view with subviews (textarea + statusbar)
|
|
||||||
// - a popup panel / dialog with it's own interactions
|
|
||||||
// - an autocomplete popup that doesn't change focus
|
|
||||||
|
|
||||||
pub struct Compositor {
|
pub struct Compositor {
|
||||||
layers: Vec<Box<dyn Component>>,
|
layers: Vec<Box<dyn Component>>,
|
||||||
|
terminal: Terminal,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Compositor {
|
impl Compositor {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Result<Self, Error> {
|
||||||
Self { layers: Vec::new() }
|
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>) {
|
pub fn push(&mut self, layer: Box<dyn Component>) {
|
||||||
@ -120,10 +116,19 @@ impl Compositor {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
pub fn render(&mut self, cx: &mut Context) {
|
||||||
|
let area = self.size();
|
||||||
|
let surface = self.terminal.current_buffer_mut();
|
||||||
|
|
||||||
for layer in &self.layers {
|
for layer in &self.layers {
|
||||||
layer.render(area, surface, cx)
|
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> {
|
pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
|
||||||
|
@ -59,16 +59,14 @@ impl Component for Markdown {
|
|||||||
Event::End(tag) => {
|
Event::End(tag) => {
|
||||||
tags.pop();
|
tags.pop();
|
||||||
match tag {
|
match tag {
|
||||||
Tag::Heading(_) | Tag::Paragraph => {
|
Tag::Heading(_)
|
||||||
// whenever paragraph closes, new line
|
| Tag::Paragraph
|
||||||
|
| Tag::CodeBlock(CodeBlockKind::Fenced(_)) => {
|
||||||
|
// whenever code block or paragraph closes, new line
|
||||||
let spans = std::mem::replace(&mut spans, Vec::new());
|
let spans = std::mem::replace(&mut spans, Vec::new());
|
||||||
lines.push(Spans::from(spans));
|
lines.push(Spans::from(spans));
|
||||||
lines.push(Spans::default());
|
lines.push(Spans::default());
|
||||||
}
|
}
|
||||||
Tag::CodeBlock(CodeBlockKind::Fenced(_)) => {
|
|
||||||
let spans = std::mem::replace(&mut spans, Vec::new());
|
|
||||||
lines.push(Spans::from(spans));
|
|
||||||
}
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,14 +115,15 @@ impl Component for Markdown {
|
|||||||
let par = Paragraph::new(contents).wrap(Wrap { trim: false });
|
let par = Paragraph::new(contents).wrap(Wrap { trim: false });
|
||||||
// .scroll(x, y) offsets
|
// .scroll(x, y) offsets
|
||||||
|
|
||||||
// padding on all sides
|
|
||||||
let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2);
|
let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2);
|
||||||
par.render(area, surface);
|
par.render(area, surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
||||||
let contents = tui::text::Text::from(self.contents.clone());
|
let contents = tui::text::Text::from(self.contents.clone());
|
||||||
Some((contents.width(), contents.height()))
|
let width = std::cmp::min(contents.width() as u16, viewport.0);
|
||||||
|
let height = std::cmp::min(contents.height() as u16, viewport.1);
|
||||||
|
Some((width, height))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,16 +149,21 @@ impl<T> Component for Menu<T> {
|
|||||||
EventResult::Ignored
|
EventResult::Ignored
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
||||||
|
let width = std::cmp::min(30, viewport.0);
|
||||||
|
|
||||||
const MAX: usize = 5;
|
const MAX: usize = 5;
|
||||||
let height = std::cmp::min(self.options.len(), MAX);
|
let height = std::cmp::min(self.options.len(), MAX);
|
||||||
Some((30, height))
|
let height = std::cmp::min(height, viewport.1 as usize);
|
||||||
|
|
||||||
|
Some((width as u16, height as u16))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
||||||
let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
|
let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
|
||||||
let selected = Style::default().fg(Color::Rgb(255, 255, 255));
|
let selected = Style::default().fg(Color::Rgb(255, 255, 255));
|
||||||
|
|
||||||
|
// TODO: instead of a cell, all these numbers should be precomputed in handle_event + init
|
||||||
let mut scroll = self.scroll.get();
|
let mut scroll = self.scroll.get();
|
||||||
let len = self.options.len();
|
let len = self.options.len();
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ use helix_view::Editor;
|
|||||||
pub struct Popup {
|
pub struct Popup {
|
||||||
contents: Box<dyn Component>,
|
contents: Box<dyn Component>,
|
||||||
position: Option<Position>,
|
position: Option<Position>,
|
||||||
|
size: (u16, u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Popup {
|
impl Popup {
|
||||||
@ -27,6 +28,7 @@ impl Popup {
|
|||||||
Self {
|
Self {
|
||||||
contents,
|
contents,
|
||||||
position: None,
|
position: None,
|
||||||
|
size: (0, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,6 +70,17 @@ impl Component for Popup {
|
|||||||
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
|
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
||||||
|
let (width, height) = self
|
||||||
|
.contents
|
||||||
|
.required_size((120, 26)) // max width, max height
|
||||||
|
.expect("Component needs required_size implemented in order to be embedded in a popup");
|
||||||
|
|
||||||
|
self.size = (width, height);
|
||||||
|
|
||||||
|
Some(self.size)
|
||||||
|
}
|
||||||
|
|
||||||
fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
|
fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
|
||||||
use tui::text::Text;
|
use tui::text::Text;
|
||||||
use tui::widgets::{Paragraph, Widget, Wrap};
|
use tui::widgets::{Paragraph, Widget, Wrap};
|
||||||
@ -77,13 +90,7 @@ impl Component for Popup {
|
|||||||
.or_else(|| cx.editor.cursor_position())
|
.or_else(|| cx.editor.cursor_position())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let (width, height) = self
|
let (width, height) = self.size;
|
||||||
.contents
|
|
||||||
.size_hint(viewport)
|
|
||||||
.expect("Component needs size_hint implemented in order to be embedded in a popup");
|
|
||||||
|
|
||||||
let width = width.min(120) as u16;
|
|
||||||
let height = height.min(26) as u16;
|
|
||||||
|
|
||||||
// -- make sure frame doesn't stick out of bounds
|
// -- make sure frame doesn't stick out of bounds
|
||||||
let mut rel_x = position.col as u16;
|
let mut rel_x = position.col as u16;
|
||||||
|
@ -33,8 +33,10 @@ impl Component for Text {
|
|||||||
par.render(area, surface);
|
par.render(area, surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
||||||
let contents = tui::text::Text::from(self.contents.clone());
|
let contents = tui::text::Text::from(self.contents.clone());
|
||||||
Some((contents.width(), contents.height()))
|
let width = std::cmp::min(contents.width() as u16, viewport.0);
|
||||||
|
let height = std::cmp::min(contents.height() as u16, viewport.1);
|
||||||
|
Some((width, height))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user