on master help sign in
Add single width left margin for completion popup (#2728)

* Add single width left margin for completion popup * Clear with ui.menu style before rendering menu When rendering a completion popup, the popup component will clear the area with ui.popup and then the menu component would draw over it using a table component. We remove the left edge of the area before passing it to the table component (so that it will be left as padding), and the table component uses ui.menu as the style. If ui.menu and ui.popup are different the left edge of the popup will look different from the rest of the popup. We avoid this by clearing the whole area with ui.menu in Menu::render

commit16ccc7ead88faf060236cbff72dee52c58e37156 authorGokul Soumya <gokulps15@gmail.com> dateSun 26 Jun 2022, 14:26 (4 years ago) parentba85779
1 file changed+11−1
helix-term/src/ui/menu.rs+11−1
helix-term/src/ui/menu.rs+11−1
@@ -48,6 +48,8 @@ pub struct Menu<T: Item> {
4848}
4949
5050impl<T: Item> Menu<T> {
51+ const LEFT_PADDING: usize = 1;
52+
5153 // TODO: it's like a slimmed down picker, share code? (picker = menu + prompt with different
5254 // rendering)
5355 pub fn new(
@@ -150,6 +152,7 @@ impl<T: Item> Menu<T> {
150152 len += 1; // +1: reserve some space for scrollbar
151153 }
152154
155+ len += Self::LEFT_PADDING;
153156 let width = len.min(viewport.0 as usize);
154157
155158 self.widths = max_lens
@@ -271,6 +274,7 @@ impl<T: Item + 'static> Component for Menu<T> {
271274 .try_get("ui.menu")
272275 .unwrap_or_else(|| theme.get("ui.text"));
273276 let selected = theme.get("ui.menu.selected");
277+ surface.clear_with(area, style);
274278
275279 let scroll = self.scroll;
276280
@@ -306,7 +310,7 @@ impl<T: Item + 'static> Component for Menu<T> {
306310 use tui::widgets::TableState;
307311
308312 table.render_table(
309− area,
313+ area.clip_left(Self::LEFT_PADDING as u16),
310314 surface,
311315 &mut TableState {
312316 offset: scroll,
@@ -314,6 +318,12 @@ impl<T: Item + 'static> Component for Menu<T> {
314318 },
315319 );
316320
321+ if let Some(cursor) = self.cursor {
322+ let offset_from_top = cursor - scroll;
323+ let cell = &mut surface[(area.x, area.y + offset_from_top as u16)];
324+ cell.set_style(selected);
325+ }
326+
317327 let fits = len <= win_height;
318328
319329 for (i, _) in (scroll..(scroll + win_height).min(len)).enumerate() {