2014-10-31 18:17:05 +00:00
|
|
|
// 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-07-06 23:04:18 +00:00
|
|
|
#include "atom/browser/ui/views/menu_model_adapter.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "base/task/post_task.h"
|
|
|
|
#include "content/public/browser/browser_task_traits.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_runner.h"
|
|
|
|
#include "ui/views/widget/widget.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2018-10-29 18:08:47 +00:00
|
|
|
MenuDelegate::MenuDelegate(MenuBar* menu_bar)
|
|
|
|
: menu_bar_(menu_bar), id_(-1), hold_first_switch_(false) {}
|
2014-07-17 06:23:28 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
MenuDelegate::~MenuDelegate() {}
|
2014-07-17 06:23:28 +00:00
|
|
|
|
2018-10-29 18:08:47 +00:00
|
|
|
void MenuDelegate::RunMenu(AtomMenuModel* model,
|
|
|
|
views::MenuButton* button,
|
|
|
|
ui::MenuSourceType source_type) {
|
2014-07-17 06:23:28 +00:00
|
|
|
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);
|
|
|
|
|
2018-10-29 18:08:47 +00:00
|
|
|
if (source_type == ui::MENU_SOURCE_KEYBOARD) {
|
|
|
|
hold_first_switch_ = true;
|
|
|
|
}
|
|
|
|
|
2014-07-17 06:23:28 +00:00
|
|
|
id_ = button->tag();
|
2016-07-07 21:25:09 +00:00
|
|
|
adapter_.reset(new 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);
|
2016-07-07 21:25:09 +00:00
|
|
|
static_cast<MenuModelAdapter*>(adapter_.get())->BuildMenu(item);
|
2016-01-05 03:57:58 +00:00
|
|
|
|
|
|
|
menu_runner_.reset(new views::MenuRunner(
|
2014-10-16 13:22:22 +00:00
|
|
|
item,
|
2016-01-05 03:57:58 +00:00
|
|
|
views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS));
|
2018-04-18 01:55:30 +00:00
|
|
|
menu_runner_->RunMenuAt(button->GetWidget()->GetTopLevelWidget(), button,
|
2018-10-29 18:08:47 +00:00
|
|
|
bounds, views::MENU_ANCHOR_TOPRIGHT, source_type);
|
2014-07-17 06:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuDelegate::ExecuteCommand(int id) {
|
2018-10-29 18:08:47 +00:00
|
|
|
for (Observer& obs : observers_)
|
|
|
|
obs.OnBeforeExecuteCommand();
|
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) {
|
2018-10-29 18:08:47 +00:00
|
|
|
for (Observer& obs : observers_)
|
|
|
|
obs.OnBeforeExecuteCommand();
|
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
|
|
|
}
|
|
|
|
|
2018-10-27 03:37:18 +00:00
|
|
|
void MenuDelegate::GetLabelStyle(int id, LabelStyle* style) const {
|
|
|
|
return adapter_->GetLabelStyle(id, style);
|
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
|
|
|
}
|
|
|
|
|
2014-11-16 08:01:33 +00:00
|
|
|
bool MenuDelegate::IsCommandVisible(int id) const {
|
2016-01-05 03:57:58 +00:00
|
|
|
return adapter_->IsCommandVisible(id);
|
2014-11-16 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
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::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
|
|
|
}
|
|
|
|
|
2017-09-13 21:06:43 +00:00
|
|
|
void MenuDelegate::OnMenuClosed(views::MenuItemView* menu) {
|
2018-10-29 18:08:47 +00:00
|
|
|
for (Observer& obs : observers_)
|
|
|
|
obs.OnMenuClosed();
|
|
|
|
|
2017-08-28 09:59:06 +00:00
|
|
|
// Only switch to new menu when current menu is closed.
|
|
|
|
if (button_to_open_)
|
|
|
|
button_to_open_->Activate(nullptr);
|
2017-07-03 19:47:02 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
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**) {
|
2018-10-29 18:08:47 +00:00
|
|
|
if (hold_first_switch_) {
|
|
|
|
hold_first_switch_ = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-28 09:59:06 +00:00
|
|
|
// TODO(zcbenz): We should follow Chromium's logics on implementing the
|
|
|
|
// sibling menu switches, this code is almost a hack.
|
2016-01-05 03:57:58 +00:00
|
|
|
views::MenuButton* button;
|
2016-07-06 23:04:18 +00:00
|
|
|
AtomMenuModel* model;
|
2016-01-05 03:57:58 +00:00
|
|
|
if (menu_bar_->GetMenuButtonFromScreenPoint(screen_point, &model, &button) &&
|
|
|
|
button->tag() != id_) {
|
2017-08-28 09:59:06 +00:00
|
|
|
bool switch_in_progress = !!button_to_open_;
|
|
|
|
// Always update target to open.
|
|
|
|
button_to_open_ = button;
|
|
|
|
// Switching menu asyncnously to avoid crash.
|
|
|
|
if (!switch_in_progress) {
|
2019-01-12 01:00:43 +00:00
|
|
|
base::PostTaskWithTraits(
|
|
|
|
FROM_HERE, {content::BrowserThread::UI},
|
2017-08-28 09:59:06 +00:00
|
|
|
base::Bind(&views::MenuRunner::Cancel,
|
|
|
|
base::Unretained(menu_runner_.get())));
|
|
|
|
}
|
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
|