diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-19 21:08:30 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-19 21:08:30 +0000 |
commit | 0f5a3da9fbc074bbb9c63163866ba5d806919d0d (patch) | |
tree | 17520be691546c964cfe7a4d2aa1afbeb07be5fb /chrome | |
parent | 8b37ca19491d189fe6db3ca9a5a124722373f94e (diff) | |
download | chromium_src-0f5a3da9fbc074bbb9c63163866ba5d806919d0d.zip chromium_src-0f5a3da9fbc074bbb9c63163866ba5d806919d0d.tar.gz chromium_src-0f5a3da9fbc074bbb9c63163866ba5d806919d0d.tar.bz2 |
Implement badges for page actions. Also add badge text color API.
Also change color APIs from wanting ARGB to RGBA.
BUG=24635
BUG=24644
BUG=25215
Review URL: http://codereview.chromium.org/291003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29457 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
11 files changed, 67 insertions, 94 deletions
diff --git a/chrome/browser/extensions/extension_browser_actions_api.cc b/chrome/browser/extensions/extension_browser_actions_api.cc index 5cf68e8..6c83ee0 100644 --- a/chrome/browser/extensions/extension_browser_actions_api.cc +++ b/chrome/browser/extensions/extension_browser_actions_api.cc @@ -115,8 +115,8 @@ bool BrowserActionSetBadgeBackgroundColorFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); } - SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2], - color_array[3]); + SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], + color_array[2]); Extension* extension = dispatcher()->GetExtension(); if (!extension->browser_action()) { diff --git a/chrome/browser/extensions/extension_page_actions_module.cc b/chrome/browser/extensions/extension_page_actions_module.cc index 60eb86a..743e0a5 100644 --- a/chrome/browser/extensions/extension_page_actions_module.cc +++ b/chrome/browser/extensions/extension_page_actions_module.cc @@ -212,8 +212,8 @@ bool PageActionSetBadgeBackgroundColorFunction::RunImpl() { for (size_t i = 0; i < arraysize(color_array); ++i) EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); - SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2], - color_array[3]); + SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], + color_array[2]); state_->set_badge_background_color(color); contents_->PageActionStateChanged(); return true; @@ -236,7 +236,10 @@ bool PageActionSetBadgeTextColorFunction::RunImpl() { for (size_t i = 0; i < arraysize(color_array); ++i) EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); - // TODO(mpcomplete): implement text coloring. + SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], + color_array[2]); + state_->set_badge_text_color(color); + contents_->PageActionStateChanged(); return true; } diff --git a/chrome/browser/gtk/location_bar_view_gtk.cc b/chrome/browser/gtk/location_bar_view_gtk.cc index e36c8d3..3183ede 100644 --- a/chrome/browser/gtk/location_bar_view_gtk.cc +++ b/chrome/browser/gtk/location_bar_view_gtk.cc @@ -6,6 +6,7 @@ #include <string> +#include "app/gfx/canvas_paint.h" #include "app/gfx/gtk_util.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" @@ -684,6 +685,8 @@ LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box_.get()), FALSE); g_signal_connect(event_box_.get(), "button-press-event", G_CALLBACK(&OnButtonPressed), this); + g_signal_connect_after(event_box_.get(), "expose-event", + G_CALLBACK(OnExposeEvent), this); image_.Own(gtk_image_new()); gtk_container_add(GTK_CONTAINER(event_box_.get()), image_.get()); @@ -793,3 +796,20 @@ gboolean LocationBarViewGtk::PageActionViewGtk::OnButtonPressed( event->button); return true; } + +// static +gboolean LocationBarViewGtk::PageActionViewGtk::OnExposeEvent( + GtkWidget* widget, GdkEventExpose* event, PageActionViewGtk* view) { + TabContents* contents = view->owner_->browser_->GetSelectedTabContents(); + if (!contents) + return FALSE; + const ExtensionActionState* state = + contents->GetPageActionState(view->page_action_); + if (!state || state->badge_text().empty()) + return FALSE; + + gfx::CanvasPaint canvas(event, false); + gfx::Rect bounding_rect(widget->allocation); + state->PaintBadge(&canvas, bounding_rect); + return FALSE; +} diff --git a/chrome/browser/gtk/location_bar_view_gtk.h b/chrome/browser/gtk/location_bar_view_gtk.h index 32a5f9e..82f9b1e 100644 --- a/chrome/browser/gtk/location_bar_view_gtk.h +++ b/chrome/browser/gtk/location_bar_view_gtk.h @@ -120,6 +120,9 @@ class LocationBarViewGtk : public AutocompleteEditController, private: static gboolean OnButtonPressed(GtkWidget* sender, GdkEventButton* event, PageActionViewGtk* page_action_view); + static gboolean OnExposeEvent(GtkWidget* widget, + GdkEventExpose* event, + PageActionViewGtk* page_action_view); // The location bar view that owns us. LocationBarViewGtk* owner_; @@ -156,6 +159,7 @@ class LocationBarViewGtk : public AutocompleteEditController, DISALLOW_COPY_AND_ASSIGN(PageActionViewGtk); }; + friend class PageActionViewGtk; static gboolean HandleExposeThunk(GtkWidget* widget, GdkEventExpose* event, gpointer userdata) { diff --git a/chrome/browser/views/browser_actions_container.cc b/chrome/browser/views/browser_actions_container.cc index b4a2b77..1d4f825f 100644 --- a/chrome/browser/views/browser_actions_container.cc +++ b/chrome/browser/views/browser_actions_container.cc @@ -299,88 +299,8 @@ void BrowserActionView::Layout() { void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { View::PaintChildren(canvas); - - const std::string& text = button_->browser_action_state()->badge_text(); - if (text.empty()) - return; - - const int kTextSize = 8; - const int kBottomMargin = 5; - const int kPadding = 2; - const int kBadgeHeight = 11; - const int kMaxTextWidth = 23; - const int kCenterAlignThreshold = 20; // at than width, we center align - - canvas->save(); - - SkTypeface* typeface = SkTypeface::CreateFromName("Arial", SkTypeface::kBold); - SkPaint text_paint; - text_paint.setAntiAlias(true); - text_paint.setColor(SK_ColorWHITE); - text_paint.setFakeBoldText(true); - text_paint.setTextAlign(SkPaint::kLeft_Align); - text_paint.setTextSize(SkIntToScalar(kTextSize)); - text_paint.setTypeface(typeface); - - // Calculate text width. We clamp it to a max size. - SkScalar text_width = text_paint.measureText(text.c_str(), text.size()); - text_width = SkIntToScalar( - std::min(kMaxTextWidth, SkScalarFloor(text_width))); - - // Cacluate badge size. It is clamped to a min width just because it looks - // silly if it is too skinny. - int badge_width = SkScalarFloor(text_width) + kPadding * 2; - badge_width = std::max(kBadgeHeight, badge_width); - - // Paint the badge background color in the right location. It is usually - // right-aligned, but it can also be center-aligned if it is large. - SkRect rect; - rect.fBottom = SkIntToScalar(height() - kBottomMargin); - rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); - if (badge_width >= kCenterAlignThreshold) { - rect.fLeft = SkIntToScalar((width() - badge_width) / 2); - rect.fRight = rect.fLeft + SkIntToScalar(badge_width); - } else { - rect.fRight = SkIntToScalar(width()); - rect.fLeft = rect.fRight - badge_width; - } - - SkPaint rect_paint; - rect_paint.setStyle(SkPaint::kFill_Style); - rect_paint.setAntiAlias(true); - rect_paint.setColor( - button_->browser_action_state()->badge_background_color()); - canvas->drawRoundRect(rect, SkIntToScalar(2), SkIntToScalar(2), rect_paint); - - // Overlay the gradient. It is stretchy, so we do this in three parts. - ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); - SkBitmap* gradient_left = resource_bundle.GetBitmapNamed( - IDR_BROWSER_ACTION_BADGE_LEFT); - SkBitmap* gradient_right = resource_bundle.GetBitmapNamed( - IDR_BROWSER_ACTION_BADGE_RIGHT); - SkBitmap* gradient_center = resource_bundle.GetBitmapNamed( - IDR_BROWSER_ACTION_BADGE_CENTER); - - canvas->drawBitmap(*gradient_left, rect.fLeft, rect.fTop); - canvas->TileImageInt(*gradient_center, - SkScalarFloor(rect.fLeft) + gradient_left->width(), - SkScalarFloor(rect.fTop), - SkScalarFloor(rect.width()) - gradient_left->width() - - gradient_right->width(), - SkScalarFloor(rect.height())); - canvas->drawBitmap(*gradient_right, - rect.fRight - SkIntToScalar(gradient_right->width()), rect.fTop); - - // Finally, draw the text centered within the badge. We set a clip in case the - // text was too large. - rect.fLeft += kPadding; - rect.fRight -= kPadding; - canvas->clipRect(rect); - canvas->drawText(text.c_str(), text.size(), - rect.fLeft + (rect.width() - text_width) / 2, - rect.fTop + kTextSize + 1, - text_paint); - canvas->restore(); + button_->browser_action_state()->PaintBadge(canvas, + gfx::Rect(width(), height())); } diff --git a/chrome/browser/views/location_bar_view.cc b/chrome/browser/views/location_bar_view.cc index e5e4ece..663c81b 100644 --- a/chrome/browser/views/location_bar_view.cc +++ b/chrome/browser/views/location_bar_view.cc @@ -1172,6 +1172,18 @@ void LocationBarView::SecurityImageView::ShowInfoBubble() { SECURITY_INFO_BUBBLE_TEXT)); } +void LocationBarView::PageActionImageView::Paint(gfx::Canvas* canvas) { + LocationBarImageView::Paint(canvas); + + TabContents* contents = owner_->delegate_->GetTabContents(); + if (!contents) + return; + + const ExtensionActionState* state = + contents->GetPageActionState(page_action_); + state->PaintBadge(canvas, gfx::Rect(width(), height())); +} + // PageActionImageView---------------------------------------------------------- LocationBarView::PageActionImageView::PageActionImageView( diff --git a/chrome/browser/views/location_bar_view.h b/chrome/browser/views/location_bar_view.h index fb8b249..6290a99 100644 --- a/chrome/browser/views/location_bar_view.h +++ b/chrome/browser/views/location_bar_view.h @@ -356,6 +356,7 @@ class LocationBarView : public LocationBar, // Overridden from LocationBarImageView. virtual void ShowInfoBubble(); + virtual void Paint(gfx::Canvas* canvas); // Overridden from ImageLoadingTracker. virtual void OnImageLoaded(SkBitmap* image, size_t index); @@ -394,6 +395,7 @@ class LocationBarView : public LocationBar, DISALLOW_COPY_AND_ASSIGN(PageActionImageView); }; + friend class PageActionImageView; // Both Layout and OnChanged call into this. This updates the contents // of the 3 views: selected_keyword, keyword_hint and type_search_view. If diff --git a/chrome/common/extensions/extension_action.cc b/chrome/common/extensions/extension_action.cc index 13ee202..de15f17 100644 --- a/chrome/common/extensions/extension_action.cc +++ b/chrome/common/extensions/extension_action.cc @@ -20,7 +20,7 @@ ExtensionAction::~ExtensionAction() { } void ExtensionActionState::PaintBadge(gfx::Canvas* canvas, - const gfx::Rect& bounds) { + const gfx::Rect& bounds) const { const std::string& text = badge_text(); if (text.empty()) return; @@ -49,7 +49,7 @@ void ExtensionActionState::PaintBadge(gfx::Canvas* canvas, SkTypeface* typeface = SkTypeface::CreateFromName("Arial", SkTypeface::kBold); SkPaint text_paint; text_paint.setAntiAlias(true); - text_paint.setColor(SK_ColorWHITE); + text_paint.setColor(badge_text_color()); text_paint.setFakeBoldText(true); text_paint.setTextAlign(SkPaint::kLeft_Align); text_paint.setTextSize(SkIntToScalar(kTextSize)); diff --git a/chrome/common/extensions/extension_action.h b/chrome/common/extensions/extension_action.h index 6decdd4..b819edb 100644 --- a/chrome/common/extensions/extension_action.h +++ b/chrome/common/extensions/extension_action.h @@ -84,7 +84,8 @@ class ExtensionActionState { public: ExtensionActionState(std::string title, int icon_index) : hidden_(false), title_(title), icon_index_(icon_index), - badge_background_color_(SkColorSetARGB(255, 218, 0, 24)) { + badge_background_color_(SkColorSetARGB(255, 218, 0, 24)), + badge_text_color_(SK_ColorWHITE) { } const std::string& title() const { return title_; } @@ -102,6 +103,13 @@ class ExtensionActionState { badge_background_color_ = badge_background_color; } + SkColor badge_text_color() const { + return badge_text_color_; + } + void set_badge_text_color(SkColor badge_text_color) { + badge_text_color_ = badge_text_color; + } + int icon_index() const { return icon_index_; } void set_icon_index(int icon_index) { icon_index_ = icon_index; } @@ -111,7 +119,7 @@ class ExtensionActionState { bool hidden() const { return hidden_; } void set_hidden(bool hidden) { hidden_ = hidden; } - void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds); + void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds) const; private: // True if the action is in the hidden state. @@ -132,6 +140,9 @@ class ExtensionActionState { // The background color for the badge. SkColor badge_background_color_; + // The text color for the badge. + SkColor badge_text_color_; + DISALLOW_COPY_AND_ASSIGN(ExtensionActionState); }; diff --git a/chrome/test/data/extensions/samples/test_browser_action/background.html b/chrome/test/data/extensions/samples/test_browser_action/background.html index 8170e86..bcf5083 100644 --- a/chrome/test/data/extensions/samples/test_browser_action/background.html +++ b/chrome/test/data/extensions/samples/test_browser_action/background.html @@ -5,6 +5,7 @@ var clicks = 0; chrome.browserAction.onClicked.addListener(function() { chrome.browserAction.setIcon({iconIndex:clicks}); + chrome.browserAction.setBadgeText({text:""+i}); clicks++; // We only have 1 icon, but cycle through 3 icons to test the // out-of-bounds index bug. diff --git a/chrome/test/data/extensions/samples/test_page_action/background.html b/chrome/test/data/extensions/samples/test_page_action/background.html index 2c5685d..d8d9d78 100644 --- a/chrome/test/data/extensions/samples/test_page_action/background.html +++ b/chrome/test/data/extensions/samples/test_page_action/background.html @@ -14,12 +14,12 @@ chrome.pageAction.show(info.tabId); } else { chrome.pageAction.hide(info.tabId); - setTimeout(function() { chrome.pageAction.show(info.tabId); }, 1000); + setTimeout(function() { chrome.pageAction.show(info.tabId); }, 200); } chrome.pageAction.setTitle({title: "click:" + clicks, tabId: info.tabId}); chrome.pageAction.setBadgeTextColor({ tabId: info.tabId, - color: [0, 255, clicks * 50, 255] + color: [255, 255, clicks * 50, 255] }); chrome.pageAction.setBadgeBackgroundColor({ tabId: info.tabId, @@ -27,7 +27,7 @@ }); chrome.pageAction.setBadgeText({ tabId: info.tabId, - text: clicks + "" + text: i + "" }); // We only have 2 icons, but cycle through 3 icons to test the |