| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/options2/chromeos/display_options_handler.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "ash/display/display_controller.h" | |
| 10 #include "ash/display/output_configurator_animation.h" | |
| 11 #include "ash/shell.h" | |
| 12 #include "base/bind.h" | |
| 13 #include "base/json/json_value_converter.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/stringprintf.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/prefs/pref_service.h" | |
| 18 #include "chrome/browser/profiles/profile.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 20 #include "chromeos/display/output_configurator.h" | |
| 21 #include "content/public/browser/web_ui.h" | |
| 22 #include "grit/generated_resources.h" | |
| 23 #include "ui/aura/env.h" | |
| 24 #include "ui/aura/display_manager.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | |
| 26 #include "ui/gfx/display.h" | |
| 27 #include "ui/gfx/rect.h" | |
| 28 | |
| 29 namespace chromeos { | |
| 30 namespace options { | |
| 31 | |
| 32 using ash::internal::DisplayController; | |
| 33 | |
| 34 DisplayOptionsHandler::DisplayOptionsHandler() { | |
| 35 aura::Env::GetInstance()->display_manager()->AddObserver(this); | |
| 36 } | |
| 37 | |
| 38 DisplayOptionsHandler::~DisplayOptionsHandler() { | |
| 39 aura::Env::GetInstance()->display_manager()->RemoveObserver(this); | |
| 40 } | |
| 41 | |
| 42 void DisplayOptionsHandler::GetLocalizedValues( | |
| 43 DictionaryValue* localized_strings) { | |
| 44 DCHECK(localized_strings); | |
| 45 RegisterTitle(localized_strings, "displayOptionsPage", | |
| 46 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_TAB_TITLE); | |
| 47 localized_strings->SetString("startMirroring", l10n_util::GetStringUTF16( | |
| 48 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_START_MIRRORING)); | |
| 49 localized_strings->SetString("stopMirroring", l10n_util::GetStringUTF16( | |
| 50 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_STOP_MIRRORING)); | |
| 51 localized_strings->SetString("applyResult", l10n_util::GetStringUTF16( | |
| 52 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_APPLY_RESULT)); | |
| 53 localized_strings->SetString("resolution", l10n_util::GetStringUTF16( | |
| 54 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_RESOLUTION)); | |
| 55 } | |
| 56 | |
| 57 void DisplayOptionsHandler::InitializePage() { | |
| 58 DCHECK(web_ui()); | |
| 59 UpdateDisplaySectionVisibility(); | |
| 60 } | |
| 61 | |
| 62 void DisplayOptionsHandler::RegisterMessages() { | |
| 63 web_ui()->RegisterMessageCallback( | |
| 64 "getDisplayInfo", | |
| 65 base::Bind(&DisplayOptionsHandler::HandleDisplayInfo, | |
| 66 base::Unretained(this))); | |
| 67 web_ui()->RegisterMessageCallback( | |
| 68 "setMirroring", | |
| 69 base::Bind(&DisplayOptionsHandler::HandleMirroring, | |
| 70 base::Unretained(this))); | |
| 71 web_ui()->RegisterMessageCallback( | |
| 72 "setDisplayLayout", | |
| 73 base::Bind(&DisplayOptionsHandler::HandleDisplayLayout, | |
| 74 base::Unretained(this))); | |
| 75 } | |
| 76 | |
| 77 void DisplayOptionsHandler::OnDisplayBoundsChanged( | |
| 78 const gfx::Display& display) { | |
| 79 SendDisplayInfo(); | |
| 80 } | |
| 81 | |
| 82 void DisplayOptionsHandler::OnDisplayAdded(const gfx::Display& new_display) { | |
| 83 UpdateDisplaySectionVisibility(); | |
| 84 SendDisplayInfo(); | |
| 85 } | |
| 86 | |
| 87 void DisplayOptionsHandler::OnDisplayRemoved(const gfx::Display& old_display) { | |
| 88 UpdateDisplaySectionVisibility(); | |
| 89 SendDisplayInfo(); | |
| 90 } | |
| 91 | |
| 92 void DisplayOptionsHandler::UpdateDisplaySectionVisibility() { | |
| 93 chromeos::OutputState output_state = | |
| 94 ash::Shell::GetInstance()->output_configurator()->output_state(); | |
| 95 base::FundamentalValue show_options( | |
| 96 DisplayController::IsExtendedDesktopEnabled() && | |
| 97 output_state != chromeos::STATE_INVALID && | |
| 98 output_state != chromeos::STATE_HEADLESS && | |
| 99 output_state != chromeos::STATE_SINGLE); | |
| 100 web_ui()->CallJavascriptFunction( | |
| 101 "options.BrowserOptions.showDisplayOptions", show_options); | |
| 102 } | |
| 103 | |
| 104 void DisplayOptionsHandler::SendDisplayInfo() { | |
| 105 aura::DisplayManager* display_manager = | |
| 106 aura::Env::GetInstance()->display_manager(); | |
| 107 chromeos::OutputConfigurator* output_configurator = | |
| 108 ash::Shell::GetInstance()->output_configurator(); | |
| 109 base::FundamentalValue mirroring( | |
| 110 output_configurator->output_state() == chromeos::STATE_DUAL_MIRROR); | |
| 111 | |
| 112 base::ListValue displays; | |
| 113 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { | |
| 114 const gfx::Display* display = display_manager->GetDisplayAt(i); | |
| 115 const gfx::Rect& bounds = display->bounds(); | |
| 116 base::DictionaryValue* js_display = new base::DictionaryValue(); | |
| 117 js_display->SetDouble("id", display->id()); | |
| 118 js_display->SetDouble("x", bounds.x()); | |
| 119 js_display->SetDouble("y", bounds.y()); | |
| 120 js_display->SetDouble("width", bounds.width()); | |
| 121 js_display->SetDouble("height", bounds.height()); | |
| 122 // Do not have good data for displays such like vendor/model names. | |
| 123 // So here just uses 'Display 1' or 'Display 2' for their names. | |
| 124 // TODO(mukai,oshima): support vendor/model names and use it. | |
| 125 js_display->SetString("name", base::StringPrintf( | |
| 126 "Display %d", static_cast<int>(i) + 1)); | |
| 127 displays.Set(i, js_display); | |
| 128 } | |
| 129 | |
| 130 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
| 131 base::FundamentalValue layout( | |
| 132 pref_service->GetInteger(prefs::kSecondaryDisplayLayout)); | |
| 133 base::FundamentalValue offset( | |
| 134 pref_service->GetInteger(prefs::kSecondaryDisplayOffset)); | |
| 135 | |
| 136 web_ui()->CallJavascriptFunction( | |
| 137 "options.DisplayOptions.setDisplayInfo", | |
| 138 mirroring, displays, layout, offset); | |
| 139 } | |
| 140 | |
| 141 void DisplayOptionsHandler::FadeOutForMirroringFinished(bool is_mirroring) { | |
| 142 // We use 'PRIMARY_ONLY' for non-mirroring state for now. | |
| 143 // TODO(mukai): fix this and support multiple display modes. | |
| 144 chromeos::OutputState new_state = | |
| 145 is_mirroring ? STATE_DUAL_MIRROR : STATE_DUAL_PRIMARY_ONLY; | |
| 146 ash::Shell::GetInstance()->output_configurator()->SetDisplayMode(new_state); | |
| 147 SendDisplayInfo(); | |
| 148 // Not necessary to start fade-in animation. OutputConfigurator will do that. | |
| 149 } | |
| 150 | |
| 151 void DisplayOptionsHandler::FadeOutForDisplayLayoutFinished( | |
| 152 int layout, int offset) { | |
| 153 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
| 154 pref_service->SetInteger(prefs::kSecondaryDisplayLayout, layout); | |
| 155 pref_service->SetInteger(prefs::kSecondaryDisplayOffset, offset); | |
| 156 SendDisplayInfo(); | |
| 157 ash::Shell::GetInstance()->output_configurator_animation()-> | |
| 158 StartFadeInAnimation(); | |
| 159 } | |
| 160 | |
| 161 void DisplayOptionsHandler::HandleDisplayInfo( | |
| 162 const base::ListValue* unused_args) { | |
| 163 SendDisplayInfo(); | |
| 164 } | |
| 165 | |
| 166 void DisplayOptionsHandler::HandleMirroring(const base::ListValue* args) { | |
| 167 DCHECK(!args->empty()); | |
| 168 bool is_mirroring = false; | |
| 169 args->GetBoolean(0, &is_mirroring); | |
| 170 ash::Shell::GetInstance()->output_configurator_animation()-> | |
| 171 StartFadeOutAnimation(base::Bind( | |
| 172 &DisplayOptionsHandler::FadeOutForMirroringFinished, | |
| 173 base::Unretained(this), | |
| 174 is_mirroring)); | |
| 175 } | |
| 176 | |
| 177 void DisplayOptionsHandler::HandleDisplayLayout(const base::ListValue* args) { | |
| 178 double layout = -1; | |
| 179 double offset = -1; | |
| 180 if (!args->GetDouble(0, &layout) || !args->GetDouble(1, &offset)) { | |
| 181 LOG(ERROR) << "Invalid parameter"; | |
| 182 SendDisplayInfo(); | |
| 183 return; | |
| 184 } | |
| 185 DCHECK_LE(DisplayController::TOP, layout); | |
| 186 DCHECK_GE(DisplayController::LEFT, layout); | |
| 187 ash::Shell::GetInstance()->output_configurator_animation()-> | |
| 188 StartFadeOutAnimation(base::Bind( | |
| 189 &DisplayOptionsHandler::FadeOutForDisplayLayoutFinished, | |
| 190 base::Unretained(this), | |
| 191 static_cast<int>(layout), | |
| 192 static_cast<int>(offset))); | |
| 193 } | |
| 194 | |
| 195 } // namespace options | |
| 196 } // namespace chromeos | |
| OLD | NEW |