| 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 "ash/system/chromeos/tray_display.h" | 5 #include "ash/system/chromeos/tray_display.h" |
| 6 | 6 |
| 7 #include "ash/display/display_controller.h" | 7 #include "ash/display/display_controller.h" |
| 8 #include "ash/display/display_manager.h" | 8 #include "ash/display/display_manager.h" |
| 9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 10 #include "ash/system/tray/fixed_sized_image_view.h" | 10 #include "ash/system/tray/fixed_sized_image_view.h" |
| 11 #include "ash/system/tray/system_tray.h" | 11 #include "ash/system/tray/system_tray.h" |
| 12 #include "ash/system/tray/system_tray_delegate.h" | 12 #include "ash/system/tray/system_tray_delegate.h" |
| 13 #include "ash/system/tray/tray_constants.h" | 13 #include "ash/system/tray/tray_constants.h" |
| 14 #include "ash/system/tray/tray_notification_view.h" | 14 #include "ash/system/tray/tray_notification_view.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "grit/ash_resources.h" | 16 #include "grit/ash_resources.h" |
| 17 #include "grit/ash_strings.h" | 17 #include "grit/ash_strings.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 20 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
| 21 #include "ui/views/layout/box_layout.h" | 21 #include "ui/views/layout/box_layout.h" |
| 22 | 22 |
| 23 namespace ash { | 23 namespace ash { |
| 24 namespace internal { | 24 namespace internal { |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 TrayDisplayMode GetCurrentTrayDisplayMode() { | 27 bool display_notifications_enabled = true; |
| 28 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | |
| 29 if (display_manager->GetNumDisplays() > 1) | |
| 30 return TRAY_DISPLAY_EXTENDED; | |
| 31 | 28 |
| 32 if (display_manager->IsMirrored()) | 29 DisplayManager* GetDisplayManager() { |
| 33 return TRAY_DISPLAY_MIRRORED; | 30 return Shell::GetInstance()->display_manager(); |
| 31 } |
| 34 | 32 |
| 35 int64 first_id = display_manager->first_display_id(); | 33 base::string16 GetDisplayName(int64 display_id) { |
| 36 if (display_manager->HasInternalDisplay() && | 34 return UTF8ToUTF16(GetDisplayManager()->GetDisplayNameForId(display_id)); |
| 37 !display_manager->IsInternalDisplayId(first_id)) { | 35 } |
| 38 return TRAY_DISPLAY_DOCKED; | |
| 39 } | |
| 40 | 36 |
| 41 return TRAY_DISPLAY_SINGLE; | 37 base::string16 GetDisplaySize(int64 display_id) { |
| 38 return UTF8ToUTF16( |
| 39 GetDisplayManager()->GetDisplayForId(display_id).size().ToString()); |
| 40 } |
| 41 |
| 42 bool ShouldShowResolution(int64 display_id) { |
| 43 if (!GetDisplayManager()->GetDisplayForId(display_id).is_valid()) |
| 44 return false; |
| 45 |
| 46 const DisplayInfo& display_info = |
| 47 GetDisplayManager()->GetDisplayInfo(display_id); |
| 48 return display_info.rotation() != gfx::Display::ROTATE_0 || |
| 49 display_info.ui_scale() != 1.0f; |
| 42 } | 50 } |
| 43 | 51 |
| 44 // Returns the name of the currently connected external display. | 52 // Returns the name of the currently connected external display. |
| 45 base::string16 GetExternalDisplayName() { | 53 base::string16 GetExternalDisplayName() { |
| 46 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 54 DisplayManager* display_manager = GetDisplayManager(); |
| 47 int64 external_id = display_manager->mirrored_display().id(); | 55 int64 external_id = display_manager->mirrored_display().id(); |
| 48 | 56 |
| 49 if (external_id == gfx::Display::kInvalidDisplayID) { | 57 if (external_id == gfx::Display::kInvalidDisplayID) { |
| 50 int64 internal_display_id = gfx::Display::InternalDisplayId(); | 58 int64 internal_display_id = gfx::Display::InternalDisplayId(); |
| 51 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { | 59 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { |
| 52 int64 id = display_manager->GetDisplayAt(i)->id(); | 60 int64 id = display_manager->GetDisplayAt(i)->id(); |
| 53 if (id != internal_display_id) { | 61 if (id != internal_display_id) { |
| 54 external_id = id; | 62 external_id = id; |
| 55 break; | 63 break; |
| 56 } | 64 } |
| 57 } | 65 } |
| 58 } | 66 } |
| 59 if (external_id != gfx::Display::kInvalidDisplayID) | 67 |
| 60 return UTF8ToUTF16(display_manager->GetDisplayNameForId(external_id)); | 68 if (external_id == gfx::Display::kInvalidDisplayID) |
| 61 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME); | 69 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_UNKNOWN_DISPLAY_NAME); |
| 70 |
| 71 // The external display name may have an annotation of "(width x height)" in |
| 72 // case that the display is rotated or its resolution is changed. |
| 73 base::string16 name = GetDisplayName(external_id); |
| 74 if (ShouldShowResolution(external_id)) |
| 75 name += UTF8ToUTF16(" (") + GetDisplaySize(external_id) + UTF8ToUTF16(")"); |
| 76 |
| 77 return name; |
| 62 } | 78 } |
| 63 | 79 |
| 64 class DisplayViewBase { | 80 base::string16 GetTrayDisplayMessage() { |
| 65 public: | 81 DisplayManager* display_manager = GetDisplayManager(); |
| 66 DisplayViewBase(user::LoginStatus login_status) | 82 if (display_manager->GetNumDisplays() > 1) { |
| 67 : login_status_(login_status) { | 83 if (GetDisplayManager()->HasInternalDisplay()) { |
| 68 label_ = new views::Label(); | 84 return l10n_util::GetStringFUTF16( |
| 69 label_->SetMultiLine(true); | 85 IDS_ASH_STATUS_TRAY_DISPLAY_EXTENDED, GetExternalDisplayName()); |
| 70 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 86 } |
| 87 return l10n_util::GetStringUTF16( |
| 88 IDS_ASH_STATUS_TRAY_DISPLAY_EXTENDED_NO_INTERNAL); |
| 71 } | 89 } |
| 72 | 90 |
| 73 virtual ~DisplayViewBase() { | 91 if (display_manager->IsMirrored()) { |
| 92 if (GetDisplayManager()->HasInternalDisplay()) { |
| 93 return l10n_util::GetStringFUTF16( |
| 94 IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING, GetExternalDisplayName()); |
| 95 } |
| 96 return l10n_util::GetStringUTF16( |
| 97 IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING_NO_INTERNAL); |
| 74 } | 98 } |
| 75 | 99 |
| 76 protected: | 100 int64 first_id = display_manager->first_display_id(); |
| 77 void OpenSettings() { | 101 if (display_manager->HasInternalDisplay() && |
| 78 if (login_status_ == ash::user::LOGGED_IN_USER || | 102 !display_manager->IsInternalDisplayId(first_id)) { |
| 79 login_status_ == ash::user::LOGGED_IN_OWNER || | 103 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_DOCKED); |
| 80 login_status_ == ash::user::LOGGED_IN_GUEST) { | |
| 81 ash::Shell::GetInstance()->system_tray_delegate()->ShowDisplaySettings(); | |
| 82 } | |
| 83 } | 104 } |
| 84 | 105 |
| 85 bool UpdateLabelText() { | 106 return base::string16(); |
| 86 switch (GetCurrentTrayDisplayMode()) { | 107 } |
| 87 case TRAY_DISPLAY_SINGLE: | 108 |
| 88 // TODO(oshima|mukai): Support single display mode for overscan | 109 void OpenSettings(user::LoginStatus login_status) { |
| 89 // alignment. | 110 if (login_status == ash::user::LOGGED_IN_USER || |
| 90 return false; | 111 login_status == ash::user::LOGGED_IN_OWNER || |
| 91 case TRAY_DISPLAY_EXTENDED: | 112 login_status == ash::user::LOGGED_IN_GUEST) { |
| 92 if (Shell::GetInstance()->display_manager()->HasInternalDisplay()) { | 113 ash::Shell::GetInstance()->system_tray_delegate()->ShowDisplaySettings(); |
| 93 label_->SetText(l10n_util::GetStringFUTF16( | |
| 94 IDS_ASH_STATUS_TRAY_DISPLAY_EXTENDED, GetExternalDisplayName())); | |
| 95 } else { | |
| 96 label_->SetText(l10n_util::GetStringUTF16( | |
| 97 IDS_ASH_STATUS_TRAY_DISPLAY_EXTENDED_NO_INTERNAL)); | |
| 98 } | |
| 99 break; | |
| 100 case TRAY_DISPLAY_MIRRORED: | |
| 101 if (Shell::GetInstance()->display_manager()->HasInternalDisplay()) { | |
| 102 label_->SetText(l10n_util::GetStringFUTF16( | |
| 103 IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING, GetExternalDisplayName())); | |
| 104 } else { | |
| 105 label_->SetText(l10n_util::GetStringUTF16( | |
| 106 IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING_NO_INTERNAL)); | |
| 107 } | |
| 108 break; | |
| 109 case TRAY_DISPLAY_DOCKED: | |
| 110 label_->SetText(l10n_util::GetStringUTF16( | |
| 111 IDS_ASH_STATUS_TRAY_DISPLAY_DOCKED)); | |
| 112 break; | |
| 113 } | |
| 114 return true; | |
| 115 } | 114 } |
| 116 | 115 } |
| 117 views::Label* label() { return label_; } | |
| 118 | |
| 119 private: | |
| 120 user::LoginStatus login_status_; | |
| 121 views::Label* label_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(DisplayViewBase); | |
| 124 }; | |
| 125 | 116 |
| 126 } // namespace | 117 } // namespace |
| 127 | 118 |
| 128 class DisplayView : public DisplayViewBase, | 119 DisplayView::DisplayView(user::LoginStatus login_status) |
| 129 public ash::internal::ActionableView { | 120 : login_status_(login_status) { |
| 130 public: | 121 SetLayoutManager(new views::BoxLayout( |
| 131 explicit DisplayView(user::LoginStatus login_status) | 122 views::BoxLayout::kHorizontal, |
| 132 : DisplayViewBase(login_status) { | 123 ash::kTrayPopupPaddingHorizontal, 0, |
| 133 SetLayoutManager(new | 124 ash::kTrayPopupPaddingBetweenItems)); |
| 134 views::BoxLayout(views::BoxLayout::kHorizontal, | |
| 135 ash::kTrayPopupPaddingHorizontal, 0, | |
| 136 ash::kTrayPopupPaddingBetweenItems)); | |
| 137 | 125 |
| 138 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 126 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| 139 image_ = | 127 image_ = new ash::internal::FixedSizedImageView(0, ash::kTrayPopupItemHeight); |
| 140 new ash::internal::FixedSizedImageView(0, ash::kTrayPopupItemHeight); | 128 image_->SetImage( |
| 141 image_->SetImage( | 129 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_DISPLAY).ToImageSkia()); |
| 142 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_DISPLAY).ToImageSkia()); | 130 AddChildView(image_); |
| 143 AddChildView(image_); | |
| 144 AddChildView(label()); | |
| 145 Update(); | |
| 146 } | |
| 147 | 131 |
| 148 virtual ~DisplayView() {} | 132 label_ = new views::Label(); |
| 133 label_->SetMultiLine(true); |
| 134 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 135 AddChildView(label_); |
| 136 Update(); |
| 137 } |
| 149 | 138 |
| 150 void Update() { | 139 DisplayView::~DisplayView() { |
| 151 SetVisible(UpdateLabelText()); | 140 } |
| 152 } | |
| 153 | 141 |
| 154 private: | 142 void DisplayView::Update() { |
| 155 // Overridden from ActionableView. | 143 base::string16 message = GetTrayDisplayMessage(); |
| 156 virtual bool PerformAction(const ui::Event& event) OVERRIDE { | 144 if (message.empty()) |
| 157 OpenSettings(); | 145 message = GetInternalDisplayInfo(); |
| 158 return true; | 146 SetVisible(!message.empty()); |
| 159 } | 147 label_->SetText(message); |
| 148 } |
| 160 | 149 |
| 161 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { | 150 bool DisplayView::GetTooltipText(const gfx::Point& p, |
| 162 int label_max_width = bounds().width() - kTrayPopupPaddingHorizontal * 2 - | 151 string16* tooltip) const { |
| 163 kTrayPopupPaddingBetweenItems - image_->GetPreferredSize().width(); | 152 base::string16 tray_message = GetTrayDisplayMessage(); |
| 164 label()->SizeToFit(label_max_width); | 153 base::string16 internal_message = GetInternalDisplayInfo(); |
| 165 PreferredSizeChanged(); | 154 if (tray_message.empty() && internal_message.empty()) |
| 166 } | 155 return false; |
| 167 | 156 |
| 168 views::ImageView* image_; | 157 *tooltip = tray_message + ASCIIToUTF16("\n") + internal_message; |
| 158 return true; |
| 159 } |
| 169 | 160 |
| 170 DISALLOW_COPY_AND_ASSIGN(DisplayView); | 161 base::string16 DisplayView::GetInternalDisplayInfo() const { |
| 171 }; | 162 int64 first_id = GetDisplayManager()->first_display_id(); |
| 163 if (!ShouldShowResolution(first_id)) |
| 164 return base::string16(); |
| 172 | 165 |
| 173 class DisplayNotificationView : public DisplayViewBase, | 166 return l10n_util::GetStringFUTF16( |
| 174 public TrayNotificationView { | 167 IDS_ASH_STATUS_TRAY_DISPLAY_SINGLE_DISPLAY, |
| 168 GetDisplayName(first_id), |
| 169 GetDisplaySize(first_id)); |
| 170 } |
| 171 |
| 172 bool DisplayView::PerformAction(const ui::Event& event) { |
| 173 OpenSettings(login_status_); |
| 174 return true; |
| 175 } |
| 176 |
| 177 void DisplayView::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 178 int label_max_width = bounds().width() - kTrayPopupPaddingHorizontal * 2 - |
| 179 kTrayPopupPaddingBetweenItems - image_->GetPreferredSize().width(); |
| 180 label_->SizeToFit(label_max_width); |
| 181 PreferredSizeChanged(); |
| 182 } |
| 183 |
| 184 class DisplayNotificationView : public TrayNotificationView { |
| 175 public: | 185 public: |
| 176 DisplayNotificationView(user::LoginStatus login_status, | 186 DisplayNotificationView(user::LoginStatus login_status, |
| 177 TrayDisplay* tray_item) | 187 TrayDisplay* tray_item, |
| 178 : DisplayViewBase(login_status), | 188 const base::string16& message) |
| 179 TrayNotificationView(tray_item, IDR_AURA_UBER_TRAY_DISPLAY) { | 189 : TrayNotificationView(tray_item, IDR_AURA_UBER_TRAY_DISPLAY), |
| 180 InitView(label()); | 190 login_status_(login_status) { |
| 181 StartAutoCloseTimer(kTrayPopupAutoCloseDelayForTextInSeconds); | 191 StartAutoCloseTimer(kTrayPopupAutoCloseDelayForTextInSeconds); |
| 182 Update(); | 192 Update(message); |
| 183 } | 193 } |
| 184 | 194 |
| 185 virtual ~DisplayNotificationView() {} | 195 virtual ~DisplayNotificationView() {} |
| 186 | 196 |
| 187 void Update() { | 197 void Update(const base::string16& message) { |
| 188 if (UpdateLabelText()) | 198 if (message.empty()) { |
| 199 owner()->HideNotificationView(); |
| 200 } else { |
| 201 views::Label* label = new views::Label(message); |
| 202 label->SetMultiLine(true); |
| 203 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 204 UpdateView(label); |
| 189 RestartAutoCloseTimer(); | 205 RestartAutoCloseTimer(); |
| 190 else | 206 } |
| 191 owner()->HideNotificationView(); | |
| 192 } | 207 } |
| 193 | 208 |
| 194 // Overridden from TrayNotificationView: | 209 // Overridden from TrayNotificationView: |
| 195 virtual void OnClickAction() OVERRIDE { | 210 virtual void OnClickAction() OVERRIDE { |
| 196 OpenSettings(); | 211 OpenSettings(login_status_); |
| 197 } | 212 } |
| 198 | 213 |
| 199 private: | 214 private: |
| 215 user::LoginStatus login_status_; |
| 216 |
| 200 DISALLOW_COPY_AND_ASSIGN(DisplayNotificationView); | 217 DISALLOW_COPY_AND_ASSIGN(DisplayNotificationView); |
| 201 }; | 218 }; |
| 202 | 219 |
| 203 TrayDisplay::TrayDisplay(SystemTray* system_tray) | 220 TrayDisplay::TrayDisplay(SystemTray* system_tray) |
| 204 : SystemTrayItem(system_tray), | 221 : SystemTrayItem(system_tray), |
| 205 default_(NULL), | 222 default_(NULL), |
| 206 notification_(NULL), | 223 notification_(NULL) { |
| 207 current_mode_(GetCurrentTrayDisplayMode()) { | 224 current_message_ = GetDisplayMessageForNotification(); |
| 208 Shell::GetInstance()->display_controller()->AddObserver(this); | 225 Shell::GetInstance()->display_controller()->AddObserver(this); |
| 209 } | 226 } |
| 210 | 227 |
| 211 TrayDisplay::~TrayDisplay() { | 228 TrayDisplay::~TrayDisplay() { |
| 212 Shell::GetInstance()->display_controller()->RemoveObserver(this); | 229 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| 213 } | 230 } |
| 214 | 231 |
| 232 base::string16 TrayDisplay::GetDisplayMessageForNotification() { |
| 233 DisplayManager* display_manager = GetDisplayManager(); |
| 234 std::map<int64, DisplayInfo> old_info; |
| 235 old_info.swap(display_info_); |
| 236 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { |
| 237 int64 id = display_manager->GetDisplayAt(i)->id(); |
| 238 display_info_[id] = display_manager->GetDisplayInfo(id); |
| 239 } |
| 240 |
| 241 if (display_info_.size() == old_info.size()) { |
| 242 for (std::map<int64, DisplayInfo>::const_iterator iter = |
| 243 display_info_.begin(); iter != display_info_.end(); ++iter) { |
| 244 std::map<int64, DisplayInfo>::const_iterator old_iter = |
| 245 old_info.find(iter->first); |
| 246 if (old_iter == old_info.end()) |
| 247 break; |
| 248 |
| 249 if (iter->second.ui_scale() != old_iter->second.ui_scale()) { |
| 250 return l10n_util::GetStringFUTF16( |
| 251 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED, |
| 252 GetDisplayName(iter->first), |
| 253 GetDisplaySize(iter->first)); |
| 254 } |
| 255 if (iter->second.rotation() != old_iter->second.rotation()) { |
| 256 return l10n_util::GetStringFUTF16( |
| 257 IDS_ASH_STATUS_TRAY_DISPLAY_ROTATED, GetDisplayName(iter->first)); |
| 258 } |
| 259 } |
| 260 } |
| 261 |
| 262 return GetTrayDisplayMessage(); |
| 263 } |
| 264 |
| 215 views::View* TrayDisplay::CreateDefaultView(user::LoginStatus status) { | 265 views::View* TrayDisplay::CreateDefaultView(user::LoginStatus status) { |
| 216 DCHECK(default_ == NULL); | 266 DCHECK(default_ == NULL); |
| 217 default_ = new DisplayView(status); | 267 default_ = new DisplayView(status); |
| 218 return default_; | 268 return default_; |
| 219 } | 269 } |
| 220 | 270 |
| 221 views::View* TrayDisplay::CreateNotificationView(user::LoginStatus status) { | 271 views::View* TrayDisplay::CreateNotificationView(user::LoginStatus status) { |
| 222 DCHECK(notification_ == NULL); | 272 DCHECK(notification_ == NULL); |
| 223 notification_ = new DisplayNotificationView(status, this); | 273 notification_ = new DisplayNotificationView(status, this, current_message_); |
| 224 return notification_; | 274 return notification_; |
| 225 } | 275 } |
| 226 | 276 |
| 227 void TrayDisplay::DestroyDefaultView() { | 277 void TrayDisplay::DestroyDefaultView() { |
| 228 default_ = NULL; | 278 default_ = NULL; |
| 229 } | 279 } |
| 230 | 280 |
| 231 void TrayDisplay::DestroyNotificationView() { | 281 void TrayDisplay::DestroyNotificationView() { |
| 232 notification_ = NULL; | 282 notification_ = NULL; |
| 233 } | 283 } |
| 234 | 284 |
| 235 bool TrayDisplay::ShouldShowLauncher() const { | 285 bool TrayDisplay::ShouldShowLauncher() const { |
| 236 return false; | 286 return false; |
| 237 } | 287 } |
| 238 | 288 |
| 239 void TrayDisplay::OnDisplayConfigurationChanged() { | 289 void TrayDisplay::OnDisplayConfigurationChanged() { |
| 240 TrayDisplayMode new_mode = GetCurrentTrayDisplayMode(); | 290 if (!display_notifications_enabled) |
| 241 if (current_mode_ != new_mode && new_mode != TRAY_DISPLAY_SINGLE) { | 291 return; |
| 242 if (notification_) | 292 |
| 243 notification_->Update(); | 293 // TODO(mukai): do not show the notification when the configuration changed |
| 244 else | 294 // due to the user operation on display settings page. |
| 245 ShowNotificationView(); | 295 current_message_ = GetDisplayMessageForNotification(); |
| 246 } | 296 if (notification_) |
| 247 current_mode_ = new_mode; | 297 notification_->Update(current_message_); |
| 298 else if (!current_message_.empty()) |
| 299 ShowNotificationView(); |
| 300 } |
| 301 |
| 302 // static |
| 303 void TrayDisplay::SetDisplayNotificationsEnabledForTest(bool enabled) { |
| 304 display_notifications_enabled = enabled; |
| 248 } | 305 } |
| 249 | 306 |
| 250 } // namespace internal | 307 } // namespace internal |
| 251 } // namespace ash | 308 } // namespace ash |
| OLD | NEW |