From a21d96e7293e32c0d20e8548a65fdd31f92b642d Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Wed, 3 Mar 2021 17:28:50 +0900 Subject: ui: Scrollable popup menu, with scrollbar indicator. --- helix-term/src/ui/menu.rs | 62 ++++++++++++++++++++++++++++++++++++++-------- helix-term/src/ui/popup.rs | 5 ++++ 2 files changed, 57 insertions(+), 10 deletions(-) (limited to 'helix-term/src') diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 3fd5ed63..9f0e79be 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -8,6 +8,7 @@ use tui::{ }; use std::borrow::Cow; +use std::cell::Cell; use helix_core::Position; use helix_view::Editor; @@ -18,10 +19,12 @@ use helix_view::Editor; pub struct Menu { options: Vec, - cursor: usize, + cursor: Option, format_fn: Box Cow>, callback_fn: Box, MenuEvent)>, + + scroll: Cell, } impl Menu { @@ -34,25 +37,26 @@ impl Menu { ) -> Self { Self { options, - cursor: 0, + cursor: None, format_fn: Box::new(format_fn), callback_fn: Box::new(callback_fn), + scroll: Cell::new(0), } } 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) { - // TODO: len - 1 - if self.cursor < self.options.len() { - self.cursor += 1; - } + let pos = self.cursor.map(|i| i + 1).unwrap_or(0) % self.options.len(); + self.cursor = Some(pos); } 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 Component for Menu { let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender 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 surface.set_stringn( area.x, area.y + i as u16, (self.format_fn)(option), 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 }); + } } } } diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index e0f7a5c0..7260a997 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -39,6 +39,10 @@ impl Component for Popup { fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { let key = match 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, }; @@ -63,6 +67,7 @@ impl Component for Popup { // 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. } + fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) { use tui::text::Text; use tui::widgets::{Paragraph, Widget, Wrap}; -- cgit v1.2.3-70-g09d2