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/native_mate_converters/value_converter.h"
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
#include "ui/gfx/color_utils.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
SystemPreferences::SystemPreferences(v8::Isolate* isolate) {
|
SystemPreferences::SystemPreferences(v8::Isolate* isolate)
|
||||||
|
#if defined(OS_WIN)
|
||||||
|
: color_change_listener_(this)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
Init(isolate);
|
Init(isolate);
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
InitializeWindow();
|
InitializeWindow();
|
||||||
|
@ -29,6 +34,10 @@ bool SystemPreferences::IsDarkMode() {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
bool SystemPreferences::IsInvertedColorScheme() {
|
||||||
|
return color_utils::IsInvertedColorScheme();
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
mate::Handle<SystemPreferences> SystemPreferences::Create(
|
mate::Handle<SystemPreferences> SystemPreferences::Create(
|
||||||
v8::Isolate* isolate) {
|
v8::Isolate* isolate) {
|
||||||
|
@ -60,6 +69,8 @@ void SystemPreferences::BuildPrototype(
|
||||||
.SetMethod("isSwipeTrackingFromScrollEventsEnabled",
|
.SetMethod("isSwipeTrackingFromScrollEventsEnabled",
|
||||||
&SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
|
&SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
|
||||||
#endif
|
#endif
|
||||||
|
.SetMethod("isInvertedColorScheme",
|
||||||
|
&SystemPreferences::IsInvertedColorScheme)
|
||||||
.SetMethod("isDarkMode", &SystemPreferences::IsDarkMode);
|
.SetMethod("isDarkMode", &SystemPreferences::IsDarkMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,10 @@
|
||||||
#include "base/values.h"
|
#include "base/values.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
|
|
||||||
|
#if defined(OS_WIN)
|
||||||
|
#include "ui/gfx/sys_color_change_listener.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
class DictionaryValue;
|
class DictionaryValue;
|
||||||
}
|
}
|
||||||
|
@ -20,7 +24,11 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
|
class SystemPreferences : public mate::EventEmitter<SystemPreferences>
|
||||||
|
#if defined(OS_WIN)
|
||||||
|
, public gfx::SysColorChangeListener
|
||||||
|
#endif
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
static mate::Handle<SystemPreferences> Create(v8::Isolate* isolate);
|
static mate::Handle<SystemPreferences> Create(v8::Isolate* isolate);
|
||||||
|
|
||||||
|
@ -39,6 +47,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
|
||||||
|
|
||||||
void InitializeWindow();
|
void InitializeWindow();
|
||||||
|
|
||||||
|
// gfx::SysColorChangeListener:
|
||||||
|
void OnSysColorChange() override;
|
||||||
|
|
||||||
#elif defined(OS_MACOSX)
|
#elif defined(OS_MACOSX)
|
||||||
using NotificationCallback = base::Callback<
|
using NotificationCallback = base::Callback<
|
||||||
|
@ -59,6 +69,7 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
|
||||||
bool IsSwipeTrackingFromScrollEventsEnabled();
|
bool IsSwipeTrackingFromScrollEventsEnabled();
|
||||||
#endif
|
#endif
|
||||||
bool IsDarkMode();
|
bool IsDarkMode();
|
||||||
|
bool IsInvertedColorScheme();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit SystemPreferences(v8::Isolate* isolate);
|
explicit SystemPreferences(v8::Isolate* isolate);
|
||||||
|
@ -93,6 +104,10 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
|
||||||
HWND window_;
|
HWND window_;
|
||||||
|
|
||||||
std::string current_color_;
|
std::string current_color_;
|
||||||
|
|
||||||
|
bool invertered_color_scheme_;
|
||||||
|
|
||||||
|
gfx::ScopedSysColorChangeListener color_change_listener_;
|
||||||
#endif
|
#endif
|
||||||
DISALLOW_COPY_AND_ASSIGN(SystemPreferences);
|
DISALLOW_COPY_AND_ASSIGN(SystemPreferences);
|
||||||
};
|
};
|
||||||
|
|
|
@ -42,6 +42,8 @@ std::string SystemPreferences::GetAccentColor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SystemPreferences::InitializeWindow() {
|
void SystemPreferences::InitializeWindow() {
|
||||||
|
invertered_color_scheme_ = IsInvertedColorScheme();
|
||||||
|
|
||||||
WNDCLASSEX window_class;
|
WNDCLASSEX window_class;
|
||||||
base::win::InitializeWindowClass(
|
base::win::InitializeWindowClass(
|
||||||
kSystemPreferencesWindowClass,
|
kSystemPreferencesWindowClass,
|
||||||
|
@ -88,6 +90,14 @@ LRESULT CALLBACK SystemPreferences::WndProc(HWND hwnd,
|
||||||
return ::DefWindowProc(hwnd, message, wparam, lparam);
|
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 api
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -19,6 +19,13 @@ Returns:
|
||||||
* `newColor` String - The new RGBA color the user assigned to be there system
|
* `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
|
## Methods
|
||||||
|
|
||||||
|
@ -150,3 +157,8 @@ const green = color.substr(2, 2) // "bb"
|
||||||
const blue = color.substr(4, 2) // "cc"
|
const blue = color.substr(4, 2) // "cc"
|
||||||
const alpha = color.substr(6, 2) // "dd"
|
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)
|
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