electron/atom/browser/ui/views/menu_delegate.cc

122 lines
3.6 KiB
C++
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc.
2014-07-17 06:23:28 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/ui/views/menu_delegate.h"
#include "atom/browser/ui/views/menu_bar.h"
2016-01-05 03:57:58 +00:00
#include "content/public/browser/browser_thread.h"
2014-07-17 06:23:28 +00:00
#include "ui/views/controls/button/menu_button.h"
2014-07-28 10:11:47 +00:00
#include "ui/views/controls/menu/menu_item_view.h"
2014-07-17 06:23:28 +00:00
#include "ui/views/controls/menu/menu_model_adapter.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/widget/widget.h"
namespace atom {
MenuDelegate::MenuDelegate(MenuBar* menu_bar)
: menu_bar_(menu_bar),
2016-01-05 03:57:58 +00:00
id_(-1) {
2014-07-17 06:23:28 +00:00
}
MenuDelegate::~MenuDelegate() {
}
void MenuDelegate::RunMenu(ui::MenuModel* model, views::MenuButton* button) {
gfx::Point screen_loc;
views::View::ConvertPointToScreen(button, &screen_loc);
// Subtract 1 from the height to make the popup flush with the button border.
gfx::Rect bounds(screen_loc.x(), screen_loc.y(), button->width(),
button->height() - 1);
id_ = button->tag();
2016-01-05 03:57:58 +00:00
adapter_.reset(new views::MenuModelAdapter(model));
2014-07-17 06:23:28 +00:00
2016-01-05 03:57:58 +00:00
views::MenuItemView* item = new views::MenuItemView(this);
static_cast<views::MenuModelAdapter*>(adapter_.get())->BuildMenu(item);
menu_runner_.reset(new views::MenuRunner(
item,
2016-01-05 03:57:58 +00:00
views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS));
ignore_result(menu_runner_->RunMenuAt(
2014-07-17 06:23:28 +00:00
button->GetWidget()->GetTopLevelWidget(),
button,
bounds,
2014-07-28 10:11:47 +00:00
views::MENU_ANCHOR_TOPRIGHT,
2014-10-13 03:03:56 +00:00
ui::MENU_SOURCE_MOUSE));
2014-07-17 06:23:28 +00:00
}
void MenuDelegate::ExecuteCommand(int id) {
2016-01-05 03:57:58 +00:00
adapter_->ExecuteCommand(id);
2014-07-17 06:23:28 +00:00
}
void MenuDelegate::ExecuteCommand(int id, int mouse_event_flags) {
2016-01-05 03:57:58 +00:00
adapter_->ExecuteCommand(id, mouse_event_flags);
2014-07-17 06:23:28 +00:00
}
bool MenuDelegate::IsTriggerableEvent(views::MenuItemView* source,
const ui::Event& e) {
2016-01-05 03:57:58 +00:00
return adapter_->IsTriggerableEvent(source, e);
2014-07-17 06:23:28 +00:00
}
2014-09-01 12:10:14 +00:00
bool MenuDelegate::GetAccelerator(int id, ui::Accelerator* accelerator) const {
2016-01-05 03:57:58 +00:00
return adapter_->GetAccelerator(id, accelerator);
2014-07-17 06:23:28 +00:00
}
base::string16 MenuDelegate::GetLabel(int id) const {
2016-01-05 03:57:58 +00:00
return adapter_->GetLabel(id);
2014-07-17 06:23:28 +00:00
}
const gfx::FontList* MenuDelegate::GetLabelFontList(int id) const {
2016-01-05 03:57:58 +00:00
return adapter_->GetLabelFontList(id);
2014-07-17 06:23:28 +00:00
}
bool MenuDelegate::IsCommandEnabled(int id) const {
2016-01-05 03:57:58 +00:00
return adapter_->IsCommandEnabled(id);
2014-07-17 06:23:28 +00:00
}
bool MenuDelegate::IsCommandVisible(int id) const {
2016-01-05 03:57:58 +00:00
return adapter_->IsCommandVisible(id);
}
2014-07-17 06:23:28 +00:00
bool MenuDelegate::IsItemChecked(int id) const {
2016-01-05 03:57:58 +00:00
return adapter_->IsItemChecked(id);
2014-07-17 06:23:28 +00:00
}
void MenuDelegate::SelectionChanged(views::MenuItemView* menu) {
2016-01-05 03:57:58 +00:00
adapter_->SelectionChanged(menu);
2014-07-17 06:23:28 +00:00
}
void MenuDelegate::WillShowMenu(views::MenuItemView* menu) {
2016-01-05 03:57:58 +00:00
adapter_->WillShowMenu(menu);
2014-07-17 06:23:28 +00:00
}
void MenuDelegate::WillHideMenu(views::MenuItemView* menu) {
2016-01-05 03:57:58 +00:00
adapter_->WillHideMenu(menu);
2014-07-17 06:23:28 +00:00
}
views::MenuItemView* MenuDelegate::GetSiblingMenu(
views::MenuItemView* menu,
const gfx::Point& screen_point,
2014-07-28 10:11:47 +00:00
views::MenuAnchorPosition* anchor,
2014-07-17 06:23:28 +00:00
bool* has_mnemonics,
2016-01-05 03:57:58 +00:00
views::MenuButton**) {
views::MenuButton* button;
2014-07-17 06:23:28 +00:00
ui::MenuModel* model;
2016-01-05 03:57:58 +00:00
if (menu_bar_->GetMenuButtonFromScreenPoint(screen_point, &model, &button) &&
button->tag() != id_) {
DCHECK(menu_runner_->IsRunning());
menu_runner_->Cancel();
// After canceling the menu, we need to wait until next tick
// so we are out of nested message loop.
2016-01-05 03:57:58 +00:00
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(&views::MenuButton::Activate),
2016-04-30 23:56:28 +00:00
base::Unretained(button), nullptr));
2016-01-05 03:57:58 +00:00
}
2014-07-17 06:23:28 +00:00
2016-01-05 03:57:58 +00:00
return nullptr;
}
2014-07-17 06:23:28 +00:00
} // namespace atom