atom_api_system_preferences_win.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <iomanip>
  5. #include "atom/browser/api/atom_api_system_preferences.h"
  6. #include "atom/common/color_util.h"
  7. #include "base/win/wrapped_window_proc.h"
  8. #include "ui/base/win/shell.h"
  9. #include "ui/gfx/color_utils.h"
  10. #include "ui/gfx/win/hwnd_util.h"
  11. namespace atom {
  12. namespace {
  13. const wchar_t kSystemPreferencesWindowClass[] =
  14. L"Electron_SystemPreferencesHostWindow";
  15. } // namespace
  16. namespace api {
  17. bool SystemPreferences::IsAeroGlassEnabled() {
  18. return ui::win::IsAeroGlassEnabled();
  19. }
  20. std::string hexColorDWORDToRGBA(DWORD color) {
  21. DWORD rgba = color << 8 | color >> 24;
  22. std::ostringstream stream;
  23. stream << std::hex << std::setw(8) << std::setfill('0') << rgba;
  24. return stream.str();
  25. }
  26. std::string SystemPreferences::GetAccentColor() {
  27. DWORD color = 0;
  28. BOOL opaque = FALSE;
  29. if (FAILED(dwmGetColorizationColor(&color, &opaque))) {
  30. return "";
  31. }
  32. return hexColorDWORDToRGBA(color);
  33. }
  34. std::string SystemPreferences::GetColor(const std::string& color,
  35. mate::Arguments* args) {
  36. int id;
  37. if (color == "3d-dark-shadow") {
  38. id = COLOR_3DDKSHADOW;
  39. } else if (color == "3d-face") {
  40. id = COLOR_3DFACE;
  41. } else if (color == "3d-highlight") {
  42. id = COLOR_3DHIGHLIGHT;
  43. } else if (color == "3d-light") {
  44. id = COLOR_3DLIGHT;
  45. } else if (color == "3d-shadow") {
  46. id = COLOR_3DSHADOW;
  47. } else if (color == "active-border") {
  48. id = COLOR_ACTIVEBORDER;
  49. } else if (color == "active-caption") {
  50. id = COLOR_ACTIVECAPTION;
  51. } else if (color == "active-caption-gradient") {
  52. id = COLOR_GRADIENTACTIVECAPTION;
  53. } else if (color == "app-workspace") {
  54. id = COLOR_APPWORKSPACE;
  55. } else if (color == "button-text") {
  56. id = COLOR_BTNTEXT;
  57. } else if (color == "caption-text") {
  58. id = COLOR_CAPTIONTEXT;
  59. } else if (color == "desktop") {
  60. id = COLOR_DESKTOP;
  61. } else if (color == "disabled-text") {
  62. id = COLOR_GRAYTEXT;
  63. } else if (color == "highlight") {
  64. id = COLOR_HIGHLIGHT;
  65. } else if (color == "highlight-text") {
  66. id = COLOR_HIGHLIGHTTEXT;
  67. } else if (color == "hotlight") {
  68. id = COLOR_HOTLIGHT;
  69. } else if (color == "inactive-border") {
  70. id = COLOR_INACTIVEBORDER;
  71. } else if (color == "inactive-caption") {
  72. id = COLOR_INACTIVECAPTION;
  73. } else if (color == "inactive-caption-gradient") {
  74. id = COLOR_GRADIENTINACTIVECAPTION;
  75. } else if (color == "inactive-caption-text") {
  76. id = COLOR_INACTIVECAPTIONTEXT;
  77. } else if (color == "info-background") {
  78. id = COLOR_INFOBK;
  79. } else if (color == "info-text") {
  80. id = COLOR_INFOTEXT;
  81. } else if (color == "menu") {
  82. id = COLOR_MENU;
  83. } else if (color == "menu-highlight") {
  84. id = COLOR_MENUHILIGHT;
  85. } else if (color == "menubar") {
  86. id = COLOR_MENUBAR;
  87. } else if (color == "menu-text") {
  88. id = COLOR_MENUTEXT;
  89. } else if (color == "scrollbar") {
  90. id = COLOR_SCROLLBAR;
  91. } else if (color == "window") {
  92. id = COLOR_WINDOW;
  93. } else if (color == "window-frame") {
  94. id = COLOR_WINDOWFRAME;
  95. } else if (color == "window-text") {
  96. id = COLOR_WINDOWTEXT;
  97. } else {
  98. args->ThrowError("Unknown color: " + color);
  99. return "";
  100. }
  101. return ToRGBHex(color_utils::GetSysSkColor(id));
  102. }
  103. void SystemPreferences::InitializeWindow() {
  104. invertered_color_scheme_ = IsInvertedColorScheme();
  105. // Wait until app is ready before creating sys color listener
  106. // Creating this listener before the app is ready causes global shortcuts
  107. // to not fire
  108. if (Browser::Get()->is_ready())
  109. color_change_listener_.reset(new gfx::ScopedSysColorChangeListener(this));
  110. else
  111. Browser::Get()->AddObserver(this);
  112. WNDCLASSEX window_class;
  113. base::win::InitializeWindowClass(
  114. kSystemPreferencesWindowClass,
  115. &base::win::WrappedWindowProc<SystemPreferences::WndProcStatic>, 0, 0, 0,
  116. NULL, NULL, NULL, NULL, NULL, &window_class);
  117. instance_ = window_class.hInstance;
  118. atom_ = RegisterClassEx(&window_class);
  119. // Create an offscreen window for receiving broadcast messages for the system
  120. // colorization color. Create a hidden WS_POPUP window instead of an
  121. // HWND_MESSAGE window, because only top-level windows such as popups can
  122. // receive broadcast messages like "WM_DWMCOLORIZATIONCOLORCHANGED".
  123. window_ = CreateWindow(MAKEINTATOM(atom_), 0, WS_POPUP, 0, 0, 0, 0, 0, 0,
  124. instance_, 0);
  125. gfx::CheckWindowCreated(window_);
  126. gfx::SetWindowUserData(window_, this);
  127. }
  128. LRESULT CALLBACK SystemPreferences::WndProcStatic(HWND hwnd,
  129. UINT message,
  130. WPARAM wparam,
  131. LPARAM lparam) {
  132. SystemPreferences* msg_wnd = reinterpret_cast<SystemPreferences*>(
  133. GetWindowLongPtr(hwnd, GWLP_USERDATA));
  134. if (msg_wnd)
  135. return msg_wnd->WndProc(hwnd, message, wparam, lparam);
  136. else
  137. return ::DefWindowProc(hwnd, message, wparam, lparam);
  138. }
  139. LRESULT CALLBACK SystemPreferences::WndProc(HWND hwnd,
  140. UINT message,
  141. WPARAM wparam,
  142. LPARAM lparam) {
  143. if (message == WM_DWMCOLORIZATIONCOLORCHANGED) {
  144. DWORD new_color = (DWORD)wparam;
  145. std::string new_color_string = hexColorDWORDToRGBA(new_color);
  146. if (new_color_string != current_color_) {
  147. Emit("accent-color-changed", hexColorDWORDToRGBA(new_color));
  148. current_color_ = new_color_string;
  149. }
  150. }
  151. return ::DefWindowProc(hwnd, message, wparam, lparam);
  152. }
  153. void SystemPreferences::OnSysColorChange() {
  154. bool new_invertered_color_scheme = IsInvertedColorScheme();
  155. if (new_invertered_color_scheme != invertered_color_scheme_) {
  156. invertered_color_scheme_ = new_invertered_color_scheme;
  157. Emit("inverted-color-scheme-changed", new_invertered_color_scheme);
  158. }
  159. Emit("color-changed");
  160. }
  161. void SystemPreferences::OnFinishLaunching(
  162. const base::DictionaryValue& launch_info) {
  163. color_change_listener_.reset(new gfx::ScopedSysColorChangeListener(this));
  164. }
  165. } // namespace api
  166. } // namespace atom