atom_api_screen.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (c) 2013 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 "atom/browser/api/atom_api_screen.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include "atom/browser/api/atom_api_browser_window.h"
  8. #include "atom/browser/browser.h"
  9. #include "atom/common/native_mate_converters/gfx_converter.h"
  10. #include "atom/common/node_includes.h"
  11. #include "base/bind.h"
  12. #include "native_mate/dictionary.h"
  13. #include "native_mate/object_template_builder.h"
  14. #include "ui/display/display.h"
  15. #include "ui/display/screen.h"
  16. #include "ui/gfx/geometry/point.h"
  17. #if defined(OS_WIN)
  18. #include "ui/display/win/screen_win.h"
  19. #endif
  20. namespace atom {
  21. namespace api {
  22. namespace {
  23. // Find an item in container according to its ID.
  24. template <class T>
  25. typename T::iterator FindById(T* container, int id) {
  26. auto predicate = [id](const typename T::value_type& item) -> bool {
  27. return item.id() == id;
  28. };
  29. return std::find_if(container->begin(), container->end(), predicate);
  30. }
  31. // Convert the changed_metrics bitmask to string array.
  32. std::vector<std::string> MetricsToArray(uint32_t metrics) {
  33. std::vector<std::string> array;
  34. if (metrics & display::DisplayObserver::DISPLAY_METRIC_BOUNDS)
  35. array.push_back("bounds");
  36. if (metrics & display::DisplayObserver::DISPLAY_METRIC_WORK_AREA)
  37. array.push_back("workArea");
  38. if (metrics & display::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR)
  39. array.push_back("scaleFactor");
  40. if (metrics & display::DisplayObserver::DISPLAY_METRIC_ROTATION)
  41. array.push_back("rotation");
  42. return array;
  43. }
  44. void DelayEmit(Screen* screen,
  45. const base::StringPiece& name,
  46. const display::Display& display) {
  47. screen->Emit(name, display);
  48. }
  49. void DelayEmitWithMetrics(Screen* screen,
  50. const base::StringPiece& name,
  51. const display::Display& display,
  52. const std::vector<std::string>& metrics) {
  53. screen->Emit(name, display, metrics);
  54. }
  55. } // namespace
  56. Screen::Screen(v8::Isolate* isolate, display::Screen* screen)
  57. : screen_(screen) {
  58. screen_->AddObserver(this);
  59. Init(isolate);
  60. }
  61. Screen::~Screen() {
  62. screen_->RemoveObserver(this);
  63. }
  64. gfx::Point Screen::GetCursorScreenPoint() {
  65. return screen_->GetCursorScreenPoint();
  66. }
  67. display::Display Screen::GetPrimaryDisplay() {
  68. return screen_->GetPrimaryDisplay();
  69. }
  70. std::vector<display::Display> Screen::GetAllDisplays() {
  71. return screen_->GetAllDisplays();
  72. }
  73. display::Display Screen::GetDisplayNearestPoint(const gfx::Point& point) {
  74. return screen_->GetDisplayNearestPoint(point);
  75. }
  76. display::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) {
  77. return screen_->GetDisplayMatching(match_rect);
  78. }
  79. #if defined(OS_WIN)
  80. static gfx::Rect ScreenToDIPRect(atom::NativeWindow* window,
  81. const gfx::Rect& rect) {
  82. HWND hwnd = window ? window->GetAcceleratedWidget() : nullptr;
  83. return display::win::ScreenWin::ScreenToDIPRect(hwnd, rect);
  84. }
  85. static gfx::Rect DIPToScreenRect(atom::NativeWindow* window,
  86. const gfx::Rect& rect) {
  87. HWND hwnd = window ? window->GetAcceleratedWidget() : nullptr;
  88. return display::win::ScreenWin::DIPToScreenRect(hwnd, rect);
  89. }
  90. #endif
  91. void Screen::OnDisplayAdded(const display::Display& new_display) {
  92. base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
  93. FROM_HERE, base::Bind(&DelayEmit, base::Unretained(this), "display-added",
  94. new_display));
  95. }
  96. void Screen::OnDisplayRemoved(const display::Display& old_display) {
  97. base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
  98. FROM_HERE, base::Bind(&DelayEmit, base::Unretained(this),
  99. "display-removed", old_display));
  100. }
  101. void Screen::OnDisplayMetricsChanged(const display::Display& display,
  102. uint32_t changed_metrics) {
  103. base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
  104. FROM_HERE, base::Bind(&DelayEmitWithMetrics, base::Unretained(this),
  105. "display-metrics-changed", display,
  106. MetricsToArray(changed_metrics)));
  107. }
  108. // static
  109. v8::Local<v8::Value> Screen::Create(v8::Isolate* isolate) {
  110. if (!Browser::Get()->is_ready()) {
  111. isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
  112. isolate, "Cannot require \"screen\" module before app is ready")));
  113. return v8::Null(isolate);
  114. }
  115. display::Screen* screen = display::Screen::GetScreen();
  116. if (!screen) {
  117. isolate->ThrowException(v8::Exception::Error(
  118. mate::StringToV8(isolate, "Failed to get screen information")));
  119. return v8::Null(isolate);
  120. }
  121. return mate::CreateHandle(isolate, new Screen(isolate, screen)).ToV8();
  122. }
  123. // static
  124. void Screen::BuildPrototype(v8::Isolate* isolate,
  125. v8::Local<v8::FunctionTemplate> prototype) {
  126. prototype->SetClassName(mate::StringToV8(isolate, "Screen"));
  127. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  128. .SetMethod("getCursorScreenPoint", &Screen::GetCursorScreenPoint)
  129. .SetMethod("getPrimaryDisplay", &Screen::GetPrimaryDisplay)
  130. .SetMethod("getAllDisplays", &Screen::GetAllDisplays)
  131. .SetMethod("getDisplayNearestPoint", &Screen::GetDisplayNearestPoint)
  132. #if defined(OS_WIN)
  133. .SetMethod("screenToDipPoint", &display::win::ScreenWin::ScreenToDIPPoint)
  134. .SetMethod("dipToScreenPoint", &display::win::ScreenWin::DIPToScreenPoint)
  135. .SetMethod("screenToDipRect", &ScreenToDIPRect)
  136. .SetMethod("dipToScreenRect", &DIPToScreenRect)
  137. #endif
  138. .SetMethod("getDisplayMatching", &Screen::GetDisplayMatching);
  139. }
  140. } // namespace api
  141. } // namespace atom
  142. namespace {
  143. using atom::api::Screen;
  144. void Initialize(v8::Local<v8::Object> exports,
  145. v8::Local<v8::Value> unused,
  146. v8::Local<v8::Context> context,
  147. void* priv) {
  148. v8::Isolate* isolate = context->GetIsolate();
  149. mate::Dictionary dict(isolate, exports);
  150. dict.Set("screen", Screen::Create(isolate));
  151. dict.Set(
  152. "Screen",
  153. Screen::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
  154. }
  155. } // namespace
  156. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_common_screen, Initialize)