Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: chrome/browser/ui/views/toolbar/reload_button.cc

Issue 62873007: [Toolbar] Base toolbar button class with background images for button states (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add back button subclass Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/toolbar/reload_button.h" 5 #include "chrome/browser/ui/views/toolbar/reload_button.h"
6 6
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/app/chrome_command_ids.h" 8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/command_updater.h" 9 #include "chrome/browser/command_updater.h"
10 #include "chrome/browser/search/search.h" 10 #include "chrome/browser/search/search.h"
11 #include "chrome/browser/ui/search/search_model.h" 11 #include "chrome/browser/ui/search/search_model.h"
12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" 12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
13 #include "grit/generated_resources.h" 13 #include "grit/generated_resources.h"
14 #include "grit/theme_resources.h" 14 #include "grit/theme_resources.h"
15 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/models/simple_menu_model.h" 16 #include "ui/base/models/simple_menu_model.h"
17 #include "ui/base/theme_provider.h" 17 #include "ui/base/theme_provider.h"
18 #include "ui/base/window_open_disposition.h" 18 #include "ui/base/window_open_disposition.h"
19 #include "ui/views/metrics.h" 19 #include "ui/views/metrics.h"
20 #include "ui/views/widget/widget.h" 20 #include "ui/views/widget/widget.h"
21 21
22 22
23 namespace { 23 namespace {
24 24
25 const int kReloadImages[] =
26 { IDR_RELOAD, IDR_RELOAD_H, IDR_RELOAD_P, IDR_RELOAD_D };
27
28 const int kStopImages[] = { IDR_STOP, IDR_STOP_H, IDR_STOP_P, IDR_STOP_D };
29
30 // Contents of the Reload drop-down menu. 25 // Contents of the Reload drop-down menu.
31 const int kReloadMenuItems[] = { 26 const int kReloadMenuItems[] = {
32 IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM, 27 IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM,
33 IDS_RELOAD_MENU_HARD_RELOAD_ITEM, 28 IDS_RELOAD_MENU_HARD_RELOAD_ITEM,
34 IDS_RELOAD_MENU_EMPTY_AND_HARD_RELOAD_ITEM, 29 IDS_RELOAD_MENU_EMPTY_AND_HARD_RELOAD_ITEM,
35 }; 30 };
36 31
37 } // namespace 32 } // namespace
38 33
39 34
40 // ReloadButton --------------------------------------------------------------- 35 // ReloadButton ---------------------------------------------------------------
41 36
42 // static 37 // static
43 const char ReloadButton::kViewClassName[] = "ReloadButton"; 38 const char ReloadButton::kViewClassName[] = "ReloadButton";
44 39
45 ReloadButton::ReloadButton(LocationBarView* location_bar, 40 ReloadButton::ReloadButton(LocationBarView* location_bar,
46 CommandUpdater* command_updater) 41 CommandUpdater* command_updater)
47 : ButtonDropDown(this, CreateMenuModel()), 42 : ToolbarButton(this, CreateMenuModel()),
48 location_bar_(location_bar), 43 location_bar_(location_bar),
49 command_updater_(command_updater), 44 command_updater_(command_updater),
50 intended_mode_(MODE_RELOAD), 45 intended_mode_(MODE_RELOAD),
51 visible_mode_(MODE_RELOAD), 46 visible_mode_(MODE_RELOAD),
52 double_click_timer_delay_( 47 double_click_timer_delay_(
53 base::TimeDelta::FromMilliseconds(views::GetDoubleClickInterval())), 48 base::TimeDelta::FromMilliseconds(views::GetDoubleClickInterval())),
54 stop_to_reload_timer_delay_(base::TimeDelta::FromMilliseconds(1350)), 49 stop_to_reload_timer_delay_(base::TimeDelta::FromMilliseconds(1350)),
55 menu_enabled_(false), 50 menu_enabled_(false),
56 testing_mouse_hovered_(false), 51 testing_mouse_hovered_(false),
57 testing_reload_count_(0) { 52 testing_reload_count_(0) {
58 } 53 }
59 54
60 ReloadButton::~ReloadButton() { 55 ReloadButton::~ReloadButton() {
61 } 56 }
62 57
63 void ReloadButton::ChangeMode(Mode mode, bool force) { 58 void ReloadButton::ChangeMode(Mode mode, bool force) {
64 intended_mode_ = mode; 59 intended_mode_ = mode;
65 60
66 // If the change is forced, or the user isn't hovering the icon, or it's safe 61 // If the change is forced, or the user isn't hovering the icon, or it's safe
67 // to change it to the other image type, make the change immediately; 62 // to change it to the other image type, make the change immediately;
68 // otherwise we'll let it happen later. 63 // otherwise we'll let it happen later.
69 if (force || (!IsMouseHovered() && !testing_mouse_hovered_) || 64 if (force || (!IsMouseHovered() && !testing_mouse_hovered_) ||
70 ((mode == MODE_STOP) ? 65 ((mode == MODE_STOP) ?
71 !double_click_timer_.IsRunning() : (visible_mode_ != MODE_STOP))) { 66 !double_click_timer_.IsRunning() : (visible_mode_ != MODE_STOP))) {
72 double_click_timer_.Stop(); 67 double_click_timer_.Stop();
73 stop_to_reload_timer_.Stop(); 68 stop_to_reload_timer_.Stop();
74 ChangeModeInternal(mode); 69 if (mode != visible_mode_)
70 ChangeModeInternal(mode);
75 SetEnabled(true); 71 SetEnabled(true);
76 72
77 // We want to disable the button if we're preventing a change from stop to 73 // We want to disable the button if we're preventing a change from stop to
78 // reload due to hovering, but not if we're preventing a change from reload to 74 // reload due to hovering, but not if we're preventing a change from reload to
79 // stop due to the double-click timer running. (Disabled reload state is only 75 // stop due to the double-click timer running. (Disabled reload state is only
80 // applicable when instant extended API is enabled and mode is NTP, which is 76 // applicable when instant extended API is enabled and mode is NTP, which is
81 // handled just above.) 77 // handled just above.)
82 } else if (visible_mode_ != MODE_RELOAD) { 78 } else if (visible_mode_ != MODE_RELOAD) {
83 SetEnabled(false); 79 SetEnabled(false);
84 80
85 // Go ahead and change to reload after a bit, which allows repeated reloads 81 // Go ahead and change to reload after a bit, which allows repeated reloads
86 // without moving the mouse. 82 // without moving the mouse.
87 if (!stop_to_reload_timer_.IsRunning()) { 83 if (!stop_to_reload_timer_.IsRunning()) {
88 stop_to_reload_timer_.Start(FROM_HERE, stop_to_reload_timer_delay_, this, 84 stop_to_reload_timer_.Start(FROM_HERE, stop_to_reload_timer_delay_, this,
89 &ReloadButton::OnStopToReloadTimer); 85 &ReloadButton::OnStopToReloadTimer);
90 } 86 }
91 } 87 }
92 } 88 }
93 89
94 void ReloadButton::LoadImages() { 90 void ReloadButton::LoadImages() {
95 DCHECK_EQ(static_cast<int>(arraysize(kReloadImages)), STATE_COUNT); 91 ChangeModeInternal(visible_mode_);
96 DCHECK_EQ(static_cast<int>(arraysize(kStopImages)), STATE_COUNT);
97
98 gfx::ImageSkia* reload_images = images_;
99 gfx::ImageSkia* stop_images = alternate_images_;
100 if (visible_mode_ == MODE_STOP)
101 std::swap(reload_images, stop_images);
102
103 ui::ThemeProvider* tp = GetThemeProvider();
104 for (int i = 0; i < STATE_COUNT; i++) {
105 reload_images[i] = *(tp->GetImageSkiaNamed(kReloadImages[i]));
106 stop_images[i] = *(tp->GetImageSkiaNamed(kStopImages[i]));
107 }
108 92
109 SchedulePaint(); 93 SchedulePaint();
110 PreferredSizeChanged(); 94 PreferredSizeChanged();
111 } 95 }
112 96
113 void ReloadButton::OnMouseExited(const ui::MouseEvent& event) { 97 void ReloadButton::OnMouseExited(const ui::MouseEvent& event) {
114 ButtonDropDown::OnMouseExited(event); 98 ToolbarButton::OnMouseExited(event);
115 if (!IsMenuShowing()) 99 if (!IsMenuShowing())
116 ChangeMode(intended_mode_, true); 100 ChangeMode(intended_mode_, true);
117 } 101 }
118 102
119 bool ReloadButton::GetTooltipText(const gfx::Point& p, 103 bool ReloadButton::GetTooltipText(const gfx::Point& p,
120 string16* tooltip) const { 104 string16* tooltip) const {
121 int reload_tooltip = menu_enabled_ ? 105 int reload_tooltip = menu_enabled_ ?
122 IDS_TOOLTIP_RELOAD_WITH_MENU : IDS_TOOLTIP_RELOAD; 106 IDS_TOOLTIP_RELOAD_WITH_MENU : IDS_TOOLTIP_RELOAD;
123 int text_id = (visible_mode_ == MODE_RELOAD) ? 107 int text_id = (visible_mode_ == MODE_RELOAD) ?
124 reload_tooltip : IDS_TOOLTIP_STOP; 108 reload_tooltip : IDS_TOOLTIP_STOP;
125 tooltip->assign(l10n_util::GetStringUTF16(text_id)); 109 tooltip->assign(l10n_util::GetStringUTF16(text_id));
126 return true; 110 return true;
127 } 111 }
128 112
129 const char* ReloadButton::GetClassName() const { 113 const char* ReloadButton::GetClassName() const {
130 return kViewClassName; 114 return kViewClassName;
131 } 115 }
132 116
133 void ReloadButton::GetAccessibleState(ui::AccessibleViewState* state) { 117 void ReloadButton::GetAccessibleState(ui::AccessibleViewState* state) {
134 if (menu_enabled_) 118 if (menu_enabled_)
135 ButtonDropDown::GetAccessibleState(state); 119 ToolbarButton::GetAccessibleState(state);
136 else 120 else
137 CustomButton::GetAccessibleState(state); 121 CustomButton::GetAccessibleState(state);
138 } 122 }
139 123
140 bool ReloadButton::ShouldShowMenu() { 124 bool ReloadButton::ShouldShowMenu() {
141 return menu_enabled_ && (visible_mode_ == MODE_RELOAD); 125 return menu_enabled_ && (visible_mode_ == MODE_RELOAD);
142 } 126 }
143 127
144 void ReloadButton::ShowDropDownMenu(ui::MenuSourceType source_type) { 128 void ReloadButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
145 ButtonDropDown::ShowDropDownMenu(source_type); // Blocks. 129 ToolbarButton::ShowDropDownMenu(source_type); // Blocks.
146 ChangeMode(intended_mode_, true); 130 ChangeMode(intended_mode_, true);
147 } 131 }
148 132
149 void ReloadButton::ButtonPressed(views::Button* /* button */, 133 void ReloadButton::ButtonPressed(views::Button* /* button */,
150 const ui::Event& event) { 134 const ui::Event& event) {
151 ClearPendingMenu(); 135 ClearPendingMenu();
152 136
153 if (visible_mode_ == MODE_STOP) { 137 if (visible_mode_ == MODE_STOP) {
154 if (command_updater_) 138 if (command_updater_)
155 command_updater_->ExecuteCommandWithDisposition(IDC_STOP, CURRENT_TAB); 139 command_updater_->ExecuteCommandWithDisposition(IDC_STOP, CURRENT_TAB);
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 if ((disposition == CURRENT_TAB) && location_bar_) { 226 if ((disposition == CURRENT_TAB) && location_bar_) {
243 // Forcibly reset the location bar, since otherwise it won't discard any 227 // Forcibly reset the location bar, since otherwise it won't discard any
244 // ongoing user edits, since it doesn't realize this is a user-initiated 228 // ongoing user edits, since it doesn't realize this is a user-initiated
245 // action. 229 // action.
246 location_bar_->Revert(); 230 location_bar_->Revert();
247 } 231 }
248 command_updater_->ExecuteCommandWithDisposition(command, disposition); 232 command_updater_->ExecuteCommandWithDisposition(command, disposition);
249 } 233 }
250 234
251 void ReloadButton::ChangeModeInternal(Mode mode) { 235 void ReloadButton::ChangeModeInternal(Mode mode) {
252 if (visible_mode_ == mode) 236 ui::ThemeProvider* tp = GetThemeProvider();
253 return; 237 // Needed for unit tests.
Peter Kasting 2013/11/26 22:24:31 Nit: Does this mean "|tp| can be NULL in unit test
Greg Billock 2013/11/27 14:22:35 Done.
238 if (tp) {
239 SetImage(views::Button::STATE_NORMAL, *(tp->GetImageSkiaNamed(
240 (mode == MODE_RELOAD) ? IDR_RELOAD : IDR_STOP)));
241 SetImage(views::Button::STATE_DISABLED, *(tp->GetImageSkiaNamed(
242 (mode == MODE_RELOAD) ? IDR_RELOAD_D : IDR_STOP_D)));
243 }
254 244
255 for (size_t i = 0; i < STATE_COUNT; ++i)
256 std::swap(images_[i], alternate_images_[i]);
257 visible_mode_ = mode; 245 visible_mode_ = mode;
258 SchedulePaint(); 246 SchedulePaint();
259 } 247 }
260 248
261 void ReloadButton::OnDoubleClickTimer() { 249 void ReloadButton::OnDoubleClickTimer() {
262 if (!IsMenuShowing()) 250 if (!IsMenuShowing())
263 ChangeMode(intended_mode_, false); 251 ChangeMode(intended_mode_, false);
264 } 252 }
265 253
266 void ReloadButton::OnStopToReloadTimer() { 254 void ReloadButton::OnStopToReloadTimer() {
267 DCHECK(!IsMenuShowing()); 255 DCHECK(!IsMenuShowing());
268 ChangeMode(intended_mode_, true); 256 ChangeMode(intended_mode_, true);
269 } 257 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698