Skip to content

Free, open-source macOS cleaner. CleanMyMac alternative with zero telemetry. Native SwiftUI, scheduled auto-cleaning, Xcode/Homebrew/system cache cleanup. MIT licensed.

Fix/appearance system reset (#152)

Hebert Marchesi87c1a9cc11b6
diff --git a/PureMac.xcodeproj/project.pbxproj b/PureMac.xcodeproj/project.pbxproj
index 2085476..8ae097b 100644
--- a/PureMac.xcodeproj/project.pbxproj
+++ b/PureMac.xcodeproj/project.pbxproj
@@ -43,6 +43,7 @@
A549D8A39A9E5E70D91B90A6 /* Haptics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40A3F22FCEF534DE4A2CAD0E /* Haptics.swift */; };
A9C3A1F643C26930F442E729 /* OrphanSafetyPolicy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AE790496C936424EF98320A /* OrphanSafetyPolicy.swift */; };
ABDDD4F35102A39CC1A2C325 /* ConfettiView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0599AA604B0D6BA4F957537 /* ConfettiView.swift */; };
+ ABE3B80279483DB90B8EC1BC /* ThemeManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55CE234DB2DB54ED53A4EFD6 /* ThemeManagerTests.swift */; };
B51E5A95BB49A6C0F5273E21 /* FileSize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CE522B406791BCE905BC55B /* FileSize.swift */; };
B52938BBD11842631314543D /* CategoryDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 10B0AF194677EAC1D5568785 /* CategoryDetailView.swift */; };
B928E4369A3FC4369F014A29 /* SmartCareCoreArtwork.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAA389C4F50505795FB31A51 /* SmartCareCoreArtwork.swift */; };
@@ -109,6 +110,7 @@
46660271CFF167AB0FE7371D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
491771F923C93FD61A263893 /* AppLanguagePreferencesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppLanguagePreferencesTests.swift; sourceTree = "<group>"; };
4AE790496C936424EF98320A /* OrphanSafetyPolicy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrphanSafetyPolicy.swift; sourceTree = "<group>"; };
+ 55CE234DB2DB54ED53A4EFD6 /* ThemeManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeManagerTests.swift; sourceTree = "<group>"; };
5664D2BDAEAA9AE3A53DB364 /* PureMac.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PureMac.entitlements; sourceTree = "<group>"; };
5785762276FB5E3209C6DE4D /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
5A5C80929EE4A430272674BC /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Localizable.strings; sourceTree = "<group>"; };
@@ -185,6 +187,7 @@
752603285F7DBBA12BB3AA91 /* AppStateTests.swift */,
155CE1B8CDCCCA6F9FD028C1 /* LocalizationFilesTests.swift */,
C7D81BE7EA5DB17F4A0300AD /* SimulatorRuntimeSupportTests.swift */,
+ 55CE234DB2DB54ED53A4EFD6 /* ThemeManagerTests.swift */,
);
path = PureMacTests;
sourceTree = "<group>";
@@ -536,6 +539,7 @@
E2403D82F00D749C9AD4A6D4 /* AppStateTests.swift in Sources */,
CFA64F54CDBF8A4765E0068D /* LocalizationFilesTests.swift in Sources */,
F4F02F966001A1504C2B4D33 /* SimulatorRuntimeSupportTests.swift in Sources */,
+ ABE3B80279483DB90B8EC1BC /* ThemeManagerTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/PureMac/PureMacApp.swift b/PureMac/PureMacApp.swift
index 362c1b4..1b0e360 100644
--- a/PureMac/PureMacApp.swift
+++ b/PureMac/PureMacApp.swift
@@ -107,7 +107,6 @@ struct PureMacApp: App {
}
}
.environmentObject(theme)
- .preferredColorScheme(theme.appearance.colorScheme)
// Record the openWindow action so the menu-bar popover can reopen
// this window after it's been closed (the popover lives outside the
// scene graph and can't use openWindow itself).
diff --git a/PureMac/Views/Components/AppTheme.swift b/PureMac/Views/Components/AppTheme.swift
index c58ca6a..aab09ff 100644
--- a/PureMac/Views/Components/AppTheme.swift
+++ b/PureMac/Views/Components/AppTheme.swift
@@ -1,3 +1,4 @@
+import AppKit
import SwiftUI
/// User-overridable appearance setting that lives independently of the system
@@ -22,11 +23,11 @@ enum AppearanceMode: String, CaseIterable, Identifiable {
}
}
- var colorScheme: ColorScheme? {
+ var nsAppearance: NSAppearance? {
switch self {
case .system: return nil
- case .light: return .light
- case .dark: return .dark
+ case .light: return NSAppearance(named: .aqua)
+ case .dark: return NSAppearance(named: .darkAqua)
}
}
}
@@ -37,9 +38,23 @@ final class ThemeManager: ObservableObject {
@AppStorage("PureMac.Appearance") private var rawValue: String = AppearanceMode.system.rawValue
+ private init() { applyToApp() }
+
var appearance: AppearanceMode {
get { AppearanceMode(rawValue: rawValue) ?? .system }
- set { rawValue = newValue.rawValue; objectWillChange.send() }
+ set { rawValue = newValue.rawValue; objectWillChange.send(); applyToApp() }
+ }
+
+ /// The theme is driven through NSApp.appearance, not SwiftUI's
+ /// .preferredColorScheme: resetting preferredColorScheme back to nil
+ /// (System) inside a WindowGroup hosting a NavigationSplitView only
+ /// re-appearances the titlebar — the content's colorScheme environment
+ /// isn't re-evaluated until the window resigns key, so the body stays
+ /// stuck in the previous scheme while focused. The AppKit route applies
+ /// immediately and also themes windows outside the main scene
+ /// (Settings, menus, popovers).
+ func applyToApp() {
+ NSApplication.shared.appearance = appearance.nsAppearance
}
}
diff --git a/PureMacTests/ThemeManagerTests.swift b/PureMacTests/ThemeManagerTests.swift
new file mode 100644
index 0000000..7cbbd99
--- /dev/null
+++ b/PureMacTests/ThemeManagerTests.swift
@@ -0,0 +1,60 @@
+import AppKit
+import XCTest
+@testable import PureMac
+
+@MainActor
+final class ThemeManagerTests: XCTestCase {
+ private var originalRawValue: String?
+ private var originalMode: AppearanceMode = .system
+ private var originalApplicationAppearance: NSAppearance?
+
+ override func setUp() {
+ super.setUp()
+ originalRawValue = UserDefaults.standard.string(forKey: "PureMac.Appearance")
+ originalMode = ThemeManager.shared.appearance
+ originalApplicationAppearance = NSApp.appearance
+ }
+
+ override func tearDown() {
+ // Reset the singleton first: @AppStorage caches writes made through the
+ // wrapper, so changing UserDefaults directly is not enough to restore
+ // the value ThemeManager reads when applying the process-wide theme.
+ ThemeManager.shared.appearance = originalMode
+
+ // Preserve the exact defaults representation as well (including an
+ // absent or previously invalid value) and restore the AppKit override
+ // independently so this suite cannot leak appearance state.
+ if let originalRawValue {
+ UserDefaults.standard.set(originalRawValue, forKey: "PureMac.Appearance")
+ } else {
+ UserDefaults.standard.removeObject(forKey: "PureMac.Appearance")
+ }
+ NSApp.appearance = originalApplicationAppearance
+ super.tearDown()
+ }
+
+ func testDarkSelectionAppliesAppKitAppearance() {
+ ThemeManager.shared.appearance = .dark
+ XCTAssertEqual(NSApp.appearance?.name, .darkAqua)
+ }
+
+ func testLightSelectionAppliesAppKitAppearance() {
+ ThemeManager.shared.appearance = .light
+ XCTAssertEqual(NSApp.appearance?.name, .aqua)
+ }
+
+ /// Dark -> System must clear the app-level override entirely. The previous
+ /// .preferredColorScheme(nil) pipeline left the NavigationSplitView content
+ /// stuck in the old scheme until the window resigned key.
+ func testSystemSelectionClearsAppKitAppearance() {
+ ThemeManager.shared.appearance = .dark
+ ThemeManager.shared.appearance = .system
+ XCTAssertNil(NSApp.appearance)
+ }
+
+ func testSelectionPersistsAcrossManagerReads() {
+ ThemeManager.shared.appearance = .dark
+ XCTAssertEqual(UserDefaults.standard.string(forKey: "PureMac.Appearance"), "dark")
+ XCTAssertEqual(ThemeManager.shared.appearance, .dark)
+ }
+}