-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix desktop click not resigning focus #190
- Loading branch information
1 parent
c230659
commit 7c37725
Showing
4 changed files
with
96 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}) | ||
} | ||
} |