1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_RELOAD_BUTTON_H__ 6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_RELOAD_BUTTON_H__ 7 8 #include "base/basictypes.h" 9 #include "base/gtest_prod_util.h" 10 #include "base/timer/timer.h" 11 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" 12 #include "ui/base/models/simple_menu_model.h" 13 #include "ui/views/controls/button/button.h" 14 15 class CommandUpdater; 16 17 //////////////////////////////////////////////////////////////////////////////// 18 // 19 // ReloadButton 20 // 21 // The reload button in the toolbar, which changes to a stop button when a page 22 // load is in progress. Trickiness comes from the desire to have the 'stop' 23 // button not change back to 'reload' if the user's mouse is hovering over it 24 // (to prevent mis-clicks). 25 // 26 //////////////////////////////////////////////////////////////////////////////// 27 28 class ReloadButton : public ToolbarButton, 29 public views::ButtonListener, 30 public ui::SimpleMenuModel::Delegate { 31 public: 32 enum Mode { MODE_RELOAD = 0, MODE_STOP }; 33 34 // The button's class name. 35 static const char kViewClassName[]; 36 37 explicit ReloadButton(CommandUpdater* command_updater); 38 virtual ~ReloadButton(); 39 40 // Ask for a specified button state. If |force| is true this will be applied 41 // immediately. 42 void ChangeMode(Mode mode, bool force); 43 44 // Enable reload drop-down menu. set_menu_enabled(bool enable)45 void set_menu_enabled(bool enable) { menu_enabled_ = enable; } 46 47 void LoadImages(); 48 49 // ToolbarButton: 50 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; 51 virtual bool GetTooltipText(const gfx::Point& p, 52 base::string16* tooltip) const OVERRIDE; 53 virtual const char* GetClassName() const OVERRIDE; 54 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 55 virtual bool ShouldShowMenu() OVERRIDE; 56 virtual void ShowDropDownMenu(ui::MenuSourceType source_type) OVERRIDE; 57 58 // views::ButtonListener: 59 virtual void ButtonPressed(views::Button* /* button */, 60 const ui::Event& event) OVERRIDE; 61 62 // ui::SimpleMenuModel::Delegate: 63 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 64 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 65 virtual bool IsCommandIdVisible(int command_id) const OVERRIDE; 66 virtual bool GetAcceleratorForCommandId( 67 int command_id, 68 ui::Accelerator* accelerator) OVERRIDE; 69 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 70 71 private: 72 friend class ReloadButtonTest; 73 74 ui::SimpleMenuModel* CreateMenuModel(); 75 76 void ExecuteBrowserCommand(int command, int event_flags); 77 void ChangeModeInternal(Mode mode); 78 79 void OnDoubleClickTimer(); 80 void OnStopToReloadTimer(); 81 82 base::OneShotTimer<ReloadButton> double_click_timer_; 83 base::OneShotTimer<ReloadButton> stop_to_reload_timer_; 84 85 // This may be NULL when testing. 86 CommandUpdater* command_updater_; 87 88 // The mode we should be in assuming no timers are running. 89 Mode intended_mode_; 90 91 // The currently-visible mode - this may differ from the intended mode. 92 Mode visible_mode_; 93 94 // The delay times for the timers. These are members so that tests can modify 95 // them. 96 base::TimeDelta double_click_timer_delay_; 97 base::TimeDelta stop_to_reload_timer_delay_; 98 99 // Indicates if reload menu is enabled. 100 bool menu_enabled_; 101 102 // TESTING ONLY 103 // True if we should pretend the button is hovered. 104 bool testing_mouse_hovered_; 105 // Increments when we would tell the browser to "reload", so 106 // test code can tell whether we did so (as there may be no |browser_|). 107 int testing_reload_count_; 108 109 DISALLOW_IMPLICIT_CONSTRUCTORS(ReloadButton); 110 }; 111 112 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_RELOAD_BUTTON_H__ 113