OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/toolbar/app_menu_icon_painter.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "chrome/grit/theme_resources.h" | |
10 #include "ui/base/theme_provider.h" | |
11 #include "ui/gfx/animation/multi_animation.h" | |
12 #include "ui/gfx/canvas.h" | |
13 #include "ui/gfx/geometry/rect.h" | |
14 #include "ui/gfx/image/image_skia.h" | |
15 | |
16 namespace { | |
17 | |
18 // The app menu icon is made up of this many bars stacked vertically. | |
19 const int kBarCount = 3; | |
20 | |
21 // |value| is the animation progress from 0 to 1. |index| is the index of the | |
22 // bar being drawn. This function returns a new progress value (from 0 to 1) | |
23 // such that bars appear staggered. | |
24 double GetStaggeredValue(double value, int index) { | |
25 // When animating the app menu icon's bars the bars are staggered by this | |
26 // factor. | |
27 const double kStaggerFactor = 0.15; | |
28 double maxStaggeredValue = 1.0 - (kBarCount - 1) * kStaggerFactor; | |
29 double staggeredValue = (value - kStaggerFactor * index) / maxStaggeredValue; | |
30 return std::min(1.0, std::max(0.0, staggeredValue)); | |
31 } | |
32 | |
33 } // namespace | |
34 | |
35 AppMenuIconPainter::AppMenuIconPainter(Delegate* delegate) | |
36 : delegate_(delegate), severity_(SEVERITY_NONE) {} | |
37 | |
38 AppMenuIconPainter::~AppMenuIconPainter() {} | |
39 | |
40 void AppMenuIconPainter::SetSeverity(Severity severity, bool animate) { | |
41 if (severity_ == severity) | |
42 return; | |
43 | |
44 severity_ = severity; | |
45 delegate_->ScheduleAppMenuIconPaint(); | |
46 animation_.reset(); | |
47 if (severity_ == SEVERITY_NONE || !animate) | |
48 return; | |
49 | |
50 gfx::MultiAnimation::Parts parts; | |
51 // Animate the bars left to right. | |
52 parts.push_back(gfx::MultiAnimation::Part(1300, gfx::Tween::LINEAR)); | |
53 // Fade out animation. | |
54 parts.push_back(gfx::MultiAnimation::Part(1000, gfx::Tween::EASE_IN)); | |
55 // Again, animate the bars left to right. | |
56 parts.push_back(gfx::MultiAnimation::Part(1300, gfx::Tween::LINEAR)); | |
57 | |
58 animation_.reset( | |
59 new gfx::MultiAnimation(parts, base::TimeDelta::FromMilliseconds(40))); | |
60 animation_->set_delegate(this); | |
61 animation_->set_continuous(false); | |
62 animation_->Start(); | |
63 } | |
64 | |
65 void AppMenuIconPainter::Paint(gfx::Canvas* canvas, | |
66 const ui::ThemeProvider* theme_provider, | |
67 const gfx::Rect& rect, | |
68 BezelType bezel_type) { | |
69 gfx::Point center = rect.CenterPoint(); | |
70 | |
71 // Bezel. | |
72 int resource_id = 0; | |
73 switch (bezel_type) { | |
74 case BEZEL_NONE: | |
75 break; | |
76 case BEZEL_HOVER: | |
77 resource_id = IDR_TOOLBAR_BEZEL_HOVER; | |
78 break; | |
79 case BEZEL_PRESSED: | |
80 resource_id = IDR_TOOLBAR_BEZEL_PRESSED; | |
81 break; | |
82 } | |
83 | |
84 if (resource_id) { | |
85 gfx::ImageSkia* image = theme_provider->GetImageSkiaNamed(resource_id); | |
86 canvas->DrawImageInt(*image, center.x() - image->width() / 2, | |
87 center.y() - image->height() / 2); | |
88 } | |
89 | |
90 // The bars with no color. | |
91 { | |
92 gfx::ImageSkia* image = theme_provider->GetImageSkiaNamed(IDR_TOOLS_BAR); | |
93 int x = center.x() - image->width() / 2; | |
94 int y = center.y() - image->height() * kBarCount / 2; | |
95 for (int i = 0; i < kBarCount; ++i) { | |
96 canvas->DrawImageInt(*image, x, y); | |
97 y += image->height(); | |
98 } | |
99 } | |
100 | |
101 // The bars with color based on severity. | |
102 int severity_image_id = GetCurrentSeverityImageID(); | |
103 if (severity_image_id) { | |
104 gfx::ImageSkia* image = | |
105 theme_provider->GetImageSkiaNamed(severity_image_id); | |
106 int x = center.x() - image->width() / 2; | |
107 int y = center.y() - image->height() * kBarCount / 2; | |
108 for (int i = 0; i < kBarCount; ++i) { | |
109 SkPaint paint; | |
110 int width = image->width(); | |
111 | |
112 if (animation_ && animation_->is_animating()) { | |
113 if (animation_->current_part_index() % 2 == 1) { | |
114 // Fade out. | |
115 int alpha = animation_->CurrentValueBetween(0xFF, 0); | |
116 if (alpha == 0) | |
117 continue; | |
118 paint.setAlpha(alpha); | |
119 } else { | |
120 // Stagger the widths. | |
121 width = image->width() * | |
122 GetStaggeredValue(animation_->GetCurrentValue(), i); | |
123 if (width == 0) | |
124 continue; | |
125 } | |
126 } | |
127 | |
128 canvas->DrawImageInt(*image, 0, 0, width, image->height(), x, y, width, | |
129 image->height(), false, paint); | |
130 y += image->height(); | |
131 } | |
132 } | |
133 | |
134 if (!badge_.isNull()) | |
135 canvas->DrawImageInt(badge_, 0, 0); | |
136 } | |
137 | |
138 void AppMenuIconPainter::AnimationProgressed(const gfx::Animation* animation) { | |
139 delegate_->ScheduleAppMenuIconPaint(); | |
140 } | |
141 | |
142 int AppMenuIconPainter::GetCurrentSeverityImageID() const { | |
143 switch (severity_) { | |
144 case SEVERITY_NONE: | |
145 return 0; | |
146 case SEVERITY_LOW: | |
147 return IDR_TOOLS_BAR_LOW; | |
148 case SEVERITY_MEDIUM: | |
149 return IDR_TOOLS_BAR_MEDIUM; | |
150 case SEVERITY_HIGH: | |
151 return IDR_TOOLS_BAR_HIGH; | |
152 } | |
153 NOTREACHED(); | |
154 return 0; | |
155 } | |
OLD | NEW |