Files
pipelines/app/lib/ui/adaptive/window_margins.dart
T

87 lines
3.2 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;
/// Gives [child] the phone's margins inside a wide window: a small window
/// of its own (a floating chat) whose bars sit close to its frame. It
/// changes only the side inset; the window drops the top padding itself.
static Widget compact({required Widget child}) =>
_WindowMarginsScope(wide: false, child: child);
/// 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;
}