on master help sign in
ui: popup: Don't allow scrolling past the end of content
3307f44Blaž Hrastnik
helix-term/src/compositor.rs+3−2
@@ -55,9 +55,10 @@ pub trait Component: Any + AnyComponent {
55
55
56 /// May be used by the parent component to compute the child area.
56 /// May be used by the parent component to compute the child area.
57 /// viewport is the maximum allowed area, and the child should stay within those bounds.
57 /// viewport is the maximum allowed area, and the child should stay within those bounds.
58+ ///
59+ /// The returned size might be larger than the viewport if the child is too big to fit.
60+ /// In this case the parent can use the values to calculate scroll.
58 fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> {
61 fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> {
59− // TODO: for scrolling, the scroll wrapper should place a size + offset on the Context
60− // that way render can use it
61 None
62 None
62 }
63 }
63
64
helix-term/src/ui/markdown.rs−5
@@ -241,11 +241,6 @@ impl Component for Markdown {
241 } else if content_width > text_width {
241 } else if content_width > text_width {
242 text_width = content_width;
242 text_width = content_width;
243 }
243 }
244−
245− if height >= viewport.1 {
246− height = viewport.1;
247− break;
248− }
249 }
244 }
250
245
251 Some((text_width + padding, height))
246 Some((text_width + padding, height))
helix-term/src/ui/popup.rs+16−3
@@ -15,6 +15,7 @@ pub struct Popup<T: Component> {
15 contents: T,
15 contents: T,
16 position: Option<Position>,
16 position: Option<Position>,
17 size: (u16, u16),
17 size: (u16, u16),
18+ child_size: (u16, u16),
18 scroll: usize,
19 scroll: usize,
19 id: &'static str,
20 id: &'static str,
20}
21}
@@ -25,6 +26,7 @@ impl<T: Component> Popup<T> {
25 contents,
26 contents,
26 position: None,
27 position: None,
27 size: (0, 0),
28 size: (0, 0),
29+ child_size: (0, 0),
28 scroll: 0,
30 scroll: 0,
29 id,
31 id,
30 }
32 }
@@ -70,6 +72,9 @@ impl<T: Component> Popup<T> {
70 pub fn scroll(&mut self, offset: usize, direction: bool) {
72 pub fn scroll(&mut self, offset: usize, direction: bool) {
71 if direction {
73 if direction {
72 self.scroll += offset;
74 self.scroll += offset;
75+
76+ let max_offset = self.child_size.1.saturating_sub(self.size.1);
77+ self.scroll = (self.scroll + offset).min(max_offset as usize);
73 } else {
78 } else {
74 self.scroll = self.scroll.saturating_sub(offset);
79 self.scroll = self.scroll.saturating_sub(offset);
75 }
80 }
@@ -117,13 +122,21 @@ impl<T: Component> Component for Popup<T> {
117 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
122 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
118 }
123 }
119
124
120− fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> {
125+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
126+ let max_width = 120.min(viewport.0);
127+ let max_height = 26.min(viewport.1.saturating_sub(2)); // add some spacing in the viewport
128+
121 let (width, height) = self
129 let (width, height) = self
122 .contents
130 .contents
123− .required_size((120, 26)) // max width, max height
131+ .required_size((max_width, max_height))
124 .expect("Component needs required_size implemented in order to be embedded in a popup");
132 .expect("Component needs required_size implemented in order to be embedded in a popup");
125
133
134+ self.child_size = (width, height);
126− self.size = (width, height);
135+ self.size = (width.min(max_width), height.min(max_height));
136+
137+ // re-clamp scroll offset
138+ let max_offset = self.child_size.1.saturating_sub(self.size.1);
139+ self.scroll = self.scroll.min(max_offset as usize);
127
140
128 Some(self.size)
141 Some(self.size)
129 }
142 }