1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/session/logout_button_tray.h"
#include <memory>
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/tray_background_view_catalog.h"
#include "ash/public/cpp/ash_typography.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/style/ash_color_provider.h"
#include "ash/system/session/logout_confirmation_controller.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_container.h"
#include "ash/system/user/login_status.h"
#include "base/functional/bind.h"
#include "base/metrics/user_metrics.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/layout/fill_layout.h"
namespace ash {
LogoutButtonTray::LogoutButtonTray(Shelf* shelf)
: TrayBackgroundView(shelf, TrayBackgroundViewCatalogName::kLogoutButton) {
DCHECK(shelf);
Shell::Get()->session_controller()->AddObserver(this);
button_ =
tray_container()->AddChildView(std::make_unique<views::MdTextButton>(
base::BindRepeating(&LogoutButtonTray::ButtonPressed,
base::Unretained(this)),
std::u16string(), CONTEXT_LAUNCHER_BUTTON));
button_->SetStyle(ui::ButtonStyle::kProminent);
set_use_bounce_in_animation(false);
SetFocusBehavior(FocusBehavior::NEVER);
SubscribeCallbacksForAccessibility();
}
LogoutButtonTray::~LogoutButtonTray() {
Shell::Get()->session_controller()->RemoveObserver(this);
}
// static
void LogoutButtonTray::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false);
registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000);
}
void LogoutButtonTray::UpdateLayout() {
// We must first update the button so that its container can lay it out
// correctly.
UpdateButtonTextAndImage();
tray_container()->UpdateLayout();
}
void LogoutButtonTray::UpdateBackground() {
// The logout button does not have a background.
}
void LogoutButtonTray::OnActiveUserPrefServiceChanged(PrefService* prefs) {
pref_change_registrar_.reset();
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(prefs);
pref_change_registrar_->Add(
prefs::kShowLogoutButtonInTray,
base::BindRepeating(&LogoutButtonTray::UpdateShowLogoutButtonInTray,
base::Unretained(this)));
pref_change_registrar_->Add(
prefs::kLogoutDialogDurationMs,
base::BindRepeating(&LogoutButtonTray::UpdateLogoutDialogDuration,
base::Unretained(this)));
// Read the initial values.
UpdateShowLogoutButtonInTray();
UpdateLogoutDialogDuration();
}
std::u16string LogoutButtonTray::GetLoginStatusString() {
LoginStatus login_status = shelf()->GetStatusAreaWidget()->login_status();
return user::GetLocalizedSignOutStringForStatus(login_status, false);
}
void LogoutButtonTray::OnThemeChanged() {
TrayBackgroundView::OnThemeChanged();
const auto* color_provider = GetColorProvider();
button_->SetBgColorIdOverride(cros_tokens::kColorAlert);
button_->SetEnabledTextColors(
color_provider->GetColor(cros_tokens::kColorPrimaryInverted));
}
void LogoutButtonTray::UpdateShowLogoutButtonInTray() {
show_logout_button_in_tray_ = pref_change_registrar_->prefs()->GetBoolean(
prefs::kShowLogoutButtonInTray);
UpdateVisibility();
}
void LogoutButtonTray::UpdateLogoutDialogDuration() {
const int duration_ms = pref_change_registrar_->prefs()->GetInteger(
prefs::kLogoutDialogDurationMs);
dialog_duration_ = base::Milliseconds(duration_ms);
}
void LogoutButtonTray::UpdateAfterLoginStatusChange() {
UpdateButtonTextAndImage();
}
void LogoutButtonTray::ClickedOutsideBubble(const ui::LocatedEvent& event) {}
void LogoutButtonTray::HideBubbleWithView(const TrayBubbleView* bubble_view) {}
void LogoutButtonTray::HideBubble(const TrayBubbleView* bubble_view) {}
void LogoutButtonTray::HandleLocaleChange() {
UpdateButtonTextAndImage();
}
void LogoutButtonTray::UpdateVisibility() {
LoginStatus login_status = shelf()->GetStatusAreaWidget()->login_status();
SetVisiblePreferred(show_logout_button_in_tray_ &&
login_status != LoginStatus::NOT_LOGGED_IN &&
login_status != LoginStatus::LOCKED);
}
void LogoutButtonTray::UpdateButtonTextAndImage() {
if (shelf()->IsHorizontalAlignment()) {
button_->SetText(GetLoginStatusString());
button_->SetImageModel(views::Button::STATE_NORMAL, ui::ImageModel());
button_->SetMinSize(gfx::Size(0, kTrayItemSize));
} else {
button_->SetText(std::u16string());
button_->GetViewAccessibility().SetName(GetLoginStatusString());
button_->SetImageModel(
views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(
kShelfLogoutIcon,
AshColorProvider::Get()->GetContentLayerColor(
AshColorProvider::ContentLayerType::kIconColorPrimary)));
button_->SetMinSize(gfx::Size(kTrayItemSize, kTrayItemSize));
}
UpdateVisibility();
}
void LogoutButtonTray::OnButtonTextChangedCallback(
ax::mojom::StringAttribute attribute,
const std::optional<std::string>& name) {
if (name.has_value()) {
GetViewAccessibility().SetName(name.value());
}
}
void LogoutButtonTray::SubscribeCallbacksForAccessibility() {
button_text_changed_subscription_ =
button_->GetViewAccessibility().AddStringAttributeChangedCallback(
ax::mojom::StringAttribute::kName,
base::BindRepeating(&LogoutButtonTray::OnButtonTextChangedCallback,
base::Unretained(this)));
}
void LogoutButtonTray::ButtonPressed() {
if (dialog_duration_ <= base::TimeDelta()) {
if (Shell::Get()->session_controller()->IsDemoSession())
base::RecordAction(base::UserMetricsAction("DemoMode.ExitFromShelf"));
// Sign out immediately if |dialog_duration_| is non-positive.
Shell::Get()->session_controller()->RequestSignOut();
} else if (Shell::Get()->logout_confirmation_controller()) {
Shell::Get()->logout_confirmation_controller()->ConfirmLogout(
base::TimeTicks::Now() + dialog_duration_,
LogoutConfirmationController::Source::kShelfExitButton);
}
}
BEGIN_METADATA(LogoutButtonTray)
END_METADATA
} // namespace ash
|