electron_api_system_preferences_win.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright (c) 2014 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 <dwmapi.h>
  5. #include <windows.devices.enumeration.h>
  6. #include <wrl/client.h>
  7. #include <iomanip>
  8. #include "shell/browser/api/electron_api_system_preferences.h"
  9. #include "base/win/core_winrt_util.h"
  10. #include "base/win/windows_types.h"
  11. #include "base/win/wrapped_window_proc.h"
  12. #include "shell/common/color_util.h"
  13. #include "ui/base/win/shell.h"
  14. #include "ui/gfx/color_utils.h"
  15. #include "ui/gfx/win/hwnd_util.h"
  16. namespace electron {
  17. namespace {
  18. const wchar_t kSystemPreferencesWindowClass[] =
  19. L"Electron_SystemPreferencesHostWindow";
  20. using ABI::Windows::Devices::Enumeration::DeviceAccessStatus;
  21. using ABI::Windows::Devices::Enumeration::DeviceClass;
  22. using ABI::Windows::Devices::Enumeration::IDeviceAccessInformation;
  23. using ABI::Windows::Devices::Enumeration::IDeviceAccessInformationStatics;
  24. using Microsoft::WRL::ComPtr;
  25. DeviceAccessStatus GetDeviceAccessStatus(DeviceClass device_class) {
  26. ComPtr<IDeviceAccessInformationStatics> dev_access_info_statics;
  27. HRESULT hr = base::win::GetActivationFactory<
  28. IDeviceAccessInformationStatics,
  29. RuntimeClass_Windows_Devices_Enumeration_DeviceAccessInformation>(
  30. &dev_access_info_statics);
  31. if (FAILED(hr)) {
  32. VLOG(1) << "IDeviceAccessInformationStatics failed: " << hr;
  33. return DeviceAccessStatus::DeviceAccessStatus_Allowed;
  34. }
  35. ComPtr<IDeviceAccessInformation> dev_access_info;
  36. hr = dev_access_info_statics->CreateFromDeviceClass(device_class,
  37. &dev_access_info);
  38. if (FAILED(hr)) {
  39. VLOG(1) << "IDeviceAccessInformation failed: " << hr;
  40. return DeviceAccessStatus::DeviceAccessStatus_Allowed;
  41. }
  42. auto status = DeviceAccessStatus::DeviceAccessStatus_Unspecified;
  43. dev_access_info->get_CurrentStatus(&status);
  44. return status;
  45. }
  46. std::string ConvertDeviceAccessStatus(DeviceAccessStatus value) {
  47. switch (value) {
  48. case DeviceAccessStatus::DeviceAccessStatus_Unspecified:
  49. return "not-determined";
  50. case DeviceAccessStatus::DeviceAccessStatus_Allowed:
  51. return "granted";
  52. case DeviceAccessStatus::DeviceAccessStatus_DeniedBySystem:
  53. return "restricted";
  54. case DeviceAccessStatus::DeviceAccessStatus_DeniedByUser:
  55. return "denied";
  56. default:
  57. return "unknown";
  58. }
  59. }
  60. } // namespace
  61. namespace api {
  62. bool SystemPreferences::IsAeroGlassEnabled() {
  63. return ui::win::IsAeroGlassEnabled();
  64. }
  65. std::string hexColorDWORDToRGBA(DWORD color) {
  66. DWORD rgba = color << 8 | color >> 24;
  67. std::ostringstream stream;
  68. stream << std::hex << std::setw(8) << std::setfill('0') << rgba;
  69. return stream.str();
  70. }
  71. std::string SystemPreferences::GetAccentColor() {
  72. DWORD color = 0;
  73. BOOL opaque = FALSE;
  74. if (FAILED(DwmGetColorizationColor(&color, &opaque))) {
  75. return "";
  76. }
  77. return hexColorDWORDToRGBA(color);
  78. }
  79. std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
  80. const std::string& color) {
  81. int id;
  82. if (color == "3d-dark-shadow") {
  83. id = COLOR_3DDKSHADOW;
  84. } else if (color == "3d-face") {
  85. id = COLOR_3DFACE;
  86. } else if (color == "3d-highlight") {
  87. id = COLOR_3DHIGHLIGHT;
  88. } else if (color == "3d-light") {
  89. id = COLOR_3DLIGHT;
  90. } else if (color == "3d-shadow") {
  91. id = COLOR_3DSHADOW;
  92. } else if (color == "active-border") {
  93. id = COLOR_ACTIVEBORDER;
  94. } else if (color == "active-caption") {
  95. id = COLOR_ACTIVECAPTION;
  96. } else if (color == "active-caption-gradient") {
  97. id = COLOR_GRADIENTACTIVECAPTION;
  98. } else if (color == "app-workspace") {
  99. id = COLOR_APPWORKSPACE;
  100. } else if (color == "button-text") {
  101. id = COLOR_BTNTEXT;
  102. } else if (color == "caption-text") {
  103. id = COLOR_CAPTIONTEXT;
  104. } else if (color == "desktop") {
  105. id = COLOR_DESKTOP;
  106. } else if (color == "disabled-text") {
  107. id = COLOR_GRAYTEXT;
  108. } else if (color == "highlight") {
  109. id = COLOR_HIGHLIGHT;
  110. } else if (color == "highlight-text") {
  111. id = COLOR_HIGHLIGHTTEXT;
  112. } else if (color == "hotlight") {
  113. id = COLOR_HOTLIGHT;
  114. } else if (color == "inactive-border") {
  115. id = COLOR_INACTIVEBORDER;
  116. } else if (color == "inactive-caption") {
  117. id = COLOR_INACTIVECAPTION;
  118. } else if (color == "inactive-caption-gradient") {
  119. id = COLOR_GRADIENTINACTIVECAPTION;
  120. } else if (color == "inactive-caption-text") {
  121. id = COLOR_INACTIVECAPTIONTEXT;
  122. } else if (color == "info-background") {
  123. id = COLOR_INFOBK;
  124. } else if (color == "info-text") {
  125. id = COLOR_INFOTEXT;
  126. } else if (color == "menu") {
  127. id = COLOR_MENU;
  128. } else if (color == "menu-highlight") {
  129. id = COLOR_MENUHILIGHT;
  130. } else if (color == "menubar") {
  131. id = COLOR_MENUBAR;
  132. } else if (color == "menu-text") {
  133. id = COLOR_MENUTEXT;
  134. } else if (color == "scrollbar") {
  135. id = COLOR_SCROLLBAR;
  136. } else if (color == "window") {
  137. id = COLOR_WINDOW;
  138. } else if (color == "window-frame") {
  139. id = COLOR_WINDOWFRAME;
  140. } else if (color == "window-text") {
  141. id = COLOR_WINDOWTEXT;
  142. } else {
  143. thrower.ThrowError("Unknown color: " + color);
  144. return "";
  145. }
  146. return ToRGBHex(color_utils::GetSysSkColor(id));
  147. }
  148. std::string SystemPreferences::GetMediaAccessStatus(
  149. gin_helper::ErrorThrower thrower,
  150. const std::string& media_type) {
  151. if (media_type == "camera") {
  152. return ConvertDeviceAccessStatus(
  153. GetDeviceAccessStatus(DeviceClass::DeviceClass_VideoCapture));
  154. } else if (media_type == "microphone") {
  155. return ConvertDeviceAccessStatus(
  156. GetDeviceAccessStatus(DeviceClass::DeviceClass_AudioCapture));
  157. } else if (media_type == "screen") {
  158. return ConvertDeviceAccessStatus(
  159. DeviceAccessStatus::DeviceAccessStatus_Allowed);
  160. } else {
  161. thrower.ThrowError("Invalid media type");
  162. return std::string();
  163. }
  164. }
  165. void SystemPreferences::InitializeWindow() {
  166. inverted_color_scheme_ = IsInvertedColorScheme();
  167. high_contrast_color_scheme_ = IsHighContrastColorScheme();
  168. // Wait until app is ready before creating sys color listener
  169. // Creating this listener before the app is ready causes global shortcuts
  170. // to not fire
  171. if (Browser::Get()->is_ready())
  172. color_change_listener_ =
  173. std::make_unique<gfx::ScopedSysColorChangeListener>(this);
  174. else
  175. Browser::Get()->AddObserver(this);
  176. WNDCLASSEX window_class;
  177. base::win::InitializeWindowClass(
  178. kSystemPreferencesWindowClass,
  179. &base::win::WrappedWindowProc<SystemPreferences::WndProcStatic>, 0, 0, 0,
  180. NULL, NULL, NULL, NULL, NULL, &window_class);
  181. instance_ = window_class.hInstance;
  182. atom_ = RegisterClassEx(&window_class);
  183. // Create an offscreen window for receiving broadcast messages for the system
  184. // colorization color. Create a hidden WS_POPUP window instead of an
  185. // HWND_MESSAGE window, because only top-level windows such as popups can
  186. // receive broadcast messages like "WM_DWMCOLORIZATIONCOLORCHANGED".
  187. window_ = CreateWindow(MAKEINTATOM(atom_), 0, WS_POPUP, 0, 0, 0, 0, 0, 0,
  188. instance_, 0);
  189. gfx::CheckWindowCreated(window_, ::GetLastError());
  190. gfx::SetWindowUserData(window_, this);
  191. }
  192. LRESULT CALLBACK SystemPreferences::WndProcStatic(HWND hwnd,
  193. UINT message,
  194. WPARAM wparam,
  195. LPARAM lparam) {
  196. auto* msg_wnd = reinterpret_cast<SystemPreferences*>(
  197. GetWindowLongPtr(hwnd, GWLP_USERDATA));
  198. if (msg_wnd)
  199. return msg_wnd->WndProc(hwnd, message, wparam, lparam);
  200. else
  201. return ::DefWindowProc(hwnd, message, wparam, lparam);
  202. }
  203. LRESULT CALLBACK SystemPreferences::WndProc(HWND hwnd,
  204. UINT message,
  205. WPARAM wparam,
  206. LPARAM lparam) {
  207. if (message == WM_DWMCOLORIZATIONCOLORCHANGED) {
  208. DWORD new_color = (DWORD)wparam;
  209. std::string new_color_string = hexColorDWORDToRGBA(new_color);
  210. if (new_color_string != current_color_) {
  211. Emit("accent-color-changed", hexColorDWORDToRGBA(new_color));
  212. current_color_ = new_color_string;
  213. }
  214. }
  215. return ::DefWindowProc(hwnd, message, wparam, lparam);
  216. }
  217. void SystemPreferences::OnSysColorChange() {
  218. bool new_inverted_color_scheme = IsInvertedColorScheme();
  219. if (new_inverted_color_scheme != inverted_color_scheme_) {
  220. inverted_color_scheme_ = new_inverted_color_scheme;
  221. Emit("inverted-color-scheme-changed", new_inverted_color_scheme);
  222. }
  223. bool new_high_contrast_color_scheme = IsHighContrastColorScheme();
  224. if (new_high_contrast_color_scheme != high_contrast_color_scheme_) {
  225. high_contrast_color_scheme_ = new_high_contrast_color_scheme;
  226. Emit("high-contrast-color-scheme-changed", new_high_contrast_color_scheme);
  227. }
  228. Emit("color-changed");
  229. }
  230. void SystemPreferences::OnFinishLaunching(
  231. const base::DictionaryValue& launch_info) {
  232. color_change_listener_ =
  233. std::make_unique<gfx::ScopedSysColorChangeListener>(this);
  234. }
  235. } // namespace api
  236. } // namespace electron