fix: update macOS traffic lights through window events

This commit is contained in:
Levi committed 2026-10-09 20:58:24 +08:00
1 parent b6ce7756fb
commit 1589d4cbe7
1 file changed
+38 -6
@@ -7,6 +7,8 @@ class MainFlutterWindow: NSWindow {
// Keep this in sync with AppDesignTokens.railWidth in Flutter. // Keep this in sync with AppDesignTokens.railWidth in Flutter.
private static let navigationRailWidth: CGFloat = 68 private static let navigationRailWidth: CGFloat = 68
private static let trafficLightGap: CGFloat = 6 private static let trafficLightGap: CGFloat = 6
private var trafficLightLayoutScheduled = false
private var fullScreenTransitionInProgress = false
override func awakeFromNib() { override func awakeFromNib() {
let flutterViewController = FlutterViewController() let flutterViewController = FlutterViewController()
@@ -22,16 +24,22 @@ class MainFlutterWindow: NSWindow {
super.awakeFromNib() super.awakeFromNib()
for name in [NSWindow.didResizeNotification, NSWindow.didBecomeKeyNotification, for name in [NSWindow.didResizeNotification, NSWindow.didBecomeKeyNotification,
NSWindow.didExitFullScreenNotification,
NSWindow.didChangeBackingPropertiesNotification] { NSWindow.didChangeBackingPropertiesNotification] {
NotificationCenter.default.addObserver( NotificationCenter.default.addObserver(
self, selector: #selector(refreshTrafficLightLayout(_:)), name: name, object: self) self, selector: #selector(refreshTrafficLightLayout(_:)), name: name, object: self)
} }
layoutIfNeeded() for name in [NSWindow.willEnterFullScreenNotification, NSWindow.willExitFullScreenNotification] {
NotificationCenter.default.addObserver(
self, selector: #selector(beginFullScreenTransition(_:)), name: name, object: self)
}
for name in [NSWindow.didEnterFullScreenNotification, NSWindow.didExitFullScreenNotification] {
NotificationCenter.default.addObserver(
self, selector: #selector(endFullScreenTransition(_:)), name: name, object: self)
}
scheduleTrafficLightLayout()
} }
override func layoutIfNeeded() { private func positionTrafficLights() {
super.layoutIfNeeded()
// Let AppKit position controls in its own full-screen title bar. // Let AppKit position controls in its own full-screen title bar.
guard !styleMask.contains(.fullScreen) else { return } guard !styleMask.contains(.fullScreen) else { return }
@@ -56,8 +64,30 @@ class MainFlutterWindow: NSWindow {
} }
@objc private func refreshTrafficLightLayout(_ notification: Notification) { @objc private func refreshTrafficLightLayout(_ notification: Notification) {
scheduleTrafficLightLayout()
}
@objc private func beginFullScreenTransition(_ notification: Notification) {
fullScreenTransitionInProgress = true
}
@objc private func endFullScreenTransition(_ notification: Notification) {
fullScreenTransitionInProgress = false
scheduleTrafficLightLayout()
}
private func scheduleTrafficLightLayout() {
guard !trafficLightLayoutScheduled else { return }
trafficLightLayoutScheduled = true
// AppKit can reset button frames while finishing a resize or style change. // AppKit can reset button frames while finishing a resize or style change.
DispatchQueue.main.async { [weak self] in self?.layoutIfNeeded() } DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.trafficLightLayoutScheduled = false
guard !self.fullScreenTransitionInProgress, !self.styleMask.contains(.fullScreen) else { return }
// Calling this is supported; overriding AppKit's layout entry point is not.
self.layoutIfNeeded()
self.positionTrafficLights()
}
} }
deinit { deinit {
@@ -67,6 +97,8 @@ class MainFlutterWindow: NSWindow {
override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) { override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) {
super.order(place, relativeTo: otherWin) super.order(place, relativeTo: otherWin)
hiddenWindowAtLaunch() hiddenWindowAtLaunch()
layoutIfNeeded() if place == .above {
scheduleTrafficLightLayout()
}
} }
} }