1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/toolbar/reload_button.h"
#include <stddef.h>
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/menu_source_type.mojom-forward.h"
#include "ui/base/pointer/touch_ui_controller.h"
#include "ui/base/theme_provider.h"
#include "ui/base/ui_base_features.h"
#include "ui/base/window_open_disposition.h"
#include "ui/base/window_open_disposition_utils.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/metrics.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"
// ReloadButton ---------------------------------------------------------------
ReloadButton::ReloadButton(CommandUpdater* command_updater)
: ToolbarButton(base::BindRepeating(&ReloadButton::ButtonPressed,
base::Unretained(this)),
CreateMenuModel(),
nullptr),
command_updater_(command_updater),
reload_icon_(vector_icons::kReloadChromeRefreshIcon),
reload_touch_icon_(kReloadTouchIcon),
stop_icon_(kNavigateStopChromeRefreshIcon),
stop_touch_icon_(kNavigateStopTouchIcon),
double_click_timer_delay_(
base::Milliseconds(views::GetDoubleClickInterval())),
mode_switch_timer_delay_(base::Milliseconds(1350)) {
SetVisibleMode(Mode::kReload);
SetTriggerableEventFlags(ui::EF_LEFT_MOUSE_BUTTON |
ui::EF_MIDDLE_MOUSE_BUTTON);
GetViewAccessibility().SetName(l10n_util::GetStringUTF16(IDS_ACCNAME_RELOAD));
UpdateAccessibleHasPopup();
SetProperty(views::kElementIdentifierKey, kReloadButtonElementId);
SetID(VIEW_ID_RELOAD_BUTTON);
UpdateCachedTooltipText();
}
ReloadButton::~ReloadButton() = default;
void ReloadButton::ChangeMode(Mode mode, bool force) {
intended_mode_ = mode;
// If the change is forced, or the user isn't hovering the icon, or it's
// safe to change it to the other image type, make the change immediately;
// otherwise we'll let it happen later.
if (force || (!IsMouseHovered() && !testing_mouse_hovered_) ||
((mode == Mode::kStop) ? !double_click_timer_.IsRunning()
: (visible_mode_ != Mode::kStop))) {
double_click_timer_.Stop();
mode_switch_timer_.Stop();
SetVisibleMode(mode);
SetEnabled(true);
// We want to disable the button if we're preventing a change from stop to
// reload due to hovering, but not if we're preventing a change from
// reload to stop due to the double-click timer running. (Disabled reload
// state is only applicable when instant extended API is enabled and mode
// is NTP, which is handled just above.)
} else if (visible_mode_ != Mode::kReload) {
SetEnabled(false);
// Go ahead and change to reload after a bit, which allows repeated
// reloads without moving the mouse.
if (!mode_switch_timer_.IsRunning()) {
mode_switch_timer_.Start(FROM_HERE, mode_switch_timer_delay_, this,
&ReloadButton::OnStopToReloadTimer);
}
}
}
void ReloadButton::SetVectorIconsForMode(Mode mode,
const gfx::VectorIcon& icon,
const gfx::VectorIcon& touch_icon) {
switch (mode) {
case Mode::kReload:
reload_icon_ = icon;
reload_touch_icon_ = touch_icon;
break;
case Mode::kStop:
stop_icon_ = icon;
stop_touch_icon_ = touch_icon;
break;
}
if (mode == visible_mode_) {
SetVisibleMode(visible_mode_);
}
}
bool ReloadButton::GetMenuEnabled() const {
return menu_enabled_;
}
void ReloadButton::SetMenuEnabled(bool enable) {
menu_enabled_ = enable;
UpdateAccessibleHasPopup();
UpdateCachedTooltipText();
}
void ReloadButton::OnMouseExited(const ui::MouseEvent& event) {
ToolbarButton::OnMouseExited(event);
if (!IsMenuShowing()) {
ChangeMode(intended_mode_, true);
}
}
void ReloadButton::UpdateCachedTooltipText() {
int reload_tooltip =
menu_enabled_ ? IDS_TOOLTIP_RELOAD_WITH_MENU : IDS_TOOLTIP_RELOAD;
SetTooltipText(l10n_util::GetStringUTF16(
visible_mode_ == Mode::kReload ? reload_tooltip : IDS_TOOLTIP_STOP));
}
bool ReloadButton::ShouldShowMenu() {
return menu_enabled_ && (visible_mode_ == Mode::kReload);
}
void ReloadButton::ShowDropDownMenu(ui::mojom::MenuSourceType source_type) {
ToolbarButton::ShowDropDownMenu(source_type); // Blocks.
ChangeMode(intended_mode_, true);
}
bool ReloadButton::IsCommandIdChecked(int command_id) const {
return false;
}
bool ReloadButton::IsCommandIdEnabled(int command_id) const {
return true;
}
bool ReloadButton::IsCommandIdVisible(int command_id) const {
return true;
}
bool ReloadButton::GetAcceleratorForCommandId(
int command_id,
ui::Accelerator* accelerator) const {
return GetWidget()->GetAccelerator(command_id, accelerator);
}
void ReloadButton::ExecuteCommand(int command_id, int event_flags) {
ExecuteBrowserCommand(command_id, event_flags);
}
std::unique_ptr<ui::SimpleMenuModel> ReloadButton::CreateMenuModel() {
auto menu_model = std::make_unique<ui::SimpleMenuModel>(this);
menu_model->AddItemWithStringId(IDC_RELOAD,
IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM);
menu_model->AddItemWithStringId(IDC_RELOAD_BYPASSING_CACHE,
IDS_RELOAD_MENU_HARD_RELOAD_ITEM);
menu_model->AddItemWithStringId(IDC_RELOAD_CLEARING_CACHE,
IDS_RELOAD_MENU_EMPTY_AND_HARD_RELOAD_ITEM);
return menu_model;
}
void ReloadButton::SetVisibleMode(Mode mode) {
visible_mode_ = mode;
switch (mode) {
case Mode::kReload:
SetVectorIcons(*reload_icon_, *reload_touch_icon_);
break;
case Mode::kStop:
SetVectorIcons(*stop_icon_, *stop_touch_icon_);
break;
}
UpdateCachedTooltipText();
}
void ReloadButton::ButtonPressed(const ui::Event& event) {
// This is called in order to signal that external protocol dialogs are
// allowed to show due to a user action, which are likely to happen on the
// next page load after the reload button is clicked.
// Ideally, the browser UI's event system would notify ExternalProtocolHandler
// that a user action occurred and we are OK to open the dialog, but for some
// reason that isn't happening every time the reload button is clicked. See
// http://crbug.com/1206456
ExternalProtocolHandler::PermitLaunchUrl();
ClearPendingMenu();
if (visible_mode_ == Mode::kStop) {
if (command_updater_) {
command_updater_->ExecuteCommandWithDisposition(
IDC_STOP, WindowOpenDisposition::CURRENT_TAB);
}
// The user has clicked, so we can feel free to update the button, even if
// the mouse is still hovering.
ChangeMode(Mode::kReload, true);
return;
}
if (!double_click_timer_.IsRunning()) {
// Shift-clicking or ctrl-clicking the reload button means we should ignore
// any cached content.
int command;
int flags = event.flags();
if (event.IsShiftDown() || event.IsControlDown()) {
command = IDC_RELOAD_BYPASSING_CACHE;
// Mask off Shift and Control so they don't affect the disposition below.
flags &= ~(ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
} else {
command = IDC_RELOAD;
}
// Start a timer - while this timer is running, the reload button cannot be
// changed to a stop button. We do not set |intended_mode_| to Mode::kStop
// here as the browser will do that when it actually starts loading (which
// may happen synchronously, thus the need to do this before telling the
// browser to execute the reload command).
double_click_timer_.Start(FROM_HERE, double_click_timer_delay_, this,
&ReloadButton::OnDoubleClickTimer);
ExecuteBrowserCommand(command, flags);
++testing_reload_count_;
}
}
void ReloadButton::ExecuteBrowserCommand(int command, int event_flags) {
if (!command_updater_) {
return;
}
command_updater_->ExecuteCommandWithDisposition(
command, ui::DispositionFromEventFlags(event_flags));
}
void ReloadButton::OnDoubleClickTimer() {
if (!IsMenuShowing()) {
ChangeMode(intended_mode_, false);
}
}
void ReloadButton::OnStopToReloadTimer() {
DCHECK(!IsMenuShowing());
ChangeMode(intended_mode_, true);
}
void ReloadButton::UpdateAccessibleHasPopup() {
if (menu_enabled_ && menu_model()) {
GetViewAccessibility().SetHasPopup(ax::mojom::HasPopup::kMenu);
} else {
GetViewAccessibility().SetHasPopup(ax::mojom::HasPopup::kNone);
}
}
BEGIN_METADATA(ReloadButton)
ADD_PROPERTY_METADATA(bool, MenuEnabled)
END_METADATA
|