network_tray_view.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2018 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/network/network_tray_view.h"
  5. #include <utility>
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "ash/strings/grit/ash_strings.h"
  9. #include "ash/system/model/system_tray_model.h"
  10. #include "ash/system/network/network_icon.h"
  11. #include "ash/system/network/network_icon_animation.h"
  12. #include "ash/system/network/tray_network_state_model.h"
  13. #include "ui/accessibility/ax_enums.mojom.h"
  14. #include "ui/accessibility/ax_node_data.h"
  15. #include "ui/views/controls/image_view.h"
  16. namespace ash {
  17. namespace {
  18. // OOBE has a white background that makes regular tray icons not visible.
  19. network_icon::IconType GetIconType() {
  20. if (Shell::Get()->session_controller()->GetSessionState() ==
  21. session_manager::SessionState::OOBE) {
  22. return network_icon::ICON_TYPE_TRAY_OOBE;
  23. }
  24. return network_icon::ICON_TYPE_TRAY_REGULAR;
  25. }
  26. } // namespace
  27. NetworkTrayView::NetworkTrayView(Shelf* shelf, ActiveNetworkIcon::Type type)
  28. : TrayItemView(shelf), type_(type) {
  29. Shell::Get()->system_tray_model()->network_state_model()->AddObserver(this);
  30. Shell::Get()->session_controller()->AddObserver(this);
  31. CreateImageView();
  32. UpdateConnectionStatus(true /* notify_a11y */);
  33. }
  34. NetworkTrayView::~NetworkTrayView() {
  35. network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
  36. Shell::Get()->system_tray_model()->network_state_model()->RemoveObserver(
  37. this);
  38. Shell::Get()->session_controller()->RemoveObserver(this);
  39. }
  40. const char* NetworkTrayView::GetClassName() const {
  41. return "NetworkTrayView";
  42. }
  43. void NetworkTrayView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
  44. node_data->SetName(accessible_name_);
  45. node_data->SetDescription(accessible_description_);
  46. }
  47. std::u16string NetworkTrayView::GetAccessibleNameString() const {
  48. return tooltip_;
  49. }
  50. views::View* NetworkTrayView::GetTooltipHandlerForPoint(
  51. const gfx::Point& point) {
  52. return GetLocalBounds().Contains(point) ? this : nullptr;
  53. }
  54. std::u16string NetworkTrayView::GetTooltipText(const gfx::Point& p) const {
  55. return tooltip_;
  56. }
  57. void NetworkTrayView::HandleLocaleChange() {
  58. UpdateConnectionStatus(false /* notify_a11y */);
  59. }
  60. void NetworkTrayView::OnThemeChanged() {
  61. TrayItemView::OnThemeChanged();
  62. UpdateNetworkStateHandlerIcon();
  63. }
  64. void NetworkTrayView::NetworkIconChanged() {
  65. UpdateNetworkStateHandlerIcon();
  66. UpdateConnectionStatus(false /* notify_a11y */);
  67. }
  68. void NetworkTrayView::OnSessionStateChanged(
  69. session_manager::SessionState state) {
  70. UpdateNetworkStateHandlerIcon();
  71. }
  72. void NetworkTrayView::ActiveNetworkStateChanged() {
  73. UpdateNetworkStateHandlerIcon();
  74. UpdateConnectionStatus(true /* notify _a11y */);
  75. }
  76. void NetworkTrayView::NetworkListChanged() {
  77. UpdateNetworkStateHandlerIcon();
  78. }
  79. void NetworkTrayView::UpdateIcon(bool tray_icon_visible,
  80. const gfx::ImageSkia& image) {
  81. image_view()->SetImage(image);
  82. SetVisible(tray_icon_visible);
  83. SchedulePaint();
  84. }
  85. void NetworkTrayView::UpdateNetworkStateHandlerIcon() {
  86. bool animating = false;
  87. gfx::ImageSkia image =
  88. Shell::Get()->system_tray_model()->active_network_icon()->GetImage(
  89. type_, GetIconType(), &animating);
  90. bool show_in_tray = !image.isNull();
  91. UpdateIcon(show_in_tray, image);
  92. if (animating)
  93. network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
  94. else
  95. network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
  96. }
  97. void NetworkTrayView::UpdateConnectionStatus(bool notify_a11y) {
  98. std::u16string prev_accessible_name = accessible_name_;
  99. Shell::Get()
  100. ->system_tray_model()
  101. ->active_network_icon()
  102. ->GetConnectionStatusStrings(type_, &accessible_name_,
  103. &accessible_description_, &tooltip_);
  104. if (notify_a11y && !accessible_name_.empty() &&
  105. accessible_name_ != prev_accessible_name) {
  106. NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
  107. }
  108. }
  109. } // namespace ash