Skip to content
Merged
36 changes: 30 additions & 6 deletions Sources/App/SettingsWindowPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ enum SettingsWindowPresenter {
private static var pendingNavigationTarget: SettingsNavigationTarget?
private static var pendingContentNavigationTarget: SettingsNavigationTarget?
private static var shouldOpenWhenConfigured = false
#if DEBUG
private static var focusHandlerForTests: (@MainActor (NSWindow) -> Void)?
#endif

static func configure(
openWindow: @escaping @MainActor () -> Void,
Expand All @@ -33,16 +36,19 @@ enum SettingsWindowPresenter {
}

static func configure(window: NSWindow) {
let shouldFocusAfterConfiguration = settingsWindow !== window
settingsWindow = window
window.identifier = NSUserInterfaceItemIdentifier(windowIdentifier)
window.isRestorable = false
window.minSize = minimumSize
window.contentMinSize = minimumSize
clampToVisibleAreaIfNeeded(window)
attachToPreferredParent(window)
Task { @MainActor in
guard settingsWindow === window else { return }
focus(window)
if shouldFocusAfterConfiguration {
Task { @MainActor in
guard settingsWindow === window else { return }
focus(window)
}
}
}

Expand All @@ -64,10 +70,13 @@ enum SettingsWindowPresenter {
pendingContentNavigationTarget = navigationTarget

if let window = existingWindow() {
pendingNavigationTarget = nil
pendingContentNavigationTarget = nil
let shouldDeferNavigation = window.isMiniaturized
if !shouldDeferNavigation {
pendingNavigationTarget = nil
pendingContentNavigationTarget = nil
}
focus(window)
if let navigationTarget {
if let navigationTarget, !shouldDeferNavigation {
SettingsNavigationRequest.post(navigationTarget)
}
return
Expand Down Expand Up @@ -115,6 +124,11 @@ enum SettingsWindowPresenter {
pendingNavigationTarget = nil
pendingContentNavigationTarget = nil
shouldOpenWhenConfigured = false
focusHandlerForTests = nil
}

static func setFocusHandlerForTests(_ handler: @escaping @MainActor (NSWindow) -> Void) {
focusHandlerForTests = handler
}
#endif

Expand All @@ -128,6 +142,16 @@ enum SettingsWindowPresenter {
}

private static func focus(_ window: NSWindow) {
#if DEBUG
if let focusHandlerForTests {
focusHandlerForTests(window)
return
}
#endif
performFocus(window)
}

private static func performFocus(_ window: NSWindow) {
if window.isMiniaturized {
window.deminiaturize(nil)
}
Expand Down
Loading
Loading