Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Shifty.xcodeproj/xcshareddata/xcschemes/Shifty.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@
<EnvironmentVariables>
<EnvironmentVariable
key = "show_setup"
value = "false"
isEnabled = "YES">
value = "true"
isEnabled = "NO">
</EnvironmentVariable>
<EnvironmentVariable
key = "print_log"
value = "false"
isEnabled = "YES">
value = "true"
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
<LocationScenarioReference
Expand Down
51 changes: 33 additions & 18 deletions Shifty/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
updateMenuBarIcon()
setStatusToggle()

NightShiftManager.shared.onNightShiftChange {
self.updateMenuBarIcon()
}

statusItem.behavior = .terminationOnRemoval
statusItem.isVisible = true

Expand Down Expand Up @@ -212,29 +216,40 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func setStatusToggle() {
if prefs.bool(forKey: Keys.isStatusToggleEnabled) {
statusItem.menu = nil
if let button = statusItem.button {
button.action = #selector(statusBarButtonClicked)
button.sendAction(on: [.leftMouseUp, .leftMouseDown, .rightMouseUp, .rightMouseDown])
}
} else {
statusItem.menu = statusMenu
statusItem.menu = nil
if let button = statusItem.button {
button.action = #selector(statusBarButtonClicked)
button.sendAction(on: [.leftMouseUp, .leftMouseDown, .rightMouseUp, .rightMouseDown])
}
}

@objc func statusBarButtonClicked(sender: NSStatusBarButton) {
let event = NSApp.currentEvent!
guard let event = NSApp.currentEvent else { return }

if event.type == .rightMouseDown
|| event.type == .rightMouseUp
|| event.modifierFlags.contains(.control)
{
statusItem.menu = statusMenu
statusItem.button?.performClick(self)
statusItem.menu = nil
} else if event.type == .leftMouseUp {
statusItemClicked?()
if UserDefaults.standard.bool(forKey: Keys.isStatusToggleEnabled) {
if event.type == .rightMouseDown
|| event.type == .rightMouseUp
|| event.modifierFlags.contains(.control)
{
statusItem.menu = statusMenu
statusItem.button?.performClick(sender)
statusItem.menu = nil
} else if event.type == .leftMouseUp {
statusItemClicked?()
}
} else {
if event.type == .rightMouseUp
|| (event.type == .leftMouseUp
&& event.modifierFlags.contains(.control))
{
statusItemClicked?()
} else if event.type == .leftMouseDown
&& !event.modifierFlags.contains(.control)
{
statusItem.menu = statusMenu
statusItem.button?.performClick(sender)
statusItem.menu = nil
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions Shifty/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
12 changes: 12 additions & 0 deletions Shifty/Assets.xcassets/braveIcon.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "braveIcon.png",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
193 changes: 114 additions & 79 deletions Shifty/Base.lproj/Setup.storyboard

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions Shifty/BrowserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ enum SupportedBrowserID: BundleIdentifier {
case operaBeta = "com.operasoftware.OperaNext"
case operaDeveloper = "com.operasoftware.OperaDeveloper"

case brave = "com.brave.Browser"
case braveBeta = "com.brave.Browser.beta"

case vivaldi = "com.vivaldi.Vivaldi"

init?(_ rawValue: String) {
Expand Down Expand Up @@ -135,9 +138,9 @@ class BrowserManager {
}

private func fireNightShiftEvent() {
if RuleManager.shared.ruleForSubdomain == .enabled {
if RuleManager.shared.ruleForCurrentSubdomain == .enabled {
NightShiftManager.shared.respond(to: .nightShiftEnableRuleActivated)
} else if RuleManager.shared.disabledForDomain || RuleManager.shared.ruleForSubdomain == .disabled {
} else if RuleManager.shared.isDisabledForDomain || RuleManager.shared.ruleForCurrentSubdomain == .disabled {
NightShiftManager.shared.respond(to: .nightShiftDisableRuleActivated)
} else {
NightShiftManager.shared.respond(to: .nightShiftDisableRuleDeactivated)
Expand Down Expand Up @@ -254,7 +257,10 @@ class BrowserManager {

let tab: Tab?
switch browserID {
case .chrome, .chromeCanary, .chromium, .edge, .edgeBeta, .opera, .operaBeta, .operaDeveloper, .vivaldi:
case .chrome, .chromeCanary, .chromium,
.edge, .edgeBeta,
.opera, .operaBeta, .operaDeveloper,
.brave, .braveBeta, .vivaldi:
tab = window.activeTab
case .safari, .safariTechnologyPreview:
do {
Expand Down
15 changes: 11 additions & 4 deletions Shifty/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
<customObject id="YHE-ZD-Fjn" userLabel="StatusMenuController" customClass="StatusMenuController" customModule="Shifty" customModuleProvider="target">
<connections>
<outlet property="descriptionMenuItem" destination="zi6-lD-Hd5" id="DcD-QI-A3k"/>
<outlet property="disableAppMenuItem" destination="ycw-C3-JG6" id="w7g-sV-odc"/>
<outlet property="disableCurrentAppMenuItem" destination="ycw-C3-JG6" id="w7g-sV-odc"/>
<outlet property="disableCustomMenuItem" destination="VMR-D3-M60" id="Seo-8A-F4I"/>
<outlet property="disableDomainMenuItem" destination="JPo-kA-2ad" id="apR-hI-2G1"/>
<outlet property="disableHourMenuItem" destination="lLZ-zs-A2J" id="JBY-cw-D9b"/>
<outlet property="disableRunningAppMenuItem" destination="Bq2-vW-0D6" id="SiA-oU-QCK"/>
<outlet property="disableSubdomainMenuItem" destination="VgG-sQ-dcu" id="1hP-t7-xvX"/>
<outlet property="enableBrowserAutomationMenuItem" destination="Anz-oa-3rV" id="5yd-Qd-JzE"/>
<outlet property="moonIcon" destination="QFo-Gz-iR8" id="bkD-8o-fbd"/>
Expand Down Expand Up @@ -55,10 +56,10 @@
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="9KJ-0B-A9w"/>
<menuItem title="Disable for app" id="ycw-C3-JG6">
<menuItem title="Disable for current app" id="ycw-C3-JG6">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="disableForApp:" target="YHE-ZD-Fjn" id="Akx-Tu-eBm"/>
<action selector="disableForCurrentApp:" target="YHE-ZD-Fjn" id="5x5-Wn-Thl"/>
</connections>
</menuItem>
<menuItem title="Disable for Domain" hidden="YES" indentationLevel="1" id="JPo-kA-2ad">
Expand All @@ -79,6 +80,12 @@
<action selector="enableBrowserAutomation:" target="YHE-ZD-Fjn" id="KGQ-H9-T8g"/>
</connections>
</menuItem>
<menuItem title="Disable when app is running" id="Bq2-vW-0D6">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="disableForRunningApp:" target="YHE-ZD-Fjn" id="fZt-6e-AtZ"/>
</connections>
</menuItem>
<menuItem title="Disable for an hour" id="lLZ-zs-A2J">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
Expand Down Expand Up @@ -185,7 +192,7 @@
<connections>
<outlet property="shiftSlider" destination="qOz-qT-jic" id="RJ9-eC-U59"/>
</connections>
<point key="canvasLocation" x="144" y="324"/>
<point key="canvasLocation" x="144" y="343"/>
</customView>
</objects>
<resources>
Expand Down
21 changes: 11 additions & 10 deletions Shifty/NightShiftManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import SwiftLog
class NightShiftManager {
static let shared = NightShiftManager()
let client = CBBlueLightClient.shared

var nightShiftChangeListeners = [() -> Void]()

var userSet: UserSet = .notSet
var userInitiatedShift = false
Expand Down Expand Up @@ -81,16 +83,11 @@ class NightShiftManager {
}

self.updateDarkMode()

DispatchQueue.main.async {
let appDelegate = NSApplication.shared.delegate as! AppDelegate
appDelegate.updateMenuBarIcon()

let prefWindow = appDelegate.preferenceWindowController
let prefGeneral = prefWindow.viewControllers.compactMap { childViewController in
return childViewController as? PrefGeneralViewController
}.first
prefGeneral?.updateSchedule?()

for listener in self.nightShiftChangeListeners {
DispatchQueue.main.async {
listener()
}
}
}

Expand All @@ -108,6 +105,10 @@ class NightShiftManager {
}
}

func onNightShiftChange(_ listener: @escaping () -> Void) {
nightShiftChangeListeners.append(listener)
}

func updateDarkMode() {
if UserDefaults.standard.bool(forKey: Keys.isDarkModeSyncEnabled) {
let scheduledState = client.scheduledState
Expand Down
51 changes: 25 additions & 26 deletions Shifty/PrefGeneralViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ class PrefGeneralViewController: NSViewController, MASPreferencesViewController
@IBOutlet weak var toLabel: NSTextField!
@IBOutlet weak var customTimeStackView: NSStackView!

let prefs = UserDefaults.standard
var setStatusToggle: (() -> Void)?
var updateSchedule: (() -> Void)?
var updateDarkMode: (() -> Void)?

var appDelegate: AppDelegate!
var prefWindow: NSWindow!

Expand All @@ -72,6 +67,10 @@ class PrefGeneralViewController: NSViewController, MASPreferencesViewController

appDelegate = NSApplication.shared.delegate as? AppDelegate
prefWindow = appDelegate.preferenceWindowController.window

NightShiftManager.shared.onNightShiftChange {
self.updateSchedule()
}

//Hide True Tone settings on unsupported computers
if #available(macOS 10.14, *) {
Expand All @@ -86,31 +85,31 @@ class PrefGeneralViewController: NSViewController, MASPreferencesViewController
if !ProcessInfo().isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 10, minorVersion: 13, patchVersion: 0)) {
view.wantsLayer = false
}

updateSchedule = {
switch NightShiftManager.shared.schedule {
case .off:
self.schedulePopup.select(self.offMenuItem)
self.customTimeStackView.isHidden = true
case .custom(start: let startTime, end: let endTime):
self.schedulePopup.select(self.customMenuItem)
let startDate = Date(startTime)
let endDate = Date(endTime)

self.fromTimePicker.dateValue = startDate
self.toTimePicker.dateValue = endDate
self.customTimeStackView.isHidden = false
case .solar:
self.schedulePopup.select(self.sunMenuItem)
self.customTimeStackView.isHidden = true
}
}
}

override func viewWillAppear() {
super.viewWillAppear()

updateSchedule?()
updateSchedule()
}

func updateSchedule() {
switch NightShiftManager.shared.schedule {
case .off:
self.schedulePopup.select(self.offMenuItem)
self.customTimeStackView.isHidden = true
case .custom(start: let startTime, end: let endTime):
self.schedulePopup.select(self.customMenuItem)
let startDate = Date(startTime)
let endDate = Date(endTime)

self.fromTimePicker.dateValue = startDate
self.toTimePicker.dateValue = endDate
self.customTimeStackView.isHidden = false
case .solar:
self.schedulePopup.select(self.sunMenuItem)
self.customTimeStackView.isHidden = true
}
}

//MARK: IBActions
Expand Down Expand Up @@ -149,7 +148,7 @@ class PrefGeneralViewController: NSViewController, MASPreferencesViewController
if !UIElement.isProcessTrusted() {
logw("Accessibility permissions alert shown")

prefs.set(false, forKey: Keys.isWebsiteControlEnabled)
UserDefaults.standard.set(false, forKey: Keys.isWebsiteControlEnabled)
NSApp.runModal(for: AccessibilityWindow().window!)
}
} else {
Expand Down
35 changes: 4 additions & 31 deletions Shifty/PrefManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ enum Keys {
static let isWebsiteControlEnabled = "isWebsiteControlEnabled"
static let trueToneControl = "trueToneControl"
static let analyticsPermission = "fabricCrashlyticsPermission"
static let disabledApps = "disabledApps"
static let currentAppDisableRules = "disabledApps"
static let runningAppDisableRules = "disabledRunningApps"
static let browserRules = "browserRules"

static let toggleNightShiftShortcut = "toggleNightShiftShortcut"
Expand Down Expand Up @@ -55,40 +56,12 @@ class PrefManager {
Keys.isWebsiteControlEnabled: NSNumber(value: false),
Keys.trueToneControl: NSNumber(value: false),
Keys.analyticsPermission: NSNumber(value: false),
Keys.disabledApps: NSData(),
Keys.currentAppDisableRules: NSData(),
Keys.runningAppDisableRules: NSData(),
Keys.browserRules: NSData(),
Keys.hasSetupWindowShown: NSNumber(value: false)
] as [String : Any]

userDefaults.register(defaults: factoryDefaults)
}

func synchronize() {
userDefaults.synchronize()
}

func reset() {
userDefaults.removeObject(forKey: Keys.isAutoLaunchEnabled)
userDefaults.removeObject(forKey: Keys.isStatusToggleEnabled)
userDefaults.removeObject(forKey: Keys.isIconSwitchingEnabled)
userDefaults.removeObject(forKey: Keys.isDarkModeSyncEnabled)
userDefaults.removeObject(forKey: Keys.isWebsiteControlEnabled)
userDefaults.removeObject(forKey: Keys.trueToneControl)
userDefaults.removeObject(forKey: Keys.disabledApps)
userDefaults.removeObject(forKey: Keys.browserRules)
userDefaults.removeObject(forKey: Keys.toggleNightShiftShortcut)
userDefaults.removeObject(forKey: Keys.incrementColorTempShortcut)
userDefaults.removeObject(forKey: Keys.decrementColorTempShortcut)
userDefaults.removeObject(forKey: Keys.disableAppShortcut)
userDefaults.removeObject(forKey: Keys.disableDomainShortcut)
userDefaults.removeObject(forKey: Keys.disableSubdomainShortcut)
userDefaults.removeObject(forKey: Keys.disableHourShortcut)
userDefaults.removeObject(forKey: Keys.disableCustomShortcut)
userDefaults.removeObject(forKey: Keys.toggleTrueToneShortcut)
userDefaults.removeObject(forKey: Keys.toggleDarkModeShortcut)
userDefaults.removeObject(forKey: Keys.analyticsPermission)
userDefaults.removeObject(forKey: Keys.hasSetupWindowShown)

synchronize()
}
}
4 changes: 2 additions & 2 deletions Shifty/PrefShortcutsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ class PrefShortcutsViewController: NSViewController, MASPreferencesViewControlle

MASShortcutBinder.shared().bindShortcut(withDefaultsKey: Keys.disableAppShortcut) {
guard let menu = self.statusMenuController else { return }
if !menu.disableAppMenuItem.isHidden && menu.disableAppMenuItem.isEnabled {
self.statusMenuController?.disableForApp(self)
if !menu.disableCurrentAppMenuItem.isHidden && menu.disableCurrentAppMenuItem.isEnabled {
self.statusMenuController?.disableForCurrentApp(self)
} else {
NSSound.beep()
}
Expand Down
Loading
Loading