electron_api_system_preferences.cc 4.8 KB

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