diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index d431b04..e601167 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -34,6 +34,8 @@ B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D621205E03F5006E6B86 /* TouchBarController.swift */; }; B059D624205E04F3006E6B86 /* CustomButtonTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D623205E04F3006E6B86 /* CustomButtonTouchBarItem.swift */; }; 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 */; }; 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 */; }; @@ -87,6 +89,9 @@ B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarPrivateApi.h; sourceTree = ""; }; B059D62A205F0E7D006E6B86 /* TouchBarPrivateApi-Bridging.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "TouchBarPrivateApi-Bridging.h"; sourceTree = ""; }; B059D62C205F11E8006E6B86 /* DFRFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DFRFoundation.framework; path = ../../../../../System/Library/PrivateFrameworks/DFRFoundation.framework; sourceTree = ""; }; + 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 = ""; }; 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 = ""; }; @@ -118,6 +123,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + B081732A2135F354005D4908 /* CoreBrightness.framework in Frameworks */, B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */, B09EB1E6207C0F8E00D5C1E0 /* MultitouchSupport.framework in Frameworks */, ); @@ -136,6 +142,7 @@ B059D62B205F11E8006E6B86 /* Frameworks */ = { isa = PBXGroup; children = ( + B08173292135F354005D4908 /* CoreBrightness.framework */, B09EB1E5207C0F8E00D5C1E0 /* MultitouchSupport.framework */, B059D62C205F11E8006E6B86 /* DFRFoundation.framework */, ); @@ -230,6 +237,7 @@ 6042B6A92083E27000C525C8 /* DeprecatedCarbonAPI.c */, 60173D3C20C0031B002C305F /* LaunchAtLoginController.h */, 60173D3D20C0031B002C305F /* LaunchAtLoginController.m */, + B08173282135F128005D4908 /* CBBlueLightClient.h */, ); path = CBridge; sourceTree = ""; @@ -247,6 +255,7 @@ 60F7D453208CC31400ABF5D2 /* InputSourceBarItem.swift */, 60C44AFC20A373A100C0EC91 /* MusicBarItem.swift */, 60669B4220AD8FA80074E817 /* GroupBarItem.swift */, + B08173262135F02B005D4908 /* NightShiftBarItem.swift */, ); path = Widgets; sourceTree = ""; @@ -381,6 +390,7 @@ B0F3112520C9E35F0076BB88 /* SupportNSTouchBar.swift in Sources */, 6042B6AA2083E27000C525C8 /* DeprecatedCarbonAPI.c in Sources */, B09EB1E4207C082000D5C1E0 /* HapticFeedback.swift in Sources */, + B08173272135F02B005D4908 /* NightShiftBarItem.swift in Sources */, 60F7D454208CC31400ABF5D2 /* InputSourceBarItem.swift in Sources */, 36A778BE20A6C27100B38714 /* GeneralExtensions.swift in Sources */, 60669B4320AD8FA80074E817 /* GroupBarItem.swift in Sources */, diff --git a/MTMR/Assets.xcassets/ill_down.imageset/Contents.json b/MTMR/Assets.xcassets/ill_down.imageset/Contents.json index 9b00565..c593265 100644 --- a/MTMR/Assets.xcassets/ill_down.imageset/Contents.json +++ b/MTMR/Assets.xcassets/ill_down.imageset/Contents.json @@ -6,7 +6,7 @@ }, { "idiom" : "universal", - "filename" : "ill_down.png", + "filename" : "KeyboardBrightDown.png", "scale" : "2x" }, { diff --git a/MTMR/Assets.xcassets/ill_down.imageset/KeyboardBrightDown.png b/MTMR/Assets.xcassets/ill_down.imageset/KeyboardBrightDown.png new file mode 100644 index 0000000..697f213 Binary files /dev/null and b/MTMR/Assets.xcassets/ill_down.imageset/KeyboardBrightDown.png differ diff --git a/MTMR/Assets.xcassets/ill_down.imageset/ill_down.png b/MTMR/Assets.xcassets/ill_down.imageset/ill_down.png deleted file mode 100644 index 0d3a01e..0000000 Binary files a/MTMR/Assets.xcassets/ill_down.imageset/ill_down.png and /dev/null differ diff --git a/MTMR/Assets.xcassets/ill_up.imageset/Contents.json b/MTMR/Assets.xcassets/ill_up.imageset/Contents.json index 3d34b88..b99b980 100644 --- a/MTMR/Assets.xcassets/ill_up.imageset/Contents.json +++ b/MTMR/Assets.xcassets/ill_up.imageset/Contents.json @@ -6,7 +6,7 @@ }, { "idiom" : "universal", - "filename" : "ill_up.png", + "filename" : "KeyboardBrightUp.png", "scale" : "2x" }, { diff --git a/MTMR/Assets.xcassets/ill_up.imageset/KeyboardBrightUp.png b/MTMR/Assets.xcassets/ill_up.imageset/KeyboardBrightUp.png new file mode 100644 index 0000000..e27fd39 Binary files /dev/null and b/MTMR/Assets.xcassets/ill_up.imageset/KeyboardBrightUp.png differ diff --git a/MTMR/Assets.xcassets/ill_up.imageset/ill_up.png b/MTMR/Assets.xcassets/ill_up.imageset/ill_up.png deleted file mode 100644 index 4639ae7..0000000 Binary files a/MTMR/Assets.xcassets/ill_up.imageset/ill_up.png and /dev/null differ diff --git a/MTMR/Assets.xcassets/nightShiftOff.imageset/Contents.json b/MTMR/Assets.xcassets/nightShiftOff.imageset/Contents.json new file mode 100644 index 0000000..a94ddb7 --- /dev/null +++ b/MTMR/Assets.xcassets/nightShiftOff.imageset/Contents.json @@ -0,0 +1,24 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "NightShift.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + }, + "properties" : { + "template-rendering-intent" : "template" + } +} \ No newline at end of file diff --git a/MTMR/Assets.xcassets/nightShiftOff.imageset/NightShift.png b/MTMR/Assets.xcassets/nightShiftOff.imageset/NightShift.png new file mode 100644 index 0000000..0cf7fd3 Binary files /dev/null and b/MTMR/Assets.xcassets/nightShiftOff.imageset/NightShift.png differ diff --git a/MTMR/Assets.xcassets/nightShiftOn.imageset/Contents.json b/MTMR/Assets.xcassets/nightShiftOn.imageset/Contents.json new file mode 100644 index 0000000..04be4c7 --- /dev/null +++ b/MTMR/Assets.xcassets/nightShiftOn.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "NightShiftEnabled.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/MTMR/Assets.xcassets/nightShiftOn.imageset/NightShiftEnabled.png b/MTMR/Assets.xcassets/nightShiftOn.imageset/NightShiftEnabled.png new file mode 100644 index 0000000..442bae1 Binary files /dev/null and b/MTMR/Assets.xcassets/nightShiftOn.imageset/NightShiftEnabled.png differ diff --git a/MTMR/CBridge/CBBlueLightClient.h b/MTMR/CBridge/CBBlueLightClient.h new file mode 100644 index 0000000..454b55c --- /dev/null +++ b/MTMR/CBridge/CBBlueLightClient.h @@ -0,0 +1,34 @@ +// +// CBBlueLightClient.h +// MTMR +// +// Created by Anton Palgunov on 28/08/2018. +// Copyright © 2018 Anton Palgunov. All rights reserved. +// + +#import + +typedef struct { + int hour; + int minute; +} Time; + +typedef struct { + Time fromTime; + Time toTime; +} Schedule; + +typedef struct { + BOOL active; + BOOL enabled; + BOOL sunSchedulePermitted; + int mode; + Schedule schedule; + unsigned long long disableFlags; +} Status; + +@interface CBBlueLightClient: NSObject +- (BOOL) setEnabled:(BOOL)enabled; +- (BOOL) setMode:(int)mode; +- (void) getBlueLightStatus: (Status *)status; +@end diff --git a/MTMR/CBridge/TouchBarPrivateApi-Bridging.h b/MTMR/CBridge/TouchBarPrivateApi-Bridging.h index 70634c7..158b8a4 100644 --- a/MTMR/CBridge/TouchBarPrivateApi-Bridging.h +++ b/MTMR/CBridge/TouchBarPrivateApi-Bridging.h @@ -9,7 +9,8 @@ #import "TouchBarPrivateApi.h" #import "TouchBarSupport.h" #import "DeprecatedCarbonAPI.h" -#include "LaunchAtLoginController.h" +#import "CBBlueLightClient.h" +#import "LaunchAtLoginController.h" NS_ASSUME_NONNULL_BEGIN diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index 4147ff2..ab5935b 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -177,7 +177,16 @@ class SupportedTypesHolder { parameters: [:] ) }, - ] + + "nightShift": {_ in + return ( + item: .nightShift(), + action: .none, + longAction: .none, + parameters: [:] + ) + } + ] static let sharedInstance = SupportedTypesHolder() @@ -211,6 +220,7 @@ enum ItemType: Decodable { case inputsource() case music(interval: Double) case groupBar(items: [BarItemDefinition]) + case nightShift() private enum CodingKeys: String, CodingKey { case type @@ -242,6 +252,7 @@ enum ItemType: Decodable { case inputsource case music case groupBar + case nightShift } init(from decoder: Decoder) throws { @@ -298,6 +309,9 @@ enum ItemType: Decodable { case .groupBar: let items = try container.decode([BarItemDefinition].self, forKey: .items) self = .groupBar(items: items) + + case .nightShift: + self = .nightShift() } } } diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index ce0bac3..3e6ad50 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -44,6 +44,8 @@ extension ItemType { return "com.toxblh.mtmr.music." case .groupBar(items: _): return "com.toxblh.mtmr.groupBar." + case .nightShift(items: _): + return "com.toxblh.mtmr.nightShift." } } @@ -279,6 +281,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = MusicBarItem(identifier: identifier, interval: interval) case .groupBar(items: let items): barItem = GroupBarItem(identifier: identifier, items: items) + case .nightShift(): + barItem = NightShiftBarItem(identifier: identifier) } if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem { diff --git a/MTMR/Widgets/NightShiftBarItem.swift b/MTMR/Widgets/NightShiftBarItem.swift new file mode 100644 index 0000000..471acf0 --- /dev/null +++ b/MTMR/Widgets/NightShiftBarItem.swift @@ -0,0 +1,50 @@ +// +// nightShift.swift +// MTMR +// +// Created by Anton Palgunov on 28/08/2018. +// Copyright © 2018 Anton Palgunov. All rights reserved. +// + +import Foundation + +class NightShiftBarItem: CustomButtonTouchBarItem { + private let nsclient = CBBlueLightClient() + + private var blueLightStatus: Status { + var status: Status = Status() + nsclient.getBlueLightStatus(&status) + return status + } + + private var isNightShiftEnabled: Bool { + return self.blueLightStatus.enabled.boolValue + } + + private func setNightShift(state: Bool) { + self.nsclient.setEnabled(state) + } + + init(identifier: NSTouchBarItem.Identifier) { + super.init(identifier: identifier, title: "") + self.isBordered = false + self.setWidth(value: 28) + + self.tapClosure = { [weak self] in self?.nightShiftAction() } + + self.refresh() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func nightShiftAction() { + self.setNightShift(state: !self.isNightShiftEnabled) + self.refresh() + } + + func refresh() { + self.image = isNightShiftEnabled ? #imageLiteral(resourceName: "nightShiftOn") : #imageLiteral(resourceName: "nightShiftOff") + } +}