on master help sign in
Fix cursor positioning for prompts
d708efeBlaž Hrastnik
helix-term/src/ui/picker.rs+24−9
@@ -118,6 +118,20 @@ impl<T> Picker<T> {
118// - on input change:
118// - on input change:
119// - score all the names in relation to input
119// - score all the names in relation to input
120
120
121+fn inner_rect(area: Rect) -> Rect {
122+ let padding_vertical = area.height * 20 / 100;
123+ let padding_horizontal = area.width * 20 / 100;
124+
125+ let area = Rect::new(
126+ area.x + padding_horizontal,
127+ area.y + padding_vertical,
128+ area.width - padding_horizontal * 2,
129+ area.height - padding_vertical * 2,
130+ );
131+
132+ area
133+}
134+
121impl<T: 'static> Component for Picker<T> {
135impl<T: 'static> Component for Picker<T> {
122 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
136 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
123 let key_event = match event {
137 let key_event = match event {
@@ -191,15 +205,7 @@ impl<T: 'static> Component for Picker<T> {
191 }
205 }
192
206
193 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
207 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
194− let padding_vertical = area.height * 20 / 100;
208+ let area = inner_rect(area);
195− let padding_horizontal = area.width * 20 / 100;
196−
197− let area = Rect::new(
198− area.x + padding_horizontal,
199− area.y + padding_vertical,
200− area.width - padding_horizontal * 2,
201− area.height - padding_vertical * 2,
202− );
203
209
204 // -- Render the frame:
210 // -- Render the frame:
205
211
@@ -260,6 +266,15 @@ impl<T: 'static> Component for Picker<T> {
260 }
266 }
261
267
262 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
268 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
269+ // TODO: this is mostly duplicate code
270+ let area = inner_rect(area);
271+ let block = Block::default().borders(Borders::ALL);
272+ // calculate the inner area inside the box
273+ let inner = block.inner(area);
274+
275+ // prompt area
276+ let area = Rect::new(inner.x + 1, inner.y, inner.width - 1, 1);
277+
263 self.prompt.cursor_position(area, editor)
278 self.prompt.cursor_position(area, editor)
264 }
279 }
265}
280}
helix-term/src/ui/prompt.rs+2−1
@@ -271,8 +271,9 @@ impl Component for Prompt {
271 }
271 }
272
272
273 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
273 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
274+ let line = area.height as usize - 1;
274 Some(Position::new(
275 Some(Position::new(
275− area.y as usize,
276+ area.y as usize + line,
276 area.x as usize + self.prompt.len() + self.cursor,
277 area.x as usize + self.prompt.len() + self.cursor,
277 ))
278 ))
278 }
279 }