mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
+ DnD Widget
This commit is contained in:
parent
09d9f9618f
commit
71051d7ad0
@ -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 = "<group>"; };
|
||||
B08173282135F128005D4908 /* CBBlueLightClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CBBlueLightClient.h; sourceTree = "<group>"; };
|
||||
B08173292135F354005D4908 /* CoreBrightness.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreBrightness.framework; path = ../../../../../System/Library/PrivateFrameworks/CoreBrightness.framework; sourceTree = "<group>"; };
|
||||
B081732B213739FE005D4908 /* DnDBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DnDBarItem.swift; sourceTree = "<group>"; };
|
||||
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 = "<group>"; };
|
||||
B082B256205C7D8000BC04DC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||
@ -256,6 +258,7 @@
|
||||
60C44AFC20A373A100C0EC91 /* MusicBarItem.swift */,
|
||||
60669B4220AD8FA80074E817 /* GroupBarItem.swift */,
|
||||
B08173262135F02B005D4908 /* NightShiftBarItem.swift */,
|
||||
B081732B213739FE005D4908 /* DnDBarItem.swift */,
|
||||
);
|
||||
path = Widgets;
|
||||
sourceTree = "<group>";
|
||||
@ -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 */,
|
||||
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
78
MTMR/Widgets/DnDBarItem.swift
Normal file
78
MTMR/Widgets/DnDBarItem.swift
Normal file
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user