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

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: 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 { 30 namespace internal {
28 namespace { 31 namespace {
29 32
33 // The maximum value for 'offset' in DisplayLayout in case of outliers. Need
34 // to change this value in case to support even larger displays.
35 const int kMaxValidOffset = 10000;
36
30 // The number of pixels to overlap between the primary and secondary displays, 37 // The number of pixels to overlap between the primary and secondary displays,
31 // in case that the offset value is too large. 38 // in case that the offset value is too large.
32 const int kMinimumOverlapForInvalidOffset = 50; 39 const int kMinimumOverlapForInvalidOffset = 50;
33 40
41 bool GetPositionFromString(const base::StringPiece& position,
42 DisplayLayout::Position* field) {
43 if (position == "top") {
44 *field = DisplayLayout::TOP;
45 return true;
46 } else if (position == "bottom") {
47 *field = DisplayLayout::BOTTOM;
48 return true;
49 } else if (position == "right") {
50 *field = DisplayLayout::RIGHT;
51 return true;
52 } else if (position == "left") {
53 *field = DisplayLayout::LEFT;
54 return true;
55 }
56 LOG(ERROR) << "Invalid position value: " << position;
57
58 return false;
59 }
60
61 } // namespace
62
63 DisplayLayout::DisplayLayout()
64 : position(RIGHT), offset(0) {}
65
66 DisplayLayout::DisplayLayout(DisplayLayout::Position position, int offset)
67 : position(position), offset(offset) {
68 DCHECK_LE(TOP, position);
69 DCHECK_GE(LEFT, position);
70
71 if (TOP > position || LEFT < position)
oshima 2012/08/29 00:10:44 Add comment why we're doing this.
Jun Mukai 2012/08/29 02:15:03 Done.
72 this->position = RIGHT;
73
74 DCHECK_GE(kMaxValidOffset, offset);
oshima 2012/08/29 00:10:44 DCHECK_GE(kMaxValidOffsetAbs, abs(offset))
Jun Mukai 2012/08/29 02:15:03 Done.
75 }
76
77 void DisplayLayout::RegisterJSONConverter(
78 base::JSONValueConverter<DisplayLayout>* converter) {
79 converter->RegisterCustomField<Position>(
80 "position", &DisplayLayout::position, &GetPositionFromString);
81 converter->RegisterIntField("offset", &DisplayLayout::offset);
82 }
83
84 bool DisplayLayout::ConvertToValue(base::DictionaryValue* value) {
85 std::string position_value;
86 switch (position) {
87 case TOP:
88 position_value = "top";
89 break;
90 case BOTTOM:
91 position_value = "bottom";
92 break;
93 case RIGHT:
94 position_value = "right";
95 break;
96 case LEFT:
97 position_value = "left";
98 break;
99 default:
100 return false;
101 }
102
103 value->SetString("position", position_value);
104 value->SetInteger("offset", offset);
105 return true;
34 } 106 }
35 107
36 DisplayController::DisplayController() 108 DisplayController::DisplayController()
37 : secondary_display_layout_(RIGHT), 109 : dont_warp_mouse_(false) {
38 secondary_display_offset_(0),
39 dont_warp_mouse_(false) {
40 aura::Env::GetInstance()->display_manager()->AddObserver(this); 110 aura::Env::GetInstance()->display_manager()->AddObserver(this);
41 } 111 }
42 112
43 DisplayController::~DisplayController() { 113 DisplayController::~DisplayController() {
44 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); 114 aura::Env::GetInstance()->display_manager()->RemoveObserver(this);
45 // Delete all root window controllers, which deletes root window 115 // Delete all root window controllers, which deletes root window
46 // from the last so that the primary root window gets deleted last. 116 // from the last so that the primary root window gets deleted last.
47 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it = 117 for (std::map<int64, aura::RootWindow*>::const_reverse_iterator it =
48 root_windows_.rbegin(); it != root_windows_.rend(); ++it) { 118 root_windows_.rbegin(); it != root_windows_.rend(); ++it) {
49 internal::RootWindowController* controller = 119 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 = 191 for (std::map<int64, aura::RootWindow*>::const_iterator it =
122 root_windows_.begin(); it != root_windows_.end(); ++it) { 192 root_windows_.begin(); it != root_windows_.end(); ++it) {
123 internal::RootWindowController* controller = 193 internal::RootWindowController* controller =
124 GetRootWindowController(it->second); 194 GetRootWindowController(it->second);
125 if (controller) 195 if (controller)
126 controllers.push_back(controller); 196 controllers.push_back(controller);
127 } 197 }
128 return controllers; 198 return controllers;
129 } 199 }
130 200
131 void DisplayController::SetSecondaryDisplayLayout( 201 void DisplayController::SetDefaultDisplayLayout(const DisplayLayout& layout) {
132 SecondaryDisplayLayout layout) { 202 default_display_layout_ = layout;
133 secondary_display_layout_ = layout;
134 UpdateDisplayBoundsForLayout(); 203 UpdateDisplayBoundsForLayout();
135 } 204 }
136 205
137 void DisplayController::SetSecondaryDisplayOffset(int offset) { 206 void DisplayController::SetLayoutForDisplayName(const std::string& name,
138 secondary_display_offset_ = offset; 207 const DisplayLayout& layout) {
208 secondary_layouts_[name] = layout;
139 UpdateDisplayBoundsForLayout(); 209 UpdateDisplayBoundsForLayout();
140 } 210 }
141 211
212 const DisplayLayout& DisplayController::GetLayoutForDisplayName(
213 const std::string& name) {
214 std::map<std::string, DisplayLayout>::const_iterator it =
215 secondary_layouts_.find(name);
216
217 if (it != secondary_layouts_.end())
218 return it->second;
219 else
220 return default_display_layout_;
oshima 2012/08/29 00:10:44 just return (no else)
Jun Mukai 2012/08/29 02:15:03 Done.
221 }
222
142 bool DisplayController::WarpMouseCursorIfNecessary( 223 bool DisplayController::WarpMouseCursorIfNecessary(
143 aura::RootWindow* current_root, 224 aura::RootWindow* current_root,
144 const gfx::Point& point_in_root) { 225 const gfx::Point& point_in_root) {
145 if (root_windows_.size() < 2 || dont_warp_mouse_) 226 if (root_windows_.size() < 2 || dont_warp_mouse_)
146 return false; 227 return false;
147 const float scale = ui::GetDeviceScaleFactor(current_root->layer()); 228 const float scale = ui::GetDeviceScaleFactor(current_root->layer());
148 229
149 // The pointer might be outside the |current_root|. Get the root window where 230 // The pointer might be outside the |current_root|. Get the root window where
150 // the pointer is currently on. 231 // the pointer is currently on.
151 std::pair<aura::RootWindow*, gfx::Point> actual_location = 232 std::pair<aura::RootWindow*, gfx::Point> actual_location =
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 329
249 void DisplayController::UpdateDisplayBoundsForLayout() { 330 void DisplayController::UpdateDisplayBoundsForLayout() {
250 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) { 331 if (!IsExtendedDesktopEnabled() || gfx::Screen::GetNumDisplays() <= 1) {
251 return; 332 return;
252 } 333 }
253 DCHECK_EQ(2, gfx::Screen::GetNumDisplays()); 334 DCHECK_EQ(2, gfx::Screen::GetNumDisplays());
254 aura::DisplayManager* display_manager = 335 aura::DisplayManager* display_manager =
255 aura::Env::GetInstance()->display_manager(); 336 aura::Env::GetInstance()->display_manager();
256 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds(); 337 const gfx::Rect& primary_bounds = display_manager->GetDisplayAt(0)->bounds();
257 gfx::Display* secondary_display = display_manager->GetDisplayAt(1); 338 gfx::Display* secondary_display = display_manager->GetDisplayAt(1);
339 const std::string& secondary_name = display_manager->GetDisplayNameAt(1);
258 const gfx::Rect& secondary_bounds = secondary_display->bounds(); 340 const gfx::Rect& secondary_bounds = secondary_display->bounds();
259 gfx::Point new_secondary_origin = primary_bounds.origin(); 341 gfx::Point new_secondary_origin = primary_bounds.origin();
260 342
261 // TODO(oshima|mukai): Implement more flexible layout. 343 const DisplayLayout* layout = &default_display_layout_;
344 std::map<std::string, DisplayLayout>::const_iterator iter =
345 secondary_layouts_.find(secondary_name);
346 if (iter != secondary_layouts_.end())
347 layout = &iter->second;
348
349 DisplayLayout::Position position = layout->position;
262 350
263 // Ignore the offset in case the secondary display doesn't share edges with 351 // Ignore the offset in case the secondary display doesn't share edges with
264 // the primary display. 352 // the primary display.
265 int offset = secondary_display_offset_; 353 int offset = layout->offset;
266 if (secondary_display_layout_ == TOP || secondary_display_layout_ == BOTTOM) { 354 if (position == DisplayLayout::TOP || position == DisplayLayout::BOTTOM) {
267 offset = std::min( 355 offset = std::min(
268 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset); 356 offset, primary_bounds.width() - kMinimumOverlapForInvalidOffset);
269 offset = std::max( 357 offset = std::max(
270 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset); 358 offset, -secondary_bounds.width() + kMinimumOverlapForInvalidOffset);
271 } else { 359 } else {
272 offset = std::min( 360 offset = std::min(
273 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset); 361 offset, primary_bounds.height() - kMinimumOverlapForInvalidOffset);
274 offset = std::max( 362 offset = std::max(
275 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset); 363 offset, -secondary_bounds.height() + kMinimumOverlapForInvalidOffset);
276 } 364 }
277 switch (secondary_display_layout_) { 365 switch (position) {
278 case TOP: 366 case DisplayLayout::TOP:
279 new_secondary_origin.Offset(offset, -secondary_bounds.height()); 367 new_secondary_origin.Offset(offset, -secondary_bounds.height());
280 break; 368 break;
281 case RIGHT: 369 case DisplayLayout::RIGHT:
282 new_secondary_origin.Offset(primary_bounds.width(), offset); 370 new_secondary_origin.Offset(primary_bounds.width(), offset);
283 break; 371 break;
284 case BOTTOM: 372 case DisplayLayout::BOTTOM:
285 new_secondary_origin.Offset(offset, primary_bounds.height()); 373 new_secondary_origin.Offset(offset, primary_bounds.height());
286 break; 374 break;
287 case LEFT: 375 case DisplayLayout::LEFT:
288 new_secondary_origin.Offset(-secondary_bounds.width(), offset); 376 new_secondary_origin.Offset(-secondary_bounds.width(), offset);
289 break; 377 break;
290 } 378 }
291 gfx::Insets insets = secondary_display->GetWorkAreaInsets(); 379 gfx::Insets insets = secondary_display->GetWorkAreaInsets();
292 secondary_display->set_bounds( 380 secondary_display->set_bounds(
293 gfx::Rect(new_secondary_origin, secondary_bounds.size())); 381 gfx::Rect(new_secondary_origin, secondary_bounds.size()));
294 secondary_display->UpdateWorkAreaFromInsets(insets); 382 secondary_display->UpdateWorkAreaFromInsets(insets);
295 } 383 }
296 384
297 } // namespace internal 385 } // namespace internal
298 } // namespace ash 386 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698