@@ -22,6 +22,7 @@ pub struct Popup<T: Component> {
2222 auto_close: bool,
2323 ignore_escape_key: bool,
2424 id: &'static str,
25+ has_scrollbar: bool,
2526}
2627
2728impl<T: Component> Popup<T> {
@@ -37,6 +38,7 @@ impl<T: Component> Popup<T> {
3738 auto_close: false,
3839 ignore_escape_key: false,
3940 id,
41+ has_scrollbar: true,
4042 }
4143 }
4244
@@ -128,6 +130,14 @@ impl<T: Component> Popup<T> {
128130 }
129131 }
130132
133+ /// Toggles the Popup's scrollbar.
134+ /// Consider disabling the scrollbar in case the child
135+ /// already has its own.
136+ pub fn with_scrollbar(mut self, enable_scrollbar: bool) -> Self {
137+ self.has_scrollbar = enable_scrollbar;
138+ self
139+ }
140+
131141 pub fn contents(&self) -> &T {
132142 &self.contents
133143 }
@@ -228,6 +238,40 @@ impl<T: Component> Component for Popup<T> {
228238
229239 let inner = area.inner(&self.margin);
230240 self.contents.render(inner, surface, cx);
241+
242+ // render scrollbar if contents do not fit
243+ if self.has_scrollbar {
244+ let win_height = inner.height as usize;
245+ let len = self.child_size.1 as usize;
246+ let fits = len <= win_height;
247+ let scroll = self.scroll;
248+ let scroll_style = cx.editor.theme.get("ui.menu.scroll");
249+
250+ const fn div_ceil(a: usize, b: usize) -> usize {
251+ (a + b - 1) / b
252+ }
253+
254+ if !fits {
255+ let scroll_height = div_ceil(win_height.pow(2), len).min(win_height);
256+ let scroll_line = (win_height - scroll_height) * scroll
257+ / std::cmp::max(1, len.saturating_sub(win_height));
258+
259+ let mut cell;
260+ for i in 0..win_height {
261+ cell = &mut surface[(inner.right() - 1, inner.top() + i as u16)];
262+
263+ cell.set_symbol("▐"); // right half block
264+
265+ if scroll_line <= i && i < scroll_line + scroll_height {
266+ // Draw scroll thumb
267+ cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
268+ } else {
269+ // Draw scroll track
270+ cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset));
271+ }
272+ }
273+ }
274+ }
231275 }
232276
233277 fn id(&self) -> Option<&'static str> {