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

Side by Side Diff: ash/display/resolution_notification_controller.cc

Issue 22703004: Creates notifications for display resolution change. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix 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
OLDNEW
(Empty)
1 // Copyright 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.
4
5 #include "ash/display/resolution_notification_controller.h"
6
7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/shell.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "grit/ash_resources.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/l10n/time_format.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/display.h"
17 #include "ui/gfx/screen.h"
18 #include "ui/message_center/message_center.h"
19 #include "ui/message_center/notification.h"
20 #include "ui/message_center/notification_delegate.h"
21
22 using message_center::Notification;
23
24 namespace ash {
25 namespace internal {
26 namespace {
27
28 bool g_use_timer = true;
29
30 class ResolutionChangeNotificationDelegate
31 : public message_center::NotificationDelegate {
32 public:
33 ResolutionChangeNotificationDelegate(
34 ResolutionNotificationController* controller,
35 bool has_timeout);
36
37 protected:
38 virtual ~ResolutionChangeNotificationDelegate();
39
40 private:
41 // message_center::NotificationDelegate overrides:
42 virtual void Display() OVERRIDE;
43 virtual void Error() OVERRIDE;
44 virtual void Close(bool by_user) OVERRIDE;
45 virtual void Click() OVERRIDE;
46 virtual bool HasClickedListener() OVERRIDE;
47 virtual void ButtonClick(int button_index) OVERRIDE;
48
49 ResolutionNotificationController* controller_;
50 bool has_timeout_;
51
52 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate);
53 };
54
55 ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
56 ResolutionNotificationController* controller,
57 bool has_timeout)
58 : controller_(controller),
59 has_timeout_(has_timeout) {
60 DCHECK(controller_);
61 }
62
63 ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
64 }
65
66 void ResolutionChangeNotificationDelegate::Display() {
67 }
68
69 void ResolutionChangeNotificationDelegate::Error() {
70 }
71
72 void ResolutionChangeNotificationDelegate::Close(bool by_user) {
73 if (by_user)
74 controller_->AcceptResolutionChange();
75 }
76
77 void ResolutionChangeNotificationDelegate::Click() {
78 controller_->AcceptResolutionChange();
79 }
80
81 bool ResolutionChangeNotificationDelegate::HasClickedListener() {
82 return true;
83 }
84
85 void ResolutionChangeNotificationDelegate::ButtonClick(int button_index) {
86 // If there's the timeout, the first button is "Accept". Otherwise the
87 // button click should be "Revert".
88 if (has_timeout_ && button_index == 0)
89 controller_->AcceptResolutionChange();
90 else
91 controller_->RevertResolutionChange();
92 }
93
94 } // namespace
95
96 // static
97 const int ResolutionNotificationController::kTimeoutInSec = 15;
98
99 // static
100 const char ResolutionNotificationController::kNotificationId[] =
101 "chrome://settings/display/resolution";
102
103 ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
104 int64 display_id,
105 const gfx::Size& old_resolution,
106 const gfx::Size& new_resolution,
107 const base::Closure& accept_callback)
108 : display_id(display_id),
109 old_resolution(old_resolution),
110 new_resolution(new_resolution),
111 accept_callback(accept_callback),
112 timeout_count(0) {
113 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
114 if (!display_manager->HasInternalDisplay() &&
115 display_manager->num_connected_displays() == 1u) {
116 timeout_count = kTimeoutInSec;
117 }
118 }
119
120 ResolutionNotificationController::ResolutionChangeInfo::
121 ~ResolutionChangeInfo() {
122 }
123
124 ResolutionNotificationController::ResolutionNotificationController() {
125 Shell::GetInstance()->display_controller()->AddObserver(this);
126 Shell::GetScreen()->AddObserver(this);
127 }
128
129 ResolutionNotificationController::~ResolutionNotificationController() {
130 Shell::GetInstance()->display_controller()->RemoveObserver(this);
131 Shell::GetScreen()->RemoveObserver(this);
132 }
133
134 void ResolutionNotificationController::SetDisplayResolutionAndNotify(
135 int64 display_id,
136 const gfx::Size& old_resolution,
137 const gfx::Size& new_resolution,
138 const base::Closure& accept_callback) {
139 change_info_.reset(new ResolutionChangeInfo(
140 display_id, old_resolution, new_resolution, accept_callback));
141
142 // SetDisplayResolution() causes OnConfigurationChanged() and the notification
143 // will be shown at that point.
144 Shell::GetInstance()->display_manager()->SetDisplayResolution(
145 display_id, new_resolution);
146 }
147
148 bool ResolutionNotificationController::DoesNotificationTimeout() {
149 return change_info_ && change_info_->timeout_count > 0;
150 }
151
152 void ResolutionNotificationController::CreateOrUpdateNotification() {
153 message_center::MessageCenter* message_center =
154 message_center::MessageCenter::Get();
155 if (!change_info_) {
156 message_center->RemoveNotification(kNotificationId, false /* by_user */);
157 return;
158 }
159
160 base::string16 timeout_message;
161 message_center::RichNotificationData data;
162 if (change_info_->timeout_count > 0) {
163 data.buttons.push_back(message_center::ButtonInfo(
164 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_ACCEPT)));
165 timeout_message = l10n_util::GetStringFUTF16(
166 IDS_ASH_DISPLAY_RESOLUTION_TIMEOUT,
167 ui::TimeFormat::TimeDurationLong(
168 base::TimeDelta::FromSeconds(change_info_->timeout_count)));
169 }
170 data.buttons.push_back(message_center::ButtonInfo(
171 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_REVERT)));
172
173 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
174 scoped_ptr<Notification> notification(new Notification(
175 message_center::NOTIFICATION_TYPE_SIMPLE,
176 kNotificationId,
177 l10n_util::GetStringFUTF16(
178 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
179 UTF8ToUTF16(Shell::GetInstance()->display_manager()->
180 GetDisplayNameForId(change_info_->display_id)),
181 UTF8ToUTF16(change_info_->new_resolution.ToString())),
182 timeout_message,
183 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_DISPLAY),
184 base::string16() /* display_source */,
185 std::string() /* extension_id */,
186 data,
187 new ResolutionChangeNotificationDelegate(
188 this, change_info_->timeout_count > 0)));
189 notification->SetSystemPriority();
190 message_center->AddNotification(notification.Pass());
191 }
192
193 void ResolutionNotificationController::OnTimerTick() {
194 if (!change_info_)
195 return;
196
197 --change_info_->timeout_count;
198 if (change_info_->timeout_count == 0)
199 RevertResolutionChange();
200 else
201 CreateOrUpdateNotification();
202 }
203
204 void ResolutionNotificationController::AcceptResolutionChange() {
205 message_center::MessageCenter::Get()->RemoveNotification(
206 kNotificationId, false /* by_user */);
207 base::Closure callback = change_info_->accept_callback;
208 change_info_.reset();
209 callback.Run();
210 }
211
212 void ResolutionNotificationController::RevertResolutionChange() {
213 message_center::MessageCenter::Get()->RemoveNotification(
214 kNotificationId, false /* by_user */);
215 int64 display_id = change_info_->display_id;
216 gfx::Size old_resolution = change_info_->old_resolution;
217 change_info_.reset();
218 Shell::GetInstance()->display_manager()->SetDisplayResolution(
219 display_id, old_resolution);
220 }
221
222 void ResolutionNotificationController::OnDisplayBoundsChanged(
223 const gfx::Display& display) {
224 }
225
226 void ResolutionNotificationController::OnDisplayAdded(
227 const gfx::Display& new_display) {
228 }
229
230 void ResolutionNotificationController::OnDisplayRemoved(
231 const gfx::Display& old_display) {
232 if (change_info_ && change_info_->display_id == old_display.id())
233 RevertResolutionChange();
234 }
235
236 void ResolutionNotificationController::OnDisplayConfigurationChanged() {
237 if (!change_info_)
238 return;
239
240 CreateOrUpdateNotification();
241 if (g_use_timer && change_info_->timeout_count > 0) {
242 change_info_->timer.Start(FROM_HERE,
243 base::TimeDelta::FromSeconds(1),
244 this,
245 &ResolutionNotificationController::OnTimerTick);
246 }
247 }
248
249 void ResolutionNotificationController::SuppressTimerForTest() {
250 g_use_timer = false;
251 }
252
253 } // namespace internal
254 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698