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