Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: ui/message_center/views/notification_progress_bar.cc

Issue 1447933002: Implement an indeterminate progress-bar (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split into two class Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "ui/message_center/views/notification_progress_bar.h"
6
7 #include "third_party/skia/include/core/SkPaint.h"
8 #include "third_party/skia/include/core/SkPath.h"
9 #include "ui/gfx/animation/linear_animation.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/geometry/size.h"
12 #include "ui/gfx/skia_util.h"
13 #include "ui/gfx/text_elider.h"
14 #include "ui/message_center/message_center_style.h"
15
16 namespace {
17
18 // Dimensions.
19 const int kProgressBarWidth = message_center::kNotificationWidth -
20 message_center::kTextLeftPadding - message_center::kTextRightPadding;
21
22 const int kAnimationFrameRateHz = 60;
23
24 const int kAnimationDuration = 2000; // In millisecond
25
26 } // namespace
27
28 namespace message_center {
29
30 // NotificationProgressBar /////////////////////////////////////////////////////
31
32 NotificationProgressBar::NotificationProgressBar() {
33 }
34
35 NotificationProgressBar::~NotificationProgressBar() {
36 }
37
38 NotificationIndeterminateProgressBar::NotificationIndeterminateProgressBar() {
39 indeterminate_bar_animation_.reset(
40 new gfx::LinearAnimation(kAnimationFrameRateHz, this));
41 indeterminate_bar_animation_->SetDuration(kAnimationDuration);
42 indeterminate_bar_animation_->Start();
43 }
44
45 NotificationIndeterminateProgressBar::~NotificationIndeterminateProgressBar() {
46 indeterminate_bar_animation_->Stop(); // Just in case
47 }
48
49 gfx::Size NotificationProgressBarBase::GetPreferredSize() const {
50 gfx::Size pref_size(kProgressBarWidth, message_center::kProgressBarThickness);
51 gfx::Insets insets = GetInsets();
52 pref_size.Enlarge(insets.width(), insets.height());
53 return pref_size;
54 }
55
56 void NotificationProgressBar::OnPaint(gfx::Canvas* canvas) {
57 gfx::Rect content_bounds = GetContentsBounds();
58
59 // Draw background.
60 SkPath background_path;
61 background_path.addRoundRect(gfx::RectToSkRect(content_bounds),
62 message_center::kProgressBarCornerRadius,
63 message_center::kProgressBarCornerRadius);
64 SkPaint background_paint;
65 background_paint.setStyle(SkPaint::kFill_Style);
66 background_paint.setFlags(SkPaint::kAntiAlias_Flag);
67 background_paint.setColor(message_center::kProgressBarBackgroundColor);
68 canvas->DrawPath(background_path, background_paint);
69
70 // Draw slice.
71 SkPath slice_path;
72 const int slice_width =
73 static_cast<int>(content_bounds.width() * GetNormalizedValue() + 0.5);
74 if (slice_width < 1)
75 return;
76
77 gfx::Rect slice_bounds = content_bounds;
78 slice_bounds.set_width(slice_width);
79 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds),
80 message_center::kProgressBarCornerRadius,
81 message_center::kProgressBarCornerRadius);
82
83 SkPaint slice_paint;
84 slice_paint.setStyle(SkPaint::kFill_Style);
85 slice_paint.setFlags(SkPaint::kAntiAlias_Flag);
86 slice_paint.setColor(message_center::kProgressBarSliceColor);
87 canvas->DrawPath(slice_path, slice_paint);
88 }
89
90 void NotificationIndeterminateProgressBar::OnPaint(gfx::Canvas* canvas) {
91 gfx::Rect content_bounds = GetContentsBounds();
92
93 // Draw background.
94 SkPath background_path;
95 background_path.addRoundRect(gfx::RectToSkRect(content_bounds),
96 message_center::kProgressBarCornerRadius,
97 message_center::kProgressBarCornerRadius);
98 SkPaint background_paint;
99 background_paint.setStyle(SkPaint::kFill_Style);
100 background_paint.setFlags(SkPaint::kAntiAlias_Flag);
101 background_paint.setColor(message_center::kProgressBarBackgroundColor);
102 canvas->DrawPath(background_path, background_paint);
103
104 // Draw slice.
105 SkPath slice_path;
106 double time = indeterminate_bar_animation_->GetCurrentValue();
107
108 double bar1_left;
109 double bar1_width;
110 double bar2_left;
111 double bar2_width;
112 if (time < 0.25) {
113 bar1_left = time / 2;
114 bar1_width = time * 1.5;
115 bar2_left = 0;
116 bar2_width = 0;
117 } else if (time < 0.5) {
118 bar1_left = time / 2;
119 bar1_width = time * 1.5;
120 bar2_left = 0;
121 bar2_width = 0;
122 } else if (time < 0.75) {
123 bar1_left = time * 3 - 1.25;
124 bar1_width = 0.75 - (time - 0.5) * 3;
125 bar2_left = 0;
126 bar2_width = time - 0.5;
127 } else {
128 bar1_left = 1;
129 bar1_width = 0;
130 bar2_left = (time - 0.75) * 4;
131 bar2_width = 0.25 - (time - 0.75);
132 }
133
134 int bar1_x = static_cast<int>(content_bounds.width() * bar1_left);
135 int bar1_w =
136 std::min(static_cast<int>(content_bounds.width() * bar1_width + 0.5),
137 content_bounds.width() - bar1_x);
138 int bar2_x = static_cast<int>(content_bounds.width() * bar2_left);
139 int bar2_w =
140 std::min(static_cast<int>(content_bounds.width() * bar2_width + 0.5),
141 content_bounds.width() - bar2_x);
142
143
144 gfx::Rect slice_bounds = content_bounds;
145 slice_bounds.set_x(content_bounds.x() + bar1_x);
146 slice_bounds.set_width(bar1_w);
147 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds),
148 message_center::kProgressBarCornerRadius,
149 message_center::kProgressBarCornerRadius);
150 slice_bounds.set_x(content_bounds.x() + bar2_x);
151 slice_bounds.set_width(bar2_w);
152 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds),
153 message_center::kProgressBarCornerRadius,
154 message_center::kProgressBarCornerRadius);
155
156 SkPaint slice_paint;
157 slice_paint.setStyle(SkPaint::kFill_Style);
158 slice_paint.setFlags(SkPaint::kAntiAlias_Flag);
159 slice_paint.setColor(message_center::kProgressBarSliceColor);
160 canvas->DrawPath(slice_path, slice_paint);
161 }
162
163 void NotificationIndeterminateProgressBar::AnimationProgressed(
164 const gfx::Animation* animation) {
165 if (animation == indeterminate_bar_animation_.get()) {
166 DCHECK(indeterminate_bar_animation_);
167 SchedulePaint();
168 }
169 }
170
171 void NotificationIndeterminateProgressBar::AnimationEnded(
172 const gfx::Animation* animation) {
173 // Restarts animation.
174 if (animation == indeterminate_bar_animation_.get()) {
175 DCHECK(indeterminate_bar_animation_);
176 indeterminate_bar_animation_->Start();
177 }
178 }
179
180 } // namespace message_center
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698