From eaac67ac603d025664b80ad127f09c4f1ca119d8 Mon Sep 17 00:00:00 2001 From: Daniel Pham Date: Fri, 19 Feb 2016 12:40:41 -0500 Subject: [PATCH 1/5] :apple: Add 'isDarkModeEnabled' to app api --- atom/browser/api/atom_api_app.cc | 1 + atom/browser/browser.h | 3 +++ atom/browser/browser_mac.mm | 5 +++++ docs/api/app.md | 4 ++++ 4 files changed, 13 insertions(+) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index e54d7fee1769..53415c4d9c9a 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -370,6 +370,7 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( #if defined(OS_MACOSX) .SetMethod("hide", base::Bind(&Browser::Hide, browser)) .SetMethod("show", base::Bind(&Browser::Show, browser)) + .SetMethod("isDarkModeEnabled", base::Bind(&Browser::IsDarkModeEnabled, browser)) #endif #if defined(OS_WIN) .SetMethod("setUserTasks", diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 2c44eaa107bc..b2e1679b7fc1 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -83,6 +83,9 @@ class Browser : public WindowListObserver { // Show the application. void Show(); + // Check if Dark Mode enabled. + bool IsDarkModeEnabled(); + // Bounce the dock icon. enum BounceType { BOUNCE_CRITICAL = 0, diff --git a/atom/browser/browser_mac.mm b/atom/browser/browser_mac.mm index bc0be22dcae8..affeead255a2 100644 --- a/atom/browser/browser_mac.mm +++ b/atom/browser/browser_mac.mm @@ -26,6 +26,11 @@ void Browser::Show() { [[AtomApplication sharedApplication] unhide:nil]; } +bool Browser::IsDarkModeEnabled() { + NSString *mode = CFBridgingRelease(CFPreferencesCopyValue((CFStringRef)@"AppleInterfaceStyle", kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost)); + return [mode isEqualToString: @"Dark"]; +} + void Browser::AddRecentDocument(const base::FilePath& path) { NSString* path_string = base::mac::FilePathToNSString(path); if (!path_string) diff --git a/docs/api/app.md b/docs/api/app.md index bdf0a8a55870..3d0790a92946 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -461,6 +461,10 @@ if (browserOptions.transparent) { } ``` +### `app.isDarkModeEnabled()` _OS X_ + +This method returns `true` if Dark Mode is enabled, and `false` otherwise. + ### `app.commandLine.appendSwitch(switch[, value])` Append a switch (with optional `value`) to Chromium's command line. From c4049cb393cc6266e5d1cc34780621b8dad2ad26 Mon Sep 17 00:00:00 2001 From: Daniel Pham Date: Tue, 1 Mar 2016 15:05:44 -0500 Subject: [PATCH 2/5] :apple: Add 'dark-mode-changed' event to app api --- atom/browser/api/atom_api_app.cc | 9 ++++++++- atom/browser/api/atom_api_app.h | 4 ++++ atom/browser/browser.cc | 4 ++++ atom/browser/browser.h | 2 ++ atom/browser/browser_observer.h | 2 ++ atom/browser/mac/atom_application_delegate.mm | 7 +++++++ docs/api/app.md | 4 ++++ 7 files changed, 31 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 53415c4d9c9a..76a5f32a2450 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -282,6 +282,12 @@ void App::OnGpuProcessCrashed(base::TerminationStatus exit_code) { Emit("gpu-process-crashed"); } +#if defined(OS_MACOSX) +void App::OnDarkModeChanged() { + Emit("dark-mode-changed"); +} +#endif + base::FilePath App::GetPath(mate::Arguments* args, const std::string& name) { bool succeed = false; base::FilePath path; @@ -370,7 +376,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( #if defined(OS_MACOSX) .SetMethod("hide", base::Bind(&Browser::Hide, browser)) .SetMethod("show", base::Bind(&Browser::Show, browser)) - .SetMethod("isDarkModeEnabled", base::Bind(&Browser::IsDarkModeEnabled, browser)) + .SetMethod("isDarkModeEnabled", + base::Bind(&Browser::IsDarkModeEnabled, browser)) #endif #if defined(OS_WIN) .SetMethod("setUserTasks", diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index c59b9154df11..54d3f9dc7433 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -71,6 +71,10 @@ class App : public AtomBrowserClient::Delegate, // content::GpuDataManagerObserver: void OnGpuProcessCrashed(base::TerminationStatus exit_code) override; +#if defined(OS_MACOSX) + void OnDarkModeChanged() override; +#endif + // mate::Wrappable: mate::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate) override; diff --git a/atom/browser/browser.cc b/atom/browser/browser.cc index 7a2c22ea9d23..7fbe01633b0c 100644 --- a/atom/browser/browser.cc +++ b/atom/browser/browser.cc @@ -181,4 +181,8 @@ void Browser::OnWindowAllClosed() { FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWindowAllClosed()); } +void Browser::DarkModeChanged() { + FOR_EACH_OBSERVER(BrowserObserver, observers_, OnDarkModeChanged()); +} + } // namespace atom diff --git a/atom/browser/browser.h b/atom/browser/browser.h index b2e1679b7fc1..04b7a7257875 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -145,6 +145,8 @@ class Browser : public WindowListObserver { // Request basic auth login. void RequestLogin(LoginHandler* login_handler); + void DarkModeChanged(); + void AddObserver(BrowserObserver* obs) { observers_.AddObserver(obs); } diff --git a/atom/browser/browser_observer.h b/atom/browser/browser_observer.h index f6d76bc13fb3..0577712becd0 100644 --- a/atom/browser/browser_observer.h +++ b/atom/browser/browser_observer.h @@ -45,6 +45,8 @@ class BrowserObserver { // The browser requests HTTP login. virtual void OnLogin(LoginHandler* login_handler) {} + virtual void OnDarkModeChanged() {} + protected: virtual ~BrowserObserver() {} }; diff --git a/atom/browser/mac/atom_application_delegate.mm b/atom/browser/mac/atom_application_delegate.mm index 7662162ab618..0b9c60e7d261 100644 --- a/atom/browser/mac/atom_application_delegate.mm +++ b/atom/browser/mac/atom_application_delegate.mm @@ -24,6 +24,9 @@ // Don't add the "Enter Full Screen" menu item automatically. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSFullScreenMenuItemEverywhere"]; + // Add observer to monitor the system's Dark Mode theme. + [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil]; + atom::Browser::Get()->WillFinishLaunching(); } @@ -59,4 +62,8 @@ return flag; } +- (void)darkModeChanged:(NSNotification *)notify { + atom::Browser::Get()->DarkModeChanged(); +} + @end diff --git a/docs/api/app.md b/docs/api/app.md index 3d0790a92946..3469c6b108d5 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -228,6 +228,10 @@ app.on('login', function(event, webContents, request, authInfo, callback) { Emitted when the gpu process crashes. +### Event: 'dark-mode-changed' _OS X_ + +Emitted when the system's Dark Mode theme is toggled. + ## Methods The `app` object has the following methods: From b1eefbdcd93779c932be8b2a0cac7e342997bf79 Mon Sep 17 00:00:00 2001 From: Daniel Pham Date: Thu, 3 Mar 2016 23:48:30 -0500 Subject: [PATCH 3/5] Simplify the isDarkModeEnabled check --- atom/browser/browser_mac.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/browser/browser_mac.mm b/atom/browser/browser_mac.mm index affeead255a2..f5859df97aaa 100644 --- a/atom/browser/browser_mac.mm +++ b/atom/browser/browser_mac.mm @@ -27,7 +27,7 @@ void Browser::Show() { } bool Browser::IsDarkModeEnabled() { - NSString *mode = CFBridgingRelease(CFPreferencesCopyValue((CFStringRef)@"AppleInterfaceStyle", kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost)); + NSString *mode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"]; return [mode isEqualToString: @"Dark"]; } From 63294892f0be54713a2104646291a7e1a6eddb94 Mon Sep 17 00:00:00 2001 From: Daniel Pham Date: Thu, 3 Mar 2016 23:58:58 -0500 Subject: [PATCH 4/5] Rename dark-mode-changed to platform-theme-changed --- atom/browser/api/atom_api_app.cc | 4 ++-- atom/browser/api/atom_api_app.h | 2 +- atom/browser/browser.cc | 4 ++-- atom/browser/browser.h | 3 ++- atom/browser/browser_observer.h | 2 +- atom/browser/mac/atom_application_delegate.mm | 6 +++--- docs/api/app.md | 2 +- 7 files changed, 12 insertions(+), 11 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 76a5f32a2450..fc1afb6d35c4 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -283,8 +283,8 @@ void App::OnGpuProcessCrashed(base::TerminationStatus exit_code) { } #if defined(OS_MACOSX) -void App::OnDarkModeChanged() { - Emit("dark-mode-changed"); +void App::OnPlatformThemeChanged() { + Emit("platform-theme-changed"); } #endif diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index 54d3f9dc7433..3d4ac17adf75 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -72,7 +72,7 @@ class App : public AtomBrowserClient::Delegate, void OnGpuProcessCrashed(base::TerminationStatus exit_code) override; #if defined(OS_MACOSX) - void OnDarkModeChanged() override; + void OnPlatformThemeChanged() override; #endif // mate::Wrappable: diff --git a/atom/browser/browser.cc b/atom/browser/browser.cc index 7fbe01633b0c..e89f52283b3c 100644 --- a/atom/browser/browser.cc +++ b/atom/browser/browser.cc @@ -181,8 +181,8 @@ void Browser::OnWindowAllClosed() { FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWindowAllClosed()); } -void Browser::DarkModeChanged() { - FOR_EACH_OBSERVER(BrowserObserver, observers_, OnDarkModeChanged()); +void Browser::PlatformThemeChanged() { + FOR_EACH_OBSERVER(BrowserObserver, observers_, OnPlatformThemeChanged()); } } // namespace atom diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 04b7a7257875..8ea523700270 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -145,7 +145,8 @@ class Browser : public WindowListObserver { // Request basic auth login. void RequestLogin(LoginHandler* login_handler); - void DarkModeChanged(); + // Tell the application that plaform's theme changed. + void PlatformThemeChanged(); void AddObserver(BrowserObserver* obs) { observers_.AddObserver(obs); diff --git a/atom/browser/browser_observer.h b/atom/browser/browser_observer.h index 0577712becd0..da327eb90a02 100644 --- a/atom/browser/browser_observer.h +++ b/atom/browser/browser_observer.h @@ -45,7 +45,7 @@ class BrowserObserver { // The browser requests HTTP login. virtual void OnLogin(LoginHandler* login_handler) {} - virtual void OnDarkModeChanged() {} + virtual void OnPlatformThemeChanged() {} protected: virtual ~BrowserObserver() {} diff --git a/atom/browser/mac/atom_application_delegate.mm b/atom/browser/mac/atom_application_delegate.mm index 0b9c60e7d261..f4db929bf575 100644 --- a/atom/browser/mac/atom_application_delegate.mm +++ b/atom/browser/mac/atom_application_delegate.mm @@ -25,7 +25,7 @@ [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSFullScreenMenuItemEverywhere"]; // Add observer to monitor the system's Dark Mode theme. - [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil]; + [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(platformThemeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil]; atom::Browser::Get()->WillFinishLaunching(); } @@ -62,8 +62,8 @@ return flag; } -- (void)darkModeChanged:(NSNotification *)notify { - atom::Browser::Get()->DarkModeChanged(); +- (void)platformThemeChanged:(NSNotification *)notify { + atom::Browser::Get()->PlatformThemeChanged(); } @end diff --git a/docs/api/app.md b/docs/api/app.md index 3469c6b108d5..8cc9c6e37477 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -228,7 +228,7 @@ app.on('login', function(event, webContents, request, authInfo, callback) { Emitted when the gpu process crashes. -### Event: 'dark-mode-changed' _OS X_ +### Event: 'platform-theme-changed' _OS X_ Emitted when the system's Dark Mode theme is toggled. From 68b453770bc36539f14e19ca94f7726a238c3e79 Mon Sep 17 00:00:00 2001 From: Daniel Pham Date: Sun, 6 Mar 2016 10:55:47 -0500 Subject: [PATCH 5/5] :apple: Rename 'isDarkModeEnabled' to 'isDarkMode' --- atom/browser/api/atom_api_app.cc | 4 ++-- atom/browser/browser.h | 4 ++-- atom/browser/browser_mac.mm | 2 +- docs/api/app.md | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index fc1afb6d35c4..961637852bef 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -376,8 +376,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( #if defined(OS_MACOSX) .SetMethod("hide", base::Bind(&Browser::Hide, browser)) .SetMethod("show", base::Bind(&Browser::Show, browser)) - .SetMethod("isDarkModeEnabled", - base::Bind(&Browser::IsDarkModeEnabled, browser)) + .SetMethod("isDarkMode", + base::Bind(&Browser::IsDarkMode, browser)) #endif #if defined(OS_WIN) .SetMethod("setUserTasks", diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 8ea523700270..1d0b4aec8ba1 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -83,8 +83,8 @@ class Browser : public WindowListObserver { // Show the application. void Show(); - // Check if Dark Mode enabled. - bool IsDarkModeEnabled(); + // Check if the system is in Dark Mode. + bool IsDarkMode(); // Bounce the dock icon. enum BounceType { diff --git a/atom/browser/browser_mac.mm b/atom/browser/browser_mac.mm index f5859df97aaa..5988e6620438 100644 --- a/atom/browser/browser_mac.mm +++ b/atom/browser/browser_mac.mm @@ -26,7 +26,7 @@ void Browser::Show() { [[AtomApplication sharedApplication] unhide:nil]; } -bool Browser::IsDarkModeEnabled() { +bool Browser::IsDarkMode() { NSString *mode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"]; return [mode isEqualToString: @"Dark"]; } diff --git a/docs/api/app.md b/docs/api/app.md index 8cc9c6e37477..f18c978fadd2 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -465,9 +465,9 @@ if (browserOptions.transparent) { } ``` -### `app.isDarkModeEnabled()` _OS X_ +### `app.isDarkMode()` _OS X_ -This method returns `true` if Dark Mode is enabled, and `false` otherwise. +This method returns `true` if the system is in Dark Mode, and `false` otherwise. ### `app.commandLine.appendSwitch(switch[, value])`