Skip to content

feat: add {get|set}AccentColor on Windows #47741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: remove redundant parsing
  • Loading branch information
codebytere committed Jul 15, 2025
commit d0937014784fedd01aea7c0d4a5ced4e0df63eb7
7 changes: 6 additions & 1 deletion shell/browser/api/electron_api_base_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,14 @@ void BaseWindow::SetAccentColor(gin_helper::Arguments* args) {
bool accent_color = false;
std::string accent_color_string;
if (args->GetNext(&accent_color_string)) {
window_->SetAccentColor(accent_color_string);
std::optional<SkColor> maybe_color = ParseCSSColor(accent_color_string);
if (maybe_color.has_value()) {
window_->SetAccentColor(maybe_color.value());
window_->UpdateWindowAccentColor();
}
} else if (args->GetNext(&accent_color)) {
window_->SetAccentColor(accent_color);
window_->UpdateWindowAccentColor();
} else {
args->ThrowError(
"Invalid accent color value - must be a string or boolean");
Expand Down
4 changes: 3 additions & 1 deletion shell/browser/native_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,10 @@ class NativeWindow : public base::SupportsUserData,

#if BUILDFLAG(IS_WIN)
void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param);
virtual void SetAccentColor(std::variant<bool, std::string> accent_color) = 0;
virtual void SetAccentColor(
std::variant<std::monostate, bool, SkColor> accent_color) = 0;
virtual std::variant<bool, std::string> GetAccentColor() const = 0;
virtual void UpdateWindowAccentColor() = 0;
#endif

void AddObserver(NativeWindowObserver* obs) { observers_.AddObserver(obs); }
Expand Down
5 changes: 3 additions & 2 deletions shell/browser/native_window_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ class NativeWindowViews : public NativeWindow,
#endif

#if BUILDFLAG(IS_WIN)
void SetAccentColor(std::variant<bool, std::string> accent_color) override;
void SetAccentColor(
std::variant<std::monostate, bool, SkColor> accent_color) override;
std::variant<bool, std::string> GetAccentColor() const override;
void UpdateWindowAccentColor() override;
TaskbarHost& taskbar_host() { return taskbar_host_; }
void UpdateThickFrame();
#endif
Expand Down Expand Up @@ -224,7 +226,6 @@ class NativeWindowViews : public NativeWindow,
void ResetWindowControls();
void SetRoundedCorners(bool rounded);
void SetForwardMouseMessages(bool forward);
void UpdateWindowAccentColor();
static LRESULT CALLBACK SubclassProc(HWND hwnd,
UINT msg,
WPARAM w_param,
Expand Down
26 changes: 7 additions & 19 deletions shell/browser/native_window_views_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -594,31 +594,19 @@
}

void NativeWindowViews::SetAccentColor(
std::variant<bool, std::string> accent_color) {
if (std::holds_alternative<std::string>(accent_color)) {
std::optional<SkColor> maybe_color =
ParseCSSColor(std::get<std::string>(accent_color));
if (maybe_color.has_value())
accent_color_ = maybe_color.value();
} else if (std::holds_alternative<bool>(accent_color)) {
accent_color_ = std::get<bool>(accent_color);
}

UpdateWindowAccentColor();
std::variant<std::monostate, bool, SkColor> accent_color) {
accent_color_ = accent_color;
}

/*
* Returns the accent color of the window, per te following hiuristic:
* Returns the window's accent color, per the following heuristic:
*
* 1. If the accent color is set to a specific SkColor, it returns that color as
* a hex string.
* 2. If the accent color is set to true, it returns the system accent color as
* a hex string.
* 3. If the accent color is set to false, it returns false.
* 4. If the accent color is std::monostate, it returns the system accent color
* as a hex string.
* - If |accent_color_| is an SkColor, return that color as a hex string.
* - If |accent_color_| is true, return the system accent color as a hex string.
* - If |accent_color_| is false, return false.
* - Otherwise, return the system accent color as a hex string.

Check failure on line 607 in shell/browser/native_window_views_win.cc

View check run for this annotation

trop / Backportable? - 36-x-y

shell/browser/native_window_views_win.cc#L600-L607

Patch Conflict
Raw output
++<<<<<<< HEAD
 +    if (IsAccentColorOnTitleBarsEnabled()) {
 +      std::optional<DWORD> accent_color = GetAccentColor();
 +      if (accent_color.has_value()) {
 +        border_color = RGB(GetRValue(accent_color.value()),
 +                           GetGValue(accent_color.value()),
 +                           GetBValue(accent_color.value()));
 +        should_apply_accent = true;
 +      }
++=======
+     // If no explicit color was set, default to the system accent color.
+     should_apply_accent = IsAccentColorOnTitleBarsEnabled();
+   }
+ 
+   // Use system accent color as fallback if no explicit color was set.
+   if (!border_color.has_value() && should_apply_accent) {
+     std::optional<DWORD> system_accent_color = GetSystemAccentColor();
+     if (system_accent_color.has_value()) {
+       border_color = RGB(GetRValue(system_accent_color.value()),
+                          GetGValue(system_accent_color.value()),
+                          GetBValue(system_accent_color.value()));
++>>>>>>> feat: add setAccentColor on Windows
*/
std::variant<bool, std::string> NativeWindowViews::GetAccentColor() const {

Check failure on line 609 in shell/browser/native_window_views_win.cc

View check run for this annotation

trop / Backportable? - 37-x-y

shell/browser/native_window_views_win.cc#L602-L609

Patch Conflict
Raw output
++<<<<<<< HEAD
 +    if (IsAccentColorOnTitleBarsEnabled()) {
 +      std::optional<DWORD> accent_color = GetAccentColor();
 +      if (accent_color.has_value()) {
 +        border_color = RGB(GetRValue(accent_color.value()),
 +                           GetGValue(accent_color.value()),
 +                           GetBValue(accent_color.value()));
 +        should_apply_accent = true;
 +      }
++=======
+     // If no explicit color was set, default to the system accent color.
+     should_apply_accent = IsAccentColorOnTitleBarsEnabled();
+   }
+ 
+   // Use system accent color as fallback if no explicit color was set.
+   if (!border_color.has_value() && should_apply_accent) {
+     std::optional<DWORD> system_accent_color = GetSystemAccentColor();
+     if (system_accent_color.has_value()) {
+       border_color = RGB(GetRValue(system_accent_color.value()),
+                          GetGValue(system_accent_color.value()),
+                          GetBValue(system_accent_color.value()));
++>>>>>>> feat: add setAccentColor on Windows
std::optional<DWORD> system_color = GetSystemAccentColor();

if (std::holds_alternative<SkColor>(accent_color_)) {
Expand Down
Loading