From d708efe275a82e515e6d7056e20426af9466a32e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sat, 29 May 2021 00:06:23 +0900 Subject: [PATCH] Fix cursor positioning for prompts --- helix-term/src/ui/picker.rs | 33 ++++++++++++++++++++++++--------- helix-term/src/ui/prompt.rs | 3 ++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index bf072877b..a715ebb5e 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -118,6 +118,20 @@ impl Picker { // - on input change: // - score all the names in relation to input +fn inner_rect(area: Rect) -> Rect { + let padding_vertical = area.height * 20 / 100; + let padding_horizontal = area.width * 20 / 100; + + let area = Rect::new( + area.x + padding_horizontal, + area.y + padding_vertical, + area.width - padding_horizontal * 2, + area.height - padding_vertical * 2, + ); + + area +} + impl Component for Picker { fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { let key_event = match event { @@ -191,15 +205,7 @@ impl Component for Picker { } fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) { - let padding_vertical = area.height * 20 / 100; - let padding_horizontal = area.width * 20 / 100; - - let area = Rect::new( - area.x + padding_horizontal, - area.y + padding_vertical, - area.width - padding_horizontal * 2, - area.height - padding_vertical * 2, - ); + let area = inner_rect(area); // -- Render the frame: @@ -260,6 +266,15 @@ impl Component for Picker { } fn cursor_position(&self, area: Rect, editor: &Editor) -> Option { + // TODO: this is mostly duplicate code + let area = inner_rect(area); + let block = Block::default().borders(Borders::ALL); + // calculate the inner area inside the box + let inner = block.inner(area); + + // prompt area + let area = Rect::new(inner.x + 1, inner.y, inner.width - 1, 1); + self.prompt.cursor_position(area, editor) } } diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index dbbef72c7..e6c383b65 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -271,8 +271,9 @@ impl Component for Prompt { } fn cursor_position(&self, area: Rect, editor: &Editor) -> Option { + let line = area.height as usize - 1; Some(Position::new( - area.y as usize, + area.y as usize + line, area.x as usize + self.prompt.len() + self.cursor, )) }