OLD | NEW |
---|---|
(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 if (indeterminate_bar_animation_) | |
37 indeterminate_bar_animation_->Stop(); // Just in case | |
38 } | |
39 | |
40 gfx::Size NotificationProgressBar::GetPreferredSize() const { | |
41 gfx::Size pref_size(kProgressBarWidth, message_center::kProgressBarThickness); | |
42 gfx::Insets insets = GetInsets(); | |
43 pref_size.Enlarge(insets.width(), insets.height()); | |
44 return pref_size; | |
45 } | |
46 | |
47 void NotificationProgressBar::SetIndeterminate(bool is_indeterminate) { | |
48 if (is_indeterminate) { | |
49 if (!indeterminate_bar_animation_) { | |
50 // Initialize the animation object. | |
51 indeterminate_bar_animation_.reset( | |
52 new gfx::LinearAnimation(kAnimationFrameRateHz, this)); | |
53 indeterminate_bar_animation_->SetDuration(kAnimationDuration); | |
54 } | |
55 indeterminate_bar_animation_->Start(); | |
56 } else if (indeterminate_bar_animation_) { | |
57 indeterminate_bar_animation_->Stop(); | |
58 } | |
59 SchedulePaint(); | |
60 } | |
61 | |
62 void NotificationProgressBar::OnPaint(gfx::Canvas* canvas) { | |
63 gfx::Rect content_bounds = GetContentsBounds(); | |
64 | |
65 // Draw background. | |
66 SkPath background_path; | |
67 background_path.addRoundRect(gfx::RectToSkRect(content_bounds), | |
68 message_center::kProgressBarCornerRadius, | |
69 message_center::kProgressBarCornerRadius); | |
70 SkPaint background_paint; | |
71 background_paint.setStyle(SkPaint::kFill_Style); | |
72 background_paint.setFlags(SkPaint::kAntiAlias_Flag); | |
73 background_paint.setColor(message_center::kProgressBarBackgroundColor); | |
74 canvas->DrawPath(background_path, background_paint); | |
75 | |
76 // Draw slice. | |
77 SkPath slice_path; | |
78 if (indeterminate_bar_animation_ && | |
79 indeterminate_bar_animation_->is_animating()) { | |
80 double time = indeterminate_bar_animation_->GetCurrentValue(); | |
81 | |
82 double bar1_left; | |
83 double bar1_width; | |
84 double bar2_left; | |
85 double bar2_width; | |
86 if (time < 0.25) { | |
87 bar1_left = time / 2; | |
88 bar1_width = time * 1.5; | |
89 bar2_left = 0; | |
90 bar2_width = 0; | |
Jun Mukai
2015/11/16 08:29:31
Those values look exactly same as time < 0.5. What
yoshiki
2015/11/24 16:39:24
The bar2 is hidden if time < 0.5. It's intended.
Jun Mukai
2015/11/24 17:51:11
You don't address my question. I said this line 87
yoshiki
2015/11/25 15:14:28
Ah, I misunderstood. Your're right, and they were
| |
91 } else if (time < 0.5) { | |
92 bar1_left = time / 2; | |
93 bar1_width = time * 1.5; | |
94 bar2_left = 0; | |
95 bar2_width = 0; | |
96 } else if (time < 0.75) { | |
97 bar1_left = time * 3 - 1.25; | |
98 bar1_width = 0.75 - (time - 0.5) * 3; | |
99 bar2_left = 0; | |
100 bar2_width = time - 0.5; | |
101 } else { | |
102 bar1_left = 1; | |
103 bar1_width = 0; | |
104 bar2_left = (time - 0.75) * 4; | |
105 bar2_width = 0.25 - (time - 0.75); | |
106 } | |
107 | |
108 int bar1_x = static_cast<int>(content_bounds.width() * bar1_left); | |
109 int bar1_w = | |
110 std::min(static_cast<int>(content_bounds.width() * bar1_width + 0.5), | |
111 content_bounds.width() - bar1_x); | |
112 int bar2_x = static_cast<int>(content_bounds.width() * bar2_left); | |
113 int bar2_w = | |
114 std::min(static_cast<int>(content_bounds.width() * bar2_width + 0.5), | |
115 content_bounds.width() - bar2_x); | |
116 | |
117 | |
118 gfx::Rect slice_bounds = content_bounds; | |
119 slice_bounds.set_x(content_bounds.x() + bar1_x); | |
120 slice_bounds.set_width(bar1_w); | |
121 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds), | |
122 message_center::kProgressBarCornerRadius, | |
123 message_center::kProgressBarCornerRadius); | |
124 slice_bounds.set_x(content_bounds.x() + bar2_x); | |
125 slice_bounds.set_width(bar2_w); | |
126 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds), | |
127 message_center::kProgressBarCornerRadius, | |
128 message_center::kProgressBarCornerRadius); | |
129 } else { | |
130 const int slice_width = | |
131 static_cast<int>(content_bounds.width() * GetNormalizedValue() + 0.5); | |
132 if (slice_width < 1) | |
133 return; | |
134 | |
135 gfx::Rect slice_bounds = content_bounds; | |
136 slice_bounds.set_width(slice_width); | |
137 slice_path.addRoundRect(gfx::RectToSkRect(slice_bounds), | |
138 message_center::kProgressBarCornerRadius, | |
139 message_center::kProgressBarCornerRadius); | |
140 } | |
141 SkPaint slice_paint; | |
142 slice_paint.setStyle(SkPaint::kFill_Style); | |
143 slice_paint.setFlags(SkPaint::kAntiAlias_Flag); | |
144 slice_paint.setColor(message_center::kProgressBarSliceColor); | |
145 canvas->DrawPath(slice_path, slice_paint); | |
146 } | |
147 | |
148 void NotificationProgressBar::AnimationProgressed( | |
149 const gfx::Animation* animation) { | |
150 if (animation == indeterminate_bar_animation_.get()) { | |
151 DCHECK(indeterminate_bar_animation_); | |
152 SchedulePaint(); | |
153 } | |
154 } | |
155 | |
156 void NotificationProgressBar::AnimationEnded(const gfx::Animation* animation) { | |
157 // Restarts animation. | |
158 if (animation == indeterminate_bar_animation_.get()) { | |
159 DCHECK(indeterminate_bar_animation_); | |
160 indeterminate_bar_animation_->Start(); | |
161 } | |
162 } | |
163 | |
164 } // namespace message_center | |
OLD | NEW |