Skip to content

Commit 9a37300

Browse files
authored
Change the save to KeyCode or Key to support special keys (#59)
* Change the storage target from KeyCode to Key * Add test cases
1 parent df4db1d commit 9a37300

File tree

4 files changed

+299
-32
lines changed

4 files changed

+299
-32
lines changed

Lib/Magnet.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
099F2A07246D094500992925 /* ModifierEventHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 099F2A04246D094500992925 /* ModifierEventHandlerTests.swift */; };
1818
09DEE124245B128C00169BEC /* Sauce.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 091D85CF2459553A00930473 /* Sauce.framework */; };
1919
FA3AA2162315A6A3007EAA1F /* CollectionExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3AA2152315A6A3007EAA1F /* CollectionExtensionTests.swift */; };
20+
FADEA45A24796103003AEC83 /* v3_1_0KeyCombo.swift in Sources */ = {isa = PBXBuildFile; fileRef = FADEA45924796103003AEC83 /* v3_1_0KeyCombo.swift */; };
2021
FAEC34B31C9059DF004177E2 /* Magnet.h in Headers */ = {isa = PBXBuildFile; fileRef = FAEC34B21C9059DF004177E2 /* Magnet.h */; settings = {ATTRIBUTES = (Public, ); }; };
2122
FAEC34BA1C9059DF004177E2 /* Magnet.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FAEC34AF1C9059DF004177E2 /* Magnet.framework */; };
2223
FAEC34D81C905B4D004177E2 /* HotKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEC34D71C905B4D004177E2 /* HotKey.swift */; };
@@ -59,6 +60,7 @@
5960
099F29FF246D091400992925 /* ModifierEventHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModifierEventHandler.swift; sourceTree = "<group>"; };
6061
099F2A04246D094500992925 /* ModifierEventHandlerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModifierEventHandlerTests.swift; sourceTree = "<group>"; };
6162
FA3AA2152315A6A3007EAA1F /* CollectionExtensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionExtensionTests.swift; sourceTree = "<group>"; };
63+
FADEA45924796103003AEC83 /* v3_1_0KeyCombo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = v3_1_0KeyCombo.swift; sourceTree = "<group>"; };
6264
FAEC34AF1C9059DF004177E2 /* Magnet.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Magnet.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6365
FAEC34B21C9059DF004177E2 /* Magnet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Magnet.h; sourceTree = "<group>"; };
6466
FAEC34B41C9059DF004177E2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -121,6 +123,7 @@
121123
isa = PBXGroup;
122124
children = (
123125
099F29F9246CFA9500992925 /* v2_0_0KeyCombo.swift */,
126+
FADEA45924796103003AEC83 /* v3_1_0KeyCombo.swift */,
124127
);
125128
path = Fixtures;
126129
sourceTree = "<group>";
@@ -319,6 +322,7 @@
319322
isa = PBXSourcesBuildPhase;
320323
buildActionMask = 2147483647;
321324
files = (
325+
FADEA45A24796103003AEC83 /* v3_1_0KeyCombo.swift in Sources */,
322326
099F2A07246D094500992925 /* ModifierEventHandlerTests.swift in Sources */,
323327
090077D4245D449F0099B20A /* KeyComboTests.swift in Sources */,
324328
099F29FA246CFA9500992925 /* v2_0_0KeyCombo.swift in Sources */,

Lib/Magnet/KeyCombo.swift

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,31 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable {
9494
// MARK: - NSCoding
9595
public init?(coder aDecoder: NSCoder) {
9696
self.doubledModifiers = aDecoder.decodeBool(forKey: CodingKeys.doubledModifiers.rawValue)
97-
if doubledModifiers {
97+
self.modifiers = aDecoder.decodeInteger(forKey: CodingKeys.modifiers.rawValue)
98+
guard !doubledModifiers else {
9899
self.key = .a
99-
} else {
100-
// Changed KeyCode to QWERTYKeyCode from v3.0.0
101-
let containsKeyCode = aDecoder.containsValue(forKey: CodingKeys.keyCode.rawValue)
102-
let QWERTYKeyCode: Int
103-
if containsKeyCode {
104-
QWERTYKeyCode = aDecoder.decodeInteger(forKey: CodingKeys.keyCode.rawValue)
105-
} else {
106-
QWERTYKeyCode = aDecoder.decodeInteger(forKey: CodingKeys.QWERTYKeyCode.rawValue)
107-
}
108-
guard let key = Key(QWERTYKeyCode: QWERTYKeyCode) else { return nil }
100+
return
101+
}
102+
// Changed KeyCode to Key from v3.2.0
103+
guard !aDecoder.containsValue(forKey: CodingKeys.key.rawValue) else {
104+
guard let keyRawValue = aDecoder.decodeObject(forKey: CodingKeys.key.rawValue) as? String else { return nil }
105+
guard let key = Key(rawValue: keyRawValue) else { return nil }
109106
self.key = key
107+
return
110108
}
111-
self.modifiers = aDecoder.decodeInteger(forKey: CodingKeys.modifiers.rawValue)
109+
// Changed KeyCode to QWERTYKeyCode from v3.0.0
110+
let QWERTYKeyCode: Int
111+
if aDecoder.containsValue(forKey: CodingKeys.keyCode.rawValue) {
112+
QWERTYKeyCode = aDecoder.decodeInteger(forKey: CodingKeys.keyCode.rawValue)
113+
} else {
114+
QWERTYKeyCode = aDecoder.decodeInteger(forKey: CodingKeys.QWERTYKeyCode.rawValue)
115+
}
116+
guard let key = Key(QWERTYKeyCode: QWERTYKeyCode) else { return nil }
117+
self.key = key
112118
}
113119

114120
public func encode(with aCoder: NSCoder) {
115-
aCoder.encode(QWERTYKeyCode, forKey: CodingKeys.QWERTYKeyCode.rawValue)
121+
aCoder.encode(key.rawValue, forKey: CodingKeys.key.rawValue)
116122
aCoder.encode(modifiers, forKey: CodingKeys.modifiers.rawValue)
117123
aCoder.encode(doubledModifiers, forKey: CodingKeys.doubledModifiers.rawValue)
118124
}
@@ -121,33 +127,37 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable {
121127
public init(from decoder: Decoder) throws {
122128
let container = try decoder.container(keyedBy: CodingKeys.self)
123129
self.doubledModifiers = try container.decode(Bool.self, forKey: .doubledModifiers)
124-
if doubledModifiers {
130+
self.modifiers = try container.decode(Int.self, forKey: .modifiers)
131+
guard !doubledModifiers else {
125132
self.key = .a
133+
return
134+
}
135+
// Changed KeyCode to Key from v3.2.0
136+
guard !container.contains(.key) else {
137+
self.key = try container.decode(Key.self, forKey: .key)
138+
return
139+
}
140+
// Changed KeyCode to QWERTYKeyCode from v3.0.0
141+
let QWERTYKeyCode: Int
142+
if container.contains(.keyCode) {
143+
QWERTYKeyCode = try container.decode(Int.self, forKey: .keyCode)
126144
} else {
127-
let QWERTYKeyCode: Int
128-
if container.contains(.keyCode) {
129-
// Changed KeyCode to QWERTYKeyCode from v3.0.0
130-
QWERTYKeyCode = try container.decode(Int.self, forKey: .keyCode)
131-
} else {
132-
QWERTYKeyCode = try container.decode(Int.self, forKey: .QWERTYKeyCode)
133-
}
134-
guard let key = Key(QWERTYKeyCode: QWERTYKeyCode) else {
135-
throw KeyCombo.InitializeError()
136-
}
137-
self.key = key
145+
QWERTYKeyCode = try container.decode(Int.self, forKey: .QWERTYKeyCode)
138146
}
139-
self.modifiers = try container.decode(Int.self, forKey: .modifiers)
147+
guard let key = Key(QWERTYKeyCode: QWERTYKeyCode) else { throw KeyCombo.InitializeError() }
148+
self.key = key
140149
}
141150

142151
public func encode(to encoder: Encoder) throws {
143152
var container = encoder.container(keyedBy: CodingKeys.self)
144-
try container.encode(QWERTYKeyCode, forKey: .QWERTYKeyCode)
153+
try container.encode(key, forKey: .key)
145154
try container.encode(modifiers, forKey: .modifiers)
146155
try container.encode(doubledModifiers, forKey: .doubledModifiers)
147156
}
148157

149158
// MARK: - Coding Keys
150159
private enum CodingKeys: String, CodingKey {
160+
case key
151161
case keyCode
152162
case QWERTYKeyCode
153163
case modifiers
@@ -157,9 +167,9 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable {
157167
// MARK: - Equatable
158168
public override func isEqual(_ object: Any?) -> Bool {
159169
guard let keyCombo = object as? KeyCombo else { return false }
160-
return QWERTYKeyCode == keyCombo.QWERTYKeyCode &&
161-
modifiers == keyCombo.modifiers &&
162-
doubledModifiers == keyCombo.doubledModifiers
170+
return key == keyCombo.key &&
171+
modifiers == keyCombo.modifiers &&
172+
doubledModifiers == keyCombo.doubledModifiers
163173
}
164174

165175
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// v3_1_0KeyCombo.swift
3+
//
4+
// MagnetTests
5+
// GitHub: https://github.com/clipy
6+
// HP: https://clipy-app.com
7+
//
8+
// Copyright © 2015-2020 Clipy Project.
9+
//
10+
11+
import Foundation
12+
import Sauce
13+
14+
// swiftlint:disable type_name
15+
// ref: https://github.com/Clipy/Magnet/blob/v3.1.0/Lib/Magnet/KeyCombo.swift
16+
final class v3_1_0KeyCombo: NSObject, NSCoding, Codable {
17+
18+
// MARK: - Properties
19+
public let key: Key
20+
public let modifiers: Int
21+
public let doubledModifiers: Bool
22+
public var QWERTYKeyCode: Int {
23+
guard !doubledModifiers else { return 0 }
24+
return Int(key.QWERTYKeyCode)
25+
}
26+
27+
// MARK: - Initialize
28+
init(key: Key, modifiers: Int, doubledModifiers: Bool) {
29+
self.key = key
30+
self.modifiers = modifiers
31+
self.doubledModifiers = doubledModifiers
32+
}
33+
34+
public init?(coder aDecoder: NSCoder) {
35+
self.doubledModifiers = aDecoder.decodeBool(forKey: CodingKeys.doubledModifiers.rawValue)
36+
if doubledModifiers {
37+
self.key = .a
38+
} else {
39+
let QWERTYKeyCode = aDecoder.decodeInteger(forKey: CodingKeys.QWERTYKeyCode.rawValue)
40+
guard let key = Key(QWERTYKeyCode: QWERTYKeyCode) else { return nil }
41+
self.key = key
42+
}
43+
self.modifiers = aDecoder.decodeInteger(forKey: CodingKeys.modifiers.rawValue)
44+
}
45+
46+
public func encode(with aCoder: NSCoder) {
47+
aCoder.encode(QWERTYKeyCode, forKey: CodingKeys.QWERTYKeyCode.rawValue)
48+
aCoder.encode(modifiers, forKey: CodingKeys.modifiers.rawValue)
49+
aCoder.encode(doubledModifiers, forKey: CodingKeys.doubledModifiers.rawValue)
50+
}
51+
52+
public init(from decoder: Decoder) throws {
53+
let container = try decoder.container(keyedBy: CodingKeys.self)
54+
self.doubledModifiers = try container.decode(Bool.self, forKey: .doubledModifiers)
55+
if doubledModifiers {
56+
self.key = .a
57+
} else {
58+
let QWERTYKeyCode = try container.decode(Int.self, forKey: .QWERTYKeyCode)
59+
guard let key = Key(QWERTYKeyCode: QWERTYKeyCode) else { throw NSError() }
60+
self.key = key
61+
}
62+
self.modifiers = try container.decode(Int.self, forKey: .modifiers)
63+
}
64+
65+
public func encode(to encoder: Encoder) throws {
66+
var container = encoder.container(keyedBy: CodingKeys.self)
67+
try container.encode(QWERTYKeyCode, forKey: .QWERTYKeyCode)
68+
try container.encode(modifiers, forKey: .modifiers)
69+
try container.encode(doubledModifiers, forKey: .doubledModifiers)
70+
}
71+
72+
// MARK: - Coding Keys
73+
private enum CodingKeys: String, CodingKey {
74+
case QWERTYKeyCode
75+
case modifiers
76+
case doubledModifiers
77+
}
78+
79+
}

0 commit comments

Comments
 (0)