Skip to content

Commit 1c8c90f

Browse files
committed
Resolve RawScrollbar.mouseCursor with the hovered state
Restore WidgetState.hovered to cursorStates, alongside WidgetState.disabled, so WidgetStateProperty.fromMap and resolveWith can tell an idle scrollbar from a hovered one. Release the pointer state that feeds the cursor whenever no later event could clear it. Hover events are not delivered while a pointer button is down, so the hover state is recomputed when a mouse or trackpad thumb drag ends. The hovered and dragged states are also released when the scrollbar stops being interactive, or when its child stops being scrollable and the thumb drag recognizer is dropped without reporting a cancel. Without this the resolved cursor stayed applied over the whole scroll view. Refer to EditableText rather than TextField, so the doc imports stay on the widgets side of the Material split.
1 parent f1293b2 commit 1c8c90f

2 files changed

Lines changed: 397 additions & 18 deletions

File tree

‎packages/flutter/lib/src/widgets/scrollbar.dart‎

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,18 +1351,24 @@ class RawScrollbar extends StatefulWidget {
13511351
///
13521352
/// Resolves in the following [WidgetState]s:
13531353
/// * [WidgetState.dragged].
1354-
///
1355-
/// The hovered state is not included because the cursor is only resolved for
1356-
/// a mouse pointer that is already hovering over the painted scrollbar.
1357-
///
1358-
/// The cursor is applied while the pointer is over the painted scrollbar
1359-
/// (using the same hit test area as the hover interaction) and while the
1360-
/// scrollbar thumb is being dragged, so the dragged cursor (typically
1361-
/// [SystemMouseCursors.grabbing]) remains stable even if the pointer
1362-
/// briefly moves outside the scrollbar during a drag. When neither
1363-
/// condition holds, cursor resolution is deferred to ancestors, so a
1364-
/// parent widget's cursor (for example, the I-beam cursor of an enclosing
1365-
/// [TextField]) is preserved.
1354+
/// * [WidgetState.hovered].
1355+
/// * [WidgetState.disabled].
1356+
///
1357+
/// The [WidgetState.hovered] state is resolved when the pointer is over the
1358+
/// painted scrollbar track or thumb (using the same hit test area as hover
1359+
/// interactions). The [WidgetState.dragged] state is resolved while the
1360+
/// scrollbar thumb is being dragged. The [WidgetState.disabled] state is
1361+
/// resolved when the scrollbar is not [interactive]; a non-interactive
1362+
/// scrollbar does not track pointer hovering, so ancestor cursors are
1363+
/// preserved over it.
1364+
///
1365+
/// The cursor is applied while the pointer is over the painted scrollbar and
1366+
/// while the scrollbar thumb is being dragged, so the dragged cursor
1367+
/// (typically [SystemMouseCursors.grabbing]) remains stable even if the
1368+
/// pointer moves outside the scrollbar during a drag. When neither condition
1369+
/// holds, cursor resolution is deferred to ancestors, so a parent widget's
1370+
/// cursor (for example, the I-beam cursor of an enclosing [EditableText]) is
1371+
/// preserved.
13661372
///
13671373
/// When this is null, or when its resolved value is null, [MouseCursor.defer]
13681374
/// is used and ancestor cursors are left unchanged.
@@ -1391,6 +1397,7 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
13911397
final GlobalKey _scrollbarPainterKey = GlobalKey();
13921398
bool _hoverIsActive = false;
13931399
bool _dragIsActive = false;
1400+
PointerDeviceKind? _thumbDragPointerKind;
13941401
MouseCursor _effectiveMouseCursor = MouseCursor.defer;
13951402
Drag? _thumbDrag;
13961403
bool _maxScrollExtentPermitsScrolling = false;
@@ -1637,6 +1644,7 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
16371644
if (widget.mouseCursor != oldWidget.mouseCursor) {
16381645
updateMouseCursor();
16391646
}
1647+
_releaseStalePointerState();
16401648
}
16411649

16421650
void _maybeStartFadeoutTimer() {
@@ -1967,6 +1975,7 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
19671975
setState(() {
19681976
_maxScrollExtentPermitsScrolling = !_maxScrollExtentPermitsScrolling;
19691977
});
1978+
_releaseStalePointerState();
19701979
}
19711980

19721981
return false;
@@ -2024,6 +2033,7 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
20242033
}
20252034

20262035
void _handleThumbDragStart(DragStartDetails details) {
2036+
_thumbDragPointerKind = details.kind;
20272037
handleThumbPressStart(_globalToScrollbar(details.globalPosition));
20282038
}
20292039

@@ -2032,9 +2042,61 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
20322042
}
20332043

20342044
void _handleThumbDragEnd(DragEndDetails details) {
2045+
_updateHoverStateAfterThumbDrag(details.globalPosition);
20352046
handleThumbPressEnd(_globalToScrollbar(details.globalPosition), details.velocity);
20362047
}
20372048

2049+
// Hover events are not delivered while a pointer button is held down, so
2050+
// _hoverIsActive can be stale by the time a thumb drag ends. Recompute it
2051+
// from the position the drag ended at, before handleThumbPressEnd resolves
2052+
// the cursor. Otherwise releasing a drag away from the scrollbar would leave
2053+
// the hovered cursor applied over the scroll view's content until the next
2054+
// pointer move.
2055+
//
2056+
// Only mouse and trackpad pointers drive the hover state, matching the kinds
2057+
// handled by handleHover. A touch or stylus drag must not clobber the hover
2058+
// state of a separate mouse pointer.
2059+
void _updateHoverStateAfterThumbDrag(Offset globalPosition) {
2060+
final PointerDeviceKind? kind = _thumbDragPointerKind;
2061+
_thumbDragPointerKind = null;
2062+
if (kind == null || !enableGestures) {
2063+
return;
2064+
}
2065+
switch (kind) {
2066+
case PointerDeviceKind.mouse:
2067+
case PointerDeviceKind.trackpad:
2068+
_hoverIsActive = isPointerOverScrollbar(globalPosition, kind, forHover: true);
2069+
case PointerDeviceKind.stylus:
2070+
case PointerDeviceKind.invertedStylus:
2071+
case PointerDeviceKind.touch:
2072+
case PointerDeviceKind.unknown:
2073+
break;
2074+
}
2075+
}
2076+
2077+
// The hover callbacks are only installed while the scrollbar is interactive,
2078+
// and the thumb drag recognizers only exist while the child is scrollable.
2079+
// When either stops being true the callbacks simply stop arriving, and a
2080+
// dropped drag recognizer is disposed without reporting a cancel, so the
2081+
// pointer state feeding the cursor has to be released here. Otherwise the
2082+
// resolved cursor would stay applied over the whole scroll view, with no
2083+
// event left to clear it.
2084+
void _releaseStalePointerState() {
2085+
final bool hoverIsStale = _hoverIsActive && !enableGestures;
2086+
final bool dragIsStale = _dragIsActive && !_canHandleScrollGestures();
2087+
if (!hoverIsStale && !dragIsStale) {
2088+
return;
2089+
}
2090+
if (hoverIsStale) {
2091+
_hoverIsActive = false;
2092+
}
2093+
if (dragIsStale) {
2094+
_dragIsActive = false;
2095+
_thumbDragPointerKind = null;
2096+
}
2097+
updateMouseCursor();
2098+
}
2099+
20382100
void _handleThumbDragCancel() {
20392101
if (_gestureDetectorKey.currentContext == null) {
20402102
// The cancel was caused by the GestureDetector getting disposed, which
@@ -2049,6 +2111,7 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
20492111
_thumbDrag?.cancel();
20502112
assert(_thumbHold == null);
20512113
assert(_thumbDrag == null);
2114+
_thumbDragPointerKind = null;
20522115
if (_dragIsActive) {
20532116
_dragIsActive = false;
20542117
updateMouseCursor();
@@ -2197,9 +2260,18 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
21972260

21982261
/// The set of [WidgetState]s used to resolve [RawScrollbar.mouseCursor].
21992262
///
2263+
/// Contains [WidgetState.dragged] while the scrollbar thumb is being dragged,
2264+
/// [WidgetState.hovered] while a pointer is over the painted scrollbar track
2265+
/// or thumb, and [WidgetState.disabled] while the scrollbar is not
2266+
/// interactive.
2267+
///
22002268
/// Subclasses can override this to add additional states.
22012269
@protected
2202-
Set<WidgetState> get cursorStates => <WidgetState>{if (_dragIsActive) WidgetState.dragged};
2270+
Set<WidgetState> get cursorStates => <WidgetState>{
2271+
if (_dragIsActive) WidgetState.dragged,
2272+
if (_hoverIsActive) WidgetState.hovered,
2273+
if (!enableGestures) WidgetState.disabled,
2274+
};
22032275

22042276
/// Recomputes the effective mouse cursor for the scrollbar and updates the
22052277
/// active cursor if it has changed.
@@ -2212,7 +2284,8 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
22122284
// The wrapping MouseRegion covers the entire scrollable viewport rather
22132285
// than just the scrollbar painter. Therefore, _hoverIsActive is used to
22142286
// gate the cursor override to the painted scrollbar track/thumb; otherwise
2215-
// MouseCursor.defer is used to preserve ancestor cursors (e.g. TextField I-beam).
2287+
// MouseCursor.defer is used to preserve ancestor cursors (e.g. the
2288+
// EditableText I-beam).
22162289
final MouseCursor? resolved = widget.mouseCursor?.resolve(cursorStates);
22172290
final MouseCursor desired = ((_hoverIsActive || _dragIsActive) && resolved != null)
22182291
? resolved

0 commit comments

Comments
 (0)