Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/display/display_controller.h" | 5 #include "ash/display/display_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/ash_switches.h" | 9 #include "ash/ash_switches.h" |
| 10 #include "ash/display/multi_display_manager.h" | 10 #include "ash/display/multi_display_manager.h" |
| 11 #include "ash/root_window_controller.h" | 11 #include "ash/root_window_controller.h" |
| 12 #include "ash/screen_ash.h" | 12 #include "ash/screen_ash.h" |
| 13 #include "ash/shell.h" | 13 #include "ash/shell.h" |
| 14 #include "ash/wm/coordinate_conversion.h" | 14 #include "ash/wm/coordinate_conversion.h" |
| 15 #include "ash/wm/property_util.h" | 15 #include "ash/wm/property_util.h" |
| 16 #include "ash/wm/window_util.h" | 16 #include "ash/wm/window_util.h" |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/json/json_value_converter.h" | |
| 19 #include "base/string_piece.h" | |
| 20 #include "base/values.h" | |
| 18 #include "ui/aura/client/screen_position_client.h" | 21 #include "ui/aura/client/screen_position_client.h" |
| 19 #include "ui/aura/env.h" | 22 #include "ui/aura/env.h" |
| 20 #include "ui/aura/root_window.h" | 23 #include "ui/aura/root_window.h" |
| 21 #include "ui/aura/window.h" | 24 #include "ui/aura/window.h" |
| 22 #include "ui/compositor/dip_util.h" | 25 #include "ui/compositor/dip_util.h" |
| 23 #include "ui/gfx/display.h" | 26 #include "ui/gfx/display.h" |
| 24 #include "ui/gfx/screen.h" | 27 #include "ui/gfx/screen.h" |
| 25 | 28 |
| 26 namespace ash { | 29 namespace ash { |
| 27 namespace internal { | 30 namespace internal { |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 30 // The number of pixels to overlap between the primary and secondary displays, | 33 // The number of pixels to overlap between the primary and secondary displays, |
| 31 // in case that the offset value is too large. | 34 // in case that the offset value is too large. |
| 32 const int kMinimumOverlapForInvalidOffset = 50; | 35 const int kMinimumOverlapForInvalidOffset = 50; |
| 33 | 36 |
| 37 bool GetPositionFromString(const base::StringPiece& position, | |
| 38 DisplayLayout::Position* field) { | |
| 39 if (position == "top") { | |
| 40 *field = DisplayLayout::TOP; | |
| 41 return true; | |
| 42 } else if (position == "bottom") { | |
| 43 *field = DisplayLayout::BOTTOM; | |
| 44 return true; | |
| 45 } else if (position == "right") { | |
| 46 *field = DisplayLayout::RIGHT; | |
| 47 return true; | |
| 48 } else if (position == "left") { | |
| 49 *field = DisplayLayout::LEFT; | |
| 50 return true; | |
| 51 } | |
| 52 LOG(ERROR) << "Invalid position value: " << position; | |
| 53 | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 DisplayLayout::DisplayLayout() | |
| 60 : position(RIGHT), offset(0) {} | |
| 61 | |
| 62 DisplayLayout::DisplayLayout(DisplayLayout::Position position, int offset) | |
| 63 : position(position), offset(offset) { | |
| 64 DCHECK_LE(DisplayLayout::TOP, position); | |
| 65 DCHECK_GE(DisplayLayout::LEFT, position); | |
|
oshima
2012/08/28 08:55:50
Since this can be read from file, it's probably be
Jun Mukai
2012/08/28 09:51:00
Done.
| |
| 66 } | |
| 67 | |
| 68 void DisplayLayout::RegisterJSONConverter( | |
| 69 base::JSONValueConverter<DisplayLayout>* converter) { | |
| 70 converter->RegisterCustomField<Position>( | |
| 71 "position", &DisplayLayout::position, &GetPositionFromString); | |
| 72 converter->RegisterIntField("offset", &DisplayLayout::offset); | |
| 73 } | |
| 74 | |
| 75 bool DisplayLayout::ConvertToValue(base::DictionaryValue* value) { | |
| 76 std::string position_value; | |
| 77 switch (position) { | |
| 78 case TOP: | |
| 79 position_value = "top"; | |
| 80 break; | |
| 81 case BOTTOM: | |
| 82 position_value = "bottom"; | |
| 83 break; | |
| 84 case RIGHT: | |
| 85 position_value = "right"; | |
| 86 break; | |
| 87 case LEFT: | |
| 88 position_value = "left"; | |
| 89 break; | |
| 90 default: | |
| 91 return false; | |
| 92 } | |
| 93 | |
| 94 value->SetString("position", position_value); | |
| 95 value->SetInteger("offset", offset); | |
| 96 return true; | |
| 34 } | 97 } |
| 35 | 98 |
| 36 DisplayController::DisplayController() | 99 DisplayController::DisplayController() |
| 37 : secondary_display_layout_(RIGHT), | 100 : dont_warp_mouse_(false) { |
| 38 secondary_display_offset_(0), | |
| 39 dont_warp_mouse_(false) { | |
| 40 aura::Env::GetInstance()->display_manager()->AddObserver(this); | 101 aura::Env::GetInstance()->display_manager()->AddObserver(this); |
| 41 } | 102 } |
| 42 | 103 |
| 43 DisplayController::~DisplayController() { | 104 DisplayController::~DisplayController() { |
| 44 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); | 105 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); |
| 45 // Delete all root window controllers, which deletes root window | 106 // Delete all root window controllers, which deletes root window |
| 46 // from the last so that the primary root window gets deleted last. | 107 // from the last so that the primary root window gets deleted last. |
| 47 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it = | 108 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it = |
| 48 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { | 109 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { |
| 49 internal::RootWindowController* controller = | 110 internal::RootWindowController* controller = |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 for (std::map<int64, aura::RootWindow*>::const_iterator it = | 182 for (std::map<int64, aura::RootWindow*>::const_iterator it = |
| 122 root_windows_.begin(); it != root_windows_.end(); ++it) { | 183 root_windows_.begin(); it != root_windows_.end(); ++it) { |
| 123 internal::RootWindowController* controller = | 184 internal::RootWindowController* controller = |
| 124 GetRootWindowController(it->second); | 185 GetRootWindowController(it->second); |
| 125 if (controller) | 186 if (controller) |
| 126 controllers.push_back(controller); | 187 controllers.push_back(controller); |
| 127 } | 188 } |
| 128 return controllers; | 189 return controllers; |
| 129 } | 190 } |
| 130 | 191 |
| 131 void DisplayController::SetSecondaryDisplayLayout( | 192 void DisplayController::SetDefaultDisplayLayout(const DisplayLayout& layout) { |
| 132 SecondaryDisplayLayout layout) { | 193 default_display_layout_ = layout; |
| 133 secondary_display_layout_ = layout; | |
| 134 UpdateDisplayBoundsForLayout(); | 194 UpdateDisplayBoundsForLayout(); |
| 135 } | 195 } |
| 136 | 196 |
| 137 void DisplayController::SetSecondaryDisplayOffset(int offset) { | 197 void DisplayController::SetLayoutForDisplayName(const std::string& name, |
| 138 secondary_display_offset_ = offset; | 198 const DisplayLayout& layout) { |
| 199 secondary_layouts_[name] = layout; | |
| 139 UpdateDisplayBoundsForLayout(); | 200 UpdateDisplayBoundsForLayout(); |
| 140 } | 201 } |
| 141 | 202 |
| 203 const DisplayLayout& DisplayController::GetLayoutForDisplayName( | |
| 204 const std::string& name) { | |
| 205 std::map<std::string, DisplayLayout>::const_iterator it = | |
| 206 secondary_layouts_.find(name); | |
| 207 | |
| 208 if (it != secondary_layouts_.end()) | |
| 209 return it->second; | |
| 210 else | |
| 211 return default_display_layout_; | |
| 212 } | |
| 213 | |
| 142 bool DisplayController::WarpMouseCursorIfNecessary( | 214 bool DisplayController::WarpMouseCursorIfNecessary( |
| 143 aura::RootWindow* current_root, | 215 aura::RootWindow* current_root, |
| 144 const gfx::Point& point_in_root) { | 216 const gfx::Point& point_in_root) { |
| 145 if (root_windows_.size() < 2 || dont_warp_mouse_) | 217 if (root_windows_.size() < 2 || dont_warp_mouse_) |
| 146 return false; | 218 return false; |
| 147 const float scale = ui::GetDeviceScaleFactor(current_root->layer()); | 219 const float scale = ui::GetDeviceScaleFactor(current_root->layer()); |
| 148 | 220 |
| 149 // The pointer might be outside the |current_root|. Get the root window where | 221 // The pointer might be outside the |current_root|. Get the root window where |
| 150 // the pointer is currently on. | 222 // the pointer is currently on. |
| 151 std::pair<aura::RootWindow*, gfx::Point> actual_location = | 223 std::pair<aura::RootWindow*, gfx::Point> actual_location = |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 | 320 |
| 249 void DisplayController::UpdateDisplayBoundsForLayout() { | 321 void DisplayController::UpdateDisplayBoundsForLayout() { |
| 250 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) { | 322 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) { |
| 251 return; | 323 return; |
| 252 } | 324 } |
| 253 DCHECK_EQ(2, gfx::Screen::GetNumDisplays()); | 325 DCHECK_EQ(2, gfx::Screen::GetNumDisplays()); |
| 254 aura::DisplayManager* display_manager = | 326 aura::DisplayManager* display_manager = |
| 255 aura::Env::GetInstance()->display_manager(); | 327 aura::Env::GetInstance()->display_manager(); |
| 256 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds(); | 328 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds(); |
| 257 gfx::Display* secondary_display = display_manager->GetDisplayAt(1); | 329 gfx::Display* secondary_display = display_manager->GetDisplayAt(1); |
| 330 const std::string& secondary_name = display_manager->GetDisplayNameAt(1); | |
| 258 const gfx::Rect& secondary_bounds = secondary_display->bounds(); | 331 const gfx::Rect& secondary_bounds = secondary_display->bounds(); |
| 259 gfx::Point new_secondary_origin = primary_bounds.origin(); | 332 gfx::Point new_secondary_origin = primary_bounds.origin(); |
| 260 | 333 |
| 261 // TODO(oshima|mukai): Implement more flexible layout. | 334 // TODO(oshima|mukai): Implement more flexible layout. |
|
oshima
2012/08/28 08:55:50
we don't need this TODO any more, correct?
Jun Mukai
2012/08/28 09:51:00
Removed.
| |
| 262 | 335 |
| 336 const DisplayLayout* layout = &default_display_layout_; | |
| 337 std::map<std::string, DisplayLayout>::const_iterator iter = | |
| 338 secondary_layouts_.find(secondary_name); | |
| 339 if (iter != secondary_layouts_.end()) | |
| 340 layout = &iter->second; | |
| 341 | |
| 342 DisplayLayout::Position position = layout->position; | |
| 343 | |
| 263 // Ignore the offset in case the secondary display doesn't share edges with | 344 // Ignore the offset in case the secondary display doesn't share edges with |
| 264 // the primary display. | 345 // the primary display. |
| 265 int offset = secondary_display_offset_; | 346 int offset = layout->offset; |
| 266 if (secondary_display_layout_ == TOP || secondary_display_layout_ == BOTTOM) { | 347 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) { |
| 267 offset = std::min( | 348 offset = std::min( |
| 268 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); | 349 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); |
| 269 offset = std::max( | 350 offset = std::max( |
| 270 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); | 351 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); |
| 271 } else { | 352 } else { |
| 272 offset = std::min( | 353 offset = std::min( |
| 273 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); | 354 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); |
| 274 offset = std::max( | 355 offset = std::max( |
| 275 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); | 356 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); |
| 276 } | 357 } |
| 277 switch (secondary_display_layout_) { | 358 switch (position) { |
| 278 case TOP: | 359 case DisplayLayout::TOP: |
| 279 new_secondary_origin.Offset(offset, -secondary_bounds.height()); | 360 new_secondary_origin.Offset(offset, -secondary_bounds.height()); |
| 280 break; | 361 break; |
| 281 case RIGHT: | 362 case DisplayLayout::RIGHT: |
| 282 new_secondary_origin.Offset(primary_bounds.width(), offset); | 363 new_secondary_origin.Offset(primary_bounds.width(), offset); |
| 283 break; | 364 break; |
| 284 case BOTTOM: | 365 case DisplayLayout::BOTTOM: |
| 285 new_secondary_origin.Offset(offset, primary_bounds.height()); | 366 new_secondary_origin.Offset(offset, primary_bounds.height()); |
| 286 break; | 367 break; |
| 287 case LEFT: | 368 case DisplayLayout::LEFT: |
| 288 new_secondary_origin.Offset(-secondary_bounds.width(), offset); | 369 new_secondary_origin.Offset(-secondary_bounds.width(), offset); |
| 289 break; | 370 break; |
| 290 } | 371 } |
| 291 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); | 372 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); |
| 292 secondary_display->set_bounds( | 373 secondary_display->set_bounds( |
| 293 gfx::Rect(new_secondary_origin, secondary_bounds.size())); | 374 gfx::Rect(new_secondary_origin, secondary_bounds.size())); |
| 294 secondary_display->UpdateWorkAreaFromInsets(insets); | 375 secondary_display->UpdateWorkAreaFromInsets(insets); |
| 295 } | 376 } |
| 296 | 377 |
| 297 } // namespace internal | 378 } // namespace internal |
| 298 } // namespace ash | 379 } // namespace ash |
| OLD | NEW |