1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "ash/screen_ash.h" 6 7 #include "ash/display/display_controller.h" 8 #include "ash/display/display_manager.h" 9 #include "ash/root_window_controller.h" 10 #include "ash/shelf/shelf_layout_manager.h" 11 #include "ash/shelf/shelf_widget.h" 12 #include "ash/shell.h" 13 #include "ash/wm/coordinate_conversion.h" 14 #include "base/logging.h" 15 #include "ui/aura/client/screen_position_client.h" 16 #include "ui/aura/env.h" 17 #include "ui/aura/root_window.h" 18 #include "ui/gfx/display.h" 19 #include "ui/gfx/screen.h" 20 21 namespace ash { 22 23 namespace { 24 internal::DisplayManager* GetDisplayManager() { 25 return Shell::GetInstance()->display_manager(); 26 } 27 28 DisplayController* GetDisplayController() { 29 return Shell::GetInstance()->display_controller(); 30 } 31 } // namespace 32 33 ScreenAsh::ScreenAsh() { 34 } 35 36 ScreenAsh::~ScreenAsh() { 37 } 38 39 // static 40 gfx::Display ScreenAsh::FindDisplayContainingPoint(const gfx::Point& point) { 41 return GetDisplayManager()->FindDisplayContainingPoint(point); 42 } 43 44 // static 45 gfx::Rect ScreenAsh::GetMaximizedWindowBoundsInParent(aura::Window* window) { 46 if (internal::GetRootWindowController(window->GetRootWindow())->shelf()) 47 return GetDisplayWorkAreaBoundsInParent(window); 48 else 49 return GetDisplayBoundsInParent(window); 50 } 51 52 // static 53 gfx::Rect ScreenAsh::GetDisplayBoundsInParent(aura::Window* window) { 54 return ConvertRectFromScreen( 55 window->parent(), 56 Shell::GetScreen()->GetDisplayNearestWindow(window).bounds()); 57 } 58 59 // static 60 gfx::Rect ScreenAsh::GetDisplayWorkAreaBoundsInParent(aura::Window* window) { 61 return ConvertRectFromScreen( 62 window->parent(), 63 Shell::GetScreen()->GetDisplayNearestWindow(window).work_area()); 64 } 65 66 // static 67 gfx::Rect ScreenAsh::ConvertRectToScreen(aura::Window* window, 68 const gfx::Rect& rect) { 69 gfx::Point point = rect.origin(); 70 aura::client::GetScreenPositionClient(window->GetRootWindow())-> 71 ConvertPointToScreen(window, &point); 72 return gfx::Rect(point, rect.size()); 73 } 74 75 // static 76 gfx::Rect ScreenAsh::ConvertRectFromScreen(aura::Window* window, 77 const gfx::Rect& rect) { 78 gfx::Point point = rect.origin(); 79 aura::client::GetScreenPositionClient(window->GetRootWindow())-> 80 ConvertPointFromScreen(window, &point); 81 return gfx::Rect(point, rect.size()); 82 } 83 84 // static 85 const gfx::Display& ScreenAsh::GetSecondaryDisplay() { 86 internal::DisplayManager* display_manager = GetDisplayManager(); 87 CHECK_EQ(2U, display_manager->GetNumDisplays()); 88 return display_manager->GetDisplayAt(0).id() == 89 DisplayController::GetPrimaryDisplay().id() ? 90 display_manager->GetDisplayAt(1) : display_manager->GetDisplayAt(0); 91 } 92 93 // static 94 const gfx::Display& ScreenAsh::GetDisplayForId(int64 display_id) { 95 return GetDisplayManager()->GetDisplayForId(display_id); 96 } 97 98 void ScreenAsh::NotifyBoundsChanged(const gfx::Display& display) { 99 FOR_EACH_OBSERVER(gfx::DisplayObserver, observers_, 100 OnDisplayBoundsChanged(display)); 101 } 102 103 void ScreenAsh::NotifyDisplayAdded(const gfx::Display& display) { 104 FOR_EACH_OBSERVER(gfx::DisplayObserver, observers_, OnDisplayAdded(display)); 105 } 106 107 void ScreenAsh::NotifyDisplayRemoved(const gfx::Display& display) { 108 FOR_EACH_OBSERVER( 109 gfx::DisplayObserver, observers_, OnDisplayRemoved(display)); 110 } 111 112 bool ScreenAsh::IsDIPEnabled() { 113 return true; 114 } 115 116 gfx::Point ScreenAsh::GetCursorScreenPoint() { 117 return aura::Env::GetInstance()->last_mouse_location(); 118 } 119 120 gfx::NativeWindow ScreenAsh::GetWindowUnderCursor() { 121 return GetWindowAtScreenPoint(Shell::GetScreen()->GetCursorScreenPoint()); 122 } 123 124 gfx::NativeWindow ScreenAsh::GetWindowAtScreenPoint(const gfx::Point& point) { 125 return wm::GetRootWindowAt(point)->GetTopWindowContainingPoint(point); 126 } 127 128 int ScreenAsh::GetNumDisplays() const { 129 return DisplayController::GetNumDisplays(); 130 } 131 132 std::vector<gfx::Display> ScreenAsh::GetAllDisplays() const { 133 if (!Shell::HasInstance()) 134 return std::vector<gfx::Display>(1, GetPrimaryDisplay()); 135 return GetDisplayManager()->displays(); 136 } 137 138 gfx::Display ScreenAsh::GetDisplayNearestWindow(gfx::NativeView window) const { 139 if (!Shell::HasInstance()) 140 return GetPrimaryDisplay(); 141 return GetDisplayController()->GetDisplayNearestWindow(window); 142 } 143 144 gfx::Display ScreenAsh::GetDisplayNearestPoint(const gfx::Point& point) const { 145 if (!Shell::HasInstance()) 146 return GetPrimaryDisplay(); 147 return GetDisplayController()->GetDisplayNearestPoint(point); 148 } 149 150 gfx::Display ScreenAsh::GetDisplayMatching(const gfx::Rect& match_rect) const { 151 if (!Shell::HasInstance()) 152 return GetPrimaryDisplay(); 153 return GetDisplayController()->GetDisplayMatching(match_rect); 154 } 155 156 gfx::Display ScreenAsh::GetPrimaryDisplay() const { 157 return DisplayController::GetPrimaryDisplay(); 158 } 159 160 void ScreenAsh::AddObserver(gfx::DisplayObserver* observer) { 161 observers_.AddObserver(observer); 162 } 163 164 void ScreenAsh::RemoveObserver(gfx::DisplayObserver* observer) { 165 observers_.RemoveObserver(observer); 166 } 167 168 } // namespace ash 169