1 // Copyright (c) 2013 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.
5 #include "chrome/browser/ui/webui/options/chromeos/display_overscan_handler.h"
9 #include "ash/display/display_manager.h"
10 #include "ash/display/window_tree_host_manager.h"
11 #include "ash/shell.h"
12 #include "base/bind.h"
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/chromeos/display/overscan_calibrator.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "content/public/browser/web_ui.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/gfx/display.h"
21 #include "ui/gfx/screen.h"
27 // The value for the orientation of overscan operations.
28 const char kOrientationHorizontal
[] = "horizontal";
29 const char kOrientationVertical
[] = "vertical";
33 DisplayOverscanHandler::DisplayOverscanHandler() {
34 ash::Shell::GetScreen()->AddObserver(this);
37 DisplayOverscanHandler::~DisplayOverscanHandler() {
38 ash::Shell::GetScreen()->RemoveObserver(this);
41 void DisplayOverscanHandler::GetLocalizedValues(
42 base::DictionaryValue
* localized_strings
) {
43 RegisterTitle(localized_strings
, "displayOverscanPage",
44 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_TAB_TITLE
);
45 localized_strings
->SetString("shrinkAndExpand", l10n_util::GetStringUTF16(
46 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_SHRINK_EXPAND
));
47 localized_strings
->SetString("move", l10n_util::GetStringUTF16(
48 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_MOVE
));
49 localized_strings
->SetString("overscanReset", l10n_util::GetStringUTF16(
50 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_RESET_BUTTON_LABEL
));
51 localized_strings
->SetString("overscanOK", l10n_util::GetStringUTF16(
52 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_OK_BUTTON_LABEL
));
53 localized_strings
->SetString("overscanCancel", l10n_util::GetStringUTF16(
54 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_CANCEL_BUTTON_LABEL
));
57 void DisplayOverscanHandler::RegisterMessages() {
58 web_ui()->RegisterMessageCallback(
60 base::Bind(&DisplayOverscanHandler::HandleStart
,
61 base::Unretained(this)));
62 web_ui()->RegisterMessageCallback(
64 base::Bind(&DisplayOverscanHandler::HandleCommit
,
65 base::Unretained(this)));
66 web_ui()->RegisterMessageCallback(
68 base::Bind(&DisplayOverscanHandler::HandleReset
,
69 base::Unretained(this)));
70 web_ui()->RegisterMessageCallback(
72 base::Bind(&DisplayOverscanHandler::HandleCancel
,
73 base::Unretained(this)));
74 web_ui()->RegisterMessageCallback(
76 base::Bind(&DisplayOverscanHandler::HandleMove
,
77 base::Unretained(this)));
78 web_ui()->RegisterMessageCallback(
80 base::Bind(&DisplayOverscanHandler::HandleResize
,
81 base::Unretained(this)));
84 void DisplayOverscanHandler::OnDisplayAdded(const gfx::Display
& new_display
) {
85 if (!overscan_calibrator_
)
88 web_ui()->CallJavascriptFunction(
89 "options.DisplayOverscan.onOverscanCanceled");
92 void DisplayOverscanHandler::OnDisplayRemoved(const gfx::Display
& old_display
) {
93 if (!overscan_calibrator_
)
96 web_ui()->CallJavascriptFunction(
97 "options.DisplayOverscan.onOverscanCanceled");
100 void DisplayOverscanHandler::OnDisplayMetricsChanged(const gfx::Display
&,
104 void DisplayOverscanHandler::HandleStart(const base::ListValue
* args
) {
105 int64 display_id
= gfx::Display::kInvalidDisplayID
;
106 std::string id_value
;
107 if (!args
->GetString(0, &id_value
)) {
108 LOG(ERROR
) << "Can't find ID";
112 if (!base::StringToInt64(id_value
, &display_id
) ||
113 display_id
== gfx::Display::kInvalidDisplayID
) {
114 LOG(ERROR
) << "Invalid parameter: " << id_value
;
118 const gfx::Display
& display
=
119 ash::Shell::GetInstance()->display_manager()->GetDisplayForId(display_id
);
120 DCHECK(display
.is_valid());
121 if (!display
.is_valid())
124 ash::WindowTreeHostManager
* window_tree_host_manager
=
125 ash::Shell::GetInstance()->window_tree_host_manager();
126 overscan_calibrator_
.reset(new OverscanCalibrator(
127 display
, window_tree_host_manager
->GetOverscanInsets(display_id
)));
130 void DisplayOverscanHandler::HandleCommit(const base::ListValue
* unused_args
) {
131 if (overscan_calibrator_
.get()) {
132 overscan_calibrator_
->Commit();
133 overscan_calibrator_
.reset();
137 void DisplayOverscanHandler::HandleReset(const base::ListValue
* unused_args
) {
138 if (overscan_calibrator_
.get())
139 overscan_calibrator_
->Reset();
142 void DisplayOverscanHandler::HandleCancel(const base::ListValue
* unused_args
) {
143 overscan_calibrator_
.reset();
146 void DisplayOverscanHandler::HandleMove(const base::ListValue
* args
) {
147 std::string orientation
;
149 if (!args
->GetString(0, &orientation
)) {
150 LOG(ERROR
) << "The first argument must be orientation";
153 if (!args
->GetDouble(1, &length
)) {
154 LOG(ERROR
) << "The second argument must be a numeric";
158 if (!overscan_calibrator_
.get())
161 gfx::Insets insets
= overscan_calibrator_
->insets();
162 if (orientation
== kOrientationHorizontal
) {
163 insets
.Set(insets
.top(), insets
.left() + length
,
164 insets
.bottom(), insets
.right() - length
);
165 } else if (orientation
== kOrientationVertical
) {
166 insets
.Set(insets
.top() + length
, insets
.left(),
167 insets
.bottom() - length
, insets
.right());
169 LOG(ERROR
) << "The orientation must be '" << kOrientationHorizontal
170 << "' or '" << kOrientationVertical
<< "': "
174 overscan_calibrator_
->UpdateInsets(insets
);
177 void DisplayOverscanHandler::HandleResize(const base::ListValue
* args
) {
178 std::string orientation
;
180 if (!args
->GetString(0, &orientation
)) {
181 LOG(ERROR
) << "The first argument must be orientation";
184 if (!args
->GetDouble(1, &length
)) {
185 LOG(ERROR
) << "The second argument must be a numeric";
189 if (!overscan_calibrator_
.get())
192 gfx::Insets insets
= overscan_calibrator_
->insets();
193 if (orientation
== kOrientationHorizontal
) {
194 insets
.Set(insets
.top(), insets
.left() + length
,
195 insets
.bottom(), insets
.right() + length
);
196 } else if (orientation
== kOrientationVertical
) {
197 insets
.Set(insets
.top() + length
, insets
.left(),
198 insets
.bottom() + length
, insets
.right());
200 LOG(ERROR
) << "The orientation must be '" << kOrientationHorizontal
201 << "' or '" << kOrientationVertical
<< "': "
205 overscan_calibrator_
->UpdateInsets(insets
);
208 } // namespace options
209 } // namespace chromeos