Skip to content

Commit

Permalink
Migrate from MASShortcut to KeyboardShortcuts (#1549)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianyh authored Dec 14, 2024
1 parent dc6bf0f commit da914e2
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 93 deletions.
109 changes: 50 additions & 59 deletions Amethyst.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"pins" : [
{
"identity" : "cartography",
"kind" : "remoteSourceControl",
"location" : "https://github.com/robb/Cartography",
"state" : {
"revision" : "b75197ea134f42b5feafb04b526b37eb1a41034b",
"version" : "4.0.0"
}
},
{
"identity" : "cwlcatchexception",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlCatchException.git",
"state" : {
"revision" : "3b123999de19bf04905bc1dfdb76f817b0f2cc00",
"version" : "2.1.2"
}
},
{
"identity" : "cwlpreconditiontesting",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state" : {
"revision" : "a23ded2c91df9156628a6996ab4f347526f17b6b",
"version" : "2.1.2"
}
},
{
"identity" : "keyboardshortcuts",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sindresorhus/KeyboardShortcuts",
"state" : {
"revision" : "ac302e21da5883f4bd0490cbd0cb710b08740500",
"version" : "1.15.0"
}
},
{
"identity" : "loginservicekit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Clipy/LoginServiceKit",
"state" : {
"branch" : "master",
"revision" : "a8e68051aca8bbb702e62ab36006a301966ab053"
}
},
{
"identity" : "masshortcut",
"kind" : "remoteSourceControl",
"location" : "https://github.com/shpakovski/MASShortcut",
"state" : {
"branch" : "master",
"revision" : "6f2603c6b6cc18f64a799e5d2c9d3bbc467c413a"
}
},
{
"identity" : "nimble",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Nimble",
"state" : {
"revision" : "eb5e3d717224fa0d1f6aff3fc2c5e8e81fa1f728",
"version" : "11.2.2"
}
},
{
"identity" : "quick",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Quick",
"state" : {
"revision" : "16910e406be96e08923918315388c3e989deac9e",
"version" : "6.1.0"
}
},
{
"identity" : "rxswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ReactiveX/RxSwift",
"state" : {
"revision" : "9dcaa4b333db437b0fbfaf453fad29069044a8b4",
"version" : "6.6.0"
}
},
{
"identity" : "silica",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ianyh/silica",
"state" : {
"branch" : "master",
"revision" : "8bda2ed4aac62917c696282822e0de2341837d0a"
}
},
{
"identity" : "sparkle",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sparkle-project/Sparkle",
"state" : {
"revision" : "f0ceaf5cc9f3f23daa0ccb6dcebd79fc96ccc7d9",
"version" : "2.5.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
"version" : "1.2.3"
}
},
{
"identity" : "swiftybeaver",
"kind" : "remoteSourceControl",
"location" : "https://github.com/SwiftyBeaver/SwiftyBeaver",
"state" : {
"revision" : "12b5acf96d98f91d50de447369bd18df74600f1a",
"version" : "1.9.6"
}
},
{
"identity" : "swiftyjson",
"kind" : "remoteSourceControl",
"location" : "https://github.com/SwiftyJSON/SwiftyJSON",
"state" : {
"revision" : "b3dcd7dbd0d488e1a7077cb33b00f2083e382f07",
"version" : "5.0.1"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams",
"state" : {
"revision" : "0d9ee7ea8c4ebd4a489ad7a73d5c6cad55d6fed3",
"version" : "5.0.6"
}
}
],
"version" : 2
}
11 changes: 10 additions & 1 deletion Amethyst.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "dacd754e4470b2e150237d6958482482a937b9ccadab57ae7aab15ece775f80c",
"originHash" : "fe3d2c8ad5bfad1c11700f1d2199c7af01af5866fc7886f59b9eaee2c9467cd2",
"pins" : [
{
"identity" : "cartography",
Expand Down Expand Up @@ -28,6 +28,15 @@
"version" : "2.1.2"
}
},
{
"identity" : "keyboardshortcuts",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sindresorhus/KeyboardShortcuts",
"state" : {
"revision" : "b878f8132be59576fc87e39405b1914eff9f55d3",
"version" : "1.14.1"
}
},
{
"identity" : "loginservicekit",
"kind" : "remoteSourceControl",
Expand Down
9 changes: 5 additions & 4 deletions Amethyst/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22154" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17701"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22154"/>
<capability name="Image references" minToolsVersion="12.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down Expand Up @@ -74,7 +75,7 @@
<rect key="contentRect" x="446" y="278" width="480" height="270"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="875"/>
<view key="contentView" id="IHj-fa-gIc">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<rect key="frame" x="0.0" y="0.0" width="480" height="260"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
<toolbar key="toolbar" implicitIdentifier="2383369F-98A6-4420-8EFE-319DB9588C53" autosavesConfiguration="NO" allowsUserCustomization="NO" displayMode="iconAndLabel" sizeMode="regular" id="5oI-m4-akz">
Expand Down Expand Up @@ -160,7 +161,7 @@
</objects>
<resources>
<image name="123.rectangle" catalog="system" width="24" height="18"/>
<image name="dots.and.line.vertical.and.cursorarrow.rectangle" catalog="system" width="24" height="23"/>
<image name="dots.and.line.vertical.and.cursorarrow.rectangle" catalog="system" width="24" height="25"/>
<image name="macwindow.on.rectangle" catalog="system" width="25" height="20"/>
<image name="text.and.command.macwindow" catalog="system" width="24" height="18"/>
<image name="uiwindow.split.2x1" catalog="system" width="24" height="18"/>
Expand Down
3 changes: 1 addition & 2 deletions Amethyst/Events/HotKeyManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2016 Ian Ynda-Hummel. All rights reserved.
//

import Carbon
import Foundation
import MASShortcut
import Silica

// Type for defining key code.
Expand All @@ -33,7 +33,6 @@ class HotKeyManager<Application: ApplicationType>: NSObject {
self.userConfiguration = userConfiguration
super.init()
_ = constructKeyCodeMap()
MASShortcutValidator.shared().allowAnyShortcutWithOptionModifier = true
}

private static func keyCodeForNumber(_ number: NSNumber) -> AMKeyCode {
Expand Down
33 changes: 22 additions & 11 deletions Amethyst/Managers/HotKeyRegistrar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import KeyboardShortcuts
import MASShortcut

protocol HotKeyRegistrar {
Expand All @@ -15,30 +16,40 @@ protocol HotKeyRegistrar {

extension HotKeyManager: HotKeyRegistrar {
func registerHotKey(with string: String?, modifiers: AMModifierFlags?, handler: @escaping () -> Void, defaultsKey: String, override: Bool) {
let userDefaults = UserDefaults.standard
let name = KeyboardShortcuts.Name(defaultsKey)

defer {
KeyboardShortcuts.onKeyUp(for: name, action: handler)
}

if override {
MASShortcutBinder.shared().breakBinding(withDefaultsKey: defaultsKey)
userDefaults.removeObject(forKey: defaultsKey)
UserDefaults.standard.removeObject(forKey: defaultsKey)
KeyboardShortcuts.setShortcut(nil, for: name)
}

guard KeyboardShortcuts.getShortcut(for: name) == nil else {
return
}

if userDefaults.object(forKey: defaultsKey) != nil {
MASShortcutBinder.shared().bindShortcut(withDefaultsKey: defaultsKey, toAction: handler)
if let value = UserDefaults.standard.object(forKey: defaultsKey),
let shortcut = ValueTransformer(forName: .keyedUnarchiveFromDataTransformerName)?.transformedValue(value) as? MASShortcut {
let shortcutKey = KeyboardShortcuts.Key(rawValue: shortcut.keyCode)
let newShortcut = KeyboardShortcuts.Shortcut(shortcutKey, modifiers: shortcut.modifierFlags)
// Keeping the old shortcuts in defaults for now to prevent data loss
// UserDefaults.standard.removeObject(forKey: defaultsKey)
KeyboardShortcuts.setShortcut(newShortcut, for: name)
return
}

// If a command is specified, set it as the default shortcut
if let string = string, let modifiers = modifiers {
if let keyCodes = stringToKeyCodes[string.lowercased()], !keyCodes.isEmpty {
let shortcut = MASShortcut(keyCode: keyCodes[0], modifierFlags: modifiers)
MASShortcutBinder.shared().registerDefaultShortcuts([ defaultsKey: shortcut as Any ])
// Note that the shortcut binder above only sets the default value, not the stored value, so we explicitly store it here.
userDefaults.set(userDefaults.object(forKey: defaultsKey), forKey: defaultsKey)
let shortcutKey = KeyboardShortcuts.Key(rawValue: keyCodes[0])
let shortcut = KeyboardShortcuts.Shortcut(shortcutKey, modifiers: modifiers)
KeyboardShortcuts.setShortcut(shortcut, for: name)
} else {
log.warning("String \"\(string)\" does not map to any keycodes")
}
}

MASShortcutBinder.shared().bindShortcut(withDefaultsKey: defaultsKey, toAction: handler)
}
}
25 changes: 12 additions & 13 deletions Amethyst/Preferences/ShortcutsPreferencesListItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
//

import Cartography
import Cocoa
import Foundation
import MASShortcut
import KeyboardShortcuts

class ShortcutsPreferencesListItemView: NSView {
private(set) var nameLabel: NSTextField?
private(set) var shortcutView: MASShortcutView?

override init(frame frameRect: NSRect) {
super.init(frame: frameRect)

let label = NSTextField()
let shortcutView = MASShortcutView(frame: NSRect(x: 0, y: 0, width: 120, height: 19))

label.isBezeled = false
label.isEditable = false
Expand All @@ -27,27 +26,27 @@ class ShortcutsPreferencesListItemView: NSView {
label.sizeToFit()

addSubview(label)
addSubview(shortcutView)

constrain(label, shortcutView, self) { label, shortcutView, view in
constrain(label, self) { label, view in
label.centerY == view.centerY
label.left == view.left + 8

shortcutView.centerY == view.centerY
shortcutView.right == view.right - 16
shortcutView.width == 120
shortcutView.height == 19
}

self.nameLabel = label
self.shortcutView = shortcutView
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit {
shortcutView?.associatedUserDefaultsKey = nil
func setShortcutName(name: KeyboardShortcuts.Name) {
let shortcutView = KeyboardShortcuts.RecorderCocoa(for: name)

addSubview(shortcutView)

constrain(shortcutView, self) { shortcutView, view in
shortcutView.centerY == view.centerY
shortcutView.right == view.right - 16
}
}
}
4 changes: 2 additions & 2 deletions Amethyst/Preferences/ShortcutsPreferencesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Cocoa
import Foundation
import MASShortcut
import KeyboardShortcuts
import Silica

class ShortcutsPreferencesViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
Expand Down Expand Up @@ -38,7 +38,7 @@ class ShortcutsPreferencesViewController: NSViewController, NSTableViewDataSourc
let key = hotKeyNameToDefaultsKey[row][1]

shortcutItemView.nameLabel?.stringValue = name
shortcutItemView.shortcutView?.associatedUserDefaultsKey = key
shortcutItemView.setShortcutName(name: KeyboardShortcuts.Name(key))

return shortcutItemView
}
Expand Down

0 comments on commit da914e2

Please sign in to comment.