@@ -15,6 +15,7 @@ pub struct Popup<T: Component> {
1515 contents: T,
1616 position: Option<Position>,
1717 size: (u16, u16),
18+ child_size: (u16, u16),
1819 scroll: usize,
1920 id: &'static str,
2021}
@@ -25,6 +26,7 @@ impl<T: Component> Popup<T> {
2526 contents,
2627 position: None,
2728 size: (0, 0),
29+ child_size: (0, 0),
2830 scroll: 0,
2931 id,
3032 }
@@ -70,6 +72,9 @@ impl<T: Component> Popup<T> {
7072 pub fn scroll(&mut self, offset: usize, direction: bool) {
7173 if direction {
7274 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);
7378 } else {
7479 self.scroll = self.scroll.saturating_sub(offset);
7580 }
@@ -117,13 +122,21 @@ impl<T: Component> Component for Popup<T> {
117122 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
118123 }
119124
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+
121129 let (width, height) = self
122130 .contents
123− .required_size((120, 26)) // max width, max height
131+ .required_size((max_width, max_height))
124132 .expect("Component needs required_size implemented in order to be embedded in a popup");
125133
126− self.size = (width, height);
134+ self.child_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);
127140
128141 Some(self.size)
129142 }