This source file includes following definitions.
- show_menu_factory_
- Init
- ClearPendingMenu
- IsMenuShowing
- GetPreferredSize
- OnMousePressed
- OnMouseDragged
- OnMouseReleased
- OnMouseCaptureLost
- OnMouseExited
- OnGestureEvent
- GetAccessibleState
- ShowContextMenuForView
- ShouldEnterPushedState
- ShouldShowMenu
- ShowDropDownMenu
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
#include "base/bind.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "grit/theme_resources.h"
#include "grit/ui_strings.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/menu_model.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"
#include "ui/views/controls/button/label_button_border.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/menu_model_adapter.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/widget/widget.h"
ToolbarButton::ToolbarButton(views::ButtonListener* listener,
ui::MenuModel* model)
: views::LabelButton(listener, base::string16()),
model_(model),
menu_showing_(false),
y_position_on_lbuttondown_(0),
show_menu_factory_(this) {
set_context_menu_controller(this);
}
ToolbarButton::~ToolbarButton() {
}
void ToolbarButton::Init() {
SetFocusable(false);
SetAccessibilityFocusable(true);
image()->EnableCanvasFlippingForRTLUI(true);
}
void ToolbarButton::ClearPendingMenu() {
show_menu_factory_.InvalidateWeakPtrs();
}
bool ToolbarButton::IsMenuShowing() const {
return menu_showing_;
}
gfx::Size ToolbarButton::GetPreferredSize() {
gfx::Size size(image()->GetPreferredSize());
gfx::Size label_size = label()->GetPreferredSize();
if (label_size.width() > 0)
size.Enlarge(label_size.width() + LocationBarView::GetItemPadding(), 0);
return size;
}
bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) {
if (enabled() && ShouldShowMenu() &&
IsTriggerableEvent(event) && HitTestPoint(event.location())) {
y_position_on_lbuttondown_ = event.y();
const int kMenuTimerDelay = 500;
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&ToolbarButton::ShowDropDownMenu,
show_menu_factory_.GetWeakPtr(),
ui::GetMenuSourceTypeForEvent(event)),
base::TimeDelta::FromMilliseconds(kMenuTimerDelay));
}
return LabelButton::OnMousePressed(event);
}
bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) {
bool result = LabelButton::OnMouseDragged(event);
if (show_menu_factory_.HasWeakPtrs()) {
if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) {
show_menu_factory_.InvalidateWeakPtrs();
ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
}
}
return result;
}
void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) {
if (IsTriggerableEvent(event) ||
(event.IsRightMouseButton() && !HitTestPoint(event.location()))) {
LabelButton::OnMouseReleased(event);
}
if (IsTriggerableEvent(event))
show_menu_factory_.InvalidateWeakPtrs();
}
void ToolbarButton::OnMouseCaptureLost() {
}
void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) {
if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED)
SetState(STATE_NORMAL);
}
void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) {
if (menu_showing_) {
event->StopPropagation();
return;
}
LabelButton::OnGestureEvent(event);
}
void ToolbarButton::GetAccessibleState(ui::AXViewState* state) {
CustomButton::GetAccessibleState(state);
state->role = ui::AX_ROLE_BUTTON_DROP_DOWN;
state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
state->AddStateFlag(ui::AX_STATE_HASPOPUP);
}
void ToolbarButton::ShowContextMenuForView(View* source,
const gfx::Point& point,
ui::MenuSourceType source_type) {
if (!enabled())
return;
show_menu_factory_.InvalidateWeakPtrs();
ShowDropDownMenu(source_type);
}
bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) {
return event.type() == ui::ET_GESTURE_TAP ||
event.type() == ui::ET_GESTURE_TAP_DOWN ||
(event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON |
ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0);
}
bool ToolbarButton::ShouldShowMenu() {
return model_ != NULL;
}
void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
if (!ShouldShowMenu())
return;
gfx::Rect lb = GetLocalBounds();
gfx::Point menu_position(lb.origin());
menu_position.Offset(0, lb.height() - 1);
if (base::i18n::IsRTL())
menu_position.Offset(lb.width() - 1, 0);
View::ConvertPointToScreen(this, &menu_position);
#if defined(OS_WIN)
int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN);
#elif defined(OS_CHROMEOS)
gfx::NativeView view = GetWidget()->GetNativeView();
gfx::Display display = gfx::Screen::GetScreenFor(
view)->GetDisplayNearestWindow(view);
int left_bound = display.bounds().x();
#else
gfx::NativeView view = GetWidget()->GetNativeView();
gfx::Screen* screen = gfx::Screen::GetScreenFor(view);
gfx::Display display = screen->GetDisplayNearestPoint(
screen->GetCursorScreenPoint());
int left_bound = display.bounds().x();
#endif
if (menu_position.x() < left_bound)
menu_position.set_x(left_bound);
SetState(STATE_PRESSED);
menu_showing_ = true;
if (model_.get()) {
views::MenuModelAdapter menu_delegate(model_.get());
menu_delegate.set_triggerable_event_flags(triggerable_event_flags());
menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu()));
views::MenuRunner::RunResult result =
menu_runner_->RunMenuAt(GetWidget(), NULL,
gfx::Rect(menu_position, gfx::Size(0, 0)),
views::MenuItemView::TOPLEFT,
source_type,
views::MenuRunner::HAS_MNEMONICS);
if (result == views::MenuRunner::MENU_DELETED)
return;
} else {
views::MenuDelegate menu_delegate;
views::MenuItemView* menu = new views::MenuItemView(&menu_delegate);
menu_runner_.reset(new views::MenuRunner(menu));
views::MenuRunner::RunResult result =
menu_runner_->RunMenuAt(GetWidget(), NULL,
gfx::Rect(menu_position, gfx::Size(0, 0)),
views::MenuItemView::TOPLEFT,
source_type,
views::MenuRunner::HAS_MNEMONICS);
if (result == views::MenuRunner::MENU_DELETED)
return;
}
menu_showing_ = false;
SetMouseHandler(NULL);
if (state_ != STATE_DISABLED)
SetState(STATE_NORMAL);
}