bartfab@chromium.org | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
James Cook | a35a1e2 | 2017-04-08 02:33:08 | [diff] [blame] | 5 | #include "ash/system/session/logout_button_tray.h" |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 6 | |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 9 | #include "ash/public/cpp/ash_pref_names.h" |
tapted | e3880da | 2017-05-16 05:35:17 | [diff] [blame] | 10 | #include "ash/public/cpp/ash_typography.h" |
estade | 1b92771b | 2017-01-10 04:51:20 | [diff] [blame] | 11 | #include "ash/resources/vector_icons/vector_icons.h" |
Xiyuan Xia | e7b1954 | 2019-05-06 23:05:18 | [diff] [blame] | 12 | #include "ash/session/session_controller_impl.h" |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 13 | #include "ash/shelf/shelf.h" |
sky | 79fa3471 | 2017-03-20 23:46:47 | [diff] [blame] | 14 | #include "ash/shell.h" |
minch | bee2e31 | 2019-08-15 21:52:40 | [diff] [blame] | 15 | #include "ash/style/ash_color_provider.h" |
James Cook | a35a1e2 | 2017-04-08 02:33:08 | [diff] [blame] | 16 | #include "ash/system/session/logout_confirmation_controller.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 17 | #include "ash/system/status_area_widget.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 18 | #include "ash/system/tray/tray_constants.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 19 | #include "ash/system/tray/tray_container.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 20 | #include "ash/system/user/login_status.h" |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 21 | #include "base/bind.h" |
Wenzhao Zang | b06aeea | 2018-11-28 01:43:35 | [diff] [blame] | 22 | #include "base/metrics/user_metrics.h" |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 23 | #include "components/prefs/pref_change_registrar.h" |
| 24 | #include "components/prefs/pref_registry_simple.h" |
| 25 | #include "components/prefs/pref_service.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 26 | #include "ui/accessibility/ax_node_data.h" |
yukishiino@chromium.org | c3d71b64 | 2014-02-05 08:39:47 | [diff] [blame] | 27 | #include "ui/gfx/geometry/size.h" |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 28 | #include "ui/gfx/paint_vector_icon.h" |
estade | ab41358 | 2016-08-03 16:37:07 | [diff] [blame] | 29 | #include "ui/views/controls/button/md_text_button.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 30 | #include "ui/views/layout/fill_layout.h" |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 31 | |
| 32 | namespace ash { |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 33 | |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 34 | LogoutButtonTray::LogoutButtonTray(Shelf* shelf) : TrayBackgroundView(shelf) { |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 35 | DCHECK(shelf); |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 36 | Shell::Get()->session_controller()->AddObserver(this); |
yiyix | ee16fc4 | 2017-01-10 05:49:29 | [diff] [blame] | 37 | |
minch | 72c4610 | 2020-11-16 21:48:41 | [diff] [blame] | 38 | button_ = |
| 39 | tray_container()->AddChildView(std::make_unique<views::MdTextButton>( |
| 40 | base::BindRepeating(&LogoutButtonTray::ButtonPressed, |
| 41 | base::Unretained(this)), |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 42 | std::u16string(), CONTEXT_LAUNCHER_BUTTON)); |
minch | 72c4610 | 2020-11-16 21:48:41 | [diff] [blame] | 43 | button_->SetProminent(true); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | LogoutButtonTray::~LogoutButtonTray() { |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 47 | Shell::Get()->session_controller()->RemoveObserver(this); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // static |
| 51 | void LogoutButtonTray::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
James Cook | 277f643 | 2019-10-18 22:39:33 | [diff] [blame] | 52 | registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); |
| 53 | registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 54 | } |
| 55 | |
Manu Cornet | 26988c5 | 2020-02-27 21:40:16 | [diff] [blame] | 56 | void LogoutButtonTray::UpdateLayout() { |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 57 | // We must first update the button so that its container can lay it out |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 58 | // correctly. |
| 59 | UpdateButtonTextAndImage(); |
Manu Cornet | 26988c5 | 2020-02-27 21:40:16 | [diff] [blame] | 60 | tray_container()->UpdateLayout(); |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void LogoutButtonTray::UpdateBackground() { |
| 64 | // The logout button does not have a background. |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 65 | } |
| 66 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 67 | void LogoutButtonTray::OnActiveUserPrefServiceChanged(PrefService* prefs) { |
| 68 | pref_change_registrar_.reset(); |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 69 | pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>(); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 70 | pref_change_registrar_->Init(prefs); |
| 71 | pref_change_registrar_->Add( |
| 72 | prefs::kShowLogoutButtonInTray, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 73 | base::BindRepeating(&LogoutButtonTray::UpdateShowLogoutButtonInTray, |
| 74 | base::Unretained(this))); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 75 | pref_change_registrar_->Add( |
| 76 | prefs::kLogoutDialogDurationMs, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 77 | base::BindRepeating(&LogoutButtonTray::UpdateLogoutDialogDuration, |
| 78 | base::Unretained(this))); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 79 | |
| 80 | // Read the initial values. |
| 81 | UpdateShowLogoutButtonInTray(); |
| 82 | UpdateLogoutDialogDuration(); |
| 83 | } |
| 84 | |
Andrew Lee | 9b38f10 | 2019-05-20 23:21:29 | [diff] [blame] | 85 | const char* LogoutButtonTray::GetClassName() const { |
| 86 | return "LogoutButtonTray"; |
| 87 | } |
| 88 | |
minch | 72c4610 | 2020-11-16 21:48:41 | [diff] [blame] | 89 | void LogoutButtonTray::OnThemeChanged() { |
| 90 | TrayBackgroundView::OnThemeChanged(); |
| 91 | button_->SetBgColorOverride(AshColorProvider::Get()->GetControlsLayerColor( |
| 92 | AshColorProvider::ControlsLayerType::kControlBackgroundColorAlert)); |
| 93 | } |
| 94 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 95 | void LogoutButtonTray::UpdateShowLogoutButtonInTray() { |
| 96 | show_logout_button_in_tray_ = pref_change_registrar_->prefs()->GetBoolean( |
| 97 | prefs::kShowLogoutButtonInTray); |
bruthig | a8048991 | 2016-10-27 23:20:48 | [diff] [blame] | 98 | UpdateVisibility(); |
| 99 | } |
| 100 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 101 | void LogoutButtonTray::UpdateLogoutDialogDuration() { |
| 102 | const int duration_ms = pref_change_registrar_->prefs()->GetInteger( |
| 103 | prefs::kLogoutDialogDurationMs); |
| 104 | dialog_duration_ = base::TimeDelta::FromMilliseconds(duration_ms); |
bruthig | a8048991 | 2016-10-27 23:20:48 | [diff] [blame] | 105 | } |
| 106 | |
Manu Cornet | 3e74a3c | 2020-02-24 22:43:17 | [diff] [blame] | 107 | void LogoutButtonTray::UpdateAfterLoginStatusChange() { |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 108 | UpdateButtonTextAndImage(); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 109 | } |
| 110 | |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 111 | void LogoutButtonTray::ClickedOutsideBubble() {} |
| 112 | |
| 113 | void LogoutButtonTray::HideBubbleWithView(const TrayBubbleView* bubble_view) {} |
| 114 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 115 | std::u16string LogoutButtonTray::GetAccessibleNameForTray() { |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 116 | return button_->GetText(); |
| 117 | } |
| 118 | |
Toni Barzic | e4627b85 | 2020-06-04 02:22:49 | [diff] [blame] | 119 | void LogoutButtonTray::HandleLocaleChange() { |
| 120 | UpdateButtonTextAndImage(); |
| 121 | } |
| 122 | |
yilkal | 4ae1c615 | 2019-06-28 21:37:53 | [diff] [blame] | 123 | void LogoutButtonTray::UpdateVisibility() { |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 124 | LoginStatus login_status = shelf()->GetStatusAreaWidget()->login_status(); |
| 125 | SetVisiblePreferred(show_logout_button_in_tray_ && |
| 126 | login_status != LoginStatus::NOT_LOGGED_IN && |
| 127 | login_status != LoginStatus::LOCKED); |
yilkal | 4ae1c615 | 2019-06-28 21:37:53 | [diff] [blame] | 128 | } |
| 129 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 130 | void LogoutButtonTray::UpdateButtonTextAndImage() { |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 131 | LoginStatus login_status = shelf()->GetStatusAreaWidget()->login_status(); |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 132 | const std::u16string title = |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 133 | user::GetLocalizedSignOutStringForStatus(login_status, false); |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 134 | if (shelf()->IsHorizontalAlignment()) { |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 135 | button_->SetText(title); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 136 | button_->SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
estade | 3cc64b83 | 2017-02-08 02:29:42 | [diff] [blame] | 137 | button_->SetMinSize(gfx::Size(0, kTrayItemSize)); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 138 | } else { |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 139 | button_->SetText(std::u16string()); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 140 | button_->SetAccessibleName(title); |
Xiaoqian Dai | f125208 | 2020-06-16 23:13:36 | [diff] [blame] | 141 | button_->SetImage( |
| 142 | views::Button::STATE_NORMAL, |
| 143 | gfx::CreateVectorIcon( |
| 144 | kShelfLogoutIcon, |
| 145 | AshColorProvider::Get()->GetContentLayerColor( |
minch | 5847c6f | 2020-09-08 01:13:17 | [diff] [blame] | 146 | AshColorProvider::ContentLayerType::kIconColorPrimary))); |
estade | 3cc64b83 | 2017-02-08 02:29:42 | [diff] [blame] | 147 | button_->SetMinSize(gfx::Size(kTrayItemSize, kTrayItemSize)); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 148 | } |
| 149 | UpdateVisibility(); |
| 150 | } |
| 151 | |
Peter Kasting | 256b947 | 2020-10-31 03:50:53 | [diff] [blame] | 152 | void LogoutButtonTray::ButtonPressed() { |
| 153 | if (dialog_duration_ <= base::TimeDelta()) { |
| 154 | if (Shell::Get()->session_controller()->IsDemoSession()) |
| 155 | base::RecordAction(base::UserMetricsAction("DemoMode.ExitFromShelf")); |
| 156 | // Sign out immediately if |dialog_duration_| is non-positive. |
| 157 | Shell::Get()->session_controller()->RequestSignOut(); |
| 158 | } else if (Shell::Get()->logout_confirmation_controller()) { |
| 159 | Shell::Get()->logout_confirmation_controller()->ConfirmLogout( |
| 160 | base::TimeTicks::Now() + dialog_duration_, |
| 161 | LogoutConfirmationController::Source::kShelfExitButton); |
| 162 | } |
| 163 | } |
| 164 | |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 165 | } // namespace ash |