ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / wrench_icon_painter.cc
blobbdf54c35ac96e192cb8e43fc3780008b47cd9f9c
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 "chrome/browser/ui/toolbar/wrench_icon_painter.h"
7 #include <algorithm>
9 #include "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"
16 namespace {
18 // The wrench icon is made up of this many bars stacked vertically.
19 const int kBarCount = 3;
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 wrench 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));
33 } // namespace
35 WrenchIconPainter::WrenchIconPainter(Delegate* delegate)
36 : delegate_(delegate),
37 severity_(SEVERITY_NONE) {
40 WrenchIconPainter::~WrenchIconPainter() {}
42 void WrenchIconPainter::SetSeverity(Severity severity, bool animate) {
43 if (severity_ == severity)
44 return;
46 severity_ = severity;
47 delegate_->ScheduleWrenchIconPaint();
48 animation_.reset();
49 if (severity_ == SEVERITY_NONE || !animate)
50 return;
52 gfx::MultiAnimation::Parts parts;
53 // Animate the bars left to right.
54 parts.push_back(gfx::MultiAnimation::Part(1300, gfx::Tween::LINEAR));
55 // Fade out animation.
56 parts.push_back(gfx::MultiAnimation::Part(1000, gfx::Tween::EASE_IN));
57 // Again, animate the bars left to right.
58 parts.push_back(gfx::MultiAnimation::Part(1300, gfx::Tween::LINEAR));
60 animation_.reset(
61 new gfx::MultiAnimation(parts, base::TimeDelta::FromMilliseconds(40)));
62 animation_->set_delegate(this);
63 animation_->set_continuous(false);
64 animation_->Start();
67 void WrenchIconPainter::Paint(gfx::Canvas* canvas,
68 ui::ThemeProvider* theme_provider,
69 const gfx::Rect& rect,
70 BezelType bezel_type) {
71 gfx::Point center = rect.CenterPoint();
73 // Bezel.
74 int resource_id = 0;
75 switch (bezel_type) {
76 case BEZEL_NONE:
77 break;
78 case BEZEL_HOVER:
79 resource_id = IDR_TOOLBAR_BEZEL_HOVER;
80 break;
81 case BEZEL_PRESSED:
82 resource_id = IDR_TOOLBAR_BEZEL_PRESSED;
83 break;
84 case BEZEL_RAISED:
85 resource_id = IDR_TOOLBAR_BEZEL_RAISED;
86 break;
89 if (resource_id) {
90 gfx::ImageSkia* image = theme_provider->GetImageSkiaNamed(resource_id);
91 canvas->DrawImageInt(*image,
92 center.x() - image->width() / 2,
93 center.y() - image->height() / 2);
96 // The bars with no color.
98 gfx::ImageSkia* image = theme_provider->GetImageSkiaNamed(IDR_TOOLS_BAR);
99 int x = center.x() - image->width() / 2;
100 int y = center.y() - image->height() * kBarCount / 2;
101 for (int i = 0; i < kBarCount; ++i) {
102 canvas->DrawImageInt(*image, x, y);
103 y += image->height();
107 // The bars with color based on severity.
108 int severity_image_id = GetCurrentSeverityImageID();
109 if (severity_image_id) {
110 gfx::ImageSkia* image =
111 theme_provider->GetImageSkiaNamed(severity_image_id);
112 int x = center.x() - image->width() / 2;
113 int y = center.y() - image->height() * kBarCount / 2;
114 for (int i = 0; i < kBarCount; ++i) {
115 SkPaint paint;
116 int width = image->width();
118 if (animation_ && animation_->is_animating()) {
119 if (animation_->current_part_index() % 2 == 1) {
120 // Fade out.
121 int alpha = animation_->CurrentValueBetween(0xFF, 0);
122 if (alpha == 0)
123 continue;
124 paint.setAlpha(alpha);
125 } else {
126 // Stagger the widths.
127 width = image->width() *
128 GetStaggeredValue(animation_->GetCurrentValue(), i);
129 if (width == 0)
130 continue;
134 canvas->DrawImageInt(*image, 0, 0, width, image->height(),
135 x, y, width, image->height(), false, paint);
136 y += image->height();
140 if (!badge_.isNull())
141 canvas->DrawImageInt(badge_, 0, 0);
144 void WrenchIconPainter::AnimationProgressed(const gfx::Animation* animation) {
145 delegate_->ScheduleWrenchIconPaint();
148 int WrenchIconPainter::GetCurrentSeverityImageID() const {
149 switch (severity_) {
150 case SEVERITY_NONE:
151 return 0;
152 case SEVERITY_LOW:
153 return IDR_TOOLS_BAR_LOW;
154 case SEVERITY_MEDIUM:
155 return IDR_TOOLS_BAR_MEDIUM;
156 case SEVERITY_HIGH:
157 return IDR_TOOLS_BAR_HIGH;
159 NOTREACHED();
160 return 0;