tray_date.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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.
4
5#include "ash/system/date/tray_date.h"
6
7#include "ash/session_state_delegate.h"
8#include "ash/shell.h"
9#include "ash/shell_delegate.h"
10#include "ash/system/date/date_view.h"
11#include "ash/system/tray/system_tray.h"
12#include "ash/system/tray/system_tray_delegate.h"
13#include "ash/system/tray/system_tray_notifier.h"
14#include "ash/system/tray/tray_constants.h"
15#include "ash/system/tray/tray_item_view.h"
16#include "ash/system/tray/tray_popup_header_button.h"
17#include "base/i18n/time_formatting.h"
18#include "base/strings/stringprintf.h"
19#include "base/strings/utf_string_conversions.h"
20#include "base/time/time.h"
21#include "base/timer/timer.h"
22#include "grit/ash_resources.h"
23#include "grit/ash_strings.h"
24#include "third_party/icu/source/i18n/unicode/datefmt.h"
25#include "third_party/icu/source/i18n/unicode/fieldpos.h"
26#include "third_party/icu/source/i18n/unicode/fmtable.h"
27#include "third_party/skia/include/core/SkRect.h"
28#include "ui/base/l10n/l10n_util.h"
29#include "ui/base/resource/resource_bundle.h"
30#include "ui/gfx/image/image.h"
31#include "ui/gfx/image/image_skia.h"
32#include "ui/gfx/size.h"
33#include "ui/views/controls/button/button.h"
34#include "ui/views/controls/image_view.h"
35#include "ui/views/controls/label.h"
36#include "ui/views/layout/box_layout.h"
37#include "ui/views/layout/fill_layout.h"
38#include "ui/views/painter.h"
39#include "ui/views/view.h"
40#include "ui/views/widget/widget.h"
41
42#if defined(OS_CHROMEOS)
43#include "ash/system/chromeos/system_clock_observer.h"
44#endif
45
46namespace {
47
48const int kPaddingVertical = 19;
49
50class DateDefaultView : public views::View,
51                        public views::ButtonListener {
52 public:
53  explicit DateDefaultView(ash::user::LoginStatus login)
54      : help_(NULL),
55        shutdown_(NULL),
56        lock_(NULL) {
57    SetLayoutManager(new views::FillLayout);
58
59    ash::internal::tray::DateView* date_view =
60        new ash::internal::tray::DateView();
61    date_view->set_border(views::Border::CreateEmptyBorder(kPaddingVertical,
62        ash::kTrayPopupPaddingHorizontal,
63        0,
64        0));
65    ash::internal::SpecialPopupRow* view = new ash::internal::SpecialPopupRow();
66    view->SetContent(date_view);
67    AddChildView(view);
68
69    if (login == ash::user::LOGGED_IN_LOCKED ||
70        login == ash::user::LOGGED_IN_NONE)
71      return;
72
73    date_view->SetActionable(true);
74
75    help_ = new ash::internal::TrayPopupHeaderButton(this,
76        IDR_AURA_UBER_TRAY_HELP,
77        IDR_AURA_UBER_TRAY_HELP,
78        IDR_AURA_UBER_TRAY_HELP_HOVER,
79        IDR_AURA_UBER_TRAY_HELP_HOVER,
80        IDS_ASH_STATUS_TRAY_HELP);
81    help_->SetTooltipText(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_HELP));
82    view->AddButton(help_);
83
84    if (login != ash::user::LOGGED_IN_LOCKED &&
85        login != ash::user::LOGGED_IN_RETAIL_MODE) {
86      shutdown_ = new ash::internal::TrayPopupHeaderButton(this,
87          IDR_AURA_UBER_TRAY_SHUTDOWN,
88          IDR_AURA_UBER_TRAY_SHUTDOWN,
89          IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
90          IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
91          IDS_ASH_STATUS_TRAY_SHUTDOWN);
92      shutdown_->SetTooltipText(
93          l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
94      view->AddButton(shutdown_);
95    }
96
97    if (ash::Shell::GetInstance()->session_state_delegate()->CanLockScreen()) {
98      lock_ = new ash::internal::TrayPopupHeaderButton(this,
99          IDR_AURA_UBER_TRAY_LOCKSCREEN,
100          IDR_AURA_UBER_TRAY_LOCKSCREEN,
101          IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
102          IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
103          IDS_ASH_STATUS_TRAY_LOCK);
104      lock_->SetTooltipText(
105          l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOCK));
106      view->AddButton(lock_);
107    }
108  }
109
110  virtual ~DateDefaultView() {}
111
112 private:
113  // Overridden from views::ButtonListener.
114  virtual void ButtonPressed(views::Button* sender,
115                             const ui::Event& event) OVERRIDE {
116    ash::Shell* shell = ash::Shell::GetInstance();
117    ash::ShellDelegate* shell_delegate = shell->delegate();
118    ash::SystemTrayDelegate* tray_delegate = shell->system_tray_delegate();
119    if (sender == help_) {
120      shell_delegate->RecordUserMetricsAction(ash::UMA_TRAY_HELP);
121      tray_delegate->ShowHelp();
122    } else if (sender == shutdown_) {
123      shell_delegate->RecordUserMetricsAction(ash::UMA_TRAY_SHUT_DOWN);
124      tray_delegate->ShutDown();
125    } else if (sender == lock_) {
126      shell_delegate->RecordUserMetricsAction(ash::UMA_TRAY_LOCK_SCREEN);
127      tray_delegate->RequestLockScreen();
128    } else {
129      NOTREACHED();
130    }
131  }
132
133  ash::internal::TrayPopupHeaderButton* help_;
134  ash::internal::TrayPopupHeaderButton* shutdown_;
135  ash::internal::TrayPopupHeaderButton* lock_;
136
137  DISALLOW_COPY_AND_ASSIGN(DateDefaultView);
138};
139
140}  // namespace
141
142namespace ash {
143namespace internal {
144
145TrayDate::TrayDate(SystemTray* system_tray)
146    : SystemTrayItem(system_tray),
147      time_tray_(NULL) {
148#if defined(OS_CHROMEOS)
149  system_clock_observer_.reset(new SystemClockObserver());
150#endif
151  Shell::GetInstance()->system_tray_notifier()->AddClockObserver(this);
152}
153
154TrayDate::~TrayDate() {
155  Shell::GetInstance()->system_tray_notifier()->RemoveClockObserver(this);
156}
157
158views::View* TrayDate::CreateTrayView(user::LoginStatus status) {
159  CHECK(time_tray_ == NULL);
160  ClockLayout clock_layout =
161      (system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
162       system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP) ?
163          HORIZONTAL_CLOCK : VERTICAL_CLOCK;
164  time_tray_ = new tray::TimeView(clock_layout);
165  views::View* view = new TrayItemView(this);
166  view->AddChildView(time_tray_);
167  return view;
168}
169
170views::View* TrayDate::CreateDefaultView(user::LoginStatus status) {
171  return new DateDefaultView(status);
172}
173
174views::View* TrayDate::CreateDetailedView(user::LoginStatus status) {
175  return NULL;
176}
177
178void TrayDate::DestroyTrayView() {
179  time_tray_ = NULL;
180}
181
182void TrayDate::DestroyDefaultView() {
183}
184
185void TrayDate::DestroyDetailedView() {
186}
187
188void TrayDate::UpdateAfterLoginStatusChange(user::LoginStatus status) {
189}
190
191void TrayDate::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
192  if (time_tray_) {
193    ClockLayout clock_layout = (alignment == SHELF_ALIGNMENT_BOTTOM ||
194        alignment == SHELF_ALIGNMENT_TOP) ?
195            HORIZONTAL_CLOCK : VERTICAL_CLOCK;
196    time_tray_->UpdateClockLayout(clock_layout);
197  }
198}
199
200void TrayDate::OnDateFormatChanged() {
201  if (time_tray_)
202    time_tray_->UpdateTimeFormat();
203}
204
205void TrayDate::OnSystemClockTimeUpdated() {
206  if (time_tray_)
207    time_tray_->UpdateTimeFormat();
208}
209
210void TrayDate::Refresh() {
211  if (time_tray_)
212    time_tray_->UpdateText();
213}
214
215}  // namespace internal
216}  // namespace ash
217