Files
pipelines/app/lib/ui/adaptive/window_margins.dart
T
alan a49b59a2bc Keep even 16px window margins for top bars on wide windows
On a wide window (both sides at least 600 logical px) every SegmentedBar
keeps the floating tab bar's 16px from the top and the sides, in every
pane and page. A root WindowMargins scope adds the extra 8px to the top
media padding, so Cupertino's fixed reserved bar height, list clearances,
pinned headers and measured overlays all move together; the bar and the
TopGlassStrip read the 16px side inset from the same scope, and sticky
section chevrons keep 16px from the trailing edge. Phones keep 8/12.
2026-11-10 13:09:36 +00:00

81 lines
2.9 KiB
Dart

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;
}