From fd84a9a5fca71885fde641e576861a0a772508bf Mon Sep 17 00:00:00 2001 From: decodism Date: Tue, 1 Nov 2022 00:58:00 +0100 Subject: [PATCH] feat: play alert sound for unavailable thumbnail shortcuts --- src/logic/Application.swift | 9 ++++++++- src/logic/Window.swift | 25 +++++++++++++++++++++---- src/ui/main-window/ThumbnailView.swift | 6 +++--- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/logic/Application.swift b/src/logic/Application.swift index 4e2b7a4f..20733a34 100644 --- a/src/logic/Application.swift +++ b/src/logic/Application.swift @@ -125,9 +125,16 @@ class Application: NSObject { } } + func canBeQuit() -> Bool { + return runningApplication.bundleIdentifier != "com.apple.finder" || Preferences.finderShowsQuitMenuItem + } + func quit() { // only let power users quit Finder if they opt-in - if runningApplication.bundleIdentifier == "com.apple.finder" && !Preferences.finderShowsQuitMenuItem { return } + if !canBeQuit() { + NSSound.beep() + return + } if alreadyRequestedToQuit { runningApplication.forceTerminate() } else { diff --git a/src/logic/Window.swift b/src/logic/Window.swift index 6a68fd41..cdacfaff 100644 --- a/src/logic/Window.swift +++ b/src/logic/Window.swift @@ -101,8 +101,15 @@ class Window { } } + func canBeClosed() -> Bool { + return !isWindowlessApp + } + func close() { - if isWindowlessApp { return } + if !canBeClosed() { + NSSound.beep() + return + } BackgroundWork.accessibilityCommandsQueue.asyncWithCap { [weak self] in guard let self = self else { return } if self.isFullscreen { @@ -114,8 +121,15 @@ class Window { } } + func canBeMinDeminOrFullscreened() -> Bool { + return !isWindowlessApp && !isTabbed + } + func minDemin() { - if isWindowlessApp { return } + if !canBeMinDeminOrFullscreened() { + NSSound.beep() + return + } BackgroundWork.accessibilityCommandsQueue.asyncWithCap { [weak self] in guard let self = self else { return } if self.isFullscreen { @@ -132,7 +146,10 @@ class Window { } func toggleFullscreen() { - if isWindowlessApp { return } + if canBeMinDeminOrFullscreened() { + NSSound.beep() + return + } BackgroundWork.accessibilityCommandsQueue.asyncWithCap { [weak self] in guard let self = self else { return } self.axUiElement.setAttribute(kAXFullscreenAttribute, !self.isFullscreen) @@ -164,7 +181,7 @@ class Window { } } - // The following function was ported from https://github.com/Hammerspoon/hammerspoon/issues/370#issuecomment-545545468 + /// The following function was ported from https://github.com/Hammerspoon/hammerspoon/issues/370#issuecomment-545545468 func makeKeyWindow(_ psn: ProcessSerialNumber) -> Void { var psn_ = psn var bytes1 = [UInt8](repeating: 0, count: 0xf8) diff --git a/src/ui/main-window/ThumbnailView.swift b/src/ui/main-window/ThumbnailView.swift index fb778144..531261bc 100644 --- a/src/ui/main-window/ThumbnailView.swift +++ b/src/ui/main-window/ThumbnailView.swift @@ -78,9 +78,9 @@ class ThumbnailView: NSStackView { var yOffset = CGFloat(2 + ThumbnailView.windowsControlSize) [quitIcon, closeIcon, minimizeIcon, maximizeIcon].forEach { icon in icon.isHidden = !shouldShow || - ((window_?.isWindowlessApp ?? true) && icon.type != .quit) || - (icon.type == .quit && window_?.application.runningApplication.bundleIdentifier == "com.apple.finder" && !Preferences.finderShowsQuitMenuItem) || - ((icon.type == .miniaturize || icon.type == .fullscreen) && (window_?.isTabbed ?? true)) + (icon.type == .quit && !(window_?.application.canBeQuit() ?? true)) || + (icon.type == .close && !(window_?.canBeClosed() ?? true)) || + ((icon.type == .miniaturize || icon.type == .fullscreen) && !(window_?.canBeMinDeminOrFullscreened() ?? true)) if !icon.isHidden { icon.setFrameOrigin(NSPoint( x: xOffset,