OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/status/clock_menu_button.h" | 5 #include "chrome/browser/chromeos/status/clock_menu_button.h" |
6 | 6 |
7 #include "base/i18n/time_formatting.h" | 7 #include "base/i18n/time_formatting.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/chromeos/cros/cros_library.h" | 11 #include "chrome/browser/chromeos/cros/cros_library.h" |
12 #include "chrome/browser/chromeos/status/status_area_host.h" | 12 #include "chrome/browser/chromeos/status/status_area_host.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "content/common/notification_details.h" | 16 #include "content/common/notification_details.h" |
17 #include "content/common/notification_source.h" | 17 #include "content/common/notification_source.h" |
18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
20 #include "ui/base/resource/resource_bundle.h" | |
21 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
22 #include "ui/gfx/font.h" | 21 #include "ui/gfx/font.h" |
| 22 #include "unicode/datefmt.h" |
23 | 23 |
24 namespace chromeos { | 24 namespace chromeos { |
25 | 25 |
26 // Amount of slop to add into the timer to make sure we're into the next minute | 26 // Amount of slop to add into the timer to make sure we're into the next minute |
27 // when the timer goes off. | 27 // when the timer goes off. |
28 const int kTimerSlopSeconds = 1; | 28 const int kTimerSlopSeconds = 1; |
29 | 29 |
30 #if defined(CROS_FONTS_USING_BCI) | |
31 const int kFontSizeDelta = 0; | |
32 #else | |
33 const int kFontSizeDelta = 1; | |
34 #endif | |
35 | |
36 ClockMenuButton::ClockMenuButton(StatusAreaHost* host) | 30 ClockMenuButton::ClockMenuButton(StatusAreaHost* host) |
37 : StatusAreaButton(this), | 31 : StatusAreaButton(host, this) { |
38 host_(host) { | |
39 // Add as SystemAccess observer. We update the clock if timezone changes. | 32 // Add as SystemAccess observer. We update the clock if timezone changes. |
40 SystemAccess::GetInstance()->AddObserver(this); | 33 SystemAccess::GetInstance()->AddObserver(this); |
41 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); | 34 CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); |
42 // Start monitoring the kUse24HourClock preference. | 35 // Start monitoring the kUse24HourClock preference. |
43 if (host->GetProfile()) { // This can be NULL in the login screen. | 36 if (host->GetProfile()) { // This can be NULL in the login screen. |
44 registrar_.Init(host->GetProfile()->GetPrefs()); | 37 registrar_.Init(host->GetProfile()->GetPrefs()); |
45 registrar_.Add(prefs::kUse24HourClock, this); | 38 registrar_.Add(prefs::kUse24HourClock, this); |
46 } | 39 } |
47 | 40 |
48 set_border(NULL); | |
49 set_use_menu_button_paint(true); | |
50 SetFont(ResourceBundle::GetSharedInstance().GetFont( | |
51 ResourceBundle::BaseFont).DeriveFont(kFontSizeDelta)); | |
52 SetEnabledColor(0xB3FFFFFF); // White with 70% Alpha | |
53 SetShowMultipleIconStates(false); | |
54 set_alignment(TextButton::ALIGN_CENTER); | |
55 UpdateTextAndSetNextTimer(); | 41 UpdateTextAndSetNextTimer(); |
56 } | 42 } |
57 | 43 |
58 ClockMenuButton::~ClockMenuButton() { | 44 ClockMenuButton::~ClockMenuButton() { |
59 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); | 45 CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this); |
60 SystemAccess::GetInstance()->RemoveObserver(this); | 46 SystemAccess::GetInstance()->RemoveObserver(this); |
61 } | 47 } |
62 | 48 |
63 void ClockMenuButton::UpdateTextAndSetNextTimer() { | 49 void ClockMenuButton::UpdateTextAndSetNextTimer() { |
64 UpdateText(); | 50 UpdateText(); |
(...skipping 15 matching lines...) Expand all Loading... |
80 // called just a teeny bit early, then it will skip the next minute. | 66 // called just a teeny bit early, then it will skip the next minute. |
81 seconds_left += kTimerSlopSeconds; | 67 seconds_left += kTimerSlopSeconds; |
82 | 68 |
83 timer_.Start(base::TimeDelta::FromSeconds(seconds_left), this, | 69 timer_.Start(base::TimeDelta::FromSeconds(seconds_left), this, |
84 &ClockMenuButton::UpdateTextAndSetNextTimer); | 70 &ClockMenuButton::UpdateTextAndSetNextTimer); |
85 } | 71 } |
86 | 72 |
87 void ClockMenuButton::UpdateText() { | 73 void ClockMenuButton::UpdateText() { |
88 base::Time time(base::Time::Now()); | 74 base::Time time(base::Time::Now()); |
89 // If the profie is present, check the use 24-hour clock preference. | 75 // If the profie is present, check the use 24-hour clock preference. |
90 if (host_->GetProfile()) { // This can be NULL in the login screen. | 76 const bool use_24hour_clock = |
91 const bool use_24hour_clock = | 77 host_->GetProfile() && |
92 host_->GetProfile()->GetPrefs()->GetBoolean(prefs::kUse24HourClock); | 78 host_->GetProfile()->GetPrefs()->GetBoolean(prefs::kUse24HourClock); |
93 base::HourClockType clock_type = (use_24hour_clock ? | 79 if (use_24hour_clock) { |
94 base::k24HourClock : base::k12HourClock); | |
95 SetText(UTF16ToWide(base::TimeFormatTimeOfDayWithHourClockType( | 80 SetText(UTF16ToWide(base::TimeFormatTimeOfDayWithHourClockType( |
96 time, clock_type))); | 81 time, base::k24HourClock))); |
97 } else { | 82 } else { |
98 SetText(UTF16ToWide(base::TimeFormatTimeOfDay(time))); | 83 // Remove the am/pm field if it's present. |
| 84 scoped_ptr<icu::DateFormat> formatter( |
| 85 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)); |
| 86 icu::UnicodeString time_string; |
| 87 icu::FieldPosition ampm_field(icu::DateFormat::kAmPmField); |
| 88 formatter->format( |
| 89 static_cast<UDate>(time.ToDoubleT() * 1000), time_string, ampm_field); |
| 90 int ampm_length = ampm_field.getEndIndex() - ampm_field.getBeginIndex(); |
| 91 if (ampm_length) { |
| 92 int begin = ampm_field.getBeginIndex(); |
| 93 // Doesn't include any spacing before the field. |
| 94 if (begin) |
| 95 begin--; |
| 96 time_string.removeBetween(begin, ampm_field.getEndIndex()); |
| 97 } |
| 98 string16 time_string16 = |
| 99 string16(time_string.getBuffer(), |
| 100 static_cast<size_t>(time_string.length())); |
| 101 SetText(UTF16ToWide(time_string16)); |
99 } | 102 } |
100 SetTooltipText(UTF16ToWide(base::TimeFormatShortDate(time))); | 103 SetTooltipText(UTF16ToWide(base::TimeFormatShortDate(time))); |
101 SchedulePaint(); | 104 SchedulePaint(); |
102 } | 105 } |
103 | 106 |
104 //////////////////////////////////////////////////////////////////////////////// | 107 //////////////////////////////////////////////////////////////////////////////// |
105 // ClockMenuButton, NotificationObserver implementation: | 108 // ClockMenuButton, NotificationObserver implementation: |
106 | 109 |
107 void ClockMenuButton::Observe(NotificationType type, | 110 void ClockMenuButton::Observe(NotificationType type, |
108 const NotificationSource& source, | 111 const NotificationSource& source, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } | 177 } |
175 | 178 |
176 //////////////////////////////////////////////////////////////////////////////// | 179 //////////////////////////////////////////////////////////////////////////////// |
177 // ClockMenuButton, views::View implementation: | 180 // ClockMenuButton, views::View implementation: |
178 | 181 |
179 void ClockMenuButton::OnLocaleChanged() { | 182 void ClockMenuButton::OnLocaleChanged() { |
180 UpdateText(); | 183 UpdateText(); |
181 } | 184 } |
182 | 185 |
183 } // namespace chromeos | 186 } // namespace chromeos |
OLD | NEW |