| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/browser/chromeos/clock_menu_button.h" |
| 6 |
| 7 #include "app/gfx/canvas.h" |
| 8 #include "app/gfx/font.h" |
| 9 #include "app/l10n_util.h" |
| 10 #include "app/resource_bundle.h" |
| 11 #include "base/string_util.h" |
| 12 #include "base/time.h" |
| 13 #include "base/time_format.h" |
| 14 #include "grit/generated_resources.h" |
| 15 |
| 16 // Amount of slop to add into the timer to make sure we're into the next minute |
| 17 // when the timer goes off. |
| 18 const int kTimerSlopSeconds = 1; |
| 19 |
| 20 ClockMenuButton::ClockMenuButton() |
| 21 : MenuButton(NULL, std::wstring(), this, false), |
| 22 clock_menu_(this) { |
| 23 SetFont(ResourceBundle::GetSharedInstance().GetFont( |
| 24 ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD)); |
| 25 SetEnabledColor(SK_ColorWHITE); |
| 26 SetShowHighlighted(false); |
| 27 UpdateText(); |
| 28 } |
| 29 |
| 30 void ClockMenuButton::SetNextTimer() { |
| 31 // Try to set the timer to go off at the next change of the minute. We don't |
| 32 // want to have the timer go off more than necessary since that will cause |
| 33 // the CPU to wake up and consume power. |
| 34 base::Time now = base::Time::Now(); |
| 35 base::Time::Exploded exploded; |
| 36 now.LocalExplode(&exploded); |
| 37 |
| 38 // Often this will be called at minute boundaries, and we'll actually want |
| 39 // 60 seconds from now. |
| 40 int seconds_left = 60 - exploded.second; |
| 41 if (seconds_left == 0) |
| 42 seconds_left = 60; |
| 43 |
| 44 // Make sure that the timer fires on the next minute. Without this, if it is |
| 45 // called just a teeny bit early, then it will skip the next minute. |
| 46 seconds_left += kTimerSlopSeconds; |
| 47 |
| 48 timer_.Start(base::TimeDelta::FromSeconds(seconds_left), this, |
| 49 &ClockMenuButton::UpdateText); |
| 50 } |
| 51 |
| 52 void ClockMenuButton::UpdateText() { |
| 53 base::Time::Exploded now; |
| 54 base::Time::Now().LocalExplode(&now); |
| 55 int hour = now.hour % 12; |
| 56 if (hour == 0) |
| 57 hour = 12; |
| 58 |
| 59 std::wstring hour_str = IntToWString(hour); |
| 60 std::wstring min_str = IntToWString(now.minute); |
| 61 // Append a "0" before the minute if it's only a single digit. |
| 62 if (now.minute < 10) |
| 63 min_str = IntToWString(0) + min_str; |
| 64 int msg = now.hour < 12 ? IDS_STATUSBAR_CLOCK_SHORT_TIME_AM : |
| 65 IDS_STATUSBAR_CLOCK_SHORT_TIME_PM; |
| 66 |
| 67 std::wstring time_string = l10n_util::GetStringF(msg, hour_str, min_str); |
| 68 |
| 69 SetText(time_string); |
| 70 SchedulePaint(); |
| 71 SetNextTimer(); |
| 72 } |
| 73 |
| 74 //////////////////////////////////////////////////////////////////////////////// |
| 75 // ClockMenuButton, views::Menu2Model implementation: |
| 76 |
| 77 int ClockMenuButton::GetItemCount() const { |
| 78 return 1; |
| 79 } |
| 80 |
| 81 views::Menu2Model::ItemType ClockMenuButton::GetTypeAt(int index) const { |
| 82 return views::Menu2Model::TYPE_COMMAND; |
| 83 } |
| 84 |
| 85 string16 ClockMenuButton::GetLabelAt(int index) const { |
| 86 return WideToUTF16(base::TimeFormatFriendlyDate(base::Time::Now())); |
| 87 } |
| 88 |
| 89 //////////////////////////////////////////////////////////////////////////////// |
| 90 // ClockMenuButton, views::ViewMenuDelegate implementation: |
| 91 |
| 92 void ClockMenuButton::RunMenu(views::View* source, const gfx::Point& pt, |
| 93 gfx::NativeView hwnd) { |
| 94 clock_menu_.Rebuild(); |
| 95 clock_menu_.UpdateStates(); |
| 96 clock_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
| 97 } |
| OLD | NEW |