e00c39aa5f
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.
132 lines
4.8 KiB
Swift
132 lines
4.8 KiB
Swift
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,
|
|
]
|
|
}
|
|
}
|