feat: detect high contrast color mode (#15493)
* feat: add event and method to detect high contrast color mode * docs: add docs for isHighContrastColorScheme and high-contrast-color-scheme-changed * refactor: correct type of contrast
This commit is contained in:
parent
aa6f7a5d9f
commit
6d2a088be9
4 changed files with 54 additions and 5 deletions
|
@ -20,6 +20,18 @@ namespace {
|
|||
const wchar_t kSystemPreferencesWindowClass[] =
|
||||
L"Electron_SystemPreferencesHostWindow";
|
||||
|
||||
bool g_is_high_contract_color_scheme = false;
|
||||
bool g_is_high_contract_color_scheme_initialized = false;
|
||||
|
||||
void UpdateHighContrastColorScheme() {
|
||||
HIGHCONTRAST high_contrast = {0};
|
||||
high_contrast.cbSize = sizeof(HIGHCONTRAST);
|
||||
g_is_high_contract_color_scheme =
|
||||
SystemParametersInfo(SPI_GETHIGHCONTRAST, 0, &high_contrast, 0) &&
|
||||
((high_contrast.dwFlags & HCF_HIGHCONTRASTON) != 0);
|
||||
g_is_high_contract_color_scheme_initialized = true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace api {
|
||||
|
@ -28,6 +40,12 @@ bool SystemPreferences::IsAeroGlassEnabled() {
|
|||
return ui::win::IsAeroGlassEnabled();
|
||||
}
|
||||
|
||||
bool SystemPreferences::IsHighContrastColorScheme() {
|
||||
if (!g_is_high_contract_color_scheme_initialized)
|
||||
UpdateHighContrastColorScheme();
|
||||
return g_is_high_contract_color_scheme;
|
||||
}
|
||||
|
||||
std::string hexColorDWORDToRGBA(DWORD color) {
|
||||
DWORD rgba = color << 8 | color >> 24;
|
||||
std::ostringstream stream;
|
||||
|
@ -119,6 +137,7 @@ std::string SystemPreferences::GetColor(const std::string& color,
|
|||
|
||||
void SystemPreferences::InitializeWindow() {
|
||||
invertered_color_scheme_ = IsInvertedColorScheme();
|
||||
high_contrast_color_scheme_ = IsHighContrastColorScheme();
|
||||
|
||||
// Wait until app is ready before creating sys color listener
|
||||
// Creating this listener before the app is ready causes global shortcuts
|
||||
|
@ -169,6 +188,9 @@ LRESULT CALLBACK SystemPreferences::WndProc(HWND hwnd,
|
|||
Emit("accent-color-changed", hexColorDWORDToRGBA(new_color));
|
||||
current_color_ = new_color_string;
|
||||
}
|
||||
} else if (message == WM_SYSCOLORCHANGE ||
|
||||
(message == WM_SETTINGCHANGE && wparam == SPI_SETHIGHCONTRAST)) {
|
||||
UpdateHighContrastColorScheme();
|
||||
}
|
||||
return ::DefWindowProc(hwnd, message, wparam, lparam);
|
||||
}
|
||||
|
@ -179,6 +201,13 @@ void SystemPreferences::OnSysColorChange() {
|
|||
invertered_color_scheme_ = new_invertered_color_scheme;
|
||||
Emit("inverted-color-scheme-changed", new_invertered_color_scheme);
|
||||
}
|
||||
|
||||
bool new_high_contrast_color_scheme = IsHighContrastColorScheme();
|
||||
if (new_high_contrast_color_scheme != high_contrast_color_scheme_) {
|
||||
high_contrast_color_scheme_ = new_high_contrast_color_scheme;
|
||||
Emit("high-contrast-color-scheme-changed", new_high_contrast_color_scheme);
|
||||
}
|
||||
|
||||
Emit("color-changed");
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue