on master help sign in
ui: Scrollable popup menu, with scrollbar indicator.
a21d96eBlaž Hrastnik
helix-term/src/ui/menu.rs+52−10
@@ -8,6 +8,7 @@ use tui::{
8};
8};
9
9
10use std::borrow::Cow;
10use std::borrow::Cow;
11+use std::cell::Cell;
11
12
12use helix_core::Position;
13use helix_core::Position;
13use helix_view::Editor;
14use helix_view::Editor;
@@ -18,10 +19,12 @@ use helix_view::Editor;
18pub struct Menu<T> {
19pub struct Menu<T> {
19 options: Vec<T>,
20 options: Vec<T>,
20
21
21− cursor: usize,
22+ cursor: Option<usize>,
22
23
23 format_fn: Box<dyn Fn(&T) -> Cow<str>>,
24 format_fn: Box<dyn Fn(&T) -> Cow<str>>,
24 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
25 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
26+
27+ scroll: Cell<usize>,
25}
28}
26
29
27impl<T> Menu<T> {
30impl<T> Menu<T> {
@@ -34,25 +37,26 @@ impl<T> Menu<T> {
34 ) -> Self {
37 ) -> Self {
35 Self {
38 Self {
36 options,
39 options,
37− cursor: 0,
40+ cursor: None,
38 format_fn: Box::new(format_fn),
41 format_fn: Box::new(format_fn),
39 callback_fn: Box::new(callback_fn),
42 callback_fn: Box::new(callback_fn),
43+ scroll: Cell::new(0),
40 }
44 }
41 }
45 }
42
46
43 pub fn move_up(&mut self) {
47 pub fn move_up(&mut self) {
48+ // TODO: wrap around to end
44− self.cursor = self.cursor.saturating_sub(1);
49+ let pos = self.cursor.map(|i| i.saturating_sub(1)).unwrap_or(0) % self.options.len();
50+ self.cursor = Some(pos);
45 }
51 }
46
52
47 pub fn move_down(&mut self) {
53 pub fn move_down(&mut self) {
48− // TODO: len - 1
54+ let pos = self.cursor.map(|i| i + 1).unwrap_or(0) % self.options.len();
49− if self.cursor < self.options.len() {
50− self.cursor += 1;
55+ self.cursor = Some(pos);
51− }
52 }
56 }
53
57
54 pub fn selection(&self) -> Option<&T> {
58 pub fn selection(&self) -> Option<&T> {
55− self.options.get(self.cursor)
59+ self.cursor.and_then(|cursor| self.options.get(cursor))
56 }
60 }
57}
61}
58
62
@@ -155,15 +159,53 @@ impl<T> Component for Menu<T> {
155 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
159 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
156 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
160 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
157
161
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+
158− for (i, option) in self.options.iter().take(area.height as usize).enumerate() {
187+ for (i, option) in self.options[scroll..(scroll + win_height).min(len)]
188+ .iter()
189+ .enumerate()
190+ {
191+ let line = Some(i + scroll);
159 // TODO: set bg for the whole row if selected
192 // TODO: set bg for the whole row if selected
160 surface.set_stringn(
193 surface.set_stringn(
161 area.x,
194 area.x,
162 area.y + i as u16,
195 area.y + i as u16,
163 (self.format_fn)(option),
196 (self.format_fn)(option),
164 area.width as usize - 1,
197 area.width as usize - 1,
165− if i == self.cursor { selected } else { style },
198+ if line == self.cursor { selected } else { style },
166 );
199 );
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+ }
167 }
209 }
168 }
210 }
169}
211}
helix-term/src/ui/popup.rs+5
@@ -39,6 +39,10 @@ impl Component for Popup {
39 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
39 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
40 let key = match event {
40 let key = match event {
41 Event::Key(event) => event,
41 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+ }
42 _ => return EventResult::Ignored,
46 _ => return EventResult::Ignored,
43 };
47 };
44
48
@@ -63,6 +67,7 @@ impl Component for Popup {
63 // for some events, we want to process them but send ignore, specifically all input except
67 // for some events, we want to process them but send ignore, specifically all input except
64 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
68 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
65 }
69 }
70+
66 fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
71 fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
67 use tui::text::Text;
72 use tui::text::Text;
68 use tui::widgets::{Paragraph, Widget, Wrap};
73 use tui::widgets::{Paragraph, Widget, Wrap};