feat: Add getAccentColor on Linux (#48628)

* feat: Implement `getAccentColor` on Linux

Co-authored-by: Tau Gärtli <git@tau.garden>

* doc: Update OS support for accent color APIs

Co-authored-by: Tau Gärtli <git@tau.garden>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Tau Gärtli <git@tau.garden>
This commit is contained in:
trop[bot] 2025-10-23 16:07:40 +02:00 committed by GitHub
commit 4fda94be9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 65 additions and 7 deletions

View file

@ -11,6 +11,12 @@
#include "shell/common/gin_helper/handle.h"
#include "shell/common/node_includes.h"
#include "ui/gfx/animation/animation.h"
#if BUILDFLAG(IS_LINUX)
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "shell/browser/api/electron_api_system_preferences.h"
#include "shell/common/color_util.h"
#endif
namespace electron::api {
@ -21,6 +27,11 @@ gin::DeprecatedWrapperInfo SystemPreferences::kWrapperInfo = {
SystemPreferences::SystemPreferences() {
InitializeWindow();
}
#elif BUILDFLAG(IS_LINUX)
SystemPreferences::SystemPreferences()
: ui_theme_(ui::NativeTheme::GetInstanceForNativeUi()) {
ui_theme_->AddObserver(this);
}
#else
SystemPreferences::SystemPreferences() = default;
#endif
@ -45,6 +56,29 @@ v8::Local<v8::Value> SystemPreferences::GetAnimationSettings(
return dict.GetHandle();
}
#if BUILDFLAG(IS_LINUX)
std::string SystemPreferences::GetAccentColor() {
auto const color = ui_theme_->user_color();
if (!color.has_value())
return "";
return ToRGBAHex(*color);
}
void SystemPreferences::OnNativeThemeUpdatedOnUI() {
auto const new_accent_color = GetAccentColor();
if (current_accent_color_ == new_accent_color)
return;
Emit("accent-color-changed", new_accent_color);
current_accent_color_ = new_accent_color;
}
void SystemPreferences::OnNativeThemeUpdated(ui::NativeTheme* theme) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&SystemPreferences::OnNativeThemeUpdatedOnUI,
base::Unretained(this)));
}
#endif
// static
gin_helper::Handle<SystemPreferences> SystemPreferences::Create(
v8::Isolate* isolate) {
@ -55,9 +89,9 @@ gin::ObjectTemplateBuilder SystemPreferences::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::EventEmitterMixin<
SystemPreferences>::GetObjectTemplateBuilder(isolate)
.SetMethod("getAccentColor", &SystemPreferences::GetAccentColor)
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
.SetMethod("getColor", &SystemPreferences::GetColor)
.SetMethod("getAccentColor", &SystemPreferences::GetAccentColor)
.SetMethod("getMediaAccessStatus",
&SystemPreferences::GetMediaAccessStatus)
#endif