1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #import <Cocoa/Cocoa.h>
8 #include "AppearanceOverride.h"
10 #include "mozilla/Preferences.h"
11 #include "mozilla/StaticPrefs_browser.h"
12 #include "mozilla/StaticPrefs_widget.h"
14 #include "nsXULAppAPI.h"
16 static void ToolbarThemePrefChanged(const char* aPref, void* aUserInfo);
18 @interface MOZGlobalAppearance ()
19 @property NSInteger toolbarTheme;
22 @implementation MOZGlobalAppearance
24 + (MOZGlobalAppearance*)sharedInstance {
25 static MOZGlobalAppearance* sInstance = nil;
27 sInstance = [[MOZGlobalAppearance alloc] init];
28 if (XRE_IsParentProcess()) {
29 mozilla::Preferences::RegisterCallbackAndCall(
30 &ToolbarThemePrefChanged,
32 mozilla::StaticPrefs::GetPrefName_browser_theme_toolbar_theme()));
38 + (NSSet*)keyPathsForValuesAffectingAppearance {
39 return [NSSet setWithObjects:@"toolbarTheme", nil];
42 - (NSAppearance*)appearance {
43 switch (self.toolbarTheme) { // Value for browser.theme.toolbar-theme pref
45 return [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
47 return [NSAppearance appearanceNamed:NSAppearanceNameAqua];
50 return nil; // nil means "no override".
54 - (void)setAppearance:(NSAppearance*)aAppearance {
58 - (NSApplication*)_app {
62 + (NSSet*)keyPathsForValuesAffectingEffectiveAppearance {
63 // Automatically notify any key-value observers of our effectiveAppearance
64 // property whenever the pref or the NSApp's effectiveAppearance change.
66 [NSSet setWithObjects:@"toolbarTheme", @"_app.effectiveAppearance", nil];
69 - (NSAppearance*)effectiveAppearance {
70 switch (self.toolbarTheme) { // Value for browser.theme.toolbar-theme pref
72 return [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
74 return [NSAppearance appearanceNamed:NSAppearanceNameAqua];
77 // Use the NSApp effectiveAppearance. This is the system appearance.
78 return NSApp.effectiveAppearance;
84 static void ToolbarThemePrefChanged(const char* aPref, void* aUserInfo) {
85 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
86 MOZ_RELEASE_ASSERT(NS_IsMainThread());
88 MOZGlobalAppearance.sharedInstance.toolbarTheme =
89 mozilla::StaticPrefs::browser_theme_toolbar_theme();