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
Next Next commit
refactor: unify GetSystemAccentColor
  • Loading branch information
codebytere committed Jul 15, 2025
commit 22a2cd8c71ce76221d8e3efa88fc8073a11090a9
9 changes: 3 additions & 6 deletions shell/browser/api/electron_api_system_preferences_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <iomanip>
#include <string_view>

#include <dwmapi.h>
#include <windows.devices.enumeration.h>
#include <wrl/client.h>

Expand Down Expand Up @@ -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,
Expand Down
38 changes: 0 additions & 38 deletions shell/browser/native_window_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1706,44 +1706,6 @@ void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
#endif

#if BUILDFLAG(IS_WIN)
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<bool, std::string> NativeWindowViews::GetAccentColor() const {
if (std::holds_alternative<SkColor>(accent_color_)) {
// If accent_color_ is a SkColor (string), return that as hex string.
return ToRGBHex(std::get<SkColor>(accent_color_));
} else if (std::holds_alternative<bool>(accent_color_)) {
// If accent_color_ is a bool
if (std::get<bool>(accent_color_)) {
// If accent_color_ is true, return system color (convert RGBA to RGB).
std::string system_color =
electron::api::SystemPreferences::GetAccentColor();
return system_color.empty() ? system_color : system_color.substr(0, 6);
} else {
// If accent_color_ is false, return false.
return false;
}
} else {
// If accent_color_ is std::monostate (default/unset), return system color
// as RGB.
std::string system_color =
electron::api::SystemPreferences::GetAccentColor();
return system_color.empty() ? system_color : system_color.substr(0, 6);
}
}

void NativeWindowViews::UpdateThickFrame() {
if (!thick_frame_)
return;
Expand Down
62 changes: 47 additions & 15 deletions shell/browser/native_window_views_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#include "shell/browser/native_window_views.h"
#include "shell/browser/ui/views/root_view.h"
#include "shell/browser/ui/views/win_frame_view.h"
#include "shell/common/color_util.h"
#include "shell/common/electron_constants.h"
#include "skia/ext/skia_utils_win.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/resize_utils.h"
Expand Down Expand Up @@ -46,21 +48,6 @@ void SetWindowBorderAndCaptionColor(HWND hwnd, COLORREF color) {
LOG(WARNING) << "Failed to set border color";
}

std::optional<DWORD> GetSystemAccentColor() {
base::win::RegKey key;
if (key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\DWM",
KEY_READ) != ERROR_SUCCESS) {
return std::nullopt;
}

DWORD accent_color = 0;
if (key.ReadValueDW(L"AccentColor", &accent_color) != ERROR_SUCCESS) {
return std::nullopt;
}

return accent_color;
}

bool IsAccentColorOnTitleBarsEnabled() {
base::win::RegKey key;
if (key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\DWM",
Expand Down Expand Up @@ -606,6 +593,51 @@ void NativeWindowViews::UpdateWindowAccentColor() {
SetWindowBorderAndCaptionColor(GetAcceleratedWidget(), final_color);
}

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();
}

/*
* Returns the accent color of the window, per te following hiuristic:
*
* 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.
*/
std::variant<bool, std::string> NativeWindowViews::GetAccentColor() const {
std::optional<DWORD> system_color = GetSystemAccentColor();

if (std::holds_alternative<SkColor>(accent_color_)) {
return ToRGBHex(std::get<SkColor>(accent_color_));
} else if (std::holds_alternative<bool>(accent_color_)) {
if (std::get<bool>(accent_color_)) {
if (!system_color.has_value())
return false;
return ToRGBHex(skia::COLORREFToSkColor(system_color.value()));
} else {
return false;
}
} else {
if (!system_color.has_value())
return false;
return ToRGBHex(skia::COLORREFToSkColor(system_color.value()));
}
}

void NativeWindowViews::ResetWindowControls() {
// If a given window was minimized and has since been
// unminimized (restored/maximized), ensure the WCO buttons
Expand Down
15 changes: 15 additions & 0 deletions shell/common/color_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include "content/public/common/color_parser.h"
#include "third_party/abseil-cpp/absl/strings/str_format.h"

#if BUILDFLAG(IS_WIN)
#include <dwmapi.h>
#endif

namespace {

bool IsHexFormatWithAlpha(const std::string& str) {
Expand Down Expand Up @@ -62,4 +66,15 @@ std::string ToRGBAHex(SkColor color, bool include_hash) {
return color_str;
}

#if BUILDFLAG(IS_WIN)
std::optional<DWORD> GetSystemAccentColor() {
DWORD color = 0;
BOOL opaque = FALSE;

if (FAILED(DwmGetColorizationColor(&color, &opaque)))
return std::nullopt;
return color;
}
#endif

} // namespace electron
10 changes: 10 additions & 0 deletions shell/common/color_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <optional>
#include <string>

#include "build/build_config.h"

#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif

#include "third_party/skia/include/core/SkColor.h"

// SkColor is a typedef for uint32_t, this wrapper is to tag an SkColor for
Expand All @@ -31,6 +37,10 @@ std::string ToRGBHex(SkColor color);
// Convert color to RGBA hex value like "#RRGGBBAA".
std::string ToRGBAHex(SkColor color, bool include_hash = true);

#if BUILDFLAG(IS_WIN)
std::optional<DWORD> GetSystemAccentColor();
#endif

} // namespace electron

#endif // ELECTRON_SHELL_COMMON_COLOR_UTIL_H_