Merge pull request #7518 from electron/inverted-color-scheme
Support detecting inverted color scheme on Windows
This commit is contained in:
commit
2a3bcdcaab
5 changed files with 59 additions and 5 deletions
|
@ -8,12 +8,17 @@
|
|||
#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)
|
||||
InitializeWindow();
|
||||
|
@ -29,6 +34,10 @@ bool SystemPreferences::IsDarkMode() {
|
|||
}
|
||||
#endif
|
||||
|
||||
bool SystemPreferences::IsInvertedColorScheme() {
|
||||
return color_utils::IsInvertedColorScheme();
|
||||
}
|
||||
|
||||
// static
|
||||
mate::Handle<SystemPreferences> 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<SystemPreferences> {
|
||||
class SystemPreferences : public mate::EventEmitter<SystemPreferences>
|
||||
#if defined(OS_WIN)
|
||||
, public gfx::SysColorChangeListener
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
static mate::Handle<SystemPreferences> Create(v8::Isolate* isolate);
|
||||
|
||||
|
@ -39,6 +47,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
|
|||
|
||||
void InitializeWindow();
|
||||
|
||||
// gfx::SysColorChangeListener:
|
||||
void OnSysColorChange() override;
|
||||
|
||||
#elif defined(OS_MACOSX)
|
||||
using NotificationCallback = base::Callback<
|
||||
|
@ -59,6 +69,7 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
|
|||
bool IsSwipeTrackingFromScrollEventsEnabled();
|
||||
#endif
|
||||
bool IsDarkMode();
|
||||
bool IsInvertedColorScheme();
|
||||
|
||||
protected:
|
||||
explicit SystemPreferences(v8::Isolate* isolate);
|
||||
|
@ -93,6 +104,10 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
|
|||
HWND window_;
|
||||
|
||||
std::string current_color_;
|
||||
|
||||
bool invertered_color_scheme_;
|
||||
|
||||
gfx::ScopedSysColorChangeListener color_change_listener_;
|
||||
#endif
|
||||
DISALLOW_COPY_AND_ASSIGN(SystemPreferences);
|
||||
};
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -19,6 +19,13 @@ Returns:
|
|||
* `newColor` String - The new RGBA color the user assigned to be there system
|
||||
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.
|
||||
|
|
|
@ -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')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue