diff options
author | Blaž Hrastnik | 2021-03-01 05:23:10 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-03-01 05:23:10 +0000 |
commit | 00808afe3c215d159574b23e30326379428060bf (patch) | |
tree | b9f4fdb9cf559e95340a625c7741e9dbcd31b41b /helix-term | |
parent | 2c9b02039bac81cb32309bd0d4e2b08191356b9c (diff) |
ui: Make editor more resilient about being shrunk too small.
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/application.rs | 7 | ||||
-rw-r--r-- | helix-term/src/commands.rs | 14 | ||||
-rw-r--r-- | helix-term/src/compositor.rs | 6 | ||||
-rw-r--r-- | helix-term/src/terminal.rs | 16 | ||||
-rw-r--r-- | helix-term/src/ui/editor.rs | 24 |
5 files changed, 36 insertions, 31 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index dfa819db..c63e6e57 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -70,10 +70,11 @@ impl Application { let area = self.terminal.size().unwrap(); compositor.render(area, self.terminal.current_buffer_mut(), &mut cx); - let pos = compositor.cursor_position(area, &editor); + let pos = compositor + .cursor_position(area, &editor) + .map(|pos| (pos.col as u16, pos.row as u16)); - self.terminal.draw(); - self.terminal.set_cursor(pos.col as u16, pos.row as u16); + self.terminal.draw(pos); } pub async fn event_loop(&mut self) { diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 07e71a70..855a12eb 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1072,9 +1072,10 @@ pub fn completion(cx: &mut Context) { cx.callback = Some(Box::new( move |compositor: &mut Compositor, editor: &mut Editor| { let area = tui::layout::Rect::default(); // TODO: unused remove from cursor_position - let mut pos = compositor.cursor_position(area, editor); - pos.row += 1; // shift down by one row - menu.set_position(pos); + if let Some(mut pos) = compositor.cursor_position(area, editor) { + pos.row += 1; // shift down by one row + menu.set_position(pos); + }; compositor.push(Box::new(menu)); }, @@ -1133,9 +1134,10 @@ pub fn hover(cx: &mut Context) { cx.callback = Some(Box::new( move |compositor: &mut Compositor, editor: &mut Editor| { let area = tui::layout::Rect::default(); // TODO: unused remove from cursor_position - let mut pos = compositor.cursor_position(area, editor); - pos.row += 1; // shift down by one row - popup.set_position(pos); + if let Some(mut pos) = compositor.cursor_position(area, editor) { + pos.row += 1; // shift down by one row + popup.set_position(pos); + }; compositor.push(Box::new(popup)); }, diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index 3fee1214..59e93e03 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -120,12 +120,12 @@ impl Compositor { } } - pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Position { + pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> { for layer in self.layers.iter().rev() { if let Some(pos) = layer.cursor_position(area, editor) { - return pos; + return Some(pos); } } - panic!("No layer returned a position!"); + None } } diff --git a/helix-term/src/terminal.rs b/helix-term/src/terminal.rs index e40343bd..fc9bc5ba 100644 --- a/helix-term/src/terminal.rs +++ b/helix-term/src/terminal.rs @@ -154,7 +154,7 @@ where /// Synchronizes terminal size, calls the rendering closure, flushes the current internal state /// and prepares for the next draw call. - pub fn draw(&mut self) -> io::Result<()> { + pub fn draw(&mut self, cursor_position: Option<(u16, u16)>) -> io::Result<()> { // // Autoresize - otherwise we get glitches if shrinking or potential desync between widgets // // and the terminal (if growing), which may OOB. // self.autoresize()?; @@ -169,13 +169,13 @@ where // Draw to stdout self.flush()?; - // match cursor_position { - // None => self.hide_cursor()?, - // Some((x, y)) => { - // self.show_cursor()?; - // self.set_cursor(x, y)?; - // } - // } + match cursor_position { + None => self.hide_cursor()?, + Some((x, y)) => { + self.show_cursor()?; + self.set_cursor(x, y)?; + } + } // Swap buffers self.buffers[1 - self.current].reset(); diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 32697a03..499d8021 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -42,7 +42,7 @@ impl EditorView { viewport.x + OFFSET, viewport.y, viewport.width - OFFSET, - viewport.height - 1, + viewport.height.saturating_sub(1), ); // - 1 for statusline self.render_buffer(view, area, surface, theme, is_focused); @@ -52,7 +52,7 @@ impl EditorView { let area = Rect::new( viewport.x, - viewport.y + viewport.height - 1, + viewport.y + viewport.height.saturating_sub(1), viewport.width, 1, ); @@ -433,14 +433,16 @@ impl Component for EditorView { // Mode::Insert => write!(stdout, "\x1B[6 q"), // mode => write!(stdout, "\x1B[2 q"), // }; - let view = editor.view(); - let cursor = view.doc.selection().cursor(); - - let mut pos = view - .screen_coords_at_pos(view.doc.text().slice(..), cursor) - .expect("Cursor is out of bounds."); - pos.col += view.area.x as usize + area.x as usize + OFFSET as usize; - pos.row += view.area.y as usize + area.y as usize; - Some(pos) + // let view = editor.view(); + // let cursor = view.doc.selection().cursor(); + + // if let Some(mut pos) = view.screen_coords_at_pos(view.doc.text().slice(..), cursor) { + // pos.col += view.area.x as usize + area.x as usize + OFFSET as usize; + // pos.row += view.area.y as usize + area.y as usize; + // return Some(pos); + // } + + // It's easier to just not render the cursor and use selection rendering instead. + None } } |