This source file includes following definitions.
- testing_reload_count_
- ChangeMode
- LoadImages
- OnMouseExited
- GetTooltipText
- GetClassName
- GetAccessibleState
- ShouldShowMenu
- ShowDropDownMenu
- ButtonPressed
- IsCommandIdChecked
- IsCommandIdEnabled
- IsCommandIdVisible
- GetAcceleratorForCommandId
- ExecuteCommand
- CreateMenuModel
- ExecuteBrowserCommand
- ChangeModeInternal
- OnDoubleClickTimer
- OnStopToReloadTimer
#include "chrome/browser/ui/views/toolbar/reload_button.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/search/search_model.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/base/theme_provider.h"
#include "ui/base/window_open_disposition.h"
#include "ui/views/metrics.h"
#include "ui/views/widget/widget.h"
namespace {
const int kReloadMenuItems[] = {
IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM,
IDS_RELOAD_MENU_HARD_RELOAD_ITEM,
IDS_RELOAD_MENU_EMPTY_AND_HARD_RELOAD_ITEM,
};
}
const char ReloadButton::kViewClassName[] = "ReloadButton";
ReloadButton::ReloadButton(LocationBarView* location_bar,
CommandUpdater* command_updater)
: ToolbarButton(this, CreateMenuModel()),
location_bar_(location_bar),
command_updater_(command_updater),
intended_mode_(MODE_RELOAD),
visible_mode_(MODE_RELOAD),
double_click_timer_delay_(
base::TimeDelta::FromMilliseconds(views::GetDoubleClickInterval())),
stop_to_reload_timer_delay_(base::TimeDelta::FromMilliseconds(1350)),
menu_enabled_(false),
testing_mouse_hovered_(false),
testing_reload_count_(0) {
}
ReloadButton::~ReloadButton() {
}
void ReloadButton::ChangeMode(Mode mode, bool force) {
intended_mode_ = mode;
if (force || (!IsMouseHovered() && !testing_mouse_hovered_) ||
((mode == MODE_STOP) ?
!double_click_timer_.IsRunning() : (visible_mode_ != MODE_STOP))) {
double_click_timer_.Stop();
stop_to_reload_timer_.Stop();
if (mode != visible_mode_)
ChangeModeInternal(mode);
SetEnabled(true);
} else if (visible_mode_ != MODE_RELOAD) {
SetEnabled(false);
if (!stop_to_reload_timer_.IsRunning()) {
stop_to_reload_timer_.Start(FROM_HERE, stop_to_reload_timer_delay_, this,
&ReloadButton::OnStopToReloadTimer);
}
}
}
void ReloadButton::LoadImages() {
ChangeModeInternal(visible_mode_);
SchedulePaint();
PreferredSizeChanged();
}
void ReloadButton::OnMouseExited(const ui::MouseEvent& event) {
ToolbarButton::OnMouseExited(event);
if (!IsMenuShowing())
ChangeMode(intended_mode_, true);
}
bool ReloadButton::GetTooltipText(const gfx::Point& p,
base::string16* tooltip) const {
int reload_tooltip = menu_enabled_ ?
IDS_TOOLTIP_RELOAD_WITH_MENU : IDS_TOOLTIP_RELOAD;
int text_id = (visible_mode_ == MODE_RELOAD) ?
reload_tooltip : IDS_TOOLTIP_STOP;
tooltip->assign(l10n_util::GetStringUTF16(text_id));
return true;
}
const char* ReloadButton::GetClassName() const {
return kViewClassName;
}
void ReloadButton::GetAccessibleState(ui::AXViewState* state) {
if (menu_enabled_)
ToolbarButton::GetAccessibleState(state);
else
CustomButton::GetAccessibleState(state);
}
bool ReloadButton::ShouldShowMenu() {
return menu_enabled_ && (visible_mode_ == MODE_RELOAD);
}
void ReloadButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
ToolbarButton::ShowDropDownMenu(source_type);
ChangeMode(intended_mode_, true);
}
void ReloadButton::ButtonPressed(views::Button* ,
const ui::Event& event) {
ClearPendingMenu();
if (visible_mode_ == MODE_STOP) {
if (command_updater_)
command_updater_->ExecuteCommandWithDisposition(IDC_STOP, CURRENT_TAB);
ChangeMode(MODE_RELOAD, true);
} else if (!double_click_timer_.IsRunning()) {
int command;
int flags = event.flags();
if (event.IsShiftDown() || event.IsControlDown()) {
command = IDC_RELOAD_IGNORING_CACHE;
flags &= ~(ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
} else {
command = IDC_RELOAD;
}
double_click_timer_.Start(FROM_HERE, double_click_timer_delay_, this,
&ReloadButton::OnDoubleClickTimer);
ExecuteBrowserCommand(command, flags);
++testing_reload_count_;
}
}
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) {
switch (command_id) {
case IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM:
GetWidget()->GetAccelerator(IDC_RELOAD, accelerator);
return true;
case IDS_RELOAD_MENU_HARD_RELOAD_ITEM:
GetWidget()->GetAccelerator(IDC_RELOAD_IGNORING_CACHE, accelerator);
return true;
}
return GetWidget()->GetAccelerator(command_id, accelerator);
}
void ReloadButton::ExecuteCommand(int command_id, int event_flags) {
int browser_command = 0;
switch (command_id) {
case IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM:
browser_command = IDC_RELOAD;
break;
case IDS_RELOAD_MENU_HARD_RELOAD_ITEM:
browser_command = IDC_RELOAD_IGNORING_CACHE;
break;
case IDS_RELOAD_MENU_EMPTY_AND_HARD_RELOAD_ITEM:
browser_command = IDC_RELOAD_CLEARING_CACHE;
break;
default:
NOTREACHED();
}
ExecuteBrowserCommand(browser_command, event_flags);
}
ui::SimpleMenuModel* ReloadButton::CreateMenuModel() {
ui::SimpleMenuModel* menu_model = new ui::SimpleMenuModel(this);
for (size_t i = 0; i < arraysize(kReloadMenuItems); ++i)
menu_model->AddItemWithStringId(kReloadMenuItems[i], kReloadMenuItems[i]);
return menu_model;
}
void ReloadButton::ExecuteBrowserCommand(int command, int event_flags) {
if (!command_updater_)
return;
command_updater_->ExecuteCommandWithDisposition(
command, ui::DispositionFromEventFlags(event_flags));
}
void ReloadButton::ChangeModeInternal(Mode mode) {
ui::ThemeProvider* tp = GetThemeProvider();
if (tp) {
SetImage(views::Button::STATE_NORMAL, *(tp->GetImageSkiaNamed(
(mode == MODE_RELOAD) ? IDR_RELOAD : IDR_STOP)));
SetImage(views::Button::STATE_DISABLED, *(tp->GetImageSkiaNamed(
(mode == MODE_RELOAD) ? IDR_RELOAD_D : IDR_STOP_D)));
}
visible_mode_ = mode;
SchedulePaint();
}
void ReloadButton::OnDoubleClickTimer() {
if (!IsMenuShowing())
ChangeMode(intended_mode_, false);
}
void ReloadButton::OnStopToReloadTimer() {
DCHECK(!IsMenuShowing());
ChangeMode(intended_mode_, true);
}