Skip to content

Commit

Permalink
Added support for system appearance
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamik423 committed Feb 16, 2022
1 parent 880f7f4 commit d827da6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
Binary file not shown.
14 changes: 10 additions & 4 deletions Padbury Clock Revived/ClockView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ final class ClockView: ScreenSaverView {

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// ClockView.shared = self
}

// MARK: - Configuration
Expand All @@ -40,24 +39,31 @@ final class ClockView: ScreenSaverView {
var attributes: [NSAttributedString.Key: Any] = [:]

var lastUsedNightTimeMode: Bool = false
var sytemWasDarkMode: Bool = false

func setup(force: Bool = false) {
// Get the current hour to determine conditions for night mode
let currentHour = Calendar.current.component(.hour, from: Date())
let useNightTimeMode = preferences.nightTimeMode && (currentHour >= (10 + 12) || currentHour < 6)
let nightTimeModeChanged = lastUsedNightTimeMode != useNightTimeMode
lastUsedNightTimeMode = useNightTimeMode

// Compute dark mode
let systemIsDarkMode = effectiveAppearance.bestMatch(from: [.darkAqua, .vibrantDark]) == .darkAqua
let darkModeChanged = systemIsDarkMode != sytemWasDarkMode
sytemWasDarkMode = systemIsDarkMode

// Don't run the setup for every frame, only for the first time or when explicitly asked to
// or when the night time mode changed
if hasSetup && !force && !nightTimeModeChanged { return }
// or when the night time mode conditions or dark mode changed
if hasSetup && !(force || nightTimeModeChanged || darkModeChanged) { return }
hasSetup = true

// Set the background and foreground color variables to use later
if useNightTimeMode {
backgroundColor = .black
// Red night time mode color, picked from the original
foregroundColor = NSColor(red: 255 / 255, green: 59 / 255, blue: 48 / 255, alpha: 1)
} else if preferences.darkTheme {
} else if preferences.appearance == .dark || preferences.appearance == .system && systemIsDarkMode {
backgroundColor = .black
foregroundColor = .white
} else { // light theme
Expand Down
25 changes: 13 additions & 12 deletions Padbury Clock Revived/ConfigureSheet.xib
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="ConfigureSheetController" customModule="Padbury_Clock_Revived" customModuleProvider="target">
<connections>
<outlet property="darkThemeCheckbox" destination="S00-ph-A7z" id="O8B-jv-brD"/>
<outlet property="appearanceSelector" destination="GTP-Pj-lTg" id="6wV-Kz-gLW"/>
<outlet property="fontSelector" destination="zfh-nJ-hc4" id="sgj-ET-U7i"/>
<outlet property="fontWeightSelector" destination="ZPr-3s-mEV" id="Lvz-Is-N9l"/>
<outlet property="mainScreenCheckbox" destination="1UD-4O-M08" id="QDq-cb-heL"/>
Expand All @@ -34,7 +34,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<gridView xPlacement="leading" yPlacement="fill" rowAlignment="firstBaseline" rowSpacing="16" translatesAutoresizingMaskIntoConstraints="NO" id="gJC-Fu-Ran">
<rect key="frame" x="16" y="37" width="430" height="307"/>
<rect key="frame" x="16" y="33" width="430" height="311"/>
<rows>
<gridRow yPlacement="top" rowAlignment="firstBaseline" id="qR4-PX-gJf"/>
<gridRow yPlacement="top" rowAlignment="firstBaseline" id="hNR-Sn-yK5"/>
Expand All @@ -49,7 +49,7 @@
<gridCells>
<gridCell row="qR4-PX-gJf" column="zi3-Ws-PdH" id="0fO-xp-vHq">
<textField key="contentView" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="nvf-RD-QbQ">
<rect key="frame" x="57" y="291" width="50" height="16"/>
<rect key="frame" x="57" y="294" width="50" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Theme:" id="LXO-Ih-MrQ">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -59,18 +59,19 @@
</gridCell>
<gridCell row="qR4-PX-gJf" column="Ci0-4x-MqC" id="Wxv-Fj-hWb">
<stackView key="contentView" distribution="fill" orientation="vertical" alignment="leading" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="sea-gl-kUy">
<rect key="frame" x="111" y="246" width="319" height="61"/>
<rect key="frame" x="111" y="246" width="319" height="65"/>
<subviews>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="S00-ph-A7z">
<rect key="frame" x="-2" y="44" width="100" height="18"/>
<buttonCell key="cell" type="check" title="Dark Theme" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="lIc-rj-tyA">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="GTP-Pj-lTg">
<rect key="frame" x="-3" y="41" width="39" height="25"/>
<popUpButtonCell key="cell" type="push" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" id="VHP-lP-KjH">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
<menu key="menu" id="a6L-EO-Mvl"/>
</popUpButtonCell>
<connections>
<action selector="toggledCheckbox:" target="-2" id="GfI-fe-sKG"/>
<action selector="toggledCheckbox:" target="-2" id="ULQ-J9-qLO"/>
</connections>
</button>
</popUpButton>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Amm-Fe-f9G">
<rect key="frame" x="-2" y="20" width="130" height="18"/>
<buttonCell key="cell" type="check" title="Night Time Mode" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="vLg-7I-fzF">
Expand Down
11 changes: 7 additions & 4 deletions Padbury Clock Revived/ConfigureSheetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ConfigureSheetController: NSObject {
// Outlets for the checkboxes to use
@IBOutlet var versionStringLabel: NSTextField!

@IBOutlet var darkThemeCheckbox: NSButton!
@IBOutlet var appearanceSelector: NSPopUpButton!
@IBOutlet var nightTimeModeCheckbox: NSButton!
@IBOutlet var twentyfourHoursCheckbox: NSButton!
@IBOutlet var showSecondsCheckbox: NSButton!
Expand Down Expand Up @@ -46,8 +46,12 @@ class ConfigureSheetController: NSObject {
// Continue only if the preferences can be loaded
guard let preferences = Preferences.shared else { return }

// Remove all options from the appearance selectorand add the ones corresponding to the available ones
appearanceSelector.removeAllItems()
appearanceSelector.addItems(withTitles: Appearance.allCases.map({ $0.title }))
appearanceSelector.selectItem(withTitle: preferences.appearance.title)

// Set the checkboxes according to the settings
darkThemeCheckbox.state = preferences.darkTheme ? .on : .off
nightTimeModeCheckbox.state = preferences.nightTimeMode ? .on : .off
twentyfourHoursCheckbox.state = preferences.useAmPm ? .off : .on
showSecondsCheckbox.state = preferences.showSeconds ? .on : .off
Expand Down Expand Up @@ -88,8 +92,7 @@ class ConfigureSheetController: NSObject {

// Continue only if the preferences can be loaded correctly
guard let preferences = Preferences.shared else { return }

preferences.darkTheme = darkThemeCheckbox.state == .on
preferences.appearance = Appearance.titled(appearanceSelector.titleOfSelectedItem ?? "") ?? .dark
preferences.nightTimeMode = nightTimeModeCheckbox.state == .on
preferences.useAmPm = twentyfourHoursCheckbox.state == .off
preferences.showSeconds = showSecondsCheckbox.state == .on
Expand Down
31 changes: 27 additions & 4 deletions Padbury Clock Revived/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class Preferences: NSObject {
}
}

var darkTheme: Bool {
var appearance: Appearance {
// Should the dark theme be used
get { return (defaults.value(forKey: "DarkTheme") as? Bool) ?? true }
get { return Appearance(rawValue: defaults.value(forKey: "appearance") as? String ?? "") ?? .dark }
set {
defaults.setValue(newValue, forKey: "DarkTheme")
defaults.setValue(newValue.rawValue, forKey: "appearance")
defaults.synchronize()
}
}
Expand Down Expand Up @@ -125,7 +125,30 @@ class Preferences: NSObject {
}
}

// MARK: - Supported Fonts ENum
// MARK: - Dark Mode Enum

enum Appearance: String, CaseIterable {
case dark
case light
case system

var title: String {
switch self {
case .dark:
return "Dark"
case .light:
return "Light"
case .system:
return "System"
}
}

static func titled(_ title: String) -> Appearance? {
return Appearance.allCases.first(where: { $0.title == title })
}
}

// MARK: - Supported Fonts Enum

enum SupportedFont: String, CaseIterable {
// Enum of the supported fonts
Expand Down

0 comments on commit d827da6

Please sign in to comment.