James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 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" |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 6 | |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 7 | #include "ash/resources/vector_icons/vector_icons.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 8 | #include "ash/shelf/wm_shelf.h" |
sky | 79fa3471 | 2017-03-20 23:46:47 | [diff] [blame] | 9 | #include "ash/shell.h" |
James Cook | a35a1e2 | 2017-04-08 02:33:08 | [diff] [blame] | 10 | #include "ash/system/session/logout_confirmation_controller.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 11 | #include "ash/system/status_area_widget.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 12 | #include "ash/system/tray/system_tray_controller.h" |
| 13 | #include "ash/system/tray/system_tray_notifier.h" |
| 14 | #include "ash/system/tray/tray_constants.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 15 | #include "ash/system/tray/tray_container.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 16 | #include "ash/system/user/login_status.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 17 | #include "ui/accessibility/ax_node_data.h" |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 18 | #include "ui/gfx/color_palette.h" |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 19 | #include "ui/gfx/geometry/size.h" |
| 20 | #include "ui/gfx/paint_vector_icon.h" |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 21 | #include "ui/views/controls/button/md_text_button.h" |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 22 | #include "ui/views/layout/fill_layout.h" |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 23 | |
| 24 | namespace ash { |
| 25 | |
| 26 | LogoutButtonTray::LogoutButtonTray(WmShelf* wm_shelf) |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 27 | : wm_shelf_(wm_shelf), |
| 28 | container_(new TrayContainer(wm_shelf)), |
| 29 | button_(views::MdTextButton::Create(this, base::string16())), |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 30 | show_logout_button_in_tray_(false) { |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 31 | SetLayoutManager(new views::FillLayout); |
| 32 | AddChildView(container_); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 33 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 34 | button_->SetProminent(true); |
| 35 | button_->SetBgColorOverride(gfx::kGoogleRed700); |
| 36 | button_->AdjustFontSize(kTrayTextFontSizeIncrease); |
| 37 | |
| 38 | container_->AddChildView(button_); |
sky | 5b45ed8 | 2017-03-27 02:54:23 | [diff] [blame] | 39 | Shell::Get()->system_tray_notifier()->AddLogoutButtonObserver(this); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 40 | SetVisible(false); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | LogoutButtonTray::~LogoutButtonTray() { |
sky | 5b45ed8 | 2017-03-27 02:54:23 | [diff] [blame] | 44 | Shell::Get()->system_tray_notifier()->RemoveLogoutButtonObserver(this); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 45 | } |
| 46 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 47 | void LogoutButtonTray::UpdateAfterShelfAlignmentChange() { |
| 48 | // We must first update the button so that |container_| can lay it out |
| 49 | // correctly. |
| 50 | UpdateButtonTextAndImage(); |
| 51 | container_->UpdateAfterShelfAlignmentChange(); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 52 | } |
| 53 | |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 54 | void LogoutButtonTray::ButtonPressed(views::Button* sender, |
| 55 | const ui::Event& event) { |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 56 | DCHECK_EQ(button_, sender); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 57 | |
| 58 | if (dialog_duration_ <= base::TimeDelta()) { |
| 59 | // Sign out immediately if |dialog_duration_| is non-positive. |
sky | 79fa3471 | 2017-03-20 23:46:47 | [diff] [blame] | 60 | Shell::Get()->system_tray_controller()->SignOut(); |
| 61 | } else if (Shell::Get()->logout_confirmation_controller()) { |
| 62 | Shell::Get()->logout_confirmation_controller()->ConfirmLogout( |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 63 | base::TimeTicks::Now() + dialog_duration_); |
| 64 | } |
| 65 | } |
| 66 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 67 | void LogoutButtonTray::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 68 | View::GetAccessibleNodeData(node_data); |
| 69 | node_data->SetName(button_->GetText()); |
| 70 | } |
| 71 | |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 72 | void LogoutButtonTray::OnShowLogoutButtonInTrayChanged(bool show) { |
| 73 | show_logout_button_in_tray_ = show; |
| 74 | UpdateVisibility(); |
| 75 | } |
| 76 | |
| 77 | void LogoutButtonTray::OnLogoutDialogDurationChanged(base::TimeDelta duration) { |
| 78 | dialog_duration_ = duration; |
| 79 | } |
| 80 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 81 | void LogoutButtonTray::UpdateAfterLoginStatusChange() { |
| 82 | UpdateButtonTextAndImage(); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void LogoutButtonTray::UpdateVisibility() { |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 86 | LoginStatus login_status = wm_shelf_->GetStatusAreaWidget()->login_status(); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 87 | SetVisible(show_logout_button_in_tray_ && |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 88 | login_status != LoginStatus::NOT_LOGGED_IN && |
| 89 | login_status != LoginStatus::LOCKED); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 90 | } |
| 91 | |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 92 | void LogoutButtonTray::UpdateButtonTextAndImage() { |
| 93 | LoginStatus login_status = wm_shelf_->GetStatusAreaWidget()->login_status(); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 94 | const base::string16 title = |
| 95 | user::GetLocalizedSignOutStringForStatus(login_status, false); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 96 | if (wm_shelf_->IsHorizontalAlignment()) { |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 97 | button_->SetText(title); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 98 | button_->SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 99 | button_->SetMinSize(gfx::Size(0, kTrayItemSize)); |
| 100 | } else { |
| 101 | button_->SetText(base::string16()); |
| 102 | button_->SetAccessibleName(title); |
mohsen | e6704a0 | 2017-04-20 06:30:20 | [diff] [blame^] | 103 | button_->SetImage(views::Button::STATE_NORMAL, |
James Cook | 6def4d9d | 2017-03-05 22:13:47 | [diff] [blame] | 104 | gfx::CreateVectorIcon(kShelfLogoutIcon, kTrayIconColor)); |
| 105 | button_->SetMinSize(gfx::Size(kTrayItemSize, kTrayItemSize)); |
| 106 | } |
| 107 | UpdateVisibility(); |
| 108 | } |
| 109 | |
| 110 | } // namespace ash |