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

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 home button Created 7 years, 1 month 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
Peter Kasting 2013/11/19 02:28:50 Nit: Extra blank line. I actually preferred the o
Greg Billock 2013/11/20 00:59:03 Done.
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 ui::ThemeProvider* tp = GetThemeProvider(); 91 ChangeModeInternal(visible_mode_);
96
97 DCHECK_EQ(static_cast<int>(arraysize(kReloadImages)), STATE_COUNT);
98 DCHECK_EQ(static_cast<int>(arraysize(kStopImages)), STATE_COUNT);
99
100 gfx::ImageSkia* reload_images = images_;
101 gfx::ImageSkia* stop_images = alternate_images_;
102 if (visible_mode_ == MODE_STOP)
103 std::swap(reload_images, stop_images);
104
105 for (int i = 0; i < STATE_COUNT; i++) {
106 reload_images[i] = *(tp->GetImageSkiaNamed(kReloadImages[i]));
107 stop_images[i] = *(tp->GetImageSkiaNamed(kStopImages[i]));
108 }
109 92
110 SchedulePaint(); 93 SchedulePaint();
111 PreferredSizeChanged(); 94 PreferredSizeChanged();
112 } 95 }
113 96
114 void ReloadButton::OnMouseExited(const ui::MouseEvent& event) { 97 void ReloadButton::OnMouseExited(const ui::MouseEvent& event) {
115 ButtonDropDown::OnMouseExited(event); 98 ToolbarButton::OnMouseExited(event);
116 if (!IsMenuShowing()) 99 if (!IsMenuShowing())
117 ChangeMode(intended_mode_, true); 100 ChangeMode(intended_mode_, true);
118 } 101 }
119 102
120 bool ReloadButton::GetTooltipText(const gfx::Point& p, 103 bool ReloadButton::GetTooltipText(const gfx::Point& p,
121 string16* tooltip) const { 104 string16* tooltip) const {
122 int reload_tooltip = menu_enabled_ ? 105 int reload_tooltip = menu_enabled_ ?
123 IDS_TOOLTIP_RELOAD_WITH_MENU : IDS_TOOLTIP_RELOAD; 106 IDS_TOOLTIP_RELOAD_WITH_MENU : IDS_TOOLTIP_RELOAD;
124 int text_id = (visible_mode_ == MODE_RELOAD) ? 107 int text_id = (visible_mode_ == MODE_RELOAD) ?
125 reload_tooltip : IDS_TOOLTIP_STOP; 108 reload_tooltip : IDS_TOOLTIP_STOP;
126 tooltip->assign(l10n_util::GetStringUTF16(text_id)); 109 tooltip->assign(l10n_util::GetStringUTF16(text_id));
127 return true; 110 return true;
128 } 111 }
129 112
130 const char* ReloadButton::GetClassName() const { 113 const char* ReloadButton::GetClassName() const {
131 return kViewClassName; 114 return kViewClassName;
132 } 115 }
133 116
134 void ReloadButton::GetAccessibleState(ui::AccessibleViewState* state) { 117 void ReloadButton::GetAccessibleState(ui::AccessibleViewState* state) {
135 if (menu_enabled_) 118 if (menu_enabled_)
136 ButtonDropDown::GetAccessibleState(state); 119 ToolbarButton::GetAccessibleState(state);
137 else 120 else
138 CustomButton::GetAccessibleState(state); 121 CustomButton::GetAccessibleState(state);
139 } 122 }
140 123
141 bool ReloadButton::ShouldShowMenu() { 124 bool ReloadButton::ShouldShowMenu() {
142 return menu_enabled_ && (visible_mode_ == MODE_RELOAD); 125 return menu_enabled_ && (visible_mode_ == MODE_RELOAD);
143 } 126 }
144 127
145 void ReloadButton::ShowDropDownMenu(ui::MenuSourceType source_type) { 128 void ReloadButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
146 ButtonDropDown::ShowDropDownMenu(source_type); // Blocks. 129 ToolbarButton::ShowDropDownMenu(source_type); // Blocks.
147 ChangeMode(intended_mode_, true); 130 ChangeMode(intended_mode_, true);
148 } 131 }
149 132
150 void ReloadButton::ButtonPressed(views::Button* /* button */, 133 void ReloadButton::ButtonPressed(views::Button* /* button */,
151 const ui::Event& event) { 134 const ui::Event& event) {
152 ClearPendingMenu(); 135 ClearPendingMenu();
153 136
154 if (visible_mode_ == MODE_STOP) { 137 if (visible_mode_ == MODE_STOP) {
155 if (command_updater_) 138 if (command_updater_)
156 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
243 if ((disposition == CURRENT_TAB) && location_bar_) { 226 if ((disposition == CURRENT_TAB) && location_bar_) {
244 // 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
245 // 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
246 // action. 229 // action.
247 location_bar_->Revert(); 230 location_bar_->Revert();
248 } 231 }
249 command_updater_->ExecuteCommandWithDisposition(command, disposition); 232 command_updater_->ExecuteCommandWithDisposition(command, disposition);
250 } 233 }
251 234
252 void ReloadButton::ChangeModeInternal(Mode mode) { 235 void ReloadButton::ChangeModeInternal(Mode mode) {
253 if (visible_mode_ == mode) 236 ui::ThemeProvider* tp = GetThemeProvider();
254 return; 237 LOG(INFO) << "Switch to mode " << mode;
Peter Kasting 2013/11/19 02:28:50 Don't land this
Greg Billock 2013/11/20 00:59:03 Removed debugging stuff.
255 238
256 for (size_t i = 0; i < STATE_COUNT; ++i) 239 if (mode == MODE_RELOAD) {
Peter Kasting 2013/11/19 02:28:50 Nit: Shorter: SetImage(views::Button::STATE_NOR
Greg Billock 2013/11/20 00:59:03 Done.
257 std::swap(images_[i], alternate_images_[i]); 240 SetImage(views::CustomButton::STATE_NORMAL,
Peter Kasting 2013/11/19 02:28:50 Nit: Qualify this with Button:: instead of CustomB
Greg Billock 2013/11/20 00:59:03 Done.
241 *(tp->GetImageSkiaNamed(IDR_RELOAD)));
Peter Kasting 2013/11/19 02:28:50 Did you mean to be using IDR_RELOAD_INNER here and
Greg Billock 2013/11/20 00:59:03 Decided to not deal with the graphics switch in th
242 SetImage(views::Button::STATE_DISABLED,
243 *(tp->GetImageSkiaNamed(IDR_RELOAD_D)));
244 } else {
245 SetImage(views::CustomButton::STATE_NORMAL,
246 *(tp->GetImageSkiaNamed(IDR_STOP)));
247 SetImage(views::Button::STATE_DISABLED,
248 *(tp->GetImageSkiaNamed(IDR_STOP_D)));
249 }
250
258 visible_mode_ = mode; 251 visible_mode_ = mode;
259 SchedulePaint(); 252 SchedulePaint();
260 } 253 }
261 254
262 void ReloadButton::OnDoubleClickTimer() { 255 void ReloadButton::OnDoubleClickTimer() {
263 if (!IsMenuShowing()) 256 if (!IsMenuShowing())
264 ChangeMode(intended_mode_, false); 257 ChangeMode(intended_mode_, false);
265 } 258 }
266 259
267 void ReloadButton::OnStopToReloadTimer() { 260 void ReloadButton::OnStopToReloadTimer() {
268 DCHECK(!IsMenuShowing()); 261 DCHECK(!IsMenuShowing());
269 ChangeMode(intended_mode_, true); 262 ChangeMode(intended_mode_, true);
270 } 263 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698