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 "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_configurator_animation.h" | 9 #include "ash/display/display_configurator_animation.h" |
10 #include "ash/display/display_controller.h" | 10 #include "ash/display/display_controller.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 int64 display_id = gfx::Display::kInvalidDisplayID; | 48 int64 display_id = gfx::Display::kInvalidDisplayID; |
49 if (!base::StringToInt64(id_value, &display_id)) { | 49 if (!base::StringToInt64(id_value, &display_id)) { |
50 LOG(ERROR) << "Invalid display id: " << id_value; | 50 LOG(ERROR) << "Invalid display id: " << id_value; |
51 return gfx::Display::kInvalidDisplayID; | 51 return gfx::Display::kInvalidDisplayID; |
52 } | 52 } |
53 | 53 |
54 return display_id; | 54 return display_id; |
55 } | 55 } |
56 | 56 |
57 bool CompareDisplayMode(ash::DisplayMode d1, ash::DisplayMode d2) { | |
58 if (d1.size.GetArea() == d2.size.GetArea()) | |
59 return d1.refresh_rate < d2.refresh_rate; | |
60 return d1.size.GetArea() < d2.size.GetArea(); | |
61 } | |
62 | |
63 base::string16 GetColorProfileName(ui::ColorCalibrationProfile profile) { | 57 base::string16 GetColorProfileName(ui::ColorCalibrationProfile profile) { |
64 switch (profile) { | 58 switch (profile) { |
65 case ui::COLOR_PROFILE_STANDARD: | 59 case ui::COLOR_PROFILE_STANDARD: |
66 return l10n_util::GetStringUTF16( | 60 return l10n_util::GetStringUTF16( |
67 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_COLOR_PROFILE_STANDARD); | 61 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_COLOR_PROFILE_STANDARD); |
68 case ui::COLOR_PROFILE_DYNAMIC: | 62 case ui::COLOR_PROFILE_DYNAMIC: |
69 return l10n_util::GetStringUTF16( | 63 return l10n_util::GetStringUTF16( |
70 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_COLOR_PROFILE_DYNAMIC); | 64 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_COLOR_PROFILE_DYNAMIC); |
71 case ui::COLOR_PROFILE_MOVIE: | 65 case ui::COLOR_PROFILE_MOVIE: |
72 return l10n_util::GetStringUTF16( | 66 return l10n_util::GetStringUTF16( |
73 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_COLOR_PROFILE_MOVIE); | 67 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_COLOR_PROFILE_MOVIE); |
74 case ui::COLOR_PROFILE_READING: | 68 case ui::COLOR_PROFILE_READING: |
75 return l10n_util::GetStringUTF16( | 69 return l10n_util::GetStringUTF16( |
76 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_COLOR_PROFILE_READING); | 70 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_COLOR_PROFILE_READING); |
77 case ui::NUM_COLOR_PROFILES: | 71 case ui::NUM_COLOR_PROFILES: |
78 break; | 72 break; |
79 } | 73 } |
80 | 74 |
81 NOTREACHED(); | 75 NOTREACHED(); |
82 return base::string16(); | 76 return base::string16(); |
83 } | 77 } |
84 | 78 |
| 79 int GetIntOrDouble(const base::DictionaryValue* dict, |
| 80 const std::string& field) { |
| 81 double double_result = 0; |
| 82 if (dict->GetDouble(field, &double_result)) |
| 83 return static_cast<int>(double_result); |
| 84 |
| 85 int result = 0; |
| 86 dict->GetInteger(field, &result); |
| 87 return result; |
| 88 } |
| 89 |
| 90 bool GetFloat(const base::DictionaryValue* dict, |
| 91 const std::string& field, |
| 92 float* result) { |
| 93 double double_result = 0; |
| 94 if (dict->GetDouble(field, &double_result)) { |
| 95 *result = static_cast<float>(double_result); |
| 96 return true; |
| 97 } |
| 98 return false; |
| 99 } |
| 100 |
| 101 bool ConvertValueToDisplayMode(const base::DictionaryValue* dict, |
| 102 ash::DisplayMode* mode) { |
| 103 mode->size.set_width(GetIntOrDouble(dict, "originalWidth")); |
| 104 mode->size.set_height(GetIntOrDouble(dict, "originalHeight")); |
| 105 if (mode->size.IsEmpty()) { |
| 106 LOG(ERROR) << "missing width or height."; |
| 107 return false; |
| 108 } |
| 109 if (!GetFloat(dict, "refreshRate", &mode->refresh_rate)) { |
| 110 LOG(ERROR) << "missing refreshRate."; |
| 111 return false; |
| 112 } |
| 113 if (!GetFloat(dict, "scale", &mode->ui_scale)) { |
| 114 LOG(ERROR) << "missing ui-scale."; |
| 115 return false; |
| 116 } |
| 117 if (!GetFloat(dict, "deviceScaleFactor", &mode->device_scale_factor)) { |
| 118 LOG(ERROR) << "missing deviceScaleFactor."; |
| 119 return false; |
| 120 } |
| 121 return true; |
| 122 } |
| 123 |
| 124 base::DictionaryValue* ConvertDisplayModeToValue(int64 display_id, |
| 125 const ash::DisplayMode& mode) { |
| 126 base::DictionaryValue* result = new base::DictionaryValue(); |
| 127 gfx::Size size_dip = mode.GetSizeInDIP(); |
| 128 result->SetInteger("width", size_dip.width()); |
| 129 result->SetInteger("height", size_dip.height()); |
| 130 result->SetInteger("originalWidth", mode.size.width()); |
| 131 result->SetInteger("originalHeight", mode.size.height()); |
| 132 result->SetDouble("deviceScaleFactor", mode.device_scale_factor); |
| 133 result->SetDouble("scale", mode.ui_scale); |
| 134 result->SetDouble("refreshRate", mode.refresh_rate); |
| 135 result->SetBoolean("isBest", mode.native); |
| 136 result->SetBoolean( |
| 137 "selected", mode.IsEquivalent( |
| 138 GetDisplayManager()->GetActiveModeForDisplayId(display_id))); |
| 139 return result; |
| 140 } |
| 141 |
85 } // namespace | 142 } // namespace |
86 | 143 |
87 DisplayOptionsHandler::DisplayOptionsHandler() { | 144 DisplayOptionsHandler::DisplayOptionsHandler() { |
88 ash::Shell::GetInstance()->display_controller()->AddObserver(this); | 145 ash::Shell::GetInstance()->display_controller()->AddObserver(this); |
89 } | 146 } |
90 | 147 |
91 DisplayOptionsHandler::~DisplayOptionsHandler() { | 148 DisplayOptionsHandler::~DisplayOptionsHandler() { |
92 ash::Shell::GetInstance()->display_controller()->RemoveObserver(this); | 149 ash::Shell::GetInstance()->display_controller()->RemoveObserver(this); |
93 } | 150 } |
94 | 151 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 base::Unretained(this))); | 209 base::Unretained(this))); |
153 web_ui()->RegisterMessageCallback( | 210 web_ui()->RegisterMessageCallback( |
154 "setPrimary", | 211 "setPrimary", |
155 base::Bind(&DisplayOptionsHandler::HandleSetPrimary, | 212 base::Bind(&DisplayOptionsHandler::HandleSetPrimary, |
156 base::Unretained(this))); | 213 base::Unretained(this))); |
157 web_ui()->RegisterMessageCallback( | 214 web_ui()->RegisterMessageCallback( |
158 "setDisplayLayout", | 215 "setDisplayLayout", |
159 base::Bind(&DisplayOptionsHandler::HandleDisplayLayout, | 216 base::Bind(&DisplayOptionsHandler::HandleDisplayLayout, |
160 base::Unretained(this))); | 217 base::Unretained(this))); |
161 web_ui()->RegisterMessageCallback( | 218 web_ui()->RegisterMessageCallback( |
162 "setUIScale", | 219 "setDisplayMode", |
163 base::Bind(&DisplayOptionsHandler::HandleSetUIScale, | 220 base::Bind(&DisplayOptionsHandler::HandleSetDisplayMode, |
164 base::Unretained(this))); | |
165 web_ui()->RegisterMessageCallback( | |
166 "setResolution", | |
167 base::Bind(&DisplayOptionsHandler::HandleSetResolution, | |
168 base::Unretained(this))); | 221 base::Unretained(this))); |
169 web_ui()->RegisterMessageCallback( | 222 web_ui()->RegisterMessageCallback( |
170 "setOrientation", | 223 "setOrientation", |
171 base::Bind(&DisplayOptionsHandler::HandleSetOrientation, | 224 base::Bind(&DisplayOptionsHandler::HandleSetOrientation, |
172 base::Unretained(this))); | 225 base::Unretained(this))); |
173 web_ui()->RegisterMessageCallback( | 226 web_ui()->RegisterMessageCallback( |
174 "setColorProfile", | 227 "setColorProfile", |
175 base::Bind(&DisplayOptionsHandler::HandleSetColorProfile, | 228 base::Bind(&DisplayOptionsHandler::HandleSetColorProfile, |
176 base::Unretained(this))); | 229 base::Unretained(this))); |
177 } | 230 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 js_display->SetInteger("x", bounds.x()); | 263 js_display->SetInteger("x", bounds.x()); |
211 js_display->SetInteger("y", bounds.y()); | 264 js_display->SetInteger("y", bounds.y()); |
212 js_display->SetInteger("width", bounds.width()); | 265 js_display->SetInteger("width", bounds.width()); |
213 js_display->SetInteger("height", bounds.height()); | 266 js_display->SetInteger("height", bounds.height()); |
214 js_display->SetString("name", | 267 js_display->SetString("name", |
215 display_manager->GetDisplayNameForId(display.id())); | 268 display_manager->GetDisplayNameForId(display.id())); |
216 js_display->SetBoolean("isPrimary", display.id() == primary_id); | 269 js_display->SetBoolean("isPrimary", display.id() == primary_id); |
217 js_display->SetBoolean("isInternal", display.IsInternal()); | 270 js_display->SetBoolean("isInternal", display.IsInternal()); |
218 js_display->SetInteger("orientation", | 271 js_display->SetInteger("orientation", |
219 static_cast<int>(display_info.rotation())); | 272 static_cast<int>(display_info.rotation())); |
220 std::vector<ash::DisplayMode> display_modes; | |
221 std::vector<float> ui_scales; | |
222 if (display.IsInternal()) { | |
223 ui_scales = DisplayManager::GetScalesForDisplay(display_info); | |
224 gfx::SizeF base_size = display_info.bounds_in_native().size(); | |
225 base_size.Scale(1.0f / display_info.device_scale_factor()); | |
226 if (display_info.rotation() == gfx::Display::ROTATE_90 || | |
227 display_info.rotation() == gfx::Display::ROTATE_270) { | |
228 float tmp = base_size.width(); | |
229 base_size.set_width(base_size.height()); | |
230 base_size.set_height(tmp); | |
231 } | |
232 for (size_t i = 0; i < ui_scales.size(); ++i) { | |
233 gfx::SizeF new_size = base_size; | |
234 new_size.Scale(ui_scales[i]); | |
235 display_modes.push_back(ash::DisplayMode( | |
236 gfx::ToFlooredSize(new_size), -1.0f, false, false)); | |
237 } | |
238 } else { | |
239 for (size_t i = 0; i < display_info.display_modes().size(); ++i) | |
240 display_modes.push_back(display_info.display_modes()[i]); | |
241 } | |
242 std::sort(display_modes.begin(), display_modes.end(), CompareDisplayMode); | |
243 | 273 |
244 base::ListValue* js_resolutions = new base::ListValue(); | 274 base::ListValue* js_resolutions = new base::ListValue(); |
245 gfx::Size current_size = display_info.bounds_in_native().size(); | 275 const std::vector<ash::DisplayMode>& display_modes = |
| 276 display_info.display_modes(); |
246 for (size_t i = 0; i < display_modes.size(); ++i) { | 277 for (size_t i = 0; i < display_modes.size(); ++i) { |
247 base::DictionaryValue* resolution_info = new base::DictionaryValue(); | 278 js_resolutions->Append( |
248 gfx::Size resolution = display_modes[i].size; | 279 ConvertDisplayModeToValue(display.id(), display_modes[i])); |
249 if (!ui_scales.empty()) { | |
250 resolution_info->SetDouble("scale", ui_scales[i]); | |
251 if (ui_scales[i] == 1.0f) | |
252 resolution_info->SetBoolean("isBest", true); | |
253 resolution_info->SetBoolean( | |
254 "selected", display_info.configured_ui_scale() == ui_scales[i]); | |
255 } else { | |
256 // Picks the largest one as the "best", which is the last element | |
257 // because |display_modes| is sorted by its area. | |
258 if (i == display_modes.size() - 1) | |
259 resolution_info->SetBoolean("isBest", true); | |
260 resolution_info->SetBoolean("selected", (resolution == current_size)); | |
261 } | |
262 resolution_info->SetInteger("width", resolution.width()); | |
263 resolution_info->SetInteger("height", resolution.height()); | |
264 if (display_modes[i].refresh_rate > 0.0f) { | |
265 resolution_info->SetDouble("refreshRate", | |
266 display_modes[i].refresh_rate); | |
267 } | |
268 js_resolutions->Append(resolution_info); | |
269 } | 280 } |
270 js_display->Set("resolutions", js_resolutions); | 281 js_display->Set("resolutions", js_resolutions); |
271 | 282 |
272 js_display->SetInteger("colorProfile", display_info.color_profile()); | 283 js_display->SetInteger("colorProfile", display_info.color_profile()); |
273 base::ListValue* available_color_profiles = new base::ListValue(); | 284 base::ListValue* available_color_profiles = new base::ListValue(); |
274 for (size_t i = 0; | 285 for (size_t i = 0; |
275 i < display_info.available_color_profiles().size(); ++i) { | 286 i < display_info.available_color_profiles().size(); ++i) { |
276 base::DictionaryValue* color_profile_dict = new base::DictionaryValue(); | 287 base::DictionaryValue* color_profile_dict = new base::DictionaryValue(); |
277 ui::ColorCalibrationProfile color_profile = | 288 ui::ColorCalibrationProfile color_profile = |
278 display_info.available_color_profiles()[i]; | 289 display_info.available_color_profiles()[i]; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 DCHECK_GE(ash::DisplayLayout::LEFT, layout); | 363 DCHECK_GE(ash::DisplayLayout::LEFT, layout); |
353 content::RecordAction(base::UserMetricsAction("Options_DisplayRearrange")); | 364 content::RecordAction(base::UserMetricsAction("Options_DisplayRearrange")); |
354 ash::Shell::GetInstance()->display_configurator_animation()-> | 365 ash::Shell::GetInstance()->display_configurator_animation()-> |
355 StartFadeOutAnimation(base::Bind( | 366 StartFadeOutAnimation(base::Bind( |
356 &DisplayOptionsHandler::OnFadeOutForDisplayLayoutFinished, | 367 &DisplayOptionsHandler::OnFadeOutForDisplayLayoutFinished, |
357 base::Unretained(this), | 368 base::Unretained(this), |
358 static_cast<int>(layout), | 369 static_cast<int>(layout), |
359 static_cast<int>(offset))); | 370 static_cast<int>(offset))); |
360 } | 371 } |
361 | 372 |
362 void DisplayOptionsHandler::HandleSetUIScale(const base::ListValue* args) { | 373 void DisplayOptionsHandler::HandleSetDisplayMode(const base::ListValue* args) { |
363 DCHECK(!args->empty()); | 374 DCHECK(!args->empty()); |
364 | 375 |
365 int64 display_id = GetDisplayId(args); | 376 int64 display_id = GetDisplayId(args); |
366 if (display_id == gfx::Display::kInvalidDisplayID) | 377 if (display_id == gfx::Display::kInvalidDisplayID) |
367 return; | 378 return; |
368 | 379 |
369 double ui_scale = 0.0f; | 380 const base::DictionaryValue* mode_data = NULL; |
370 if (!args->GetDouble(1, &ui_scale) || ui_scale == 0.0f) { | 381 if (!args->GetDictionary(1, &mode_data)) { |
371 LOG(ERROR) << "Can't find new ui_scale"; | 382 LOG(ERROR) << "Failed to get mode data"; |
372 return; | 383 return; |
373 } | 384 } |
374 | 385 |
375 GetDisplayManager()->SetDisplayUIScale(display_id, ui_scale); | 386 ash::DisplayMode mode; |
376 } | 387 if (!ConvertValueToDisplayMode(mode_data, &mode)) |
377 | |
378 void DisplayOptionsHandler::HandleSetResolution(const base::ListValue* args) { | |
379 DCHECK(!args->empty()); | |
380 int64 display_id = GetDisplayId(args); | |
381 if (display_id == gfx::Display::kInvalidDisplayID) | |
382 return; | 388 return; |
383 | 389 |
384 content::RecordAction( | 390 content::RecordAction( |
385 base::UserMetricsAction("Options_DisplaySetResolution")); | 391 base::UserMetricsAction("Options_DisplaySetResolution")); |
386 double width = 0.0f; | 392 ash::DisplayManager* display_manager = GetDisplayManager(); |
387 double height = 0.0f; | 393 ash::DisplayMode current_mode = |
388 if (!args->GetDouble(1, &width) || width == 0.0f) { | 394 display_manager->GetActiveModeForDisplayId(display_id); |
389 LOG(ERROR) << "Can't find new width"; | 395 if (display_manager->SetDisplayMode(display_id, mode)) { |
390 return; | 396 ash::Shell::GetInstance()->resolution_notification_controller()-> |
| 397 PrepareNotification( |
| 398 display_id, current_mode, mode, base::Bind(&StoreDisplayPrefs)); |
391 } | 399 } |
392 if (!args->GetDouble(2, &height) || height == 0.0f) { | |
393 LOG(ERROR) << "Can't find new height"; | |
394 return; | |
395 } | |
396 | |
397 const ash::DisplayInfo& display_info = | |
398 GetDisplayManager()->GetDisplayInfo(display_id); | |
399 gfx::Size new_resolution = gfx::ToFlooredSize(gfx::SizeF(width, height)); | |
400 gfx::Size old_resolution = display_info.bounds_in_native().size(); | |
401 bool has_new_resolution = false; | |
402 bool has_old_resolution = false; | |
403 for (size_t i = 0; i < display_info.display_modes().size(); ++i) { | |
404 ash::DisplayMode display_mode = display_info.display_modes()[i]; | |
405 if (display_mode.size == new_resolution) | |
406 has_new_resolution = true; | |
407 if (display_mode.size == old_resolution) | |
408 has_old_resolution = true; | |
409 } | |
410 if (!has_new_resolution) { | |
411 LOG(ERROR) << "No new resolution " << new_resolution.ToString() | |
412 << " is found in the display info " << display_info.ToString(); | |
413 return; | |
414 } | |
415 if (!has_old_resolution) { | |
416 LOG(ERROR) << "No old resolution " << old_resolution.ToString() | |
417 << " is found in the display info " << display_info.ToString(); | |
418 return; | |
419 } | |
420 | |
421 ash::Shell::GetInstance()->resolution_notification_controller()-> | |
422 SetDisplayResolutionAndNotify( | |
423 display_id, old_resolution, new_resolution, | |
424 base::Bind(&StoreDisplayPrefs)); | |
425 } | 400 } |
426 | 401 |
427 void DisplayOptionsHandler::HandleSetOrientation(const base::ListValue* args) { | 402 void DisplayOptionsHandler::HandleSetOrientation(const base::ListValue* args) { |
428 DCHECK(!args->empty()); | 403 DCHECK(!args->empty()); |
429 | 404 |
430 int64 display_id = GetDisplayId(args); | 405 int64 display_id = GetDisplayId(args); |
431 if (display_id == gfx::Display::kInvalidDisplayID) | 406 if (display_id == gfx::Display::kInvalidDisplayID) |
432 return; | 407 return; |
433 | 408 |
434 std::string rotation_value; | 409 std::string rotation_value; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 return; | 450 return; |
476 } | 451 } |
477 | 452 |
478 GetDisplayManager()->SetColorCalibrationProfile( | 453 GetDisplayManager()->SetColorCalibrationProfile( |
479 display_id, static_cast<ui::ColorCalibrationProfile>(profile_id)); | 454 display_id, static_cast<ui::ColorCalibrationProfile>(profile_id)); |
480 SendAllDisplayInfo(); | 455 SendAllDisplayInfo(); |
481 } | 456 } |
482 | 457 |
483 } // namespace options | 458 } // namespace options |
484 } // namespace chromeos | 459 } // namespace chromeos |
OLD | NEW |