113 lines
4.7 KiB
Dart
113 lines
4.7 KiB
Dart
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
/// Mouse-wheel and trackpad scrolling that reaches the page's vertical list.
|
|
///
|
|
/// A [Scrollable] claims a pointer scroll event whenever its own axis' delta
|
|
/// can move it, and the first claim (the innermost) wins. Two cases follow
|
|
/// that make a vertical list feel stuck:
|
|
///
|
|
/// * a horizontally scrolling code block or table takes a trackpad gesture
|
|
/// for its small sideways component, and the vertical part is dropped;
|
|
/// * controls floating over the list (a bar, a composer, a button) claim
|
|
/// nothing, so the wheel over them moves nothing.
|
|
///
|
|
/// [HorizontalScrollView] and [VerticalWheelForwarder] hand those events to
|
|
/// the vertical list instead.
|
|
|
|
/// Claims [event] for scrolling [position] by the event's vertical delta,
|
|
/// when that delta would move it. Returns whether it was claimed.
|
|
///
|
|
/// The claim goes through the [PointerSignalResolver], so a scrollable deeper
|
|
/// under the pointer that claimed the event first keeps it.
|
|
bool claimVerticalWheel(PointerSignalEvent event, ScrollPosition position) {
|
|
if (event is! PointerScrollEvent) return false;
|
|
if (position.axis != Axis.vertical || !position.hasPixels) return false;
|
|
if (!position.physics.shouldAcceptUserOffset(position)) return false;
|
|
final dy = event.scrollDelta.dy;
|
|
final delta = axisDirectionIsReversed(position.axisDirection) ? -dy : dy;
|
|
if (delta == 0 || _target(position, delta) == position.pixels) return false;
|
|
GestureBinding.instance.pointerSignalResolver.register(event, (event) {
|
|
if (!position.hasPixels) return;
|
|
if (_target(position, delta) == position.pixels) return;
|
|
position.pointerScroll(delta);
|
|
(event as PointerScrollEvent).respond(allowPlatformDefault: false);
|
|
});
|
|
return true;
|
|
}
|
|
|
|
double _target(ScrollPosition position, double delta) =>
|
|
(position.pixels + delta)
|
|
.clamp(position.minScrollExtent, position.maxScrollExtent)
|
|
.toDouble();
|
|
|
|
/// Whether [event] means to scroll sideways: a mostly horizontal gesture, or
|
|
/// a mouse wheel turned with the axis modifier (Shift) held.
|
|
bool isSidewaysWheel(BuildContext context, PointerScrollEvent event) {
|
|
final delta = event.scrollDelta;
|
|
if (event.kind == PointerDeviceKind.mouse) {
|
|
final modifiers = ScrollConfiguration.of(context).pointerAxisModifiers;
|
|
final pressed = HardwareKeyboard.instance.logicalKeysPressed;
|
|
if (pressed.any(modifiers.contains)) return true;
|
|
}
|
|
return delta.dx.abs() > delta.dy.abs();
|
|
}
|
|
|
|
/// A horizontally scrolling box (a code block, a table) inside a vertical
|
|
/// list, which leaves mostly vertical wheel and trackpad scrolling to that
|
|
/// list. A mostly sideways gesture, or Shift + wheel, scrolls this box; touch
|
|
/// drags are unchanged (the gesture arena already splits them by axis).
|
|
class HorizontalScrollView extends StatelessWidget {
|
|
const HorizontalScrollView({super.key, required this.child, this.controller});
|
|
|
|
final Widget child;
|
|
final ScrollController? controller;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => SingleChildScrollView(
|
|
scrollDirection: Axis.horizontal,
|
|
controller: controller,
|
|
// Below the horizontal scrollable, so this claim comes before its own.
|
|
// Translucent: the gaps between a table's cells or a block's lines hit no
|
|
// child, yet the scrollable around them still claims the event there.
|
|
child: Listener(
|
|
behavior: HitTestBehavior.translucent,
|
|
onPointerSignal: (event) {
|
|
if (event is! PointerScrollEvent) return;
|
|
if (isSidewaysWheel(context, event)) return;
|
|
final list = Scrollable.maybeOf(context, axis: Axis.vertical);
|
|
if (list != null) claimVerticalWheel(event, list.position);
|
|
},
|
|
child: child,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Scrolls [controller]'s vertical list with a wheel or trackpad over
|
|
/// [child] (controls floating over the list) when nothing under the pointer
|
|
/// claimed the event first, as the browser scrolls a page under fixed bars.
|
|
/// A multi-line field that can still scroll its own text keeps the event.
|
|
class VerticalWheelForwarder extends StatelessWidget {
|
|
const VerticalWheelForwarder({
|
|
super.key,
|
|
required this.controller,
|
|
required this.child,
|
|
});
|
|
|
|
final ScrollController controller;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Listener(
|
|
onPointerSignal: (event) {
|
|
if (event is! PointerScrollEvent || !controller.hasClients) return;
|
|
if (isSidewaysWheel(context, event)) return;
|
|
// A list with several positions (it is being swapped) is left alone.
|
|
if (controller.positions.length != 1) return;
|
|
claimVerticalWheel(event, controller.position);
|
|
},
|
|
child: child,
|
|
);
|
|
}
|