This source file includes following definitions.
- GetStaggeredValue
- SeverityFromUpgradeLevel
- ShouldAnimateUpgradeLevel
- GlobalErrorSeverity
- severity_
- SetSeverity
- AnimationProgressed
- GetCurrentSeverityImageID
#include "chrome/browser/ui/toolbar/wrench_icon_painter.h"
#include "grit/theme_resources.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/animation/multi_animation.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/rect.h"
namespace {
const int kBarCount = 3;
double GetStaggeredValue(double value, int index) {
const double kStaggerFactor = 0.15;
double maxStaggeredValue = 1.0 - (kBarCount - 1) * kStaggerFactor;
double staggeredValue = (value - kStaggerFactor * index) / maxStaggeredValue;
return std::min(1.0, std::max(0.0, staggeredValue));
}
}
WrenchIconPainter::Severity WrenchIconPainter::SeverityFromUpgradeLevel(
UpgradeDetector::UpgradeNotificationAnnoyanceLevel level) {
switch (level) {
case UpgradeDetector::UPGRADE_ANNOYANCE_NONE:
return SEVERITY_NONE;
case UpgradeDetector::UPGRADE_ANNOYANCE_LOW:
return SEVERITY_LOW;
case UpgradeDetector::UPGRADE_ANNOYANCE_ELEVATED:
return SEVERITY_MEDIUM;
case UpgradeDetector::UPGRADE_ANNOYANCE_HIGH:
return SEVERITY_HIGH;
case UpgradeDetector::UPGRADE_ANNOYANCE_SEVERE:
return SEVERITY_HIGH;
case UpgradeDetector::UPGRADE_ANNOYANCE_CRITICAL:
return SEVERITY_HIGH;
}
NOTREACHED();
return SEVERITY_NONE;
}
bool WrenchIconPainter::ShouldAnimateUpgradeLevel(
UpgradeDetector::UpgradeNotificationAnnoyanceLevel level) {
bool should_animate = true;
if (level == UpgradeDetector::UPGRADE_ANNOYANCE_LOW) {
static bool should_animate_low_severity = true;
should_animate = should_animate_low_severity;
should_animate_low_severity = false;
}
return should_animate;
}
WrenchIconPainter::Severity WrenchIconPainter::GlobalErrorSeverity() {
return SEVERITY_MEDIUM;
}
WrenchIconPainter::WrenchIconPainter(Delegate* delegate)
: delegate_(delegate),
severity_(SEVERITY_NONE) {
}
WrenchIconPainter::~WrenchIconPainter() {}
void WrenchIconPainter::SetSeverity(Severity severity, bool animate) {
if (severity_ == severity)
return;
severity_ = severity;
delegate_->ScheduleWrenchIconPaint();
animation_.reset();
if (severity_ == SEVERITY_NONE || !animate)
return;
gfx::MultiAnimation::Parts parts;
parts.push_back(gfx::MultiAnimation::Part(1300, gfx::Tween::LINEAR));
parts.push_back(gfx::MultiAnimation::Part(1000, gfx::Tween::EASE_IN));
parts.push_back(gfx::MultiAnimation::Part(1300, gfx::Tween::LINEAR));
animation_.reset(
new gfx::MultiAnimation(parts, base::TimeDelta::FromMilliseconds(40)));
animation_->set_delegate(this);
animation_->set_continuous(false);
animation_->Start();
}
void WrenchIconPainter::Paint(gfx::Canvas* canvas,
ui::ThemeProvider* theme_provider,
const gfx::Rect& rect,
BezelType bezel_type) {
gfx::Point center = rect.CenterPoint();
if (bezel_type != BEZEL_NONE) {
gfx::ImageSkia* image = theme_provider->GetImageSkiaNamed(
bezel_type == BEZEL_HOVER ? IDR_TOOLBAR_BEZEL_HOVER
: IDR_TOOLBAR_BEZEL_PRESSED);
canvas->DrawImageInt(*image,
center.x() - image->width() / 2,
center.y() - image->height() / 2);
}
{
gfx::ImageSkia* image = theme_provider->GetImageSkiaNamed(IDR_TOOLS_BAR);
int x = center.x() - image->width() / 2;
int y = center.y() - image->height() * kBarCount / 2;
for (int i = 0; i < kBarCount; ++i) {
canvas->DrawImageInt(*image, x, y);
y += image->height();
}
}
int severity_image_id = GetCurrentSeverityImageID();
if (severity_image_id) {
gfx::ImageSkia* image =
theme_provider->GetImageSkiaNamed(severity_image_id);
int x = center.x() - image->width() / 2;
int y = center.y() - image->height() * kBarCount / 2;
for (int i = 0; i < kBarCount; ++i) {
SkPaint paint;
int width = image->width();
if (animation_ && animation_->is_animating()) {
if (animation_->current_part_index() % 2 == 1) {
int alpha = animation_->CurrentValueBetween(0xFF, 0);
if (alpha == 0)
continue;
paint.setAlpha(alpha);
} else {
width = image->width() *
GetStaggeredValue(animation_->GetCurrentValue(), i);
if (width == 0)
continue;
}
}
canvas->DrawImageInt(*image, 0, 0, width, image->height(),
x, y, width, image->height(), false, paint);
y += image->height();
}
}
if (!badge_.isNull())
canvas->DrawImageInt(badge_, 0, 0);
}
void WrenchIconPainter::AnimationProgressed(const gfx::Animation* animation) {
delegate_->ScheduleWrenchIconPaint();
}
int WrenchIconPainter::GetCurrentSeverityImageID() const {
switch (severity_) {
case SEVERITY_NONE:
return 0;
case SEVERITY_LOW:
return IDR_TOOLS_BAR_LOW;
case SEVERITY_MEDIUM:
return IDR_TOOLS_BAR_MEDIUM;
case SEVERITY_HIGH:
return IDR_TOOLS_BAR_HIGH;
}
NOTREACHED();
return 0;
}