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"
18 // Time in ms per throbber frame.
19 const int kThrobberFrameMs
= 30;
21 // Duration for showing/hiding animation in milliseconds.
22 const int kThrobberAnimationDurationMs
= 200;
26 SystemTrayThrobber::SystemTrayThrobber(int frame_delay_ms
)
27 : views::SmoothedThrobber(frame_delay_ms
) {
30 SystemTrayThrobber::~SystemTrayThrobber() {
33 void SystemTrayThrobber::SetTooltipText(const string16
& tooltip_text
) {
34 tooltip_text_
= tooltip_text
;
37 bool SystemTrayThrobber::GetTooltipText(const gfx::Point
& p
,
38 string16
* tooltip
) const {
39 if (tooltip_text_
.empty())
42 *tooltip
= tooltip_text_
;
46 ThrobberView::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_
);
53 SetPaintToLayer(true);
54 layer()->SetFillsBoundsOpaquely(false);
55 layer()->SetOpacity(0.0);
58 ThrobberView::~ThrobberView() {
61 gfx::Size
ThrobberView::GetPreferredSize() {
62 return gfx::Size(ash::kTrayPopupItemHeight
, ash::kTrayPopupItemHeight
);
65 void 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();
74 bool ThrobberView::GetTooltipText(const gfx::Point
& p
,
75 string16
* tooltip
) const {
76 if (tooltip_text_
.empty())
79 *tooltip
= tooltip_text_
;
83 void ThrobberView::Start() {
84 ScheduleAnimation(true);
88 void ThrobberView::Stop() {
89 ScheduleAnimation(false);
93 void ThrobberView::SetTooltipText(const string16
& tooltip_text
) {
94 tooltip_text_
= tooltip_text
;
95 throbber_
->SetTooltipText(tooltip_text
);
98 void ThrobberView::ScheduleAnimation(bool start_throbber
) {
99 // Stop any previous animation.
100 layer()->GetAnimator()->StopAnimating();
102 ui::ScopedLayerAnimationSettings
animation(layer()->GetAnimator());
103 animation
.SetTransitionDuration(
104 base::TimeDelta::FromMilliseconds(kThrobberAnimationDurationMs
));
106 layer()->SetOpacity(start_throbber
? 1.0 : 0.0);
109 } // namespace internal