diff --git a/src/logic/Preferences.swift b/src/logic/Preferences.swift index 1b687dff..d599ad68 100644 --- a/src/logic/Preferences.swift +++ b/src/logic/Preferences.swift @@ -17,8 +17,9 @@ class Preferences { "previousWindowShortcut": "⇧⇥", "cancelShortcut": "⎋", "closeWindowShortcut": "W", - "quitAppShortcut": "Q", "minDeminWindowShortcut": "M", + "quitAppShortcut": "Q", + "hideShowAppShortcut": "H", "arrowKeysEnabled": true, "mouseHoverEnabled": true, "showMinimizedWindows": true, @@ -55,8 +56,9 @@ class Preferences { static var previousWindowShortcut: String { defaults.string(forKey: "previousWindowShortcut")! } static var cancelShortcut: String { defaults.string(forKey: "cancelShortcut")! } static var closeWindowShortcut: String { defaults.string(forKey: "closeWindowShortcut")! } - static var quitAppShortcut: String { defaults.string(forKey: "quitAppShortcut")! } static var minDeminWindowShortcut: String { defaults.string(forKey: "minDeminWindowShortcut")! } + static var quitAppShortcut: String { defaults.string(forKey: "quitAppShortcut")! } + static var hideShowAppShortcut: String { defaults.string(forKey: "hideShowAppShortcut")! } static var arrowKeysEnabled: Bool { defaults.bool(forKey: "arrowKeysEnabled") } static var mouseHoverEnabled: Bool { defaults.bool(forKey: "mouseHoverEnabled") } static var showMinimizedWindows: Bool { defaults.bool(forKey: "showMinimizedWindows") } diff --git a/src/logic/Window.swift b/src/logic/Window.swift index a8804cb0..abfe2adb 100644 --- a/src/logic/Window.swift +++ b/src/logic/Window.swift @@ -72,15 +72,26 @@ class Window { } } + func minDemin() { + DispatchQueues.accessibilityCommands.async { [weak self] in + self?.axUiElement.minDeminWindow() + } + } + func quitApp() { DispatchQueues.accessibilityCommands.async { [weak self] in self?.application.runningApplication.terminate() } } - func minDemin() { + func hideShowApp() { DispatchQueues.accessibilityCommands.async { [weak self] in - self?.axUiElement.minDeminWindow() + guard let self = self else { return } + if self.application.runningApplication.isHidden { + self.application.runningApplication.unhide() + } else { + self.application.runningApplication.hide() + } } } diff --git a/src/ui/App.swift b/src/ui/App.swift index 3d861d96..9e822d1e 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -100,12 +100,16 @@ class App: NSApplication, NSApplicationDelegate { Windows.focusedWindow()?.close() } + func minDeminSelectedWindow() { + Windows.focusedWindow()?.minDemin() + } + func quitSelectedApp() { Windows.focusedWindow()?.quitApp() } - func minDeminSelectedWindow() { - Windows.focusedWindow()?.minDemin() + func hideShowSelectedApp() { + Windows.focusedWindow()?.hideShowApp() } func focusTarget() { diff --git a/src/ui/preferences-window/tabs/GeneralTab.swift b/src/ui/preferences-window/tabs/GeneralTab.swift index 1cd3ef70..26fc4ba4 100644 --- a/src/ui/preferences-window/tabs/GeneralTab.swift +++ b/src/ui/preferences-window/tabs/GeneralTab.swift @@ -18,8 +18,9 @@ class GeneralTab: NSViewController, PreferencePane { let previousWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Select previous window", comment: ""), "previousWindowShortcut", Preferences.previousWindowShortcut, labelPosition: .right) let cancelShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Cancel and hide", comment: ""), "cancelShortcut", Preferences.cancelShortcut, labelPosition: .right) let closeWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Close window", comment: ""), "closeWindowShortcut", Preferences.closeWindowShortcut, labelPosition: .right) - let quitAppShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Quit app", comment: ""), "quitAppShortcut", Preferences.quitAppShortcut, labelPosition: .right) let minDeminWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Minimize/Deminimize window", comment: ""), "minDeminWindowShortcut", Preferences.minDeminWindowShortcut, labelPosition: .right) + let quitAppShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Quit app", comment: ""), "quitAppShortcut", Preferences.quitAppShortcut, labelPosition: .right) + let hideShowAppShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Hide/Show app", comment: ""), "hideShowAppShortcut", Preferences.hideShowAppShortcut, labelPosition: .right) let enableArrows = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Arrow keys", comment: ""), "arrowKeysEnabled", extraAction: GeneralTab.arrowKeysEnabledCallback, labelPosition: .right) let enableMouse = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Mouse hover", comment: ""), "mouseHoverEnabled", labelPosition: .right) let holdAndPress = StackView(holdShortcut) @@ -30,7 +31,7 @@ class GeneralTab: NSViewController, PreferencePane { let screensToShow = dropdown("screensToShow", ScreensToShowPreference.allCases) let showMinimizedWindows = StackView(LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Minimized", comment: ""), "showMinimizedWindows", labelPosition: .right)) let showHiddenWindows = StackView(LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Hidden", comment: ""), "showHiddenWindows", labelPosition: .right)) - let shortcuts = StackView([nextWindowShortcut, previousWindowShortcut, cancelShortcut, closeWindowShortcut, quitAppShortcut, minDeminWindowShortcut].map { (view: [NSView]) in StackView(view) }, .vertical) + let shortcuts = StackView([nextWindowShortcut, previousWindowShortcut, cancelShortcut, closeWindowShortcut, minDeminWindowShortcut, quitAppShortcut, hideShowAppShortcut].map { (view: [NSView]) in StackView(view) }, .vertical) let toShowDropdowns = StackView([appsToShow, spacesToShow, screensToShow], .vertical) let toShowCheckboxes = StackView([showMinimizedWindows, showHiddenWindows], .vertical) let toShowExplanations = LabelAndControl.makeLabel(NSLocalizedString("Show the following windows:", comment: "")) @@ -47,7 +48,7 @@ class GeneralTab: NSViewController, PreferencePane { grid.fit() GeneralTab.shortcutsDependentOnHoldShortcut.append(contentsOf: [enableArrows[0] as! NSControl] + - [nextWindowShortcut, previousWindowShortcut, cancelShortcut, closeWindowShortcut, quitAppShortcut, minDeminWindowShortcut].map { $0[0] as! NSControl }) + [nextWindowShortcut, previousWindowShortcut, cancelShortcut, closeWindowShortcut, minDeminWindowShortcut, quitAppShortcut, hideShowAppShortcut].map { $0[0] as! NSControl }) GeneralTab.arrowKeysEnabledCallback(enableArrows[0] as! NSControl) startAtLoginCallback(startAtLogin[1] as! NSControl) @@ -63,7 +64,7 @@ class GeneralTab: NSViewController, PreferencePane { removeShortcutIfExists(controlId, type) // remove the previous shortcut shortcutActions[controlId] = ShortcutAction(shortcut: shortcut, actionHandler: { _ in let isShortcutInitiatingTheApp = ["previousWindowShortcut", "nextWindowShortcut"].contains(controlId) - let isShortcutWithUiShown = ["←", "→", "closeWindowShortcut", "quitAppShortcut", "minDeminWindowShortcut"].contains(controlId) + let isShortcutWithUiShown = ["←", "→", "closeWindowShortcut", "minDeminWindowShortcut", "quitAppShortcut", "hideShowAppShortcut"].contains(controlId) App.app.uiWorkShouldBeDone = isShortcutInitiatingTheApp || isShortcutWithUiShown if isShortcutInitiatingTheApp { App.app.appIsBeingUsed = true @@ -102,10 +103,12 @@ class GeneralTab: NSViewController, PreferencePane { return { App.app.hideUi() } } else if controlId == "closeWindowShortcut" { return { App.app.closeSelectedWindow() } - } else if controlId == "quitAppShortcut" { - return { App.app.quitSelectedApp() } } else if controlId == "minDeminWindowShortcut" { return { App.app.minDeminSelectedWindow() } + } else if controlId == "quitAppShortcut" { + return { App.app.quitSelectedApp() } + } else if controlId == "hideShowAppShortcut" { + return { App.app.hideShowSelectedApp() } } return {} }