electron_api_system_preferences.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/object_template_builder.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 {
  14. namespace api {
  15. SystemPreferences::SystemPreferences(v8::Isolate* isolate) {
  16. Init(isolate);
  17. #if defined(OS_WIN)
  18. InitializeWindow();
  19. #endif
  20. }
  21. SystemPreferences::~SystemPreferences() {
  22. #if defined(OS_WIN)
  23. Browser::Get()->RemoveObserver(this);
  24. #endif
  25. }
  26. #if !defined(OS_MACOSX)
  27. bool SystemPreferences::IsDarkMode() {
  28. return ui::NativeTheme::GetInstanceForNativeUi()->ShouldUseDarkColors();
  29. }
  30. #endif
  31. bool SystemPreferences::IsInvertedColorScheme() {
  32. return ui::NativeTheme::GetInstanceForNativeUi()
  33. ->GetPlatformHighContrastColorScheme() ==
  34. ui::NativeTheme::PlatformHighContrastColorScheme::kDark;
  35. }
  36. bool SystemPreferences::IsHighContrastColorScheme() {
  37. return ui::NativeTheme::GetInstanceForNativeUi()->UsesHighContrastColors();
  38. }
  39. v8::Local<v8::Value> SystemPreferences::GetAnimationSettings(
  40. v8::Isolate* isolate) {
  41. gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
  42. dict.SetHidden("simple", true);
  43. dict.Set("shouldRenderRichAnimation",
  44. gfx::Animation::ShouldRenderRichAnimation());
  45. dict.Set("scrollAnimationsEnabledBySystem",
  46. gfx::Animation::ScrollAnimationsEnabledBySystem());
  47. dict.Set("prefersReducedMotion", gfx::Animation::PrefersReducedMotion());
  48. return dict.GetHandle();
  49. }
  50. // static
  51. gin::Handle<SystemPreferences> SystemPreferences::Create(v8::Isolate* isolate) {
  52. return gin::CreateHandle(isolate, new SystemPreferences(isolate));
  53. }
  54. // static
  55. void SystemPreferences::BuildPrototype(
  56. v8::Isolate* isolate,
  57. v8::Local<v8::FunctionTemplate> prototype) {
  58. prototype->SetClassName(gin::StringToV8(isolate, "SystemPreferences"));
  59. gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  60. #if defined(OS_WIN) || defined(OS_MACOSX)
  61. .SetMethod("getColor", &SystemPreferences::GetColor)
  62. .SetMethod("getAccentColor", &SystemPreferences::GetAccentColor)
  63. .SetMethod("getMediaAccessStatus",
  64. &SystemPreferences::GetMediaAccessStatus)
  65. #endif
  66. #if defined(OS_WIN)
  67. .SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled)
  68. #elif defined(OS_MACOSX)
  69. .SetMethod("postNotification", &SystemPreferences::PostNotification)
  70. .SetMethod("subscribeNotification",
  71. &SystemPreferences::SubscribeNotification)
  72. .SetMethod("unsubscribeNotification",
  73. &SystemPreferences::UnsubscribeNotification)
  74. .SetMethod("postLocalNotification",
  75. &SystemPreferences::PostLocalNotification)
  76. .SetMethod("subscribeLocalNotification",
  77. &SystemPreferences::SubscribeLocalNotification)
  78. .SetMethod("unsubscribeLocalNotification",
  79. &SystemPreferences::UnsubscribeLocalNotification)
  80. .SetMethod("postWorkspaceNotification",
  81. &SystemPreferences::PostWorkspaceNotification)
  82. .SetMethod("subscribeWorkspaceNotification",
  83. &SystemPreferences::SubscribeWorkspaceNotification)
  84. .SetMethod("unsubscribeWorkspaceNotification",
  85. &SystemPreferences::UnsubscribeWorkspaceNotification)
  86. .SetMethod("registerDefaults", &SystemPreferences::RegisterDefaults)
  87. .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
  88. .SetMethod("setUserDefault", &SystemPreferences::SetUserDefault)
  89. .SetMethod("removeUserDefault", &SystemPreferences::RemoveUserDefault)
  90. .SetMethod("isSwipeTrackingFromScrollEventsEnabled",
  91. &SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
  92. .SetMethod("getEffectiveAppearance",
  93. &SystemPreferences::GetEffectiveAppearance)
  94. .SetMethod("getAppLevelAppearance",
  95. &SystemPreferences::GetAppLevelAppearance)
  96. .SetMethod("setAppLevelAppearance",
  97. &SystemPreferences::SetAppLevelAppearance)
  98. .SetMethod("getSystemColor", &SystemPreferences::GetSystemColor)
  99. .SetMethod("canPromptTouchID", &SystemPreferences::CanPromptTouchID)
  100. .SetMethod("promptTouchID", &SystemPreferences::PromptTouchID)
  101. .SetMethod("isTrustedAccessibilityClient",
  102. &SystemPreferences::IsTrustedAccessibilityClient)
  103. .SetMethod("askForMediaAccess", &SystemPreferences::AskForMediaAccess)
  104. #endif
  105. .SetMethod("isInvertedColorScheme",
  106. &SystemPreferences::IsInvertedColorScheme)
  107. .SetMethod("isHighContrastColorScheme",
  108. &SystemPreferences::IsHighContrastColorScheme)
  109. .SetMethod("isDarkMode", &SystemPreferences::IsDarkMode)
  110. .SetMethod("getAnimationSettings",
  111. &SystemPreferences::GetAnimationSettings);
  112. }
  113. } // namespace api
  114. } // namespace electron
  115. namespace {
  116. using electron::api::SystemPreferences;
  117. void Initialize(v8::Local<v8::Object> exports,
  118. v8::Local<v8::Value> unused,
  119. v8::Local<v8::Context> context,
  120. void* priv) {
  121. v8::Isolate* isolate = context->GetIsolate();
  122. gin_helper::Dictionary dict(isolate, exports);
  123. dict.Set("systemPreferences", SystemPreferences::Create(isolate));
  124. dict.Set("SystemPreferences", SystemPreferences::GetConstructor(isolate)
  125. ->GetFunction(context)
  126. .ToLocalChecked());
  127. }
  128. } // namespace
  129. NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_system_preferences,
  130. Initialize)