diff --git a/app/lib/application/app.dart b/app/lib/application/app.dart index 2f343a33..d1c8082b 100644 --- a/app/lib/application/app.dart +++ b/app/lib/application/app.dart @@ -1,4 +1,5 @@ import '../ui/adaptive/platform.dart'; +import '../ui/adaptive/window_margins.dart'; import '../ui/theme/app_font_controller.dart'; import '../ui/theme/app_theme.dart'; import '../ui/scale/ui_scale.dart'; @@ -102,6 +103,7 @@ class MyApp extends StatelessWidget { bool get _platformIsCupertino => prefersCupertino(); - static Widget _scaled(BuildContext context, Widget? child) => - UiScaleHost(child: child ?? const SizedBox.shrink()); + static Widget _scaled(BuildContext context, Widget? child) => UiScaleHost( + child: WindowMargins(child: child ?? const SizedBox.shrink()), + ); } diff --git a/app/lib/application/pipelines_root.dart b/app/lib/application/pipelines_root.dart index bce7a570..9a5090d2 100644 --- a/app/lib/application/pipelines_root.dart +++ b/app/lib/application/pipelines_root.dart @@ -1,6 +1,7 @@ import 'dart:async'; import '../core/servers/chat/chat_server_pool.dart'; import '../ui/adaptive/platform.dart'; +import '../ui/adaptive/window_margins.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -370,16 +371,18 @@ class _PipelinesRootState extends State { builder: (context, _) => _cupertino ? CupertinoApp( title: 'Pipelines', - builder: (context, child) => - UiScaleHost(child: child ?? const SizedBox.shrink()), + builder: (context, child) => UiScaleHost( + child: WindowMargins(child: child ?? const SizedBox.shrink()), + ), theme: appCupertinoThemeWith(fontFamily: fonts.family), debugShowCheckedModeBanner: false, home: flow, ) : MaterialApp( title: 'Pipelines', - builder: (context, child) => - UiScaleHost(child: child ?? const SizedBox.shrink()), + builder: (context, child) => UiScaleHost( + child: WindowMargins(child: child ?? const SizedBox.shrink()), + ), theme: appLightTheme(fontFamily: fonts.family), darkTheme: appDarkTheme(fontFamily: fonts.family), debugShowCheckedModeBanner: false, diff --git a/app/lib/ui/adaptive/segmented_bar.dart b/app/lib/ui/adaptive/segmented_bar.dart index 31b80ac1..5e0a30a3 100644 --- a/app/lib/ui/adaptive/segmented_bar.dart +++ b/app/lib/ui/adaptive/segmented_bar.dart @@ -11,6 +11,7 @@ import 'segmented_bar_overflow.dart'; import 'segmented_bar_motion.dart'; import 'segmented_bar_pieces.dart'; import 'window_controls.dart'; +import 'window_margins.dart'; export 'segmented_bar_overflow.dart' show @@ -43,8 +44,9 @@ export 'segmented_bar_pieces.dart' show SegmentedBackButton, SegmentedPill; /// [CupertinoPageScaffold.navigationBar], which inset the content by the /// bar's height through [MediaQuery.paddingOf], so a page keeps its /// scaffold's structure; [SegmentedBarOverlay] does the same anywhere else. -/// It handles the top safe area itself, and keeps its pieces clear of the -/// system's window controls ([WindowControlsClearance]). +/// It handles the top safe area itself, keeps its pieces clear of the +/// system's window controls ([WindowControlsClearance]), and on a wide window +/// keeps the wider [WindowMargins] from the top and the sides. class SegmentedBar extends StatefulWidget implements ObstructingPreferredSizeWidget { const SegmentedBar({ @@ -142,7 +144,7 @@ class SegmentedBar extends StatefulWidget /// top edge (a page's own tint, such as a chat's colour). It takes no taps. final Widget? backdrop; - static const double _topPadding = 8; + static const double _topPadding = WindowMargins.narrowTop; /// The share of the bar the title always keeps beside the actions. static const double minTitleShare = 0.25; @@ -530,6 +532,9 @@ class _SegmentedBarState extends State { ); final background = widget._background(context); final inset = MediaQuery.paddingOf(context).top; + // A wide window's extra top margin arrives in the top padding (see + // WindowMargins), so the reserved height stays the bar's own. + final side = WindowMargins.sideOf(context); return Stack( children: [ // The scrim: the page's background, opaque at the edge and fading out @@ -570,9 +575,9 @@ class _SegmentedBarState extends State { bottom: false, child: Padding( padding: EdgeInsetsDirectional.fromSTEB( - 12, + side, SegmentedBar._topPadding, - 12, + side, bottomPadding, ), // In a window, iPadOS draws its window controls over the top diff --git a/app/lib/ui/adaptive/top_glass_strip.dart b/app/lib/ui/adaptive/top_glass_strip.dart index 892a296a..00805d76 100644 --- a/app/lib/ui/adaptive/top_glass_strip.dart +++ b/app/lib/ui/adaptive/top_glass_strip.dart @@ -1,6 +1,7 @@ import 'package:flutter/widgets.dart'; import 'glass_pill.dart'; +import 'window_margins.dart'; /// The corner radius of a [TopGlassStrip]. const double kTopGlassStripRadius = 22; @@ -11,8 +12,8 @@ const double kTopGlassStripRadius = 22; /// /// Float it with `FloatingBarOverlay(edge: FloatingBarEdge.top)`: the strip /// sits below the bar and the content's top padding grows by the strip's -/// height, so at rest nothing is hidden. It starts at the bar's 12px side -/// inset, is as wide as its content up to [maxWidth] (and the area), and is +/// height, so at rest nothing is hidden. It starts at the bar's side inset +/// ([WindowMargins.sideOf]), is as wide as its content up to [maxWidth] (and the area), and is /// only as tall as its content; the space beside it passes taps through. class TopGlassStrip extends StatelessWidget { const TopGlassStrip({ @@ -35,8 +36,14 @@ class TopGlassStrip extends StatelessWidget { @override Widget build(BuildContext context) { final insets = MediaQuery.paddingOf(context); + final side = WindowMargins.sideOf(context); return Padding( - padding: EdgeInsets.fromLTRB(12 + insets.left, 0, 12 + insets.right, 8), + padding: EdgeInsets.fromLTRB( + side + insets.left, + 0, + side + insets.right, + 8, + ), child: Align( alignment: AlignmentDirectional.topStart, heightFactor: 1, diff --git a/app/lib/ui/adaptive/window_margins.dart b/app/lib/ui/adaptive/window_margins.dart new file mode 100644 index 00000000..77c6278b --- /dev/null +++ b/app/lib/ui/adaptive/window_margins.dart @@ -0,0 +1,80 @@ +import 'package:flutter/widgets.dart'; + +import 'floating_bar_layout.dart'; + +/// The margins the app's chrome keeps from the window's edges. +/// +/// On a phone the top bar's pieces sit 8 px below the safe area and 12 px +/// from the sides. On a wide window (a desktop, a tablet, an unfolded fold: +/// both sides at least [kFloatingBarWideWidth]) every piece keeps the +/// floating tab bar's [kFloatingBarMargin] from the top and the sides +/// instead, in every pane and page. +/// +/// The extra top margin is added to the subtree's [MediaQuery] padding, like +/// a taller safe area: a scaffold reserves a bar's fixed preferred height +/// plus that padding, so every bar, and everything that clears or aligns +/// with a bar (list clearances, pinned headers, overlays measuring the bar), +/// moves down together without knowing about the margin. +/// +/// Place it once at the app's root, below the interface-scale host, so the +/// width is the scaled one and pushed pages, dialogs and sheets share it. +class WindowMargins extends StatelessWidget { + const WindowMargins({super.key, required this.child}); + + final Widget child; + + /// A bar piece's distance from the top (below the safe area) and from the + /// sides on a phone. + static const double narrowTop = 8; + static const double narrowSide = 12; + + /// The same distances on a wide window. + static const double wideMargin = kFloatingBarMargin; + + /// What the wide window adds to the top padding: the bar's own top inset + /// stays [narrowTop]. + static const double wideExtraTop = wideMargin - narrowTop; + + /// Whether [size] is a wide window. + static bool isWide(Size size) => size.shortestSide >= kFloatingBarWideWidth; + + /// Whether the margins above [context] are the wide ones; false without a + /// [WindowMargins] (a bar in isolation keeps the phone's). + static bool wideOf(BuildContext context) => + context.dependOnInheritedWidgetOfExactType<_WindowMarginsScope>()?.wide ?? + false; + + /// A top bar piece's distance from the sides at [context]. + static double sideOf(BuildContext context) => + wideOf(context) ? wideMargin : narrowSide; + + @override + Widget build(BuildContext context) { + final media = MediaQuery.of(context); + final wide = isWide(media.size); + final extra = wide ? wideExtraTop : 0.0; + // The same tree either way, so crossing the width never remounts the app. + return _WindowMarginsScope( + wide: wide, + child: MediaQuery( + data: media.copyWith( + padding: media.padding.copyWith(top: media.padding.top + extra), + viewPadding: media.viewPadding.copyWith( + top: media.viewPadding.top + extra, + ), + ), + child: child, + ), + ); + } +} + +class _WindowMarginsScope extends InheritedWidget { + const _WindowMarginsScope({required this.wide, required super.child}); + + final bool wide; + + @override + bool updateShouldNotify(_WindowMarginsScope oldWidget) => + wide != oldWidget.wide; +} diff --git a/app/lib/ui/layout/sticky_section.dart b/app/lib/ui/layout/sticky_section.dart index fa47650c..20fd8e70 100644 --- a/app/lib/ui/layout/sticky_section.dart +++ b/app/lib/ui/layout/sticky_section.dart @@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../adaptive/host.dart'; +import '../adaptive/window_margins.dart'; import '../theme/app_theme.dart'; import '../motion/motion.dart'; import 'section_heading.dart'; @@ -197,8 +198,13 @@ class StickySectionTitle extends StatelessWidget { color: muted, ), ); + // On a wide window the chevron keeps the title's 16 px from the edge, in + // line with the top bar's trailing piece; a phone keeps it closer. final row = Padding( - padding: const EdgeInsetsDirectional.only(start: 16, end: 8), + padding: EdgeInsetsDirectional.only( + start: 16, + end: WindowMargins.wideOf(context) ? 16 : 8, + ), child: Row( children: [ Expanded( diff --git a/app/test/shell/window_margins_shell_test.dart b/app/test/shell/window_margins_shell_test.dart new file mode 100644 index 00000000..ffbd711c --- /dev/null +++ b/app/test/shell/window_margins_shell_test.dart @@ -0,0 +1,200 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/shell/adaptive_shell.dart'; +import 'package:pipelines_app/shell/destination.dart'; +import 'package:pipelines_app/shell/detail_page.dart'; +import 'package:pipelines_app/shell/list_detail.dart'; +import 'package:pipelines_app/shell/shell_bar.dart'; +import 'package:pipelines_app/ui/adaptive/floating_bar_layout.dart'; +import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; +import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; +import 'package:pipelines_app/ui/adaptive/window_margins.dart'; + +/// On a wide window every top bar keeps the floating tab bar's 16 px from the +/// top and the sides, in the list pane and the detail pane alike; a phone +/// keeps the bar's 8 px top and 12 px sides. +void main() { + Future setSize(WidgetTester tester, Size size) async { + tester.view.physicalSize = size; + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + } + + Widget list() => ShellBar( + actions: const [Icon(Icons.sort, semanticLabel: 'Sort')], + child: Builder( + builder: (context) => ListView( + key: const Key('list-list'), + padding: withTopBarClearance(context, EdgeInsets.zero), + children: [ + for (var index = 0; index < 30; index++) + SizedBox(height: 40, child: Text('list row $index')), + ], + ), + ), + ); + + Widget item() => ShellBar( + title: 'Open item', + actions: const [Icon(Icons.more_vert, semanticLabel: 'More')], + child: Builder( + builder: (context) => ListView( + key: const Key('item-list'), + padding: withTopBarClearance(context, EdgeInsets.zero), + children: [ + for (var index = 0; index < 30; index++) + SizedBox(height: 40, child: Text('item row $index')), + ], + ), + ), + ); + + Widget app({required bool cupertino, required bool split}) { + final shell = AdaptiveShell( + cupertino: cupertino, + destinationBodies: { + AppDestination.runs: (_) => split + ? ListDetailPanes( + list: list(), + detail: DetailPane( + destination: AppDestination.runs, + implyBack: false, + builder: (_) => item(), + ), + ) + : list(), + }, + destination: AppDestination.runs, + ); + Widget margins(BuildContext context, Widget? child) => + WindowMargins(child: child!); + return cupertino + ? CupertinoApp(builder: margins, home: shell) + : MaterialApp(builder: margins, home: shell); + } + + Finder pills(Finder bar) => + find.descendant(of: bar, matching: find.byType(GlassPill)); + final shellBar = find.byWidgetPredicate( + (widget) => widget is SegmentedBar && widget.title == 'Runs', + ); + final paneBar = find.descendant( + of: find.byType(DetailPane), + matching: find.byType(SegmentedBar), + ); + + /// The bar's pieces span: the leading edge of the first, the trailing + /// edge of the last, and their shared top and bottom. + Rect span(WidgetTester tester, Finder bar) { + final rects = pills(bar).evaluate().map((element) { + final box = element.renderObject! as RenderBox; + return box.localToGlobal(Offset.zero) & box.size; + }).toList(); + return rects.reduce((a, b) => a.expandToInclude(b)); + } + + for (final cupertino in [false, true]) { + final host = cupertino ? 'Cupertino' : 'Material'; + + testWidgets('$host 1280: list and detail bars keep 16 px from the top ' + 'and every side', (tester) async { + await setSize(tester, const Size(1280, 800)); + await tester.pumpWidget(app(cupertino: cupertino, split: true)); + await tester.pump(); + // The pieces pop in; measure them at rest. + await tester.pump(const Duration(seconds: 2)); + + final listPane = tester.getRect( + find.byKey(const ValueKey('list-detail-list')), + ); + final body = tester.getRect( + find.byKey(const ValueKey('list-detail-body')), + ); + final listBar = span(tester, shellBar); + final itemBar = span(tester, paneBar); + expect(listBar.top, closeTo(16, 0.5)); + expect(listBar.left, closeTo(listPane.left + 16, 0.5)); + expect(listBar.right, closeTo(listPane.right - 16, 0.5)); + expect(itemBar.top, closeTo(16, 0.5)); + expect(itemBar.left, closeTo(body.left + 16, 0.5)); + expect(itemBar.right, closeTo(body.right - 16, 0.5)); + + // Each pane's content still starts below its bar (and its 8 px gap) + // at rest, so nothing hides behind the taller margin. + expect( + tester.getTopLeft(find.text('list row 0')).dy, + greaterThanOrEqualTo(listBar.bottom + 8 - 0.5), + ); + expect( + tester.getTopLeft(find.text('item row 0')).dy, + greaterThanOrEqualTo(itemBar.bottom + 8 - 0.5), + ); + expect(tester.takeException(), isNull); + }); + + testWidgets('$host 390: the phone keeps the bar at 8 px from the top and ' + '12 px from the sides', (tester) async { + await setSize(tester, const Size(390, 844)); + await tester.pumpWidget(app(cupertino: cupertino, split: false)); + await tester.pump(); + // The pieces pop in; measure them at rest. + await tester.pump(const Duration(seconds: 2)); + + final bar = span(tester, shellBar); + expect(bar.top, closeTo(8, 0.5)); + expect(bar.left, closeTo(12, 0.5)); + expect(bar.right, closeTo(390 - 12, 0.5)); + expect( + tester.getTopLeft(find.text('list row 0')).dy, + greaterThanOrEqualTo(bar.bottom + 8 - 0.5), + ); + expect(tester.takeException(), isNull); + }); + + testWidgets('$host 1280: a pushed page\'s bar keeps the wide margins', ( + tester, + ) async { + await setSize(tester, const Size(1280, 800)); + final bar = SegmentedBar( + cupertino: cupertino, + title: 'Settings', + actions: const [Icon(Icons.more_vert, semanticLabel: 'More')], + ); + final body = Builder( + builder: (context) => ListView( + padding: withTopBarClearance(context, EdgeInsets.zero), + children: const [SizedBox(height: 40, child: Text('first row'))], + ), + ); + Widget margins(BuildContext context, Widget? child) => + WindowMargins(child: child!); + await tester.pumpWidget( + cupertino + ? CupertinoApp( + builder: margins, + home: CupertinoPageScaffold(navigationBar: bar, child: body), + ) + : MaterialApp( + builder: margins, + home: Scaffold( + extendBodyBehindAppBar: true, + appBar: bar, + body: body, + ), + ), + ); + await tester.pump(const Duration(seconds: 2)); + + final pieces = span(tester, find.byType(SegmentedBar)); + expect(pieces.top, closeTo(16, 0.5)); + expect(pieces.left, closeTo(16, 0.5)); + expect(pieces.right, closeTo(1280 - 16, 0.5)); + expect( + tester.getTopLeft(find.text('first row')).dy, + closeTo(pieces.bottom + 8, 0.5), + ); + expect(tester.takeException(), isNull); + }); + } +} diff --git a/app/test/ui/adaptive/window_margins_test.dart b/app/test/ui/adaptive/window_margins_test.dart new file mode 100644 index 00000000..66cdcd8e --- /dev/null +++ b/app/test/ui/adaptive/window_margins_test.dart @@ -0,0 +1,78 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/adaptive/window_margins.dart'; + +void main() { + test('a window is wide when both sides reach the wide breakpoint', () { + expect(WindowMargins.isWide(const Size(1280, 800)), isTrue); + expect(WindowMargins.isWide(const Size(800, 1000)), isTrue); + expect(WindowMargins.isWide(const Size(390, 844)), isFalse); + // A phone on its side keeps the phone's margins. + expect(WindowMargins.isWide(const Size(844, 390)), isFalse); + }); + + Future<(EdgeInsets, bool, double)> pumpAt( + WidgetTester tester, + Size size, { + EdgeInsets padding = EdgeInsets.zero, + }) async { + late EdgeInsets seen; + late bool wide; + late double side; + await tester.pumpWidget( + MediaQuery( + data: MediaQueryData(size: size, padding: padding), + child: WindowMargins( + child: Builder( + builder: (context) { + seen = MediaQuery.paddingOf(context); + wide = WindowMargins.wideOf(context); + side = WindowMargins.sideOf(context); + return const SizedBox(); + }, + ), + ), + ), + ); + return (seen, wide, side); + } + + testWidgets('a wide window adds 8 px to the top padding only', ( + tester, + ) async { + final (padding, wide, side) = await pumpAt( + tester, + const Size(1280, 800), + padding: const EdgeInsets.fromLTRB(4, 24, 4, 20), + ); + expect(padding, const EdgeInsets.fromLTRB(4, 32, 4, 20)); + expect(wide, isTrue); + expect(side, 16); + }); + + testWidgets('a phone keeps its padding and the 12 px sides', (tester) async { + final (padding, wide, side) = await pumpAt( + tester, + const Size(390, 844), + padding: const EdgeInsets.only(top: 47), + ); + expect(padding, const EdgeInsets.only(top: 47)); + expect(wide, isFalse); + expect(side, 12); + }); + + testWidgets('without a WindowMargins a bar keeps the phone margins', ( + tester, + ) async { + late double side; + await tester.pumpWidget( + Builder( + builder: (context) { + side = WindowMargins.sideOf(context); + return const SizedBox(); + }, + ), + ); + expect(side, 12); + }); +}