Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: chrome/common/extensions/extension_action.cc

Issue 10559054: Animate the script badges. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: move to extension action Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/extensions/extension_action.h" 5 #include "chrome/common/extensions/extension_action.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "chrome/common/badge_util.h" 10 #include "chrome/common/badge_util.h"
11 #include "googleurl/src/gurl.h" 11 #include "googleurl/src/gurl.h"
12 #include "grit/ui_resources.h" 12 #include "grit/ui_resources.h"
13 #include "third_party/skia/include/core/SkBitmap.h" 13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "third_party/skia/include/core/SkDevice.h"
16 #include "third_party/skia/include/core/SkPaint.h"
14 #include "third_party/skia/include/effects/SkGradientShader.h" 17 #include "third_party/skia/include/effects/SkGradientShader.h"
18 #include "ui/base/animation/animation_delegate.h"
15 #include "ui/base/resource/resource_bundle.h" 19 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/canvas.h" 20 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/rect.h" 21 #include "ui/gfx/rect.h"
18 22
19 namespace { 23 namespace {
20 24
21 // Different platforms need slightly different constants to look good. 25 // Different platforms need slightly different constants to look good.
22 #if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS) 26 #if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
23 const float kTextSize = 9.0; 27 const float kTextSize = 9.0;
24 const int kBottomMargin = 0; 28 const int kBottomMargin = 0;
(...skipping 20 matching lines...) Expand all
45 const int kBadgeHeight = 11; 49 const int kBadgeHeight = 11;
46 const int kMaxTextWidth = 23; 50 const int kMaxTextWidth = 23;
47 // The minimum width for center-aligning the badge. 51 // The minimum width for center-aligning the badge.
48 const int kCenterAlignThreshold = 20; 52 const int kCenterAlignThreshold = 20;
49 53
50 54
51 } // namespace 55 } // namespace
52 56
53 const int ExtensionAction::kDefaultTabId = -1; 57 const int ExtensionAction::kDefaultTabId = -1;
54 58
59 ExtensionAction::IconAnimation::IconAnimation(
60 ui::AnimationDelegate* delegate)
61 // 100ms animation at 50fps (so 5 animation frames in total).
62 : ui::LinearAnimation(100, 50, delegate) {}
63
64 ExtensionAction::IconAnimation::~IconAnimation() {}
65
66 const SkBitmap& ExtensionAction::IconAnimation::Apply(
67 const SkBitmap& icon) const {
68 DCHECK_GT(icon.width(), 0);
69 DCHECK_GT(icon.height(), 0);
70
71 if (!device_.get() ||
72 (device_->width() != icon.width()) ||
73 (device_->height() != icon.height())) {
74 device_.reset(new SkDevice(
75 SkBitmap::kARGB_8888_Config, icon.width(), icon.height(), true));
76 }
77
78 SkCanvas canvas(device_.get());
79 canvas.clear(SK_ColorWHITE);
80 SkPaint paint;
81 paint.setAlpha(CurrentValueBetween(0, 255));
82 canvas.drawBitmap(icon, 0, 0, &paint);
83 return device_->accessBitmap(false);
84 }
85
86 void ExtensionAction::IconAnimation::AddObserver(
87 ExtensionAction::IconAnimation::Observer* observer) {
88 observers_.AddObserver(observer);
89 }
90
91 void ExtensionAction::IconAnimation::RemoveObserver(
92 ExtensionAction::IconAnimation::Observer* observer) {
93 observers_.RemoveObserver(observer);
94 }
95
96 void ExtensionAction::IconAnimation::AnimateToState(double state) {
97 FOR_EACH_OBSERVER(Observer, observers_, OnIconChanged(*this));
98 }
99
55 ExtensionAction::ExtensionAction(const std::string& extension_id, 100 ExtensionAction::ExtensionAction(const std::string& extension_id,
56 Type action_type) 101 Type action_type)
57 : extension_id_(extension_id), 102 : extension_id_(extension_id),
58 action_type_(action_type) { 103 action_type_(action_type) {
59 } 104 }
60 105
61 ExtensionAction::~ExtensionAction() { 106 ExtensionAction::~ExtensionAction() {
62 } 107 }
63 108
64 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) { 109 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) {
(...skipping 23 matching lines...) Expand all
88 133
89 void ExtensionAction::SetIconIndex(int tab_id, int index) { 134 void ExtensionAction::SetIconIndex(int tab_id, int index) {
90 if (static_cast<size_t>(index) >= icon_paths_.size()) { 135 if (static_cast<size_t>(index) >= icon_paths_.size()) {
91 NOTREACHED(); 136 NOTREACHED();
92 return; 137 return;
93 } 138 }
94 SetValue(&icon_index_, tab_id, index); 139 SetValue(&icon_index_, tab_id, index);
95 } 140 }
96 141
97 void ExtensionAction::ClearAllValuesForTab(int tab_id) { 142 void ExtensionAction::ClearAllValuesForTab(int tab_id) {
143 popup_url_.erase(tab_id);
98 title_.erase(tab_id); 144 title_.erase(tab_id);
99 icon_.erase(tab_id); 145 icon_.erase(tab_id);
100 icon_index_.erase(tab_id); 146 icon_index_.erase(tab_id);
101 badge_text_.erase(tab_id); 147 badge_text_.erase(tab_id);
102 badge_text_color_.erase(tab_id); 148 badge_text_color_.erase(tab_id);
103 badge_background_color_.erase(tab_id); 149 badge_background_color_.erase(tab_id);
104 visible_.erase(tab_id); 150 visible_.erase(tab_id);
105 popup_url_.erase(tab_id); 151 icon_animation_.erase(tab_id);
106 } 152 }
107 153
108 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, 154 void ExtensionAction::PaintBadge(gfx::Canvas* canvas,
109 const gfx::Rect& bounds, 155 const gfx::Rect& bounds,
110 int tab_id) { 156 int tab_id) {
111 std::string text = GetBadgeText(tab_id); 157 std::string text = GetBadgeText(tab_id);
112 if (text.empty()) 158 if (text.empty())
113 return; 159 return;
114 160
115 SkColor text_color = GetBadgeTextColor(tab_id); 161 SkColor text_color = GetBadgeTextColor(tab_id);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 // text was too large. 232 // text was too large.
187 rect.fLeft += kPadding; 233 rect.fLeft += kPadding;
188 rect.fRight -= kPadding; 234 rect.fRight -= kPadding;
189 canvas->sk_canvas()->clipRect(rect); 235 canvas->sk_canvas()->clipRect(rect);
190 canvas->sk_canvas()->drawText(text.c_str(), text.size(), 236 canvas->sk_canvas()->drawText(text.c_str(), text.size(),
191 rect.fLeft + (rect.width() - text_width) / 2, 237 rect.fLeft + (rect.width() - text_width) / 2,
192 rect.fTop + kTextSize + kTopTextPadding, 238 rect.fTop + kTextSize + kTopTextPadding,
193 *text_paint); 239 *text_paint);
194 canvas->Restore(); 240 canvas->Restore();
195 } 241 }
242
243 // Wraps an IconAnimation and implements its ui::AnimationDelegate to erase the
244 // animation from a map when the animation ends or is cancelled, causing itself
245 // and its owned IconAnimation to be deleted.
246 class ExtensionAction::IconAnimationWrapper : public ui::AnimationDelegate {
247 public:
248 IconAnimationWrapper(ExtensionAction* owner, int tab_id)
249 : owner_(owner),
250 tab_id_(tab_id),
251 ALLOW_THIS_IN_INITIALIZER_LIST(animation_(this)) {}
252
253 virtual ~IconAnimationWrapper() {}
254
255 IconAnimation* animation() {
256 return &animation_;
257 }
258
259 private:
260 virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE {
261 Done();
262 }
263
264 virtual void AnimationCanceled(const ui::Animation* animation) OVERRIDE {
265 Done();
266 }
267
268 void Done() {
269 owner_->icon_animation_.erase(tab_id_);
270 // this will now have been deleted.
271 }
272
273 ExtensionAction* owner_;
274 int tab_id_;
275 IconAnimation animation_;
276 };
277
278 base::WeakPtr<ExtensionAction::IconAnimation> ExtensionAction::GetIconAnimation(
Yoyo Zhou 2012/06/26 18:38:15 nit: Group ExtensionAnimation functions together i
not at google - send to devlin 2012/06/27 06:26:54 I don't understand what this means; they are group
Yoyo Zhou 2012/06/27 06:28:25 Between ExtensionAction::PaintBadge and ExtensionA
279 int tab_id) const {
280 std::map<int, linked_ptr<IconAnimationWrapper> >::const_iterator it =
281 icon_animation_.find(tab_id);
282 return (it != icon_animation_.end()) ? it->second->animation()->AsWeakPtr()
283 : base::WeakPtr<IconAnimation>();
284 }
285
286 void ExtensionAction::RunIconAnimation(int tab_id) {
287 IconAnimationWrapper* icon_animation =
288 new IconAnimationWrapper(this, tab_id);
289 icon_animation_[tab_id] = make_linked_ptr(icon_animation);
290 icon_animation->animation()->Start();
291 }
OLDNEW
« chrome/common/extensions/extension_action.h ('K') | « chrome/common/extensions/extension_action.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698