tray_date.cc revision 868fa2fe829687343ffae624259930155e16dbd8
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/date/tray_date.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/session_state_delegate.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell_delegate.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/date/date_view.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ash/system/tray/system_tray.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/system_tray_delegate.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ash/system/tray/system_tray_notifier.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/tray_constants.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ash/system/tray/tray_item_view.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/tray_popup_header_button.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/time_formatting.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/strings/stringprintf.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/time.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/timer.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/ash_resources.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/ash_strings.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/icu/public/i18n/unicode/datefmt.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/icu/public/i18n/unicode/fieldpos.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/icu/public/i18n/unicode/fmtable.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkRect.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image_skia.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/size.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/button/button.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/image_view.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/label.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/layout/box_layout.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/layout/fill_layout.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/painter.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/view.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/widget/widget.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kPaddingVertical = 19;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DateDefaultView : public views::View,
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        public views::ButtonListener {
48a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) public:
49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  explicit DateDefaultView(ash::user::LoginStatus login)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : help_(NULL),
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        shutdown_(NULL),
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        lock_(NULL) {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetLayoutManager(new views::FillLayout);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ash::internal::tray::DateView* date_view =
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new ash::internal::tray::DateView();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    date_view->set_border(views::Border::CreateEmptyBorder(kPaddingVertical,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ash::kTrayPopupPaddingHorizontal,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        0,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        0));
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ash::internal::SpecialPopupRow* view = new ash::internal::SpecialPopupRow();
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    view->SetContent(date_view);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    AddChildView(view);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (login == ash::user::LOGGED_IN_LOCKED ||
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        login == ash::user::LOGGED_IN_NONE)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    date_view->SetActionable(true);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    help_ = new ash::internal::TrayPopupHeaderButton(this,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        IDR_AURA_UBER_TRAY_HELP,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        IDR_AURA_UBER_TRAY_HELP,
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        IDR_AURA_UBER_TRAY_HELP_HOVER,
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        IDR_AURA_UBER_TRAY_HELP_HOVER,
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        IDS_ASH_STATUS_TRAY_HELP);
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    help_->SetTooltipText(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_HELP));
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    view->AddButton(help_);
797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (login != ash::user::LOGGED_IN_LOCKED &&
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        login != ash::user::LOGGED_IN_RETAIL_MODE) {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shutdown_ = new ash::internal::TrayPopupHeaderButton(this,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          IDR_AURA_UBER_TRAY_SHUTDOWN,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          IDR_AURA_UBER_TRAY_SHUTDOWN,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          IDS_ASH_STATUS_TRAY_SHUTDOWN);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shutdown_->SetTooltipText(
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      view->AddButton(shutdown_);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (ash::Shell::GetInstance()->session_state_delegate()->CanLockScreen()) {
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      lock_ = new ash::internal::TrayPopupHeaderButton(this,
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          IDR_AURA_UBER_TRAY_LOCKSCREEN,
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          IDR_AURA_UBER_TRAY_LOCKSCREEN,
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          IDR_AURA_UBER_TRAY_LOCKSCREEN_HOVER,
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          IDS_ASH_STATUS_TRAY_LOCK);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      lock_->SetTooltipText(
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOCK));
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      view->AddButton(lock_);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~DateDefaultView() {}
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from views::ButtonListener.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ButtonPressed(views::Button* sender,
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const ui::Event& event) OVERRIDE {
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ash::Shell* shell = ash::Shell::GetInstance();
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ash::ShellDelegate* shell_delegate = shell->delegate();
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ash::SystemTrayDelegate* tray_delegate = shell->system_tray_delegate();
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (sender == help_) {
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell_delegate->RecordUserMetricsAction(ash::UMA_TRAY_HELP);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      tray_delegate->ShowHelp();
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (sender == shutdown_) {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell_delegate->RecordUserMetricsAction(ash::UMA_TRAY_SHUT_DOWN);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      tray_delegate->ShutDown();
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (sender == lock_) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell_delegate->RecordUserMetricsAction(ash::UMA_TRAY_LOCK_SCREEN);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      tray_delegate->RequestLockScreen();
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ash::internal::TrayPopupHeaderButton* help_;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ash::internal::TrayPopupHeaderButton* shutdown_;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ash::internal::TrayPopupHeaderButton* lock_;
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DateDefaultView);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ash {
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace internal {
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TrayDate::TrayDate(SystemTray* system_tray)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : SystemTrayItem(system_tray),
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      time_tray_(NULL) {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell::GetInstance()->system_tray_notifier()->AddClockObserver(this);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TrayDate::~TrayDate() {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell::GetInstance()->system_tray_notifier()->RemoveClockObserver(this);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
150
151views::View* TrayDate::CreateTrayView(user::LoginStatus status) {
152  CHECK(time_tray_ == NULL);
153  ClockLayout clock_layout =
154      (system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
155       system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP) ?
156          HORIZONTAL_CLOCK : VERTICAL_CLOCK;
157  time_tray_ = new tray::TimeView(clock_layout);
158  views::View* view = new TrayItemView(this);
159  view->AddChildView(time_tray_);
160  return view;
161}
162
163views::View* TrayDate::CreateDefaultView(user::LoginStatus status) {
164  return new DateDefaultView(status);
165}
166
167views::View* TrayDate::CreateDetailedView(user::LoginStatus status) {
168  return NULL;
169}
170
171void TrayDate::DestroyTrayView() {
172  time_tray_ = NULL;
173}
174
175void TrayDate::DestroyDefaultView() {
176}
177
178void TrayDate::DestroyDetailedView() {
179}
180
181void TrayDate::UpdateAfterLoginStatusChange(user::LoginStatus status) {
182}
183
184void TrayDate::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
185  if (time_tray_) {
186    ClockLayout clock_layout = (alignment == SHELF_ALIGNMENT_BOTTOM ||
187        alignment == SHELF_ALIGNMENT_TOP) ?
188            HORIZONTAL_CLOCK : VERTICAL_CLOCK;
189    time_tray_->UpdateClockLayout(clock_layout);
190  }
191}
192
193void TrayDate::OnDateFormatChanged() {
194  if (time_tray_)
195    time_tray_->UpdateTimeFormat();
196}
197
198void TrayDate::OnSystemClockTimeUpdated() {
199  if (time_tray_)
200    time_tray_->UpdateTimeFormat();
201}
202
203void TrayDate::Refresh() {
204  if (time_tray_)
205    time_tray_->UpdateText();
206}
207
208}  // namespace internal
209}  // namespace ash
210