diff options
11 files changed, 94 insertions, 67 deletions
diff --git a/chrome/browser/extensions/extension_browser_actions_api.cc b/chrome/browser/extensions/extension_browser_actions_api.cc index 6c83ee0..5cf68e8 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[3], color_array[0], color_array[1], - color_array[2]); + SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2], + color_array[3]); 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 743e0a5..60eb86a 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[3], color_array[0], color_array[1], - color_array[2]); + SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2], + color_array[3]); state_->set_badge_background_color(color); contents_->PageActionStateChanged(); return true; @@ -236,10 +236,7 @@ bool PageActionSetBadgeTextColorFunction::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[3], color_array[0], color_array[1], - color_array[2]); - state_->set_badge_text_color(color); - contents_->PageActionStateChanged(); + // TODO(mpcomplete): implement text coloring. return true; } diff --git a/chrome/browser/gtk/location_bar_view_gtk.cc b/chrome/browser/gtk/location_bar_view_gtk.cc index 3183ede..e36c8d3 100644 --- a/chrome/browser/gtk/location_bar_view_gtk.cc +++ b/chrome/browser/gtk/location_bar_view_gtk.cc @@ -6,7 +6,6 @@ #include <string> -#include "app/gfx/canvas_paint.h" #include "app/gfx/gtk_util.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" @@ -685,8 +684,6 @@ 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()); @@ -796,20 +793,3 @@ 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 82f9b1e..32a5f9e 100644 --- a/chrome/browser/gtk/location_bar_view_gtk.h +++ b/chrome/browser/gtk/location_bar_view_gtk.h @@ -120,9 +120,6 @@ 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_; @@ -159,7 +156,6 @@ 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 52cc137..f5ae483 100644 --- a/chrome/browser/views/browser_actions_container.cc +++ b/chrome/browser/views/browser_actions_container.cc @@ -203,8 +203,88 @@ void BrowserActionView::Layout() { void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { View::PaintChildren(canvas); - button_->browser_action_state()->PaintBadge(canvas, - gfx::Rect(width(), height())); + + 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(); } diff --git a/chrome/browser/views/location_bar_view.cc b/chrome/browser/views/location_bar_view.cc index 663c81b..e5e4ece 100644 --- a/chrome/browser/views/location_bar_view.cc +++ b/chrome/browser/views/location_bar_view.cc @@ -1172,18 +1172,6 @@ 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 6290a99..fb8b249 100644 --- a/chrome/browser/views/location_bar_view.h +++ b/chrome/browser/views/location_bar_view.h @@ -356,7 +356,6 @@ 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); @@ -395,7 +394,6 @@ 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 de15f17..13ee202 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 { + const gfx::Rect& bounds) { 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(badge_text_color()); + text_paint.setColor(SK_ColorWHITE); 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 b819edb..6decdd4 100644 --- a/chrome/common/extensions/extension_action.h +++ b/chrome/common/extensions/extension_action.h @@ -84,8 +84,7 @@ 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_text_color_(SK_ColorWHITE) { + badge_background_color_(SkColorSetARGB(255, 218, 0, 24)) { } const std::string& title() const { return title_; } @@ -103,13 +102,6 @@ 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; } @@ -119,7 +111,7 @@ class ExtensionActionState { bool hidden() const { return hidden_; } void set_hidden(bool hidden) { hidden_ = hidden; } - void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds) const; + void PaintBadge(gfx::Canvas* canvas, const gfx::Rect& bounds); private: // True if the action is in the hidden state. @@ -140,9 +132,6 @@ 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 bcf5083..8170e86 100644 --- a/chrome/test/data/extensions/samples/test_browser_action/background.html +++ b/chrome/test/data/extensions/samples/test_browser_action/background.html @@ -5,7 +5,6 @@ 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 d8d9d78..2c5685d 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); }, 200); + setTimeout(function() { chrome.pageAction.show(info.tabId); }, 1000); } chrome.pageAction.setTitle({title: "click:" + clicks, tabId: info.tabId}); chrome.pageAction.setBadgeTextColor({ tabId: info.tabId, - color: [255, 255, clicks * 50, 255] + color: [0, 255, clicks * 50, 255] }); chrome.pageAction.setBadgeBackgroundColor({ tabId: info.tabId, @@ -27,7 +27,7 @@ }); chrome.pageAction.setBadgeText({ tabId: info.tabId, - text: i + "" + text: clicks + "" }); // We only have 2 icons, but cycle through 3 icons to test the |