Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(390)

Side by Side Diff: ash/display/display_controller.cc

Issue 10870036: Allow storing display preferences per device. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 {
28 namespace { 30 namespace {
29 31
32 // The maximum value for 'offset' in DisplayLayout in case of outliers. Need
33 // to change this value in case to support even larger displays.
34 const int kMaxValidOffset = 10000;
35
30 // The number of pixels to overlap between the primary and secondary displays, 36 // The number of pixels to overlap between the primary and secondary displays,
31 // in case that the offset value is too large. 37 // in case that the offset value is too large.
32 const int kMinimumOverlapForInvalidOffset = 50; 38 const int kMinimumOverlapForInvalidOffset = 50;
33 39
40 bool GetPositionFromString(const base::StringPiece& position,
41 DisplayLayout::Position* field) {
42 if (position == "top") {
43 *field = DisplayLayout::TOP;
44 return true;
45 } else if (position == "bottom") {
46 *field = DisplayLayout::BOTTOM;
47 return true;
48 } else if (position == "right") {
49 *field = DisplayLayout::RIGHT;
50 return true;
51 } else if (position == "left") {
52 *field = DisplayLayout::LEFT;
53 return true;
54 }
55 LOG(ERROR) << "Invalid position value: " << position;
56
57 return false;
58 }
59
60 } // namespace
61
62 DisplayLayout::DisplayLayout()
63 : position(RIGHT),
64 offset(0) {}
65
66 DisplayLayout::DisplayLayout(DisplayLayout::Position position, int offset)
67 : position(position),
68 offset(offset) {
69 DCHECK_LE(TOP, position);
70 DCHECK_GE(LEFT, position);
71
72 // Set the default value to |position| in case position is invalid. DCHECKs
73 // above doesn't stop in Release builds.
74 if (TOP > position || LEFT < position)
75 this->position = RIGHT;
76
77 DCHECK_GE(kMaxValidOffset, abs(offset));
78 }
79
80 // static
81 bool DisplayLayout::ConvertFromValue(const base::Value& value,
82 DisplayLayout* layout) {
83 base::JSONValueConverter<DisplayLayout> converter;
84 return converter.Convert(value, layout);
85 }
86
87 // static
88 bool DisplayLayout::ConvertToValue(const DisplayLayout& layout,
89 base::Value* value) {
90 base::DictionaryValue* dict_value = NULL;
91 if (!value->GetAsDictionary(&dict_value) || dict_value == NULL)
92 return false;
93
94 std::string position_value;
95 switch (layout.position) {
96 case TOP:
97 position_value = "top";
98 break;
99 case BOTTOM:
100 position_value = "bottom";
101 break;
102 case RIGHT:
103 position_value = "right";
104 break;
105 case LEFT:
106 position_value = "left";
107 break;
108 default:
109 return false;
110 }
111
112 dict_value->SetString("position", position_value);
113 dict_value->SetInteger("offset", layout.offset);
114 return true;
115 }
116
117 // static
118 void DisplayLayout::RegisterJSONConverter(
119 base::JSONValueConverter<DisplayLayout>* converter) {
120 converter->RegisterCustomField<Position>(
121 "position", &DisplayLayout::position, &GetPositionFromString);
122 converter->RegisterIntField("offset", &DisplayLayout::offset);
34 } 123 }
35 124
36 DisplayController::DisplayController() 125 DisplayController::DisplayController()
37 : secondary_display_layout_(RIGHT), 126 : dont_warp_mouse_(false) {
38 secondary_display_offset_(0),
39 dont_warp_mouse_(false) {
40 aura::Env::GetInstance()->display_manager()->AddObserver(this); 127 aura::Env::GetInstance()->display_manager()->AddObserver(this);
41 } 128 }
42 129
43 DisplayController::~DisplayController() { 130 DisplayController::~DisplayController() {
44 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); 131 aura::Env::GetInstance()->display_manager()->RemoveObserver(this);
45 // Delete all root window controllers, which deletes root window 132 // Delete all root window controllers, which deletes root window
46 // from the last so that the primary root window gets deleted last. 133 // from the last so that the primary root window gets deleted last.
47 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it = 134 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it =
48 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { 135 root_windows_.rbegin(); it != root_windows_.rend(); ++it) {
49 internal::RootWindowController* controller = 136 internal::RootWindowController* controller =
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 for (std::map<int64, aura::RootWindow*>::const_iterator it = 208 for (std::map<int64, aura::RootWindow*>::const_iterator it =
122 root_windows_.begin(); it != root_windows_.end(); ++it) { 209 root_windows_.begin(); it != root_windows_.end(); ++it) {
123 internal::RootWindowController* controller = 210 internal::RootWindowController* controller =
124 GetRootWindowController(it->second); 211 GetRootWindowController(it->second);
125 if (controller) 212 if (controller)
126 controllers.push_back(controller); 213 controllers.push_back(controller);
127 } 214 }
128 return controllers; 215 return controllers;
129 } 216 }
130 217
131 void DisplayController::SetSecondaryDisplayLayout( 218 void DisplayController::SetDefaultDisplayLayout(const DisplayLayout& layout) {
132 SecondaryDisplayLayout layout) { 219 default_display_layout_ = layout;
133 secondary_display_layout_ = layout;
134 UpdateDisplayBoundsForLayout(); 220 UpdateDisplayBoundsForLayout();
135 } 221 }
136 222
137 void DisplayController::SetSecondaryDisplayOffset(int offset) { 223 void DisplayController::SetLayoutForDisplayName(const std::string& name,
138 secondary_display_offset_ = offset; 224 const DisplayLayout& layout) {
225 secondary_layouts_[name] = layout;
139 UpdateDisplayBoundsForLayout(); 226 UpdateDisplayBoundsForLayout();
140 } 227 }
141 228
229 const DisplayLayout& DisplayController::GetLayoutForDisplayName(
230 const std::string& name) {
231 std::map<std::string, DisplayLayout>::const_iterator it =
232 secondary_layouts_.find(name);
233
234 if (it != secondary_layouts_.end())
235 return it->second;
236 return default_display_layout_;
237 }
238
142 bool DisplayController::WarpMouseCursorIfNecessary( 239 bool DisplayController::WarpMouseCursorIfNecessary(
143 aura::RootWindow* current_root, 240 aura::RootWindow* current_root,
144 const gfx::Point& point_in_root) { 241 const gfx::Point& point_in_root) {
145 if (root_windows_.size() < 2 || dont_warp_mouse_) 242 if (root_windows_.size() < 2 || dont_warp_mouse_)
146 return false; 243 return false;
147 const float scale = ui::GetDeviceScaleFactor(current_root->layer()); 244 const float scale = ui::GetDeviceScaleFactor(current_root->layer());
148 245
149 // The pointer might be outside the |current_root|. Get the root window where 246 // The pointer might be outside the |current_root|. Get the root window where
150 // the pointer is currently on. 247 // the pointer is currently on.
151 std::pair<aura::RootWindow*, gfx::Point> actual_location = 248 std::pair<aura::RootWindow*, gfx::Point> actual_location =
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 345
249 void DisplayController::UpdateDisplayBoundsForLayout() { 346 void DisplayController::UpdateDisplayBoundsForLayout() {
250 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) { 347 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) {
251 return; 348 return;
252 } 349 }
253 DCHECK_EQ(2, gfx::Screen::GetNumDisplays()); 350 DCHECK_EQ(2, gfx::Screen::GetNumDisplays());
254 aura::DisplayManager* display_manager = 351 aura::DisplayManager* display_manager =
255 aura::Env::GetInstance()->display_manager(); 352 aura::Env::GetInstance()->display_manager();
256 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds(); 353 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds();
257 gfx::Display* secondary_display = display_manager->GetDisplayAt(1); 354 gfx::Display* secondary_display = display_manager->GetDisplayAt(1);
355 const std::string& secondary_name = display_manager->GetDisplayNameAt(1);
258 const gfx::Rect& secondary_bounds = secondary_display->bounds(); 356 const gfx::Rect& secondary_bounds = secondary_display->bounds();
259 gfx::Point new_secondary_origin = primary_bounds.origin(); 357 gfx::Point new_secondary_origin = primary_bounds.origin();
260 358
261 // TODO(oshima|mukai): Implement more flexible layout. 359 const DisplayLayout* layout = &default_display_layout_;
360 std::map<std::string, DisplayLayout>::const_iterator iter =
361 secondary_layouts_.find(secondary_name);
362 if (iter != secondary_layouts_.end())
363 layout = &iter->second;
364
365 DisplayLayout::Position position = layout->position;
262 366
263 // Ignore the offset in case the secondary display doesn't share edges with 367 // Ignore the offset in case the secondary display doesn't share edges with
264 // the primary display. 368 // the primary display.
265 int offset = secondary_display_offset_; 369 int offset = layout->offset;
266 if (secondary_display_layout_ == TOP || secondary_display_layout_ == BOTTOM) { 370 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) {
267 offset = std::min( 371 offset = std::min(
268 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); 372 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset);
269 offset = std::max( 373 offset = std::max(
270 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); 374 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset);
271 } else { 375 } else {
272 offset = std::min( 376 offset = std::min(
273 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); 377 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset);
274 offset = std::max( 378 offset = std::max(
275 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); 379 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset);
276 } 380 }
277 switch (secondary_display_layout_) { 381 switch (position) {
278 case TOP: 382 case DisplayLayout::TOP:
279 new_secondary_origin.Offset(offset, -secondary_bounds.height()); 383 new_secondary_origin.Offset(offset, -secondary_bounds.height());
280 break; 384 break;
281 case RIGHT: 385 case DisplayLayout::RIGHT:
282 new_secondary_origin.Offset(primary_bounds.width(), offset); 386 new_secondary_origin.Offset(primary_bounds.width(), offset);
283 break; 387 break;
284 case BOTTOM: 388 case DisplayLayout::BOTTOM:
285 new_secondary_origin.Offset(offset, primary_bounds.height()); 389 new_secondary_origin.Offset(offset, primary_bounds.height());
286 break; 390 break;
287 case LEFT: 391 case DisplayLayout::LEFT:
288 new_secondary_origin.Offset(-secondary_bounds.width(), offset); 392 new_secondary_origin.Offset(-secondary_bounds.width(), offset);
289 break; 393 break;
290 } 394 }
291 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); 395 gfx::Insets insets = secondary_display->GetWorkAreaInsets();
292 secondary_display->set_bounds( 396 secondary_display->set_bounds(
293 gfx::Rect(new_secondary_origin, secondary_bounds.size())); 397 gfx::Rect(new_secondary_origin, secondary_bounds.size()));
294 secondary_display->UpdateWorkAreaFromInsets(insets); 398 secondary_display->UpdateWorkAreaFromInsets(insets);
295 } 399 }
296 400
297 } // namespace internal
298 } // namespace ash 401 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698