Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / ash / system / date / date_default_view.cc
blobca64a1efa5352955cf799a32668a6e888eacb179
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.
5 #include "ash/system/date/date_default_view.h"
7 #include "ash/metrics/user_metrics_recorder.h"
8 #include "ash/session/session_state_delegate.h"
9 #include "ash/shell.h"
10 #include "ash/system/date/date_view.h"
11 #include "ash/system/tray/special_popup_row.h"
12 #include "ash/system/tray/system_tray_delegate.h"
13 #include "ash/system/tray/tray_constants.h"
14 #include "ash/system/tray/tray_popup_header_button.h"
15 #include "ash/wm/lock_state_controller.h"
16 #include "grit/ash_resources.h"
17 #include "grit/ash_strings.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/views/border.h"
20 #include "ui/views/controls/button/button.h"
21 #include "ui/views/layout/fill_layout.h"
22 #include "ui/views/view.h"
24 namespace {
26 const int kPaddingVertical = 19;
28 } // namespace
30 namespace ash {
32 DateDefaultView::DateDefaultView(ash::user::LoginStatus login)
33 : help_button_(NULL),
34 shutdown_button_(NULL),
35 lock_button_(NULL),
36 date_view_(NULL),
37 weak_factory_(this) {
38 SetLayoutManager(new views::FillLayout);
40 date_view_ = new tray::DateView();
41 date_view_->SetBorder(views::Border::CreateEmptyBorder(
42 kPaddingVertical, ash::kTrayPopupPaddingHorizontal, 0, 0));
43 SpecialPopupRow* view = new SpecialPopupRow();
44 view->SetContent(date_view_);
45 AddChildView(view);
47 bool userAddingRunning = ash::Shell::GetInstance()
48 ->session_state_delegate()
49 ->IsInSecondaryLoginScreen();
51 if (login == user::LOGGED_IN_LOCKED ||
52 login == user::LOGGED_IN_NONE || userAddingRunning)
53 return;
55 date_view_->SetAction(TrayDate::SHOW_DATE_SETTINGS);
57 help_button_ = new TrayPopupHeaderButton(
58 this, IDR_AURA_UBER_TRAY_HELP, IDR_AURA_UBER_TRAY_HELP,
59 IDR_AURA_UBER_TRAY_HELP_HOVER, IDR_AURA_UBER_TRAY_HELP_HOVER,
60 IDS_ASH_STATUS_TRAY_HELP);
61 help_button_->SetTooltipText(
62 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_HELP));
63 view->AddButton(help_button_);
65 #if !defined(OS_WIN)
66 if (login != ash::user::LOGGED_IN_LOCKED) {
67 shutdown_button_ = new TrayPopupHeaderButton(
68 this, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN,
69 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
70 IDS_ASH_STATUS_TRAY_SHUTDOWN);
71 shutdown_button_->SetTooltipText(
72 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
73 view->AddButton(shutdown_button_);
76 if (ash::Shell::GetInstance()->session_state_delegate()->CanLockScreen()) {
77 lock_button_ = new TrayPopupHeaderButton(
78 this, IDR_AURA_UBER_TRAY_LOCKSCREEN, IDR_AURA_UBER_TRAY_LOCKSCREEN,
79 IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
80 IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER, IDS_ASH_STATUS_TRAY_LOCK);
81 lock_button_->SetTooltipText(
82 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOCK));
83 view->AddButton(lock_button_);
85 SystemTrayDelegate* system_tray_delegate =
86 Shell::GetInstance()->system_tray_delegate();
87 system_tray_delegate->AddShutdownPolicyObserver(this);
88 system_tray_delegate->ShouldRebootOnShutdown(base::Bind(
89 &DateDefaultView::OnShutdownPolicyChanged, weak_factory_.GetWeakPtr()));
90 #endif // !defined(OS_WIN)
93 DateDefaultView::~DateDefaultView() {
94 // We need the check as on shell destruction, the delegate is destroyed first.
95 SystemTrayDelegate* system_tray_delegate =
96 Shell::GetInstance()->system_tray_delegate();
97 if (system_tray_delegate)
98 system_tray_delegate->RemoveShutdownPolicyObserver(this);
101 views::View* DateDefaultView::GetHelpButtonView() {
102 return help_button_;
105 const views::View* DateDefaultView::GetShutdownButtonViewForTest() const {
106 return shutdown_button_;
109 tray::DateView* DateDefaultView::GetDateView() {
110 return date_view_;
113 const tray::DateView* DateDefaultView::GetDateView() const {
114 return date_view_;
117 void DateDefaultView::ButtonPressed(views::Button* sender,
118 const ui::Event& event) {
119 ash::Shell* shell = ash::Shell::GetInstance();
120 ash::SystemTrayDelegate* tray_delegate = shell->system_tray_delegate();
121 if (sender == help_button_) {
122 shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_HELP);
123 tray_delegate->ShowHelp();
124 } else if (sender == shutdown_button_) {
125 shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_SHUT_DOWN);
126 ash::Shell::GetInstance()->lock_state_controller()->RequestShutdown();
127 } else if (sender == lock_button_) {
128 shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_LOCK_SCREEN);
129 tray_delegate->RequestLockScreen();
130 } else {
131 NOTREACHED();
135 void DateDefaultView::OnShutdownPolicyChanged(bool reboot_on_shutdown) {
136 if (!shutdown_button_)
137 return;
139 shutdown_button_->SetTooltipText(l10n_util::GetStringUTF16(
140 reboot_on_shutdown ? IDS_ASH_STATUS_TRAY_REBOOT
141 : IDS_ASH_STATUS_TRAY_SHUTDOWN));
144 } // namespace ash