} // namespace
-// TODO(tbarzic): Merge AnimationIconImageSource and IconAnimation together.
-// Source for painting animated skia image.
-class AnimatedIconImageSource : public gfx::ImageSkiaSource {
- public:
- AnimatedIconImageSource(
- const gfx::ImageSkia& image,
- base::WeakPtr<ExtensionAction::IconAnimation> animation)
- : image_(image),
- animation_(animation) {
- }
-
- private:
- virtual ~AnimatedIconImageSource() {}
-
- virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
- gfx::ImageSkiaRep original_rep = image_.GetRepresentation(scale);
- if (!animation_.get())
- return original_rep;
-
- // Original representation's scale factor may be different from scale
- // factor passed to this method. We want to use the former (since we are
- // using bitmap for that scale).
- return gfx::ImageSkiaRep(
- animation_->Apply(original_rep.sk_bitmap()), original_rep.scale());
- }
-
- gfx::ImageSkia image_;
- base::WeakPtr<ExtensionAction::IconAnimation> animation_;
-
- DISALLOW_COPY_AND_ASSIGN(AnimatedIconImageSource);
-};
-
const int ExtensionAction::kDefaultTabId = -1;
-// 100ms animation at 50fps (so 5 animation frames in total).
-const int kIconFadeInDurationMs = 100;
-const int kIconFadeInFramesPerSecond = 50;
-
-ExtensionAction::IconAnimation::IconAnimation()
- : gfx::LinearAnimation(kIconFadeInDurationMs, kIconFadeInFramesPerSecond,
- NULL),
- weak_ptr_factory_(this) {}
-
-ExtensionAction::IconAnimation::~IconAnimation() {
- // Make sure observers don't access *this after its destructor has started.
- weak_ptr_factory_.InvalidateWeakPtrs();
- // In case the animation was destroyed before it finished (likely due to
- // delays in timer scheduling), make sure it's fully visible.
- FOR_EACH_OBSERVER(Observer, observers_, OnIconChanged());
-}
-
-const SkBitmap& ExtensionAction::IconAnimation::Apply(
- const SkBitmap& icon) const {
- DCHECK_GT(icon.width(), 0);
- DCHECK_GT(icon.height(), 0);
-
- if (!device_.get() ||
- (device_->width() != icon.width()) ||
- (device_->height() != icon.height())) {
- device_.reset(new SkBitmapDevice(
- SkBitmap::kARGB_8888_Config, icon.width(), icon.height(), true));
- }
-
- SkCanvas canvas(device_.get());
- canvas.clear(SK_ColorWHITE);
- SkPaint paint;
- paint.setAlpha(CurrentValueBetween(0, 255));
- canvas.drawBitmap(icon, 0, 0, &paint);
- return device_->accessBitmap(false);
-}
-
-base::WeakPtr<ExtensionAction::IconAnimation>
-ExtensionAction::IconAnimation::AsWeakPtr() {
- return weak_ptr_factory_.GetWeakPtr();
-}
-
-void ExtensionAction::IconAnimation::AddObserver(
- ExtensionAction::IconAnimation::Observer* observer) {
- observers_.AddObserver(observer);
-}
-
-void ExtensionAction::IconAnimation::RemoveObserver(
- ExtensionAction::IconAnimation::Observer* observer) {
- observers_.RemoveObserver(observer);
-}
-
-void ExtensionAction::IconAnimation::AnimateToState(double state) {
- FOR_EACH_OBSERVER(Observer, observers_, OnIconChanged());
-}
-
-ExtensionAction::IconAnimation::ScopedObserver::ScopedObserver(
- const base::WeakPtr<IconAnimation>& icon_animation,
- Observer* observer)
- : icon_animation_(icon_animation),
- observer_(observer) {
- if (icon_animation.get())
- icon_animation->AddObserver(observer);
-}
-
-ExtensionAction::IconAnimation::ScopedObserver::~ScopedObserver() {
- if (icon_animation_.get())
- icon_animation_->RemoveObserver(observer_);
-}
ExtensionAction::ExtensionAction(
const std::string& extension_id,
has_changed_(false) {
// Page/script actions are hidden/disabled by default, and browser actions are
// visible/enabled by default.
- SetAppearance(kDefaultTabId,
- action_type == extensions::ActionInfo::TYPE_BROWSER ?
- ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE);
+ SetIsVisible(kDefaultTabId,
+ action_type == extensions::ActionInfo::TYPE_BROWSER);
SetTitle(kDefaultTabId, manifest_data.default_title);
SetPopupUrl(kDefaultTabId, manifest_data.default_popup_url);
if (!manifest_data.default_icon.empty()) {
copy->badge_text_ = badge_text_;
copy->badge_background_color_ = badge_background_color_;
copy->badge_text_color_ = badge_text_color_;
- copy->appearance_ = appearance_;
- copy->icon_animation_ = icon_animation_;
+ copy->is_visible_ = is_visible_;
copy->id_ = id_;
if (default_icon_)
SetValue(&icon_, tab_id, image.AsImageSkia());
}
-gfx::Image ExtensionAction::ApplyAttentionAndAnimation(
- const gfx::ImageSkia& original_icon,
- int tab_id) const {
- gfx::ImageSkia icon = original_icon;
- if (GetValue(&appearance_, tab_id) == WANTS_ATTENTION)
- icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size());
-
- return gfx::Image(ApplyIconAnimation(tab_id, icon));
-}
-
gfx::ImageSkia ExtensionAction::GetExplicitlySetIcon(int tab_id) const {
return GetValue(&icon_, tab_id);
}
-bool ExtensionAction::SetAppearance(int tab_id, Appearance new_appearance) {
- const Appearance old_appearance = GetValue(&appearance_, tab_id);
+bool ExtensionAction::SetIsVisible(int tab_id, bool new_visibility) {
+ const bool old_visibility = GetValue(&is_visible_, tab_id);
- if (old_appearance == new_appearance)
+ if (old_visibility == new_visibility)
return false;
- SetValue(&appearance_, tab_id, new_appearance);
+ SetValue(&is_visible_, tab_id, new_visibility);
return true;
}
badge_text_.erase(tab_id);
badge_text_color_.erase(tab_id);
badge_background_color_.erase(tab_id);
- appearance_.erase(tab_id);
+ is_visible_.erase(tab_id);
// TODO(jyasskin): Erase the element from declarative_show_count_
// when the tab's closed. There's a race between the
// PageActionController and the ContentRulesRegistry on navigation,
// which prevents me from cleaning everything up now.
- icon_animation_.erase(tab_id);
}
void ExtensionAction::PaintBadge(gfx::Canvas* canvas,
return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_EXTENSIONS_FAVICON).ToImageSkia()->width();
}
-
-base::WeakPtr<ExtensionAction::IconAnimation> ExtensionAction::GetIconAnimation(
- int tab_id) const {
- std::map<int, base::WeakPtr<IconAnimation> >::iterator it =
- icon_animation_.find(tab_id);
- if (it == icon_animation_.end())
- return base::WeakPtr<ExtensionAction::IconAnimation>();
- if (it->second.get())
- return it->second;
-
- // Take this opportunity to remove all the NULL IconAnimations from
- // icon_animation_.
- icon_animation_.erase(it);
- for (it = icon_animation_.begin(); it != icon_animation_.end();) {
- if (it->second.get()) {
- ++it;
- } else {
- // The WeakPtr is null; remove it from the map.
- icon_animation_.erase(it++);
- }
- }
- return base::WeakPtr<ExtensionAction::IconAnimation>();
-}
-
-gfx::ImageSkia ExtensionAction::ApplyIconAnimation(
- int tab_id,
- const gfx::ImageSkia& icon) const {
- base::WeakPtr<IconAnimation> animation = GetIconAnimation(tab_id);
- if (animation.get() == NULL)
- return icon;
-
- return gfx::ImageSkia(new AnimatedIconImageSource(icon, animation),
- icon.size());
-}