Skip to content
This repository has been archived by the owner on May 8, 2021. It is now read-only.

Commit

Permalink
- Fixed some issues
Browse files Browse the repository at this point in the history
- Fixed issue with preserving last intensity change to setMode from setEnabled
- You can see color temperature under status if you press option while click the menubar icon
  • Loading branch information
isaiasmatewos committed Aug 16, 2017
1 parent 8861abe commit c101935
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 26 deletions.
Binary file modified .DS_Store
Binary file not shown.
16 changes: 12 additions & 4 deletions CBBlueLightClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ typedef struct {
} Schedule;


typedef struct {
float minCCT;
float maxCCT;
float midCCT;
} CCTRange;


typedef struct {
char active;
Expand All @@ -33,14 +39,16 @@ typedef struct {
} StatusData;

@interface CBBlueLightClient : NSObject

+ (BOOL)supportsBlueLightReduction;

- (BOOL)setEnabled:(BOOL)arg1;
- (BOOL)setStrength:(float)arg1 withPeriod: (float) arg2 commit: (BOOL) arg3;
- (BOOL)setStrength:(float)arg1 commit: (BOOL)arg2;
- (BOOL)setMode:(int)arg1;
- (BOOL)getStrength:(float *)arg1;
- (BOOL)getCCT:(float *)arg1;
- (BOOL)setCCT:(float) arg1 commit: (BOOL) arg2;
- (BOOL)setEnabled:(BOOL)arg1;
- (BOOL)setEnabled:(BOOL)arg1 withOption: (int) arg2;
+ (BOOL)supportsBlueLightReduction;
- (BOOL)getCCTRange:(CCTRange *) arg1;
- (BOOL)getBlueLightStatus:(StatusData *)arg1;
@end

Expand Down
8 changes: 4 additions & 4 deletions Night Shift Control.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
6A0AE2E81F39E57E00FE2219 /* CoreBrightness.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A0AE2E71F39E57E00FE2219 /* CoreBrightness.framework */; };
6A43DF651F3B956000C57988 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6A43DF631F3B956000C57988 /* MainMenu.xib */; };
6A8981541F40AFCF001C0197 /* screenshot.png in Resources */ = {isa = PBXBuildFile; fileRef = 6A8981531F40AFCF001C0197 /* screenshot.png */; };
6ADEC3991F405BFE00897F6B /* Credits.html in Resources */ = {isa = PBXBuildFile; fileRef = 6ADEC3981F405BFE00897F6B /* Credits.html */; };
6ADEC39B1F40A01600897F6B /* README.md in Sources */ = {isa = PBXBuildFile; fileRef = 6ADEC39A1F40A01600897F6B /* README.md */; };
6AE5F71E1F4382C0002C391E /* Credits.html in Resources */ = {isa = PBXBuildFile; fileRef = 6AE5F71D1F4382C0002C391E /* Credits.html */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -30,8 +30,8 @@
6A0AE2EB1F39E63700FE2219 /* CBBlueLightClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CBBlueLightClient.h; sourceTree = "<group>"; };
6A43DF641F3B956000C57988 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
6A8981531F40AFCF001C0197 /* screenshot.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = screenshot.png; sourceTree = "<group>"; };
6ADEC3981F405BFE00897F6B /* Credits.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = Credits.html; sourceTree = "<group>"; };
6ADEC39A1F40A01600897F6B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
6AE5F71D1F4382C0002C391E /* Credits.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = Credits.html; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -69,7 +69,7 @@
6A0AE2D61F39E4A400FE2219 /* Night Shift Control */ = {
isa = PBXGroup;
children = (
6ADEC3981F405BFE00897F6B /* Credits.html */,
6AE5F71D1F4382C0002C391E /* Credits.html */,
6A0AE2D71F39E4A400FE2219 /* AppDelegate.swift */,
6A02006A1F3DD49700FF6985 /* string_resource.plist */,
6A43DF631F3B956000C57988 /* MainMenu.xib */,
Expand Down Expand Up @@ -158,10 +158,10 @@
buildActionMask = 2147483647;
files = (
6A0AE2DA1F39E4A400FE2219 /* Assets.xcassets in Resources */,
6ADEC3991F405BFE00897F6B /* Credits.html in Resources */,
6A43DF651F3B956000C57988 /* MainMenu.xib in Resources */,
6A8981541F40AFCF001C0197 /* screenshot.png in Resources */,
6A0200681F3DD49700FF6985 /* string_resource.plist in Resources */,
6AE5F71E1F4382C0002C391E /* Credits.html in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
56 changes: 46 additions & 10 deletions Night Shift Control/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
@IBOutlet weak var adjustWarmthTitleMenuItem: NSMenuItem!
@IBOutlet weak var statusTitleMenuItem: NSMenuItem!
@IBOutlet weak var nightShiftStatus: NSMenuItem!
@IBOutlet weak var currentColorTempMenuItem: NSMenuItem!
@IBOutlet weak var colorTemRangeMenuItem: NSMenuItem!
@IBOutlet weak var launchNightShiftPrefsMenuItem: NSMenuItem!
@IBOutlet weak var launchAboutDialogMenuItem: NSMenuItem!
@IBOutlet weak var quitMenuItem: NSMenuItem!
Expand Down Expand Up @@ -47,6 +49,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
var wasEnabled : Bool = false // Flag to store whether Night Shift was enabled in the last non blacklisted app
var blueLightNotAvailableALert = NSAlert() // Dialog to show that Night Shift is not supported by the current hardware
var disabledForApp : Bool = false
var lastMode : Int32 = 0
var colorTemprature : Float = 0
var colorTempratureRange : CCTRange = CCTRange.init()


func applicationDidFinishLaunching(_ aNotification: Notification) {
Expand Down Expand Up @@ -74,6 +79,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
blackListedApps = (defaults.object(forKey: blackListedAppsPrefKey) as? [String])! // Get blacklisted apps array from defaults
blueLightClient.getStrength(&blueLightStrength) // Get the current strength and store it in blueLightStrenght
blueLightClient.getBlueLightStatus(&blueLightStatus) // Get the current status of blue light and store it in blueLightStatus
blueLightClient.getCCT(&colorTemprature)
blueLightClient.getCCTRange(&colorTempratureRange)

lastMode = blueLightStatus.mode


// We add a notification when an application becomes active
applicationActiveObserver = self.notificationCenter.addObserver(forName: NSNotification.Name.NSWorkspaceDidActivateApplication, object: nil, queue: OperationQueue.main) {
Expand All @@ -83,6 +93,20 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
print("Front app is --> \(self.currentApp.localizedName!)")
}

NSEvent.addLocalMonitorForEvents(matching: NSEventMask.any) { nsevent in
if nsevent.modifierFlags.contains(NSEventModifierFlags.option) {
self.colorTemRangeMenuItem.isHidden = false
self.currentColorTempMenuItem.isHidden = false
} else {
self.colorTemRangeMenuItem.isHidden = true
self.currentColorTempMenuItem.isHidden = true
}



return nsevent
}


let icon = NSImage(named: "statusicon")
icon?.isTemplate = true // best for dark mode
Expand All @@ -95,6 +119,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
disableForAppMenuItem.title = String(format: "disable_ns_for_app".localized, (NSWorkspace.shared().frontmostApplication?.localizedName)!)
statusTitleMenuItem.title = "status_title".localized
adjustWarmthTitleMenuItem.title = "adjust_warmth_title".localized
currentColorTempMenuItem.title = String(format: "current_color_temprature".localized, String(Int(colorTemprature)))
colorTemRangeMenuItem.title = String(format: "color_temprature_range".localized, String(Int(colorTempratureRange.minCCT)), String(Int(colorTempratureRange.maxCCT)))

launchNightShiftPrefsMenuItem.title = "open_display_pref_pane".localized
launchAboutDialogMenuItem.title = "about".localized
quitMenuItem.title = "quit".localized
Expand Down Expand Up @@ -153,14 +180,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {

@IBAction func onNightShiftWarmSliderChanged(_ sender: NSSlider) {
// Called when the warmth slider changed value
if sender.integerValue == 0 {
// If the slider value is zero we turn off NightShift alltogether
disabledNightShift(forApp: false)
enableNightShiftMenuItem.state = 0
} else {
enableNightShiftMenuItem.state = 1
blueLightClient.setStrength(sender.floatValue/100, commit: true)
}
blueLightClient.setStrength(sender.floatValue/100, commit: true)
blueLightClient.getCCT(&colorTemprature)
currentColorTempMenuItem.title = String(format: "current_color_temprature".localized, String(Int(colorTemprature)))
}

@IBAction func launchDisplaysPrefPaneMenuItemClicked(_ sender: NSMenuItem) {
Expand Down Expand Up @@ -209,13 +231,16 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
// If it hasn't already been disabled, disable Night Shift
disabledNightShift(forApp: true)
}

// Changes the title to "Enable for {Black Listed App}"
disableForAppMenuItem.title = String(format: "enable_ns_for_app".localized, self.currentApp.localizedName!)
} else {
// Non black listed app
if disabledForApp {
// Enable it if it has been disabled for a specific app
enableNightShift()
} else {
blueLightClient.setMode(lastMode)
}
// Changes the title to "Disable for {Black Listed App}"
disableForAppMenuItem.title = String(format: "disable_ns_for_app".localized, self.currentApp.localizedName!)
Expand All @@ -225,13 +250,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
func enableNightShift() {
blueLightClient.setEnabled(true)
disabledForApp = false
// We are restoring the last mode
blueLightClient.setMode(lastMode)
print("Current mode is \(lastMode)")
}

func disabledNightShift(forApp: Bool) {
blueLightClient.getBlueLightStatus(&blueLightStatus)
disabledForApp = forApp && blueLightStatus.enabled == 1
blueLightClient.setEnabled(false)

lastMode = blueLightStatus.mode
blueLightClient.setMode(0)
print("Current mode is \(lastMode)")
}


Expand All @@ -241,6 +270,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {


blueLightClient.getBlueLightStatus(&blueLightStatus) // Get the current blue light status
blueLightClient.getCCTRange(&colorTempratureRange)
blueLightClient.getCCT(&colorTemprature)
blueLightClient.getStrength(&blueLightStrength) // Get the current blue light strength

wasEnabled = blueLightStatus.enabled == 1
Expand Down Expand Up @@ -324,7 +355,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
// Changed the title to "Enabled {fromTime} to {toTime}"
nightShiftStatus.title = String(format:"custom_time_ns_status".localized, fromTime, toTime )
enableNightShiftMenuItem.title = "turn_ns_on_until_to_tomorrow".localized

}

currentColorTempMenuItem.title = String(format: "current_color_temprature".localized, String(Int(colorTemprature)))
colorTemRangeMenuItem.title = String(format: "color_temprature_range".localized, String(Int(colorTempratureRange.minCCT)), String(Int(colorTempratureRange.maxCCT)))

}

}
Expand Down
8 changes: 8 additions & 0 deletions Night Shift Control/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Night_Shift_Control" customModuleProvider="target">
<connections>
<outlet property="adjustWarmthTitleMenuItem" destination="doq-fn-spb" id="INE-Lj-tDk"/>
<outlet property="colorTemRangeMenuItem" destination="zrw-Ce-PoS" id="7yE-dD-yu5"/>
<outlet property="currentColorTempMenuItem" destination="iF3-Ov-kQf" id="SJB-1h-9wg"/>
<outlet property="disableForAppMenuItem" destination="zwE-ed-i7I" id="JVt-7D-i0b"/>
<outlet property="enableNightShiftMenuItem" destination="peK-6b-QRY" id="g6Q-yq-1fx"/>
<outlet property="launchAboutDialogMenuItem" destination="XdH-KK-hDM" id="MNC-dP-v5A"/>
Expand Down Expand Up @@ -59,6 +61,12 @@
<menuItem title="Night Shift Status" id="7kE-Mb-YJf">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Color temprature" hidden="YES" enabled="NO" id="iF3-Ov-kQf">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Range" hidden="YES" enabled="NO" id="zrw-Ce-PoS">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="4Io-SY-1Wz"/>
<menuItem title="Night Shift Preferences..." id="uSx-ud-boM" userLabel="Launch Night Shift Prefs">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down
17 changes: 9 additions & 8 deletions Night Shift Control/Credits.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
<head>
<title>Credits</title>
<style type="text/css">
#main {
font-family: Helvatica Neue, sans-serif;
width: 500px;
height: 100%;
body {
font-family: sans-serif;
width: auto;
height: auto;
max-width: 100%;
margin: 1px auto;
display: block;
margin-top: 20px;
margin-bottom: 20px;
}
p, h3 {
p {
text-align: center;
}
</style>
</head>
<body>
<div id="main">
<h3>Credits</h3>
<p>Based on the CoreBrightness private API exposed by <strong>Eric Lanini</strong> in his <a href="https://github.com/elanini/NightShifter/">github repository.</a></p>
<p><strong>GitHub:</strong> <a href="https://github.com/isaiasmatewos/night-shift-control">https://github.com/isaiasmatewos/night-shift-control</a></p>
<p><strong>Email:</strong> <a href="mailto:[email protected]">[email protected]</a> </p>
<p>GitHub: <a href="https://github.com/isaiasmatewos/night-shift-control">https://github.com/isaiasmatewos/night-shift-control</a></p>
<p>Email: <a href="mailto:[email protected]">[email protected]</a> </p>
</div>
</body>
</html>
14 changes: 14 additions & 0 deletions Night Shift Control/en.lproj/string_resource.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>color_temprature_range</key>
<dict>
<key>value</key>
<string>Range: %@K to %@K</string>
<key>comment</key>
<string>Show the color temprature range from min to max when option key is pressed</string>
</dict>
<key>current_color_temprature</key>
<dict>
<key>value</key>
<string>Color Temprature: %@K</string>
<key>comment</key>
<string>Show the current color temprature when option key is pressed </string>
</dict>
<key>ns_not_supported_os_version_dialog_info</key>
<dict>
<key>value</key>
Expand Down

0 comments on commit c101935

Please sign in to comment.