diff --git a/src/logic/Application.swift b/src/logic/Application.swift index 6d531943..288917c4 100644 --- a/src/logic/Application.swift +++ b/src/logic/Application.swift @@ -14,6 +14,7 @@ class Application: NSObject { var dockLabel: String? var pid: pid_t! var focusedWindow: Window? = nil + var alreadyRequestedToQuit = false init(_ runningApplication: NSRunningApplication) { self.runningApplication = runningApplication @@ -103,6 +104,37 @@ class Application: NSObject { } } + func addWindowslessAppsIfNeeded() -> [Window]? { + if !Preferences.hideWindowlessApps && + runningApplication.activationPolicy == .regular && + !runningApplication.isTerminated && + (Windows.list.firstIndex { $0.application.pid == pid }) == nil { + let window = Window(self) + Windows.appendAndUpdateFocus(window) + return [window] + } + return nil + } + + func hideOrShow() { + if runningApplication.isHidden { + runningApplication.unhide() + } else { + runningApplication.hide() + } + } + + func quit() { + // only let power users quit Finder if they opt-in + if runningApplication.bundleIdentifier == "com.apple.finder" && !Preferences.finderShowsQuitMenuItem { return } + if alreadyRequestedToQuit { + runningApplication.forceTerminate() + } else { + runningApplication.terminate() + alreadyRequestedToQuit = true + } + } + private func addWindows(_ axWindows: [(AXUIElement, CGWindowID, String?, Bool, Bool, CGPoint?, CGSize?)]) -> [Window] { let windows: [Window] = axWindows.compactMap { (axUiElement, wid, axTitle, isFullscreen, isMinimized, position, size) in if (Windows.list.firstIndex { $0.isEqualRobust(axUiElement, wid) }) == nil { @@ -118,18 +150,6 @@ class Application: NSObject { return windows } - func addWindowslessAppsIfNeeded() -> [Window]? { - if !Preferences.hideWindowlessApps && - runningApplication.activationPolicy == .regular && - !runningApplication.isTerminated && - (Windows.list.firstIndex { $0.application.pid == pid }) == nil { - let window = Window(self) - Windows.appendAndUpdateFocus(window) - return [window] - } - return nil - } - private func observeEvents() { guard let axObserver = axObserver else { return } for notification in [ diff --git a/src/logic/Window.swift b/src/logic/Window.swift index aa5c3cd0..024e8cd0 100644 --- a/src/logic/Window.swift +++ b/src/logic/Window.swift @@ -138,25 +138,6 @@ class Window { } } - func quitApp() { - // prevent users from quitting Finder - if application.runningApplication.bundleIdentifier == "com.apple.finder" && !Preferences.finderShowsQuitMenuItem { return } - BackgroundWork.accessibilityCommandsQueue.asyncWithCap { [weak self] in - self?.application.runningApplication.terminate() - } - } - - func hideShowApp() { - BackgroundWork.accessibilityCommandsQueue.asyncWithCap { [weak self] in - guard let self = self else { return } - if self.application.runningApplication.isHidden { - self.application.runningApplication.unhide() - } else { - self.application.runningApplication.hide() - } - } - } - func focus() { if isWindowlessApp || cgWindowId == nil { if let bundleID = application.runningApplication.bundleIdentifier { diff --git a/src/ui/App.swift b/src/ui/App.swift index fe0fd897..85aba063 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -133,11 +133,11 @@ class App: AppCenterApplication, NSApplicationDelegate { } func quitSelectedApp() { - Windows.focusedWindow()?.quitApp() + Windows.focusedWindow()?.application.quit() } func hideShowSelectedApp() { - Windows.focusedWindow()?.hideShowApp() + Windows.focusedWindow()?.application.hideOrShow() } func focusTarget() {