From ccd6badec4d69c0ca5c0b4cd6d8efe57f554bbdf Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Sat, 7 Nov 2026 19:32:12 +0700 Subject: [PATCH] perf(desktop): keep resizes from rebuilding Home and hidden tabs Home's explore sizing read the whole pane size from its state, so every resize, rail-expand and inspector frame rebuilt the full Home tree. On desktop the card scale is now the constant it always clamps to, the height cap and the empty state read MediaQuery.heightOf, and Material desktop decides tablet type sizes by height alone (its pane is always wider than 600). Library's cover size takes its desktop constant, and wideListInset depends on width only. Material PageView pages now sit in the hidden-tab MediaQuery that Mornye's LazyTabView already used (exported as HiddenTabMediaQuery), so kept-alive hidden tabs keep their last size until they show again. Under reduced motion Flutter's NavigationRail ran its 200 ms extend animation; a rail per state now snaps to its width. --- lib/screens/home_tab.dart | 22 +++++++-- lib/screens/home_tab_explore.dart | 6 +-- lib/screens/main_shell.dart | 7 ++- lib/screens/queue_tab.dart | 7 ++- lib/utils/adaptive_layout.dart | 3 +- lib/widgets/desktop/desktop_navigation.dart | 9 +++- lib/widgets/lazy_tab_view.dart | 18 ++++--- test/desktop_shell_test.dart | 26 ++++++++++ test/main_shell_rebuild_test.dart | 53 +++++++++++++++++++++ 9 files changed, 132 insertions(+), 19 deletions(-) diff --git a/lib/screens/home_tab.dart b/lib/screens/home_tab.dart index f70e4c983..d9b34f2aa 100644 --- a/lib/screens/home_tab.dart +++ b/lib/screens/home_tab.dart @@ -191,16 +191,30 @@ class _HomeTabState extends ConsumerState double _exploreCardSize(BuildContext context) { // Six phone-sized cards across an iPad made the feed look like a desktop // page zoomed out. A higher tablet cap gives roughly five generous cards - // in portrait while leaving all phone sizes unchanged. - final scale = responsiveScale(context: context, min: 0.82, max: 1.32); + // in portrait while leaving all phone sizes unchanged. A desktop pane is + // always wider than the 515 that reaches the cap, and one shorter than + // that takes the height cap below, so desktop skips the size read and + // width-only resizes do not rebuild Home. + final scale = isDesktopPlatform + ? 1.32 + : responsiveScale(context: context, min: 0.82, max: 1.32); final textScale = effectiveTextScale(context); final size = 145 * scale * (1 + (textScale - 1) * 0.12); // Landscape tablets are short: one carousel row should not take over a // third of the screen. Portrait and phone heights never reach this cap. - final heightCap = MediaQuery.sizeOf(context).height * 0.24; + final heightCap = MediaQuery.heightOf(context) * 0.24; return size < heightCap ? size : heightCap; } + /// Whether Home takes its tablet type sizes (a shortest side of at least + /// 600). A Material desktop pane is always wider than that (an 800 window + /// less the collapsed rail, or a 1200 window less the expanded rail and + /// the inspector beside it), so only its height counts there. + bool _usesTabletSizes(BuildContext context) => + isDesktopPlatform && !context.isMornye + ? MediaQuery.heightOf(context) >= 600 + : MediaQuery.sizeOf(context).shortestSide >= 600; + /// The square artwork of an explore card. Each card pads 6 on both sides, /// so the artwork takes the width that is left rather than the full card. double _exploreCoverSize(BuildContext context) => @@ -835,7 +849,7 @@ class _HomeTabState extends ConsumerState final isShowingRecentAccess = _showsSearch && ref.watch(trackProvider.select((s) => s.isShowingRecentAccess)); - final screenHeight = MediaQuery.sizeOf(context).height; + final screenHeight = MediaQuery.heightOf(context); final recentModeRequested = widget.mode == HomeTabMode.search || diff --git a/lib/screens/home_tab_explore.dart b/lib/screens/home_tab_explore.dart index 25413f47f..bd12a75a2 100644 --- a/lib/screens/home_tab_explore.dart +++ b/lib/screens/home_tab_explore.dart @@ -8,7 +8,7 @@ extension _HomeTabExploreUI on _HomeTabState { ) { final hasGreeting = !context.isMornye && greeting != null && greeting.isNotEmpty; - final isTablet = MediaQuery.sizeOf(context).shortestSide >= 600; + final isTablet = _usesTabletSizes(context); final sectionOffset = hasGreeting ? 1 : 0; final totalCount = sections.length + sectionOffset + 1; @@ -57,7 +57,7 @@ extension _HomeTabExploreUI on _HomeTabState { ); } final sectionHeight = _exploreSectionHeight(context); - final isTablet = MediaQuery.sizeOf(context).shortestSide >= 600; + final isTablet = _usesTabletSizes(context); if (section.isQuickPicks) { return _buildQuickPicksSection(section, colorScheme); } @@ -189,7 +189,7 @@ extension _HomeTabExploreUI on _HomeTabState { ColorScheme colorScheme, ) { final isArtist = item.type == 'artist'; - final isTablet = MediaQuery.sizeOf(context).shortestSide >= 600; + final isTablet = _usesTabletSizes(context); final cardSize = _exploreCardSize(context); final coverSize = _exploreCoverSize(context); final iconSize = cardSize * 0.3; diff --git a/lib/screens/main_shell.dart b/lib/screens/main_shell.dart index b7b422127..8b084de53 100644 --- a/lib/screens/main_shell.dart +++ b/lib/screens/main_shell.dart @@ -1082,7 +1082,12 @@ class _MainShellState extends ConsumerState key: ValueKey('page-$index'), child: TickerMode( enabled: index == _currentIndex, - child: observeScroll(tabs[index], index), + // Kept-alive hidden pages keep their last size, so + // resizes and rail motion rebuild only the page shown. + child: HiddenTabMediaQuery( + hidden: index != _currentIndex, + child: observeScroll(tabs[index], index), + ), ), ), ), diff --git a/lib/screens/queue_tab.dart b/lib/screens/queue_tab.dart index 924895c3a..c9eeaa0a5 100644 --- a/lib/screens/queue_tab.dart +++ b/lib/screens/queue_tab.dart @@ -261,8 +261,11 @@ class _QueueTabState extends ConsumerState { ); double _queueCoverSize() { - final shortestSide = MediaQuery.sizeOf(context).shortestSide; - final scale = (shortestSide / 390).clamp(0.82, 1.0); + // A desktop pane's shortest side is always past 390, so desktop skips + // the size read and resizes do not rebuild Library through it. + final scale = isDesktopPlatform + ? 1.0 + : (MediaQuery.sizeOf(context).shortestSide / 390).clamp(0.82, 1.0); final textScale = effectiveTextScale(context); return (56 * scale * (1 + ((textScale - 1) * 0.12))).clamp(46.0, 56.0); } diff --git a/lib/utils/adaptive_layout.dart b/lib/utils/adaptive_layout.dart index 3508f3bdc..54e8c9967 100644 --- a/lib/utils/adaptive_layout.dart +++ b/lib/utils/adaptive_layout.dart @@ -120,7 +120,8 @@ double wideInsetForWidth( /// instead of stretching across a wide window. double wideListInset(BuildContext context, {double? contentMaxWidth}) => wideInsetForWidth( - MediaQuery.sizeOf(context).width, + // Width only, so a height change does not rebuild the caller. + MediaQuery.widthOf(context), contentMaxWidth: contentMaxWidth ?? (isDesktopPlatform diff --git a/lib/widgets/desktop/desktop_navigation.dart b/lib/widgets/desktop/desktop_navigation.dart index 67b8a027b..3fdd6038d 100644 --- a/lib/widgets/desktop/desktop_navigation.dart +++ b/lib/widgets/desktop/desktop_navigation.dart @@ -603,9 +603,12 @@ class DesktopNavigationRail extends StatelessWidget { final colorScheme = Theme.of(context).colorScheme; final background = colorScheme.surfaceContainer; if (!materialExpressiveEnabled(context)) { - // Reduced motion keeps Flutter's rail; Settings stays last. + // Reduced motion keeps Flutter's rail; Settings stays last. A new rail + // per state snaps to its width instead of running Flutter's extend + // animation, which ignores reduced motion. final index = destinations.indexWhere((d) => d.tab == selected); return NavigationRail( + key: ValueKey('desktop-rail-extended:$extended'), extended: extended, minExtendedWidth: desktopSidebarWidth, // Collapsed, the rail shows icons alone. @@ -635,9 +638,11 @@ class DesktopNavigationRail extends StatelessWidget { final settings = destinations .where((destination) => destination.tab == ShellTab.settings) .firstOrNull; + // Only reached with expressive motion on; reduced motion takes the + // static rail above, so the fallback never animates. final motion = AppMotion.spatial( context, - const AppMotionSpec(Duration(milliseconds: 300), Curves.easeInOutCubic), + const AppMotionSpec(Duration.zero, Curves.linear), ); final openLink = onOpenLibraryLink; // One expansion value drives the width, every pill and every label, so diff --git a/lib/widgets/lazy_tab_view.dart b/lib/widgets/lazy_tab_view.dart index da4fa88a1..fa4f506fb 100644 --- a/lib/widgets/lazy_tab_view.dart +++ b/lib/widgets/lazy_tab_view.dart @@ -89,7 +89,7 @@ class _LazyTabViewState extends State { excluding: index != widget.index, child: TickerMode( enabled: index == widget.index, - child: _TabInsets( + child: HiddenTabMediaQuery( hidden: index != widget.index, child: _mountedTabs.contains(_tabKey(index)) ? widget.children[index] @@ -102,18 +102,24 @@ class _LazyTabViewState extends State { ); } -/// Keeps hidden tabs at their last visible window size and without a keyboard. -class _TabInsets extends StatefulWidget { - const _TabInsets({required this.hidden, required this.child}); +/// Keeps hidden tabs at their last visible window size and without a keyboard, +/// so resizes and pane motion rebuild only the tab on screen. A tab gets the +/// current size when it shows again. +class HiddenTabMediaQuery extends StatefulWidget { + const HiddenTabMediaQuery({ + super.key, + required this.hidden, + required this.child, + }); final bool hidden; final Widget child; @override - State<_TabInsets> createState() => _TabInsetsState(); + State createState() => _HiddenTabMediaQueryState(); } -class _TabInsetsState extends State<_TabInsets> { +class _HiddenTabMediaQueryState extends State { Size? _visibleSize; @override diff --git a/test/desktop_shell_test.dart b/test/desktop_shell_test.dart index f70a34b90..ce83dea51 100644 --- a/test/desktop_shell_test.dart +++ b/test/desktop_shell_test.dart @@ -637,6 +637,32 @@ void main() { expect(menuIcon(Icons.menu_open), findsOneWidget); }); + testWidgets('reduced motion snaps the rail open and closed', ( + tester, + ) async { + tester.platformDispatcher.accessibilityFeaturesTestValue = + const FakeAccessibilityFeatures(disableAnimations: true); + addTearDown( + tester.platformDispatcher.clearAccessibilityFeaturesTestValue, + ); + await pumpShell(tester, mornye: false, width: 1400); + double railWidth() => tester.getSize(find.byType(NavigationRail)).width; + final expanded = railWidth(); + expect(expanded, greaterThan(desktopCollapsedRailWidth)); + + desktopSidebarCollapsed.value = true; + await tester.pump(); + final collapsed = railWidth(); + expect(collapsed, lessThan(expanded)); + // Nothing is left to animate. + await tester.pump(const Duration(milliseconds: 100)); + expect(railWidth(), collapsed); + + desktopSidebarCollapsed.value = false; + await tester.pump(); + expect(railWidth(), expanded); + }); + testWidgets('narrow windows hide the rail menu button', (tester) async { await pumpShell(tester, mornye: false, width: 900); diff --git a/test/main_shell_rebuild_test.dart b/test/main_shell_rebuild_test.dart index 6e3afc62c..d1094e49e 100644 --- a/test/main_shell_rebuild_test.dart +++ b/test/main_shell_rebuild_test.dart @@ -288,6 +288,59 @@ void main() { }); } + /// The widget [type]'s state built last; a new instance means its state + /// built again. + Widget builtBy(WidgetTester tester, Type type) { + late Widget child; + tester + .element(find.byType(type, skipOffstage: false).first) + .visitChildElements((element) => child = element.widget); + return child; + } + + testWidgets('desktop Material width changes rebuild neither Home nor a ' + 'hidden Library', (tester) async { + await pumpShell(tester, desktop: true, mornye: false); + ShellNavigationService.requestTab(ShellTab.library); + await settle(tester); + ShellNavigationService.requestTab(ShellTab.home); + await settle(tester); + final home = builtBy(tester, HomeTab); + final library = builtBy(tester, QueueTab); + Size librarySize() => MediaQuery.sizeOf( + tester.element(find.byType(QueueTab, skipOffstage: false).first), + ); + final hiddenSize = librarySize(); + + // Width-only frames, as in a live edge drag or the rail expanding. + for (var frame = 1; frame <= 10; frame++) { + tester.view.physicalSize = Size(1280.0 + frame * 12, 800); + await tester.pump(const Duration(milliseconds: 16)); + } + await settle(tester); + expect(builtBy(tester, HomeTab), same(home)); + expect(builtBy(tester, QueueTab), same(library)); + + // Home follows a height change; the hidden Library keeps its size. + tester.view.physicalSize = const Size(1400, 760); + await settle(tester); + expect(builtBy(tester, HomeTab), isNot(same(home))); + expect(builtBy(tester, QueueTab), same(library)); + expect(librarySize(), hiddenSize); + + // Library takes the new size when it shows again. + ShellNavigationService.requestTab(ShellTab.library); + await settle(tester); + expect(librarySize(), isNot(hiddenSize)); + expect( + librarySize().height, + MediaQuery.sizeOf( + tester.element(find.byType(HomeTab, skipOffstage: false).first), + ).height, + ); + expect(tester.takeException(), isNull); + }); + testWidgets('a Material rail playlist link fades in like a destination', ( tester, ) async {