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

Side by Side Diff: chrome/browser/ui/webui/options/chromeos/display_options_handler.cc

Issue 21629002: Supports changing resolutions from options UI page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: polish Created 7 years, 4 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
« no previous file with comments | « chrome/browser/ui/webui/options/chromeos/display_options_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "chrome/browser/ui/webui/options/chromeos/display_options_handler.h" 5 #include "chrome/browser/ui/webui/options/chromeos/display_options_handler.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "ash/display/display_controller.h" 9 #include "ash/display/display_controller.h"
10 #include "ash/display/display_manager.h" 10 #include "ash/display/display_manager.h"
11 #include "ash/display/output_configurator_animation.h" 11 #include "ash/display/output_configurator_animation.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 "base/bind.h" 14 #include "base/bind.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "chrome/browser/chromeos/display/display_preferences.h" 19 #include "chrome/browser/chromeos/display/display_preferences.h"
20 #include "chromeos/display/output_configurator.h" 20 #include "chromeos/display/output_configurator.h"
21 #include "content/public/browser/web_ui.h" 21 #include "content/public/browser/web_ui.h"
22 #include "grit/generated_resources.h" 22 #include "grit/generated_resources.h"
23 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/gfx/display.h" 24 #include "ui/gfx/display.h"
25 #include "ui/gfx/rect.h" 25 #include "ui/gfx/rect.h"
26 #include "ui/gfx/safe_integer_conversions.h"
27 #include "ui/gfx/screen.h" 26 #include "ui/gfx/screen.h"
27 #include "ui/gfx/size_conversions.h"
28 28
29 using ash::internal::DisplayManager; 29 using ash::internal::DisplayManager;
30 30
31 namespace chromeos { 31 namespace chromeos {
32 namespace options { 32 namespace options {
33 namespace { 33 namespace {
34 34
35 DisplayManager* GetDisplayManager() { 35 DisplayManager* GetDisplayManager() {
36 return ash::Shell::GetInstance()->display_manager(); 36 return ash::Shell::GetInstance()->display_manager();
37 } 37 }
38 38
39 int64 GetDisplayId(const base::ListValue* args) { 39 int64 GetDisplayId(const base::ListValue* args) {
40 // Assumes the display ID is specified as the first argument. 40 // Assumes the display ID is specified as the first argument.
41 std::string id_value; 41 std::string id_value;
42 if (!args->GetString(0, &id_value)) { 42 if (!args->GetString(0, &id_value)) {
43 LOG(ERROR) << "Can't find ID"; 43 LOG(ERROR) << "Can't find ID";
44 return gfx::Display::kInvalidDisplayID; 44 return gfx::Display::kInvalidDisplayID;
45 } 45 }
46 46
47 int64 display_id = gfx::Display::kInvalidDisplayID; 47 int64 display_id = gfx::Display::kInvalidDisplayID;
48 if (!base::StringToInt64(id_value, &display_id)) { 48 if (!base::StringToInt64(id_value, &display_id)) {
49 LOG(ERROR) << "Invalid display id: " << id_value; 49 LOG(ERROR) << "Invalid display id: " << id_value;
50 return gfx::Display::kInvalidDisplayID; 50 return gfx::Display::kInvalidDisplayID;
51 } 51 }
52 52
53 return display_id; 53 return display_id;
54 } 54 }
55 55
56 bool CompareResolution(ash::internal::Resolution r1,
57 ash::internal::Resolution r2) {
58 return r1.size.GetArea() < r2.size.GetArea();
59 }
60
56 } // namespace 61 } // namespace
57 62
58 DisplayOptionsHandler::DisplayOptionsHandler() { 63 DisplayOptionsHandler::DisplayOptionsHandler() {
59 ash::Shell::GetInstance()->display_controller()->AddObserver(this); 64 ash::Shell::GetInstance()->display_controller()->AddObserver(this);
60 } 65 }
61 66
62 DisplayOptionsHandler::~DisplayOptionsHandler() { 67 DisplayOptionsHandler::~DisplayOptionsHandler() {
63 ash::Shell::GetInstance()->display_controller()->RemoveObserver(this); 68 ash::Shell::GetInstance()->display_controller()->RemoveObserver(this);
64 } 69 }
65 70
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 base::Unretained(this))); 129 base::Unretained(this)));
125 web_ui()->RegisterMessageCallback( 130 web_ui()->RegisterMessageCallback(
126 "setDisplayLayout", 131 "setDisplayLayout",
127 base::Bind(&DisplayOptionsHandler::HandleDisplayLayout, 132 base::Bind(&DisplayOptionsHandler::HandleDisplayLayout,
128 base::Unretained(this))); 133 base::Unretained(this)));
129 web_ui()->RegisterMessageCallback( 134 web_ui()->RegisterMessageCallback(
130 "setUIScale", 135 "setUIScale",
131 base::Bind(&DisplayOptionsHandler::HandleSetUIScale, 136 base::Bind(&DisplayOptionsHandler::HandleSetUIScale,
132 base::Unretained(this))); 137 base::Unretained(this)));
133 web_ui()->RegisterMessageCallback( 138 web_ui()->RegisterMessageCallback(
139 "setResolution",
140 base::Bind(&DisplayOptionsHandler::HandleSetResolution,
141 base::Unretained(this)));
142 web_ui()->RegisterMessageCallback(
134 "setOrientation", 143 "setOrientation",
135 base::Bind(&DisplayOptionsHandler::HandleSetOrientation, 144 base::Bind(&DisplayOptionsHandler::HandleSetOrientation,
136 base::Unretained(this))); 145 base::Unretained(this)));
137 } 146 }
138 147
139 void DisplayOptionsHandler::OnDisplayConfigurationChanging() { 148 void DisplayOptionsHandler::OnDisplayConfigurationChanging() {
140 } 149 }
141 150
142 void DisplayOptionsHandler::OnDisplayConfigurationChanged() { 151 void DisplayOptionsHandler::OnDisplayConfigurationChanged() {
143 SendAllDisplayInfo(); 152 SendAllDisplayInfo();
(...skipping 28 matching lines...) Expand all
172 js_display->SetInteger("x", bounds.x()); 181 js_display->SetInteger("x", bounds.x());
173 js_display->SetInteger("y", bounds.y()); 182 js_display->SetInteger("y", bounds.y());
174 js_display->SetInteger("width", bounds.width()); 183 js_display->SetInteger("width", bounds.width());
175 js_display->SetInteger("height", bounds.height()); 184 js_display->SetInteger("height", bounds.height());
176 js_display->SetString("name", 185 js_display->SetString("name",
177 display_manager->GetDisplayNameForId(display.id())); 186 display_manager->GetDisplayNameForId(display.id()));
178 js_display->SetBoolean("isPrimary", display.id() == primary_id); 187 js_display->SetBoolean("isPrimary", display.id() == primary_id);
179 js_display->SetBoolean("isInternal", display.IsInternal()); 188 js_display->SetBoolean("isInternal", display.IsInternal());
180 js_display->SetInteger("orientation", 189 js_display->SetInteger("orientation",
181 static_cast<int>(display_info.rotation())); 190 static_cast<int>(display_info.rotation()));
182 std::vector<float> ui_scales = DisplayManager::GetScalesForDisplay( 191 std::vector<ash::internal::Resolution> resolutions;
183 display_info); 192 std::vector<float> ui_scales;
184 base::ListValue* js_scales = new base::ListValue(); 193 if (display.IsInternal()) {
185 gfx::SizeF base_size = display_info.bounds_in_pixel().size(); 194 ui_scales = DisplayManager::GetScalesForDisplay(display_info);
186 base_size.Scale(1.0f / display.device_scale_factor()); 195 gfx::SizeF base_size = display_info.bounds_in_pixel().size();
187 if (display_info.rotation() == gfx::Display::ROTATE_90 || 196 base_size.Scale(1.0f / display.device_scale_factor());
188 display_info.rotation() == gfx::Display::ROTATE_270) { 197 if (display_info.rotation() == gfx::Display::ROTATE_90 ||
189 float tmp = base_size.width(); 198 display_info.rotation() == gfx::Display::ROTATE_270) {
190 base_size.set_width(base_size.height()); 199 float tmp = base_size.width();
191 base_size.set_height(tmp); 200 base_size.set_width(base_size.height());
201 base_size.set_height(tmp);
202 }
203 for (size_t i = 0; i < ui_scales.size(); ++i) {
204 gfx::SizeF new_size = base_size;
205 new_size.Scale(ui_scales[i]);
206 resolutions.push_back(ash::internal::Resolution(
207 gfx::ToFlooredSize(new_size), false /* interlaced */));
208 }
209 } else {
210 for (size_t i = 0; i < display_info.resolutions().size(); ++i)
211 resolutions.push_back(display_info.resolutions()[i]);
192 } 212 }
213 std::sort(resolutions.begin(), resolutions.end(), CompareResolution);
193 214
194 for (size_t i = 0; i < ui_scales.size(); ++i) { 215 base::ListValue* js_resolutions = new base::ListValue();
195 base::DictionaryValue* scale_info = new base::DictionaryValue(); 216 gfx::Size current_size(bounds.width() * display.device_scale_factor(),
196 scale_info->SetDouble("scale", ui_scales[i]); 217 bounds.height() * display.device_scale_factor());
197 scale_info->SetInteger( 218 for (size_t i = 0; i < resolutions.size(); ++i) {
198 "width", gfx::ToFlooredInt(base_size.width() * ui_scales[i])); 219 base::DictionaryValue* resolution_info = new base::DictionaryValue();
199 scale_info->SetInteger( 220 if (!ui_scales.empty()) {
200 "height", gfx::ToFlooredInt(base_size.height() * ui_scales[i])); 221 resolution_info->SetDouble("scale", ui_scales[i]);
201 scale_info->SetBoolean("selected", 222 if (ui_scales[i] == 1.0f)
202 display_info.ui_scale() == ui_scales[i]); 223 resolution_info->SetBoolean("isBest", true);
203 js_scales->Append(scale_info); 224 resolution_info->SetBoolean(
225 "selected", display_info.ui_scale() == ui_scales[i]);
226 } else {
227 // Picks the largest one as the "best", which is the last element
228 // because |resolutions| is sorted by its area.
229 if (i == resolutions.size() - 1)
230 resolution_info->SetBoolean("isBest", true);
231 resolution_info->SetBoolean(
232 "selected", (resolutions[i].size == current_size));
233 }
234 resolution_info->SetInteger("width", resolutions[i].size.width());
235 resolution_info->SetInteger("height",resolutions[i].size.height());
236 js_resolutions->Append(resolution_info);
204 } 237 }
205 js_display->Set("uiScales", js_scales); 238 js_display->Set("resolutions", js_resolutions);
206 js_displays.Append(js_display); 239 js_displays.Append(js_display);
207 } 240 }
208 241
209 scoped_ptr<base::Value> layout_value(base::Value::CreateNullValue()); 242 scoped_ptr<base::Value> layout_value(base::Value::CreateNullValue());
210 scoped_ptr<base::Value> offset_value(base::Value::CreateNullValue()); 243 scoped_ptr<base::Value> offset_value(base::Value::CreateNullValue());
211 if (display_manager->GetNumDisplays() > 1) { 244 if (display_manager->GetNumDisplays() > 1) {
212 const ash::DisplayLayout layout = 245 const ash::DisplayLayout layout =
213 display_controller->GetCurrentDisplayLayout(); 246 display_controller->GetCurrentDisplayLayout();
214 layout_value.reset(new base::FundamentalValue(layout.position)); 247 layout_value.reset(new base::FundamentalValue(layout.position));
215 offset_value.reset(new base::FundamentalValue(layout.offset)); 248 offset_value.reset(new base::FundamentalValue(layout.offset));
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 static_cast<int>(offset))); 310 static_cast<int>(offset)));
278 } 311 }
279 312
280 void DisplayOptionsHandler::HandleSetUIScale(const base::ListValue* args) { 313 void DisplayOptionsHandler::HandleSetUIScale(const base::ListValue* args) {
281 DCHECK(!args->empty()); 314 DCHECK(!args->empty());
282 315
283 int64 display_id = GetDisplayId(args); 316 int64 display_id = GetDisplayId(args);
284 if (display_id == gfx::Display::kInvalidDisplayID) 317 if (display_id == gfx::Display::kInvalidDisplayID)
285 return; 318 return;
286 319
287 std::string ui_scale_value;
288 double ui_scale = 0.0f; 320 double ui_scale = 0.0f;
289 if (!args->GetString(1, &ui_scale_value)) { 321 if (!args->GetDouble(1, &ui_scale) || ui_scale == 0.0f) {
290 LOG(ERROR) << "Ca't find new ui_scale"; 322 LOG(ERROR) << "Can't find new ui_scale";
291 return;
292 }
293 if (!base::StringToDouble(ui_scale_value, &ui_scale)) {
294 LOG(ERROR) << "Invalid ui_scale: " << ui_scale_value;
295 return; 323 return;
296 } 324 }
297 325
298 GetDisplayManager()->SetDisplayUIScale(display_id, ui_scale); 326 GetDisplayManager()->SetDisplayUIScale(display_id, ui_scale);
299 } 327 }
300 328
329 void DisplayOptionsHandler::HandleSetResolution(const base::ListValue* args) {
330 DCHECK(!args->empty());
331 int64 display_id = GetDisplayId(args);
332 if (display_id == gfx::Display::kInvalidDisplayID)
333 return;
334 LOG(ERROR) << display_id;
oshima 2013/08/01 22:50:44 remove this?
Jun Mukai 2013/08/02 01:35:18 Done.
335
336 double width = 0.0f;
337 double height = 0.0f;
338 if (!args->GetDouble(1, &width) || width == 0.0f) {
339 LOG(ERROR) << "Can't find new width";
340 return;
341 }
342 if (!args->GetDouble(2, &height) || height == 0.0f) {
343 LOG(ERROR) << "Can't find new height";
344 return;
345 }
346
347 // TODO(mukai): creates a confirmation dialog.
348 GetDisplayManager()->SetDisplayResolution(
349 display_id, gfx::ToFlooredSize(gfx::SizeF(width, height)));
350 }
351
301 void DisplayOptionsHandler::HandleSetOrientation(const base::ListValue* args) { 352 void DisplayOptionsHandler::HandleSetOrientation(const base::ListValue* args) {
302 DCHECK(!args->empty()); 353 DCHECK(!args->empty());
303 354
304 int64 display_id = GetDisplayId(args); 355 int64 display_id = GetDisplayId(args);
305 if (display_id == gfx::Display::kInvalidDisplayID) 356 if (display_id == gfx::Display::kInvalidDisplayID)
306 return; 357 return;
307 358
308 std::string rotation_value; 359 std::string rotation_value;
309 gfx::Display::Rotation new_rotation = gfx::Display::ROTATE_0; 360 gfx::Display::Rotation new_rotation = gfx::Display::ROTATE_0;
310 if (!args->GetString(1, &rotation_value)) { 361 if (!args->GetString(1, &rotation_value)) {
311 LOG(ERROR) << "Can't find new orientation"; 362 LOG(ERROR) << "Can't find new orientation";
312 return; 363 return;
313 } 364 }
314 if (rotation_value == "90") 365 if (rotation_value == "90")
315 new_rotation = gfx::Display::ROTATE_90; 366 new_rotation = gfx::Display::ROTATE_90;
316 else if (rotation_value == "180") 367 else if (rotation_value == "180")
317 new_rotation = gfx::Display::ROTATE_180; 368 new_rotation = gfx::Display::ROTATE_180;
318 else if (rotation_value == "270") 369 else if (rotation_value == "270")
319 new_rotation = gfx::Display::ROTATE_270; 370 new_rotation = gfx::Display::ROTATE_270;
320 else if (rotation_value != "0") 371 else if (rotation_value != "0")
321 LOG(ERROR) << "Invalid rotation: " << rotation_value << " Falls back to 0"; 372 LOG(ERROR) << "Invalid rotation: " << rotation_value << " Falls back to 0";
322 373
323 GetDisplayManager()->SetDisplayRotation(display_id, new_rotation); 374 GetDisplayManager()->SetDisplayRotation(display_id, new_rotation);
324 } 375 }
325 376
326 } // namespace options 377 } // namespace options
327 } // namespace chromeos 378 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options/chromeos/display_options_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698