diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-03 19:39:24 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-03 19:39:24 +0000 |
commit | 5132483c2e2775f0e6bcc249e1438cf88dac97a2 (patch) | |
tree | efa309ebfc998bec325fcfafa0663ec53982ae58 /chrome | |
parent | e8bce8f868f0ffbd8d608461eb938ee4cc4c0020 (diff) | |
download | chromium_src-5132483c2e2775f0e6bcc249e1438cf88dac97a2.zip chromium_src-5132483c2e2775f0e6bcc249e1438cf88dac97a2.tar.gz chromium_src-5132483c2e2775f0e6bcc249e1438cf88dac97a2.tar.bz2 |
Add an API to manipulate the browser action badge.
BUG=23268
Review URL: http://codereview.chromium.org/256032
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27951 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
8 files changed, 138 insertions, 20 deletions
diff --git a/chrome/browser/extensions/extension_browser_actions_api.cc b/chrome/browser/extensions/extension_browser_actions_api.cc index 6087d14..d80637d 100755 --- a/chrome/browser/extensions/extension_browser_actions_api.cc +++ b/chrome/browser/extensions/extension_browser_actions_api.cc @@ -12,6 +12,9 @@ namespace extension_browser_actions_api_constants { const char kSetNameFunction[] = "browserAction.setName"; const char kSetIconFunction[] = "browserAction.setIcon"; +const char kSetBadgeTextFunction[] = "browserAction.setBadgeText"; +const char kSetBadgeBackgroundColorFunction[] = + "browserAction.setBadgeBackgroundColor"; } // namespace extension_browser_actions_api_constants @@ -58,3 +61,50 @@ bool BrowserActionSetIconFunction::RunImpl() { Details<ExtensionActionState>(extension->browser_action_state())); return true; } + +bool BrowserActionSetBadgeTextFunction::RunImpl() { + std::string badge_text; + EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&badge_text)); + + Extension* extension = dispatcher()->GetExtension(); + if (!extension->browser_action()) { + error_ = kNoBrowserActionError; + return false; + } + + extension->browser_action_state()->set_badge_text(badge_text); + + NotificationService::current()->Notify( + NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, + Source<ExtensionAction>(extension->browser_action()), + Details<ExtensionActionState>(extension->browser_action_state())); + return true; +} + +bool BrowserActionSetBadgeBackgroundColorFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); + ListValue* list = static_cast<ListValue*>(args_); + EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); + + int color_array[4] = {0}; + for (size_t i = 0; i < arraysize(color_array); ++i) { + EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); + } + + SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2], + color_array[3]); + + Extension* extension = dispatcher()->GetExtension(); + if (!extension->browser_action()) { + error_ = kNoBrowserActionError; + return false; + } + + extension->browser_action_state()->set_badge_background_color(color); + + NotificationService::current()->Notify( + NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, + Source<ExtensionAction>(extension->browser_action()), + Details<ExtensionActionState>(extension->browser_action_state())); + return true; +} diff --git a/chrome/browser/extensions/extension_browser_actions_api.h b/chrome/browser/extensions/extension_browser_actions_api.h index 91f2eb0..e08baa4 100755 --- a/chrome/browser/extensions/extension_browser_actions_api.h +++ b/chrome/browser/extensions/extension_browser_actions_api.h @@ -15,11 +15,22 @@ class BrowserActionSetIconFunction : public SyncExtensionFunction { virtual bool RunImpl(); }; +class BrowserActionSetBadgeTextFunction : public SyncExtensionFunction { + virtual bool RunImpl(); +}; + +class BrowserActionSetBadgeBackgroundColorFunction + : public SyncExtensionFunction { + virtual bool RunImpl(); +}; + namespace extension_browser_actions_api_constants { // Function names. extern const char kSetNameFunction[]; extern const char kSetIconFunction[]; +extern const char kSetBadgeTextFunction[]; +extern const char kSetBadgeBackgroundColorFunction[]; }; // namespace extension_browser_actions_api_constants diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index df2bf0d..eb3a92e 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -130,6 +130,10 @@ void FactoryRegistry::ResetFunctions() { &NewExtensionFunction<BrowserActionSetNameFunction>; factories_[browser_actions::kSetIconFunction] = &NewExtensionFunction<BrowserActionSetIconFunction>; + factories_[browser_actions::kSetBadgeTextFunction] = + &NewExtensionFunction<BrowserActionSetBadgeTextFunction>; + factories_[browser_actions::kSetBadgeBackgroundColorFunction] = + &NewExtensionFunction<BrowserActionSetBadgeBackgroundColorFunction>; // Bookmarks. factories_[bookmarks::kGetBookmarksFunction] = diff --git a/chrome/browser/views/browser_actions_container.cc b/chrome/browser/views/browser_actions_container.cc index 73981c29..2fba91b 100644 --- a/chrome/browser/views/browser_actions_container.cc +++ b/chrome/browser/views/browser_actions_container.cc @@ -51,6 +51,8 @@ class BrowserActionImageView : public views::MenuButton, BrowserActionsContainer* panel); ~BrowserActionImageView(); + ExtensionActionState* browser_action_state() { return browser_action_state_; } + // Overridden from views::ButtonListener: virtual void ButtonPressed(views::Button* sender, const views::Event& event); @@ -162,6 +164,7 @@ void BrowserActionImageView::OnStateUpdated() { SetIcon(*image); SetTooltipText(ASCIIToWide(browser_action_state_->title())); panel_->OnBrowserActionVisibilityChanged(); + SchedulePaint(); } void BrowserActionImageView::Observe(NotificationType type, @@ -358,7 +361,7 @@ gfx::Size BrowserActionsContainer::GetPreferredSize() { void BrowserActionsContainer::Layout() { for (size_t i = 0; i < browser_action_views_.size(); ++i) { - views::TextButton* view = browser_action_views_[i]; + BrowserActionImageView* view = browser_action_views_[i]; int x = kHorizontalPadding + i * kIconSize; view->SetBounds(x, kControlVertOffset, kIconSize, height() - (2 * kControlVertOffset)); @@ -400,20 +403,18 @@ void BrowserActionsContainer::BubbleLostFocus(BrowserBubble* bubble) { void BrowserActionsContainer::PaintChildren(gfx::Canvas* canvas) { View::PaintChildren(canvas); - // TODO(aa): Hook this up to the API to feed the badge color and text - // dynamically. - std::string text; for (size_t i = 0; i < browser_action_views_.size(); ++i) { - if (i > 0) { - text += IntToString(i); - PaintBadge(canvas, browser_action_views_[i], - SkColorSetARGB(255, 218, 0, 24), text); - } + BrowserActionImageView* view = browser_action_views_[i]; + const std::string& text = view->browser_action_state()->badge_text(); + SkColor* color = view->browser_action_state()->badge_background_color(); + + if (!text.empty()) + PaintBadge(canvas, browser_action_views_[i], *color, text); } } void BrowserActionsContainer::PaintBadge(gfx::Canvas* canvas, - views::TextButton* view, + BrowserActionImageView* view, const SkColor& badge_color, const std::string& text) { const int kTextSize = 8; diff --git a/chrome/browser/views/browser_actions_container.h b/chrome/browser/views/browser_actions_container.h index b0aebec..dc77d38 100644 --- a/chrome/browser/views/browser_actions_container.h +++ b/chrome/browser/views/browser_actions_container.h @@ -18,10 +18,6 @@ class ExtensionAction; class ExtensionPopup; class Profile; class ToolbarView; -namespace views { -class MenuButton; -class TextButton; -} //////////////////////////////////////////////////////////////////////////////// // @@ -75,12 +71,12 @@ class BrowserActionsContainer : public views::View, virtual void PaintChildren(gfx::Canvas* canvas); // Paints an individual badge. - virtual void PaintBadge(gfx::Canvas* canvas, views::TextButton* button, + virtual void PaintBadge(gfx::Canvas* canvas, BrowserActionImageView* button, const SkColor& badge_color, const std::string& text); // The vector of browser actions (icons/image buttons for each action). - std::vector<views::MenuButton*> browser_action_views_; + std::vector<BrowserActionImageView*> browser_action_views_; NotificationRegistrar registrar_; diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json index 357944c..1a8035b 100755 --- a/chrome/common/extensions/api/extension_api.json +++ b/chrome/common/extensions/api/extension_api.json @@ -809,7 +809,7 @@ { "name": "setName", "type": "function", - "description": "Sets the extension's browser action name.", + "description": "Sets the text for the browser action. Shows up in the tooltip if the browser action is visible, and in the menu item.", "parameters": [ {"type": "string", "name": "name", "description": "The string the browser action should display when moused over.", optional: false} ] @@ -817,10 +817,37 @@ { "name": "setIcon", "type": "function", - "description": "Sets icon", + "description": "Sets the icon for the browser action. Can be up to about 22px square.", "parameters": [ {"type": "integer", "name": "iconId", "minimum": 0, "optional": true, "description": "A zero-based index into the |icons| vector specified in the manifest. This id is useful to represent different browser action states. Example: A GMail checker could have a 'new email' icon and a 'no unread email' icon."} ] + }, + { + "name": "setBadgeText", + "type": "function", + "description": "Sets the badge text for the browser action. This is printed on top of the icon.", + "parameters": [ + {"type": "string", "name": "text", "description": "Any number of characters can be passed, but only about four can fit in the space."} + ] + }, + { + "name": "setBadgeBackgroundColor", + "type": "function", + "description": "Sets the background color for the badge.", + "parameters": [ + { + "type": "array", + "name": "color", + "description": "An array of four integers in the range [0,255] that make up the ARGB color for the bakground of the badge.", + "items": { + "type": "integer", + "minimum": 0, + "maximum": 255 + }, + "minItems": 4, + "maxItems": 4 + } + ] } ], "events": [ diff --git a/chrome/common/extensions/extension_action.h b/chrome/common/extensions/extension_action.h index 630671c..70ea093 100644 --- a/chrome/common/extensions/extension_action.h +++ b/chrome/common/extensions/extension_action.h @@ -10,7 +10,9 @@ #include <vector> #include "base/basictypes.h" +#include "base/scoped_ptr.h" #include "googleurl/src/gurl.h" +#include "third_party/skia/include/core/SkColor.h" class ExtensionAction { public: @@ -85,12 +87,25 @@ typedef std::map<std::string, ExtensionAction*> ExtensionActionMap; class ExtensionActionState { public: ExtensionActionState(std::string title, int icon_index) - : title_(title), icon_index_(icon_index) { + : title_(title), icon_index_(icon_index), + badge_background_color_(new SkColor(SkColorSetARGB(255, 218, 0, 24))) { } - std::string title() const { return title_; } + const std::string& title() const { return title_; } void set_title(const std::string& title) { title_ = title; } + const std::string& badge_text() const { return badge_text_; } + void set_badge_text(const std::string& badge_text) { + badge_text_ = badge_text; + } + + SkColor* badge_background_color() const { + return badge_background_color_.get(); + } + void set_badge_background_color(const SkColor& badge_background_color) { + badge_background_color_.reset(new SkColor(badge_background_color)); + } + int icon_index() const { return icon_index_; } void set_icon_index(int icon_index) { icon_index_ = icon_index; } @@ -101,6 +116,12 @@ class ExtensionActionState { // The icon to use. int icon_index_; + // The badge text. + std::string badge_text_; + + // The background color for the badge. + scoped_ptr<SkColor> badge_background_color_; + DISALLOW_COPY_AND_ASSIGN(ExtensionActionState); }; diff --git a/chrome/test/data/extensions/samples/make_page_red/background.html b/chrome/test/data/extensions/samples/make_page_red/background.html index 0843595..30631d2 100755 --- a/chrome/test/data/extensions/samples/make_page_red/background.html +++ b/chrome/test/data/extensions/samples/make_page_red/background.html @@ -5,6 +5,14 @@ chrome.browserAction.onClicked.addListener(function(windowId) { chrome.tabs.executeScript(null, {code:"document.body.bgColor='red'"}); }); + + chrome.browserAction.setBadgeBackgroundColor([100, 0, 200, 0]); + + var i = 0; + window.setInterval(function() { + chrome.browserAction.setBadgeText(String(i)); + i++; + }, 10); </script> </head> </html> |