|
@@ -6,6 +6,7 @@ final class WidgetsWindowManager {
|
|
|
|
|
|
|
|
private var windowController: NSWindowController?
|
|
private var windowController: NSWindowController?
|
|
|
private let defaultWindowSize = CGSize(width: 1120, height: 760)
|
|
private let defaultWindowSize = CGSize(width: 1120, height: 760)
|
|
|
|
|
+ private var windowNotificationObservers: [NSObjectProtocol] = []
|
|
|
|
|
|
|
|
private init() {}
|
|
private init() {}
|
|
|
|
|
|
|
@@ -17,6 +18,7 @@ final class WidgetsWindowManager {
|
|
|
if let window = controller.window {
|
|
if let window = controller.window {
|
|
|
applyScreenBoundedMaxSize(to: window)
|
|
applyScreenBoundedMaxSize(to: window)
|
|
|
resizeAndCenter(window: window)
|
|
resizeAndCenter(window: window)
|
|
|
|
|
+ attachRefocusObservers(to: window)
|
|
|
}
|
|
}
|
|
|
controller.showWindow(nil)
|
|
controller.showWindow(nil)
|
|
|
controller.window?.makeKeyAndOrderFront(nil)
|
|
controller.window?.makeKeyAndOrderFront(nil)
|
|
@@ -40,6 +42,7 @@ final class WidgetsWindowManager {
|
|
|
applyScreenBoundedMaxSize(to: window)
|
|
applyScreenBoundedMaxSize(to: window)
|
|
|
window.standardWindowButton(.zoomButton)?.isHidden = true
|
|
window.standardWindowButton(.zoomButton)?.isHidden = true
|
|
|
resizeAndCenter(window: window)
|
|
resizeAndCenter(window: window)
|
|
|
|
|
+ attachRefocusObservers(to: window)
|
|
|
|
|
|
|
|
let controller = NSWindowController(window: window)
|
|
let controller = NSWindowController(window: window)
|
|
|
self.windowController = controller
|
|
self.windowController = controller
|
|
@@ -61,6 +64,34 @@ final class WidgetsWindowManager {
|
|
|
window.setFrame(frame, display: true)
|
|
window.setFrame(frame, display: true)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private func attachRefocusObservers(to window: NSWindow) {
|
|
|
|
|
+ // Ensure we don't accumulate observers across repeated opens.
|
|
|
|
|
+ for token in windowNotificationObservers {
|
|
|
|
|
+ NotificationCenter.default.removeObserver(token)
|
|
|
|
|
+ }
|
|
|
|
|
+ windowNotificationObservers.removeAll()
|
|
|
|
|
+
|
|
|
|
|
+ let willClose = NotificationCenter.default.addObserver(
|
|
|
|
|
+ forName: NSWindow.willCloseNotification,
|
|
|
|
|
+ object: window,
|
|
|
|
|
+ queue: .main
|
|
|
|
|
+ ) { [weak self] _ in
|
|
|
|
|
+ // Drop the cached controller so a new instance can be created next open.
|
|
|
|
|
+ self?.windowController = nil
|
|
|
|
|
+ NotificationCenter.default.post(name: .refocusLauncherWindowRequested, object: nil)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let didMiniaturize = NotificationCenter.default.addObserver(
|
|
|
|
|
+ forName: NSWindow.didMiniaturizeNotification,
|
|
|
|
|
+ object: window,
|
|
|
|
|
+ queue: .main
|
|
|
|
|
+ ) { _ in
|
|
|
|
|
+ NotificationCenter.default.post(name: .refocusLauncherWindowRequested, object: nil)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ windowNotificationObservers = [willClose, didMiniaturize]
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/// Keeps the Widgets window from growing past the visible screen when hosted SwiftUI reports a wide fitting size.
|
|
/// Keeps the Widgets window from growing past the visible screen when hosted SwiftUI reports a wide fitting size.
|
|
|
private func applyScreenBoundedMaxSize(to window: NSWindow) {
|
|
private func applyScreenBoundedMaxSize(to window: NSWindow) {
|
|
|
let screenFrame = (window.screen ?? NSScreen.main)?.visibleFrame ?? NSRect(x: 0, y: 0, width: 1440, height: 900)
|
|
let screenFrame = (window.screen ?? NSScreen.main)?.visibleFrame ?? NSRect(x: 0, y: 0, width: 1440, height: 900)
|