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

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

Issue 2329633003: Implement progress bar spec (determinate and indeterminate). (Closed)
Patch Set: self review Created 4 years, 3 months 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/canvas.h"
10 #include "ui/message_center/message_center_style.h"
11
12 namespace {
13
14 // Dimensions.
15 const int kProgressBarWidth = message_center::kNotificationWidth -
16 message_center::kTextLeftPadding - message_center::kTextRightPadding;
17
18 const int kAnimationFrameRateHz = 60;
19
20 const int kAnimationDuration = 2000; // In millisecond
21
22 } // namespace
23
24 namespace message_center {
25
26 // NotificationProgressBarBase /////////////////////////////////////////////////
27
28 gfx::Size NotificationProgressBarBase::GetPreferredSize() const {
29 gfx::Size pref_size(kProgressBarWidth, message_center::kProgressBarThickness);
30 gfx::Insets insets = GetInsets();
31 pref_size.Enlarge(insets.width(), insets.height());
32 return pref_size;
33 }
34
35 // NotificationProgressBar /////////////////////////////////////////////////////
36
37 NotificationProgressBar::NotificationProgressBar() {
38 }
39
40 NotificationProgressBar::~NotificationProgressBar() {
41 }
42
43 bool NotificationProgressBar::is_indeterminate() {
44 return false;
45 }
46
47 void NotificationProgressBar::OnPaint(gfx::Canvas* canvas) {
48 gfx::Rect content_bounds = GetContentsBounds();
49
50 // Draw background.
51 SkPath background_path;
52 background_path.addRoundRect(gfx::RectToSkRect(content_bounds),
53 message_center::kProgressBarCornerRadius,
54 message_center::kProgressBarCornerRadius);
55 SkPaint background_paint;
56 background_paint.setStyle(SkPaint::kFill_Style);
57 background_paint.setFlags(SkPaint::kAntiAlias_Flag);
58 background_paint.setColor(message_center::kProgressBarBackgroundColor);
59 canvas->DrawPath(background_path, background_paint);
60
61 // Draw slice.
62 SkPath slice_path;
63 const int slice_width =
64 static_cast<int>(content_bounds.width() * GetNormalizedValue() + 0.5);
65 if (slice_width < 1)
66 return;
67
68 gfx::Rect slice_bounds = content_bounds;
69 slice_bounds.set_width(slice_width);
70 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds),
71 message_center::kProgressBarCornerRadius,
72 message_center::kProgressBarCornerRadius);
73
74 SkPaint slice_paint;
75 slice_paint.setStyle(SkPaint::kFill_Style);
76 slice_paint.setFlags(SkPaint::kAntiAlias_Flag);
77 slice_paint.setColor(message_center::kProgressBarSliceColor);
78 canvas->DrawPath(slice_path, slice_paint);
79 }
80
81 // NotificationIndeteminateProgressBar /////////////////////////////////////////
82
83 NotificationIndeterminateProgressBar::NotificationIndeterminateProgressBar() {
84 indeterminate_bar_animation_.reset(
85 new gfx::LinearAnimation(kAnimationFrameRateHz, this));
86 indeterminate_bar_animation_->SetDuration(kAnimationDuration);
87 indeterminate_bar_animation_->Start();
88 }
89
90 NotificationIndeterminateProgressBar::~NotificationIndeterminateProgressBar() {
91 indeterminate_bar_animation_->Stop(); // Just in case
92 }
93
94 bool NotificationIndeterminateProgressBar::is_indeterminate() {
95 return true;
96 }
97
98 void NotificationIndeterminateProgressBar::OnPaint(gfx::Canvas* canvas) {
99 gfx::Rect content_bounds = GetContentsBounds();
100
101 // Draw background.
102 SkPath background_path;
103 background_path.addRoundRect(gfx::RectToSkRect(content_bounds),
104 message_center::kProgressBarCornerRadius,
105 message_center::kProgressBarCornerRadius);
106 SkPaint background_paint;
107 background_paint.setStyle(SkPaint::kFill_Style);
108 background_paint.setFlags(SkPaint::kAntiAlias_Flag);
109 background_paint.setColor(message_center::kProgressBarBackgroundColor);
110 canvas->DrawPath(background_path, background_paint);
111
112 // Draw slice.
113 SkPath slice_path;
114 double time = indeterminate_bar_animation_->GetCurrentValue();
115
116 // The animation spec corresponds to the material design lite's parameter.
117 // (cf. https://github.com/google/material-design-lite/)
118 double bar1_left;
119 double bar1_width;
120 double bar2_left;
121 double bar2_width;
122 if (time < 0.50) {
123 bar1_left = time / 2;
124 bar1_width = time * 1.5;
125 bar2_left = 0;
126 bar2_width = 0;
127 } else if (time < 0.75) {
128 bar1_left = time * 3 - 1.25;
129 bar1_width = 0.75 - (time - 0.5) * 3;
130 bar2_left = 0;
131 bar2_width = time - 0.5;
132 } else {
133 bar1_left = 1;
134 bar1_width = 0;
135 bar2_left = (time - 0.75) * 4;
136 bar2_width = 0.25 - (time - 0.75);
137 }
138
139 int bar1_x = static_cast<int>(content_bounds.width() * bar1_left);
140 int bar1_w =
141 std::min(static_cast<int>(content_bounds.width() * bar1_width + 0.5),
142 content_bounds.width() - bar1_x);
143 int bar2_x = static_cast<int>(content_bounds.width() * bar2_left);
144 int bar2_w =
145 std::min(static_cast<int>(content_bounds.width() * bar2_width + 0.5),
146 content_bounds.width() - bar2_x);
147
148
149 gfx::Rect slice_bounds = content_bounds;
150 slice_bounds.set_x(content_bounds.x() + bar1_x);
151 slice_bounds.set_width(bar1_w);
152 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds),
153 message_center::kProgressBarCornerRadius,
154 message_center::kProgressBarCornerRadius);
155 slice_bounds.set_x(content_bounds.x() + bar2_x);
156 slice_bounds.set_width(bar2_w);
157 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds),
158 message_center::kProgressBarCornerRadius,
159 message_center::kProgressBarCornerRadius);
160
161 SkPaint slice_paint;
162 slice_paint.setStyle(SkPaint::kFill_Style);
163 slice_paint.setFlags(SkPaint::kAntiAlias_Flag);
164 slice_paint.setColor(message_center::kProgressBarSliceColor);
165 canvas->DrawPath(slice_path, slice_paint);
166 }
167
168 void NotificationIndeterminateProgressBar::AnimationProgressed(
169 const gfx::Animation* animation) {
170 if (animation == indeterminate_bar_animation_.get()) {
171 DCHECK(indeterminate_bar_animation_);
172 SchedulePaint();
173 }
174 }
175
176 void NotificationIndeterminateProgressBar::AnimationEnded(
177 const gfx::Animation* animation) {
178 // Restarts animation.
179 if (animation == indeterminate_bar_animation_.get()) {
180 DCHECK(indeterminate_bar_animation_);
181 indeterminate_bar_animation_->Start();
182 }
183 }
184
185 } // namespace message_center
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698