Skip to content

Commit

Permalink
More consistent settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxblh committed Oct 12, 2019
1 parent e44ff00 commit 41a9544
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 59 deletions.
15 changes: 15 additions & 0 deletions MTMR/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true] as NSDictionary)

TouchBarController.shared.setupControlStripPresence()
HapticFeedbackUpdate()

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

func applicationWillTerminate(_: Notification) {}

func HapticFeedbackUpdate() {
HapticFeedback.shared = TouchBarController.shared.hapticFeedbackState ? HapticFeedback() : nil
}

@objc func updateIsBlockedApp() {
var blacklistAppIdentifiers: [String] = []
if let blackListed = UserDefaults.standard.stringArray(forKey: "com.toxblh.mtmr.blackListedApps") {
Expand Down Expand Up @@ -90,6 +95,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

@objc func toggleHapticFeedback(_: Any?) {
TouchBarController.shared.hapticFeedbackState = !TouchBarController.shared.hapticFeedbackState
HapticFeedbackUpdate()
createMenu()
}

@objc func openPreset(_: Any?) {
let dialog = NSOpenPanel()

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

let hapticFeedback = NSMenuItem(title: "Haptic Feedback", action: #selector(toggleHapticFeedback(_:)), keyEquivalent: "H")
hapticFeedback.state = TouchBarController.shared.hapticFeedbackState ? .on : .off

let settingSeparator = NSMenuItem(title: "Settings", action: nil, keyEquivalent: "")
settingSeparator.isEnabled = false

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

menu.addItem(NSMenuItem.separator())
menu.addItem(settingSeparator)
menu.addItem(hapticFeedback)
menu.addItem(hideControlStrip)
menu.addItem(toggleBlackList)
menu.addItem(startAtLogin)
Expand Down
2 changes: 1 addition & 1 deletion MTMR/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>0.23.1</string>
<key>CFBundleVersion</key>
<string>278</string>
<string>294</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
20 changes: 2 additions & 18 deletions MTMR/ItemsParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,11 @@ import AppKit
import Foundation

extension Data {
func mtmrPreset() -> Preset? {
let data = self.utf8string!.stripComments().data(using: .utf8)!
guard let preset = try? JSONDecoder().decode(Preset.self, from: data) else {
if let oldFormat = try? JSONDecoder().decode([BarItemDefinition].self, from: data) {
return Preset(settings: nil, barItems: oldFormat)
}
return nil
}
return preset
func barItemDefinitions() -> [BarItemDefinition]? {
return try? JSONDecoder().decode([BarItemDefinition].self, from: utf8string!.stripComments().data(using: .utf8)!)
}
}

struct Preset: Decodable {
let settings: GlobalSettings?
let barItems: [BarItemDefinition]
}

struct GlobalSettings: Decodable {
let hapticFeedback: Bool?
}

struct BarItemDefinition: Decodable {
let type: ItemType
let action: ActionType
Expand Down
23 changes: 11 additions & 12 deletions MTMR/TouchBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
UserDefaults.standard.set(newValue, forKey: "com.toxblh.mtmr.settings.showControlStrip")
}
}

var hapticFeedbackState: Bool {
get {
return UserDefaults.standard.bool(forKey: "com.toxblh.mtmr.settings.hapticFeedback")
}
set {
UserDefaults.standard.set(newValue, forKey: "com.toxblh.mtmr.settings.hapticFeedback")
}
}

var blacklistAppIdentifiers: [String] = []
var frontmostApplicationIdentifier: String? {
Expand Down Expand Up @@ -172,18 +181,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {

func reloadPreset(path: String) {
lastPresetPath = path
let preset = path.fileData?.mtmrPreset() ?? fallbackPreset()
applySettings(preset.settings ?? GlobalSettings(hapticFeedback: true))
createAndUpdatePreset(newJsonItems: preset.barItems)
}

func fallbackPreset() -> Preset {
let items = [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
return Preset(settings: nil, barItems: items)
}

func applySettings(_ settings: GlobalSettings) {
HapticFeedback.shared = settings.hapticFeedback ?? true ? HapticFeedback() : nil
let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
createAndUpdatePreset(newJsonItems: items)
}

func loadItemDefinitions(jsonItems: [BarItemDefinition]) {
Expand Down
28 changes: 0 additions & 28 deletions MTMRTests/ParseConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,4 @@ class ParseConfig: XCTestCase {
return
}
}

func testParsesOldFormat() {
let fixture = """
[ { "type": "escape" } ]
""".data(using: .utf8)!
let result = fixture.mtmrPreset()
XCTAssertEqual(result?.barItems.count, 1)
guard case .staticButton("esc")? = result?.barItems.first?.type else {
XCTFail()
return
}
}

func testParsesHapticFeedbackSettings() {
let fixture = """
{
"settings": { "hapticFeedback": false },
"barItems": [ { "type": "escape" } ]
}
""".data(using: .utf8)!
let result = fixture.mtmrPreset()
XCTAssertEqual(result?.barItems.count, 1)
guard case .staticButton("esc")? = result?.barItems.first?.type else {
XCTFail()
return
}
XCTAssertEqual(result?.settings?.hapticFeedback, .some(false))
}
}

0 comments on commit 41a9544

Please sign in to comment.