ui: Scrollable popup menu, with scrollbar indicator.

pull/8/head
Blaž Hrastnik 4 years ago
parent 513effd972
commit a21d96e729

@ -8,6 +8,7 @@ use tui::{
}; };
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::Cell;
use helix_core::Position; use helix_core::Position;
use helix_view::Editor; use helix_view::Editor;
@ -18,10 +19,12 @@ use helix_view::Editor;
pub struct Menu<T> { pub struct Menu<T> {
options: Vec<T>, options: Vec<T>,
cursor: usize, cursor: Option<usize>,
format_fn: Box<dyn Fn(&T) -> Cow<str>>, format_fn: Box<dyn Fn(&T) -> Cow<str>>,
callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>, callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
scroll: Cell<usize>,
} }
impl<T> Menu<T> { impl<T> Menu<T> {
@ -34,25 +37,26 @@ impl<T> Menu<T> {
) -> Self { ) -> Self {
Self { Self {
options, options,
cursor: 0, cursor: None,
format_fn: Box::new(format_fn), format_fn: Box::new(format_fn),
callback_fn: Box::new(callback_fn), callback_fn: Box::new(callback_fn),
scroll: Cell::new(0),
} }
} }
pub fn move_up(&mut self) { pub fn move_up(&mut self) {
self.cursor = self.cursor.saturating_sub(1); // TODO: wrap around to end
let pos = self.cursor.map(|i| i.saturating_sub(1)).unwrap_or(0) % self.options.len();
self.cursor = Some(pos);
} }
pub fn move_down(&mut self) { pub fn move_down(&mut self) {
// TODO: len - 1 let pos = self.cursor.map(|i| i + 1).unwrap_or(0) % self.options.len();
if self.cursor < self.options.len() { self.cursor = Some(pos);
self.cursor += 1;
}
} }
pub fn selection(&self) -> Option<&T> { pub fn selection(&self) -> Option<&T> {
self.options.get(self.cursor) self.cursor.and_then(|cursor| self.options.get(cursor))
} }
} }
@ -155,15 +159,53 @@ impl<T> Component for Menu<T> {
let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
let selected = Style::default().fg(Color::Rgb(255, 255, 255)); let selected = Style::default().fg(Color::Rgb(255, 255, 255));
for (i, option) in self.options.iter().take(area.height as usize).enumerate() { let mut scroll = self.scroll.get();
let len = self.options.len();
let win_height = area.height as usize;
if let Some(cursor) = self.cursor {
if cursor > (win_height + scroll).saturating_sub(1) {
// scroll down
scroll += cursor - (win_height + scroll).saturating_sub(1)
} else if cursor < scroll {
// scroll up
scroll = cursor
}
self.scroll.set(scroll);
}
fn div_ceil(a: usize, b: usize) -> usize {
(a + b - 1) / a
}
let scroll_height = std::cmp::min(div_ceil(win_height.pow(2), len), win_height as usize);
let scroll_line = (win_height - scroll_height) * scroll
/ std::cmp::max(1, len.saturating_sub(win_height));
for (i, option) in self.options[scroll..(scroll + win_height).min(len)]
.iter()
.enumerate()
{
let line = Some(i + scroll);
// TODO: set bg for the whole row if selected // TODO: set bg for the whole row if selected
surface.set_stringn( surface.set_stringn(
area.x, area.x,
area.y + i as u16, area.y + i as u16,
(self.format_fn)(option), (self.format_fn)(option),
area.width as usize - 1, area.width as usize - 1,
if i == self.cursor { selected } else { style }, if line == self.cursor { selected } else { style },
); );
let is_marked = i >= scroll_line && i < scroll_line + scroll_height;
if is_marked {
let cell = surface.get_mut(area.x + area.width - 2, area.y + i as u16);
cell.set_symbol("▐ ");
cell.set_style(selected);
// cell.set_style(if is_marked { selected } else { style });
}
} }
} }
} }

@ -39,6 +39,10 @@ impl Component for Popup {
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
let key = match event { let key = match event {
Event::Key(event) => event, Event::Key(event) => event,
Event::Resize(width, height) => {
// TODO: calculate inner area, call component's handle_event with that area
return EventResult::Ignored;
}
_ => return EventResult::Ignored, _ => return EventResult::Ignored,
}; };
@ -63,6 +67,7 @@ impl Component for Popup {
// for some events, we want to process them but send ignore, specifically all input except // for some events, we want to process them but send ignore, specifically all input except
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll. // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
} }
fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) { fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
use tui::text::Text; use tui::text::Text;
use tui::widgets::{Paragraph, Widget, Wrap}; use tui::widgets::{Paragraph, Widget, Wrap};

Loading…
Cancel
Save