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

Side by Side Diff: chrome/browser/views/browser_actions_container.cc

Issue 256032: Add an API to manipulate the browser action badge. (Closed)
Patch Set: meet halfway Created 11 years, 2 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) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 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/browser/views/browser_actions_container.h" 5 #include "chrome/browser/views/browser_actions_container.h"
6 6
7 #include "app/gfx/canvas.h" 7 #include "app/gfx/canvas.h"
8 #include "app/resource_bundle.h" 8 #include "app/resource_bundle.h"
9 #include "base/stl_util-inl.h" 9 #include "base/stl_util-inl.h"
10 #include "chrome/browser/extensions/extension_browser_event_router.h" 10 #include "chrome/browser/extensions/extension_browser_event_router.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 class BrowserActionImageView : public views::TextButton, 42 class BrowserActionImageView : public views::TextButton,
43 public views::ButtonListener, 43 public views::ButtonListener,
44 public ImageLoadingTracker::Observer, 44 public ImageLoadingTracker::Observer,
45 public NotificationObserver { 45 public NotificationObserver {
46 public: 46 public:
47 BrowserActionImageView(ExtensionAction* browser_action, 47 BrowserActionImageView(ExtensionAction* browser_action,
48 Extension* extension, 48 Extension* extension,
49 BrowserActionsContainer* panel); 49 BrowserActionsContainer* panel);
50 ~BrowserActionImageView(); 50 ~BrowserActionImageView();
51 51
52 ExtensionActionState* browser_action_state() { return browser_action_state_; }
53
52 // Overridden from views::ButtonListener: 54 // Overridden from views::ButtonListener:
53 virtual void ButtonPressed(views::Button* sender, const views::Event& event); 55 virtual void ButtonPressed(views::Button* sender, const views::Event& event);
54 56
55 // Overridden from ImageLoadingTracker. 57 // Overridden from ImageLoadingTracker.
56 virtual void OnImageLoaded(SkBitmap* image, size_t index); 58 virtual void OnImageLoaded(SkBitmap* image, size_t index);
57 59
58 // Overridden from NotificationObserver: 60 // Overridden from NotificationObserver:
59 virtual void Observe(NotificationType type, 61 virtual void Observe(NotificationType type,
60 const NotificationSource& source, 62 const NotificationSource& source,
61 const NotificationDetails& details); 63 const NotificationDetails& details);
62 64
63 private: 65 private:
64 // Called to update the display to match the browser action's state. 66 // Called to update the display to match the browser action's state.
65 void OnStateUpdated(); 67 void OnStateUpdated();
66 68
67 // Override painting to implement the badge.
68 virtual void Paint(gfx::Canvas* canvas);
69
70 // The browser action this view represents. The ExtensionAction is not owned 69 // The browser action this view represents. The ExtensionAction is not owned
71 // by this class. 70 // by this class.
72 ExtensionAction* browser_action_; 71 ExtensionAction* browser_action_;
73 72
74 // The state of our browser action. Not owned by this class. 73 // The state of our browser action. Not owned by this class.
75 ExtensionActionState* browser_action_state_; 74 ExtensionActionState* browser_action_state_;
76 75
77 // The icons representing different states for the browser action. 76 // The icons representing different states for the browser action.
78 std::vector<SkBitmap> browser_action_icons_; 77 std::vector<SkBitmap> browser_action_icons_;
79 78
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 OnStateUpdated(); 135 OnStateUpdated();
137 tracker_ = NULL; // The tracker object will delete itself when we return. 136 tracker_ = NULL; // The tracker object will delete itself when we return.
138 } 137 }
139 } 138 }
140 139
141 void BrowserActionImageView::OnStateUpdated() { 140 void BrowserActionImageView::OnStateUpdated() {
142 SkBitmap* image = &browser_action_icons_[browser_action_state_->icon_index()]; 141 SkBitmap* image = &browser_action_icons_[browser_action_state_->icon_index()];
143 SetIcon(*image); 142 SetIcon(*image);
144 SetTooltipText(ASCIIToWide(browser_action_state_->title())); 143 SetTooltipText(ASCIIToWide(browser_action_state_->title()));
145 panel_->OnBrowserActionVisibilityChanged(); 144 panel_->OnBrowserActionVisibilityChanged();
145 SchedulePaint();
146 } 146 }
147 147
148 void BrowserActionImageView::Observe(NotificationType type, 148 void BrowserActionImageView::Observe(NotificationType type,
149 const NotificationSource& source, 149 const NotificationSource& source,
150 const NotificationDetails& details) { 150 const NotificationDetails& details) {
151 if (type == NotificationType::EXTENSION_BROWSER_ACTION_UPDATED) { 151 if (type == NotificationType::EXTENSION_BROWSER_ACTION_UPDATED) {
152 OnStateUpdated(); 152 OnStateUpdated();
153 } else { 153 } else {
154 NOTREACHED() << L"Received unexpected notification"; 154 NOTREACHED() << L"Received unexpected notification";
155 } 155 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 gfx::Size BrowserActionsContainer::GetPreferredSize() { 224 gfx::Size BrowserActionsContainer::GetPreferredSize() {
225 if (browser_action_views_.empty()) 225 if (browser_action_views_.empty())
226 return gfx::Size(0, 0); 226 return gfx::Size(0, 0);
227 int width = kHorizontalPadding * 2 + 227 int width = kHorizontalPadding * 2 +
228 browser_action_views_.size() * kIconSize; 228 browser_action_views_.size() * kIconSize;
229 return gfx::Size(width, kIconSize); 229 return gfx::Size(width, kIconSize);
230 } 230 }
231 231
232 void BrowserActionsContainer::Layout() { 232 void BrowserActionsContainer::Layout() {
233 for (size_t i = 0; i < browser_action_views_.size(); ++i) { 233 for (size_t i = 0; i < browser_action_views_.size(); ++i) {
234 views::TextButton* view = browser_action_views_[i]; 234 BrowserActionImageView* view = browser_action_views_[i];
235 int x = kHorizontalPadding + i * kIconSize; 235 int x = kHorizontalPadding + i * kIconSize;
236 view->SetBounds(x, kControlVertOffset, kIconSize, 236 view->SetBounds(x, kControlVertOffset, kIconSize,
237 height() - (2 * kControlVertOffset)); 237 height() - (2 * kControlVertOffset));
238 } 238 }
239 } 239 }
240 240
241 void BrowserActionsContainer::Observe(NotificationType type, 241 void BrowserActionsContainer::Observe(NotificationType type,
242 const NotificationSource& source, 242 const NotificationSource& source,
243 const NotificationDetails& details) { 243 const NotificationDetails& details) {
244 if (type == NotificationType::EXTENSION_LOADED || 244 if (type == NotificationType::EXTENSION_LOADED ||
245 type == NotificationType::EXTENSION_UNLOADED || 245 type == NotificationType::EXTENSION_UNLOADED ||
246 type == NotificationType::EXTENSION_UNLOADED_DISABLED) { 246 type == NotificationType::EXTENSION_UNLOADED_DISABLED) {
247 RefreshBrowserActionViews(); 247 RefreshBrowserActionViews();
248 } else { 248 } else {
249 NOTREACHED() << L"Received unexpected notification"; 249 NOTREACHED() << L"Received unexpected notification";
250 } 250 }
251 } 251 }
252 252
253 void BrowserActionsContainer::PaintChildren(gfx::Canvas* canvas) { 253 void BrowserActionsContainer::PaintChildren(gfx::Canvas* canvas) {
254 View::PaintChildren(canvas); 254 View::PaintChildren(canvas);
255 255
256 // TODO(aa): Hook this up to the API to feed the badge color and text
257 // dynamically.
258 std::string text;
259 for (size_t i = 0; i < browser_action_views_.size(); ++i) { 256 for (size_t i = 0; i < browser_action_views_.size(); ++i) {
260 if (i > 0) { 257 BrowserActionImageView* view = browser_action_views_[i];
261 text += IntToString(i); 258 const std::string& text = view->browser_action_state()->badge_text();
262 PaintBadge(canvas, browser_action_views_[i], 259 SkColor* color = view->browser_action_state()->badge_background_color();
263 SkColorSetARGB(255, 218, 0, 24), text); 260
264 } 261 if (!text.empty())
262 PaintBadge(canvas, browser_action_views_[i], *color, text);
265 } 263 }
266 } 264 }
267 265
268 void BrowserActionsContainer::PaintBadge(gfx::Canvas* canvas, 266 void BrowserActionsContainer::PaintBadge(gfx::Canvas* canvas,
269 views::TextButton* view, 267 BrowserActionImageView* view,
270 const SkColor& badge_color, 268 const SkColor& badge_color,
271 const std::string& text) { 269 const std::string& text) {
272 const int kTextSize = 8; 270 const int kTextSize = 8;
273 const int kBottomMargin = 6; 271 const int kBottomMargin = 6;
274 const int kPadding = 2; 272 const int kPadding = 2;
275 const int kBadgeHeight = 11; 273 const int kBadgeHeight = 11;
276 const int kMaxTextWidth = 23; 274 const int kMaxTextWidth = 23;
277 const int kCenterAlignThreshold = 20; // at than width, we center align 275 const int kCenterAlignThreshold = 20; // at than width, we center align
278 276
279 canvas->save(); 277 canvas->save();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 // text was too large. 338 // text was too large.
341 rect.fLeft += kPadding; 339 rect.fLeft += kPadding;
342 rect.fRight -= kPadding; 340 rect.fRight -= kPadding;
343 canvas->clipRect(rect); 341 canvas->clipRect(rect);
344 canvas->drawText(text.c_str(), text.size(), 342 canvas->drawText(text.c_str(), text.size(),
345 rect.fLeft + (rect.width() - text_width) / 2, 343 rect.fLeft + (rect.width() - text_width) / 2,
346 rect.fTop + kTextSize + 1, 344 rect.fTop + kTextSize + 1,
347 text_paint); 345 text_paint);
348 canvas->restore(); 346 canvas->restore();
349 } 347 }
OLDNEW
« no previous file with comments | « chrome/browser/views/browser_actions_container.h ('k') | chrome/common/extensions/api/extension_api.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698