From 1fc025209fbaeb2eb91cfeac8dbced8bc493f3b2 Mon Sep 17 00:00:00 2001 From: "ben@chromium.org" Date: Tue, 20 Jan 2009 23:03:14 +0000 Subject: Rework the command updater to not be dependent on views::Button (needed for porting). TEST=make sure back/forward buttons still enable/disable correctly depending on the length of the back/forward navigation list. Review URL: http://codereview.chromium.org/18343 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8332 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/views/browser_views.vcproj | 8 --- chrome/browser/views/delay_view.cc | 89 ------------------------------ chrome/browser/views/delay_view.h | 49 ---------------- chrome/browser/views/frame/browser_view.cc | 21 +++---- chrome/browser/views/go_button.cc | 9 +-- chrome/browser/views/go_button.h | 8 ++- chrome/browser/views/location_bar_view.cc | 13 +++-- chrome/browser/views/location_bar_view.h | 8 +-- chrome/browser/views/toolbar_view.cc | 54 ++++++++++++++---- chrome/browser/views/toolbar_view.h | 17 ++++-- 10 files changed, 85 insertions(+), 191 deletions(-) delete mode 100644 chrome/browser/views/delay_view.cc delete mode 100644 chrome/browser/views/delay_view.h (limited to 'chrome/browser/views') diff --git a/chrome/browser/views/browser_views.vcproj b/chrome/browser/views/browser_views.vcproj index b605de5..557e4a2 100644 --- a/chrome/browser/views/browser_views.vcproj +++ b/chrome/browser/views/browser_views.vcproj @@ -450,14 +450,6 @@ > - - - - diff --git a/chrome/browser/views/delay_view.cc b/chrome/browser/views/delay_view.cc deleted file mode 100644 index 4b8e871..0000000 --- a/chrome/browser/views/delay_view.cc +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) 2006-2008 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. - -#include "chrome/browser/views/delay_view.h" - -#include "chrome/common/l10n_util.h" - -#include "generated_resources.h" - -// The amount of horizontal space between the throbber and the label. -const int kThrobberLabelSpace = 7; - -// The amount of space between controls and the edge of the window. -const int kWindowMargin = 5; - -DelayView::DelayView(const std::wstring& text, CommandController* controller, - bool show_cancel) - : controller_(controller), - label_(NULL), - cancel_button_(NULL) { - DCHECK(controller); - - label_ = new views::Label(text); - AddChildView(label_); - - if (show_cancel) { - cancel_button_ = new views::NativeButton(l10n_util::GetString(IDS_CANCEL)); - cancel_button_->SetID(ID_CANCEL); - cancel_button_->SetListener(this); - AddChildView(cancel_button_); - } - - throbber_ = new views::Throbber(50, true); - AddChildView(throbber_); - throbber_->Start(); -} - -DelayView::~DelayView() { -} - -void DelayView::ButtonPressed(views::NativeButton *sender) { - if (sender->GetID() == ID_CANCEL) { - controller_->ExecuteCommand(IDCANCEL); - } -} - -void DelayView::Layout() { - if (!GetParent()) - return; - - gfx::Size available = GetParent()->size(); - - if (cancel_button_) { - gfx::Size button_size = cancel_button_->GetPreferredSize(); - cancel_button_->SetBounds(available.width() - kWindowMargin - - button_size.width(), - available.height() - kWindowMargin - - button_size.height(), - button_size.width(), button_size.height()); - } - - DCHECK(label_); - gfx::Size label_size = label_->GetPreferredSize(); - - DCHECK(throbber_); - gfx::Size throbber_size = throbber_->GetPreferredSize(); - - gfx::Rect main_rect(0, 0, - throbber_size.width() + kThrobberLabelSpace + - label_size.width(), - std::max(throbber_size.height(), label_size.height())); - - main_rect.set_x((available.width() / 2) - (main_rect.width() / 2)); - main_rect.set_y((available.height() / 2) - (main_rect.height() / 2)); - - label_->SetBounds(main_rect.x() + throbber_size.width() + - kThrobberLabelSpace, - main_rect.y() + main_rect.height() / 2 - - label_size.height() / 2, - label_size.width(), - label_size.height()); - - throbber_->SetBounds( - main_rect.x(), - main_rect.y() + main_rect.height() / 2 - throbber_size.height() / 2, - throbber_size.width(), - throbber_size.height()); -} diff --git a/chrome/browser/views/delay_view.h b/chrome/browser/views/delay_view.h deleted file mode 100644 index 86ac7cc..0000000 --- a/chrome/browser/views/delay_view.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2006-2008 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. - -// A simple view that indicates to the user that a time-consuming operation -// is being performed, using a throbber and some explanatory text. - -#ifndef CHROME_BROWSER_VIEWS_DELAY_VIEW_H__ -#define CHROME_BROWSER_VIEWS_DELAY_VIEW_H__ - -#include "chrome/browser/controller.h" -#include "base/basictypes.h" -#include "chrome/views/label.h" -#include "chrome/views/native_button.h" -#include "chrome/views/throbber.h" - -class DelayView : public views::View, - public views::NativeButton::Listener { - public: - // |text| explains the delay - // |controller| receives notifications when the "cancel" button is pressed - // |show_cancel| determines whether the cancel button is shown - DelayView(const std::wstring& text, - CommandController* controller, - bool show_cancel); - virtual ~DelayView(); - - enum ViewID { - ID_CANCEL = 10000, - }; - - // Overridden from views::View - virtual void Layout(); - - // Implemented from views::NativeButton::Listener - virtual void ButtonPressed(views::NativeButton *sender); - - private: - CommandController* controller_; - - views::Label* label_; - views::NativeButton* cancel_button_; - views::Throbber* throbber_; - - DISALLOW_EVIL_CONSTRUCTORS(DelayView); -}; - -#endif // CHROME_BROWSER_VIEWS_DELAY_VIEW_H__ - diff --git a/chrome/browser/views/frame/browser_view.cc b/chrome/browser/views/frame/browser_view.cc index 9930cc1..7bf7156e 100644 --- a/chrome/browser/views/frame/browser_view.cc +++ b/chrome/browser/views/frame/browser_view.cc @@ -212,8 +212,8 @@ bool BrowserView::AcceleratorPressed(const views::Accelerator& accelerator) { DCHECK(iter != accelerator_table_->end()); int command_id = iter->second; - if (browser_->SupportsCommand(command_id) && - browser_->IsCommandEnabled(command_id)) { + if (browser_->command_updater()->SupportsCommand(command_id) && + browser_->command_updater()->IsCommandEnabled(command_id)) { browser_->ExecuteCommand(command_id); return true; } @@ -236,7 +236,8 @@ bool BrowserView::SystemCommandReceived(UINT notification_code, const gfx::Point& point) { bool handled = false; - if (browser_->SupportsCommand(notification_code)) { + if (browser_->command_updater()->SupportsCommand(notification_code) && + browser_->command_updater()->IsCommandEnabled(notification_code)) { browser_->ExecuteCommand(notification_code); handled = true; } @@ -285,8 +286,9 @@ void BrowserView::PrepareToRunSystemMenu(HMENU menu) { // |command| can be zero on submenu items (IDS_ENCODING, // IDS_ZOOM) and on separators. if (command != 0) { - system_menu_->EnableMenuItemByID(command, - browser_->IsCommandEnabled(command)); + system_menu_->EnableMenuItemByID( + command, + browser_->command_updater()->IsCommandEnabled(command)); } } } @@ -335,7 +337,7 @@ void BrowserView::Init() { tabstrip_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_TABSTRIP)); AddChildView(tabstrip_); - toolbar_ = new BrowserToolbarView(browser_->controller(), browser_.get()); + toolbar_ = new BrowserToolbarView(browser_.get()); AddChildView(toolbar_); toolbar_->SetID(VIEW_ID_TOOLBAR); toolbar_->Init(browser_->profile()); @@ -730,8 +732,8 @@ bool BrowserView::ExecuteWindowsCommand(int command_id) { if (command_id_from_app_command != -1) command_id = command_id_from_app_command; - if (browser_->SupportsCommand(command_id)) { - if (browser_->IsCommandEnabled(command_id)) + if (browser_->command_updater()->SupportsCommand(command_id)) { + if (browser_->command_updater()->IsCommandEnabled(command_id)) browser_->ExecuteCommand(command_id); return true; } @@ -1256,8 +1258,7 @@ void BrowserView::LoadAccelerators() { void BrowserView::BuildMenuForTabStriplessWindow(Menu* menu, int insertion_index) { encoding_menu_delegate_.reset(new EncodingMenuControllerDelegate( - browser_.get(), - browser_->controller())); + browser_.get())); for (int i = 0; i < arraysize(kMenuLayout); ++i) { if (kMenuLayout[i].separator) { diff --git a/chrome/browser/views/go_button.cc b/chrome/browser/views/go_button.cc index f9025fb..47f6175 100644 --- a/chrome/browser/views/go_button.cc +++ b/chrome/browser/views/go_button.cc @@ -5,15 +5,16 @@ #include "chrome/browser/views/go_button.h" #include "chrome/app/chrome_dll_resource.h" +#include "chrome/browser/command_updater.h" #include "chrome/browser/views/location_bar_view.h" #include "chrome/common/l10n_util.h" #include "generated_resources.h" GoButton::GoButton(LocationBarView* location_bar, - CommandController* controller) : ToggleButton(), + CommandUpdater* command_updater) : ToggleButton(), location_bar_(location_bar), - controller_(controller), + command_updater_(command_updater), intended_mode_(MODE_GO), visible_mode_(MODE_GO), button_delay_(NULL), @@ -27,14 +28,14 @@ GoButton::~GoButton() { void GoButton::NotifyClick(int mouse_event_flags) { if (visible_mode_ == MODE_STOP) { - controller_->ExecuteCommand(IDC_STOP); + command_updater_->ExecuteCommand(IDC_STOP); // The user has clicked, so we can feel free to update the button, // even if the mouse is still hovering. ChangeMode(MODE_GO); } else if (visible_mode_ == MODE_GO && stop_timer_.empty()) { // If the go button is visible and not within the doubleclick timer, go. - controller_->ExecuteCommand(IDC_GO); + command_updater_->ExecuteCommand(IDC_GO); // Figure out the system double-click time. if (button_delay_ == NULL) diff --git a/chrome/browser/views/go_button.h b/chrome/browser/views/go_button.h index 1cfd107..4566184 100644 --- a/chrome/browser/views/go_button.h +++ b/chrome/browser/views/go_button.h @@ -6,9 +6,9 @@ #define CHROME_BROWSER_VIEWS_GO_BUTTON_H__ #include "chrome/views/button.h" -#include "chrome/browser/controller.h" #include "base/task.h" +class CommandUpdater; class LocationBarView; //////////////////////////////////////////////////////////////////////////////// @@ -25,7 +25,9 @@ class LocationBarView; class GoButton : public views::ToggleButton { public: - GoButton(LocationBarView* location_bar, CommandController* controller); + // TODO(beng): get rid of the command updater param and instead have a + // delegate. + GoButton(LocationBarView* location_bar, CommandUpdater* command_updater); virtual ~GoButton(); typedef enum Mode { MODE_GO = 0, MODE_STOP }; @@ -49,7 +51,7 @@ class GoButton : public views::ToggleButton { ScopedRunnableMethodFactory stop_timer_; LocationBarView* location_bar_; - CommandController* controller_; + CommandUpdater* command_updater_; ButtonListener* listener_; // The mode we should be in diff --git a/chrome/browser/views/location_bar_view.cc b/chrome/browser/views/location_bar_view.cc index 8c727b7..aa6fb1d 100644 --- a/chrome/browser/views/location_bar_view.cc +++ b/chrome/browser/views/location_bar_view.cc @@ -12,6 +12,7 @@ #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/command_updater.h" #include "chrome/browser/profile.h" #include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url_model.h" @@ -77,12 +78,12 @@ static std::wstring GetKeywordDescription(Profile* profile, } LocationBarView::LocationBarView(Profile* profile, - CommandController* controller, + CommandUpdater* command_updater, ToolbarModel* model, Delegate* delegate, bool popup_window_mode) : profile_(profile), - controller_(controller), + command_updater_(command_updater), model_(model), delegate_(delegate), disposition_(CURRENT_TAB), @@ -126,7 +127,7 @@ void LocationBarView::Init() { views::Widget* widget = GetWidget(); location_entry_.reset(new AutocompleteEditView(font_, this, model_, this, widget->GetHWND(), - profile_, controller_, + profile_, command_updater_, popup_window_mode_)); // View container for URL edit field. @@ -319,9 +320,9 @@ void LocationBarView::OnAutocompleteAccept( disposition_ = disposition; transition_ = transition; - if (controller_) { + if (command_updater_) { if (!alternate_nav_url.is_valid()) { - controller_->ExecuteCommand(IDC_OPEN_CURRENT_URL); + command_updater_->ExecuteCommand(IDC_OPEN_CURRENT_URL); return; } @@ -330,7 +331,7 @@ void LocationBarView::OnAutocompleteAccept( // The AlternateNavURLFetcher will listen for the pending navigation // notification that will be issued as a result of the "open URL." It // will automatically install itself into that navigation controller. - controller_->ExecuteCommand(IDC_OPEN_CURRENT_URL); + command_updater_->ExecuteCommand(IDC_OPEN_CURRENT_URL); if (fetcher->state() == AlternateNavURLFetcher::NOT_STARTED) { // I'm not sure this should be reachable, but I'm not also sure enough // that it shouldn't to stick in a NOTREACHED(). In any case, this is diff --git a/chrome/browser/views/location_bar_view.h b/chrome/browser/views/location_bar_view.h index ad99fb5..eb3b4ef 100644 --- a/chrome/browser/views/location_bar_view.h +++ b/chrome/browser/views/location_bar_view.h @@ -9,7 +9,6 @@ #include "base/gfx/rect.h" #include "chrome/browser/autocomplete/autocomplete_edit.h" -#include "chrome/browser/controller.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/toolbar_model.h" #include "chrome/browser/views/info_bubble.h" @@ -19,6 +18,7 @@ #include "chrome/views/label.h" #include "chrome/views/painter.h" +class CommandUpdater; class GURL; class Profile; @@ -46,7 +46,7 @@ class LocationBarView : public views::View, }; LocationBarView(Profile* profile, - CommandController* controller, + CommandUpdater* command_updater, ToolbarModel* model_, Delegate* delegate, bool popup_window_mode); @@ -339,8 +339,8 @@ class LocationBarView : public views::View, // The Autocomplete Edit field. scoped_ptr location_entry_; - // The command controller for this View. - CommandController* controller_; + // The CommandUpdater for the Browser object that corresponds to this View. + CommandUpdater* command_updater_; // The model. ToolbarModel* model_; diff --git a/chrome/browser/views/toolbar_view.cc b/chrome/browser/views/toolbar_view.cc index 091c97e..f6ed905 100644 --- a/chrome/browser/views/toolbar_view.cc +++ b/chrome/browser/views/toolbar_view.cc @@ -60,10 +60,8 @@ static const int kMenuButtonOffset = 3; // Padding to the right of the location bar static const int kPaddingRight = 2; -BrowserToolbarView::BrowserToolbarView(CommandController* controller, - Browser* browser) - : EncodingMenuControllerDelegate(browser, controller), - controller_(controller), +BrowserToolbarView::BrowserToolbarView(Browser* browser) + : EncodingMenuControllerDelegate(browser), model_(browser->toolbar_model()), back_(NULL), forward_(NULL), @@ -78,11 +76,15 @@ BrowserToolbarView::BrowserToolbarView(CommandController* controller, tab_(NULL), profiles_helper_(new GetProfilesHelper(this)), profiles_menu_(NULL) { + browser_->command_updater()->AddCommandObserver(IDC_BACK, this); + browser_->command_updater()->AddCommandObserver(IDC_FORWARD, this); + browser_->command_updater()->AddCommandObserver(IDC_RELOAD, this); + browser_->command_updater()->AddCommandObserver(IDC_HOME, this); + browser_->command_updater()->AddCommandObserver(IDC_STAR, this); back_menu_model_.reset(new BackForwardMenuModel( browser, BackForwardMenuModel::BACKWARD_MENU_DELEGATE)); forward_menu_model_.reset(new BackForwardMenuModel( browser, BackForwardMenuModel::FORWARD_MENU_DELEGATE)); - if (browser->type() == Browser::TYPE_NORMAL) display_mode_ = DISPLAYMODE_NORMAL; else @@ -116,6 +118,7 @@ void BrowserToolbarView::CreateLeftSideControls() { ResourceBundle &rb = ResourceBundle::GetSharedInstance(); back_ = new views::ButtonDropDown(back_menu_model_.get()); + back_->SetListener(this, IDC_BACK); back_->SetImageAlignment(views::Button::ALIGN_RIGHT, views::Button::ALIGN_TOP); back_->SetImage(views::Button::BS_NORMAL, rb.GetBitmapNamed(IDR_BACK)); @@ -126,9 +129,9 @@ void BrowserToolbarView::CreateLeftSideControls() { back_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_BACK)); back_->SetID(VIEW_ID_BACK_BUTTON); AddChildView(back_); - controller_->AddManagedButton(back_, IDC_BACK); forward_ = new views::ButtonDropDown(forward_menu_model_.get()); + forward_->SetListener(this, IDC_FORWARD); forward_->SetImage(views::Button::BS_NORMAL, rb.GetBitmapNamed(IDR_FORWARD)); forward_->SetImage(views::Button::BS_HOT, rb.GetBitmapNamed(IDR_FORWARD_H)); forward_->SetImage(views::Button::BS_PUSHED, @@ -139,9 +142,9 @@ void BrowserToolbarView::CreateLeftSideControls() { forward_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_FORWARD)); forward_->SetID(VIEW_ID_FORWARD_BUTTON); AddChildView(forward_); - controller_->AddManagedButton(forward_, IDC_FORWARD); reload_ = new views::Button(); + reload_->SetListener(this, IDC_RELOAD); reload_->SetImage(views::Button::BS_NORMAL, rb.GetBitmapNamed(IDR_RELOAD)); reload_->SetImage(views::Button::BS_HOT, rb.GetBitmapNamed(IDR_RELOAD_H)); reload_->SetImage(views::Button::BS_PUSHED, rb.GetBitmapNamed(IDR_RELOAD_P)); @@ -149,9 +152,9 @@ void BrowserToolbarView::CreateLeftSideControls() { reload_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_RELOAD)); reload_->SetID(VIEW_ID_RELOAD_BUTTON); AddChildView(reload_); - controller_->AddManagedButton(reload_, IDC_RELOAD); home_ = new views::Button(); + home_->SetListener(this, IDC_HOME); home_->SetImage(views::Button::BS_NORMAL, rb.GetBitmapNamed(IDR_HOME)); home_->SetImage(views::Button::BS_HOT, rb.GetBitmapNamed(IDR_HOME_H)); home_->SetImage(views::Button::BS_PUSHED, rb.GetBitmapNamed(IDR_HOME_P)); @@ -159,13 +162,13 @@ void BrowserToolbarView::CreateLeftSideControls() { home_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_HOME)); home_->SetID(VIEW_ID_HOME_BUTTON); AddChildView(home_); - controller_->AddManagedButton(home_, IDC_HOME); } void BrowserToolbarView::CreateCenterStack(Profile *profile) { ResourceBundle &rb = ResourceBundle::GetSharedInstance(); star_ = new ToolbarStarToggle(this); + star_->SetListener(this, IDC_STAR); star_->SetImage(views::Button::BS_NORMAL, rb.GetBitmapNamed(IDR_STAR)); star_->SetImage(views::Button::BS_HOT, rb.GetBitmapNamed(IDR_STAR_H)); star_->SetImage(views::Button::BS_PUSHED, rb.GetBitmapNamed(IDR_STAR_P)); @@ -182,15 +185,15 @@ void BrowserToolbarView::CreateCenterStack(Profile *profile) { star_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_STAR)); star_->SetID(VIEW_ID_STAR_BUTTON); AddChildView(star_); - controller_->AddManagedButton(star_, IDC_STAR); - location_bar_ = new LocationBarView(profile, controller_, model_, this, + location_bar_ = new LocationBarView(profile, browser_->command_updater(), + model_, this, display_mode_ == DISPLAYMODE_LOCATION); AddChildView(location_bar_); location_bar_->Init(); // The Go button. - go_ = new GoButton(location_bar_, controller_); + go_ = new GoButton(location_bar_, browser_->command_updater()); go_->SetImage(views::Button::BS_NORMAL, rb.GetBitmapNamed(IDR_GO)); go_->SetImage(views::Button::BS_HOT, rb.GetBitmapNamed(IDR_GO_H)); go_->SetImage(views::Button::BS_PUSHED, rb.GetBitmapNamed(IDR_GO_P)); @@ -750,6 +753,33 @@ TabContents* BrowserToolbarView::GetTabContents() { return tab_; } +void BrowserToolbarView::EnabledStateChangedForCommand(int id, bool enabled) { + views::Button* button = NULL; + switch (id) { + case IDC_BACK: + button = back_; + break; + case IDC_FORWARD: + button = forward_; + break; + case IDC_RELOAD: + button = reload_; + break; + case IDC_HOME: + button = home_; + break; + case IDC_STAR: + button = star_; + break; + } + if (button) + button->SetEnabled(enabled); +} + +void BrowserToolbarView::ButtonPressed(views::BaseButton* sender) { + browser_->ExecuteCommand(sender->GetTag()); +} + void BrowserToolbarView::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { diff --git a/chrome/browser/views/toolbar_view.h b/chrome/browser/views/toolbar_view.h index a927f0c..a4b6ec0 100644 --- a/chrome/browser/views/toolbar_view.h +++ b/chrome/browser/views/toolbar_view.h @@ -10,7 +10,7 @@ #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "chrome/browser/back_forward_menu_model.h" -#include "chrome/browser/controller.h" +#include "chrome/browser/command_updater.h" #include "chrome/browser/encoding_menu_controller_delegate.h" #include "chrome/browser/user_data_manager.h" #include "chrome/browser/views/dom_view.h" @@ -40,9 +40,11 @@ class BrowserToolbarView : public views::View, public views::DragController, public LocationBarView::Delegate, public NotificationObserver, - public GetProfilesHelper::Delegate { + public GetProfilesHelper::Delegate, + public CommandUpdater::CommandObserver, + public views::BaseButton::ButtonListener { public: - BrowserToolbarView(CommandController* controller, Browser* browser); + explicit BrowserToolbarView(Browser* browser); virtual ~BrowserToolbarView(); // Create the contents of the Browser Toolbar @@ -116,6 +118,12 @@ class BrowserToolbarView : public views::View, Browser* browser() { return browser_; } + // Overridden from CommandUpdater::CommandObserver: + virtual void EnabledStateChangedForCommand(int id, bool enabled); + + // Overridden from views::BaseButton::ButtonListener: + virtual void ButtonPressed(views::BaseButton* sender); + private: // NotificationObserver virtual void Observe(NotificationType type, @@ -159,9 +167,6 @@ class BrowserToolbarView : public views::View, return display_mode_ == DISPLAYMODE_NORMAL; } - // This View's Command Controller - CommandController* controller_; - scoped_ptr back_menu_model_; scoped_ptr forward_menu_model_; -- cgit v1.1