summaryrefslogtreecommitdiffstats
path: root/athena/system/time_view.cc
blob: 9570546d3e3b09feac5d1ecc9539b9b18b2b9532 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "athena/system/time_view.h"

#include "base/i18n/time_formatting.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/views/border.h"

namespace athena {
namespace {

// Amount of slop to add into the timer to make sure we're into the next minute
// when the timer goes off.
const int kTimerSlopSeconds = 1;

}  // namespace

TimeView::TimeView(SystemUI::ColorScheme color_scheme) {
  SetHorizontalAlignment(gfx::ALIGN_LEFT);
  SetEnabledColor((color_scheme == SystemUI::COLOR_SCHEME_LIGHT)
                      ? SK_ColorWHITE
                      : SK_ColorDKGRAY);
  SetAutoColorReadabilityEnabled(false);
  SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD));
  SetSubpixelRenderingEnabled(false);

  const int kHorizontalSpacing = 10;
  const int kVerticalSpacing = 3;
  SetBorder(views::Border::CreateEmptyBorder(kVerticalSpacing,
                                             kHorizontalSpacing,
                                             kVerticalSpacing,
                                             kHorizontalSpacing));

  UpdateText();
}

TimeView::~TimeView() {
}

void TimeView::SetTimer(base::Time now) {
  // Try to set the timer to go off at the next change of the minute. We don't
  // want to have the timer go off more than necessary since that will cause
  // the CPU to wake up and consume power.
  base::Time::Exploded exploded;
  now.LocalExplode(&exploded);

  // Often this will be called at minute boundaries, and we'll actually want
  // 60 seconds from now.
  int seconds_left = 60 - exploded.second;
  if (seconds_left == 0)
    seconds_left = 60;

  // Make sure that the timer fires on the next minute. Without this, if it is
  // called just a teeny bit early, then it will skip the next minute.
  seconds_left += kTimerSlopSeconds;

  timer_.Stop();
  timer_.Start(
      FROM_HERE, base::TimeDelta::FromSeconds(seconds_left),
      this, &TimeView::UpdateText);
}

void TimeView::UpdateText() {
  base::Time now = base::Time::Now();
  SetText(base::TimeFormatTimeOfDayWithHourClockType(
      now, base::k12HourClock, base::kKeepAmPm));
  SetTimer(now);
}

}  // namespace athena