Skip to content

Commit e44ff00

Browse files
committed
support for global settings. haptic feedback as the first one
resolves #185
1 parent f378de6 commit e44ff00

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed

MTMR/CustomButtonTouchBarItem.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ class CustomButtonCell: NSButtonCell {
176176

177177
class HapticClickGestureRecognizer: NSClickGestureRecognizer {
178178
override func touchesBegan(with event: NSEvent) {
179-
HapticFeedback.shared.tap(strong: 2)
179+
HapticFeedback.shared?.tap(strong: 2)
180180
super.touchesBegan(with: event)
181181
}
182182

183183
override func touchesEnded(with event: NSEvent) {
184-
HapticFeedback.shared.tap(strong: 1)
184+
HapticFeedback.shared?.tap(strong: 1)
185185
super.touchesEnded(with: event)
186186
}
187187
}
@@ -226,7 +226,7 @@ class LongPressGestureRecognizer: NSPressGestureRecognizer {
226226
@objc private func onTimer() {
227227
if let target = self.target, let action = self.action {
228228
target.performSelector(onMainThread: action, with: self, waitUntilDone: false)
229-
HapticFeedback.shared.tap(strong: 6)
229+
HapticFeedback.shared?.tap(strong: 6)
230230
}
231231
}
232232

MTMR/HapticFeedback.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import IOKit
1010

1111
class HapticFeedback {
12-
static let shared = HapticFeedback()
12+
static var shared: HapticFeedback?
1313

1414
// Here we have list of possible IDs for Haptic Generator Device. They are not constant
1515
// To find deviceID, you will need IORegistryExplorer app from Additional Tools for Xcode dmg

MTMR/ItemsParsing.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@ import AppKit
22
import Foundation
33

44
extension Data {
5-
func barItemDefinitions() -> [BarItemDefinition]? {
6-
return try? JSONDecoder().decode([BarItemDefinition].self, from: utf8string!.stripComments().data(using: .utf8)!)
5+
func mtmrPreset() -> Preset? {
6+
let data = self.utf8string!.stripComments().data(using: .utf8)!
7+
guard let preset = try? JSONDecoder().decode(Preset.self, from: data) else {
8+
if let oldFormat = try? JSONDecoder().decode([BarItemDefinition].self, from: data) {
9+
return Preset(settings: nil, barItems: oldFormat)
10+
}
11+
return nil
12+
}
13+
return preset
714
}
815
}
916

17+
struct Preset: Decodable {
18+
let settings: GlobalSettings?
19+
let barItems: [BarItemDefinition]
20+
}
21+
22+
struct GlobalSettings: Decodable {
23+
let hapticFeedback: Bool?
24+
}
25+
1026
struct BarItemDefinition: Decodable {
1127
let type: ItemType
1228
let action: ActionType

MTMR/TouchBarController.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,18 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
172172

173173
func reloadPreset(path: String) {
174174
lastPresetPath = path
175-
let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
176-
createAndUpdatePreset(newJsonItems: items)
175+
let preset = path.fileData?.mtmrPreset() ?? fallbackPreset()
176+
applySettings(preset.settings ?? GlobalSettings(hapticFeedback: true))
177+
createAndUpdatePreset(newJsonItems: preset.barItems)
178+
}
179+
180+
func fallbackPreset() -> Preset {
181+
let items = [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
182+
return Preset(settings: nil, barItems: items)
183+
}
184+
185+
func applySettings(_ settings: GlobalSettings) {
186+
HapticFeedback.shared = settings.hapticFeedback ?? true ? HapticFeedback() : nil
177187
}
178188

179189
func loadItemDefinitions(jsonItems: [BarItemDefinition]) {

MTMR/Widgets/AppScrubberTouchBarItem.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
146146
ticks += 1
147147

148148
if ticks == minTicks {
149-
HapticFeedback.shared.tap(strong: 2)
149+
HapticFeedback.shared?.tap(strong: 2)
150150
}
151151

152152
if ticks > maxTicks {
153153
stopTimer()
154-
HapticFeedback.shared.tap(strong: 6)
154+
HapticFeedback.shared?.tap(strong: 6)
155155
}
156156
}
157157

@@ -182,7 +182,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
182182
NSWorkspace.shared.openFile(bundleIdentifier!.replacingOccurrences(of: "file://", with: ""))
183183
} else {
184184
NSWorkspace.shared.launchApplication(withBundleIdentifier: bundleIdentifier!, options: [.default], additionalEventParamDescriptor: nil, launchIdentifier: nil)
185-
HapticFeedback.shared.tap(strong: 6)
185+
HapticFeedback.shared?.tap(strong: 6)
186186
}
187187
updateRunningApplication()
188188

@@ -201,7 +201,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
201201
}
202202
}
203203
} else {
204-
HapticFeedback.shared.tap(strong: 6)
204+
HapticFeedback.shared?.tap(strong: 6)
205205
if let index = self.persistentAppIdentifiers.index(of: bundleIdentifier!) {
206206
persistentAppIdentifiers.remove(at: index)
207207
} else {

MTMRTests/ParseConfigTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,32 @@ class ParseConfig: XCTestCase {
6464
return
6565
}
6666
}
67+
68+
func testParsesOldFormat() {
69+
let fixture = """
70+
[ { "type": "escape" } ]
71+
""".data(using: .utf8)!
72+
let result = fixture.mtmrPreset()
73+
XCTAssertEqual(result?.barItems.count, 1)
74+
guard case .staticButton("esc")? = result?.barItems.first?.type else {
75+
XCTFail()
76+
return
77+
}
78+
}
79+
80+
func testParsesHapticFeedbackSettings() {
81+
let fixture = """
82+
{
83+
"settings": { "hapticFeedback": false },
84+
"barItems": [ { "type": "escape" } ]
85+
}
86+
""".data(using: .utf8)!
87+
let result = fixture.mtmrPreset()
88+
XCTAssertEqual(result?.barItems.count, 1)
89+
guard case .staticButton("esc")? = result?.barItems.first?.type else {
90+
XCTFail()
91+
return
92+
}
93+
XCTAssertEqual(result?.settings?.hapticFeedback, .some(false))
94+
}
6795
}

0 commit comments

Comments
 (0)