Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit

Permalink
fix window activation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx committed May 4, 2023
1 parent 22fb0b4 commit 4727f66
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions DuckDuckGo/Main/View/MainWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

import Cocoa
import Combine
import Common

@MainActor
final class MainWindowController: NSWindowController {

private static let windowFrameSaveName = "MainWindow"
private var fireViewModel: FireViewModel
private static var knownFullScreenMouseDetectionWindows = Set<NSValue>()

var mainViewController: MainViewController {
// swiftlint:disable force_cast
Expand Down Expand Up @@ -210,13 +212,43 @@ extension MainWindowController: NSWindowDelegate {
func windowDidEnterFullScreen(_ notification: Notification) {
// fix NSToolbarFullScreenWindow occurring beneath the MainWindow
// https://app.asana.com/0/1177771139624306/1203853030672990/f
if !NSApp.isActive {
for window in NSApp.windows {
guard window.className.contains("FullScreen"),
window.parent == nil,
window.screen == self.window!.screen else { continue }
// NSApp should be active at the moment of window ordering otherwise toolbar would disappear on activation
for window in NSApp.windows {
let windowValue = NSValue(nonretainedObject: window)

guard window.className.contains("NSFullScreenMouseDetectionWindow"),
!Self.knownFullScreenMouseDetectionWindows.contains(windowValue),
window.screen == self.window!.screen else { continue }

// keep record of NSFullScreenMouseDetectionWindow to avoid adding other‘s windows
Self.knownFullScreenMouseDetectionWindows.insert(windowValue)
window.onDeinit {
Self.knownFullScreenMouseDetectionWindows.remove(windowValue)
}

// add NSFullScreenMouseDetectionWindow as a child window to activate the app without revealing all of its windows
let activeApp = NSWorkspace.shared.frontmostApplication
if activeApp != .current {
self.window!.addChildWindow(window, ordered: .above)
}

// remove the child window and reactivate initially active app as soon as current app becomes active
// otherwise the fullscreen will reactivate its Space when switching to window in another Space
var cancellable: AnyCancellable!
cancellable = NSApp.isActivePublisher().dropFirst().sink { [weak self, weak window] _ in
withExtendedLifetime(cancellable) {
if let activeApp, activeApp != .current {
activeApp.activate()
}

if let self, let window, self.window?.childWindows?.contains(window) == true {
self.window?.removeChildWindow(window)
}
cancellable = nil
}
}

break
}
}

Expand Down

0 comments on commit 4727f66

Please sign in to comment.