OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "athena/system/time_view.h" |
| 6 |
| 7 #include "base/i18n/time_formatting.h" |
| 8 #include "third_party/skia/include/core/SkColor.h" |
| 9 #include "ui/views/border.h" |
| 10 |
| 11 namespace athena { |
| 12 namespace { |
| 13 |
| 14 // Amount of slop to add into the timer to make sure we're into the next minute |
| 15 // when the timer goes off. |
| 16 const int kTimerSlopSeconds = 1; |
| 17 |
| 18 } // namespace |
| 19 |
| 20 TimeView::TimeView() { |
| 21 SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 22 SetEnabledColor(SK_ColorWHITE); |
| 23 SetAutoColorReadabilityEnabled(false); |
| 24 SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD)); |
| 25 |
| 26 const int kHorizontalSpacing = 10; |
| 27 const int kVerticalSpacing = 3; |
| 28 SetBorder(views::Border::CreateEmptyBorder(kVerticalSpacing, |
| 29 kHorizontalSpacing, |
| 30 kVerticalSpacing, |
| 31 kHorizontalSpacing)); |
| 32 |
| 33 UpdateText(); |
| 34 } |
| 35 |
| 36 TimeView::~TimeView() { |
| 37 } |
| 38 |
| 39 void TimeView::SetTimer(base::Time now) { |
| 40 // Try to set the timer to go off at the next change of the minute. We don't |
| 41 // want to have the timer go off more than necessary since that will cause |
| 42 // the CPU to wake up and consume power. |
| 43 base::Time::Exploded exploded; |
| 44 now.LocalExplode(&exploded); |
| 45 |
| 46 // Often this will be called at minute boundaries, and we'll actually want |
| 47 // 60 seconds from now. |
| 48 int seconds_left = 60 - exploded.second; |
| 49 if (seconds_left == 0) |
| 50 seconds_left = 60; |
| 51 |
| 52 // Make sure that the timer fires on the next minute. Without this, if it is |
| 53 // called just a teeny bit early, then it will skip the next minute. |
| 54 seconds_left += kTimerSlopSeconds; |
| 55 |
| 56 timer_.Stop(); |
| 57 timer_.Start( |
| 58 FROM_HERE, base::TimeDelta::FromSeconds(seconds_left), |
| 59 this, &TimeView::UpdateText); |
| 60 } |
| 61 |
| 62 void TimeView::UpdateText() { |
| 63 base::Time now = base::Time::Now(); |
| 64 SetText(base::TimeFormatTimeOfDayWithHourClockType( |
| 65 now, base::k12HourClock, base::kKeepAmPm)); |
| 66 SetTimer(now); |
| 67 } |
| 68 |
| 69 } // namespace athena |
OLD | NEW |