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" |
estade | ab41358 | 2016-08-03 16:37:07 | [diff] [blame] | 27 | #include "ui/gfx/color_palette.h" |
yukishiino@chromium.org | c3d71b64 | 2014-02-05 08:39:47 | [diff] [blame] | 28 | #include "ui/gfx/geometry/size.h" |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 29 | #include "ui/gfx/paint_vector_icon.h" |
estade | ab41358 | 2016-08-03 16:37:07 | [diff] [blame] | 30 | #include "ui/views/controls/button/md_text_button.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 31 | #include "ui/views/layout/fill_layout.h" |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 32 | |
| 33 | namespace ash { |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 34 | |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 35 | LogoutButtonTray::LogoutButtonTray(Shelf* shelf) : TrayBackgroundView(shelf) { |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 36 | DCHECK(shelf); |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 37 | Shell::Get()->session_controller()->AddObserver(this); |
yiyix | ee16fc4 | 2017-01-10 05:49:29 | [diff] [blame] | 38 | |
Yeol Park | ca31607 | 2019-05-22 01:47:02 | [diff] [blame] | 39 | auto button = views::MdTextButton::Create(this, base::string16(), |
| 40 | CONTEXT_LAUNCHER_BUTTON); |
| 41 | button->SetProminent(true); |
Peter Kasting | 440758b | 2019-11-26 03:19:52 | [diff] [blame] | 42 | button->SetBgColorOverride(AshColorProvider::Get()->GetBaseLayerColor( |
| 43 | AshColorProvider::BaseLayerType::kRed, |
| 44 | AshColorProvider::AshColorMode::kDark)); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 45 | |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 46 | button_ = tray_container()->AddChildView(std::move(button)); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | LogoutButtonTray::~LogoutButtonTray() { |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 50 | Shell::Get()->session_controller()->RemoveObserver(this); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // static |
| 54 | void LogoutButtonTray::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
James Cook | 277f643 | 2019-10-18 22:39:33 | [diff] [blame] | 55 | registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); |
| 56 | registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 57 | } |
| 58 | |
Manu Cornet | 26988c5 | 2020-02-27 21:40:16 | [diff] [blame] | 59 | void LogoutButtonTray::UpdateLayout() { |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 60 | // We must first update the button so that its container can lay it out |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 61 | // correctly. |
| 62 | UpdateButtonTextAndImage(); |
Manu Cornet | 26988c5 | 2020-02-27 21:40:16 | [diff] [blame] | 63 | tray_container()->UpdateLayout(); |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void LogoutButtonTray::UpdateBackground() { |
| 67 | // The logout button does not have a background. |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 68 | } |
| 69 | |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 70 | void LogoutButtonTray::ButtonPressed(views::Button* sender, |
| 71 | const ui::Event& event) { |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 72 | DCHECK_EQ(button_, sender); |
bruthig | a8048991 | 2016-10-27 23:20:48 | [diff] [blame] | 73 | |
bartfab@chromium.org | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 74 | if (dialog_duration_ <= base::TimeDelta()) { |
Wenzhao Zang | b06aeea | 2018-11-28 01:43:35 | [diff] [blame] | 75 | if (Shell::Get()->session_controller()->IsDemoSession()) |
| 76 | base::RecordAction(base::UserMetricsAction("DemoMode.ExitFromShelf")); |
bartfab@chromium.org | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 77 | // Sign out immediately if |dialog_duration_| is non-positive. |
Steven Bennetts | 40ab7f7 | 2017-10-04 23:32:27 | [diff] [blame] | 78 | Shell::Get()->session_controller()->RequestSignOut(); |
sky | 79fa3471 | 2017-03-20 23:46:47 | [diff] [blame] | 79 | } else if (Shell::Get()->logout_confirmation_controller()) { |
| 80 | Shell::Get()->logout_confirmation_controller()->ConfirmLogout( |
Wenzhao Zang | b06aeea | 2018-11-28 01:43:35 | [diff] [blame] | 81 | base::TimeTicks::Now() + dialog_duration_, |
| 82 | LogoutConfirmationController::Source::kShelfExitButton); |
bartfab@chromium.org | 7b3f91ee | 2014-03-06 11:41:40 | [diff] [blame] | 83 | } |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 84 | } |
| 85 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 86 | void LogoutButtonTray::OnActiveUserPrefServiceChanged(PrefService* prefs) { |
| 87 | pref_change_registrar_.reset(); |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 88 | pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>(); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 89 | pref_change_registrar_->Init(prefs); |
| 90 | pref_change_registrar_->Add( |
| 91 | prefs::kShowLogoutButtonInTray, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 92 | base::BindRepeating(&LogoutButtonTray::UpdateShowLogoutButtonInTray, |
| 93 | base::Unretained(this))); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 94 | pref_change_registrar_->Add( |
| 95 | prefs::kLogoutDialogDurationMs, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 96 | base::BindRepeating(&LogoutButtonTray::UpdateLogoutDialogDuration, |
| 97 | base::Unretained(this))); |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 98 | |
| 99 | // Read the initial values. |
| 100 | UpdateShowLogoutButtonInTray(); |
| 101 | UpdateLogoutDialogDuration(); |
| 102 | } |
| 103 | |
Andrew Lee | 9b38f10 | 2019-05-20 23:21:29 | [diff] [blame] | 104 | const char* LogoutButtonTray::GetClassName() const { |
| 105 | return "LogoutButtonTray"; |
| 106 | } |
| 107 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 108 | void LogoutButtonTray::UpdateShowLogoutButtonInTray() { |
| 109 | show_logout_button_in_tray_ = pref_change_registrar_->prefs()->GetBoolean( |
| 110 | prefs::kShowLogoutButtonInTray); |
bruthig | a8048991 | 2016-10-27 23:20:48 | [diff] [blame] | 111 | UpdateVisibility(); |
| 112 | } |
| 113 | |
James Cook | dc04112 | 2017-08-01 14:03:22 | [diff] [blame] | 114 | void LogoutButtonTray::UpdateLogoutDialogDuration() { |
| 115 | const int duration_ms = pref_change_registrar_->prefs()->GetInteger( |
| 116 | prefs::kLogoutDialogDurationMs); |
| 117 | dialog_duration_ = base::TimeDelta::FromMilliseconds(duration_ms); |
bruthig | a8048991 | 2016-10-27 23:20:48 | [diff] [blame] | 118 | } |
| 119 | |
Manu Cornet | 3e74a3c | 2020-02-24 22:43:17 | [diff] [blame] | 120 | void LogoutButtonTray::UpdateAfterLoginStatusChange() { |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 121 | UpdateButtonTextAndImage(); |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 122 | } |
| 123 | |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 124 | void LogoutButtonTray::ClickedOutsideBubble() {} |
| 125 | |
| 126 | void LogoutButtonTray::HideBubbleWithView(const TrayBubbleView* bubble_view) {} |
| 127 | |
| 128 | base::string16 LogoutButtonTray::GetAccessibleNameForTray() { |
| 129 | return button_->GetText(); |
| 130 | } |
| 131 | |
Toni Barzic | e4627b85 | 2020-06-04 02:22:49 | [diff] [blame^] | 132 | void LogoutButtonTray::HandleLocaleChange() { |
| 133 | UpdateButtonTextAndImage(); |
| 134 | } |
| 135 | |
yilkal | 4ae1c615 | 2019-06-28 21:37:53 | [diff] [blame] | 136 | void LogoutButtonTray::UpdateVisibility() { |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 137 | LoginStatus login_status = shelf()->GetStatusAreaWidget()->login_status(); |
| 138 | SetVisiblePreferred(show_logout_button_in_tray_ && |
| 139 | login_status != LoginStatus::NOT_LOGGED_IN && |
| 140 | login_status != LoginStatus::LOCKED); |
yilkal | 4ae1c615 | 2019-06-28 21:37:53 | [diff] [blame] | 141 | } |
| 142 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 143 | void LogoutButtonTray::UpdateButtonTextAndImage() { |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 144 | LoginStatus login_status = shelf()->GetStatusAreaWidget()->login_status(); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 145 | const base::string16 title = |
| 146 | user::GetLocalizedSignOutStringForStatus(login_status, false); |
Tim Song | 23378f3 | 2019-11-21 06:07:07 | [diff] [blame] | 147 | if (shelf()->IsHorizontalAlignment()) { |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 148 | button_->SetText(title); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame] | 149 | button_->SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
estade | 3cc64b83 | 2017-02-08 02:29:42 | [diff] [blame] | 150 | button_->SetMinSize(gfx::Size(0, kTrayItemSize)); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 151 | } else { |
| 152 | button_->SetText(base::string16()); |
| 153 | button_->SetAccessibleName(title); |
minch | bee2e31 | 2019-08-15 21:52:40 | [diff] [blame] | 154 | button_->SetImage(views::Button::STATE_NORMAL, |
| 155 | gfx::CreateVectorIcon( |
| 156 | kShelfLogoutIcon, |
| 157 | AshColorProvider::Get()->GetContentLayerColor( |
| 158 | AshColorProvider::ContentLayerType::kIconPrimary, |
| 159 | AshColorProvider::AshColorMode::kDark))); |
estade | 3cc64b83 | 2017-02-08 02:29:42 | [diff] [blame] | 160 | button_->SetMinSize(gfx::Size(kTrayItemSize, kTrayItemSize)); |
estade | 71ec8bb | 2016-08-09 00:00:47 | [diff] [blame] | 161 | } |
| 162 | UpdateVisibility(); |
| 163 | } |
| 164 | |
bartfab@chromium.org | c80357d | 2013-08-14 11:54:42 | [diff] [blame] | 165 | } // namespace ash |