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/property_util.h" 14 #include "ash/wm/coordinate_conversion.h" 15 #include "base/logging.h" 16 #include "ui/aura/client/screen_position_client.h" 17 #include "ui/aura/env.h" 18 #include "ui/aura/root_window.h" 19 #include "ui/gfx/display.h" 20 #include "ui/gfx/screen.h" 21 22 namespace ash { 23 24 namespace { 25 internal::DisplayManager* GetDisplayManager() { 26 return Shell::GetInstance()->display_manager(); 27 } 28 29 DisplayController* GetDisplayController() { 30 return Shell::GetInstance()->display_controller(); 31 } 32 } // namespace 33 34 ScreenAsh::ScreenAsh() { 35 } 36 37 ScreenAsh::~ScreenAsh() { 38 } 39 40 // static 41 gfx::Display ScreenAsh::FindDisplayContainingPoint(const gfx::Point& point) { 42 return GetDisplayManager()->FindDisplayContainingPoint(point); 43 } 44 45 // static 46 gfx::Rect ScreenAsh::GetMaximizedWindowBoundsInParent(aura::Window* window) { 47 if (GetRootWindowController(window->GetRootWindow())->shelf()) 48 return GetDisplayWorkAreaBoundsInParent(window); 49 else 50 return GetDisplayBoundsInParent(window); 51 } 52 53 // static 54 gfx::Rect ScreenAsh::GetDisplayBoundsInParent(aura::Window* window) { 55 return ConvertRectFromScreen( 56 window->parent(), 57 Shell::GetScreen()->GetDisplayNearestWindow(window).bounds()); 58 } 59 60 // static 61 gfx::Rect ScreenAsh::GetDisplayWorkAreaBoundsInParent(aura::Window* window) { 62 return ConvertRectFromScreen( 63 window->parent(), 64 Shell::GetScreen()->GetDisplayNearestWindow(window).work_area()); 65 } 66 67 // static 68 gfx::Rect ScreenAsh::ConvertRectToScreen(aura::Window* window, 69 const gfx::Rect& rect) { 70 gfx::Point point = rect.origin(); 71 aura::client::GetScreenPositionClient(window->GetRootWindow())-> 72 ConvertPointToScreen(window, &point); 73 return gfx::Rect(point, rect.size()); 74 } 75 76 // static 77 gfx::Rect ScreenAsh::ConvertRectFromScreen(aura::Window* window, 78 const gfx::Rect& rect) { 79 gfx::Point point = rect.origin(); 80 aura::client::GetScreenPositionClient(window->GetRootWindow())-> 81 ConvertPointFromScreen(window, &point); 82 return gfx::Rect(point, rect.size()); 83 } 84 85 // static 86 const gfx::Display& ScreenAsh::GetSecondaryDisplay() { 87 internal::DisplayManager* display_manager = GetDisplayManager(); 88 CHECK_EQ(2U, display_manager->GetNumDisplays()); 89 return display_manager->GetDisplayAt(0).id() == 90 DisplayController::GetPrimaryDisplay().id() ? 91 display_manager->GetDisplayAt(1) : display_manager->GetDisplayAt(0); 92 } 93 94 // static 95 const gfx::Display& ScreenAsh::GetDisplayForId(int64 display_id) { 96 return GetDisplayManager()->GetDisplayForId(display_id); 97 } 98 99 void ScreenAsh::NotifyBoundsChanged(const gfx::Display& display) { 100 FOR_EACH_OBSERVER(gfx::DisplayObserver, observers_, 101 OnDisplayBoundsChanged(display)); 102 } 103 104 void ScreenAsh::NotifyDisplayAdded(const gfx::Display& display) { 105 FOR_EACH_OBSERVER(gfx::DisplayObserver, observers_, OnDisplayAdded(display)); 106 } 107 108 void ScreenAsh::NotifyDisplayRemoved(const gfx::Display& display) { 109 FOR_EACH_OBSERVER( 110 gfx::DisplayObserver, observers_, OnDisplayRemoved(display)); 111 } 112 113 bool ScreenAsh::IsDIPEnabled() { 114 return true; 115 } 116 117 gfx::Point ScreenAsh::GetCursorScreenPoint() { 118 return aura::Env::GetInstance()->last_mouse_location(); 119 } 120 121 gfx::NativeWindow ScreenAsh::GetWindowAtCursorScreenPoint() { 122 const gfx::Point point = Shell::GetScreen()->GetCursorScreenPoint(); 123 return wm::GetRootWindowAt(point)->GetTopWindowContainingPoint(point); 124 } 125 126 int ScreenAsh::GetNumDisplays() { 127 return DisplayController::GetNumDisplays(); 128 } 129 130 gfx::Display ScreenAsh::GetDisplayNearestWindow(gfx::NativeView window) const { 131 return GetDisplayController()->GetDisplayNearestWindow(window); 132 } 133 134 gfx::Display ScreenAsh::GetDisplayNearestPoint(const gfx::Point& point) const { 135 return GetDisplayController()->GetDisplayNearestPoint(point); 136 } 137 138 gfx::Display ScreenAsh::GetDisplayMatching(const gfx::Rect& match_rect) const { 139 return GetDisplayController()->GetDisplayMatching(match_rect); 140 } 141 142 gfx::Display ScreenAsh::GetPrimaryDisplay() const { 143 return DisplayController::GetPrimaryDisplay(); 144 } 145 146 void ScreenAsh::AddObserver(gfx::DisplayObserver* observer) { 147 observers_.AddObserver(observer); 148 } 149 150 void ScreenAsh::RemoveObserver(gfx::DisplayObserver* observer) { 151 observers_.RemoveObserver(observer); 152 } 153 154 } // namespace ash 155