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.
5 #include "ash/system/chromeos/tray_display.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_manager.h"
10 #include "ash/system/system_notifier.h"
11 #include "ash/system/tray/actionable_view.h"
12 #include "ash/system/tray/fixed_sized_image_view.h"
13 #include "ash/system/tray/system_tray.h"
14 #include "ash/system/tray/system_tray_delegate.h"
15 #include "ash/system/tray/tray_constants.h"
16 #include "ash/system/tray/tray_notification_view.h"
17 #include "base/bind.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "grit/ash_resources.h"
21 #include "grit/ash_strings.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/message_center/message_center.h"
25 #include "ui/message_center/notification.h"
26 #include "ui/message_center/notification_delegate.h"
27 #include "ui/views/controls/image_view.h"
28 #include "ui/views/controls/label.h"
29 #include "ui/views/layout/box_layout.h"
31 using message_center::Notification;
37 DisplayManager* GetDisplayManager() {
38 return Shell::GetInstance()->display_manager();
41 base::string16 GetDisplayName(int64 display_id) {
42 return base::UTF8ToUTF16(
43 GetDisplayManager()->GetDisplayNameForId(display_id));
46 base::string16 GetDisplaySize(int64 display_id) {
47 DisplayManager* display_manager = GetDisplayManager();
49 const gfx::Display* display = &display_manager->GetDisplayForId(display_id);
51 // We don't show display size for mirrored display. Fallback
52 // to empty string if this happens on release build.
53 bool mirrored_display = display_manager->mirrored_display_id() == display_id;
54 DCHECK(!mirrored_display);
56 return base::string16();
58 DCHECK(display->is_valid());
59 return base::UTF8ToUTF16(display->size().ToString());
62 // Returns 1-line information for the specified display, like
63 // "InternalDisplay: 1280x750"
64 base::string16 GetDisplayInfoLine(int64 display_id) {
65 const DisplayInfo& display_info =
66 GetDisplayManager()->GetDisplayInfo(display_id);
67 if (GetDisplayManager()->mirrored_display_id() == display_id)
68 return GetDisplayName(display_id);
70 base::string16 size_text = GetDisplaySize(display_id);
71 base::string16 display_data;
72 if (display_info.has_overscan()) {
73 display_data = l10n_util::GetStringFUTF16(
74 IDS_ASH_STATUS_TRAY_DISPLAY_ANNOTATION,
76 l10n_util::GetStringUTF16(
77 IDS_ASH_STATUS_TRAY_DISPLAY_ANNOTATION_OVERSCAN));
79 display_data = size_text;
82 return l10n_util::GetStringFUTF16(
83 IDS_ASH_STATUS_TRAY_DISPLAY_SINGLE_DISPLAY,
84 GetDisplayName(display_id),
88 base::string16 GetAllDisplayInfo() {
89 DisplayManager* display_manager = GetDisplayManager();
90 std::vector<base::string16> lines;
91 int64 internal_id = gfx::Display::kInvalidDisplayID;
92 // Make sure to show the internal display first.
93 if (display_manager->HasInternalDisplay() &&
94 display_manager->IsInternalDisplayId(
95 display_manager->first_display_id())) {
96 internal_id = display_manager->first_display_id();
97 lines.push_back(GetDisplayInfoLine(internal_id));
100 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
101 int64 id = display_manager->GetDisplayAt(i).id();
102 if (id == internal_id)
104 lines.push_back(GetDisplayInfoLine(id));
107 return JoinString(lines, '\n');
110 void OpenSettings() {
111 // switch is intentionally introduced without default, to cause an error when
112 // a new type of login status is introduced.
113 switch (Shell::GetInstance()->system_tray_delegate()->GetUserLoginStatus()) {
114 case user::LOGGED_IN_NONE:
115 case user::LOGGED_IN_LOCKED:
118 case user::LOGGED_IN_USER:
119 case user::LOGGED_IN_OWNER:
120 case user::LOGGED_IN_GUEST:
121 case user::LOGGED_IN_RETAIL_MODE:
122 case user::LOGGED_IN_PUBLIC:
123 case user::LOGGED_IN_LOCALLY_MANAGED:
124 case user::LOGGED_IN_KIOSK_APP:
125 Shell::GetInstance()->system_tray_delegate()->ShowDisplaySettings();
131 const char TrayDisplay::kNotificationId[] = "chrome://settings/display";
133 class DisplayView : public internal::ActionableView {
135 explicit DisplayView() {
136 SetLayoutManager(new views::BoxLayout(
137 views::BoxLayout::kHorizontal,
138 kTrayPopupPaddingHorizontal, 0,
139 kTrayPopupPaddingBetweenItems));
141 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
142 image_ = new internal::FixedSizedImageView(0, kTrayPopupItemHeight);
144 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_DISPLAY).ToImageSkia());
145 AddChildView(image_);
147 label_ = new views::Label();
148 label_->SetMultiLine(true);
149 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
150 AddChildView(label_);
154 virtual ~DisplayView() {}
157 base::string16 message = GetTrayDisplayMessage(NULL);
158 if (message.empty() && ShouldShowFirstDisplayInfo())
159 message = GetDisplayInfoLine(GetDisplayManager()->first_display_id());
160 SetVisible(!message.empty());
161 label_->SetText(message);
165 const views::Label* label() const { return label_; }
167 // Overridden from views::View.
168 virtual bool GetTooltipText(const gfx::Point& p,
169 base::string16* tooltip) const OVERRIDE {
170 base::string16 tray_message = GetTrayDisplayMessage(NULL);
171 base::string16 display_message = GetAllDisplayInfo();
172 if (tray_message.empty() && display_message.empty())
175 *tooltip = tray_message + base::ASCIIToUTF16("\n") + display_message;
179 // Returns the name of the currently connected external display.
180 // This should not be used when the external display is used for
182 static base::string16 GetExternalDisplayName() {
183 DisplayManager* display_manager = GetDisplayManager();
184 DCHECK(!display_manager->IsMirrored());
186 int64 external_id = gfx::Display::kInvalidDisplayID;
187 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
188 int64 id = display_manager->GetDisplayAt(i).id();
189 if (id != gfx::Display::InternalDisplayId()) {
195 if (external_id == gfx::Display::kInvalidDisplayID) {
196 return l10n_util::GetStringUTF16(
197 IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME);
200 // The external display name may have an annotation of "(width x height)" in
201 // case that the display is rotated or its resolution is changed.
202 base::string16 name = GetDisplayName(external_id);
203 const DisplayInfo& display_info =
204 display_manager->GetDisplayInfo(external_id);
205 if (display_info.rotation() != gfx::Display::ROTATE_0 ||
206 display_info.configured_ui_scale() != 1.0f ||
207 !display_info.overscan_insets_in_dip().empty()) {
208 name = l10n_util::GetStringFUTF16(
209 IDS_ASH_STATUS_TRAY_DISPLAY_ANNOTATED_NAME,
210 name, GetDisplaySize(external_id));
211 } else if (display_info.overscan_insets_in_dip().empty() &&
212 display_info.has_overscan()) {
213 name = l10n_util::GetStringFUTF16(
214 IDS_ASH_STATUS_TRAY_DISPLAY_ANNOTATED_NAME,
215 name, l10n_util::GetStringUTF16(
216 IDS_ASH_STATUS_TRAY_DISPLAY_ANNOTATION_OVERSCAN));
222 static base::string16 GetTrayDisplayMessage(
223 base::string16* additional_message_out) {
224 DisplayManager* display_manager = GetDisplayManager();
225 if (display_manager->GetNumDisplays() > 1) {
226 if (GetDisplayManager()->HasInternalDisplay()) {
227 return l10n_util::GetStringFUTF16(
228 IDS_ASH_STATUS_TRAY_DISPLAY_EXTENDED, GetExternalDisplayName());
230 return l10n_util::GetStringUTF16(
231 IDS_ASH_STATUS_TRAY_DISPLAY_EXTENDED_NO_INTERNAL);
234 if (display_manager->IsMirrored()) {
235 if (GetDisplayManager()->HasInternalDisplay()) {
236 return l10n_util::GetStringFUTF16(
237 IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING,
238 GetDisplayName(display_manager->mirrored_display_id()));
240 return l10n_util::GetStringUTF16(
241 IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING_NO_INTERNAL);
244 int64 primary_id = Shell::GetScreen()->GetPrimaryDisplay().id();
245 if (display_manager->HasInternalDisplay() &&
246 !display_manager->IsInternalDisplayId(primary_id)) {
247 if (additional_message_out) {
248 *additional_message_out = l10n_util::GetStringUTF16(
249 IDS_ASH_STATUS_TRAY_DISPLAY_DOCKED_DESCRIPTION);
251 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_DOCKED);
254 return base::string16();
258 bool ShouldShowFirstDisplayInfo() const {
259 const DisplayInfo& display_info = GetDisplayManager()->GetDisplayInfo(
260 GetDisplayManager()->first_display_id());
261 return display_info.rotation() != gfx::Display::ROTATE_0 ||
262 display_info.configured_ui_scale() != 1.0f ||
263 !display_info.overscan_insets_in_dip().empty() ||
264 display_info.has_overscan();
267 // Overridden from ActionableView.
268 virtual bool PerformAction(const ui::Event& event) OVERRIDE {
273 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE {
274 int label_max_width = bounds().width() - kTrayPopupPaddingHorizontal * 2 -
275 kTrayPopupPaddingBetweenItems - image_->GetPreferredSize().width();
276 label_->SizeToFit(label_max_width);
279 views::ImageView* image_;
280 views::Label* label_;
282 DISALLOW_COPY_AND_ASSIGN(DisplayView);
285 TrayDisplay::TrayDisplay(SystemTray* system_tray)
286 : SystemTrayItem(system_tray),
288 Shell::GetInstance()->display_controller()->AddObserver(this);
289 UpdateDisplayInfo(NULL);
292 TrayDisplay::~TrayDisplay() {
293 Shell::GetInstance()->display_controller()->RemoveObserver(this);
296 void TrayDisplay::UpdateDisplayInfo(TrayDisplay::DisplayInfoMap* old_info) {
298 old_info->swap(display_info_);
299 display_info_.clear();
301 DisplayManager* display_manager = GetDisplayManager();
302 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
303 int64 id = display_manager->GetDisplayAt(i).id();
304 display_info_[id] = display_manager->GetDisplayInfo(id);
308 bool TrayDisplay::GetDisplayMessageForNotification(
309 const TrayDisplay::DisplayInfoMap& old_info,
310 base::string16* message_out,
311 base::string16* additional_message_out) {
312 // Display is added or removed. Use the same message as the one in
314 if (display_info_.size() != old_info.size()) {
315 *message_out = DisplayView::GetTrayDisplayMessage(additional_message_out);
319 for (DisplayInfoMap::const_iterator iter = display_info_.begin();
320 iter != display_info_.end(); ++iter) {
321 DisplayInfoMap::const_iterator old_iter = old_info.find(iter->first);
322 // The display's number is same but different displays. This happens
323 // for the transition between docked mode and mirrored display. Falls back
324 // to GetTrayDisplayMessage().
325 if (old_iter == old_info.end()) {
326 *message_out = DisplayView::GetTrayDisplayMessage(additional_message_out);
330 if (iter->second.configured_ui_scale() !=
331 old_iter->second.configured_ui_scale()) {
332 *message_out = l10n_util::GetStringFUTF16(
333 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
334 GetDisplayName(iter->first),
335 GetDisplaySize(iter->first));
338 if (iter->second.rotation() != old_iter->second.rotation()) {
339 int rotation_text_id = 0;
340 switch (iter->second.rotation()) {
341 case gfx::Display::ROTATE_0:
342 rotation_text_id = IDS_ASH_STATUS_TRAY_DISPLAY_STANDARD_ORIENTATION;
344 case gfx::Display::ROTATE_90:
345 rotation_text_id = IDS_ASH_STATUS_TRAY_DISPLAY_ORIENTATION_90;
347 case gfx::Display::ROTATE_180:
348 rotation_text_id = IDS_ASH_STATUS_TRAY_DISPLAY_ORIENTATION_180;
350 case gfx::Display::ROTATE_270:
351 rotation_text_id = IDS_ASH_STATUS_TRAY_DISPLAY_ORIENTATION_270;
354 *message_out = l10n_util::GetStringFUTF16(
355 IDS_ASH_STATUS_TRAY_DISPLAY_ROTATED,
356 GetDisplayName(iter->first),
357 l10n_util::GetStringUTF16(rotation_text_id));
362 // Found nothing special
366 void TrayDisplay::CreateOrUpdateNotification(
367 const base::string16& message,
368 const base::string16& additional_message) {
369 // Always remove the notification to make sure the notification appears
370 // as a popup in any situation.
371 message_center::MessageCenter::Get()->RemoveNotification(
372 kNotificationId, false /* by_user */);
377 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
378 scoped_ptr<Notification> notification(new Notification(
379 message_center::NOTIFICATION_TYPE_SIMPLE,
383 bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
384 base::string16(), // display_source
385 message_center::NotifierId(
386 message_center::NotifierId::SYSTEM_COMPONENT,
387 system_notifier::kNotifierDisplay),
388 message_center::RichNotificationData(),
389 new message_center::HandleNotificationClickedDelegate(
390 base::Bind(&OpenSettings))));
391 message_center::MessageCenter::Get()->AddNotification(notification.Pass());
394 views::View* TrayDisplay::CreateDefaultView(user::LoginStatus status) {
395 DCHECK(default_ == NULL);
396 default_ = new DisplayView();
400 void TrayDisplay::DestroyDefaultView() {
404 void TrayDisplay::OnDisplayConfigurationChanged() {
405 DisplayInfoMap old_info;
406 UpdateDisplayInfo(&old_info);
411 if (!Shell::GetInstance()->system_tray_delegate()->
412 ShouldShowDisplayNotification()) {
416 base::string16 message;
417 base::string16 additional_message;
418 if (GetDisplayMessageForNotification(old_info, &message, &additional_message))
419 CreateOrUpdateNotification(message, additional_message);
422 base::string16 TrayDisplay::GetDefaultViewMessage() const {
423 if (!default_ || !default_->visible())
424 return base::string16();
426 return static_cast<DisplayView*>(default_)->label()->text();
429 } // namespace internal