diff options
author | Blaž Hrastnik | 2021-05-28 15:06:23 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-05-28 15:06:23 +0000 |
commit | d708efe275a82e515e6d7056e20426af9466a32e (patch) | |
tree | 3a366281c9c6d5e1d5b7be22aadf1e3be82f7a84 /helix-term/src/ui | |
parent | 3336023614bb5ae8740f71c3df31030d6757081a (diff) |
Fix cursor positioning for prompts
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r-- | helix-term/src/ui/picker.rs | 33 | ||||
-rw-r--r-- | 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 bf072877..a715ebb5 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -118,6 +118,20 @@ impl<T> Picker<T> { // - 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<T: 'static> Component for Picker<T> { fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { let key_event = match event { @@ -191,15 +205,7 @@ impl<T: 'static> Component for Picker<T> { } 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<T: 'static> Component for Picker<T> { } fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> { + // 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 dbbef72c..e6c383b6 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<Position> { + 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, )) } |