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 } | |
|
oshima
2012/08/27 23:00:46
log if the position is not one of value above.
Jun Mukai
2012/08/28 08:12:42
Done.
| |
| 52 | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 DisplayLayout::DisplayLayout() | |
| 59 : position(RIGHT), offset(0) {} | |
| 60 | |
| 61 DisplayLayout::DisplayLayout(DisplayLayout::Position p, int o) | |
| 62 : position(p), offset(o) {} | |
|
oshima
2012/08/27 23:00:46
you probably want to check the range of both posit
Jun Mukai
2012/08/28 08:12:42
Added DCHECK for position. The offset value valid
| |
| 63 | |
| 64 void DisplayLayout::RegisterJSONConverter( | |
| 65 base::JSONValueConverter<DisplayLayout>* converter) { | |
| 66 converter->RegisterCustomField<Position>( | |
| 67 "position", &DisplayLayout::position, &GetPositionFromString); | |
| 68 converter->RegisterIntField("offset", &DisplayLayout::offset); | |
| 69 } | |
| 70 | |
| 71 bool DisplayLayout::GetValue(base::DictionaryValue* value) { | |
| 72 std::string position_value; | |
| 73 switch (position) { | |
| 74 case TOP: | |
| 75 position_value = "top"; | |
| 76 break; | |
| 77 case BOTTOM: | |
| 78 position_value = "bottom"; | |
| 79 break; | |
| 80 case RIGHT: | |
| 81 position_value = "right"; | |
| 82 break; | |
| 83 case LEFT: | |
| 84 position_value = "left"; | |
| 85 break; | |
|
oshima
2012/08/27 23:00:46
default:
return false;
is better.
Jun Mukai
2012/08/28 08:12:42
Done.
| |
| 86 } | |
| 87 if (position_value.empty()) | |
|
oshima
2012/08/27 23:00:46
if you're worrying about fall through, it should b
Jun Mukai
2012/08/28 08:12:42
removed.
| |
| 88 return false; | |
| 89 | |
| 90 value->SetString("position", position_value); | |
| 91 value->SetInteger("offset", offset); | |
| 92 return true; | |
| 34 } | 93 } |
| 35 | 94 |
| 36 DisplayController::DisplayController() | 95 DisplayController::DisplayController() |
| 37 : secondary_display_layout_(RIGHT), | 96 : dont_warp_mouse_(false) { |
| 38 secondary_display_offset_(0), | |
| 39 dont_warp_mouse_(false) { | |
| 40 aura::Env::GetInstance()->display_manager()->AddObserver(this); | 97 aura::Env::GetInstance()->display_manager()->AddObserver(this); |
| 41 } | 98 } |
| 42 | 99 |
| 43 DisplayController::~DisplayController() { | 100 DisplayController::~DisplayController() { |
| 44 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); | 101 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); |
| 45 // Delete all root window controllers, which deletes root window | 102 // Delete all root window controllers, which deletes root window |
| 46 // from the last so that the primary root window gets deleted last. | 103 // from the last so that the primary root window gets deleted last. |
| 47 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it = | 104 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it = |
| 48 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { | 105 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { |
| 49 internal::RootWindowController* controller = | 106 internal::RootWindowController* controller = |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 root_windows_.begin(); it != root_windows_.end(); ++it) { | 179 root_windows_.begin(); it != root_windows_.end(); ++it) { |
| 123 internal::RootWindowController* controller = | 180 internal::RootWindowController* controller = |
| 124 GetRootWindowController(it->second); | 181 GetRootWindowController(it->second); |
| 125 if (controller) | 182 if (controller) |
| 126 controllers.push_back(controller); | 183 controllers.push_back(controller); |
| 127 } | 184 } |
| 128 return controllers; | 185 return controllers; |
| 129 } | 186 } |
| 130 | 187 |
| 131 void DisplayController::SetSecondaryDisplayLayout( | 188 void DisplayController::SetSecondaryDisplayLayout( |
| 132 SecondaryDisplayLayout layout) { | 189 DisplayLayout::Position position) { |
| 133 secondary_display_layout_ = layout; | 190 default_display_layout_.position = position; |
| 134 UpdateDisplayBoundsForLayout(); | 191 UpdateDisplayBoundsForLayout(); |
| 135 } | 192 } |
| 136 | 193 |
| 137 void DisplayController::SetSecondaryDisplayOffset(int offset) { | 194 void DisplayController::SetSecondaryDisplayOffset(int offset) { |
| 138 secondary_display_offset_ = offset; | 195 default_display_layout_.offset = offset; |
| 139 UpdateDisplayBoundsForLayout(); | 196 UpdateDisplayBoundsForLayout(); |
| 140 } | 197 } |
| 141 | 198 |
| 199 void DisplayController::SetDisplayLayoutForName(const std::string& name, | |
| 200 const base::Value& value) { | |
| 201 base::JSONValueConverter<DisplayLayout> converter; | |
| 202 DisplayLayout layout; | |
| 203 if (!converter.Convert(value, &layout)) | |
| 204 return; | |
| 205 | |
| 206 secondary_layouts_[name] = layout; | |
| 207 UpdateDisplayBoundsForLayout(); | |
| 208 } | |
| 209 | |
| 210 void DisplayController::GetLayoutForDisplay(const gfx::Display& display, | |
| 211 int* layout, int* offset) { | |
| 212 // TODO | |
| 213 } | |
| 214 | |
| 142 bool DisplayController::WarpMouseCursorIfNecessary( | 215 bool DisplayController::WarpMouseCursorIfNecessary( |
| 143 aura::RootWindow* current_root, | 216 aura::RootWindow* current_root, |
| 144 const gfx::Point& point_in_root) { | 217 const gfx::Point& point_in_root) { |
| 145 if (root_windows_.size() < 2 || dont_warp_mouse_) | 218 if (root_windows_.size() < 2 || dont_warp_mouse_) |
| 146 return false; | 219 return false; |
| 147 const float scale = ui::GetDeviceScaleFactor(current_root->layer()); | 220 const float scale = ui::GetDeviceScaleFactor(current_root->layer()); |
| 148 | 221 |
| 149 // The pointer might be outside the |current_root|. Get the root window where | 222 // The pointer might be outside the |current_root|. Get the root window where |
| 150 // the pointer is currently on. | 223 // the pointer is currently on. |
| 151 std::pair<aura::RootWindow*, gfx::Point> actual_location = | 224 std::pair<aura::RootWindow*, gfx::Point> actual_location = |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 | 321 |
| 249 void DisplayController::UpdateDisplayBoundsForLayout() { | 322 void DisplayController::UpdateDisplayBoundsForLayout() { |
| 250 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) { | 323 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) { |
| 251 return; | 324 return; |
| 252 } | 325 } |
| 253 DCHECK_EQ(2, gfx::Screen::GetNumDisplays()); | 326 DCHECK_EQ(2, gfx::Screen::GetNumDisplays()); |
| 254 aura::DisplayManager* display_manager = | 327 aura::DisplayManager* display_manager = |
| 255 aura::Env::GetInstance()->display_manager(); | 328 aura::Env::GetInstance()->display_manager(); |
| 256 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds(); | 329 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds(); |
| 257 gfx::Display* secondary_display = display_manager->GetDisplayAt(1); | 330 gfx::Display* secondary_display = display_manager->GetDisplayAt(1); |
| 331 const std::string& secondary_name = display_manager->GetDisplayNameAt(1); | |
| 258 const gfx::Rect& secondary_bounds = secondary_display->bounds(); | 332 const gfx::Rect& secondary_bounds = secondary_display->bounds(); |
| 259 gfx::Point new_secondary_origin = primary_bounds.origin(); | 333 gfx::Point new_secondary_origin = primary_bounds.origin(); |
| 260 | 334 |
| 261 // TODO(oshima|mukai): Implement more flexible layout. | 335 // TODO(oshima|mukai): Implement more flexible layout. |
| 262 | 336 |
| 337 const DisplayLayout* layout = &default_display_layout_; | |
| 338 std::map<std::string, DisplayLayout>::const_iterator iter = | |
| 339 secondary_layouts_.find(secondary_name); | |
| 340 if (iter != secondary_layouts_.end()) | |
| 341 layout = &iter->second; | |
| 342 | |
| 343 DisplayLayout::Position position = layout->position; | |
| 344 | |
| 263 // Ignore the offset in case the secondary display doesn't share edges with | 345 // Ignore the offset in case the secondary display doesn't share edges with |
| 264 // the primary display. | 346 // the primary display. |
| 265 int offset = secondary_display_offset_; | 347 int offset = layout->offset; |
| 266 if (secondary_display_layout_ == TOP || secondary_display_layout_ == BOTTOM) { | 348 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) { |
| 267 offset = std::min( | 349 offset = std::min( |
| 268 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); | 350 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); |
| 269 offset = std::max( | 351 offset = std::max( |
| 270 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); | 352 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); |
| 271 } else { | 353 } else { |
| 272 offset = std::min( | 354 offset = std::min( |
| 273 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); | 355 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); |
| 274 offset = std::max( | 356 offset = std::max( |
| 275 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); | 357 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); |
| 276 } | 358 } |
| 277 switch (secondary_display_layout_) { | 359 switch (position) { |
| 278 case TOP: | 360 case DisplayLayout::TOP: |
| 279 new_secondary_origin.Offset(offset, -secondary_bounds.height()); | 361 new_secondary_origin.Offset(offset, -secondary_bounds.height()); |
| 280 break; | 362 break; |
| 281 case RIGHT: | 363 case DisplayLayout::RIGHT: |
| 282 new_secondary_origin.Offset(primary_bounds.width(), offset); | 364 new_secondary_origin.Offset(primary_bounds.width(), offset); |
| 283 break; | 365 break; |
| 284 case BOTTOM: | 366 case DisplayLayout::BOTTOM: |
| 285 new_secondary_origin.Offset(offset, primary_bounds.height()); | 367 new_secondary_origin.Offset(offset, primary_bounds.height()); |
| 286 break; | 368 break; |
| 287 case LEFT: | 369 case DisplayLayout::LEFT: |
| 288 new_secondary_origin.Offset(-secondary_bounds.width(), offset); | 370 new_secondary_origin.Offset(-secondary_bounds.width(), offset); |
| 289 break; | 371 break; |
| 290 } | 372 } |
| 291 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); | 373 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); |
| 292 secondary_display->set_bounds( | 374 secondary_display->set_bounds( |
| 293 gfx::Rect(new_secondary_origin, secondary_bounds.size())); | 375 gfx::Rect(new_secondary_origin, secondary_bounds.size())); |
| 294 secondary_display->UpdateWorkAreaFromInsets(insets); | 376 secondary_display->UpdateWorkAreaFromInsets(insets); |
| 295 } | 377 } |
| 296 | 378 |
| 297 } // namespace internal | 379 } // namespace internal |
| 298 } // namespace ash | 380 } // namespace ash |
| OLD | NEW |