feat: add {get|set}AccentColor on Windows (#47939)

* feat: add setAccentColor on Windows

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* refactor: unify GetSystemAccentColor

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* refactor: remove redundant parsing

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fixup documentation

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* Update docs/api/browser-window.md

Co-authored-by: Will Anderson <andersonw@dropbox.com>

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* Update docs/api/base-window.md

Co-authored-by: Will Anderson <andersonw@dropbox.com>

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2025-08-06 19:39:18 +02:00 committed by GitHub
commit f3774d578d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 265 additions and 24 deletions

View file

@ -1089,6 +1089,33 @@ void BaseWindow::SetAppDetails(const gin_helper::Dictionary& options) {
bool BaseWindow::IsSnapped() const {
return window_->IsSnapped();
}
void BaseWindow::SetAccentColor(gin_helper::Arguments* args) {
bool accent_color = false;
std::string accent_color_string;
if (args->GetNext(&accent_color_string)) {
std::optional<SkColor> maybe_color = ParseCSSColor(accent_color_string);
if (maybe_color.has_value()) {
window_->SetAccentColor(maybe_color.value());
window_->UpdateWindowAccentColor(window_->IsActive());
}
} else if (args->GetNext(&accent_color)) {
window_->SetAccentColor(accent_color);
window_->UpdateWindowAccentColor(window_->IsActive());
} else {
args->ThrowError(
"Invalid accent color value - must be a string or boolean");
}
}
v8::Local<v8::Value> BaseWindow::GetAccentColor() const {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
auto accent_color = window_->GetAccentColor();
if (std::holds_alternative<bool>(accent_color))
return v8::Boolean::New(isolate, std::get<bool>(accent_color));
return gin::StringToV8(isolate, std::get<std::string>(accent_color));
}
#endif
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
@ -1277,6 +1304,8 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate,
#if BUILDFLAG(IS_WIN)
.SetMethod("isSnapped", &BaseWindow::IsSnapped)
.SetProperty("snapped", &BaseWindow::IsSnapped)
.SetMethod("setAccentColor", &BaseWindow::SetAccentColor)
.SetMethod("getAccentColor", &BaseWindow::GetAccentColor)
.SetMethod("hookWindowMessage", &BaseWindow::HookWindowMessage)
.SetMethod("isWindowMessageHooked", &BaseWindow::IsWindowMessageHooked)
.SetMethod("unhookWindowMessage", &BaseWindow::UnhookWindowMessage)

View file

@ -255,6 +255,8 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
bool SetThumbnailToolTip(const std::string& tooltip);
void SetAppDetails(const gin_helper::Dictionary& options);
bool IsSnapped() const;
void SetAccentColor(gin_helper::Arguments* args);
v8::Local<v8::Value> GetAccentColor() const;
#endif
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)

View file

@ -56,7 +56,7 @@ class SystemPreferences final
const char* GetTypeName() override;
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
std::string GetAccentColor();
static std::string GetAccentColor();
std::string GetColor(gin_helper::ErrorThrower thrower,
const std::string& color);
std::string GetMediaAccessStatus(gin_helper::ErrorThrower thrower,

View file

@ -5,7 +5,6 @@
#include <iomanip>
#include <string_view>
#include <dwmapi.h>
#include <windows.devices.enumeration.h>
#include <wrl/client.h>
@ -84,14 +83,12 @@ std::string hexColorDWORDToRGBA(DWORD color) {
}
std::string SystemPreferences::GetAccentColor() {
DWORD color = 0;
BOOL opaque = FALSE;
std::optional<DWORD> color = GetSystemAccentColor();
if (FAILED(DwmGetColorizationColor(&color, &opaque))) {
if (!color.has_value())
return "";
}
return hexColorDWORDToRGBA(color);
return hexColorDWORDToRGBA(color.value());
}
std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,