From 7b49d94e9f3edc52ad478dc6cf95520f7a8b6c62 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 6 Oct 2016 15:27:24 -0700 Subject: [PATCH 1/6] Add systemPreferences.isInvertedColorScheme API --- atom/browser/api/atom_api_system_preferences.cc | 3 +++ spec/api-system-preferences-spec.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index 6d3897b197be..0c7cbe2fba8d 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -8,6 +8,7 @@ #include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/node_includes.h" #include "native_mate/dictionary.h" +#include "ui/gfx/color_utils.h" namespace atom { @@ -60,6 +61,8 @@ void SystemPreferences::BuildPrototype( .SetMethod("isSwipeTrackingFromScrollEventsEnabled", &SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled) #endif + .SetMethod("isInvertedColorScheme", + &color_utils::IsInvertedColorScheme) .SetMethod("isDarkMode", &SystemPreferences::IsDarkMode); } diff --git a/spec/api-system-preferences-spec.js b/spec/api-system-preferences-spec.js index 16992c31e112..03bff26ad82e 100644 --- a/spec/api-system-preferences-spec.js +++ b/spec/api-system-preferences-spec.js @@ -30,4 +30,10 @@ describe('systemPreferences module', function () { assert(languages.length > 0) }) }) + + describe('systemPreferences.isInvertedColorScheme()', function () { + it('returns a boolean', function () { + assert.equal(typeof systemPreferences.isInvertedColorScheme(), 'boolean') + }) + }) }) From 223e9d97a082a5dd056cf33bb4132b57772dfb1e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 6 Oct 2016 15:31:43 -0700 Subject: [PATCH 2/6] Doc systemPreferences.isInvertedColorScheme --- docs/api/system-preferences.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index c5ad763264ab..e39efac69f27 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -150,3 +150,8 @@ const green = color.substr(2, 2) // "bb" const blue = color.substr(4, 2) // "cc" const alpha = color.substr(6, 2) // "dd" ``` + +### `systemPreferences.isInvertedColorScheme()` _Windows_ + +Returns `Boolean` - `true` if an inverted color scheme, such as a high contrast +theme, is active, `false` otherwise. From 8b16b82ce9843c9e7b69f1616b3727b95b6ec2c0 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 7 Oct 2016 09:38:19 -0700 Subject: [PATCH 3/6] Add inverted-color-scheme-changed event --- .../api/atom_api_system_preferences.cc | 10 ++++--- .../browser/api/atom_api_system_preferences.h | 11 ++++++++ .../api/atom_api_system_preferences_win.cc | 27 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index 0c7cbe2fba8d..f7baf715ed84 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -16,9 +16,9 @@ namespace api { SystemPreferences::SystemPreferences(v8::Isolate* isolate) { Init(isolate); - #if defined(OS_WIN) +#if defined(OS_WIN) InitializeWindow(); - #endif +#endif } SystemPreferences::~SystemPreferences() { @@ -30,6 +30,10 @@ bool SystemPreferences::IsDarkMode() { } #endif +bool SystemPreferences::IsInvertedColorScheme() { + return color_utils::IsInvertedColorScheme(); +} + // static mate::Handle SystemPreferences::Create( v8::Isolate* isolate) { @@ -62,7 +66,7 @@ void SystemPreferences::BuildPrototype( &SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled) #endif .SetMethod("isInvertedColorScheme", - &color_utils::IsInvertedColorScheme) + &SystemPreferences::IsInvertedColorScheme) .SetMethod("isDarkMode", &SystemPreferences::IsDarkMode); } diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index e257dc518a31..1d3dbb977912 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -12,6 +12,10 @@ #include "base/values.h" #include "native_mate/handle.h" +#if defined(OS_WIN) +#include "ui/gfx/sys_color_change_listener.h" +#endif + namespace base { class DictionaryValue; } @@ -39,6 +43,8 @@ class SystemPreferences : public mate::EventEmitter { void InitializeWindow(); + void OnColorChanged(); + #elif defined(OS_MACOSX) using NotificationCallback = base::Callback< @@ -59,6 +65,7 @@ class SystemPreferences : public mate::EventEmitter { bool IsSwipeTrackingFromScrollEventsEnabled(); #endif bool IsDarkMode(); + bool IsInvertedColorScheme(); protected: explicit SystemPreferences(v8::Isolate* isolate); @@ -93,6 +100,10 @@ class SystemPreferences : public mate::EventEmitter { HWND window_; std::string current_color_; + + bool invertered_color_scheme_ = false; + + std::unique_ptr color_change_listener_; #endif DISALLOW_COPY_AND_ASSIGN(SystemPreferences); }; diff --git a/atom/browser/api/atom_api_system_preferences_win.cc b/atom/browser/api/atom_api_system_preferences_win.cc index 454c8ae24f7f..8772219a315a 100644 --- a/atom/browser/api/atom_api_system_preferences_win.cc +++ b/atom/browser/api/atom_api_system_preferences_win.cc @@ -19,6 +19,21 @@ const wchar_t kSystemPreferencesWindowClass[] = namespace api { +class SystemPreferencesColorChangeListener : + public gfx::SysColorChangeListener { + public: + explicit SystemPreferencesColorChangeListener(SystemPreferences* prefs): + prefs_(prefs) { + } + + void OnSysColorChange() { + prefs_->OnColorChanged(); + } + + private: + SystemPreferences* prefs_; +}; + bool SystemPreferences::IsAeroGlassEnabled() { return ui::win::IsAeroGlassEnabled(); } @@ -59,6 +74,10 @@ void SystemPreferences::InitializeWindow() { 0, WS_POPUP, 0, 0, 0, 0, 0, 0, instance_, 0); gfx::CheckWindowCreated(window_); gfx::SetWindowUserData(window_, this); + + color_change_listener_.reset( + new gfx::ScopedSysColorChangeListener( + new SystemPreferencesColorChangeListener(this))); } LRESULT CALLBACK SystemPreferences::WndProcStatic(HWND hwnd, @@ -88,6 +107,14 @@ LRESULT CALLBACK SystemPreferences::WndProc(HWND hwnd, return ::DefWindowProc(hwnd, message, wparam, lparam); } +void SystemPreferences::OnColorChanged() { + bool new_invertered_color_scheme = IsInvertedColorScheme(); + if (new_invertered_color_scheme != invertered_color_scheme_) { + Emit("inverted-color-scheme-changed", new_invertered_color_scheme); + invertered_color_scheme_ = new_invertered_color_scheme; + } +} + } // namespace api } // namespace atom From af27c62b07022d95a12ef2f098a1221919a9be24 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 7 Oct 2016 09:55:50 -0700 Subject: [PATCH 4/6] Doc inverted-color-scheme-changed event --- atom/browser/api/atom_api_system_preferences_win.cc | 2 +- docs/api/system-preferences.md | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences_win.cc b/atom/browser/api/atom_api_system_preferences_win.cc index 8772219a315a..1ba81550f91f 100644 --- a/atom/browser/api/atom_api_system_preferences_win.cc +++ b/atom/browser/api/atom_api_system_preferences_win.cc @@ -110,8 +110,8 @@ LRESULT CALLBACK SystemPreferences::WndProc(HWND hwnd, void SystemPreferences::OnColorChanged() { bool new_invertered_color_scheme = IsInvertedColorScheme(); if (new_invertered_color_scheme != invertered_color_scheme_) { - Emit("inverted-color-scheme-changed", new_invertered_color_scheme); invertered_color_scheme_ = new_invertered_color_scheme; + Emit("inverted-color-scheme-changed", new_invertered_color_scheme); } } diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index e39efac69f27..98fe63634b65 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -17,8 +17,15 @@ Returns: * `event` Event * `newColor` String - The new RGBA color the user assigned to be there system -accent color. + accent color. +### Event: 'inverted-color-scheme-changed' _Windows_ + +Returns: + +* `event` Event +* `invertedColorScheme` Boolean - `true` if a inverted color scheme, such as + a high contrast theme, is being used, `false` otherwise. ## Methods From 25bcc2dd0d55c015996a42742e265c5e90f15084 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 7 Oct 2016 09:59:53 -0700 Subject: [PATCH 5/6] :art: --- atom/browser/api/atom_api_system_preferences_win.cc | 8 ++++---- docs/api/system-preferences.md | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences_win.cc b/atom/browser/api/atom_api_system_preferences_win.cc index 1ba81550f91f..04e216b28eac 100644 --- a/atom/browser/api/atom_api_system_preferences_win.cc +++ b/atom/browser/api/atom_api_system_preferences_win.cc @@ -19,11 +19,11 @@ const wchar_t kSystemPreferencesWindowClass[] = namespace api { -class SystemPreferencesColorChangeListener : - public gfx::SysColorChangeListener { +class SystemPreferencesColorChangeListener + : public gfx::SysColorChangeListener { public: - explicit SystemPreferencesColorChangeListener(SystemPreferences* prefs): - prefs_(prefs) { + explicit SystemPreferencesColorChangeListener(SystemPreferences* prefs) + : prefs_(prefs) { } void OnSysColorChange() { diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 98fe63634b65..790e78c1499e 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -24,7 +24,7 @@ Returns: Returns: * `event` Event -* `invertedColorScheme` Boolean - `true` if a inverted color scheme, such as +* `invertedColorScheme` Boolean - `true` if an inverted color scheme, such as a high contrast theme, is being used, `false` otherwise. ## Methods From 38281f336421c44a4d88aa6c6ff65ab28fc8799e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 10 Oct 2016 10:20:51 -0700 Subject: [PATCH 6/6] Implement SysColorChangeListener directly on Windows --- .../api/atom_api_system_preferences.cc | 6 ++++- .../browser/api/atom_api_system_preferences.h | 14 +++++++---- .../api/atom_api_system_preferences_win.cc | 23 +++---------------- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index f7baf715ed84..96efe80c3244 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -14,7 +14,11 @@ namespace atom { namespace api { -SystemPreferences::SystemPreferences(v8::Isolate* isolate) { +SystemPreferences::SystemPreferences(v8::Isolate* isolate) +#if defined(OS_WIN) + : color_change_listener_(this) +#endif + { Init(isolate); #if defined(OS_WIN) InitializeWindow(); diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index 1d3dbb977912..1c00bfe29c4f 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -24,7 +24,11 @@ namespace atom { namespace api { -class SystemPreferences : public mate::EventEmitter { +class SystemPreferences : public mate::EventEmitter +#if defined(OS_WIN) + , public gfx::SysColorChangeListener +#endif + { public: static mate::Handle Create(v8::Isolate* isolate); @@ -43,8 +47,8 @@ class SystemPreferences : public mate::EventEmitter { void InitializeWindow(); - void OnColorChanged(); - + // gfx::SysColorChangeListener: + void OnSysColorChange() override; #elif defined(OS_MACOSX) using NotificationCallback = base::Callback< @@ -101,9 +105,9 @@ class SystemPreferences : public mate::EventEmitter { std::string current_color_; - bool invertered_color_scheme_ = false; + bool invertered_color_scheme_; - std::unique_ptr color_change_listener_; + gfx::ScopedSysColorChangeListener color_change_listener_; #endif DISALLOW_COPY_AND_ASSIGN(SystemPreferences); }; diff --git a/atom/browser/api/atom_api_system_preferences_win.cc b/atom/browser/api/atom_api_system_preferences_win.cc index 04e216b28eac..c157d5281b78 100644 --- a/atom/browser/api/atom_api_system_preferences_win.cc +++ b/atom/browser/api/atom_api_system_preferences_win.cc @@ -19,21 +19,6 @@ const wchar_t kSystemPreferencesWindowClass[] = namespace api { -class SystemPreferencesColorChangeListener - : public gfx::SysColorChangeListener { - public: - explicit SystemPreferencesColorChangeListener(SystemPreferences* prefs) - : prefs_(prefs) { - } - - void OnSysColorChange() { - prefs_->OnColorChanged(); - } - - private: - SystemPreferences* prefs_; -}; - bool SystemPreferences::IsAeroGlassEnabled() { return ui::win::IsAeroGlassEnabled(); } @@ -57,6 +42,8 @@ std::string SystemPreferences::GetAccentColor() { } void SystemPreferences::InitializeWindow() { + invertered_color_scheme_ = IsInvertedColorScheme(); + WNDCLASSEX window_class; base::win::InitializeWindowClass( kSystemPreferencesWindowClass, @@ -74,10 +61,6 @@ void SystemPreferences::InitializeWindow() { 0, WS_POPUP, 0, 0, 0, 0, 0, 0, instance_, 0); gfx::CheckWindowCreated(window_); gfx::SetWindowUserData(window_, this); - - color_change_listener_.reset( - new gfx::ScopedSysColorChangeListener( - new SystemPreferencesColorChangeListener(this))); } LRESULT CALLBACK SystemPreferences::WndProcStatic(HWND hwnd, @@ -107,7 +90,7 @@ LRESULT CALLBACK SystemPreferences::WndProc(HWND hwnd, return ::DefWindowProc(hwnd, message, wparam, lparam); } -void SystemPreferences::OnColorChanged() { +void SystemPreferences::OnSysColorChange() { bool new_invertered_color_scheme = IsInvertedColorScheme(); if (new_invertered_color_scheme != invertered_color_scheme_) { invertered_color_scheme_ = new_invertered_color_scheme;