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