electron_api_system_preferences.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "gin/handle.h"
  6. #include "shell/common/gin_converters/callback_converter.h"
  7. #include "shell/common/gin_converters/value_converter.h"
  8. #include "shell/common/gin_helper/dictionary.h"
  9. #include "shell/common/gin_helper/error_thrower.h"
  10. #include "shell/common/node_includes.h"
  11. #include "ui/gfx/animation/animation.h"
  12. namespace electron::api {
  13. gin::WrapperInfo SystemPreferences::kWrapperInfo = {gin::kEmbedderNativeGin};
  14. #if BUILDFLAG(IS_WIN)
  15. SystemPreferences::SystemPreferences() {
  16. InitializeWindow();
  17. }
  18. #else
  19. SystemPreferences::SystemPreferences() = default;
  20. #endif
  21. #if BUILDFLAG(IS_WIN)
  22. SystemPreferences::~SystemPreferences() {
  23. Browser::Get()->RemoveObserver(this);
  24. }
  25. #else
  26. SystemPreferences::~SystemPreferences() = default;
  27. #endif
  28. v8::Local<v8::Value> SystemPreferences::GetAnimationSettings(
  29. v8::Isolate* isolate) {
  30. auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
  31. dict.Set("shouldRenderRichAnimation",
  32. gfx::Animation::ShouldRenderRichAnimation());
  33. dict.Set("scrollAnimationsEnabledBySystem",
  34. gfx::Animation::ScrollAnimationsEnabledBySystem());
  35. dict.Set("prefersReducedMotion", gfx::Animation::PrefersReducedMotion());
  36. return dict.GetHandle();
  37. }
  38. // static
  39. gin::Handle<SystemPreferences> SystemPreferences::Create(v8::Isolate* isolate) {
  40. return gin::CreateHandle(isolate, new SystemPreferences());
  41. }
  42. gin::ObjectTemplateBuilder SystemPreferences::GetObjectTemplateBuilder(
  43. v8::Isolate* isolate) {
  44. return gin_helper::EventEmitterMixin<
  45. SystemPreferences>::GetObjectTemplateBuilder(isolate)
  46. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  47. .SetMethod("getColor", &SystemPreferences::GetColor)
  48. .SetMethod("getAccentColor", &SystemPreferences::GetAccentColor)
  49. .SetMethod("getMediaAccessStatus",
  50. &SystemPreferences::GetMediaAccessStatus)
  51. #endif
  52. #if BUILDFLAG(IS_WIN)
  53. .SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled)
  54. #elif BUILDFLAG(IS_MAC)
  55. .SetMethod("postNotification", &SystemPreferences::PostNotification)
  56. .SetMethod("subscribeNotification",
  57. &SystemPreferences::SubscribeNotification)
  58. .SetMethod("unsubscribeNotification",
  59. &SystemPreferences::UnsubscribeNotification)
  60. .SetMethod("postLocalNotification",
  61. &SystemPreferences::PostLocalNotification)
  62. .SetMethod("subscribeLocalNotification",
  63. &SystemPreferences::SubscribeLocalNotification)
  64. .SetMethod("unsubscribeLocalNotification",
  65. &SystemPreferences::UnsubscribeLocalNotification)
  66. .SetMethod("postWorkspaceNotification",
  67. &SystemPreferences::PostWorkspaceNotification)
  68. .SetMethod("subscribeWorkspaceNotification",
  69. &SystemPreferences::SubscribeWorkspaceNotification)
  70. .SetMethod("unsubscribeWorkspaceNotification",
  71. &SystemPreferences::UnsubscribeWorkspaceNotification)
  72. .SetMethod("registerDefaults", &SystemPreferences::RegisterDefaults)
  73. .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
  74. .SetMethod("setUserDefault", &SystemPreferences::SetUserDefault)
  75. .SetMethod("removeUserDefault", &SystemPreferences::RemoveUserDefault)
  76. .SetMethod("isSwipeTrackingFromScrollEventsEnabled",
  77. &SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
  78. .SetMethod("getEffectiveAppearance",
  79. &SystemPreferences::GetEffectiveAppearance)
  80. .SetMethod("getSystemColor", &SystemPreferences::GetSystemColor)
  81. .SetMethod("canPromptTouchID", &SystemPreferences::CanPromptTouchID)
  82. .SetMethod("promptTouchID", &SystemPreferences::PromptTouchID)
  83. .SetMethod("isTrustedAccessibilityClient",
  84. &SystemPreferences::IsTrustedAccessibilityClient)
  85. .SetMethod("askForMediaAccess", &SystemPreferences::AskForMediaAccess)
  86. .SetProperty(
  87. "accessibilityDisplayShouldReduceTransparency",
  88. &SystemPreferences::AccessibilityDisplayShouldReduceTransparency)
  89. #endif
  90. .SetMethod("getAnimationSettings",
  91. &SystemPreferences::GetAnimationSettings);
  92. }
  93. const char* SystemPreferences::GetTypeName() {
  94. return "SystemPreferences";
  95. }
  96. } // namespace electron::api
  97. namespace {
  98. using electron::api::SystemPreferences;
  99. void Initialize(v8::Local<v8::Object> exports,
  100. v8::Local<v8::Value> unused,
  101. v8::Local<v8::Context> context,
  102. void* priv) {
  103. v8::Isolate* isolate = context->GetIsolate();
  104. gin_helper::Dictionary dict(isolate, exports);
  105. dict.Set("systemPreferences", SystemPreferences::Create(isolate));
  106. }
  107. } // namespace
  108. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_system_preferences,
  109. Initialize)