Skip to content

Commit 9ab68b0

Browse files
committed
refactor: unify GetSystemAccentColor
1 parent 767e34c commit 9ab68b0

File tree

5 files changed

+75
-59
lines changed

5 files changed

+75
-59
lines changed

shell/browser/api/electron_api_system_preferences_win.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <iomanip>
66
#include <string_view>
77

8-
#include <dwmapi.h>
98
#include <windows.devices.enumeration.h>
109
#include <wrl/client.h>
1110

@@ -84,14 +83,12 @@ std::string hexColorDWORDToRGBA(DWORD color) {
8483
}
8584

8685
std::string SystemPreferences::GetAccentColor() {
87-
DWORD color = 0;
88-
BOOL opaque = FALSE;
86+
std::optional<DWORD> color = GetSystemAccentColor();
8987

90-
if (FAILED(DwmGetColorizationColor(&color, &opaque))) {
88+
if (!color.has_value())
9189
return "";
92-
}
9390

94-
return hexColorDWORDToRGBA(color);
91+
return hexColorDWORDToRGBA(color.value());
9592
}
9693

9794
std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,

shell/browser/native_window_views.cc

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,44 +1706,6 @@ void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
17061706
#endif
17071707

17081708
#if BUILDFLAG(IS_WIN)
1709-
void NativeWindowViews::SetAccentColor(
1710-
std::variant<bool, std::string> accent_color) {
1711-
if (std::holds_alternative<std::string>(accent_color)) {
1712-
std::optional<SkColor> maybe_color =
1713-
ParseCSSColor(std::get<std::string>(accent_color));
1714-
if (maybe_color.has_value())
1715-
accent_color_ = maybe_color.value();
1716-
} else if (std::holds_alternative<bool>(accent_color)) {
1717-
accent_color_ = std::get<bool>(accent_color);
1718-
}
1719-
1720-
UpdateWindowAccentColor();
1721-
}
1722-
1723-
std::variant<bool, std::string> NativeWindowViews::GetAccentColor() const {
1724-
if (std::holds_alternative<SkColor>(accent_color_)) {
1725-
// If accent_color_ is a SkColor (string), return that as hex string.
1726-
return ToRGBHex(std::get<SkColor>(accent_color_));
1727-
} else if (std::holds_alternative<bool>(accent_color_)) {
1728-
// If accent_color_ is a bool
1729-
if (std::get<bool>(accent_color_)) {
1730-
// If accent_color_ is true, return system color (convert RGBA to RGB).
1731-
std::string system_color =
1732-
electron::api::SystemPreferences::GetAccentColor();
1733-
return system_color.empty() ? system_color : system_color.substr(0, 6);
1734-
} else {
1735-
// If accent_color_ is false, return false.
1736-
return false;
1737-
}
1738-
} else {
1739-
// If accent_color_ is std::monostate (default/unset), return system color
1740-
// as RGB.
1741-
std::string system_color =
1742-
electron::api::SystemPreferences::GetAccentColor();
1743-
return system_color.empty() ? system_color : system_color.substr(0, 6);
1744-
}
1745-
}
1746-
17471709
void NativeWindowViews::UpdateThickFrame() {
17481710
if (!thick_frame_)
17491711
return;

shell/browser/native_window_views_win.cc

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#include "shell/browser/native_window_views.h"
1717
#include "shell/browser/ui/views/root_view.h"
1818
#include "shell/browser/ui/views/win_frame_view.h"
19+
#include "shell/common/color_util.h"
1920
#include "shell/common/electron_constants.h"
21+
#include "skia/ext/skia_utils_win.h"
2022
#include "ui/display/display.h"
2123
#include "ui/display/screen.h"
2224
#include "ui/gfx/geometry/resize_utils.h"
@@ -46,21 +48,6 @@ void SetWindowBorderAndCaptionColor(HWND hwnd, COLORREF color) {
4648
LOG(WARNING) << "Failed to set border color";
4749
}
4850

49-
std::optional<DWORD> GetSystemAccentColor() {
50-
base::win::RegKey key;
51-
if (key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\DWM",
52-
KEY_READ) != ERROR_SUCCESS) {
53-
return std::nullopt;
54-
}
55-
56-
DWORD accent_color = 0;
57-
if (key.ReadValueDW(L"AccentColor", &accent_color) != ERROR_SUCCESS) {
58-
return std::nullopt;
59-
}
60-
61-
return accent_color;
62-
}
63-
6451
bool IsAccentColorOnTitleBarsEnabled() {
6552
base::win::RegKey key;
6653
if (key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\DWM",
@@ -606,6 +593,51 @@ void NativeWindowViews::UpdateWindowAccentColor() {
606593
SetWindowBorderAndCaptionColor(GetAcceleratedWidget(), final_color);
607594
}
608595

596+
void NativeWindowViews::SetAccentColor(
597+
std::variant<bool, std::string> accent_color) {
598+
if (std::holds_alternative<std::string>(accent_color)) {
599+
std::optional<SkColor> maybe_color =
600+
ParseCSSColor(std::get<std::string>(accent_color));
601+
if (maybe_color.has_value())
602+
accent_color_ = maybe_color.value();
603+
} else if (std::holds_alternative<bool>(accent_color)) {
604+
accent_color_ = std::get<bool>(accent_color);
605+
}
606+
607+
UpdateWindowAccentColor();
608+
}
609+
610+
/*
611+
* Returns the accent color of the window, per te following hiuristic:
612+
*
613+
* 1. If the accent color is set to a specific SkColor, it returns that color as
614+
* a hex string.
615+
* 2. If the accent color is set to true, it returns the system accent color as
616+
* a hex string.
617+
* 3. If the accent color is set to false, it returns false.
618+
* 4. If the accent color is std::monostate, it returns the system accent color
619+
* as a hex string.
620+
*/
621+
std::variant<bool, std::string> NativeWindowViews::GetAccentColor() const {
622+
std::optional<DWORD> system_color = GetSystemAccentColor();
623+
624+
if (std::holds_alternative<SkColor>(accent_color_)) {
625+
return ToRGBHex(std::get<SkColor>(accent_color_));
626+
} else if (std::holds_alternative<bool>(accent_color_)) {
627+
if (std::get<bool>(accent_color_)) {
628+
if (!system_color.has_value())
629+
return false;
630+
return ToRGBHex(skia::COLORREFToSkColor(system_color.value()));
631+
} else {
632+
return false;
633+
}
634+
} else {
635+
if (!system_color.has_value())
636+
return false;
637+
return ToRGBHex(skia::COLORREFToSkColor(system_color.value()));
638+
}
639+
}
640+
609641
void NativeWindowViews::ResetWindowControls() {
610642
// If a given window was minimized and has since been
611643
// unminimized (restored/maximized), ensure the WCO buttons

shell/common/color_util.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include "content/public/common/color_parser.h"
1111
#include "third_party/abseil-cpp/absl/strings/str_format.h"
1212

13+
#if BUILDFLAG(IS_WIN)
14+
#include <dwmapi.h>
15+
#endif
16+
1317
namespace {
1418

1519
bool IsHexFormatWithAlpha(const std::string& str) {
@@ -62,4 +66,15 @@ std::string ToRGBAHex(SkColor color, bool include_hash) {
6266
return color_str;
6367
}
6468

69+
#if BUILDFLAG(IS_WIN)
70+
std::optional<DWORD> GetSystemAccentColor() {
71+
DWORD color = 0;
72+
BOOL opaque = FALSE;
73+
74+
if (FAILED(DwmGetColorizationColor(&color, &opaque)))
75+
return std::nullopt;
76+
return color;
77+
}
78+
#endif
79+
6580
} // namespace electron

shell/common/color_util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
#include <optional>
99
#include <string>
1010

11+
#include "build/build_config.h"
12+
13+
#if BUILDFLAG(IS_WIN)
14+
#include <windows.h>
15+
#endif
16+
1117
#include "third_party/skia/include/core/SkColor.h"
1218

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

40+
#if BUILDFLAG(IS_WIN)
41+
std::optional<DWORD> GetSystemAccentColor();
42+
#endif
43+
3444
} // namespace electron
3545

3646
#endif // ELECTRON_SHELL_COMMON_COLOR_UTIL_H_

0 commit comments

Comments
 (0)