Home | History | Annotate | Download | only in system
      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(SystemUI::ColorScheme color_scheme) {
     21   SetHorizontalAlignment(gfx::ALIGN_LEFT);
     22   SetEnabledColor((color_scheme == SystemUI::COLOR_SCHEME_LIGHT)
     23                       ? SK_ColorWHITE
     24                       : SK_ColorDKGRAY);
     25   SetAutoColorReadabilityEnabled(false);
     26   SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD));
     27   SetSubpixelRenderingEnabled(false);
     28 
     29   const int kHorizontalSpacing = 10;
     30   const int kVerticalSpacing = 3;
     31   SetBorder(views::Border::CreateEmptyBorder(kVerticalSpacing,
     32                                              kHorizontalSpacing,
     33                                              kVerticalSpacing,
     34                                              kHorizontalSpacing));
     35 
     36   UpdateText();
     37 }
     38 
     39 TimeView::~TimeView() {
     40 }
     41 
     42 void TimeView::SetTimer(base::Time now) {
     43   // Try to set the timer to go off at the next change of the minute. We don't
     44   // want to have the timer go off more than necessary since that will cause
     45   // the CPU to wake up and consume power.
     46   base::Time::Exploded exploded;
     47   now.LocalExplode(&exploded);
     48 
     49   // Often this will be called at minute boundaries, and we'll actually want
     50   // 60 seconds from now.
     51   int seconds_left = 60 - exploded.second;
     52   if (seconds_left == 0)
     53     seconds_left = 60;
     54 
     55   // Make sure that the timer fires on the next minute. Without this, if it is
     56   // called just a teeny bit early, then it will skip the next minute.
     57   seconds_left += kTimerSlopSeconds;
     58 
     59   timer_.Stop();
     60   timer_.Start(
     61       FROM_HERE, base::TimeDelta::FromSeconds(seconds_left),
     62       this, &TimeView::UpdateText);
     63 }
     64 
     65 void TimeView::UpdateText() {
     66   base::Time now = base::Time::Now();
     67   SetText(base::TimeFormatTimeOfDayWithHourClockType(
     68       now, base::k12HourClock, base::kKeepAmPm));
     69   SetTimer(now);
     70 }
     71 
     72 }  // namespace athena
     73