[go: nahoru, domu]

blob: b32d72e1823e927038c1bbc3c1c3270dcaef4676 [file] [log] [blame]
bartfab@chromium.org7b3f91ee2014-03-06 11:41:401// Copyright 2014 The Chromium Authors. All rights reserved.
bartfab@chromium.orgc80357d2013-08-14 11:54:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
James Cooka35a1e22017-04-08 02:33:085#include "ash/system/session/logout_button_tray.h"
bartfab@chromium.orgc80357d2013-08-14 11:54:426
Mitsuru Oshima04b54d02017-10-09 14:22:457#include <memory>
8
James Cookdc041122017-08-01 14:03:229#include "ash/public/cpp/ash_pref_names.h"
taptede3880da2017-05-16 05:35:1710#include "ash/public/cpp/ash_typography.h"
estade1b92771b2017-01-10 04:51:2011#include "ash/resources/vector_icons/vector_icons.h"
Xiyuan Xiae7b19542019-05-06 23:05:1812#include "ash/session/session_controller_impl.h"
James Cook840177e2017-05-25 02:20:0113#include "ash/shelf/shelf.h"
sky79fa34712017-03-20 23:46:4714#include "ash/shell.h"
James Cooka35a1e22017-04-08 02:33:0815#include "ash/system/session/logout_confirmation_controller.h"
mohsene6704a02017-04-20 06:30:2016#include "ash/system/status_area_widget.h"
James Cookb0bf8e82017-04-09 17:01:4417#include "ash/system/tray/tray_constants.h"
mohsene6704a02017-04-20 06:30:2018#include "ash/system/tray/tray_container.h"
James Cookb0bf8e82017-04-09 17:01:4419#include "ash/system/user/login_status.h"
Sebastien Marchand6d0558fd2019-01-25 16:49:3720#include "base/bind.h"
Wenzhao Zangb06aeea2018-11-28 01:43:3521#include "base/metrics/user_metrics.h"
James Cookdc041122017-08-01 14:03:2222#include "components/prefs/pref_change_registrar.h"
23#include "components/prefs/pref_registry_simple.h"
24#include "components/prefs/pref_service.h"
mohsene6704a02017-04-20 06:30:2025#include "ui/accessibility/ax_node_data.h"
estadeab413582016-08-03 16:37:0726#include "ui/gfx/color_palette.h"
yukishiino@chromium.orgc3d71b642014-02-05 08:39:4727#include "ui/gfx/geometry/size.h"
estade71ec8bb2016-08-09 00:00:4728#include "ui/gfx/paint_vector_icon.h"
estadeab413582016-08-03 16:37:0729#include "ui/views/controls/button/md_text_button.h"
mohsene6704a02017-04-20 06:30:2030#include "ui/views/layout/fill_layout.h"
bartfab@chromium.orgc80357d2013-08-14 11:54:4231
32namespace ash {
bartfab@chromium.orgc80357d2013-08-14 11:54:4233
Yeol Parkca316072019-05-22 01:47:0234LogoutButtonTray::LogoutButtonTray(Shelf* shelf) : shelf_(shelf) {
James Cookdc041122017-08-01 14:03:2235 DCHECK(shelf);
Sam McNallyf4dab612017-08-16 03:06:3336 Shell::Get()->session_controller()->AddObserver(this);
Brett Wilson501d6642017-12-14 17:58:1837 SetLayoutManager(std::make_unique<views::FillLayout>());
Yeol Parkca316072019-05-22 01:47:0238 container_ = AddChildView(std::make_unique<TrayContainer>(shelf));
yiyixee16fc42017-01-10 05:49:2939
Yeol Parkca316072019-05-22 01:47:0240 auto button = views::MdTextButton::Create(this, base::string16(),
41 CONTEXT_LAUNCHER_BUTTON);
42 button->SetProminent(true);
43 button->SetBgColorOverride(gfx::kGoogleRed700);
mohsene6704a02017-04-20 06:30:2044
Yeol Parkca316072019-05-22 01:47:0245 button_ = container_->AddChildView(std::move(button));
mohsene6704a02017-04-20 06:30:2046 SetVisible(false);
bartfab@chromium.orgc80357d2013-08-14 11:54:4247}
48
49LogoutButtonTray::~LogoutButtonTray() {
Sam McNallyf4dab612017-08-16 03:06:3350 Shell::Get()->session_controller()->RemoveObserver(this);
James Cookdc041122017-08-01 14:03:2251}
52
53// static
54void LogoutButtonTray::RegisterProfilePrefs(PrefRegistrySimple* registry) {
Sam McNallyf4dab612017-08-16 03:06:3355 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false,
56 PrefRegistry::PUBLIC);
57 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000,
58 PrefRegistry::PUBLIC);
bartfab@chromium.orgc80357d2013-08-14 11:54:4259}
60
mohsene6704a02017-04-20 06:30:2061void LogoutButtonTray::UpdateAfterShelfAlignmentChange() {
62 // We must first update the button so that |container_| can lay it out
63 // correctly.
64 UpdateButtonTextAndImage();
65 container_->UpdateAfterShelfAlignmentChange();
bartfab@chromium.orgc80357d2013-08-14 11:54:4266}
67
bartfab@chromium.orgc80357d2013-08-14 11:54:4268void LogoutButtonTray::ButtonPressed(views::Button* sender,
69 const ui::Event& event) {
mohsene6704a02017-04-20 06:30:2070 DCHECK_EQ(button_, sender);
bruthiga80489912016-10-27 23:20:4871
bartfab@chromium.org7b3f91ee2014-03-06 11:41:4072 if (dialog_duration_ <= base::TimeDelta()) {
Wenzhao Zangb06aeea2018-11-28 01:43:3573 if (Shell::Get()->session_controller()->IsDemoSession())
74 base::RecordAction(base::UserMetricsAction("DemoMode.ExitFromShelf"));
bartfab@chromium.org7b3f91ee2014-03-06 11:41:4075 // Sign out immediately if |dialog_duration_| is non-positive.
Steven Bennetts40ab7f72017-10-04 23:32:2776 Shell::Get()->session_controller()->RequestSignOut();
sky79fa34712017-03-20 23:46:4777 } else if (Shell::Get()->logout_confirmation_controller()) {
78 Shell::Get()->logout_confirmation_controller()->ConfirmLogout(
Wenzhao Zangb06aeea2018-11-28 01:43:3579 base::TimeTicks::Now() + dialog_duration_,
80 LogoutConfirmationController::Source::kShelfExitButton);
bartfab@chromium.org7b3f91ee2014-03-06 11:41:4081 }
bartfab@chromium.orgc80357d2013-08-14 11:54:4282}
83
James Cookdc041122017-08-01 14:03:2284void LogoutButtonTray::OnActiveUserPrefServiceChanged(PrefService* prefs) {
85 pref_change_registrar_.reset();
Mitsuru Oshima04b54d02017-10-09 14:22:4586 pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
James Cookdc041122017-08-01 14:03:2287 pref_change_registrar_->Init(prefs);
88 pref_change_registrar_->Add(
89 prefs::kShowLogoutButtonInTray,
Qiang Xudadd3ad62018-03-07 17:22:4590 base::BindRepeating(&LogoutButtonTray::UpdateShowLogoutButtonInTray,
91 base::Unretained(this)));
James Cookdc041122017-08-01 14:03:2292 pref_change_registrar_->Add(
93 prefs::kLogoutDialogDurationMs,
Qiang Xudadd3ad62018-03-07 17:22:4594 base::BindRepeating(&LogoutButtonTray::UpdateLogoutDialogDuration,
95 base::Unretained(this)));
James Cookdc041122017-08-01 14:03:2296
97 // Read the initial values.
98 UpdateShowLogoutButtonInTray();
99 UpdateLogoutDialogDuration();
100}
101
mohsene6704a02017-04-20 06:30:20102void LogoutButtonTray::GetAccessibleNodeData(ui::AXNodeData* node_data) {
103 View::GetAccessibleNodeData(node_data);
104 node_data->SetName(button_->GetText());
105}
106
Andrew Lee9b38f102019-05-20 23:21:29107const char* LogoutButtonTray::GetClassName() const {
108 return "LogoutButtonTray";
109}
110
James Cookdc041122017-08-01 14:03:22111void LogoutButtonTray::UpdateShowLogoutButtonInTray() {
112 show_logout_button_in_tray_ = pref_change_registrar_->prefs()->GetBoolean(
113 prefs::kShowLogoutButtonInTray);
bruthiga80489912016-10-27 23:20:48114 UpdateVisibility();
115}
116
James Cookdc041122017-08-01 14:03:22117void LogoutButtonTray::UpdateLogoutDialogDuration() {
118 const int duration_ms = pref_change_registrar_->prefs()->GetInteger(
119 prefs::kLogoutDialogDurationMs);
120 dialog_duration_ = base::TimeDelta::FromMilliseconds(duration_ms);
bruthiga80489912016-10-27 23:20:48121}
122
mohsene6704a02017-04-20 06:30:20123void LogoutButtonTray::UpdateAfterLoginStatusChange() {
124 UpdateButtonTextAndImage();
bartfab@chromium.orgc80357d2013-08-14 11:54:42125}
126
127void LogoutButtonTray::UpdateVisibility() {
James Cook840177e2017-05-25 02:20:01128 LoginStatus login_status = shelf_->GetStatusAreaWidget()->login_status();
bartfab@chromium.orgc80357d2013-08-14 11:54:42129 SetVisible(show_logout_button_in_tray_ &&
mohsene6704a02017-04-20 06:30:20130 login_status != LoginStatus::NOT_LOGGED_IN &&
131 login_status != LoginStatus::LOCKED);
bartfab@chromium.orgc80357d2013-08-14 11:54:42132}
133
mohsene6704a02017-04-20 06:30:20134void LogoutButtonTray::UpdateButtonTextAndImage() {
James Cook840177e2017-05-25 02:20:01135 LoginStatus login_status = shelf_->GetStatusAreaWidget()->login_status();
estade71ec8bb2016-08-09 00:00:47136 const base::string16 title =
137 user::GetLocalizedSignOutStringForStatus(login_status, false);
James Cook840177e2017-05-25 02:20:01138 if (shelf_->IsHorizontalAlignment()) {
estade71ec8bb2016-08-09 00:00:47139 button_->SetText(title);
mohsene6704a02017-04-20 06:30:20140 button_->SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia());
estade3cc64b832017-02-08 02:29:42141 button_->SetMinSize(gfx::Size(0, kTrayItemSize));
estade71ec8bb2016-08-09 00:00:47142 } else {
143 button_->SetText(base::string16());
144 button_->SetAccessibleName(title);
mohsene6704a02017-04-20 06:30:20145 button_->SetImage(views::Button::STATE_NORMAL,
estade1b92771b2017-01-10 04:51:20146 gfx::CreateVectorIcon(kShelfLogoutIcon, kTrayIconColor));
estade3cc64b832017-02-08 02:29:42147 button_->SetMinSize(gfx::Size(kTrayItemSize, kTrayItemSize));
estade71ec8bb2016-08-09 00:00:47148 }
149 UpdateVisibility();
150}
151
bartfab@chromium.orgc80357d2013-08-14 11:54:42152} // namespace ash