Skip to content

Commit

Permalink
switched from HotKey to KeyboardShortcuts, global shortcut now modifi…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
antingle committed May 6, 2022
1 parent 09c795b commit 6136d86
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 34 deletions.
34 changes: 17 additions & 17 deletions Calculator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -52,8 +52,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
92E70FD82821C08E008FF2F5 /* HotKey in Frameworks */,
92B01F4D280942500069F3D7 /* Expression in Frameworks */,
9298D4AB2825055200EC852D /* KeyboardShortcuts in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -150,7 +150,7 @@
name = Calculator;
packageProductDependencies = (
92B01F4C280942500069F3D7 /* Expression */,
92E70FD72821C08E008FF2F5 /* HotKey */,
9298D4AA2825055200EC852D /* KeyboardShortcuts */,
);
productName = Calculator;
productReference = 92F4476828089FEE00EC45D5 /* Calculator.app */;
Expand Down Expand Up @@ -182,7 +182,7 @@
mainGroup = 92F4475F28089FEE00EC45D5;
packageReferences = (
92B01F4B280942500069F3D7 /* XCRemoteSwiftPackageReference "Expression" */,
92E70FD62821C08E008FF2F5 /* XCRemoteSwiftPackageReference "HotKey" */,
9298D4A92825055200EC852D /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */,
);
productRefGroup = 92F4476928089FEE00EC45D5 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -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 */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
],
Expand Down
6 changes: 6 additions & 0 deletions Calculator/Helpers/HelperFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import SwiftUI
import Expression
import KeyboardShortcuts

func evaluateExpression(_ givenExpression: String) throws -> Double {
var solution: Double = 0
Expand Down Expand Up @@ -40,3 +41,8 @@ extension Double {
return String(formatter.string(from: number) ?? "")
}
}

// global shortcut
extension KeyboardShortcuts.Name {
static let togglePopover = Self("togglePopover")
}
8 changes: 0 additions & 8 deletions Calculator/Main/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
//

import SwiftUI
import HotKey

// Using AppDelegate because it is needed for NSPopover
class AppDelegate: NSObject, NSApplicationDelegate {
static var shared : AppDelegate!
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() {
Expand Down Expand Up @@ -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?) {
Expand Down
21 changes: 18 additions & 3 deletions Calculator/Main/CalculatorApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
}
}
1 change: 0 additions & 1 deletion Calculator/Views/CustomMacTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ extension CustomMacTextView {
}

self.parent.text = textView.string
self.parent.onSubmit()
}

// handles commands
Expand Down
2 changes: 1 addition & 1 deletion Calculator/Views/InfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Calculator/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

import SwiftUI
import AudioToolbox
import KeyboardShortcuts

struct SettingsView: View {
@EnvironmentObject private var historyStore: HistoryStore
@AppStorage("showingButtons") private var showingButtons = true

var body: some View {
VStack(alignment: .leading, spacing: 10) {
KeyboardShortcuts.Recorder(for: .togglePopover)

// MARK: - Toggle Buttons
Toggle(isOn: $showingButtons, label: {
Expand Down

0 comments on commit 6136d86

Please sign in to comment.