From 7c37725202c1b58b65ca72f1bd6f524d13c317ae Mon Sep 17 00:00:00 2001 From: Carlos Enumo Date: Mon, 10 Jun 2024 17:35:06 +0100 Subject: [PATCH] Fix desktop click not resigning focus #190 --- Calendr.xcodeproj/project.pbxproj | 12 +++++-- Calendr/Main/AppDelegate.swift | 27 ++-------------- Calendr/Main/AppEditShortcuts.swift | 34 ++++++++++++++++++++ Calendr/Main/AppResignFocus.swift | 50 +++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 27 deletions(-) create mode 100644 Calendr/Main/AppEditShortcuts.swift create mode 100644 Calendr/Main/AppResignFocus.swift diff --git a/Calendr.xcodeproj/project.pbxproj b/Calendr.xcodeproj/project.pbxproj index 2a1702fc..fc12c53f 100644 --- a/Calendr.xcodeproj/project.pbxproj +++ b/Calendr.xcodeproj/project.pbxproj @@ -104,6 +104,8 @@ 349355B225BCB07B00957945 /* EventViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 349355B125BCB07B00957945 /* EventViewModel.swift */; }; 349E4CEF25CF48ED00A6FC18 /* VirtualTimeScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 349E4CEE25CF48ED00A6FC18 /* VirtualTimeScheduler.swift */; }; 34A4C3E925963FB200D0F949 /* NSStackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34A4C3E825963FB200D0F949 /* NSStackView.swift */; }; + 34A591E42C175BBD00DE419D /* AppEditShortcuts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34A591E32C175BBD00DE419D /* AppEditShortcuts.swift */; }; + 34A591E62C175C0F00DE419D /* AppResignFocus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34A591E52C175C0F00DE419D /* AppResignFocus.swift */; }; 34A6434D25F42375000A0F08 /* EventViewPreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34A6434C25F42375000A0F08 /* EventViewPreview.swift */; }; 34ABB9DD29A11C1B0021F3CF /* EventOptionsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34ABB9DC29A11C1B0021F3CF /* EventOptionsViewModel.swift */; }; 34ABB9E629A12B8A0021F3CF /* ReminderOptionsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34ABB9E529A12B8A0021F3CF /* ReminderOptionsViewModel.swift */; }; @@ -282,6 +284,8 @@ 34938CCB2978833800CED771 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/Localizable.strings; sourceTree = ""; }; 349E4CEE25CF48ED00A6FC18 /* VirtualTimeScheduler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VirtualTimeScheduler.swift; sourceTree = ""; }; 34A4C3E825963FB200D0F949 /* NSStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSStackView.swift; sourceTree = ""; }; + 34A591E32C175BBD00DE419D /* AppEditShortcuts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppEditShortcuts.swift; sourceTree = ""; }; + 34A591E52C175C0F00DE419D /* AppResignFocus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppResignFocus.swift; sourceTree = ""; }; 34A6434C25F42375000A0F08 /* EventViewPreview.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventViewPreview.swift; sourceTree = ""; }; 34ABB9DC29A11C1B0021F3CF /* EventOptionsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventOptionsViewModel.swift; sourceTree = ""; }; 34ABB9E529A12B8A0021F3CF /* ReminderOptionsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReminderOptionsViewModel.swift; sourceTree = ""; }; @@ -412,6 +416,8 @@ isa = PBXGroup; children = ( 347D0F9725952F89002451EC /* AppDelegate.swift */, + 34A591E32C175BBD00DE419D /* AppEditShortcuts.swift */, + 34A591E52C175C0F00DE419D /* AppResignFocus.swift */, 345FBD98293EA476005DD6DF /* Keyboard.swift */, 34F128E02597B9C9007DF31C /* MainViewController.swift */, 34427691269B5760004CFE1C /* MockMainViewController.swift */, @@ -1026,9 +1032,11 @@ 347D0FF225954920002451EC /* EventModel.swift in Sources */, 34A4C3E925963FB200D0F949 /* NSStackView.swift in Sources */, 34D3655829402D5700849457 /* NSColor.swift in Sources */, + 34A591E42C175BBD00DE419D /* AppEditShortcuts.swift in Sources */, 3453E6FF28393943002DCC3C /* ContextMenu.swift in Sources */, 34924CEE259FD064009C3450 /* DateFormatter.swift in Sources */, 3468E651284BD3D600B21EC8 /* EventLink.swift in Sources */, + 34A591E62C175C0F00DE419D /* AppResignFocus.swift in Sources */, 3470214A259DFF3000827AE7 /* CalendarServiceProvider.swift in Sources */, 3408D13E25982DB400CF1425 /* DateSelector.swift in Sources */, 348D694029ABF29C009F1882 /* AppleScript.swift in Sources */, @@ -1285,7 +1293,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 1.11.1; + MARKETING_VERSION = 1.11.2; PRODUCT_BUNDLE_IDENTIFIER = br.paker.Calendr; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Calendr/Config/Calendr-Bridging-Header.h"; @@ -1311,7 +1319,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 1.11.1; + MARKETING_VERSION = 1.11.2; PRODUCT_BUNDLE_IDENTIFIER = br.paker.Calendr; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Calendr/Config/Calendr-Bridging-Header.h"; diff --git a/Calendr/Main/AppDelegate.swift b/Calendr/Main/AppDelegate.swift index 4755714e..532e2b70 100644 --- a/Calendr/Main/AppDelegate.swift +++ b/Calendr/Main/AppDelegate.swift @@ -32,30 +32,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { notificationCenter: .default ) - setUpKeyboard() + setUpEditShortcuts() + setUpResignFocus() } - - // 🔨 This will not be visible, but it allows us to use basic commands in text fields - private func setUpKeyboard() { - - let mainMenu = NSMenu(title: "MainMenu") - let menuItem = mainMenu.addItem(withTitle: "", action: nil, keyEquivalent: "") - - let submenu = NSMenu() - submenu.addItem(withTitle: "Close Window", action: #selector(NSWindow.performClose(_:)), keyEquivalent: "w") - submenu.addItem(withTitle: "Undo", action: #selector(EditMenuActions.undo(_:)), keyEquivalent: "z") - submenu.addItem(withTitle: "Redo", action: #selector(EditMenuActions.redo(_:)), keyEquivalent: "Z") - submenu.addItem(withTitle: "Cut", action: #selector(NSText.cut(_:)), keyEquivalent: "x") - submenu.addItem(withTitle: "Copy", action: #selector(NSText.copy(_:)), keyEquivalent: "c") - submenu.addItem(withTitle: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: "v") - submenu.addItem(withTitle: "Select All", action: #selector(NSText.selectAll(_:)), keyEquivalent: "a") - - mainMenu.setSubmenu(submenu, for: menuItem) - NSApp.mainMenu = mainMenu - } -} - -@objc private protocol EditMenuActions { - func redo(_ sender: AnyObject) - func undo(_ sender: AnyObject) } diff --git a/Calendr/Main/AppEditShortcuts.swift b/Calendr/Main/AppEditShortcuts.swift new file mode 100644 index 00000000..52034a28 --- /dev/null +++ b/Calendr/Main/AppEditShortcuts.swift @@ -0,0 +1,34 @@ +// +// AppEditShortcuts.swift +// Calendr +// +// Created by Paker on 10/06/24. +// + +import AppKit + +extension AppDelegate { + + // 🔨 This will not be visible, but it allows us to use basic commands in text fields + func setUpEditShortcuts() { + + let mainMenu = NSMenu(title: "MainMenu") + let menuItem = mainMenu.addItem(withTitle: "", action: nil, keyEquivalent: "") + + let submenu = NSMenu() + submenu.addItem(withTitle: "Undo", action: #selector(EditMenuActions.undo(_:)), keyEquivalent: "z") + submenu.addItem(withTitle: "Redo", action: #selector(EditMenuActions.redo(_:)), keyEquivalent: "Z") + submenu.addItem(withTitle: "Cut", action: #selector(NSText.cut(_:)), keyEquivalent: "x") + submenu.addItem(withTitle: "Copy", action: #selector(NSText.copy(_:)), keyEquivalent: "c") + submenu.addItem(withTitle: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: "v") + submenu.addItem(withTitle: "Select All", action: #selector(NSText.selectAll(_:)), keyEquivalent: "a") + + mainMenu.setSubmenu(submenu, for: menuItem) + NSApp.mainMenu = mainMenu + } +} + +@objc private protocol EditMenuActions { + func redo(_ sender: AnyObject) + func undo(_ sender: AnyObject) +} diff --git a/Calendr/Main/AppResignFocus.swift b/Calendr/Main/AppResignFocus.swift new file mode 100644 index 00000000..d10f1ea9 --- /dev/null +++ b/Calendr/Main/AppResignFocus.swift @@ -0,0 +1,50 @@ +// +// AppResignFocus.swift +// Calendr +// +// Created by Paker on 10/06/24. +// + +import AppKit + +extension AppDelegate { + + // 🔨 Fix desktop click when user disables: + /// System Settings > Desktop & Dock > Desktop & Stage Manager > Show Items > On Desktop + func setUpResignFocus() { + + NSEvent.addGlobalMonitorForEvents( + matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown], + handler: resignFocus + ) + } + + private func resignFocus(event: NSEvent) { + guard + NSApp.keyWindow != nil, + isDesktop(event.windowNumber) + else { return } + + for window in NSApp.windows { + window.resignKey() + } + } + + private func isDesktop(_ windowNumber: Int) -> Bool { + guard + let infoList = CGWindowListCopyWindowInfo(.optionAll, kCGNullWindowID) as NSArray? + else { return false } + + return infoList.contains(where: { + guard + let info = $0 as? NSDictionary, + info[kCGWindowNumber as String] as? Int == windowNumber, + info[kCGWindowOwnerName as String] as? String == "WindowManager" + else { + return false + } + + return true + }) + } +}