@@ -165,6 +165,34 @@ static int scroll_vbox_input(const struct widget *w, wchar_t key)
165165 return 1 ;
166166}
167167
168+ static void scroll_vbox_ensure_visible (struct widget * container , struct widget * child )
169+ {
170+ struct widget_svbox * state = container -> state .svbox ;
171+
172+ if (!state )
173+ return ;
174+
175+ int child_y = child -> ly ;
176+ int child_h = child -> h ;
177+
178+ int view_top = state -> scroll ;
179+ int view_bot = state -> scroll + container -> h ;
180+
181+ if (child_y < view_top ) {
182+ state -> scroll = child_y ;
183+ } else if (child_y + child_h > view_bot ) {
184+ state -> scroll = child_y + child_h - container -> h ;
185+ }
186+
187+ if (state -> scroll < 0 )
188+ state -> scroll = 0 ;
189+
190+ int max_scroll = state -> content_h - container -> h ;
191+
192+ if (state -> scroll > max_scroll )
193+ state -> scroll = max_scroll ;
194+ }
195+
168196static void scroll_vbox_freedata (struct widget * w )
169197{
170198 struct widget_svbox * state = w -> state .svbox ;
@@ -187,15 +215,16 @@ struct widget *make_scroll_vbox(void)
187215 return NULL ;
188216 }
189217
190- w -> state .svbox = state ;
191- w -> measure = scroll_vbox_measure ;
192- w -> layout = scroll_vbox_layout ;
193- w -> render = scroll_vbox_render ;
194- w -> create_win = scroll_vbox_createwin ;
195- w -> noutrefresh = scroll_vbox_refresh ;
196- w -> input = scroll_vbox_input ;
197- w -> free_data = scroll_vbox_freedata ;
198- w -> color_pair = COLOR_PAIR_WINDOW ;
218+ w -> state .svbox = state ;
219+ w -> measure = scroll_vbox_measure ;
220+ w -> layout = scroll_vbox_layout ;
221+ w -> render = scroll_vbox_render ;
222+ w -> create_win = scroll_vbox_createwin ;
223+ w -> noutrefresh = scroll_vbox_refresh ;
224+ w -> input = scroll_vbox_input ;
225+ w -> free_data = scroll_vbox_freedata ;
226+ w -> ensure_visible = scroll_vbox_ensure_visible ;
227+ w -> color_pair = COLOR_PAIR_WINDOW ;
199228
200229 w -> stretch_w = true;
201230 w -> stretch_h = true;
0 commit comments