on master help sign in
ui: Scrollable popup menu, with scrollbar indicator.
commita21d96e7293e32c0d20e8548a65fdd31f92b642d authorBlaž Hrastnik <blaz@mxxn.io> dateWed 3 Mar 2021, 17:28 (6 years ago) parent513effd
2 files changed+57−10
helix-term/src/ui/menu.rs+52−10 helix-term/src/ui/popup.rs+5
helix-term/src/ui/menu.rs+52−10
@@ -8,6 +8,7 @@ use tui::{
88};
99
1010use std::borrow::Cow;
11+use std::cell::Cell;
1112
1213use helix_core::Position;
1314use helix_view::Editor;
@@ -18,10 +19,12 @@ use helix_view::Editor;
1819pub struct Menu<T> {
1920 options: Vec<T>,
2021
21− cursor: usize,
22+ cursor: Option<usize>,
2223
2324 format_fn: Box<dyn Fn(&T) -> Cow<str>>,
2425 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
26+
27+ scroll: Cell<usize>,
2528}
2629
2730impl<T> Menu<T> {
@@ -34,25 +37,26 @@ impl<T> Menu<T> {
3437 ) -> Self {
3538 Self {
3639 options,
37− cursor: 0,
40+ cursor: None,
3841 format_fn: Box::new(format_fn),
3942 callback_fn: Box::new(callback_fn),
43+ scroll: Cell::new(0),
4044 }
4145 }
4246
4347 pub fn move_up(&mut self) {
44− self.cursor = self.cursor.saturating_sub(1);
48+ // TODO: wrap around to end
49+ let pos = self.cursor.map(|i| i.saturating_sub(1)).unwrap_or(0) % self.options.len();
50+ self.cursor = Some(pos);
4551 }
4652
4753 pub fn move_down(&mut self) {
48− // TODO: len - 1
49− if self.cursor < self.options.len() {
50− self.cursor += 1;
51− }
54+ let pos = self.cursor.map(|i| i + 1).unwrap_or(0) % self.options.len();
55+ self.cursor = Some(pos);
5256 }
5357
5458 pub fn selection(&self) -> Option<&T> {
55− self.options.get(self.cursor)
59+ self.cursor.and_then(|cursor| self.options.get(cursor))
5660 }
5761}
5862
@@ -155,15 +159,53 @@ impl<T> Component for Menu<T> {
155159 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
156160 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
157161
158− for (i, option) in self.options.iter().take(area.height as usize).enumerate() {
162+ let mut scroll = self.scroll.get();
163+ let len = self.options.len();
164+
165+ let win_height = area.height as usize;
166+
167+ if let Some(cursor) = self.cursor {
168+ if cursor > (win_height + scroll).saturating_sub(1) {
169+ // scroll down
170+ scroll += cursor - (win_height + scroll).saturating_sub(1)
171+ } else if cursor < scroll {
172+ // scroll up
173+ scroll = cursor
174+ }
175+ self.scroll.set(scroll);
176+ }
177+
178+ fn div_ceil(a: usize, b: usize) -> usize {
179+ (a + b - 1) / a
180+ }
181+
182+ let scroll_height = std::cmp::min(div_ceil(win_height.pow(2), len), win_height as usize);
183+
184+ let scroll_line = (win_height - scroll_height) * scroll
185+ / std::cmp::max(1, len.saturating_sub(win_height));
186+
187+ for (i, option) in self.options[scroll..(scroll + win_height).min(len)]
188+ .iter()
189+ .enumerate()
190+ {
191+ let line = Some(i + scroll);
159192 // TODO: set bg for the whole row if selected
160193 surface.set_stringn(
161194 area.x,
162195 area.y + i as u16,
163196 (self.format_fn)(option),
164197 area.width as usize - 1,
165− if i == self.cursor { selected } else { style },
198+ if line == self.cursor { selected } else { style },
166199 );
200+
201+ let is_marked = i >= scroll_line && i < scroll_line + scroll_height;
202+
203+ if is_marked {
204+ let cell = surface.get_mut(area.x + area.width - 2, area.y + i as u16);
205+ cell.set_symbol("▐ ");
206+ cell.set_style(selected);
207+ // cell.set_style(if is_marked { selected } else { style });
208+ }
167209 }
168210 }
169211}
helix-term/src/ui/popup.rs+5
@@ -39,6 +39,10 @@ impl Component for Popup {
3939 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
4040 let key = match event {
4141 Event::Key(event) => event,
42+ Event::Resize(width, height) => {
43+ // TODO: calculate inner area, call component's handle_event with that area
44+ return EventResult::Ignored;
45+ }
4246 _ => return EventResult::Ignored,
4347 };
4448
@@ -63,6 +67,7 @@ impl Component for Popup {
6367 // for some events, we want to process them but send ignore, specifically all input except
6468 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
6569 }
70+
6671 fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
6772 use tui::text::Text;
6873 use tui::widgets::{Paragraph, Widget, Wrap};