Skip to content

Commit 41a9544

Browse files
committed
More consistent settings
1 parent e44ff00 commit 41a9544

File tree

5 files changed

+29
-59
lines changed

5 files changed

+29
-59
lines changed

MTMR/AppDelegate.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2525
AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true] as NSDictionary)
2626

2727
TouchBarController.shared.setupControlStripPresence()
28+
HapticFeedbackUpdate()
2829

2930
if let button = statusItem.button {
3031
button.image = #imageLiteral(resourceName: "StatusImage")
@@ -40,6 +41,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4041

4142
func applicationWillTerminate(_: Notification) {}
4243

44+
func HapticFeedbackUpdate() {
45+
HapticFeedback.shared = TouchBarController.shared.hapticFeedbackState ? HapticFeedback() : nil
46+
}
47+
4348
@objc func updateIsBlockedApp() {
4449
var blacklistAppIdentifiers: [String] = []
4550
if let blackListed = UserDefaults.standard.stringArray(forKey: "com.toxblh.mtmr.blackListedApps") {
@@ -90,6 +95,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
9095
}
9196
}
9297

98+
@objc func toggleHapticFeedback(_: Any?) {
99+
TouchBarController.shared.hapticFeedbackState = !TouchBarController.shared.hapticFeedbackState
100+
HapticFeedbackUpdate()
101+
createMenu()
102+
}
103+
93104
@objc func openPreset(_: Any?) {
94105
let dialog = NSOpenPanel()
95106

@@ -124,6 +135,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
124135
let hideControlStrip = NSMenuItem(title: "Hide Control Strip", action: #selector(toggleControlStrip(_:)), keyEquivalent: "T")
125136
hideControlStrip.state = TouchBarController.shared.showControlStripState ? .off : .on
126137

138+
let hapticFeedback = NSMenuItem(title: "Haptic Feedback", action: #selector(toggleHapticFeedback(_:)), keyEquivalent: "H")
139+
hapticFeedback.state = TouchBarController.shared.hapticFeedbackState ? .on : .off
140+
127141
let settingSeparator = NSMenuItem(title: "Settings", action: nil, keyEquivalent: "")
128142
settingSeparator.isEnabled = false
129143

@@ -133,6 +147,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
133147

134148
menu.addItem(NSMenuItem.separator())
135149
menu.addItem(settingSeparator)
150+
menu.addItem(hapticFeedback)
136151
menu.addItem(hideControlStrip)
137152
menu.addItem(toggleBlackList)
138153
menu.addItem(startAtLogin)

MTMR/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>0.23.1</string>
2121
<key>CFBundleVersion</key>
22-
<string>278</string>
22+
<string>294</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSMinimumSystemVersion</key>

MTMR/ItemsParsing.swift

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

44
extension Data {
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
5+
func barItemDefinitions() -> [BarItemDefinition]? {
6+
return try? JSONDecoder().decode([BarItemDefinition].self, from: utf8string!.stripComments().data(using: .utf8)!)
147
}
158
}
169

17-
struct Preset: Decodable {
18-
let settings: GlobalSettings?
19-
let barItems: [BarItemDefinition]
20-
}
21-
22-
struct GlobalSettings: Decodable {
23-
let hapticFeedback: Bool?
24-
}
25-
2610
struct BarItemDefinition: Decodable {
2711
let type: ItemType
2812
let action: ActionType

MTMR/TouchBarController.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
8989
UserDefaults.standard.set(newValue, forKey: "com.toxblh.mtmr.settings.showControlStrip")
9090
}
9191
}
92+
93+
var hapticFeedbackState: Bool {
94+
get {
95+
return UserDefaults.standard.bool(forKey: "com.toxblh.mtmr.settings.hapticFeedback")
96+
}
97+
set {
98+
UserDefaults.standard.set(newValue, forKey: "com.toxblh.mtmr.settings.hapticFeedback")
99+
}
100+
}
92101

93102
var blacklistAppIdentifiers: [String] = []
94103
var frontmostApplicationIdentifier: String? {
@@ -172,18 +181,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
172181

173182
func reloadPreset(path: String) {
174183
lastPresetPath = path
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
184+
let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
185+
createAndUpdatePreset(newJsonItems: items)
187186
}
188187

189188
func loadItemDefinitions(jsonItems: [BarItemDefinition]) {

MTMRTests/ParseConfigTests.swift

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,4 @@ 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-
}
9567
}

0 commit comments

Comments
 (0)