Chromium Code Reviews| Index: chrome/browser/ui/views/web_intent_picker_view.cc |
| diff --git a/chrome/browser/ui/views/web_intent_picker_view.cc b/chrome/browser/ui/views/web_intent_picker_view.cc |
| index 9186ac26b17185e5682b1a2ae323e62d81149c47..09a27d113b20fb134e03cfeb4f2d43628005cfe8 100644 |
| --- a/chrome/browser/ui/views/web_intent_picker_view.cc |
| +++ b/chrome/browser/ui/views/web_intent_picker_view.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -8,22 +8,20 @@ |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/intents/web_intent_picker.h" |
| #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" |
| +#include "chrome/browser/ui/intents/web_intent_picker_model.h" |
| +#include "chrome/browser/ui/intents/web_intent_picker_model_observer.h" |
| #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| #include "chrome/browser/ui/views/frame/browser_view.h" |
| #include "chrome/browser/ui/views/location_bar/location_icon_view.h" |
| #include "chrome/browser/ui/views/toolbar_view.h" |
| #include "chrome/browser/ui/views/window.h" |
| #include "grit/generated_resources.h" |
| -#include "grit/theme_resources.h" |
| -#include "grit/theme_resources_standard.h" |
| -#include "grit/ui_resources.h" |
| #include "ui/base/l10n/l10n_util.h" |
| -#include "ui/base/resource/resource_bundle.h" |
| #include "ui/gfx/canvas.h" |
| #include "ui/gfx/canvas_skia.h" |
| #include "ui/gfx/image/image.h" |
| #include "ui/views/bubble/bubble_delegate.h" |
| -#include "ui/views/controls/button/image_button.h" |
| +#include "ui/views/controls/button/text_button.h" |
| #include "ui/views/controls/image_view.h" |
| #include "ui/views/controls/label.h" |
| #include "ui/views/layout/box_layout.h" |
| @@ -44,52 +42,18 @@ const int kControlSpacing = 6; |
| // The size, relative to default, of the Chrome Web store label. |
| const int kWebStoreLabelFontDelta = -2; |
| -// Create a new image where |image| is drawn on top of the image identified by |
| -// |bg_id|. The result is drawn to |out_image|. |
| -void CreateButtonImage(int bg_id, const SkBitmap& image, SkBitmap* out_image) { |
| - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| - SkBitmap bg_bitmap = rb.GetImageNamed(bg_id); |
| - bg_bitmap.copyTo(out_image, SkBitmap::kARGB_8888_Config); |
| - |
| - SkPaint paint; |
| - paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
| - |
| - SkCanvas bg_canvas(*out_image); |
| - bg_canvas.drawBitmap(image, |
| - SkIntToScalar((bg_bitmap.width() - image.width()) / 2), |
| - SkIntToScalar((bg_bitmap.height() - image.height()) / 2), |
| - &paint); |
| -} |
| - |
| -// Set the images on |button| for states normal, hot, and pushed. |
| -// The images are generated by drawing |image| on top of the identified by |
| -// |bg_normal_id|, |bg_hot_id|, and |bg_pushed_id| respectively. |
| -void SetButtonImages(views::ImageButton* button, |
| - const SkBitmap& image, |
| - int bg_normal_id, int bg_hot_id, int bg_pushed_id) { |
| - SkBitmap image_normal; |
| - CreateButtonImage(bg_normal_id, image, &image_normal); |
| - button->SetImage(views::CustomButton::BS_NORMAL, &image_normal); |
| - |
| - SkBitmap image_hot; |
| - CreateButtonImage(bg_hot_id, image, &image_hot); |
| - button->SetImage(views::CustomButton::BS_HOT, &image_hot); |
| - |
| - SkBitmap image_pushed; |
| - CreateButtonImage(bg_pushed_id, image, &image_pushed); |
| - button->SetImage(views::CustomButton::BS_PUSHED, &image_pushed); |
| -} |
| - |
| } // namespace |
| // Views implementation of WebIntentPicker. |
| class WebIntentPickerView : public views::BubbleDelegateView, |
| public views::ButtonListener, |
| - public WebIntentPicker { |
| + public WebIntentPicker, |
| + public WebIntentPickerModelObserver { |
| public: |
| WebIntentPickerView(views::View* anchor_view, |
| TabContentsWrapper* tab_contents, |
| - WebIntentPickerDelegate* delegate); |
| + WebIntentPickerDelegate* delegate, |
| + WebIntentPickerModel* model); |
| virtual ~WebIntentPickerView(); |
| // views::ButtonListener implementation. |
| @@ -100,11 +64,13 @@ class WebIntentPickerView : public views::BubbleDelegateView, |
| virtual void WindowClosing() OVERRIDE; |
| // WebIntentPicker implementation. |
| - virtual void SetServiceURLs(const std::vector<GURL>& urls) OVERRIDE; |
| - virtual void SetServiceIcon(size_t index, const SkBitmap& icon) OVERRIDE; |
| - virtual void SetDefaultServiceIcon(size_t index) OVERRIDE; |
| virtual void Close() OVERRIDE; |
| - virtual content::WebContents* SetInlineDisposition(const GURL& url) OVERRIDE; |
| + |
| + // WebIntentPickerModelObserver implementation. |
| + virtual void OnModelChanged(WebIntentPickerModel* model) OVERRIDE; |
| + virtual void OnFaviconChanged(WebIntentPickerModel* model, |
| + size_t index) OVERRIDE; |
| + virtual void OnInlineDisposition(WebIntentPickerModel* model) OVERRIDE; |
| protected: |
| // views::BubbleDelegateView overrides: |
| @@ -115,31 +81,31 @@ class WebIntentPickerView : public views::BubbleDelegateView, |
| // chooses a service or cancels. |
| WebIntentPickerDelegate* delegate_; |
| + // Web pointer to the picker model. |
| + WebIntentPickerModel* model_; |
| + |
| // A weak pointer to the hbox that contains the buttons used to choose the |
| // service. |
| - views::View* button_hbox_; |
| + views::View* button_vbox_; |
| // A vector of weak pointers to each of the service buttons. |
| std::vector<views::Button*> buttons_; |
| - // A weak pointer to the plus button, used to search for more services on the |
| - // Chrome Web Store. |
| - views::ImageButton* plus_button_; |
| - |
| DISALLOW_COPY_AND_ASSIGN(WebIntentPickerView); |
| }; |
| // static |
| WebIntentPicker* WebIntentPicker::Create(Browser* browser, |
| TabContentsWrapper* wrapper, |
| - WebIntentPickerDelegate* delegate) { |
| + WebIntentPickerDelegate* delegate, |
| + WebIntentPickerModel* model) { |
| // Find where to point the bubble at. |
| BrowserView* browser_view = |
| BrowserView::GetBrowserViewForBrowser(browser); |
| views::View* anchor_view = |
| browser_view->toolbar()->location_bar()->location_icon_view(); |
| WebIntentPickerView* bubble_delegate = |
| - new WebIntentPickerView(anchor_view, wrapper, delegate); |
| + new WebIntentPickerView(anchor_view, wrapper, delegate, model); |
| browser::CreateViewsBubble(bubble_delegate); |
| bubble_delegate->Show(); |
| return bubble_delegate; |
| @@ -147,75 +113,70 @@ WebIntentPicker* WebIntentPicker::Create(Browser* browser, |
| WebIntentPickerView::WebIntentPickerView(views::View* anchor_view, |
| TabContentsWrapper* wrapper, |
| - WebIntentPickerDelegate* delegate) |
| + WebIntentPickerDelegate* delegate, |
| + WebIntentPickerModel* model) |
| : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), |
| delegate_(delegate), |
| - button_hbox_(NULL), |
| - plus_button_(NULL) { |
| + model_(model), |
| + button_vbox_(NULL) { |
| + model_->set_observer(this); |
| } |
| WebIntentPickerView::~WebIntentPickerView() { |
| + model_->set_observer(NULL); |
| } |
| void WebIntentPickerView::ButtonPressed(views::Button* sender, |
| const views::Event& event) { |
| - // TODO(binji) When we support the plus button, pressing it should forward the |
| - // user to services in the Chrome Web Store that provide this intent. |
| - if (sender == plus_button_) |
| - return; |
| - |
| std::vector<views::Button*>::iterator iter = |
| std::find(buttons_.begin(), buttons_.end(), sender); |
| DCHECK(iter != buttons_.end()); |
| size_t index = iter - buttons_.begin(); |
| - delegate_->OnServiceChosen(index); |
| + const WebIntentPickerModel::Item& item = model_->GetItemAt(index); |
| + |
| + delegate_->OnServiceChosen(index, item.disposition); |
| } |
| void WebIntentPickerView::WindowClosing() { |
| - delegate_->OnCancelled(); |
| + delegate_->OnClosing(); |
| } |
| -void WebIntentPickerView::SetServiceURLs(const std::vector<GURL>& urls) { |
| - for (size_t i = 0; i < urls.size(); ++i) { |
| - views::ImageButton* button = new views::ImageButton(this); |
| - button->SetTooltipText(UTF8ToUTF16(urls[i].spec().c_str())); |
| - button_hbox_->AddChildView(button); |
| +void WebIntentPickerView::Close() { |
| + GetWidget()->Close(); |
| +} |
| + |
| +void WebIntentPickerView::OnModelChanged(WebIntentPickerModel* model) { |
| + button_vbox_->RemoveAllChildViews(true); |
| + buttons_.clear(); |
| + |
| + for (size_t i = 0; i < model->GetItemCount(); ++i) { |
| + const WebIntentPickerModel::Item& item = model_->GetItemAt(i); |
| + |
| + views::TextButton* button = new views::TextButton(this, item.title); |
| + button->SetTooltipText(UTF8ToUTF16(item.url.spec().c_str())); |
| + button->SetIcon(*item.favicon.ToSkBitmap()); |
| + button_vbox_->AddChildView(button); |
| + |
| buttons_.push_back(button); |
| } |
| - |
| - // Add the '+' button, to use the Chrome Web Store. |
| - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| - plus_button_ = new views::ImageButton(this); |
| - plus_button_->SetTooltipText( |
| - l10n_util::GetStringUTF16(IDS_FIND_MORE_INTENT_HANDLER_TOOLTIP)); |
| - SetButtonImages(plus_button_, rb.GetImageNamed(IDR_SIDETABS_NEW_TAB), |
| - IDR_BROWSER_ACTION, IDR_BROWSER_ACTION_H, IDR_BROWSER_ACTION_P); |
| - button_hbox_->AddChildView(plus_button_); |
| } |
| -void WebIntentPickerView::SetServiceIcon(size_t index, const SkBitmap& icon) { |
| - views::ImageButton* button = |
| - static_cast<views::ImageButton*>(button_hbox_->child_at(index)); |
| - SetButtonImages(button, icon, |
| - IDR_BROWSER_ACTION, IDR_BROWSER_ACTION_H, IDR_BROWSER_ACTION_P); |
| +void WebIntentPickerView::OnFaviconChanged(WebIntentPickerModel* model, |
| + size_t index) { |
|
sky
2012/01/27 18:35:34
nit: spacing
binji
2012/01/27 19:04:20
Done.
|
| + const WebIntentPickerModel::Item& item = model_->GetItemAt(index); |
| + views::TextButton* button = |
| + static_cast<views::TextButton*>(buttons_[index]); |
|
sky
2012/01/27 18:35:34
Can we make buttons_ std::vector<views::TextButton
binji
2012/01/27 19:04:20
Done.
|
| + |
| + button->SetIcon(*item.favicon.ToSkBitmap()); |
| Layout(); |
| SizeToContents(); |
| } |
| -void WebIntentPickerView::SetDefaultServiceIcon(size_t index) { |
| - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| - SetServiceIcon(index, rb.GetImageNamed(IDR_DEFAULT_FAVICON)); |
| -} |
| - |
| -void WebIntentPickerView::Close() { |
| - GetWidget()->Close(); |
| -} |
| - |
| -WebContents* WebIntentPickerView::SetInlineDisposition(const GURL& url) { |
| +void WebIntentPickerView::OnInlineDisposition(WebIntentPickerModel* model) { |
| // TODO(gbillock): add support here. |
| - return NULL; |
| + delegate_->OnInlineDispositionWebContentsCreated(NULL); |
| } |
| void WebIntentPickerView::Init() { |
| @@ -225,13 +186,6 @@ void WebIntentPickerView::Init() { |
| kContentAreaBorder, // inside border vertical spacing |
| kContentAreaSpacing)); // between child spacing |
| - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| - SkBitmap image_bitmap = rb.GetImageNamed(IDR_PAGEINFO_INFO); |
| - views::ImageView* image = new views::ImageView(); |
| - image->SetImage(image_bitmap); |
| - image->SetVerticalAlignment(views::ImageView::LEADING); |
| - AddChildView(image); |
| - |
| views::View* main_content = new views::View(); |
| main_content->SetLayoutManager(new views::BoxLayout( |
| views::BoxLayout::kVertical, |
| @@ -244,13 +198,13 @@ void WebIntentPickerView::Init() { |
| top_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| main_content->AddChildView(top_label); |
| - button_hbox_ = new views::View(); |
| - button_hbox_->SetLayoutManager(new views::BoxLayout( |
| - views::BoxLayout::kHorizontal, |
| + button_vbox_ = new views::View(); |
| + button_vbox_->SetLayoutManager(new views::BoxLayout( |
| + views::BoxLayout::kVertical, |
| 0, // inside border horizontal spacing |
| 0, // inside border vertical spacing |
| kControlSpacing)); // between child spacing |
| - main_content->AddChildView(button_hbox_); |
| + main_content->AddChildView(button_vbox_); |
| views::Label* bottom_label = new views::Label( |
| l10n_util::GetStringUTF16(IDS_FIND_MORE_INTENT_HANDLER_MESSAGE)); |