]> khamidou — GitHub mirrors - electron.git/commitdiff
Make getAllDisplays return current displays.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 16 Jan 2015 19:39:25 +0000 (11:39 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 16 Jan 2015 19:39:25 +0000 (11:39 -0800)
atom/browser/api/atom_api_screen.cc
atom/browser/api/atom_api_screen.h

index 7d59d46d9ae9305a63dd002110254d07b1d3118d..fdd59a648f0129d7605f1f8548e56ae4a70048bb 100644 (file)
@@ -4,6 +4,8 @@
 
 #include "atom/browser/api/atom_api_screen.h"
 
+#include <algorithm>
+
 #include "atom/browser/browser.h"
 #include "atom/common/native_mate_converters/gfx_converter.h"
 #include "base/bind.h"
@@ -17,10 +19,24 @@ namespace atom {
 
 namespace api {
 
+namespace {
+
+template<class T>
+typename T::iterator FindById(T* container, int id) {
+  auto predicate = [id] (const typename T::value_type& item) -> bool {
+    return item.id() == id;
+  };
+  return std::find_if(container->begin(), container->end(), predicate);
+}
+
+}  // namespace
+
 Screen::Screen(gfx::Screen* screen) : screen_(screen) {
+  screen_->AddObserver(this);
 }
 
 Screen::~Screen() {
+  screen_->RemoveObserver(this);
 }
 
 gfx::Point Screen::GetCursorScreenPoint() {
@@ -32,7 +48,9 @@ gfx::Display Screen::GetPrimaryDisplay() {
 }
 
 std::vector<gfx::Display> Screen::GetAllDisplays() {
-  return screen_->GetAllDisplays();
+  if (displays_.size() == 0)
+    displays_ = screen_->GetAllDisplays();
+  return displays_;
 }
 
 gfx::Display Screen::GetDisplayNearestPoint(const gfx::Point& point) {
@@ -44,10 +62,12 @@ gfx::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) {
 }
 
 void Screen::OnDisplayAdded(const gfx::Display& new_display) {
+  displays_.push_back(new_display);
   Emit("display-added");
 }
 
 void Screen::OnDisplayRemoved(const gfx::Display& old_display) {
+  displays_.erase(FindById(&displays_, old_display.id()));
   Emit("display-removed");
 }
 
index ff5095666abd043ee4a172e6566f556b46eb861d..aa2d49ea16329a29cb9f0d746b65506dd80fd16f 100644 (file)
@@ -48,6 +48,7 @@ class Screen : public mate::EventEmitter,
 
  private:
   gfx::Screen* screen_;
+  std::vector<gfx::Display> displays_;
 
   DISALLOW_COPY_AND_ASSIGN(Screen);
 };