2019-08-14 13:42:55 -07:00
|
|
|
// Copyright (c) 2019 Slack Technologies, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-09-16 14:23:13 -07:00
|
|
|
#include "shell/browser/api/electron_api_native_theme.h"
|
2019-08-14 13:42:55 -07:00
|
|
|
|
2019-09-05 10:57:04 -07:00
|
|
|
#include <string>
|
|
|
|
|
2019-09-16 16:08:01 -07:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
2019-10-02 09:30:55 +09:00
|
|
|
#include "shell/common/gin_converters/std_converter.h"
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2025-08-05 02:03:06 +09:00
|
|
|
#include "shell/common/gin_helper/handle.h"
|
2019-10-02 09:30:55 +09:00
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2019-08-14 13:42:55 -07:00
|
|
|
#include "shell/common/node_includes.h"
|
|
|
|
#include "ui/native_theme/native_theme.h"
|
|
|
|
|
2022-06-29 12:55:47 -07:00
|
|
|
namespace electron::api {
|
2019-08-14 13:42:55 -07:00
|
|
|
|
2025-07-14 13:42:37 -07:00
|
|
|
gin::DeprecatedWrapperInfo NativeTheme::kWrapperInfo = {
|
|
|
|
gin::kEmbedderNativeGin};
|
2020-07-22 11:01:30 -07:00
|
|
|
|
2020-03-30 15:39:50 -07:00
|
|
|
NativeTheme::NativeTheme(v8::Isolate* isolate,
|
|
|
|
ui::NativeTheme* ui_theme,
|
|
|
|
ui::NativeTheme* web_theme)
|
|
|
|
: ui_theme_(ui_theme), web_theme_(web_theme) {
|
|
|
|
ui_theme_->AddObserver(this);
|
2025-09-08 17:08:42 -04:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
std::ignore = hkcu_themes_regkey_.Open(HKEY_CURRENT_USER,
|
|
|
|
L"Software\\Microsoft\\Windows\\"
|
|
|
|
L"CurrentVersion\\Themes\\Personalize",
|
|
|
|
KEY_READ);
|
|
|
|
#endif
|
2019-08-14 13:42:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NativeTheme::~NativeTheme() {
|
2020-03-30 15:39:50 -07:00
|
|
|
ui_theme_->RemoveObserver(this);
|
2019-08-14 13:42:55 -07:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:08:01 -07:00
|
|
|
void NativeTheme::OnNativeThemeUpdatedOnUI() {
|
2025-09-08 17:08:42 -04:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
if (hkcu_themes_regkey_.Valid()) {
|
|
|
|
DWORD system_uses_light_theme = 1;
|
|
|
|
hkcu_themes_regkey_.ReadValueDW(L"SystemUsesLightTheme",
|
|
|
|
&system_uses_light_theme);
|
|
|
|
bool system_dark_mode_enabled = (system_uses_light_theme == 0);
|
|
|
|
should_use_dark_colors_for_system_integrated_ui_ =
|
|
|
|
std::make_optional<bool>(system_dark_mode_enabled);
|
|
|
|
}
|
|
|
|
#endif
|
2019-08-14 13:42:55 -07:00
|
|
|
Emit("updated");
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:08:01 -07:00
|
|
|
void NativeTheme::OnNativeThemeUpdated(ui::NativeTheme* theme) {
|
2022-05-17 12:48:40 -04:00
|
|
|
content::GetUIThreadTaskRunner({})->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(&NativeTheme::OnNativeThemeUpdatedOnUI,
|
2019-09-18 15:58:00 -04:00
|
|
|
base::Unretained(this)));
|
2019-09-16 16:08:01 -07:00
|
|
|
}
|
|
|
|
|
2019-09-05 10:57:04 -07:00
|
|
|
void NativeTheme::SetThemeSource(ui::NativeTheme::ThemeSource override) {
|
2020-03-30 15:39:50 -07:00
|
|
|
ui_theme_->set_theme_source(override);
|
|
|
|
web_theme_->set_theme_source(override);
|
2022-02-09 18:58:52 -08:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2019-09-05 10:57:04 -07:00
|
|
|
// Update the macOS appearance setting for this new override value
|
|
|
|
UpdateMacOSAppearanceForOverrideValue(override);
|
|
|
|
#endif
|
2020-08-20 15:53:06 -05:00
|
|
|
// TODO(MarshallOfSound): Update all existing browsers windows to use GTK dark
|
|
|
|
// theme
|
2019-09-05 10:57:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ui::NativeTheme::ThemeSource NativeTheme::GetThemeSource() const {
|
2020-03-30 15:39:50 -07:00
|
|
|
return ui_theme_->theme_source();
|
2019-09-05 10:57:04 -07:00
|
|
|
}
|
|
|
|
|
2019-08-14 13:42:55 -07:00
|
|
|
bool NativeTheme::ShouldUseDarkColors() {
|
2025-09-24 11:56:54 -04:00
|
|
|
auto theme_source = GetThemeSource();
|
|
|
|
if (theme_source == ui::NativeTheme::ThemeSource::kForcedLight)
|
|
|
|
return false;
|
|
|
|
if (theme_source == ui::NativeTheme::ThemeSource::kForcedDark)
|
|
|
|
return true;
|
|
|
|
return ui_theme_->preferred_color_scheme() ==
|
|
|
|
ui::NativeTheme::PreferredColorScheme::kDark;
|
2019-08-14 13:42:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeTheme::ShouldUseHighContrastColors() {
|
2025-09-24 11:56:54 -04:00
|
|
|
return ui_theme_->preferred_contrast() ==
|
|
|
|
ui::NativeTheme::PreferredContrast::kMore;
|
2019-08-14 13:42:55 -07:00
|
|
|
}
|
|
|
|
|
2025-04-10 12:08:29 +02:00
|
|
|
bool NativeTheme::ShouldUseDarkColorsForSystemIntegratedUI() {
|
2025-09-08 17:08:42 -04:00
|
|
|
return should_use_dark_colors_for_system_integrated_ui_.value_or(
|
|
|
|
ShouldUseDarkColors());
|
2025-04-10 12:08:29 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 10:30:02 +01:00
|
|
|
bool NativeTheme::InForcedColorsMode() {
|
2025-09-24 11:56:54 -04:00
|
|
|
return ui_theme_->forced_colors();
|
2022-03-21 10:30:02 +01:00
|
|
|
}
|
|
|
|
|
2024-07-24 14:38:22 +02:00
|
|
|
bool NativeTheme::GetPrefersReducedTransparency() {
|
2025-09-24 11:56:54 -04:00
|
|
|
return ui_theme_->prefers_reduced_transparency();
|
2024-07-24 14:38:22 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:58:52 -08:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2019-08-14 13:42:55 -07:00
|
|
|
const CFStringRef WhiteOnBlack = CFSTR("whiteOnBlack");
|
|
|
|
const CFStringRef UniversalAccessDomain = CFSTR("com.apple.universalaccess");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// TODO(MarshallOfSound): Implement for Linux
|
|
|
|
bool NativeTheme::ShouldUseInvertedColorScheme() {
|
2022-02-09 18:58:52 -08:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2019-08-14 13:42:55 -07:00
|
|
|
CFPreferencesAppSynchronize(UniversalAccessDomain);
|
|
|
|
Boolean keyExistsAndHasValidFormat = false;
|
|
|
|
Boolean is_inverted = CFPreferencesGetAppBooleanValue(
|
|
|
|
WhiteOnBlack, UniversalAccessDomain, &keyExistsAndHasValidFormat);
|
|
|
|
if (!keyExistsAndHasValidFormat)
|
|
|
|
return false;
|
|
|
|
return is_inverted;
|
|
|
|
#else
|
2025-09-24 11:56:54 -04:00
|
|
|
return ui_theme_->forced_colors() &&
|
|
|
|
ui_theme_->preferred_color_scheme() ==
|
|
|
|
ui::NativeTheme::PreferredColorScheme::kDark;
|
2019-08-14 13:42:55 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2025-08-05 02:03:06 +09:00
|
|
|
gin_helper::Handle<NativeTheme> NativeTheme::Create(v8::Isolate* isolate) {
|
2020-03-30 15:39:50 -07:00
|
|
|
ui::NativeTheme* ui_theme = ui::NativeTheme::GetInstanceForNativeUi();
|
|
|
|
ui::NativeTheme* web_theme = ui::NativeTheme::GetInstanceForWeb();
|
2025-08-05 02:03:06 +09:00
|
|
|
return gin_helper::CreateHandle(
|
|
|
|
isolate, new NativeTheme(isolate, ui_theme, web_theme));
|
2019-08-14 13:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-07-22 11:01:30 -07:00
|
|
|
gin::ObjectTemplateBuilder NativeTheme::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin_helper::EventEmitterMixin<NativeTheme>::GetObjectTemplateBuilder(
|
|
|
|
isolate)
|
2019-08-14 13:42:55 -07:00
|
|
|
.SetProperty("shouldUseDarkColors", &NativeTheme::ShouldUseDarkColors)
|
2019-09-05 10:57:04 -07:00
|
|
|
.SetProperty("themeSource", &NativeTheme::GetThemeSource,
|
|
|
|
&NativeTheme::SetThemeSource)
|
2019-08-14 13:42:55 -07:00
|
|
|
.SetProperty("shouldUseHighContrastColors",
|
|
|
|
&NativeTheme::ShouldUseHighContrastColors)
|
2025-04-10 12:08:29 +02:00
|
|
|
.SetProperty("shouldUseDarkColorsForSystemIntegratedUI",
|
|
|
|
&NativeTheme::ShouldUseDarkColorsForSystemIntegratedUI)
|
2019-08-14 13:42:55 -07:00
|
|
|
.SetProperty("shouldUseInvertedColorScheme",
|
2022-03-21 10:30:02 +01:00
|
|
|
&NativeTheme::ShouldUseInvertedColorScheme)
|
2024-07-24 14:38:22 +02:00
|
|
|
.SetProperty("inForcedColorsMode", &NativeTheme::InForcedColorsMode)
|
|
|
|
.SetProperty("prefersReducedTransparency",
|
|
|
|
&NativeTheme::GetPrefersReducedTransparency);
|
2019-08-14 13:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-07-22 11:01:30 -07:00
|
|
|
const char* NativeTheme::GetTypeName() {
|
|
|
|
return "NativeTheme";
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:55:47 -07:00
|
|
|
} // namespace electron::api
|
2019-08-14 13:42:55 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-07-22 11:01:30 -07:00
|
|
|
using electron::api::NativeTheme;
|
|
|
|
|
2019-08-14 13:42:55 -07:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2025-07-21 09:34:38 -05:00
|
|
|
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
|
|
|
|
gin::Dictionary dict{isolate, exports};
|
2020-07-22 11:01:30 -07:00
|
|
|
dict.Set("nativeTheme", NativeTheme::Create(isolate));
|
2019-08-14 13:42:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-10-02 09:30:55 +09:00
|
|
|
namespace gin {
|
2019-09-05 10:57:04 -07:00
|
|
|
|
|
|
|
v8::Local<v8::Value> Converter<ui::NativeTheme::ThemeSource>::ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const ui::NativeTheme::ThemeSource& val) {
|
|
|
|
switch (val) {
|
|
|
|
case ui::NativeTheme::ThemeSource::kForcedDark:
|
2019-10-02 09:30:55 +09:00
|
|
|
return ConvertToV8(isolate, "dark");
|
2019-09-05 10:57:04 -07:00
|
|
|
case ui::NativeTheme::ThemeSource::kForcedLight:
|
2019-10-02 09:30:55 +09:00
|
|
|
return ConvertToV8(isolate, "light");
|
2019-09-05 10:57:04 -07:00
|
|
|
case ui::NativeTheme::ThemeSource::kSystem:
|
|
|
|
default:
|
2019-10-02 09:30:55 +09:00
|
|
|
return ConvertToV8(isolate, "system");
|
2019-09-05 10:57:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Converter<ui::NativeTheme::ThemeSource>::FromV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
ui::NativeTheme::ThemeSource* out) {
|
|
|
|
std::string theme_source;
|
2019-10-02 09:30:55 +09:00
|
|
|
if (ConvertFromV8(isolate, val, &theme_source)) {
|
2019-09-05 10:57:04 -07:00
|
|
|
if (theme_source == "dark") {
|
|
|
|
*out = ui::NativeTheme::ThemeSource::kForcedDark;
|
|
|
|
} else if (theme_source == "light") {
|
|
|
|
*out = ui::NativeTheme::ThemeSource::kForcedLight;
|
|
|
|
} else if (theme_source == "system") {
|
|
|
|
*out = ui::NativeTheme::ThemeSource::kSystem;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-02 09:30:55 +09:00
|
|
|
} // namespace gin
|
2019-09-05 10:57:04 -07:00
|
|
|
|
2023-02-09 02:31:38 +01:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_native_theme, Initialize)
|