diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index 6d3897b197be..96efe80c3244 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -8,16 +8,21 @@ #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 { 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) +#if defined(OS_WIN) InitializeWindow(); - #endif +#endif } SystemPreferences::~SystemPreferences() { @@ -29,6 +34,10 @@ bool SystemPreferences::IsDarkMode() { } #endif +bool SystemPreferences::IsInvertedColorScheme() { + return color_utils::IsInvertedColorScheme(); +} + // static mate::Handle SystemPreferences::Create( v8::Isolate* isolate) { @@ -60,6 +69,8 @@ void SystemPreferences::BuildPrototype( .SetMethod("isSwipeTrackingFromScrollEventsEnabled", &SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled) #endif + .SetMethod("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..1c00bfe29c4f 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; } @@ -20,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); @@ -39,6 +47,8 @@ class SystemPreferences : public mate::EventEmitter { void InitializeWindow(); + // gfx::SysColorChangeListener: + void OnSysColorChange() override; #elif defined(OS_MACOSX) using NotificationCallback = base::Callback< @@ -59,6 +69,7 @@ class SystemPreferences : public mate::EventEmitter { bool IsSwipeTrackingFromScrollEventsEnabled(); #endif bool IsDarkMode(); + bool IsInvertedColorScheme(); protected: explicit SystemPreferences(v8::Isolate* isolate); @@ -93,6 +104,10 @@ class SystemPreferences : public mate::EventEmitter { HWND window_; std::string current_color_; + + bool invertered_color_scheme_; + + 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 454c8ae24f7f..c157d5281b78 100644 --- a/atom/browser/api/atom_api_system_preferences_win.cc +++ b/atom/browser/api/atom_api_system_preferences_win.cc @@ -42,6 +42,8 @@ std::string SystemPreferences::GetAccentColor() { } void SystemPreferences::InitializeWindow() { + invertered_color_scheme_ = IsInvertedColorScheme(); + WNDCLASSEX window_class; base::win::InitializeWindowClass( kSystemPreferencesWindowClass, @@ -88,6 +90,14 @@ LRESULT CALLBACK SystemPreferences::WndProc(HWND hwnd, return ::DefWindowProc(hwnd, message, wparam, lparam); } +void SystemPreferences::OnSysColorChange() { + bool new_invertered_color_scheme = IsInvertedColorScheme(); + if (new_invertered_color_scheme != invertered_color_scheme_) { + invertered_color_scheme_ = new_invertered_color_scheme; + Emit("inverted-color-scheme-changed", new_invertered_color_scheme); + } +} + } // namespace api } // namespace atom diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index c5ad763264ab..790e78c1499e 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 an inverted color scheme, such as + a high contrast theme, is being used, `false` otherwise. ## Methods @@ -150,3 +157,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. 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') + }) + }) })