From cd2ec3d032cf4d5fab67653b3ac254f6ab23b360 Mon Sep 17 00:00:00 2001 From: Serg Date: Sat, 19 Oct 2019 00:28:04 +0700 Subject: [PATCH] use property wrappers to have settings in a nice way. now it's the only place for settings key --- MTMR.xcodeproj/project.pbxproj | 4 ++++ MTMR/AppDelegate.swift | 36 ++++++++++++++---------------- MTMR/AppSettings.swift | 30 +++++++++++++++++++++++++ MTMR/TouchBarController.swift | 39 ++++----------------------------- MTMR/Widgets/GroupBarItem.swift | 2 +- 5 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 MTMR/AppSettings.swift diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index c5b31ff..a720a6d 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ 36C2ECDD207C723B003CDA33 /* ParseConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECDC207C723B003CDA33 /* ParseConfigTests.swift */; }; 36C2ECDE207C82DE003CDA33 /* ItemsParsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */; }; 36C2ECE0207CB1B0003CDA33 /* defaultPreset.json in Resources */ = {isa = PBXBuildFile; fileRef = 36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */; }; + 36FEF872235A1CFC00A0ABCE /* AppSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36FEF871235A1CFC00A0ABCE /* AppSettings.swift */; }; 4CC9FEDC22FDEA65001512EB /* AMR_ANSIEscapeHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CC9FEDB22FDEA65001512EB /* AMR_ANSIEscapeHelper.m */; }; 4CDC6E5022FCA93F0069ADD4 /* ShellScriptTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDC6E4F22FCA93F0069ADD4 /* ShellScriptTouchBarItem.swift */; }; 4CFF5E5C22E623DD00BFB1EE /* YandexWeatherBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CFF5E5B22E623DD00BFB1EE /* YandexWeatherBarItem.swift */; }; @@ -94,6 +95,7 @@ 36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemsParsing.swift; sourceTree = ""; }; 36C2ECDC207C723B003CDA33 /* ParseConfigTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseConfigTests.swift; sourceTree = ""; }; 36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = defaultPreset.json; sourceTree = ""; }; + 36FEF871235A1CFC00A0ABCE /* AppSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppSettings.swift; sourceTree = ""; }; 4CC9FEDA22FDEA65001512EB /* AMR_ANSIEscapeHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMR_ANSIEscapeHelper.h; sourceTree = ""; }; 4CC9FEDB22FDEA65001512EB /* AMR_ANSIEscapeHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMR_ANSIEscapeHelper.m; sourceTree = ""; }; 4CDC6E4F22FCA93F0069ADD4 /* ShellScriptTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellScriptTouchBarItem.swift; sourceTree = ""; }; @@ -225,6 +227,7 @@ 36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */, B082B252205C7D8000BC04DC /* AppDelegate.swift */, B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */, + 36FEF871235A1CFC00A0ABCE /* AppSettings.swift */, B059D623205E04F3006E6B86 /* CustomButtonTouchBarItem.swift */, 36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */, 4CDC6E4F22FCA93F0069ADD4 /* ShellScriptTouchBarItem.swift */, @@ -474,6 +477,7 @@ 6042B6AA2083E27000C525C8 /* DeprecatedCarbonAPI.c in Sources */, B09EB1E4207C082000D5C1E0 /* HapticFeedback.swift in Sources */, B08173272135F02B005D4908 /* NightShiftBarItem.swift in Sources */, + 36FEF872235A1CFC00A0ABCE /* AppSettings.swift in Sources */, 60F7D454208CC31400ABF5D2 /* InputSourceBarItem.swift in Sources */, 36A778BE20A6C27100B38714 /* GeneralExtensions.swift in Sources */, 60669B4320AD8FA80074E817 /* GroupBarItem.swift in Sources */, diff --git a/MTMR/AppDelegate.swift b/MTMR/AppDelegate.swift index 72d2b45..ab27818 100644 --- a/MTMR/AppDelegate.swift +++ b/MTMR/AppDelegate.swift @@ -42,13 +42,12 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationWillTerminate(_: Notification) {} func HapticFeedbackUpdate() { - HapticFeedback.shared = TouchBarController.shared.hapticFeedbackState ? HapticFeedback() : nil + HapticFeedback.shared = AppSettings.hapticFeedbackState ? HapticFeedback() : nil } @objc func updateIsBlockedApp() { if let frontmostAppId = TouchBarController.shared.frontmostApplicationIdentifier { - let blacklistAppIdentifiers = UserDefaults.standard.stringArray(forKey: "com.toxblh.mtmr.blackListedApps") ?? [] - isBlockedApp = blacklistAppIdentifiers.firstIndex(of: frontmostAppId) != nil + isBlockedApp = AppSettings.blacklistedAppIds.firstIndex(of: frontmostAppId) != nil } else { isBlockedApp = false } @@ -64,10 +63,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { task.launch() } - @objc func toggleControlStrip(_: Any?) { - TouchBarController.shared.showControlStripState = !TouchBarController.shared.showControlStripState + @objc func toggleControlStrip(_ item: NSMenuItem) { + item.state = item.state == .on ? .off : .on + AppSettings.showControlStripState = item.state == .off TouchBarController.shared.resetControlStrip() - createMenu() } @objc func toggleBlackListedApp(_: Any?) { @@ -77,24 +76,23 @@ class AppDelegate: NSObject, NSApplicationDelegate { } else { TouchBarController.shared.blacklistAppIdentifiers.append(appIdentifier) } - - UserDefaults.standard.set(TouchBarController.shared.blacklistAppIdentifiers, forKey: "com.toxblh.mtmr.blackListedApps") - UserDefaults.standard.synchronize() - + + AppSettings.blacklistedAppIds = TouchBarController.shared.blacklistAppIdentifiers TouchBarController.shared.updateActiveApp() updateIsBlockedApp() } } - @objc func toggleHapticFeedback(_: Any?) { - TouchBarController.shared.hapticFeedbackState = !TouchBarController.shared.hapticFeedbackState + @objc func toggleHapticFeedback(_ item: NSMenuItem) { + item.state = item.state == .on ? .off : .on + AppSettings.hapticFeedbackState = item.state == .on HapticFeedbackUpdate() - createMenu() } - @objc func toggleMultitouch(_: Any?) { - TouchBarController.shared.multitouchGestures = !TouchBarController.shared.multitouchGestures - createMenu() + @objc func toggleMultitouch(_ item: NSMenuItem) { + item.state = item.state == .on ? .off : .on + AppSettings.multitouchGestures = item.state == .on + TouchBarController.shared.scrollArea?.gesturesEnabled = item.state == .on } @objc func openPreset(_: Any?) { @@ -129,13 +127,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { toggleBlackList.state = isBlockedApp ? .on : .off let hideControlStrip = NSMenuItem(title: "Hide Control Strip", action: #selector(toggleControlStrip(_:)), keyEquivalent: "T") - hideControlStrip.state = TouchBarController.shared.showControlStripState ? .off : .on + hideControlStrip.state = AppSettings.showControlStripState ? .off : .on let hapticFeedback = NSMenuItem(title: "Haptic Feedback", action: #selector(toggleHapticFeedback(_:)), keyEquivalent: "H") - hapticFeedback.state = TouchBarController.shared.hapticFeedbackState ? .on : .off + hapticFeedback.state = AppSettings.hapticFeedbackState ? .on : .off let multitouchGestures = NSMenuItem(title: "Volume/Brightness gestures", action: #selector(toggleMultitouch(_:)), keyEquivalent: "") - multitouchGestures.state = TouchBarController.shared.multitouchGestures ? .on : .off + multitouchGestures.state = AppSettings.multitouchGestures ? .on : .off let settingSeparator = NSMenuItem(title: "Settings", action: nil, keyEquivalent: "") settingSeparator.isEnabled = false diff --git a/MTMR/AppSettings.swift b/MTMR/AppSettings.swift new file mode 100644 index 0000000..c3f7cdf --- /dev/null +++ b/MTMR/AppSettings.swift @@ -0,0 +1,30 @@ +import Foundation + +struct AppSettings { + @UserDefault(key: "com.toxblh.mtmr.settings.showControlStrip", defaultValue: false) + static var showControlStripState: Bool + + @UserDefault(key: "com.toxblh.mtmr.settings.hapticFeedback", defaultValue: true) + static var hapticFeedbackState: Bool + + @UserDefault(key: "com.toxblh.mtmr.settings.multitouchGestures", defaultValue: true) + static var multitouchGestures: Bool + + @UserDefault(key: "com.toxblh.mtmr.blackListedApps", defaultValue: []) + static var blacklistedAppIds: [String] +} + +@propertyWrapper +struct UserDefault { + let key: String + let defaultValue: T + var wrappedValue: T { + get { + return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue + } + set { + UserDefaults.standard.set(newValue, forKey: key) + UserDefaults.standard.synchronize() + } + } +} diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 6706ae8..941711d 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -81,35 +81,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate { var scrollArea: ScrollViewItem? var centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString)) - var showControlStripState: Bool { - get { - return UserDefaults.standard.bool(forKey: "com.toxblh.mtmr.settings.showControlStrip") - } - set { - 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 multitouchGestures: Bool { - get { - return UserDefaults.standard.bool(forKey: "com.toxblh.mtmr.settings.multitouchGestures") - } - set { - scrollArea?.gesturesEnabled = newValue - UserDefaults.standard.set(newValue, forKey: "com.toxblh.mtmr.settings.multitouchGestures") - UserDefaults.standard.synchronize() - } - } - var blacklistAppIdentifiers: [String] = [] var frontmostApplicationIdentifier: String? { return NSWorkspace.shared.frontmostApplication?.bundleIdentifier @@ -126,10 +97,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { }), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: NSImage.stopProgressFreestandingTemplateName))!)]) } - if let blackListed = UserDefaults.standard.stringArray(forKey: "com.toxblh.mtmr.blackListedApps") { - blacklistAppIdentifiers = blackListed - } - + blacklistAppIdentifiers = AppSettings.blacklistedAppIds + NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didLaunchApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil) @@ -158,7 +127,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString)) scrollArea = ScrollViewItem(identifier: centerScrollArea, items: centerItems) - scrollArea?.gesturesEnabled = multitouchGestures + scrollArea?.gesturesEnabled = AppSettings.multitouchGestures touchBar.delegate = self touchBar.defaultItemIdentifiers = [] @@ -235,7 +204,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } @objc private func presentTouchBar() { - if showControlStripState { + if AppSettings.showControlStripState { updateControlStripPresence() presentSystemModal(touchBar, systemTrayItemIdentifier: .controlStripItem) } else { diff --git a/MTMR/Widgets/GroupBarItem.swift b/MTMR/Widgets/GroupBarItem.swift index e307833..b7ed226 100644 --- a/MTMR/Widgets/GroupBarItem.swift +++ b/MTMR/Widgets/GroupBarItem.swift @@ -52,7 +52,7 @@ class GroupBarItem: NSPopoverTouchBarItem, NSTouchBarDelegate { TouchBarController.shared.touchBar.defaultItemIdentifiers = [] TouchBarController.shared.touchBar.defaultItemIdentifiers = leftIdentifiers + [centerScrollArea] + rightIdentifiers - if TouchBarController.shared.showControlStripState { + if AppSettings.showControlStripState { presentSystemModal(TouchBarController.shared.touchBar, systemTrayItemIdentifier: .controlStripItem) } else { presentSystemModal(TouchBarController.shared.touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)