electron_api_system_preferences.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/api/electron_api_system_preferences.h"
  5. #include "shell/common/gin_converters/callback_converter.h"
  6. #include "shell/common/gin_converters/value_converter.h"
  7. #include "shell/common/gin_helper/dictionary.h"
  8. #include "shell/common/gin_helper/function_template_extensions.h"
  9. #include "shell/common/node_includes.h"
  10. #include "ui/gfx/animation/animation.h"
  11. #include "ui/gfx/color_utils.h"
  12. #include "ui/native_theme/native_theme.h"
  13. namespace electron::api {
  14. gin::WrapperInfo SystemPreferences::kWrapperInfo = {gin::kEmbedderNativeGin};
  15. #if BUILDFLAG(IS_WIN)
  16. SystemPreferences::SystemPreferences() {
  17. InitializeWindow();
  18. }
  19. #else
  20. SystemPreferences::SystemPreferences() = default;
  21. #endif
  22. #if BUILDFLAG(IS_WIN)
  23. SystemPreferences::~SystemPreferences() {
  24. Browser::Get()->RemoveObserver(this);
  25. }
  26. #else
  27. SystemPreferences::~SystemPreferences() = default;
  28. #endif
  29. bool SystemPreferences::IsInvertedColorScheme() {
  30. return ui::NativeTheme::GetInstanceForNativeUi()
  31. ->GetPlatformHighContrastColorScheme() ==
  32. ui::NativeTheme::PlatformHighContrastColorScheme::kDark;
  33. }
  34. bool SystemPreferences::IsHighContrastColorScheme() {
  35. return ui::NativeTheme::GetInstanceForNativeUi()->UserHasContrastPreference();
  36. }
  37. v8::Local<v8::Value> SystemPreferences::GetAnimationSettings(
  38. v8::Isolate* isolate) {
  39. gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
  40. dict.SetHidden("simple", true);
  41. dict.Set("shouldRenderRichAnimation",
  42. gfx::Animation::ShouldRenderRichAnimation());
  43. dict.Set("scrollAnimationsEnabledBySystem",
  44. gfx::Animation::ScrollAnimationsEnabledBySystem());
  45. dict.Set("prefersReducedMotion", gfx::Animation::PrefersReducedMotion());
  46. return dict.GetHandle();
  47. }
  48. // static
  49. gin::Handle<SystemPreferences> SystemPreferences::Create(v8::Isolate* isolate) {
  50. return gin::CreateHandle(isolate, new SystemPreferences());
  51. }
  52. gin::ObjectTemplateBuilder SystemPreferences::GetObjectTemplateBuilder(
  53. v8::Isolate* isolate) {
  54. return gin_helper::EventEmitterMixin<
  55. SystemPreferences>::GetObjectTemplateBuilder(isolate)
  56. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  57. .SetMethod("getColor", &SystemPreferences::GetColor)
  58. .SetMethod("getAccentColor", &SystemPreferences::GetAccentColor)
  59. .SetMethod("getMediaAccessStatus",
  60. &SystemPreferences::GetMediaAccessStatus)
  61. #endif
  62. #if BUILDFLAG(IS_WIN)
  63. .SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled)
  64. #elif BUILDFLAG(IS_MAC)
  65. .SetMethod("postNotification", &SystemPreferences::PostNotification)
  66. .SetMethod("subscribeNotification",
  67. &SystemPreferences::SubscribeNotification)
  68. .SetMethod("unsubscribeNotification",
  69. &SystemPreferences::UnsubscribeNotification)
  70. .SetMethod("postLocalNotification",
  71. &SystemPreferences::PostLocalNotification)
  72. .SetMethod("subscribeLocalNotification",
  73. &SystemPreferences::SubscribeLocalNotification)
  74. .SetMethod("unsubscribeLocalNotification",
  75. &SystemPreferences::UnsubscribeLocalNotification)
  76. .SetMethod("postWorkspaceNotification",
  77. &SystemPreferences::PostWorkspaceNotification)
  78. .SetMethod("subscribeWorkspaceNotification",
  79. &SystemPreferences::SubscribeWorkspaceNotification)
  80. .SetMethod("unsubscribeWorkspaceNotification",
  81. &SystemPreferences::UnsubscribeWorkspaceNotification)
  82. .SetMethod("registerDefaults", &SystemPreferences::RegisterDefaults)
  83. .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
  84. .SetMethod("setUserDefault", &SystemPreferences::SetUserDefault)
  85. .SetMethod("removeUserDefault", &SystemPreferences::RemoveUserDefault)
  86. .SetMethod("isSwipeTrackingFromScrollEventsEnabled",
  87. &SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
  88. .SetMethod("getEffectiveAppearance",
  89. &SystemPreferences::GetEffectiveAppearance)
  90. .SetMethod("getAppLevelAppearance",
  91. &SystemPreferences::GetAppLevelAppearance)
  92. .SetMethod("setAppLevelAppearance",
  93. &SystemPreferences::SetAppLevelAppearance)
  94. .SetMethod("getSystemColor", &SystemPreferences::GetSystemColor)
  95. .SetMethod("canPromptTouchID", &SystemPreferences::CanPromptTouchID)
  96. .SetMethod("promptTouchID", &SystemPreferences::PromptTouchID)
  97. .SetMethod("isTrustedAccessibilityClient",
  98. &SystemPreferences::IsTrustedAccessibilityClient)
  99. .SetMethod("askForMediaAccess", &SystemPreferences::AskForMediaAccess)
  100. #endif
  101. .SetMethod("getAnimationSettings",
  102. &SystemPreferences::GetAnimationSettings);
  103. }
  104. const char* SystemPreferences::GetTypeName() {
  105. return "SystemPreferences";
  106. }
  107. } // namespace electron::api
  108. namespace {
  109. using electron::api::SystemPreferences;
  110. void Initialize(v8::Local<v8::Object> exports,
  111. v8::Local<v8::Value> unused,
  112. v8::Local<v8::Context> context,
  113. void* priv) {
  114. v8::Isolate* isolate = context->GetIsolate();
  115. gin_helper::Dictionary dict(isolate, exports);
  116. dict.Set("systemPreferences", SystemPreferences::Create(isolate));
  117. }
  118. } // namespace
  119. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_system_preferences,
  120. Initialize)