on master help sign in
ui: Make editor more resilient about being shrunk too small.
commit00808afe3c215d159574b23e30326379428060bf authorBlaž Hrastnik <blaz@mxxn.io> dateMon 1 Mar 2021, 14:23 (6 years ago) parent2c9b020
6 files changed+37−32
helix-term/src/application.rs+4−3 helix-term/src/commands.rs+8−6 helix-term/src/compositor.rs+3−3 helix-term/src/terminal.rs+8−8 helix-term/src/ui/editor.rs+13−11 helix-view/src/view.rs+1−1
helix-term/src/application.rs+4−3
@@ -70,10 +70,11 @@ impl Application {
7070 let area = self.terminal.size().unwrap();
7171
7272 compositor.render(area, self.terminal.current_buffer_mut(), &mut cx);
73− let pos = compositor.cursor_position(area, &editor);
73+ let pos = compositor
74+ .cursor_position(area, &editor)
75+ .map(|pos| (pos.col as u16, pos.row as u16));
7476
75− self.terminal.draw();
76− self.terminal.set_cursor(pos.col as u16, pos.row as u16);
77+ self.terminal.draw(pos);
7778 }
7879
7980 pub async fn event_loop(&mut self) {
helix-term/src/commands.rs+8−6
@@ -1072,9 +1072,10 @@ pub fn completion(cx: &mut Context) {
10721072 cx.callback = Some(Box::new(
10731073 move |compositor: &mut Compositor, editor: &mut Editor| {
10741074 let area = tui::layout::Rect::default(); // TODO: unused remove from cursor_position
1075− let mut pos = compositor.cursor_position(area, editor);
1076− pos.row += 1; // shift down by one row
1077− menu.set_position(pos);
1075+ if let Some(mut pos) = compositor.cursor_position(area, editor) {
1076+ pos.row += 1; // shift down by one row
1077+ menu.set_position(pos);
1078+ };
10781079
10791080 compositor.push(Box::new(menu));
10801081 },
@@ -1133,9 +1134,10 @@ pub fn hover(cx: &mut Context) {
11331134 cx.callback = Some(Box::new(
11341135 move |compositor: &mut Compositor, editor: &mut Editor| {
11351136 let area = tui::layout::Rect::default(); // TODO: unused remove from cursor_position
1136− let mut pos = compositor.cursor_position(area, editor);
1137− pos.row += 1; // shift down by one row
1138− popup.set_position(pos);
1137+ if let Some(mut pos) = compositor.cursor_position(area, editor) {
1138+ pos.row += 1; // shift down by one row
1139+ popup.set_position(pos);
1140+ };
11391141
11401142 compositor.push(Box::new(popup));
11411143 },
helix-term/src/compositor.rs+3−3
@@ -120,12 +120,12 @@ impl Compositor {
120120 }
121121 }
122122
123− pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Position {
123+ pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
124124 for layer in self.layers.iter().rev() {
125125 if let Some(pos) = layer.cursor_position(area, editor) {
126− return pos;
126+ return Some(pos);
127127 }
128128 }
129− panic!("No layer returned a position!");
129+ None
130130 }
131131}
helix-term/src/terminal.rs+8−8
@@ -154,7 +154,7 @@ where
154154
155155 /// Synchronizes terminal size, calls the rendering closure, flushes the current internal state
156156 /// and prepares for the next draw call.
157− pub fn draw(&mut self) -> io::Result<()> {
157+ pub fn draw(&mut self, cursor_position: Option<(u16, u16)>) -> io::Result<()> {
158158 // // Autoresize - otherwise we get glitches if shrinking or potential desync between widgets
159159 // // and the terminal (if growing), which may OOB.
160160 // self.autoresize()?;
@@ -169,13 +169,13 @@ where
169169 // Draw to stdout
170170 self.flush()?;
171171
172− // match cursor_position {
173− // None => self.hide_cursor()?,
174− // Some((x, y)) => {
175− // self.show_cursor()?;
176− // self.set_cursor(x, y)?;
177− // }
178− // }
172+ match cursor_position {
173+ None => self.hide_cursor()?,
174+ Some((x, y)) => {
175+ self.show_cursor()?;
176+ self.set_cursor(x, y)?;
177+ }
178+ }
179179
180180 // Swap buffers
181181 self.buffers[1 - self.current].reset();
helix-term/src/ui/editor.rs+13−11
@@ -42,7 +42,7 @@ impl EditorView {
4242 viewport.x + OFFSET,
4343 viewport.y,
4444 viewport.width - OFFSET,
45− viewport.height - 1,
45+ viewport.height.saturating_sub(1),
4646 ); // - 1 for statusline
4747 self.render_buffer(view, area, surface, theme, is_focused);
4848
@@ -52,7 +52,7 @@ impl EditorView {
5252
5353 let area = Rect::new(
5454 viewport.x,
55− viewport.y + viewport.height - 1,
55+ viewport.y + viewport.height.saturating_sub(1),
5656 viewport.width,
5757 1,
5858 );
@@ -433,14 +433,16 @@ impl Component for EditorView {
433433 // Mode::Insert => write!(stdout, "\x1B[6 q"),
434434 // mode => write!(stdout, "\x1B[2 q"),
435435 // };
436− let view = editor.view();
437− let cursor = view.doc.selection().cursor();
438−
439− let mut pos = view
440− .screen_coords_at_pos(view.doc.text().slice(..), cursor)
441− .expect("Cursor is out of bounds.");
442− pos.col += view.area.x as usize + area.x as usize + OFFSET as usize;
443− pos.row += view.area.y as usize + area.y as usize;
444− Some(pos)
436+ // let view = editor.view();
437+ // let cursor = view.doc.selection().cursor();
438+
439+ // if let Some(mut pos) = view.screen_coords_at_pos(view.doc.text().slice(..), cursor) {
440+ // pos.col += view.area.x as usize + area.x as usize + OFFSET as usize;
441+ // pos.row += view.area.y as usize + area.y as usize;
442+ // return Some(pos);
443+ // }
444+
445+ // It's easier to just not render the cursor and use selection rendering instead.
446+ None
445447 }
446448}
helix-view/src/view.rs+1−1
@@ -65,7 +65,7 @@ impl View {
6565 /// Calculates the last visible line on screen
6666 #[inline]
6767 pub fn last_line(&self) -> usize {
68− let viewport = Rect::new(6, 0, self.area.width, self.area.height - 1); // - 1 for statusline
68+ let viewport = Rect::new(6, 0, self.area.width, self.area.height.saturating_sub(1)); // - 1 for statusline
6969 std::cmp::min(
7070 self.first_line + (viewport.height as usize),
7171 self.doc.text().len_lines() - 1,