From aa9072a9bfd32bd189ef920b3a504f90cacf1336 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:28:27 +0700 Subject: [PATCH] fix(lists): keep a dragged row under the finger when the interface is scaled Flutter's reorderable lists place the dragged row in the overlay by screen positions. With Interface size off 100%, or on a tablet, where the app scales itself, the overlay's pixels are not screen pixels, so the row was drawn up and to the left of the finger (at 85%, about 15% of the finger's distance from the top left corner), far enough to push its cover off the screen. ScaledDragProxy wraps the dragged row and moves it to where the finger is, from the overlay's own scale; it does nothing at 100%. It follows the row back to its place on a drop as well. Every reorderable list uses it: both queues, playlist and collection order, album track order and the two provider priority pages. --- lib/screens/playlist_reorder_page.dart | 2 + .../lyrics_provider_priority_page.dart | 2 + .../settings/provider_priority_list_page.dart | 2 + lib/utils/adaptive_layout.dart | 7 + lib/widgets/album_track_sort_button.dart | 13 ++ lib/widgets/collection_reorder_sliver.dart | 2 + lib/widgets/mornye_player_queue.dart | 12 +- lib/widgets/player_queue_sheet.dart | 51 +++---- lib/widgets/scaled_drag_proxy.dart | 128 ++++++++++++++++++ test/scaled_drag_proxy_test.dart | 103 ++++++++++++++ 10 files changed, 294 insertions(+), 28 deletions(-) create mode 100644 lib/widgets/scaled_drag_proxy.dart create mode 100644 test/scaled_drag_proxy_test.dart diff --git a/lib/screens/playlist_reorder_page.dart b/lib/screens/playlist_reorder_page.dart index 1fa245a40..43c4b990b 100644 --- a/lib/screens/playlist_reorder_page.dart +++ b/lib/screens/playlist_reorder_page.dart @@ -9,6 +9,7 @@ import 'package:mornye/widgets/app_snack_bar.dart'; import 'package:mornye/widgets/cached_cover_image.dart'; import 'package:mornye/widgets/discard_changes_dialog.dart'; import 'package:mornye/widgets/priority_settings_scaffold.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; import 'package:mornye/widgets/track_card.dart'; /// Reorders one playlist. Rows drag by their handle or after a long press, @@ -51,6 +52,7 @@ class _PlaylistReorderPageState extends ConsumerState { SliverPadding( padding: EdgeInsets.symmetric(horizontal: wideListInset(context)), sliver: SliverReorderableList( + proxyDecorator: ScaledDragProxy.plain, itemCount: _entries.length, itemBuilder: (context, index) { final entry = _entries[index]; diff --git a/lib/screens/settings/lyrics_provider_priority_page.dart b/lib/screens/settings/lyrics_provider_priority_page.dart index d7ce657d7..032181a0e 100644 --- a/lib/screens/settings/lyrics_provider_priority_page.dart +++ b/lib/screens/settings/lyrics_provider_priority_page.dart @@ -9,6 +9,7 @@ import 'package:mornye/utils/adaptive_layout.dart'; import 'package:mornye/widgets/discard_changes_dialog.dart'; import 'package:mornye/widgets/priority_settings_scaffold.dart'; import 'package:mornye/widgets/reorderable_priority_item.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; import 'package:mornye/widgets/settings_group.dart'; import 'package:mornye/constants/music_services.dart'; @@ -103,6 +104,7 @@ class _LyricsProviderPriorityPageState horizontal: 16 + wideListInset(context), ), sliver: SliverReorderableList( + proxyDecorator: ScaledDragProxy.plain, itemCount: _enabledProviders.length, itemBuilder: (context, index) { final id = _enabledProviders[index]; diff --git a/lib/screens/settings/provider_priority_list_page.dart b/lib/screens/settings/provider_priority_list_page.dart index 3fe168765..883632a41 100644 --- a/lib/screens/settings/provider_priority_list_page.dart +++ b/lib/screens/settings/provider_priority_list_page.dart @@ -6,6 +6,7 @@ import 'package:mornye/utils/adaptive_layout.dart'; import 'package:mornye/widgets/discard_changes_dialog.dart'; import 'package:mornye/widgets/priority_settings_scaffold.dart'; import 'package:mornye/widgets/reorderable_priority_item.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; /// Reorderable priority list shared by the download- and metadata-provider /// priority pages; the two differ only in which provider list they read/write @@ -79,6 +80,7 @@ class _ProviderPriorityListPageState horizontal: 16 + wideListInset(context), ), sliver: SliverReorderableList( + proxyDecorator: ScaledDragProxy.plain, itemCount: _providers.length, itemBuilder: (context, index) { final provider = _providers[index]; diff --git a/lib/utils/adaptive_layout.dart b/lib/utils/adaptive_layout.dart index 996d2c027..93090a743 100644 --- a/lib/utils/adaptive_layout.dart +++ b/lib/utils/adaptive_layout.dart @@ -2,6 +2,7 @@ import 'package:material_ui/material_ui.dart'; import 'package:mornye/theme/mornye_theme.dart'; import 'package:mornye/utils/desktop_platform.dart'; import 'package:mornye/utils/android_tv.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; /// Keeps touch-first tablets on the bottom navigation used by phones. A rail /// is reserved for genuinely desktop-width windows; the old 600dp threshold @@ -89,6 +90,12 @@ class _AdaptiveUiScalerState extends State { // starts or stops being 1, which adds or removes the layers around it. final _contentKey = GlobalKey(); + @override + void initState() { + super.initState(); + ScaledDragProxy.trackPointers(); + } + @override Widget build(BuildContext context) { final mediaQuery = MediaQuery.of(context); diff --git a/lib/widgets/album_track_sort_button.dart b/lib/widgets/album_track_sort_button.dart index 969916c31..af7acc658 100644 --- a/lib/widgets/album_track_sort_button.dart +++ b/lib/widgets/album_track_sort_button.dart @@ -6,6 +6,7 @@ import 'package:mornye/widgets/album_detail_header.dart'; import 'package:mornye/widgets/app_bottom_sheet.dart'; import 'package:mornye/widgets/app_icon.dart'; import 'package:mornye/widgets/desktop/context_menu.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; /// Picks how a Library album orders its songs, for the header. [songs] are /// the album's titles as the page shows them. @@ -128,6 +129,18 @@ class _CustomOrderListState extends ConsumerState<_CustomOrderList> { padding: const EdgeInsets.only(bottom: 16), itemCount: _songs.length, onReorderItem: _move, + // The list's own lift, inside the proxy that follows a scaled page. + proxyDecorator: (child, index, animation) => ScaledDragProxy( + animation: animation, + child: AnimatedBuilder( + animation: animation, + child: child, + builder: (context, child) => Material( + elevation: 6 * Curves.easeInOut.transform(animation.value), + child: child, + ), + ), + ), itemBuilder: (context, index) => ListTile( key: ValueKey('album-order-${_songs[index].$1}'), title: Text( diff --git a/lib/widgets/collection_reorder_sliver.dart b/lib/widgets/collection_reorder_sliver.dart index 738d721fc..3b54845f9 100644 --- a/lib/widgets/collection_reorder_sliver.dart +++ b/lib/widgets/collection_reorder_sliver.dart @@ -7,6 +7,7 @@ import 'package:mornye/theme/app_tokens.dart'; import 'package:mornye/utils/adaptive_layout.dart'; import 'package:mornye/widgets/app_icon.dart'; import 'package:mornye/widgets/cached_cover_image.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; import 'package:mornye/widgets/track_card.dart'; /// Editing has its own drag gestures, leaving normal track selection intact. @@ -41,6 +42,7 @@ class _CollectionReorderSliverState extends State { Widget build(BuildContext context) => SliverPadding( padding: EdgeInsets.symmetric(horizontal: wideListInset(context)), sliver: SliverReorderableList( + proxyDecorator: ScaledDragProxy.plain, itemCount: widget.entries.length, onReorderItem: (oldIndex, newIndex) { widget.onReorder(oldIndex, newIndex); diff --git a/lib/widgets/mornye_player_queue.dart b/lib/widgets/mornye_player_queue.dart index 138aed780..c4d986f83 100644 --- a/lib/widgets/mornye_player_queue.dart +++ b/lib/widgets/mornye_player_queue.dart @@ -10,6 +10,7 @@ import 'package:mornye/utils/desktop_platform.dart'; import 'package:mornye/widgets/player_queue_artwork.dart'; import 'package:mornye/widgets/player_queue_dismissible.dart'; import 'package:mornye/widgets/queue_swipe.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; /// Upcoming tracks share the player's artwork backdrop and transport controls. class MornyePlayerQueue extends ConsumerStatefulWidget { @@ -332,10 +333,13 @@ class _MornyePlayerQueueState extends ConsumerState { controller.moveQueueItem(start + oldIndex, start + newIndex); } }, - proxyDecorator: (child, index, animation) => Material( - color: colorScheme.surfaceContainerHigh, - borderRadius: BorderRadius.circular(12), - child: child, + proxyDecorator: (child, index, animation) => ScaledDragProxy( + animation: animation, + child: Material( + color: colorScheme.surfaceContainerHigh, + borderRadius: BorderRadius.circular(12), + child: child, + ), ), itemBuilder: (context, index) { final item = queue[start + index]; diff --git a/lib/widgets/player_queue_sheet.dart b/lib/widgets/player_queue_sheet.dart index 134ce4101..6eed8d687 100644 --- a/lib/widgets/player_queue_sheet.dart +++ b/lib/widgets/player_queue_sheet.dart @@ -15,6 +15,7 @@ import 'package:mornye/widgets/player_playback_controls.dart'; import 'package:mornye/widgets/player_queue_artwork.dart'; import 'package:mornye/widgets/player_queue_dismissible.dart'; import 'package:mornye/widgets/queue_swipe.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; Future showPlayerQueueSheet( BuildContext context, @@ -211,33 +212,35 @@ class _PlayerQueueSheetState extends ConsumerState { onReorderItem: (oldIndex, newIndex) { if (!locked) controller.moveQueueItem(oldIndex, newIndex); }, - proxyDecorator: (child, index, animation) { - if (!expressive) { - return Material( - elevation: 4, - color: colorScheme.surfaceContainerHighest, - borderRadius: BorderRadius.circular(12), - child: child, - ); - } - return AnimatedBuilder( - animation: animation, - child: child, - builder: (context, child) { - final t = _liftSpring.transform(animation.value); - return Transform.scale( - scale: 1 + 0.03 * t, - child: Material( - elevation: 6 * t.clamp(0.0, 1.0), + proxyDecorator: (child, index, animation) => ScaledDragProxy( + animation: animation, + child: !expressive + ? Material( + elevation: 4, color: colorScheme.surfaceContainerHighest, - borderRadius: BorderRadius.circular(_rowRadius + 8 * t), - clipBehavior: Clip.antiAlias, + borderRadius: BorderRadius.circular(12), child: child, + ) + : AnimatedBuilder( + animation: animation, + child: child, + builder: (context, child) { + final t = _liftSpring.transform(animation.value); + return Transform.scale( + scale: 1 + 0.03 * t, + child: Material( + elevation: 6 * t.clamp(0.0, 1.0), + color: colorScheme.surfaceContainerHighest, + borderRadius: BorderRadius.circular( + _rowRadius + 8 * t, + ), + clipBehavior: Clip.antiAlias, + child: child, + ), + ); + }, ), - ); - }, - ); - }, + ), itemBuilder: (context, i) { final item = queue[i]; if (i < _passedRows) { diff --git a/lib/widgets/scaled_drag_proxy.dart b/lib/widgets/scaled_drag_proxy.dart new file mode 100644 index 000000000..86876a269 --- /dev/null +++ b/lib/widgets/scaled_drag_proxy.dart @@ -0,0 +1,128 @@ +import 'package:flutter/gestures.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/widgets.dart'; + +/// Keeps a reorderable list's dragged row under the finger while the +/// interface is scaled. Flutter places that row by screen positions, which +/// are not the positions of a scaled page. For vertical lists. +class ScaledDragProxy extends SingleChildRenderObjectWidget { + const ScaledDragProxy({super.key, required this.animation, super.child}); + + /// The animation a list hands its `proxyDecorator`. + final Animation animation; + + /// The `proxyDecorator` of a list that draws the dragged row as it is. + static Widget plain(Widget child, int index, Animation animation) => + ScaledDragProxy(animation: animation, child: child); + + /// Starts following touches: a drag needs where its finger went down. + static void trackPointers() => _DragPointer.track(); + + @override + RenderObject createRenderObject(BuildContext context) => + _RenderScaledDragProxy(animation, _overlayOf(context)); + + @override + void updateRenderObject(BuildContext context, RenderObject renderObject) { + (renderObject as _RenderScaledDragProxy) + ..animation = animation + ..overlay = _overlayOf(context); + } + + static RenderBox? _overlayOf(BuildContext context) { + final box = Overlay.maybeOf(context)?.context.findRenderObject(); + return box is RenderBox ? box : null; + } +} + +/// The touch a drag follows. A vertical list keeps the row at the horizontal +/// position where the finger went down. +abstract final class _DragPointer { + static bool _tracking = false; + static int? _pointer; + static Offset? _down; + static Offset _last = Offset.zero; + + static void track() { + if (_tracking) return; + _tracking = true; + GestureBinding.instance.pointerRouter.addGlobalRoute(_route); + } + + static void _route(PointerEvent event) { + if (event is PointerDownEvent) { + _pointer = event.pointer; + _down = _last = event.position; + } else if (event is PointerMoveEvent && event.pointer == _pointer) { + _last = event.position; + } + } + + static Offset? get held => switch (_down) { + final down? => Offset(down.dx, _last.dy), + null => null, + }; +} + +class _RenderScaledDragProxy extends RenderProxyBox { + _RenderScaledDragProxy(this._animation, this.overlay); + + /// The overlay the list draws the row in, whose pixels are the page's. + RenderBox? overlay; + + Animation _animation; + set animation(Animation value) { + if (value == _animation) return; + if (attached) _animation.removeListener(markNeedsPaint); + _animation = value; + if (attached) value.addListener(markNeedsPaint); + } + + // Where the finger holds the row, from the row's corner. + Offset? _grab; + Offset _shift = Offset.zero; + + @override + void attach(PipelineOwner owner) { + super.attach(owner); + _animation.addListener(markNeedsPaint); + } + + @override + void detach() { + _animation.removeListener(markNeedsPaint); + super.detach(); + } + + Offset _measureShift() { + final overlay = this.overlay; + if (overlay == null || !overlay.attached) return Offset.zero; + final origin = overlay.localToGlobal(Offset.zero); + final scale = overlay.localToGlobal(const Offset(1, 0)).dx - origin.dx; + if (scale <= 0 || (scale - 1).abs() < 0.001) return Offset.zero; + // Flutter drew the row at the finger's screen position less the grab. + final drawn = localToGlobal(Offset.zero, ancestor: overlay); + final grab = _grab ??= switch (_DragPointer.held) { + final held? => held - origin - drawn, + null => Offset.zero, + }; + // A dropped row travels to its place on this curve, the grab with it. + final held = _animation.status == AnimationStatus.reverse + ? Curves.easeOut.transform(_animation.value) + : 1.0; + return (drawn + grab * held) * (1 / scale - 1); + } + + @override + void paint(PaintingContext context, Offset offset) { + final child = this.child; + if (child == null) return; + _shift = _measureShift(); + context.paintChild(child, offset + _shift); + } + + @override + void applyPaintTransform(RenderBox child, Matrix4 transform) { + transform.translateByDouble(_shift.dx, _shift.dy, 0, 1); + } +} diff --git a/test/scaled_drag_proxy_test.dart b/test/scaled_drag_proxy_test.dart new file mode 100644 index 000000000..72dcab789 --- /dev/null +++ b/test/scaled_drag_proxy_test.dart @@ -0,0 +1,103 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:mornye/utils/adaptive_layout.dart'; +import 'package:mornye/widgets/scaled_drag_proxy.dart'; + +Widget _app(double scale, {ReorderItemProxyDecorator? proxy}) => MaterialApp( + builder: (context, child) => + AdaptiveUiScaler(userScale: scale, child: child!), + home: Scaffold( + body: CustomScrollView( + slivers: [ + SliverPadding( + padding: const EdgeInsets.all(16), + sliver: SliverReorderableList( + itemCount: 5, + onReorderItem: (_, _) {}, + proxyDecorator: proxy, + itemBuilder: (context, index) => SizedBox( + key: ValueKey(index), + height: 60, + child: Row( + children: [ + Expanded(child: Text('Row $index')), + ReorderableDragStartListener( + index: index, + child: ColoredBox( + key: ValueKey('handle-$index'), + color: Colors.transparent, + child: const SizedBox.square(dimension: 44), + ), + ), + ], + ), + ), + ), + ), + ], + ), + ), +); + +void main() { + void phone(WidgetTester tester) { + tester.view.physicalSize = const Size(400, 800); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + } + + for (final scale in [0.8, 1.0, 1.3]) { + testWidgets('a dragged row stays under the finger at $scale', ( + tester, + ) async { + phone(tester); + await tester.pumpWidget(_app(scale, proxy: ScaledDragProxy.plain)); + final row = find.byKey(const ValueKey(2)); + final before = tester.getRect(row); + + final drag = await tester.startGesture( + tester.getCenter(find.byKey(const ValueKey('handle-2'))), + ); + await tester.pump(); + await drag.moveBy(const Offset(0, 20)); + await drag.moveBy(const Offset(3, 20)); + await tester.pump(); + // The row follows the finger down and keeps its place across. + final held = tester.getRect(row); + expect(held.left, closeTo(before.left, 0.5)); + expect(held.top, closeTo(before.top + 40, 0.5)); + expect(held.width, closeTo(before.width, 0.5)); + expect(held.height, closeTo(before.height, 0.5)); + + // It returns to its row without sliding sideways on the way. + await drag.up(); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + final dropping = tester.getRect(row); + expect(dropping.left, closeTo(before.left, 0.5)); + expect(dropping.top, inInclusiveRange(before.top, before.top + 40)); + await tester.pumpAndSettle(); + expect(tester.getTopLeft(row).dy, closeTo(before.top, 0.5)); + expect(tester.takeException(), isNull); + }); + } + + testWidgets('without the proxy a scaled page draws the row elsewhere', ( + tester, + ) async { + phone(tester); + await tester.pumpWidget(_app(0.8)); + final row = find.byKey(const ValueKey(2)); + final before = tester.getRect(row); + final drag = await tester.startGesture( + tester.getCenter(find.byKey(const ValueKey('handle-2'))), + ); + await tester.pump(); + await drag.moveBy(const Offset(0, 40)); + await tester.pump(); + // Flutter's own placement, which this widget exists to correct. + expect(tester.getRect(row).left, lessThan(before.left - 20)); + await drag.up(); + await tester.pumpAndSettle(); + }); +}