electron/shell/browser/api/electron_api_system_preferences.cc

154 lines
6 KiB
C++
Raw Normal View History

2016-04-24 12:13:46 +00:00
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/api/electron_api_system_preferences.h"
2016-04-24 12:13:46 +00:00
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h"
#include "ui/gfx/animation/animation.h"
#include "ui/gfx/color_utils.h"
#include "ui/native_theme/native_theme.h"
2016-04-24 12:13:46 +00:00
namespace electron {
2016-04-24 12:13:46 +00:00
namespace api {
SystemPreferences::SystemPreferences(v8::Isolate* isolate) {
2016-04-24 12:13:46 +00:00
Init(isolate);
#if defined(OS_WIN)
InitializeWindow();
#endif
2016-04-24 12:13:46 +00:00
}
SystemPreferences::~SystemPreferences() {
#if defined(OS_WIN)
Browser::Get()->RemoveObserver(this);
#endif
2016-04-24 12:13:46 +00:00
}
#if !defined(OS_MACOSX)
bool SystemPreferences::IsDarkMode() {
return ui::NativeTheme::GetInstanceForNativeUi()->ShouldUseDarkColors();
}
#endif
bool SystemPreferences::IsInvertedColorScheme() {
return color_utils::IsInvertedColorScheme();
}
bool SystemPreferences::IsHighContrastColorScheme() {
return ui::NativeTheme::GetInstanceForNativeUi()->UsesHighContrastColors();
}
v8::Local<v8::Value> SystemPreferences::GetAnimationSettings(
v8::Isolate* isolate) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("shouldRenderRichAnimation",
gfx::Animation::ShouldRenderRichAnimation());
dict.Set("scrollAnimationsEnabledBySystem",
gfx::Animation::ScrollAnimationsEnabledBySystem());
dict.Set("prefersReducedMotion", gfx::Animation::PrefersReducedMotion());
return dict.GetHandle();
}
2016-04-24 12:13:46 +00:00
// static
gin::Handle<SystemPreferences> SystemPreferences::Create(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new SystemPreferences(isolate));
2016-04-24 12:13:46 +00:00
}
// static
void SystemPreferences::BuildPrototype(
2018-04-18 01:55:30 +00:00
v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "SystemPreferences"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
#if defined(OS_WIN) || defined(OS_MACOSX)
.SetMethod("getColor", &SystemPreferences::GetColor)
.SetMethod("getAccentColor", &SystemPreferences::GetAccentColor)
#endif
#if defined(OS_WIN)
2016-09-16 16:09:28 +00:00
.SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled)
#elif defined(OS_MACOSX)
2018-04-18 01:55:30 +00:00
.SetMethod("postNotification", &SystemPreferences::PostNotification)
.SetMethod("subscribeNotification",
&SystemPreferences::SubscribeNotification)
.SetMethod("unsubscribeNotification",
&SystemPreferences::UnsubscribeNotification)
.SetMethod("postLocalNotification",
&SystemPreferences::PostLocalNotification)
.SetMethod("subscribeLocalNotification",
&SystemPreferences::SubscribeLocalNotification)
.SetMethod("unsubscribeLocalNotification",
&SystemPreferences::UnsubscribeLocalNotification)
.SetMethod("postWorkspaceNotification",
&SystemPreferences::PostWorkspaceNotification)
.SetMethod("subscribeWorkspaceNotification",
&SystemPreferences::SubscribeWorkspaceNotification)
.SetMethod("unsubscribeWorkspaceNotification",
&SystemPreferences::UnsubscribeWorkspaceNotification)
2017-12-12 18:08:09 +00:00
.SetMethod("registerDefaults", &SystemPreferences::RegisterDefaults)
2016-04-25 06:35:52 +00:00
.SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
.SetMethod("setUserDefault", &SystemPreferences::SetUserDefault)
.SetMethod("removeUserDefault", &SystemPreferences::RemoveUserDefault)
.SetMethod("isSwipeTrackingFromScrollEventsEnabled",
&SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
.SetMethod("_getEffectiveAppearance",
&SystemPreferences::GetEffectiveAppearance)
.SetMethod("_getAppLevelAppearance",
&SystemPreferences::GetAppLevelAppearance)
.SetMethod("_setAppLevelAppearance",
&SystemPreferences::SetAppLevelAppearance)
.SetProperty("appLevelAppearance",
&SystemPreferences::GetAppLevelAppearance,
&SystemPreferences::SetAppLevelAppearance)
.SetProperty("effectiveAppearance",
&SystemPreferences::GetEffectiveAppearance)
.SetMethod("getSystemColor", &SystemPreferences::GetSystemColor)
.SetMethod("canPromptTouchID", &SystemPreferences::CanPromptTouchID)
.SetMethod("promptTouchID", &SystemPreferences::PromptTouchID)
.SetMethod("isTrustedAccessibilityClient",
&SystemPreferences::IsTrustedAccessibilityClient)
.SetMethod("getMediaAccessStatus",
&SystemPreferences::GetMediaAccessStatus)
.SetMethod("askForMediaAccess", &SystemPreferences::AskForMediaAccess)
#endif
.SetMethod("isInvertedColorScheme",
&SystemPreferences::IsInvertedColorScheme)
.SetMethod("isHighContrastColorScheme",
&SystemPreferences::IsHighContrastColorScheme)
.SetMethod("isDarkMode", &SystemPreferences::IsDarkMode)
.SetMethod("getAnimationSettings",
&SystemPreferences::GetAnimationSettings);
2016-04-24 12:13:46 +00:00
}
} // namespace api
} // namespace electron
2016-04-24 12:13:46 +00:00
namespace {
using electron::api::SystemPreferences;
2016-08-02 11:38:35 +00:00
2018-04-18 01:55:30 +00:00
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
2016-04-24 12:13:46 +00:00
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
2016-08-02 11:38:35 +00:00
dict.Set("systemPreferences", SystemPreferences::Create(isolate));
dict.Set("SystemPreferences", SystemPreferences::GetConstructor(isolate)
->GetFunction(context)
.ToLocalChecked());
2016-04-24 12:13:46 +00:00
}
} // namespace
2019-02-27 19:14:23 +00:00
NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_system_preferences, Initialize)