Skip to content

Commit

Permalink
Menu bar font can now be chosen.
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Nov 1, 2021
1 parent 84f05b1 commit a390406
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 46 deletions.
36 changes: 32 additions & 4 deletions Hot/Classes/ApplicationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,20 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate
UserDefaults.standard.setValue( NSDate(), forKey: "LastLaunch" )
}

if UserDefaults.standard.object( forKey: "RefreshInterval" ) == nil
if UserDefaults.standard.object( forKey: "refreshInterval" ) == nil
{
UserDefaults.standard.setValue( 2, forKey: "RefreshInterval" )
UserDefaults.standard.setValue( 2, forKey: "refreshInterval" )
}

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
self.statusItem?.button?.font = NSFont.monospacedDigitSystemFont( ofSize: NSFont.smallSystemFontSize, weight: .light )
self.statusItem?.menu = self.menu

self.updateMenuFont()

let infoViewController = InfoViewController()
self.infoViewController = infoViewController
self.menu.item( withTag: 1 )?.view = infoViewController.view
Expand All @@ -86,6 +87,7 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate
UserDefaults.standard.addObserver( self, forKeyPath: "colorizeStatusItemText", options: [], context: nil )
UserDefaults.standard.addObserver( self, forKeyPath: "convertToFahrenheit", options: [], context: nil )
UserDefaults.standard.addObserver( self, forKeyPath: "hideStatusIcon", options: [], context: nil )
UserDefaults.standard.addObserver( self, forKeyPath: "fontName", options: [], context: nil )

if UserDefaults.standard.bool( forKey: "automaticallyCheckForUpdates" )
{
Expand All @@ -106,18 +108,44 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate
}
}

private func updateMenuFont()
{
let system = NSFont.monospacedDigitSystemFont( ofSize: NSFont.smallSystemFontSize, weight: .light )

guard let fontName = UserDefaults.standard.string( forKey: "fontName" ) else
{
self.statusItem?.button?.font = system

return
}

let parts = fontName.split( separator: " " )

guard parts.count >= 2, let last = parts.last, let size = Int( String( last ) ) else
{
self.statusItem?.button?.font = system

return
}

let name = parts.dropLast().joined( separator: " " )
self.statusItem?.button?.font = NSFont( name: name, size: CGFloat( size ) ) ?? system
}

override func observeValue( forKeyPath keyPath: String?, of object: Any?, change: [ NSKeyValueChangeKey : Any ]?, context: UnsafeMutableRawPointer? )
{
let keyPaths =
[
"displayCPUTemperature",
"colorizeStatusItemText",
"convertToFahrenheit",
"hideStatusIcon"
"hideStatusIcon",
"fontName"
]

if let keyPath = keyPath, let object = object as? NSObject, object == UserDefaults.standard && keyPaths.contains( keyPath )
{
self.updateMenuFont()
self.updateTitle()
self.updateSensors()
}
Expand Down
8 changes: 4 additions & 4 deletions Hot/Classes/InfoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class InfoViewController: NSViewController

deinit
{
UserDefaults.standard.removeObserver( self, forKeyPath: "RefreshInterval" )
UserDefaults.standard.removeObserver( self, forKeyPath: "refreshInterval" )
}

public override var nibName: NSNib.Name?
Expand All @@ -71,12 +71,12 @@ public class InfoViewController: NSViewController
}
}

UserDefaults.standard.addObserver( self, forKeyPath: "RefreshInterval", options: [], context: nil )
UserDefaults.standard.addObserver( self, forKeyPath: "refreshInterval", options: [], context: nil )
}

public override func observeValue( forKeyPath keyPath: String?, of object: Any?, change: [ NSKeyValueChangeKey : Any ]?, context: UnsafeMutableRawPointer? )
{
if let object = object as? NSObject, object == UserDefaults.standard && keyPath == "RefreshInterval"
if let object = object as? NSObject, object == UserDefaults.standard && keyPath == "refreshInterval"
{
self.setTimer()
}
Expand All @@ -90,7 +90,7 @@ public class InfoViewController: NSViewController
{
self.timer?.invalidate()

var interval = UserDefaults.standard.integer( forKey: "RefreshInterval" )
var interval = UserDefaults.standard.integer( forKey: "refreshInterval" )

if interval <= 0
{
Expand Down
48 changes: 46 additions & 2 deletions Hot/Classes/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,26 @@ public class PreferencesWindowController: NSWindowController
}
}

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

@objc public dynamic var fontName = UserDefaults.standard.string( forKey: "fontName" ) ?? "Default"
{
didSet
{
if self.fontName == "Default"
{
UserDefaults.standard.setValue( nil, forKey: "fontName" )
}
else
{
UserDefaults.standard.setValue( self.fontName, forKey: "fontName" )
}
}
}

Expand Down Expand Up @@ -99,4 +114,33 @@ public class PreferencesWindowController: NSWindowController
self.colorizeStatusItemTextLabel = "Colorize the status item text if the CPU speed limit is below 60%"
#endif
}

private var fontPanel: NSFontPanel?

@IBAction private func chooseFont( _ sender: Any? )
{
if let panel = self.fontPanel
{
panel.makeKeyAndOrderFront( sender )

return
}

let panel = NSFontManager.shared.fontPanel( true )
self.fontPanel = panel

panel?.makeKeyAndOrderFront( sender )
}

@IBAction private func resetFont( _ sender: Any? )
{
self.fontName = "Default"
}

@IBAction public func changeFont( _ sender: Any? )
{
let selected = NSFontManager.shared.selectedFont
let font = NSFontManager.shared.convert( selected ?? NSFont.systemFont( ofSize: NSFont.systemFontSize ) )
self.fontName = "\( font.fontName ) \( Int( font.pointSize ) )"
}
}
Loading

0 comments on commit a390406

Please sign in to comment.