From ea37124686316f8de9872255a7d0ea8243498102 Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Thu, 30 Jun 2022 01:21:01 +0200 Subject: [PATCH] widget: constrain drag offset to [0, 1] Once the user begins dragging, the cursor can move outside the clip area (or even the window on at least X11), leading to events with positions that are either negative, or larger than the clip area. Negative values outright break the delta tracking and cause the scrollbar to misbehave. Positive values "only" break the invariant of Scrollbar.ScrollDistance that the returned value is in the range [-1, 1]. Signed-off-by: Dominik Honnef --- widget/list.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/widget/list.go b/widget/list.go index 962eea7e..224b8321 100644 --- a/widget/list.go +++ b/widget/list.go @@ -74,6 +74,12 @@ func (s *Scrollbar) Layout(gtx layout.Context, axis layout.Axis, viewportStart, continue } dragOffset := axis.FConvert(event.Position).X + // The user can drag outside of the constraints, or even the window. Limit dragging to within the scrollbar. + if dragOffset < 0 { + dragOffset = 0 + } else if dragOffset > trackHeight { + dragOffset = trackHeight + } normalizedDragOffset := dragOffset / trackHeight if !s.dragging {