summaryrefslogtreecommitdiffstats
path: root/chromium/ash/system/tray/throbber_view.cc
blob: 2c2cc2069044ffb7758b7436de8e4f8dad03c8f3 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2013 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 "ash/system/tray/throbber_view.h"

#include "ash/system/tray/tray_constants.h"
#include "grit/ash_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"

namespace ash {
namespace internal {

namespace {

// Time in ms per throbber frame.
const int kThrobberFrameMs = 30;

// Duration for showing/hiding animation in milliseconds.
const int kThrobberAnimationDurationMs = 200;

}  // namespace

SystemTrayThrobber::SystemTrayThrobber(int frame_delay_ms)
    : views::SmoothedThrobber(frame_delay_ms) {
}

SystemTrayThrobber::~SystemTrayThrobber() {
}

void SystemTrayThrobber::SetTooltipText(const base::string16& tooltip_text) {
  tooltip_text_ = tooltip_text;
}

bool SystemTrayThrobber::GetTooltipText(const gfx::Point& p,
                                        base::string16* tooltip) const {
  if (tooltip_text_.empty())
    return false;

  *tooltip = tooltip_text_;
  return true;
}

ThrobberView::ThrobberView() {
  throbber_ = new SystemTrayThrobber(kThrobberFrameMs);
  throbber_->SetFrames(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
      IDR_AURA_CROS_DEFAULT_THROBBER).ToImageSkia());
  throbber_->set_stop_delay_ms(kThrobberAnimationDurationMs);
  AddChildView(throbber_);

  SetPaintToLayer(true);
  layer()->SetFillsBoundsOpaquely(false);
  layer()->SetOpacity(0.0);
}

ThrobberView::~ThrobberView() {
}

gfx::Size ThrobberView::GetPreferredSize() {
  return gfx::Size(ash::kTrayPopupItemHeight, ash::kTrayPopupItemHeight);
}

void ThrobberView::Layout() {
  View* child = child_at(0);
  gfx::Size ps = child->GetPreferredSize();
  child->SetBounds((width() - ps.width()) / 2,
                   (height() - ps.height()) / 2,
                   ps.width(), ps.height());
  SizeToPreferredSize();
}

bool ThrobberView::GetTooltipText(const gfx::Point& p,
                                  base::string16* tooltip) const {
  if (tooltip_text_.empty())
    return false;

  *tooltip = tooltip_text_;
  return true;
}

void ThrobberView::Start() {
  ScheduleAnimation(true);
  throbber_->Start();
}

void ThrobberView::Stop() {
  ScheduleAnimation(false);
  throbber_->Stop();
}

void ThrobberView::SetTooltipText(const base::string16& tooltip_text) {
  tooltip_text_ = tooltip_text;
  throbber_->SetTooltipText(tooltip_text);
}

void ThrobberView::ScheduleAnimation(bool start_throbber) {
  // Stop any previous animation.
  layer()->GetAnimator()->StopAnimating();

  ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
  animation.SetTransitionDuration(
      base::TimeDelta::FromMilliseconds(kThrobberAnimationDurationMs));

  layer()->SetOpacity(start_throbber ? 1.0 : 0.0);
}

}  // namespace internal
}  // namespace ash