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#ifndef ASH_SYSTEM_DATE_DATE_VIEW_H_
6#define ASH_SYSTEM_DATE_DATE_VIEW_H_
7
8#include "ash/ash_export.h"
9#include "ash/system/date/tray_date.h"
10#include "ash/system/tray/actionable_view.h"
11#include "base/i18n/time_formatting.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/timer/timer.h"
14#include "ui/views/view.h"
15
16namespace views {
17class Label;
18}
19
20namespace ash {
21namespace tray {
22
23// Abstract base class containing common updating and layout code for the
24// DateView popup and the TimeView tray icon. Exported for tests.
25class ASH_EXPORT BaseDateTimeView : public ActionableView {
26 public:
27  virtual ~BaseDateTimeView();
28
29  // Updates the displayed text for the current time and calls SetTimer().
30  void UpdateText();
31
32 protected:
33  BaseDateTimeView();
34
35 private:
36  // Starts |timer_| to schedule the next update.
37  void SetTimer(const base::Time& now);
38
39  // Updates labels to display the current time.
40  virtual void UpdateTextInternal(const base::Time& now) = 0;
41
42  // Overridden from views::View.
43  virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE;
44  virtual void OnLocaleChanged() OVERRIDE;
45
46  // Invokes UpdateText() when the displayed time should change.
47  base::OneShotTimer<BaseDateTimeView> timer_;
48
49  DISALLOW_COPY_AND_ASSIGN(BaseDateTimeView);
50};
51
52// Popup view used to display the date and day of week.
53class ASH_EXPORT DateView : public BaseDateTimeView {
54 public:
55  DateView();
56  virtual ~DateView();
57
58  // Sets the action the view should take. An actionable date view gives visual
59  // feedback on hover, can be focused by keyboard, and clicking/pressing space
60  // or enter on the view executes the action.
61  void SetAction(TrayDate::DateAction action);
62
63  // Updates the format of the displayed time.
64  void UpdateTimeFormat();
65
66  base::HourClockType GetHourTypeForTesting() const;
67
68 private:
69  // Overridden from BaseDateTimeView.
70  virtual void UpdateTextInternal(const base::Time& now) OVERRIDE;
71
72  // Overridden from ActionableView.
73  virtual bool PerformAction(const ui::Event& event) OVERRIDE;
74
75  // Overridden from views::View.
76  virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
77  virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
78
79  views::Label* date_label_;
80
81  // Time format (12/24hr) used for accessibility string.
82  base::HourClockType hour_type_;
83
84  TrayDate::DateAction action_;
85
86  DISALLOW_COPY_AND_ASSIGN(DateView);
87};
88
89// Tray view used to display the current time.
90// Exported for tests.
91class ASH_EXPORT TimeView : public BaseDateTimeView {
92 public:
93  explicit TimeView(TrayDate::ClockLayout clock_layout);
94  virtual ~TimeView();
95
96  // Updates the format of the displayed time.
97  void UpdateTimeFormat();
98
99  // Updates clock layout.
100  void UpdateClockLayout(TrayDate::ClockLayout clock_layout);
101
102  base::HourClockType GetHourTypeForTesting() const;
103
104 private:
105  friend class TimeViewTest;
106
107  // Overridden from BaseDateTimeView.
108  virtual void UpdateTextInternal(const base::Time& now) OVERRIDE;
109
110  // Overridden from ActionableView.
111  virtual bool PerformAction(const ui::Event& event) OVERRIDE;
112
113  // Overridden from views::View.
114  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
115
116  void SetBorderFromLayout(TrayDate::ClockLayout clock_layout);
117  void SetupLabels();
118  void SetupLabel(views::Label* label);
119
120  // Label text used for the normal horizontal shelf.
121  scoped_ptr<views::Label> horizontal_label_;
122
123  // The time label is split into two lines for the vertical shelf.
124  scoped_ptr<views::Label> vertical_label_hours_;
125  scoped_ptr<views::Label> vertical_label_minutes_;
126
127  base::HourClockType hour_type_;
128
129  DISALLOW_COPY_AND_ASSIGN(TimeView);
130};
131
132}  // namespace tray
133}  // namespace ash
134
135#endif  // ASH_SYSTEM_DATE_DATE_VIEW_H_
136