@@ -1642,9 +1642,8 @@ pub const Element = struct {
16421642 const size = text_size (display , texture , .normal );
16431643
16441644 // Do we need space between text and icon?
1645- if (content_width > 0 ) {
1645+ if (content_width > 0 )
16461646 content_width += element .type .button .spacing ;
1647- }
16481647
16491648 content_width += size .width ;
16501649 }
@@ -4165,6 +4164,12 @@ pub const Display = struct {
41654164 }
41664165 }
41674166
4167+ fn limit_scroll (min : f32 , value : f32 , max : f32 ) f32 {
4168+ if (value < min ) return min ;
4169+ if (value > max ) return max ;
4170+ return value ;
4171+ }
4172+
41684173 /// Event handler for mouse motion
41694174 inline fn handle_mouse_motion_event (display : * Display , _ : * sdl.SDL_Event ) ! void {
41704175 var cursor : Vector = undefined ;
@@ -4181,23 +4186,38 @@ pub const Display = struct {
41814186 // How far has the mouse/finger moved the item
41824187 element .offset = cursor .minus (display .scroll_start ).add (display .scroll_initial_offset );
41834188
4184- // Clamp offset so we cant scroll past start at all
4185- element .offset .x = @min (element .offset .x , 0 );
4186- element .offset .y = @min (element .offset .y , 0 );
4187-
41884189 // Clamp offset so we cant scroll past end at all
4189- const allowable_x_scroll = @min (0 , element .rect .width - panel .scrollable .size .width );
4190- const allowable_y_scroll = @min (0 , element .rect .height - panel .scrollable .size .height );
4190+ switch (element .child_align .x ) {
4191+ .centre = > {
4192+ // allowable scroll offset (negative number)
4193+ const allowable_x_scroll = @min (0 , element .rect .width - panel .scrollable .size .width ) / 2 ;
4194+ element .offset .x = limit_scroll (allowable_x_scroll , element .offset .x , - allowable_x_scroll );
4195+ },
4196+ else = > {
4197+ // allowable scroll offset (negative number)
4198+ const allowable_x_scroll = @min (0 , element .rect .width - panel .scrollable .size .width );
4199+ element .offset .x = limit_scroll (allowable_x_scroll , element .offset .x , 0 );
4200+ },
4201+ }
41914202
4192- element .offset .x = @max (element .offset .x , allowable_x_scroll );
4193- element .offset .y = @max (element .offset .y , allowable_y_scroll );
4203+ // Clamp offset so we cant scroll past start at all
4204+ switch (element .child_align .y ) {
4205+ .centre = > {
4206+ const allowable_y_scroll = @min (0 , element .rect .height - panel .scrollable .size .height ) / 2 ;
4207+ element .offset .y = limit_scroll (allowable_y_scroll , element .offset .y , - allowable_y_scroll );
4208+ },
4209+ else = > {
4210+ const allowable_y_scroll = @min (0 , element .rect .height - panel .scrollable .size .height );
4211+ element .offset .y = limit_scroll (allowable_y_scroll , element .offset .y , 0 );
4212+ },
4213+ }
41944214
4195- if (! panel .scrollable .scroll .y ) {
4215+ if (! panel .scrollable .scroll .y )
41964216 element .offset .y = 0 ;
4197- }
4198- if (! panel .scrollable .scroll .x ) {
4217+
4218+ if (! panel .scrollable .scroll .x )
41994219 element .offset .x = 0 ;
4200- }
4220+
42014221 trace ("scrolling panel {s}. scrollable.size={d}x{d} panel.size={d}x{d}. draw.offset={d}x{d}" , .{
42024222 element .name ,
42034223 panel .scrollable .size .width ,
0 commit comments