@@ -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,
@@ -25,6 +26,7 @@ impl<T: Component> Popup<T> {
26 position: None,
27 position: None,
27 size: (0, 0),
28 size: (0, 0),
28 scroll: 0,
30 scroll: 0,
@@ -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;
76+ let max_offset = self.child_size.1.saturating_sub(self.size.1);
77+ self.scroll = (self.scroll + offset).min(max_offset as usize);
74 self.scroll = self.scroll.saturating_sub(offset);
79 self.scroll = self.scroll.saturating_sub(offset);
@@ -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.
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
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");
134+ self.child_size = (width, height);
126− self.size = (width, height);
135+ self.size = (width.min(max_width), height.min(max_height));
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);
128 Some(self.size)
141 Some(self.size)