import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; /// Where the system's window controls cover the app, in global logical /// pixels: the close/minimise/zoom buttons iPadOS 26 draws over a windowed /// app's top corner. Null when there are none (full screen, other platforms). /// /// Flutter's [MediaQuery] padding is the plain safe area, which leaves the /// controls out, so the app's top bar asks this instead (see /// [WindowControlsClearance]). On iOS the rect comes from the Runner's /// `WindowControlsPlugin` (ios/Runner/WindowControlsPlugin.swift) and follows /// the window as it is resized; elsewhere it stays null. class WindowControls { WindowControls._(); static const channel = MethodChannel('pipelines/window_controls'); static ValueNotifier? _platform; /// The rect for [context]: a [WindowControlsScope]'s when there is one /// (tests), else the platform's. static ValueListenable of(BuildContext context) => context.dependOnInheritedWidgetOfExactType()?.rect ?? (_platform ??= _listen()); /// Forgets the platform's rect, so the next [of] asks the host again. @visibleForTesting static void debugReset() { _platform?.dispose(); _platform = null; channel.setMethodCallHandler(null); } static ValueNotifier _listen() { final rect = ValueNotifier(null); if (kIsWeb || defaultTargetPlatform != TargetPlatform.iOS) return rect; channel.setMethodCallHandler((call) async { if (call.method == 'changed') rect.value = _decode(call.arguments); }); channel .invokeMethod('start') .then((value) => rect.value = _decode(value)) // An iOS host without the plugin (a widget test, an app extension) // reports no controls; other failures surface. .catchError((Object _) {}, test: (e) => e is MissingPluginException); return rect; } static Rect? _decode(Object? value) { if (value is! Map) return null; double side(String key) => (value[key] as num?)?.toDouble() ?? 0; final rect = Rect.fromLTRB( side('left'), side('top'), side('right'), side('bottom'), ); return rect.isEmpty ? null : rect; } } /// Supplies the [WindowControls] rect to its subtree instead of the /// platform's (a fake in tests). class WindowControlsScope extends InheritedWidget { const WindowControlsScope({ super.key, required this.rect, required super.child, }); final ValueListenable rect; @override bool updateShouldNotify(WindowControlsScope oldWidget) => rect != oldWidget.rect; } /// Pads [child] at the side where the [WindowControls] overlap it, so its /// leading (or trailing) piece starts [gap] clear of the controls; no /// padding where they do not reach it. /// /// The overlap depends on where [child] sits in the window, which is only /// known after layout, so it is measured after each frame that rebuilds it /// and again when its route finishes a transition (a pushed page slides in /// from off screen). class WindowControlsClearance extends StatefulWidget { const WindowControlsClearance({super.key, required this.child, this.gap = 8}); final Widget child; /// The space kept between the controls and [child]'s content. final double gap; @override State createState() => _WindowControlsClearanceState(); } class _WindowControlsClearanceState extends State { ValueListenable? _controls; Animation? _route; EdgeInsets _padding = EdgeInsets.zero; @override void didChangeDependencies() { super.didChangeDependencies(); final controls = WindowControls.of(context); if (controls != _controls) { _controls?.removeListener(_schedule); _controls = controls..addListener(_schedule); } final route = ModalRoute.of(context)?.animation; if (route != _route) { _route?.removeStatusListener(_onRouteStatus); _route = route?..addStatusListener(_onRouteStatus); } } @override void dispose() { _controls?.removeListener(_schedule); _route?.removeStatusListener(_onRouteStatus); super.dispose(); } void _onRouteStatus(AnimationStatus status) { if (!status.isAnimating) _schedule(); } void _schedule() { WidgetsBinding.instance ..addPostFrameCallback((_) => _measure()) ..ensureVisualUpdate(); } void _measure() { if (!mounted) return; final box = context.findRenderObject(); if (box is! RenderBox || !box.attached || !box.hasSize) return; final padding = _clearance(box); if (padding != _padding) setState(() => _padding = padding); } /// The padding that keeps [box]'s content clear of the controls. It is /// measured from [box]'s own bounds (the padding included), so it does not /// depend on the padding it sets. EdgeInsets _clearance(RenderBox box) { final controls = _controls?.value; if (controls == null) return EdgeInsets.zero; final bounds = box.localToGlobal(Offset.zero) & box.size; if (!controls.overlaps(bounds)) return EdgeInsets.zero; if (controls.center.dx < bounds.center.dx) { final left = controls.right + widget.gap - bounds.left; return EdgeInsets.only(left: left > 0 ? left : 0); } final right = bounds.right - (controls.left - widget.gap); return EdgeInsets.only(right: right > 0 ? right : 0); } @override Widget build(BuildContext context) { _schedule(); return Padding(padding: _padding, child: widget.child); } }