1 // Copyright (c) 2012 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/tray_date.h"
7 #include "ash/metrics/user_metrics_recorder.h"
8 #include "ash/session_state_delegate.h"
10 #include "ash/shell_delegate.h"
11 #include "ash/system/date/date_view.h"
12 #include "ash/system/tray/system_tray.h"
13 #include "ash/system/tray/system_tray_delegate.h"
14 #include "ash/system/tray/system_tray_notifier.h"
15 #include "ash/system/tray/tray_constants.h"
16 #include "ash/system/tray/tray_item_view.h"
17 #include "ash/system/tray/tray_popup_header_button.h"
18 #include "base/i18n/time_formatting.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "base/timer/timer.h"
23 #include "grit/ash_resources.h"
24 #include "grit/ash_strings.h"
25 #include "third_party/icu/source/i18n/unicode/datefmt.h"
26 #include "third_party/icu/source/i18n/unicode/fieldpos.h"
27 #include "third_party/icu/source/i18n/unicode/fmtable.h"
28 #include "third_party/skia/include/core/SkRect.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/gfx/image/image.h"
32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/gfx/size.h"
34 #include "ui/views/controls/button/button.h"
35 #include "ui/views/controls/image_view.h"
36 #include "ui/views/controls/label.h"
37 #include "ui/views/layout/box_layout.h"
38 #include "ui/views/layout/fill_layout.h"
39 #include "ui/views/painter.h"
40 #include "ui/views/view.h"
41 #include "ui/views/widget/widget.h"
43 #if defined(OS_CHROMEOS)
44 #include "ash/system/chromeos/system_clock_observer.h"
49 const int kPaddingVertical = 19;
56 class DateDefaultView : public views::View,
57 public views::ButtonListener {
59 explicit DateDefaultView(ash::user::LoginStatus login)
64 SetLayoutManager(new views::FillLayout);
66 date_view_ = new tray::DateView();
67 date_view_->SetBorder(views::Border::CreateEmptyBorder(
68 kPaddingVertical, ash::kTrayPopupPaddingHorizontal, 0, 0));
69 SpecialPopupRow* view = new SpecialPopupRow();
70 view->SetContent(date_view_);
73 if (login == ash::user::LOGGED_IN_LOCKED ||
74 login == ash::user::LOGGED_IN_NONE)
77 date_view_->SetActionable(true);
79 help_ = new TrayPopupHeaderButton(this,
80 IDR_AURA_UBER_TRAY_HELP,
81 IDR_AURA_UBER_TRAY_HELP,
82 IDR_AURA_UBER_TRAY_HELP_HOVER,
83 IDR_AURA_UBER_TRAY_HELP_HOVER,
84 IDS_ASH_STATUS_TRAY_HELP);
85 help_->SetTooltipText(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_HELP));
86 view->AddButton(help_);
89 if (login != ash::user::LOGGED_IN_LOCKED &&
90 login != ash::user::LOGGED_IN_RETAIL_MODE) {
91 shutdown_ = new TrayPopupHeaderButton(this,
92 IDR_AURA_UBER_TRAY_SHUTDOWN,
93 IDR_AURA_UBER_TRAY_SHUTDOWN,
94 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
95 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
96 IDS_ASH_STATUS_TRAY_SHUTDOWN);
97 shutdown_->SetTooltipText(
98 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
99 view->AddButton(shutdown_);
102 if (ash::Shell::GetInstance()->session_state_delegate()->CanLockScreen()) {
103 lock_ = new TrayPopupHeaderButton(this,
104 IDR_AURA_UBER_TRAY_LOCKSCREEN,
105 IDR_AURA_UBER_TRAY_LOCKSCREEN,
106 IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
107 IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
108 IDS_ASH_STATUS_TRAY_LOCK);
109 lock_->SetTooltipText(
110 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOCK));
111 view->AddButton(lock_);
113 #endif // !defined(OS_WIN)
116 virtual ~DateDefaultView() {}
118 views::View* GetHelpButtonView() const {
122 tray::DateView* GetDateView() const {
127 // Overridden from views::ButtonListener.
128 virtual void ButtonPressed(views::Button* sender,
129 const ui::Event& event) OVERRIDE {
130 ash::Shell* shell = ash::Shell::GetInstance();
131 ash::SystemTrayDelegate* tray_delegate = shell->system_tray_delegate();
132 if (sender == help_) {
133 shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_HELP);
134 tray_delegate->ShowHelp();
135 } else if (sender == shutdown_) {
136 shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_SHUT_DOWN);
137 tray_delegate->ShutDown();
138 } else if (sender == lock_) {
139 shell->metrics()->RecordUserMetricsAction(ash::UMA_TRAY_LOCK_SCREEN);
140 tray_delegate->RequestLockScreen();
146 TrayPopupHeaderButton* help_;
147 TrayPopupHeaderButton* shutdown_;
148 TrayPopupHeaderButton* lock_;
149 tray::DateView* date_view_;
151 DISALLOW_COPY_AND_ASSIGN(DateDefaultView);
154 TrayDate::TrayDate(SystemTray* system_tray)
155 : SystemTrayItem(system_tray),
157 default_view_(NULL) {
158 #if defined(OS_CHROMEOS)
159 system_clock_observer_.reset(new SystemClockObserver());
161 Shell::GetInstance()->system_tray_notifier()->AddClockObserver(this);
164 TrayDate::~TrayDate() {
165 Shell::GetInstance()->system_tray_notifier()->RemoveClockObserver(this);
168 views::View* TrayDate::GetHelpButtonView() const {
171 return default_view_->GetHelpButtonView();
174 views::View* TrayDate::CreateTrayView(user::LoginStatus status) {
175 CHECK(time_tray_ == NULL);
176 ClockLayout clock_layout =
177 (system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
178 system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP) ?
179 HORIZONTAL_CLOCK : VERTICAL_CLOCK;
180 time_tray_ = new tray::TimeView(clock_layout);
181 views::View* view = new TrayItemView(this);
182 view->AddChildView(time_tray_);
186 views::View* TrayDate::CreateDefaultView(user::LoginStatus status) {
187 default_view_ = new DateDefaultView(status);
188 return default_view_;
191 views::View* TrayDate::CreateDetailedView(user::LoginStatus status) {
195 void TrayDate::DestroyTrayView() {
199 void TrayDate::DestroyDefaultView() {
200 default_view_ = NULL;
203 void TrayDate::DestroyDetailedView() {
206 void TrayDate::UpdateAfterLoginStatusChange(user::LoginStatus status) {
209 void TrayDate::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
211 ClockLayout clock_layout = (alignment == SHELF_ALIGNMENT_BOTTOM ||
212 alignment == SHELF_ALIGNMENT_TOP) ?
213 HORIZONTAL_CLOCK : VERTICAL_CLOCK;
214 time_tray_->UpdateClockLayout(clock_layout);
218 void TrayDate::OnDateFormatChanged() {
220 time_tray_->UpdateTimeFormat();
222 default_view_->GetDateView()->UpdateTimeFormat();
225 void TrayDate::OnSystemClockTimeUpdated() {
227 time_tray_->UpdateTimeFormat();
229 default_view_->GetDateView()->UpdateTimeFormat();
232 void TrayDate::Refresh() {
234 time_tray_->UpdateText();
237 } // namespace internal