diff --git a/Calculator.xcodeproj/project.pbxproj b/Calculator.xcodeproj/project.pbxproj index 21ee09c..05359aa 100644 --- a/Calculator.xcodeproj/project.pbxproj +++ b/Calculator.xcodeproj/project.pbxproj @@ -14,10 +14,10 @@ 928D96C6280A8F9300B6CE2E /* ButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 928D96C5280A8F9300B6CE2E /* ButtonView.swift */; }; 928D96C8280AA58D00B6CE2E /* CalculatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 928D96C7280AA58D00B6CE2E /* CalculatorView.swift */; }; 928D96CA280AA5E400B6CE2E /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 928D96C9280AA5E400B6CE2E /* SettingsView.swift */; }; + 9298D4AB2825055200EC852D /* KeyboardShortcuts in Frameworks */ = {isa = PBXBuildFile; productRef = 9298D4AA2825055200EC852D /* KeyboardShortcuts */; }; 92AA9002281EEBEA008C9EC8 /* HistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92AA9001281EEBEA008C9EC8 /* HistoryView.swift */; }; 92AA9004281F802C008C9EC8 /* History.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92AA9003281F802C008C9EC8 /* History.swift */; }; 92B01F4D280942500069F3D7 /* Expression in Frameworks */ = {isa = PBXBuildFile; productRef = 92B01F4C280942500069F3D7 /* Expression */; }; - 92E70FD82821C08E008FF2F5 /* HotKey in Frameworks */ = {isa = PBXBuildFile; productRef = 92E70FD72821C08E008FF2F5 /* HotKey */; }; 92F4476C28089FEE00EC45D5 /* CalculatorApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92F4476B28089FEE00EC45D5 /* CalculatorApp.swift */; }; 92F4476E28089FEE00EC45D5 /* PopoverView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92F4476D28089FEE00EC45D5 /* PopoverView.swift */; }; 92F4477028089FF100EC45D5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 92F4476F28089FF100EC45D5 /* Assets.xcassets */; }; @@ -52,8 +52,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 92E70FD82821C08E008FF2F5 /* HotKey in Frameworks */, 92B01F4D280942500069F3D7 /* Expression in Frameworks */, + 9298D4AB2825055200EC852D /* KeyboardShortcuts in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -150,7 +150,7 @@ name = Calculator; packageProductDependencies = ( 92B01F4C280942500069F3D7 /* Expression */, - 92E70FD72821C08E008FF2F5 /* HotKey */, + 9298D4AA2825055200EC852D /* KeyboardShortcuts */, ); productName = Calculator; productReference = 92F4476828089FEE00EC45D5 /* Calculator.app */; @@ -182,7 +182,7 @@ mainGroup = 92F4475F28089FEE00EC45D5; packageReferences = ( 92B01F4B280942500069F3D7 /* XCRemoteSwiftPackageReference "Expression" */, - 92E70FD62821C08E008FF2F5 /* XCRemoteSwiftPackageReference "HotKey" */, + 9298D4A92825055200EC852D /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */, ); productRefGroup = 92F4476928089FEE00EC45D5 /* Products */; projectDirPath = ""; @@ -429,35 +429,35 @@ /* End XCConfigurationList section */ /* Begin XCRemoteSwiftPackageReference section */ - 92B01F4B280942500069F3D7 /* XCRemoteSwiftPackageReference "Expression" */ = { + 9298D4A92825055200EC852D /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */ = { isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/nicklockwood/Expression.git"; + repositoryURL = "https://github.com/sindresorhus/KeyboardShortcuts"; requirement = { - kind = upToNextMajorVersion; - minimumVersion = 0.9.3; + branch = main; + kind = branch; }; }; - 92E70FD62821C08E008FF2F5 /* XCRemoteSwiftPackageReference "HotKey" */ = { + 92B01F4B280942500069F3D7 /* XCRemoteSwiftPackageReference "Expression" */ = { isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/soffes/HotKey"; + repositoryURL = "https://github.com/nicklockwood/Expression.git"; requirement = { - branch = master; - kind = branch; + kind = upToNextMajorVersion; + minimumVersion = 0.9.3; }; }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ + 9298D4AA2825055200EC852D /* KeyboardShortcuts */ = { + isa = XCSwiftPackageProductDependency; + package = 9298D4A92825055200EC852D /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */; + productName = KeyboardShortcuts; + }; 92B01F4C280942500069F3D7 /* Expression */ = { isa = XCSwiftPackageProductDependency; package = 92B01F4B280942500069F3D7 /* XCRemoteSwiftPackageReference "Expression" */; productName = Expression; }; - 92E70FD72821C08E008FF2F5 /* HotKey */ = { - isa = XCSwiftPackageProductDependency; - package = 92E70FD62821C08E008FF2F5 /* XCRemoteSwiftPackageReference "HotKey" */; - productName = HotKey; - }; /* End XCSwiftPackageProductDependency section */ }; rootObject = 92F4476028089FEE00EC45D5 /* Project object */; diff --git a/Calculator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Calculator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index d1dd597..fbc8984 100644 --- a/Calculator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Calculator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -10,12 +10,12 @@ } }, { - "identity" : "hotkey", + "identity" : "keyboardshortcuts", "kind" : "remoteSourceControl", - "location" : "https://github.com/soffes/HotKey", + "location" : "https://github.com/sindresorhus/KeyboardShortcuts", "state" : { - "branch" : "master", - "revision" : "c13662730cb5bc28de4a799854bbb018a90649bf" + "branch" : "main", + "revision" : "5e453238c13a42a3448f1effc53c4a7a4cd22c15" } } ], diff --git a/Calculator/Helpers/HelperFunctions.swift b/Calculator/Helpers/HelperFunctions.swift index 4145bd2..a3d780a 100644 --- a/Calculator/Helpers/HelperFunctions.swift +++ b/Calculator/Helpers/HelperFunctions.swift @@ -7,6 +7,7 @@ import SwiftUI import Expression +import KeyboardShortcuts func evaluateExpression(_ givenExpression: String) throws -> Double { var solution: Double = 0 @@ -40,3 +41,8 @@ extension Double { return String(formatter.string(from: number) ?? "") } } + +// global shortcut +extension KeyboardShortcuts.Name { + static let togglePopover = Self("togglePopover") +} diff --git a/Calculator/Main/AppDelegate.swift b/Calculator/Main/AppDelegate.swift index 98ee5a3..9806a6e 100644 --- a/Calculator/Main/AppDelegate.swift +++ b/Calculator/Main/AppDelegate.swift @@ -6,7 +6,6 @@ // import SwiftUI -import HotKey // Using AppDelegate because it is needed for NSPopover class AppDelegate: NSObject, NSApplicationDelegate { @@ -14,7 +13,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { private var popover = NSPopover.init() private var statusBarItem: NSStatusItem? private var window: NSWindow! - private let hotKey = HotKey(key: .c, modifiers: [.command, .option]) // global open shortcut private var historyStore = HistoryStore() // Environment Object for History @objc func openCalculatorWindow() { @@ -63,12 +61,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) statusBarItem?.button?.image = NSImage(systemSymbolName: "function", accessibilityDescription: "calculator") statusBarItem?.button?.action = #selector(AppDelegate.togglePopover(_:)) - - // enables global shortcut - hotKey.keyDownHandler = { - self.togglePopover(self) - } - } @objc func showPopover(_ sender: AnyObject?) { diff --git a/Calculator/Main/CalculatorApp.swift b/Calculator/Main/CalculatorApp.swift index 39a2c87..f1edeb0 100644 --- a/Calculator/Main/CalculatorApp.swift +++ b/Calculator/Main/CalculatorApp.swift @@ -6,14 +6,16 @@ // import SwiftUI +import KeyboardShortcuts @main struct CalculatorApp: App { - // MARK TODO: This Environment object is different than what is created in the AppDelegate - // however, the first window in WindowGroup is closed right away. Hacky solution... + // TODO: This Environment object is different than what is created in the AppDelegate + // but this object is never used because the window is closed right away in AppDelegate @StateObject private var historyStore = HistoryStore() - + @StateObject private var appState = AppState() @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate + init() { AppDelegate.shared = self.appDelegate } @@ -27,3 +29,16 @@ struct CalculatorApp: App { } } } + +// Allows toggling of the global shortcut +// NOTE: In the KeyboardShortcut docs, this class was @MainActor, but do we need it? (causes warnings) +// @MainActor +final class AppState: ObservableObject { + @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate + + init() { + KeyboardShortcuts.onKeyDown(for: .togglePopover) { [self] in + appDelegate.togglePopover(AppDelegate.self) + } + } +} diff --git a/Calculator/Views/CustomMacTextView.swift b/Calculator/Views/CustomMacTextView.swift index b861fcb..bc19d88 100644 --- a/Calculator/Views/CustomMacTextView.swift +++ b/Calculator/Views/CustomMacTextView.swift @@ -98,7 +98,6 @@ extension CustomMacTextView { } self.parent.text = textView.string - self.parent.onSubmit() } // handles commands diff --git a/Calculator/Views/InfoView.swift b/Calculator/Views/InfoView.swift index deb3406..ab28fa3 100644 --- a/Calculator/Views/InfoView.swift +++ b/Calculator/Views/InfoView.swift @@ -14,7 +14,7 @@ struct InfoView: View { Text(""" **Welcome to Menu Bar Calc!** - - Global shortcut ⌘⌥C + - Global shortcut is ⌘⌥C, but it is changeable - Add previous solutions with arrow keys - Feel free to give feedback [here](https://github.com/ingleanthony/menu-bar-calculator/discussionsr) diff --git a/Calculator/Views/SettingsView.swift b/Calculator/Views/SettingsView.swift index c4cb598..6bc388b 100644 --- a/Calculator/Views/SettingsView.swift +++ b/Calculator/Views/SettingsView.swift @@ -7,6 +7,7 @@ import SwiftUI import AudioToolbox +import KeyboardShortcuts struct SettingsView: View { @EnvironmentObject private var historyStore: HistoryStore @@ -14,6 +15,7 @@ struct SettingsView: View { var body: some View { VStack(alignment: .leading, spacing: 10) { + KeyboardShortcuts.Recorder(for: .togglePopover) // MARK: - Toggle Buttons Toggle(isOn: $showingButtons, label: {