Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Popup scrollbar #4449

Merged
merged 9 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ pub fn code_action(cx: &mut Context) {
});
picker.move_down(); // pre-select the first item

let popup = Popup::new("code-action", picker);
let popup = Popup::new("code-action", picker).with_scrollbar(false);
compositor.replace_or_push("code-action", popup);
},
)
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Completion {
}
};
});
let popup = Popup::new(Self::ID, menu);
let popup = Popup::new(Self::ID, menu).with_scrollbar(false);
let mut completion = Self {
popup,
start_offset,
Expand Down
15 changes: 4 additions & 11 deletions helix-term/src/ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,12 @@ impl<T: Item + 'static> Component for Menu<T> {
let fits = len <= win_height;

let scroll_style = theme.get("ui.menu.scroll");
for (i, _) in (scroll..(scroll + win_height).min(len)).enumerate() {
let cell = &mut surface[(area.x + area.width - 1, area.y + i as u16)];
if !fits {
// Draw scroll thumb
for i in scroll_line..(scroll_line + scroll_height) {
let cell = &mut surface[(area.x + area.width - 1, area.y + i as u16)];

if !fits {
// Draw scroll track
cell.set_symbol("▐"); // right half block
cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset));
}

let is_marked = i >= scroll_line && i < scroll_line + scroll_height;

if !fits && is_marked {
// Draw scroll thumb
cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to check for is_marked for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, I have integrated the is_marked expression inside the for-loop condition, so that I only draw the cells of the scrollbar. However, I did not realise that the other cells are used to render the scrollbar track, since my theme does not set the "ui.menu.scroll" key. I fixed it in the last commit

}
}
Expand Down
38 changes: 38 additions & 0 deletions helix-term/src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Popup<T: Component> {
auto_close: bool,
ignore_escape_key: bool,
id: &'static str,
has_scrollbar: bool,
}

impl<T: Component> Popup<T> {
Expand All @@ -37,6 +38,7 @@ impl<T: Component> Popup<T> {
auto_close: false,
ignore_escape_key: false,
id,
has_scrollbar: true,
}
}

Expand Down Expand Up @@ -128,6 +130,14 @@ impl<T: Component> Popup<T> {
}
}

/// Toggles the Popup's scrollbar.
/// Consider disabling the scrollbar in case the child
/// already has its own.
pub fn with_scrollbar(mut self, enable_scrollbar: bool) -> Self {
self.has_scrollbar = enable_scrollbar;
self
}
Manosmer marked this conversation as resolved.
Show resolved Hide resolved

pub fn contents(&self) -> &T {
&self.contents
}
Expand Down Expand Up @@ -228,6 +238,34 @@ impl<T: Component> Component for Popup<T> {

let inner = area.inner(&self.margin);
self.contents.render(inner, surface, cx);

// render scrollbar if contents do not fit
if self.has_scrollbar {
let win_height = inner.height as usize;
let len = self.child_size.1 as usize;
let fits = len <= win_height;
let theme = &cx.editor.theme;
let scroll = self.scroll;

const fn div_ceil(a: usize, b: usize) -> usize {
(a + b - 1) / b
}

let scroll_height = div_ceil(win_height.pow(2), len).min(win_height as usize);
let scroll_line = (win_height - scroll_height) * scroll
/ std::cmp::max(1, len.saturating_sub(win_height));

let scroll_style = theme.get("ui.menu.scroll");
if !fits {
// Draw scroll thumb
for i in scroll_line..(scroll_line + scroll_height) {
let cell = &mut surface[(area.x + area.width - 1, area.y + i as u16)];

cell.set_symbol("▐"); // right half block
cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
}
}
}
}

fn id(&self) -> Option<&'static str> {
Expand Down