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.
5 #include "ash/system/tray/throbber_view.h"
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"
16 // Time in ms per throbber frame.
17 const int kThrobberFrameMs
= 30;
19 // Duration for showing/hiding animation in milliseconds.
20 const int kThrobberAnimationDurationMs
= 200;
24 SystemTrayThrobber::SystemTrayThrobber(int frame_delay_ms
)
25 : views::SmoothedThrobber(frame_delay_ms
) {
28 SystemTrayThrobber::~SystemTrayThrobber() {
31 void SystemTrayThrobber::SetTooltipText(const base::string16
& tooltip_text
) {
32 tooltip_text_
= tooltip_text
;
35 bool SystemTrayThrobber::GetTooltipText(const gfx::Point
& p
,
36 base::string16
* tooltip
) const {
37 if (tooltip_text_
.empty())
40 *tooltip
= tooltip_text_
;
44 ThrobberView::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_
);
51 SetPaintToLayer(true);
52 layer()->SetFillsBoundsOpaquely(false);
53 layer()->SetOpacity(0.0);
56 ThrobberView::~ThrobberView() {
59 gfx::Size
ThrobberView::GetPreferredSize() const {
60 return gfx::Size(ash::kTrayPopupItemHeight
, ash::kTrayPopupItemHeight
);
63 void 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();
72 bool ThrobberView::GetTooltipText(const gfx::Point
& p
,
73 base::string16
* tooltip
) const {
74 if (tooltip_text_
.empty())
77 *tooltip
= tooltip_text_
;
81 void ThrobberView::Start() {
82 ScheduleAnimation(true);
86 void ThrobberView::Stop() {
87 ScheduleAnimation(false);
91 void ThrobberView::SetTooltipText(const base::string16
& tooltip_text
) {
92 tooltip_text_
= tooltip_text
;
93 throbber_
->SetTooltipText(tooltip_text
);
96 void ThrobberView::ScheduleAnimation(bool start_throbber
) {
97 // Stop any previous animation.
98 layer()->GetAnimator()->StopAnimating();
100 ui::ScopedLayerAnimationSettings
animation(layer()->GetAnimator());
101 animation
.SetTransitionDuration(
102 base::TimeDelta::FromMilliseconds(kThrobberAnimationDurationMs
));
104 layer()->SetOpacity(start_throbber
? 1.0 : 0.0);