From 71051d7ad0445bc5fb315b94a41d0d93682aacff Mon Sep 17 00:00:00 2001 From: Toxblh Date: Thu, 30 Aug 2018 16:35:41 +0100 Subject: [PATCH] + DnD Widget --- MTMR.xcodeproj/project.pbxproj | 4 ++ MTMR/ItemsParsing.swift | 15 ++++++ MTMR/TouchBarController.swift | 4 ++ MTMR/Widgets/DnDBarItem.swift | 78 ++++++++++++++++++++++++++++ MTMR/Widgets/NightShiftBarItem.swift | 2 +- 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 MTMR/Widgets/DnDBarItem.swift diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index e601167..9800258 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -36,6 +36,7 @@ B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B059D62C205F11E8006E6B86 /* DFRFoundation.framework */; }; B08173272135F02B005D4908 /* NightShiftBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B08173262135F02B005D4908 /* NightShiftBarItem.swift */; }; B081732A2135F354005D4908 /* CoreBrightness.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B08173292135F354005D4908 /* CoreBrightness.framework */; }; + B081732C213739FE005D4908 /* DnDBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B081732B213739FE005D4908 /* DnDBarItem.swift */; }; B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B082B252205C7D8000BC04DC /* AppDelegate.swift */; }; B082B257205C7D8000BC04DC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B082B256205C7D8000BC04DC /* Assets.xcassets */; }; B082B25A205C7D8000BC04DC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B082B258205C7D8000BC04DC /* Main.storyboard */; }; @@ -92,6 +93,7 @@ B08173262135F02B005D4908 /* NightShiftBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NightShiftBarItem.swift; sourceTree = ""; }; B08173282135F128005D4908 /* CBBlueLightClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CBBlueLightClient.h; sourceTree = ""; }; B08173292135F354005D4908 /* CoreBrightness.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreBrightness.framework; path = ../../../../../System/Library/PrivateFrameworks/CoreBrightness.framework; sourceTree = ""; }; + B081732B213739FE005D4908 /* DnDBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DnDBarItem.swift; sourceTree = ""; }; B082B24F205C7D8000BC04DC /* MTMR.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MTMR.app; sourceTree = BUILT_PRODUCTS_DIR; }; B082B252205C7D8000BC04DC /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; B082B256205C7D8000BC04DC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -256,6 +258,7 @@ 60C44AFC20A373A100C0EC91 /* MusicBarItem.swift */, 60669B4220AD8FA80074E817 /* GroupBarItem.swift */, B08173262135F02B005D4908 /* NightShiftBarItem.swift */, + B081732B213739FE005D4908 /* DnDBarItem.swift */, ); path = Widgets; sourceTree = ""; @@ -382,6 +385,7 @@ B0008E552080286C003AD4DD /* SupportHelpers.swift in Sources */, 6042B6A72083E03A00C525C8 /* AppScrubberTouchBarItem.swift in Sources */, B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */, + B081732C213739FE005D4908 /* DnDBarItem.swift in Sources */, 60C44AFD20A373A100C0EC91 /* MusicBarItem.swift in Sources */, B059D624205E04F3006E6B86 /* CustomButtonTouchBarItem.swift in Sources */, 60173D3E20C0031B002C305F /* LaunchAtLoginController.m in Sources */, diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index ab5935b..ad9b2e2 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -185,6 +185,16 @@ class SupportedTypesHolder { longAction: .none, parameters: [:] ) + }, + + "dnd": { _ in + return ( + item: .dnd(), + action: .none, + longAction: .none, + parameters: [:] + ) + } ] @@ -221,6 +231,7 @@ enum ItemType: Decodable { case music(interval: Double) case groupBar(items: [BarItemDefinition]) case nightShift() + case dnd() private enum CodingKeys: String, CodingKey { case type @@ -253,6 +264,7 @@ enum ItemType: Decodable { case music case groupBar case nightShift + case dnd } init(from decoder: Decoder) throws { @@ -312,6 +324,9 @@ enum ItemType: Decodable { case .nightShift: self = .nightShift() + + case .dnd: + self = .dnd() } } } diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 3e6ad50..d9f4555 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -46,6 +46,8 @@ extension ItemType { return "com.toxblh.mtmr.groupBar." case .nightShift(items: _): return "com.toxblh.mtmr.nightShift." + case .dnd(items: _): + return "com.toxblh.mtmr.dnd." } } @@ -283,6 +285,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = GroupBarItem(identifier: identifier, items: items) case .nightShift(): barItem = NightShiftBarItem(identifier: identifier) + case .dnd(): + barItem = DnDBarItem(identifier: identifier) } if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem { diff --git a/MTMR/Widgets/DnDBarItem.swift b/MTMR/Widgets/DnDBarItem.swift new file mode 100644 index 0000000..1b6f2af --- /dev/null +++ b/MTMR/Widgets/DnDBarItem.swift @@ -0,0 +1,78 @@ +// +// DnDBarItem.swift +// MTMR +// +// Created by Anton Palgunov on 29/08/2018. +// Copyright © 2018 Anton Palgunov. All rights reserved. +// + +import Foundation + +class DnDBarItem : CustomButtonTouchBarItem { + private var timer: Timer! + + init(identifier: NSTouchBarItem.Identifier) { + super.init(identifier: identifier, title: "") + self.isBordered = false + self.setWidth(value: 32) + + self.tapClosure = { [weak self] in self?.DnDToggle() } + + timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refresh), userInfo: nil, repeats: true) + + self.refresh() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func DnDToggle() { + DoNotDisturb.isEnabled = !DoNotDisturb.isEnabled + refresh() + } + + @objc func refresh() { + self.image = DoNotDisturb.isEnabled ? #imageLiteral(resourceName: "dnd-on") : #imageLiteral(resourceName: "dnd-off") + } +} + +public struct DoNotDisturb { + private static let appId = "com.apple.notificationcenterui" as CFString + private static let dndPref = "com.apple.notificationcenterui.dndprefs_changed" + + private static func set(_ key: String, value: CFPropertyList?) { + CFPreferencesSetValue(key as CFString, value, appId, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost) + } + + private static func commitChanges() { + CFPreferencesSynchronize(appId, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost) + DistributedNotificationCenter.default().postNotificationName(NSNotification.Name(dndPref), object: nil, userInfo: nil, deliverImmediately: true) + NSRunningApplication.runningApplications(withBundleIdentifier: appId as String).first?.terminate() + } + + private static func enable() { + set("dndStart", value: nil) + set("dndEnd", value: nil) + set("doNotDisturb", value: true as CFPropertyList) + set("doNotDisturbDate", value: Date() as CFPropertyList) + commitChanges() + } + + private static func disable() { + set("dndStart", value: nil) + set("dndEnd", value: nil) + set("doNotDisturb", value: false as CFPropertyList) + set("doNotDisturbDate", value: nil) + commitChanges() + } + + static var isEnabled:Bool { + get { + return CFPreferencesGetAppBooleanValue("doNotDisturb" as CFString, appId, nil) + } + set { + newValue ? enable() : disable() + } + } +} diff --git a/MTMR/Widgets/NightShiftBarItem.swift b/MTMR/Widgets/NightShiftBarItem.swift index fca678f..00c13a8 100644 --- a/MTMR/Widgets/NightShiftBarItem.swift +++ b/MTMR/Widgets/NightShiftBarItem.swift @@ -33,7 +33,7 @@ class NightShiftBarItem: CustomButtonTouchBarItem { self.tapClosure = { [weak self] in self?.nightShiftAction() } - timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(refresh), userInfo: nil, repeats: true) + timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refresh), userInfo: nil, repeats: true) self.refresh() }