123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- // Copyright (c) 2022 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "ash/system/channel_indicator/channel_indicator.h"
- #include "ash/resources/vector_icons/vector_icons.h"
- #include "ash/session/session_controller_impl.h"
- #include "ash/shelf/shelf.h"
- #include "ash/shell.h"
- #include "ash/system/channel_indicator/channel_indicator_utils.h"
- #include "ash/system/tray/tray_constants.h"
- #include "base/memory/weak_ptr.h"
- #include "base/strings/strcat.h"
- #include "base/strings/string_util.h"
- #include "components/session_manager/session_manager_types.h"
- #include "components/version_info/channel.h"
- #include "ui/accessibility/ax_node_data.h"
- #include "ui/base/l10n/l10n_util.h"
- #include "ui/gfx/geometry/rect.h"
- #include "ui/gfx/geometry/size.h"
- #include "ui/gfx/paint_vector_icon.h"
- #include "ui/views/background.h"
- #include "ui/views/border.h"
- #include "ui/views/controls/image_view.h"
- #include "ui/views/layout/box_layout.h"
- #include "ui/views/view.h"
- namespace ash {
- namespace {
- // Background rounded rectangle corner radius.
- constexpr int kIndicatorBgCornerRadius = 50;
- // Size of padding area around the icon or text.
- constexpr int kIndicatorInset = 8;
- } // namespace
- ChannelIndicatorView::ChannelIndicatorView(Shelf* shelf,
- version_info::Channel channel)
- : TrayItemView(shelf), channel_(channel), session_observer_(this) {
- shell_observer_.Observe(Shell::Get());
- SetVisible(false);
- Update();
- }
- ChannelIndicatorView::~ChannelIndicatorView() = default;
- void ChannelIndicatorView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
- node_data->SetName(accessible_name_);
- }
- views::View* ChannelIndicatorView::GetTooltipHandlerForPoint(
- const gfx::Point& point) {
- return GetLocalBounds().Contains(point) ? this : nullptr;
- }
- std::u16string ChannelIndicatorView::GetTooltipText(const gfx::Point& p) const {
- return tooltip_;
- }
- const char* ChannelIndicatorView::GetClassName() const {
- return "ChannelIndicatorView";
- }
- void ChannelIndicatorView::OnThemeChanged() {
- TrayItemView::OnThemeChanged();
- if (Shell::Get()->session_controller()->GetSessionState() ==
- session_manager::SessionState::ACTIVE) {
- // User is logged in, set image view colors.
- if (image_view()) {
- image_view()->SetBackground(views::CreateRoundedRectBackground(
- channel_indicator_utils::GetBgColor(channel_),
- kIndicatorBgCornerRadius));
- image_view()->SetImage(gfx::CreateVectorIcon(
- channel_indicator_utils::GetVectorIcon(channel_),
- kUnifiedTrayChannelIndicatorDimension,
- channel_indicator_utils::GetFgColor(channel_)));
- }
- return;
- }
- // User is not logged in, set label colors.
- if (label()) {
- label()->SetBackground(views::CreateRoundedRectBackground(
- channel_indicator_utils::GetBgColor(channel_),
- kIndicatorBgCornerRadius));
- label()->SetEnabledColor(channel_indicator_utils::GetFgColor(channel_));
- }
- }
- void ChannelIndicatorView::HandleLocaleChange() {
- Update();
- }
- void ChannelIndicatorView::Update() {
- if (!channel_indicator_utils::IsDisplayableChannel(channel_))
- return;
- SetImageOrText();
- SetVisible(true);
- SetAccessibleName();
- SetTooltip();
- }
- void ChannelIndicatorView::SetImageOrText() {
- DCHECK(channel_indicator_utils::IsDisplayableChannel(channel_));
- if (Shell::Get()->session_controller()->GetSessionState() ==
- session_manager::SessionState::ACTIVE) {
- // User is logged in, show the icon.
- if (image_view())
- return;
- DestroyLabel();
- CreateImageView();
- // Border insets depend on shelf horizontal alignment.
- SetBorder(views::CreateEmptyBorder(
- IsHorizontalAlignment() ? gfx::Insets::VH(kIndicatorInset, 0)
- : gfx::Insets::VH(0, kIndicatorInset)));
- image_view()->SetBackground(views::CreateRoundedRectBackground(
- channel_indicator_utils::GetBgColor(channel_),
- kIndicatorBgCornerRadius));
- image_view()->SetImage(
- gfx::CreateVectorIcon(channel_indicator_utils::GetVectorIcon(channel_),
- kUnifiedTrayChannelIndicatorDimension,
- channel_indicator_utils::GetFgColor(channel_)));
- PreferredSizeChanged();
- return;
- }
- // User is not logged in, show the channel name.
- if (label())
- return;
- DestroyImageView();
- CreateLabel();
- // Label is only displayed if the user is in a non-active `SessionState`,
- // where side-shelf isn't possible (for now at least!), so nothing here is
- // adjusted for shelf alignment.
- DCHECK(IsHorizontalAlignment());
- SetBorder(views::CreateEmptyBorder(gfx::Insets::VH(kIndicatorInset, 0)));
- label()->SetBorder(views::CreateEmptyBorder(gfx::Insets::VH(0, 6)));
- label()->SetBackground(views::CreateRoundedRectBackground(
- channel_indicator_utils::GetBgColor(channel_), kIndicatorBgCornerRadius));
- label()->SetEnabledColor(channel_indicator_utils::GetFgColor(channel_));
- label()->SetText(l10n_util::GetStringUTF16(
- channel_indicator_utils::GetChannelNameStringResourceID(
- channel_,
- /*append_channel=*/false)));
- PreferredSizeChanged();
- }
- void ChannelIndicatorView::SetAccessibleName() {
- DCHECK(channel_indicator_utils::IsDisplayableChannel(channel_));
- accessible_name_ = l10n_util::GetStringUTF16(
- channel_indicator_utils::GetChannelNameStringResourceID(
- channel_, /*append_channel=*/true));
- // If icon is showing, set it on the image view.
- if (image_view()) {
- DCHECK(!label());
- image_view()->SetAccessibleName(accessible_name_);
- return;
- }
- // Otherwise set it on the label.
- if (label())
- label()->SetAccessibleName(accessible_name_);
- }
- void ChannelIndicatorView::SetTooltip() {
- DCHECK(channel_indicator_utils::IsDisplayableChannel(channel_));
- tooltip_ = l10n_util::GetStringUTF16(
- channel_indicator_utils::GetChannelNameStringResourceID(
- channel_, /*append_channel=*/true));
- }
- void ChannelIndicatorView::OnSessionStateChanged(
- session_manager::SessionState state) {
- Update();
- }
- void ChannelIndicatorView::OnShelfAlignmentChanged(
- aura::Window* root_window,
- ShelfAlignment old_alignment) {
- if (image_view()) {
- // Border insets depend on shelf horizontal alignment.
- SetBorder(views::CreateEmptyBorder(
- IsHorizontalAlignment() ? gfx::Insets::VH(kIndicatorInset, 0)
- : gfx::Insets::VH(0, kIndicatorInset)));
- }
- }
- bool ChannelIndicatorView::IsLabelVisibleForTesting() {
- return label() && label()->GetVisible();
- }
- bool ChannelIndicatorView::IsImageViewVisibleForTesting() {
- return image_view() && image_view()->GetVisible();
- }
- std::u16string ChannelIndicatorView::GetAccessibleNameString() const {
- if (image_view())
- return image_view()->GetAccessibleName();
- if (label())
- return label()->GetAccessibleName();
- return base::EmptyString16();
- }
- } // namespace ash
|