tray_power.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "ash/system/power/tray_power.h"
  5. #include <utility>
  6. #include "ash/accessibility/accessibility_delegate.h"
  7. #include "ash/public/cpp/shelf_config.h"
  8. #include "ash/resources/vector_icons/vector_icons.h"
  9. #include "ash/session/session_controller_impl.h"
  10. #include "ash/shell.h"
  11. #include "ash/strings/grit/ash_strings.h"
  12. #include "ash/style/ash_color_id.h"
  13. #include "ash/system/power/battery_notification.h"
  14. #include "ash/system/power/dual_role_notification.h"
  15. #include "ash/system/time/time_view.h"
  16. #include "ash/system/tray/tray_constants.h"
  17. #include "ash/system/tray/tray_item_view.h"
  18. #include "ash/system/tray/tray_utils.h"
  19. #include "base/command_line.h"
  20. #include "base/metrics/histogram.h"
  21. #include "base/time/time.h"
  22. #include "ui/accessibility/ax_node_data.h"
  23. #include "ui/base/resource/resource_bundle.h"
  24. #include "ui/chromeos/devicetype_utils.h"
  25. #include "ui/gfx/color_utils.h"
  26. #include "ui/gfx/image/image_skia_source.h"
  27. #include "ui/gfx/paint_vector_icon.h"
  28. #include "ui/message_center/message_center.h"
  29. #include "ui/message_center/public/cpp/notification.h"
  30. #include "ui/message_center/public/cpp/notification_delegate.h"
  31. #include "ui/views/border.h"
  32. #include "ui/views/controls/image_view.h"
  33. #include "ui/views/view.h"
  34. using message_center::MessageCenter;
  35. using message_center::Notification;
  36. namespace ash {
  37. PowerTrayView::PowerTrayView(Shelf* shelf) : TrayItemView(shelf) {
  38. CreateImageView();
  39. PowerStatus::Get()->AddObserver(this);
  40. }
  41. PowerTrayView::~PowerTrayView() {
  42. PowerStatus::Get()->RemoveObserver(this);
  43. }
  44. gfx::Size PowerTrayView::CalculatePreferredSize() const {
  45. // The battery icon is a lot thinner than other icons, hence the special
  46. // logic.
  47. gfx::Size standard_size = TrayItemView::CalculatePreferredSize();
  48. if (IsHorizontalAlignment())
  49. return gfx::Size(kUnifiedTrayBatteryWidth, standard_size.height());
  50. return standard_size;
  51. }
  52. void PowerTrayView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
  53. node_data->SetName(accessible_name_);
  54. }
  55. views::View* PowerTrayView::GetTooltipHandlerForPoint(const gfx::Point& point) {
  56. return GetLocalBounds().Contains(point) ? this : nullptr;
  57. }
  58. std::u16string PowerTrayView::GetTooltipText(const gfx::Point& p) const {
  59. return tooltip_;
  60. }
  61. const char* PowerTrayView::GetClassName() const {
  62. return "PowerTrayView";
  63. }
  64. void PowerTrayView::OnThemeChanged() {
  65. TrayItemView::OnThemeChanged();
  66. UpdateStatus();
  67. UpdateImage(/*icon_color_changed=*/true);
  68. }
  69. void PowerTrayView::HandleLocaleChange() {
  70. UpdateStatus();
  71. }
  72. void PowerTrayView::OnPowerStatusChanged() {
  73. UpdateStatus();
  74. }
  75. void PowerTrayView::OnSessionStateChanged(session_manager::SessionState state) {
  76. // Icon color changes only happens when switching session states between OOBE
  77. // and other state.
  78. const bool update_image =
  79. session_state_ == session_manager::SessionState::OOBE ||
  80. state == session_manager::SessionState::OOBE;
  81. session_state_ = state;
  82. UpdateImage(update_image);
  83. }
  84. void PowerTrayView::UpdateStatus() {
  85. UpdateImage(/*icon_color_changed=*/false);
  86. SetVisible(PowerStatus::Get()->IsBatteryPresent());
  87. accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true);
  88. tooltip_ = PowerStatus::Get()->GetInlinedStatusString();
  89. // Currently ChromeVox only reads the inner view when touching the icon.
  90. // As a result this node's accessible node data will not be read.
  91. image_view()->SetAccessibleName(accessible_name_);
  92. }
  93. void PowerTrayView::UpdateImage(bool icon_color_changed) {
  94. const PowerStatus::BatteryImageInfo& info =
  95. PowerStatus::Get()->GetBatteryImageInfo();
  96. // Only change the image when the info changes or the icon color has
  97. // changed. http://crbug.com/589348
  98. if (info_ && info_->ApproximatelyEqual(info) && !icon_color_changed)
  99. return;
  100. info_ = info;
  101. // Note: The icon color (both fg and bg) changes when the UI in in OOBE mode.
  102. const SkColor icon_fg_color = TrayIconColor(session_state_);
  103. const SkColor icon_bg_color = color_utils::GetResultingPaintColor(
  104. ShelfConfig::Get()->GetShelfControlButtonColor(),
  105. GetColorProvider()->GetColor(kColorAshShieldAndBaseOpaque));
  106. image_view()->SetImage(PowerStatus::GetBatteryImage(
  107. info, kUnifiedTrayBatteryIconSize, icon_bg_color, icon_fg_color));
  108. }
  109. } // namespace ash