Commit 1e358c58 authored by Josh Pratt's avatar Josh Pratt Committed by Commit Bot

Refactor LocationBarView to move Refresh logic for some BubbleIconViews into...

Refactor LocationBarView to move Refresh logic for some BubbleIconViews into the respective subclasses.

This is initial work for adding BubbleIconViews to Chrome OS Desktop PWAs.
It partially decouples BubbleIconViews from the LocationBarView so that
they can also be used in HostedAppButtonContainer.

Bug: 788051
Change-Id: I986951b64fb02f2d2728abbe6a3964ff97e04909
Reviewed-on: https://chromium-review.googlesource.com/923607
Commit-Queue: Trent Apted <tapted@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Reviewed-by: default avatarcalamity <calamity@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542045}
parent 53ef4fcc
......@@ -8,27 +8,28 @@
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/autofill/save_card_bubble_controller_impl.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/autofill/save_card_bubble_views.h"
#include "chrome/browser/ui/views/location_bar/bubble_icon_view.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
namespace autofill {
SaveCardIconView::SaveCardIconView(CommandUpdater* command_updater,
Browser* browser)
: BubbleIconView(command_updater, IDC_SAVE_CREDIT_CARD_FOR_PAGE),
Browser* browser,
BubbleIconView::Delegate* delegate)
: BubbleIconView(command_updater, IDC_SAVE_CREDIT_CARD_FOR_PAGE, delegate),
browser_(browser) {
DCHECK(delegate);
set_id(VIEW_ID_SAVE_CREDIT_CARD_BUTTON);
SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_SAVE_CREDIT_CARD));
}
SaveCardIconView::~SaveCardIconView() {}
void SaveCardIconView::OnExecuting(
BubbleIconView::ExecuteSource execute_source) {}
views::BubbleDialogDelegateView* SaveCardIconView::GetBubble() const {
SaveCardBubbleControllerImpl* controller = GetController();
if (!controller)
......@@ -38,6 +39,24 @@ views::BubbleDialogDelegateView* SaveCardIconView::GetBubble() const {
controller->save_card_bubble_view());
}
bool SaveCardIconView::Refresh() {
if (!GetWebContents())
return false;
const bool was_visible = visible();
// |controller| may be nullptr due to lazy initialization.
SaveCardBubbleControllerImpl* controller = GetController();
bool enabled = controller && controller->IsIconVisible();
enabled &= SetCommandEnabled(enabled);
SetVisible(enabled);
return was_visible != visible();
}
void SaveCardIconView::OnExecuting(
BubbleIconView::ExecuteSource execute_source) {}
const gfx::VectorIcon& SaveCardIconView::GetVectorIcon() const {
return kCreditCardIcon;
}
......@@ -45,8 +64,8 @@ const gfx::VectorIcon& SaveCardIconView::GetVectorIcon() const {
SaveCardBubbleControllerImpl* SaveCardIconView::GetController() const {
if (!browser_)
return nullptr;
content::WebContents* web_contents =
browser_->tab_strip_model()->GetActiveWebContents();
content::WebContents* web_contents = GetWebContents();
if (!web_contents)
return nullptr;
return autofill::SaveCardBubbleControllerImpl::FromWebContents(web_contents);
......
......@@ -20,13 +20,18 @@ class SaveCardBubbleControllerImpl;
// it.
class SaveCardIconView : public BubbleIconView {
public:
SaveCardIconView(CommandUpdater* command_updater, Browser* browser);
SaveCardIconView(CommandUpdater* command_updater,
Browser* browser,
BubbleIconView::Delegate* delegate);
~SaveCardIconView() override;
// BubbleIconView:
views::BubbleDialogDelegateView* GetBubble() const override;
bool Refresh() override;
protected:
// BubbleIconView:
void OnExecuting(BubbleIconView::ExecuteSource execute_source) override;
views::BubbleDialogDelegateView* GetBubble() const override;
const gfx::VectorIcon& GetVectorIcon() const override;
private:
......
......@@ -28,10 +28,13 @@ void BubbleIconView::Init() {
SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
}
BubbleIconView::BubbleIconView(CommandUpdater* command_updater, int command_id)
BubbleIconView::BubbleIconView(CommandUpdater* command_updater,
int command_id,
BubbleIconView::Delegate* delegate)
: widget_observer_(this),
image_(new views::ImageView()),
command_updater_(command_updater),
delegate_(delegate),
command_id_(command_id),
active_(false),
suppress_mouse_released_action_(false) {}
......@@ -44,6 +47,12 @@ bool BubbleIconView::IsBubbleShowing() const {
return GetBubble() != nullptr;
}
bool BubbleIconView::SetCommandEnabled(bool enabled) const {
DCHECK(command_updater_);
command_updater_->UpdateCommandEnabled(command_id_, enabled);
return command_updater_->IsCommandEnabled(command_id_);
}
void BubbleIconView::SetImage(const gfx::ImageSkia* image_skia) {
image_->SetImage(image_skia);
}
......@@ -69,6 +78,10 @@ void BubbleIconView::OnBubbleWidgetCreated(views::Widget* bubble_widget) {
SetHighlighted(true);
}
bool BubbleIconView::Refresh() {
return false;
}
void BubbleIconView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
image_->GetAccessibleNodeData(node_data);
node_data->role = ax::mojom::Role::kButton;
......@@ -240,6 +253,10 @@ void BubbleIconView::SetActiveInternal(bool active) {
UpdateIcon();
}
content::WebContents* BubbleIconView::GetWebContents() const {
return delegate_->GetWebContentsForBubbleIconView();
}
BubbleIconView::WidgetObserver::WidgetObserver(BubbleIconView* parent)
: parent_(parent), scoped_observer_(this) {}
......
......@@ -16,6 +16,10 @@
class CommandUpdater;
namespace content {
class WebContents;
}
namespace gfx {
struct VectorIcon;
}
......@@ -28,6 +32,11 @@ class BubbleDialogDelegateView;
// TODO(spqchan): Convert this to subclass Button.
class BubbleIconView : public views::InkDropHostView {
public:
class Delegate {
public:
virtual content::WebContents* GetWebContentsForBubbleIconView() = 0;
};
void Init();
// Invoked when a bubble for this icon is created. The BubbleIconView changes
......@@ -37,6 +46,10 @@ class BubbleIconView : public views::InkDropHostView {
// Returns the bubble instance for the icon.
virtual views::BubbleDialogDelegateView* GetBubble() const = 0;
// Updates the icon state and associated bubble when the WebContents changes.
// Returns true if there was a change.
virtual bool Refresh();
protected:
enum ExecuteSource {
EXECUTE_SOURCE_MOUSE,
......@@ -44,12 +57,18 @@ class BubbleIconView : public views::InkDropHostView {
EXECUTE_SOURCE_GESTURE,
};
BubbleIconView(CommandUpdater* command_updater, int command_id);
BubbleIconView(CommandUpdater* command_updater,
int command_id,
Delegate* delegate = nullptr);
~BubbleIconView() override;
// Returns true if a related bubble is showing.
bool IsBubbleShowing() const;
// Enables or disables the associated command.
// Returns true if the command is enabled.
bool SetCommandEnabled(bool enabled) const;
// Sets the image that should be displayed in |image_|.
void SetImage(const gfx::ImageSkia* image_skia);
......@@ -108,6 +127,9 @@ class BubbleIconView : public views::InkDropHostView {
// "call to action" color.
void SetActiveInternal(bool active);
// Returns the associated web contents from the delegate.
content::WebContents* GetWebContents() const;
bool active() const { return active_; }
private:
......@@ -141,6 +163,9 @@ class BubbleIconView : public views::InkDropHostView {
// The CommandUpdater for the Browser object that owns the location bar.
CommandUpdater* command_updater_;
// Delegate for access to associated state.
Delegate* delegate_;
// The command ID executed when the user clicks this icon.
const int command_id_;
......
......@@ -258,15 +258,20 @@ void LocationBarView::Init() {
AddChildView(image_view);
}
bubble_icons_.push_back(zoom_view_ = new ZoomView(delegate_));
bubble_icons_.push_back(manage_passwords_icon_view_ =
new ManagePasswordsIconViews(command_updater()));
if (browser_)
bubble_icons_.push_back(
save_credit_card_icon_view_ =
new autofill::SaveCardIconView(command_updater(), browser_));
bubble_icons_.push_back(translate_icon_view_ =
new TranslateIconView(command_updater()));
zoom_view_ = new ZoomView(delegate_);
bubble_icons_.push_back(zoom_view_);
manage_passwords_icon_view_ =
new ManagePasswordsIconViews(command_updater(), this);
bubble_icons_.push_back(manage_passwords_icon_view_);
if (browser_) {
save_credit_card_icon_view_ =
new autofill::SaveCardIconView(command_updater(), browser_, this);
bubble_icons_.push_back(save_credit_card_icon_view_);
}
translate_icon_view_ = new TranslateIconView(command_updater(), this);
bubble_icons_.push_back(translate_icon_view_);
#if defined(OS_CHROMEOS)
if (browser_)
bubble_icons_.push_back(intent_picker_view_ =
......@@ -641,14 +646,19 @@ void LocationBarView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
void LocationBarView::Update(const WebContents* contents) {
RefreshContentSettingViews();
// TODO(calamity): Refactor Update to use BubbleIconView::Refresh.
RefreshZoomView();
RefreshTranslateIcon();
RefreshSaveCreditCardIconView();
RefreshManagePasswordsIconView();
RefreshBubbleIconViews();
// TODO(calamity): Refactor Update to use BubbleIconView::Refresh.
RefreshFindBarIcon();
if (star_view_)
if (star_view_) {
// TODO(calamity): Refactor Update to use BubbleIconView::Refresh.
UpdateBookmarkStarVisibility();
}
if (contents)
omnibox_view_->OnTabChanged(contents);
......@@ -709,6 +719,12 @@ LocationBarView::GetContentSettingBubbleModelDelegate() {
return delegate_->GetContentSettingBubbleModelDelegate();
}
////////////////////////////////////////////////////////////////////////////////
// LocationBarView, public BubbleIconView::Delegate implementation:
WebContents* LocationBarView::GetWebContentsForBubbleIconView() {
return GetWebContents();
}
////////////////////////////////////////////////////////////////////////////////
// LocationBarView, private:
......@@ -770,6 +786,21 @@ bool LocationBarView::RefreshContentSettingViews() {
return visibility_changed;
}
bool LocationBarView::RefreshBubbleIconViews() {
if (extensions::HostedAppBrowserController::IsForExperimentalHostedAppBrowser(
browser_)) {
// For hosted apps, the location bar is normally hidden and icons appear in
// the window frame instead.
GetWidget()->non_client_view()->ResetWindowControls();
}
bool visibility_changed = false;
for (auto* v : bubble_icons_) {
visibility_changed |= v->Refresh();
}
return visibility_changed;
}
bool LocationBarView::RefreshZoomView() {
DCHECK(zoom_view_);
WebContents* web_contents = GetWebContents();
......@@ -807,26 +838,6 @@ bool LocationBarView::IsVirtualKeyboardVisible() {
#endif
}
bool LocationBarView::RefreshSaveCreditCardIconView() {
WebContents* web_contents = GetWebContents();
if (!save_credit_card_icon_view_ || !web_contents)
return false;
const bool was_visible = save_credit_card_icon_view_->visible();
// |controller| may be nullptr due to lazy initialization.
autofill::SaveCardBubbleControllerImpl* controller =
autofill::SaveCardBubbleControllerImpl::FromWebContents(web_contents);
bool enabled = controller && controller->IsIconVisible();
if (!command_updater()->UpdateCommandEnabled(
IDC_SAVE_CREDIT_CARD_FOR_PAGE, enabled)) {
enabled = enabled && command_updater()->IsCommandEnabled(
IDC_SAVE_CREDIT_CARD_FOR_PAGE);
}
save_credit_card_icon_view_->SetVisible(enabled);
return was_visible != save_credit_card_icon_view_->visible();
}
bool LocationBarView::RefreshFindBarIcon() {
// |browser_| may be nullptr since some unit tests pass it in for the
// Browser*. |browser_->window()| may return nullptr because Update() is
......@@ -841,33 +852,6 @@ bool LocationBarView::RefreshFindBarIcon() {
return was_visible != find_bar_icon_->visible();
}
void LocationBarView::RefreshTranslateIcon() {
WebContents* web_contents = GetWebContents();
if (!web_contents)
return;
translate::LanguageState& language_state =
ChromeTranslateClient::FromWebContents(web_contents)->GetLanguageState();
bool enabled = language_state.translate_enabled();
if (!command_updater()->UpdateCommandEnabled(IDC_TRANSLATE_PAGE, enabled)) {
enabled = enabled && command_updater()->IsCommandEnabled(
IDC_TRANSLATE_PAGE);
}
translate_icon_view_->SetVisible(enabled);
if (!enabled)
TranslateBubbleView::CloseCurrentBubble();
}
bool LocationBarView::RefreshManagePasswordsIconView() {
DCHECK(manage_passwords_icon_view_);
WebContents* web_contents = GetWebContents();
if (!web_contents)
return false;
const bool was_visible = manage_passwords_icon_view_->visible();
ManagePasswordsUIController::FromWebContents(
web_contents)->UpdateIconAndBubbleState(manage_passwords_icon_view_);
return was_visible != manage_passwords_icon_view_->visible();
}
void LocationBarView::RefreshClearAllButtonIcon() {
if (!clear_all_button_)
return;
......@@ -959,14 +943,14 @@ void LocationBarView::UpdateContentSettingsIcons() {
}
void LocationBarView::UpdateManagePasswordsIconAndBubble() {
if (RefreshManagePasswordsIconView()) {
if (manage_passwords_icon_view_->Refresh()) {
Layout();
SchedulePaint();
}
}
void LocationBarView::UpdateSaveCreditCardIcon() {
if (RefreshSaveCreditCardIconView()) {
if (save_credit_card_icon_view_->Refresh()) {
Layout();
SchedulePaint();
}
......
......@@ -19,6 +19,7 @@
#include "chrome/browser/ui/views/dropdown_bar_host.h"
#include "chrome/browser/ui/views/dropdown_bar_host_delegate.h"
#include "chrome/browser/ui/views/extensions/extension_popup.h"
#include "chrome/browser/ui/views/location_bar/bubble_icon_view.h"
#include "chrome/browser/ui/views/location_bar/content_setting_image_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
#include "components/prefs/pref_member.h"
......@@ -31,7 +32,6 @@
#include "ui/views/controls/button/button.h"
#include "ui/views/drag_controller.h"
class BubbleIconView;
class CommandUpdater;
class ContentSettingBubbleModelDelegate;
class FindBarIcon;
......@@ -73,7 +73,8 @@ class LocationBarView : public LocationBar,
public DropdownBarHostDelegate,
public zoom::ZoomEventManagerObserver,
public views::ButtonListener,
public ContentSettingImageView::Delegate {
public ContentSettingImageView::Delegate,
public BubbleIconView::Delegate {
public:
class Delegate {
public:
......@@ -236,6 +237,9 @@ class LocationBarView : public LocationBar,
ContentSettingBubbleModelDelegate* GetContentSettingBubbleModelDelegate()
override;
// BubbleIconView::Delegate:
content::WebContents* GetWebContentsForBubbleIconView() override;
// ZoomEventManagerObserver:
// Updates the view for the zoom icon when default zoom levels change.
void OnDefaultZoomLevelChanged() override;
......@@ -271,22 +275,19 @@ class LocationBarView : public LocationBar,
// of at least one of the views in |content_setting_views_| changed.
bool RefreshContentSettingViews();
// Updates the visibility state of the BubbleIconView (page action) icons
// to reflect what actions are available on the current page.
// Returns true if the visibility of at least one of the views in
// |bubble_icons_| changed.
bool RefreshBubbleIconViews();
// Updates the view for the zoom icon based on the current tab's zoom. Returns
// true if the visibility of the view changed.
bool RefreshZoomView();
// Updates |save_credit_card_icon_view_|. Returns true if visibility changed.
bool RefreshSaveCreditCardIconView();
// Updates |find_bar_icon_|. Returns true if visibility changed.
bool RefreshFindBarIcon();
// Updates the Translate icon based on the current tab's Translate status.
void RefreshTranslateIcon();
// Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
bool RefreshManagePasswordsIconView();
// Updates the color of the icon for the "clear all" button.
void RefreshClearAllButtonIcon();
......
......@@ -6,17 +6,20 @@
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
#include "chrome/browser/ui/views/passwords/password_bubble_view_base.h"
#include "chrome/grit/generated_resources.h"
#include "components/password_manager/core/common/password_manager_ui.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
ManagePasswordsIconViews::ManagePasswordsIconViews(CommandUpdater* updater)
: BubbleIconView(updater, IDC_MANAGE_PASSWORDS_FOR_PAGE),
ManagePasswordsIconViews::ManagePasswordsIconViews(
CommandUpdater* updater,
BubbleIconView::Delegate* delegate)
: BubbleIconView(updater, IDC_MANAGE_PASSWORDS_FOR_PAGE, delegate),
state_(password_manager::ui::INACTIVE_STATE) {
DCHECK(delegate);
#if defined(OS_MACOSX)
SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
#else
......@@ -56,6 +59,20 @@ void ManagePasswordsIconViews::UpdateUiForState() {
parent()->Layout();
}
views::BubbleDialogDelegateView* ManagePasswordsIconViews::GetBubble() const {
return PasswordBubbleViewBase::manage_password_bubble();
}
bool ManagePasswordsIconViews::Refresh() {
if (!GetWebContents())
return false;
const bool was_visible = visible();
ManagePasswordsUIController::FromWebContents(GetWebContents())
->UpdateIconAndBubbleState(this);
return was_visible != visible();
}
void ManagePasswordsIconViews::OnExecuting(
BubbleIconView::ExecuteSource source) {}
......@@ -79,10 +96,6 @@ bool ManagePasswordsIconViews::OnKeyPressed(const ui::KeyEvent& event) {
return BubbleIconView::OnKeyPressed(event);
}
views::BubbleDialogDelegateView* ManagePasswordsIconViews::GetBubble() const {
return PasswordBubbleViewBase::manage_password_bubble();
}
const gfx::VectorIcon& ManagePasswordsIconViews::GetVectorIcon() const {
return kKeyIcon;
}
......
......@@ -18,17 +18,19 @@ class CommandUpdater;
class ManagePasswordsIconViews : public ManagePasswordsIconView,
public BubbleIconView {
public:
explicit ManagePasswordsIconViews(CommandUpdater* updater);
ManagePasswordsIconViews(CommandUpdater* updater,
BubbleIconView::Delegate* delegate);
~ManagePasswordsIconViews() override;
// ManagePasswordsIconView:
void SetState(password_manager::ui::State state) override;
// BubbleIconView:
views::BubbleDialogDelegateView* GetBubble() const override;
bool Refresh() override;
void OnExecuting(BubbleIconView::ExecuteSource source) override;
bool OnMousePressed(const ui::MouseEvent& event) override;
bool OnKeyPressed(const ui::KeyEvent& event) override;
views::BubbleDialogDelegateView* GetBubble() const override;
const gfx::VectorIcon& GetVectorIcon() const override;
// views::View:
......
......@@ -8,33 +8,56 @@
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/translate/translate_bubble_view_state_transition.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/translate/translate_bubble_view.h"
#include "chrome/grit/generated_resources.h"
#include "components/translate/core/browser/language_state.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
TranslateIconView::TranslateIconView(CommandUpdater* command_updater)
: BubbleIconView(command_updater, IDC_TRANSLATE_PAGE) {
TranslateIconView::TranslateIconView(CommandUpdater* command_updater,
BubbleIconView::Delegate* delegate)
: BubbleIconView(command_updater, IDC_TRANSLATE_PAGE, delegate) {
DCHECK(delegate);
set_id(VIEW_ID_TRANSLATE_BUTTON);
SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_TRANSLATE));
}
TranslateIconView::~TranslateIconView() {}
views::BubbleDialogDelegateView* TranslateIconView::GetBubble() const {
return TranslateBubbleView::GetCurrentBubble();
}
bool TranslateIconView::Refresh() {
if (!GetWebContents())
return false;
const bool was_visible = visible();
const translate::LanguageState& language_state =
ChromeTranslateClient::FromWebContents(GetWebContents())
->GetLanguageState();
bool enabled = language_state.translate_enabled();
// Enable Translate page command or disable icon.
enabled &= SetCommandEnabled(enabled);
SetVisible(enabled);
if (!enabled)
TranslateBubbleView::CloseCurrentBubble();
return was_visible != visible();
}
void TranslateIconView::OnExecuting(
BubbleIconView::ExecuteSource execute_source) {}
void TranslateIconView::OnPressed(bool activated) {
translate::ReportUiAction((activated
translate::ReportUiAction(activated
? translate::PAGE_ACTION_ICON_ACTIVATED
: translate::PAGE_ACTION_ICON_DEACTIVATED));
}
views::BubbleDialogDelegateView* TranslateIconView::GetBubble() const {
return TranslateBubbleView::GetCurrentBubble();
: translate::PAGE_ACTION_ICON_DEACTIVATED);
}
const gfx::VectorIcon& TranslateIconView::GetVectorIcon() const {
......
......@@ -14,14 +14,18 @@ class CommandUpdater;
// the page translated.
class TranslateIconView : public BubbleIconView {
public:
explicit TranslateIconView(CommandUpdater* command_updater);
TranslateIconView(CommandUpdater* command_updater,
BubbleIconView::Delegate* delegate);
~TranslateIconView() override;
// BubbleIconView:
views::BubbleDialogDelegateView* GetBubble() const override;
bool Refresh() override;
protected:
// BubbleIconView:
void OnExecuting(BubbleIconView::ExecuteSource execute_source) override;
void OnPressed(bool activated) override;
views::BubbleDialogDelegateView* GetBubble() const override;
const gfx::VectorIcon& GetVectorIcon() const override;
private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment