1// Copyright (c) 2013 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 "ash/system/tray/throbber_view.h"
6
7#include "ash/system/tray/tray_constants.h"
8#include "grit/ash_resources.h"
9#include "ui/base/resource/resource_bundle.h"
10#include "ui/compositor/layer.h"
11#include "ui/compositor/scoped_layer_animation_settings.h"
12
13namespace ash {
14namespace {
15
16// Time in ms per throbber frame.
17const int kThrobberFrameMs = 30;
18
19// Duration for showing/hiding animation in milliseconds.
20const int kThrobberAnimationDurationMs = 200;
21
22}  // namespace
23
24SystemTrayThrobber::SystemTrayThrobber(int frame_delay_ms)
25    : views::SmoothedThrobber(frame_delay_ms) {
26}
27
28SystemTrayThrobber::~SystemTrayThrobber() {
29}
30
31void SystemTrayThrobber::SetTooltipText(const base::string16& tooltip_text) {
32  tooltip_text_ = tooltip_text;
33}
34
35bool SystemTrayThrobber::GetTooltipText(const gfx::Point& p,
36                                        base::string16* tooltip) const {
37  if (tooltip_text_.empty())
38    return false;
39
40  *tooltip = tooltip_text_;
41  return true;
42}
43
44ThrobberView::ThrobberView() {
45  throbber_ = new SystemTrayThrobber(kThrobberFrameMs);
46  throbber_->SetFrames(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
47      IDR_AURA_CROS_DEFAULT_THROBBER).ToImageSkia());
48  throbber_->set_stop_delay_ms(kThrobberAnimationDurationMs);
49  AddChildView(throbber_);
50
51  SetPaintToLayer(true);
52  layer()->SetFillsBoundsOpaquely(false);
53  layer()->SetOpacity(0.0);
54}
55
56ThrobberView::~ThrobberView() {
57}
58
59gfx::Size ThrobberView::GetPreferredSize() const {
60  return gfx::Size(ash::kTrayPopupItemHeight, ash::kTrayPopupItemHeight);
61}
62
63void ThrobberView::Layout() {
64  View* child = child_at(0);
65  gfx::Size ps = child->GetPreferredSize();
66  child->SetBounds((width() - ps.width()) / 2,
67                   (height() - ps.height()) / 2,
68                   ps.width(), ps.height());
69  SizeToPreferredSize();
70}
71
72bool ThrobberView::GetTooltipText(const gfx::Point& p,
73                                  base::string16* tooltip) const {
74  if (tooltip_text_.empty())
75    return false;
76
77  *tooltip = tooltip_text_;
78  return true;
79}
80
81void ThrobberView::Start() {
82  ScheduleAnimation(true);
83  throbber_->Start();
84}
85
86void ThrobberView::Stop() {
87  ScheduleAnimation(false);
88  throbber_->Stop();
89}
90
91void ThrobberView::SetTooltipText(const base::string16& tooltip_text) {
92  tooltip_text_ = tooltip_text;
93  throbber_->SetTooltipText(tooltip_text);
94}
95
96void ThrobberView::ScheduleAnimation(bool start_throbber) {
97  // Stop any previous animation.
98  layer()->GetAnimator()->StopAnimating();
99
100  ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
101  animation.SetTransitionDuration(
102      base::TimeDelta::FromMilliseconds(kThrobberAnimationDurationMs));
103
104  layer()->SetOpacity(start_throbber ? 1.0 : 0.0);
105}
106
107}  // namespace ash
108