Top bar: clear iPadOS 26 window controls in a window

Flutter's safe area leaves out the window controls of a windowed iPad app,
so they covered the list pane's title pill and took its taps. A small Runner
plugin reports their corner from UIKit's corner-adapted safe-area regions
over a method channel, and SegmentedBar pads the side they overlap. No-op
off iOS and when there are no controls.
This commit is contained in:
2026-11-03 18:10:33 +00:00
parent fb3987ef4e
commit e00c39aa5f
14 changed files with 589 additions and 4 deletions
+4
View File
@@ -12,6 +12,7 @@
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
7884E8682EC3CC0700C636F2 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7884E8672EC3CC0400C636F2 /* SceneDelegate.swift */; };
7884E86A2EC3CC0700C636F2 /* WindowControlsPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7884E8692EC3CC0400C636F2 /* WindowControlsPlugin.swift */; };
78A318202AECB46A00862997 /* FlutterGeneratedPluginSwiftPackage in Frameworks */ = {isa = PBXBuildFile; productRef = 78A3181F2AECB46A00862997 /* FlutterGeneratedPluginSwiftPackage */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
@@ -50,6 +51,7 @@
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7884E8672EC3CC0400C636F2 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
7884E8692EC3CC0400C636F2 /* WindowControlsPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowControlsPlugin.swift; sourceTree = "<group>"; };
78E0A7A72DC9AD7400C4905E /* FlutterGeneratedPluginSwiftPackage */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = FlutterGeneratedPluginSwiftPackage; path = Flutter/ephemeral/Packages/FlutterGeneratedPluginSwiftPackage; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = "<group>"; };
@@ -123,6 +125,7 @@
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */,
74858FAE1ED2DC5600515810 /* AppDelegate.swift */,
7884E8672EC3CC0400C636F2 /* SceneDelegate.swift */,
7884E8692EC3CC0400C636F2 /* WindowControlsPlugin.swift */,
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */,
);
path = Runner;
@@ -284,6 +287,7 @@
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */,
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */,
7884E8682EC3CC0700C636F2 /* SceneDelegate.swift in Sources */,
7884E86A2EC3CC0700C636F2 /* WindowControlsPlugin.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
+3
View File
@@ -12,5 +12,8 @@ import UIKit
func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) {
GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
if let registrar = engineBridge.pluginRegistry.registrar(forPlugin: "WindowControlsPlugin") {
WindowControlsPlugin.register(with: registrar)
}
}
}
+131
View File
@@ -0,0 +1,131 @@
import Flutter
import UIKit
/// Reports where iPadOS 26's window controls (the close/minimise/zoom
/// buttons of a windowed app) cover the Flutter view, so the app's top bar
/// can keep its leading pieces clear of them.
///
/// Flutter's view padding is UIKit's plain safe area, which does not include
/// the controls. iOS 26 exposes them through corner-adapted layout regions:
/// the safe area adapted horizontally ends beside the controls, the one
/// adapted vertically ends below them (in a window on the iPadOS 26.4
/// simulator: safe area top 10, adapted left 66 and top 53). The rect is sent
/// in the view's points (Flutter's logical pixels) as
/// `{left, top, right, bottom}`, or `nil` when there are none (full screen
/// in portrait, iOS before 26). UIKit reports nothing for an app it scales
/// into a compatibility window (`UIRequiresFullScreen`).
///
/// Dart calls `start` (answered with the current rect) and then receives
/// `changed` whenever it moves, e.g. as the window is resized or leaves or
/// enters full screen. See lib/ui/adaptive/window_controls.dart.
final class WindowControlsPlugin: NSObject {
static let channelName = "pipelines/window_controls"
private let channel: FlutterMethodChannel
private weak var registrar: FlutterPluginRegistrar?
private var probe: WindowControlsProbe?
private var last: [String: Double]?
static func register(with registrar: FlutterPluginRegistrar) {
let instance = WindowControlsPlugin(registrar: registrar)
instance.channel.setMethodCallHandler { [weak instance] call, result in
instance?.handle(call, result: result)
}
// The registrar does not keep the instance; the channel handler holds it
// weakly, so the registry does.
registrar.publish(instance)
}
private init(registrar: FlutterPluginRegistrar) {
self.registrar = registrar
channel = FlutterMethodChannel(
name: Self.channelName, binaryMessenger: registrar.messenger())
}
private func handle(_ call: FlutterMethodCall, result: FlutterResult) {
guard call.method == "start" else {
result(FlutterMethodNotImplemented)
return
}
attachProbe()
last = probe?.controlsRect()
result(last)
}
/// Adds an invisible view filling the Flutter view; UIKit lays it out (and
/// updates its safe area) whenever the window's size or controls change.
/// Until the engine has its view controller, it tries again shortly.
private func attachProbe(attempts: Int = 20) {
guard probe == nil else { return }
guard let view = registrar?.viewController?.view else {
if attempts > 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { [weak self] in
self?.attachProbe(attempts: attempts - 1)
self?.report()
}
}
return
}
let probe = WindowControlsProbe(frame: view.bounds)
probe.onChange = { [weak self] in self?.report() }
view.addSubview(probe)
self.probe = probe
}
private func report() {
let rect = probe?.controlsRect()
guard rect != last else { return }
last = rect
channel.invokeMethod("changed", arguments: rect)
}
}
/// A transparent, untouchable view that fills its superview and tells its
/// owner when its layout or safe area changes.
private final class WindowControlsProbe: UIView {
var onChange: (() -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
autoresizingMask = [.flexibleWidth, .flexibleHeight]
isUserInteractionEnabled = false
isAccessibilityElement = false
backgroundColor = .clear
}
required init?(coder: NSCoder) { fatalError("init(coder:) is not used") }
override func layoutSubviews() {
super.layoutSubviews()
onChange?()
}
override func safeAreaInsetsDidChange() {
super.safeAreaInsetsDidChange()
onChange?()
}
/// The corner the window controls cover, or nil when there are none.
///
/// The horizontally adapted safe area also steps in a few points for a
/// rounded display or window corner, on both sides and at full screen;
/// only the controls also push the vertically adapted top below the plain
/// safe area, so both must grow. They sit on the side that grows most.
func controlsRect() -> [String: Double]? {
guard #available(iOS 26.0, *) else { return nil }
let safe = safeAreaInsets
let beside = edgeInsets(for: .safeArea(cornerAdaptation: .horizontal))
let below = edgeInsets(for: .safeArea(cornerAdaptation: .vertical))
let left = beside.left - safe.left
let right = beside.right - safe.right
guard below.top > safe.top, max(left, right) > 0 else { return nil }
let width = Double(bounds.width)
let bottom = Double(below.top)
if left >= right {
return ["left": 0, "top": 0, "right": Double(beside.left), "bottom": bottom]
}
return [
"left": width - Double(beside.right), "top": 0, "right": width, "bottom": bottom,
]
}
}