Skip to content

Commit

Permalink
Added preferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Sep 26, 2020
1 parent f0b97a7 commit 9c20ce2
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 28 deletions.
28 changes: 22 additions & 6 deletions Hot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
052FBC34251ECA8600C4322E /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 052FBC33251ECA8600C4322E /* IOKit.framework */; };
052FBC38251ECB8B00C4322E /* SMC.c in Sources */ = {isa = PBXBuildFile; fileRef = 052FBC37251ECB8B00C4322E /* SMC.c */; };
052FBCA3251EE43B00C4322E /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 052FBCA2251EE43B00C4322E /* PreferencesWindowController.swift */; };
052FBCA7251EE46700C4322E /* PreferencesWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 052FBCA5251EE46700C4322E /* PreferencesWindowController.xib */; };
057F8B00251D22360048F1E1 /* StatusIconTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 057F8AFF251D22360048F1E1 /* StatusIconTemplate.pdf */; };
057F8B0C251D273E0048F1E1 /* AboutWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 057F8B0B251D273E0048F1E1 /* AboutWindowController.swift */; };
057F8B11251D274C0048F1E1 /* NSApplication+LaunchServices.m in Sources */ = {isa = PBXBuildFile; fileRef = 057F8B10251D274C0048F1E1 /* NSApplication+LaunchServices.m */; };
Expand Down Expand Up @@ -62,6 +64,8 @@
052FBC32251EC69B00C4322E /* SMC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SMC.h; sourceTree = "<group>"; };
052FBC33251ECA8600C4322E /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; };
052FBC37251ECB8B00C4322E /* SMC.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SMC.c; sourceTree = "<group>"; };
052FBCA2251EE43B00C4322E /* PreferencesWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = "<group>"; };
052FBCA6251EE46700C4322E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/PreferencesWindowController.xib; sourceTree = "<group>"; };
057F8AFF251D22360048F1E1 /* StatusIconTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = StatusIconTemplate.pdf; sourceTree = "<group>"; };
057F8B0B251D273E0048F1E1 /* AboutWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AboutWindowController.swift; sourceTree = "<group>"; };
057F8B0E251D274C0048F1E1 /* Hot-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Hot-Bridging-Header.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -314,24 +318,26 @@
05AA4088251CF3F400106CEA /* Classes */ = {
isa = PBXGroup;
children = (
057F8B0F251D274C0048F1E1 /* NSApplication+LaunchServices.h */,
057F8B10251D274C0048F1E1 /* NSApplication+LaunchServices.m */,
057F8B0B251D273E0048F1E1 /* AboutWindowController.swift */,
05AA4079251CF3D200106CEA /* ApplicationDelegate.swift */,
05AA408C251CFA0600106CEA /* ThermalLog.swift */,
052FBC32251EC69B00C4322E /* SMC.h */,
052FBC37251ECB8B00C4322E /* SMC.c */,
057F8B0E251D274C0048F1E1 /* Hot-Bridging-Header.h */,
057F8B0F251D274C0048F1E1 /* NSApplication+LaunchServices.h */,
057F8B10251D274C0048F1E1 /* NSApplication+LaunchServices.m */,
052FBCA2251EE43B00C4322E /* PreferencesWindowController.swift */,
052FBC37251ECB8B00C4322E /* SMC.c */,
052FBC32251EC69B00C4322E /* SMC.h */,
05AA408C251CFA0600106CEA /* ThermalLog.swift */,
);
path = Classes;
sourceTree = "<group>";
};
05AA4089251CF3FC00106CEA /* UI */ = {
isa = PBXGroup;
children = (
057F8B13251D27650048F1E1 /* AboutWindowController.xib */,
057F8AFE251D22360048F1E1 /* Images */,
05AA407D251CF3D600106CEA /* MainMenu.xib */,
057F8B13251D27650048F1E1 /* AboutWindowController.xib */,
052FBCA5251EE46700C4322E /* PreferencesWindowController.xib */,
);
path = UI;
sourceTree = "<group>";
Expand Down Expand Up @@ -423,6 +429,7 @@
057F8B00251D22360048F1E1 /* StatusIconTemplate.pdf in Resources */,
057F8B1B251D2A970048F1E1 /* Icon.icns in Resources */,
057F8B15251D27650048F1E1 /* AboutWindowController.xib in Resources */,
052FBCA7251EE46700C4322E /* PreferencesWindowController.xib in Resources */,
05AA407F251CF3D600106CEA /* MainMenu.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -454,6 +461,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
052FBCA3251EE43B00C4322E /* PreferencesWindowController.swift in Sources */,
057F8B11251D274C0048F1E1 /* NSApplication+LaunchServices.m in Sources */,
057F8B0C251D273E0048F1E1 /* AboutWindowController.swift in Sources */,
05AA408D251CFA0600106CEA /* ThermalLog.swift in Sources */,
Expand All @@ -473,6 +481,14 @@
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
052FBCA5251EE46700C4322E /* PreferencesWindowController.xib */ = {
isa = PBXVariantGroup;
children = (
052FBCA6251EE46700C4322E /* Base */,
);
name = PreferencesWindowController.xib;
sourceTree = "<group>";
};
057F8B13251D27650048F1E1 /* AboutWindowController.xib */ = {
isa = PBXVariantGroup;
children = (
Expand Down
71 changes: 54 additions & 17 deletions Hot/Classes/ApplicationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,30 @@ import GitHubUpdates
@NSApplicationMain
class ApplicationDelegate: NSObject, NSApplicationDelegate
{
private var timer: Timer?
private var statusItem: NSStatusItem?
private var observations: [ NSKeyValueObservation ] = []
private var aboutWindowController: AboutWindowController?
private var timer: Timer?
private var statusItem: NSStatusItem?
private var observations: [ NSKeyValueObservation ] = []
private var aboutWindowController: AboutWindowController?
private var preferencesWindowController: PreferencesWindowController?


@IBOutlet private var menu: NSMenu!
@IBOutlet private var updater: GitHubUpdater!

@objc public dynamic var log = ThermalLog()
@objc public dynamic var startAtLogin = false
{
didSet
{
NSApp.setLoginItemEnabled( self.startAtLogin )
}
}
@objc public dynamic var log = ThermalLog()

func applicationDidFinishLaunching( _ notification: Notification )
{
self.startAtLogin = NSApp.isLoginItemEnabled()
if UserDefaults.standard.object( forKey: "LastLaunch" ) == nil
{
UserDefaults.standard.setValue( true, forKey: "automaticallyCheckForUpdates" )
UserDefaults.standard.setValue( true, forKey: "displayCPUTemperature" )
UserDefaults.standard.setValue( true, forKey: "colorizeStatusItemText" )
UserDefaults.standard.setValue( NSDate(), forKey: "LastLaunch" )
}

self.aboutWindowController = AboutWindowController()
self.preferencesWindowController = PreferencesWindowController()
self.statusItem = NSStatusBar.system.statusItem( withLength: NSStatusItem.variableLength )
self.statusItem?.button?.image = NSImage( named: "StatusIconTemplate" )
self.statusItem?.button?.imagePosition = .imageLeading
Expand All @@ -68,9 +71,22 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate

self.log.refresh()

DispatchQueue.main.asyncAfter( deadline: .now() + .seconds( 10 ) )
if UserDefaults.standard.bool( forKey: "automaticallyCheckForUpdates" )
{
self.updater.checkForUpdatesInBackground()
DispatchQueue.main.asyncAfter( deadline: .now() + .seconds( 2 ) )
{
self.updater.checkForUpdatesInBackground()
}
}

Timer.scheduledTimer( withTimeInterval: 3600, repeats: true )
{
_ in

if UserDefaults.standard.bool( forKey: "automaticallyCheckForUpdates" )
{
self.updater.checkForUpdatesInBackground()
}
}
}

Expand Down Expand Up @@ -98,6 +114,25 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate
window.makeKeyAndOrderFront( nil )
}

@IBAction public func showPreferencesWindow( _ sender: Any? )
{
guard let window = self.preferencesWindowController?.window else
{
NSSound.beep()

return
}

if window.isVisible == false
{
window.layoutIfNeeded()
window.center()
}

NSApp.activate( ignoringOtherApps: true )
window.makeKeyAndOrderFront( nil )
}

@IBAction public func checkForUpdates( _ sender: Any? )
{
self.updater.checkForUpdates( sender )
Expand Down Expand Up @@ -156,7 +191,9 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate

var title = ""

if let n1 = self.log.speedLimit?.intValue, let n2 = self.log.cpuTemperature?.intValue
if let n1 = self.log.speedLimit?.intValue,
let n2 = self.log.cpuTemperature?.intValue,
UserDefaults.standard.bool( forKey: "displayCPUTemperature" )
{
title = "\( n1 )% \( n2 )°"
}
Expand All @@ -171,7 +208,7 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate
}
else
{
let color = self.log.speedLimit?.intValue ?? 100 < 60 ? NSColor.orange : NSColor.white
let color = self.log.speedLimit?.intValue ?? 100 < 60 && UserDefaults.standard.bool( forKey: "colorizeStatusItemText" ) ? NSColor.orange : NSColor.white

self.statusItem?.button?.attributedTitle = NSAttributedString( string: title, attributes: [ .foregroundColor : color ] )
}
Expand Down
65 changes: 65 additions & 0 deletions Hot/Classes/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2020 Jean-David Gadina - www.xs-labs.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

import Cocoa

public class PreferencesWindowController: NSWindowController
{
@objc public dynamic var displayCPUTemperature = UserDefaults.standard.bool( forKey: "displayCPUTemperature" )
{
didSet
{
UserDefaults.standard.setValue( self.displayCPUTemperature, forKey: "displayCPUTemperature" )
}
}

@objc public dynamic var colorizeStatusItemText = UserDefaults.standard.bool( forKey: "colorizeStatusItemText" )
{
didSet
{
UserDefaults.standard.setValue( self.colorizeStatusItemText, forKey: "colorizeStatusItemText" )
}
}

@objc public dynamic var automaticallyCheckForUpdates = UserDefaults.standard.bool( forKey: "automaticallyCheckForUpdates" )
{
didSet
{
UserDefaults.standard.setValue( self.automaticallyCheckForUpdates, forKey: "automaticallyCheckForUpdates" )
}
}

@objc public dynamic var startAtLogin = NSApp.isLoginItemEnabled()
{
didSet
{
NSApp.setLoginItemEnabled( self.startAtLogin )
}
}

public override var windowNibName: NSNib.Name?
{
return "PreferencesWindowController"
}
}
4 changes: 2 additions & 2 deletions Hot/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<string>1.1.0</string>
<key>CFBundleVersion</key>
<string>13</string>
<string>18</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
5 changes: 2 additions & 3 deletions Hot/UI/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,9 @@
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="XCF-fq-sNA"/>
<menuItem title="Start at Login" id="JFH-mp-6PU">
<modifierMask key="keyEquivalentModifierMask"/>
<menuItem title="Preferences..." keyEquivalent="," id="JFH-mp-6PU">
<connections>
<binding destination="Voe-Tx-rLC" name="value" keyPath="self.startAtLogin" id="DEd-fD-Au5"/>
<action selector="showPreferencesWindow:" target="Voe-Tx-rLC" id="E99-rm-GGi"/>
</connections>
</menuItem>
<menuItem title="Check for Updates..." id="HLn-la-Ngo">
Expand Down
Loading

0 comments on commit 9c20ce2

Please sign in to comment.