From c9c15d10f97b71bc6bf4f9cf2a4987efd9dbae3d Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 31 Aug 2021 10:52:49 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Improve=20handling=20of=20global?= =?UTF-8?q?=20hotkey=20load=20on=20startup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 8 ++++---- phpmon/Domain/Core/App.swift | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index dcf9120..7af53c8 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -647,7 +647,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 57; + CURRENT_PROJECT_VERSION = 60; DEVELOPMENT_TEAM = 8M54J5J787; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = phpmon/Info.plist; @@ -655,7 +655,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 3.4.1; + MARKETING_VERSION = 3.5.0; PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -671,7 +671,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 57; + CURRENT_PROJECT_VERSION = 60; DEVELOPMENT_TEAM = 8M54J5J787; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = phpmon/Info.plist; @@ -679,7 +679,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 3.4.1; + MARKETING_VERSION = 3.5.0; PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/phpmon/Domain/Core/App.swift b/phpmon/Domain/Core/App.swift index 07d57ec..2e62f9c 100644 --- a/phpmon/Domain/Core/App.swift +++ b/phpmon/Domain/Core/App.swift @@ -89,31 +89,34 @@ class App { On startup, the preferences should be loaded from the .plist, and we'll enable the shortcut if it is set. */ private func loadGlobalHotkey() { - let hotkey = Preferences.preferences[.globalHotkey] as! String? - if hotkey == nil { + // Make sure we can retrieve the hotkey from preferences; if we cannot, no hotkey is set + guard let hotkey = Preferences.preferences[.globalHotkey] as? String else { + print("No global hotkey loaded") return } - let keybindPref = GlobalKeybindPreference.fromJson(hotkey!) - - if (keybindPref != nil) { - self.shortcutHotkey = HotKey(keyCombo: KeyCombo( - carbonKeyCode: keybindPref!.keyCode, - carbonModifiers: keybindPref!.carbonFlags - )) - } else { + // Make sure we can parse the JSON into the desired format; if we cannot, no hotkey is set + guard let keybindPref = GlobalKeybindPreference.fromJson(hotkey) else { + print("No global hotkey loaded, could not be parsed!") self.shortcutHotkey = nil + return } + + self.shortcutHotkey = HotKey(keyCombo: KeyCombo( + carbonKeyCode: keybindPref.keyCode, + carbonModifiers: keybindPref.carbonFlags + )) } /** Sets up the action that needs to occur when the shortcut key is pressed (open the menu). */ private func setupGlobalHotkeyListener() { - guard let hotKey = self.shortcutHotkey else { + guard let hotkey = self.shortcutHotkey else { return } - hotKey.keyDownHandler = { + + hotkey.keyDownHandler = { MainMenu.shared.statusItem.button?.performClick(nil) NSApplication.shared.activate(ignoringOtherApps: true) }