Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Add a way to store and restore previous settings at startup #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Nocturnal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3E4FF22A25BB9EBB00E94F24 /* Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E4FF22925BB9EBB00E94F24 /* Settings.swift */; };
7D1B5C302395340E0061D0CF /* CustomTimeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D1B5C2F2395340E0061D0CF /* CustomTimeViewController.swift */; };
7D1B5C34239934440061D0CF /* Dimness.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D1B5C33239934440061D0CF /* Dimness.swift */; };
7D1B5C372399AE770061D0CF /* NSWindow+Fade.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D1B5C362399AE770061D0CF /* NSWindow+Fade.swift */; };
Expand Down Expand Up @@ -63,6 +64,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
3E4FF22925BB9EBB00E94F24 /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = "<group>"; };
7D1B5C2F2395340E0061D0CF /* CustomTimeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTimeViewController.swift; sourceTree = "<group>"; };
7D1B5C33239934440061D0CF /* Dimness.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dimness.swift; sourceTree = "<group>"; };
7D1B5C362399AE770061D0CF /* NSWindow+Fade.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "NSWindow+Fade.swift"; path = "Nocturnal/Application/Extensions/NSWindow+Fade.swift"; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -148,6 +150,7 @@
7DA5B1F8238BAF9500FE0597 /* NightShift.swift */,
7DF36EA6239E730C000CFB5A /* Shortcuts.swift */,
7D1B5C38239A39BB0061D0CF /* StateManager.swift */,
3E4FF22925BB9EBB00E94F24 /* Settings.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -347,6 +350,7 @@
7DA5B1F4238BAC8000FE0597 /* MenuController.swift in Sources */,
7D87458B238BFB5A00A676F1 /* DimnessWindowController.swift in Sources */,
7DF36EA7239E730C000CFB5A /* Shortcuts.swift in Sources */,
3E4FF22A25BB9EBB00E94F24 /* Settings.swift in Sources */,
7D1B5C302395340E0061D0CF /* CustomTimeViewController.swift in Sources */,
7D1B5C39239A39BB0061D0CF /* StateManager.swift in Sources */,
7DF36EAA239EFB29000CFB5A /* PreferencesViewController.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion Nocturnal/Application/Main/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
static var dimnessControllers = [] as [DimnessWindowController]

func applicationDidFinishLaunching(_: Notification) {
Settings.initialize()
Utils.verifyMacOsVersion()
Utils.verifySupportsNightShift()
initDimnessControllers()
NightShift.strength = 0
NightShift.strength = Settings.nightShiftStrength
NightShift.enable()
addNotificationObservers()
Shortcuts.setupShortcuts()
Expand Down
1 change: 0 additions & 1 deletion Nocturnal/Application/Menu/MenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ class MenuController: NSMenu, NSMenuDelegate {

@IBAction func quitClicked(_ sender: NSMenuItem) {
StateManager.isNocturnalEnabled = false
NightShift.strength = 1
NSApplication.shared.terminate(sender)
}
}
3 changes: 2 additions & 1 deletion Nocturnal/Utils/Dimness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
enum Dimness {
public static let maxStrength: Float = 0.9
private static let fadeDuration = 2.5
private static var dimnessStrength: CGFloat = 0
private static var dimnessStrength: CGFloat = CGFloat(Settings.dimnessStrength)
private static var isVisible: Bool = true

static var strength: Float {
Expand All @@ -21,6 +21,7 @@ enum Dimness {
for controller in AppDelegate.dimnessControllers {
controller.setAlphaValue(dimnessStrength)
}
Settings.dimnessStrength = newValue
}
}

Expand Down
1 change: 1 addition & 0 deletions Nocturnal/Utils/NightShift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum NightShift {
}
set {
client.setStrength(newValue, commit: true)
Settings.nightShiftStrength = newValue;
}
}

Expand Down
33 changes: 33 additions & 0 deletions Nocturnal/Utils/Settings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Settings.swift
// Nocturnal
//
// Created by Karanvir Panesar on 1/22/21.
// Copyright © 2021 Joshua Jon. All rights reserved.
//

import Foundation

enum Settings {
private static let defaults = UserDefaults.standard
private static let dimnessStrengthKey = "DimnessStrength"
private static let nightShiftStrengthKey = "NighShiftStrength"
private static let initializedKey = "Initialized"

static func initialize() {
if(!defaults.bool(forKey: initializedKey)) {
defaults.set(0.2, forKey: nightShiftStrengthKey)
defaults.set(0.2, forKey: dimnessStrengthKey)
defaults.set(true, forKey: initializedKey)
}
}

static var dimnessStrength: Float {
get { return defaults.float(forKey: dimnessStrengthKey) }
set { defaults.set(newValue, forKey: dimnessStrengthKey) }
}
static var nightShiftStrength: Float {
get { return defaults.float(forKey: nightShiftStrengthKey) }
set { defaults.set(newValue, forKey: nightShiftStrengthKey) }
}
}