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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/views/menu_delegate.h"
|
2014-07-17 06:23:28 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
#include <memory>
|
|
|
|
|
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"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/views/menu_bar.h"
|
|
|
|
#include "shell/browser/ui/views/menu_model_adapter.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 electron {
|
|
|
|
|
2021-01-26 18:16:21 +00:00
|
|
|
MenuDelegate::MenuDelegate(MenuBar* menu_bar) : menu_bar_(menu_bar) {}
|
2014-07-17 06:23:28 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
MenuDelegate::~MenuDelegate() = default;
|
2014-07-17 06:23:28 +00:00
|
|
|
|
2018-10-29 18:08:47 +00:00
|
|
|
void MenuDelegate::RunMenu(ElectronMenuModel* model,
|
2019-04-20 17:20:37 +00:00
|
|
|
views::Button* button,
|
2018-10-29 18:08:47 +00:00
|
|
|
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();
|
2019-09-16 22:12:00 +00:00
|
|
|
adapter_ = std::make_unique<MenuModelAdapter>(model);
|
2014-07-17 06:23:28 +00:00
|
|
|
|
2020-10-26 18:56:31 +00:00
|
|
|
auto* 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
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
menu_runner_ = std::make_unique<views::MenuRunner>(
|
|
|
|
item, views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS);
|
2019-05-01 00:18:22 +00:00
|
|
|
menu_runner_->RunMenuAt(
|
|
|
|
button->GetWidget()->GetTopLevelWidget(),
|
|
|
|
static_cast<views::MenuButton*>(button)->button_controller(), bounds,
|
|
|
|
views::MenuAnchorPosition::kTopRight, 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
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string 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
|
|
|
}
|
|
|
|
|
2021-07-26 16:02:16 +00:00
|
|
|
const gfx::FontList* MenuDelegate::GetLabelFontList(int id) const {
|
|
|
|
return adapter_->GetLabelFontList(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::optional<SkColor> MenuDelegate::GetLabelColor(int id) const {
|
|
|
|
return adapter_->GetLabelColor(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
|
|
|
}
|
|
|
|
|
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
|
|
|
ElectronMenuModel* 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;
|
2020-10-13 17:25:21 +00:00
|
|
|
// Switching menu asynchronously to avoid crash.
|
2017-08-28 09:59:06 +00:00
|
|
|
if (!switch_in_progress) {
|
2019-09-18 19:58:00 +00:00
|
|
|
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
|
|
|
base::BindOnce(&views::MenuRunner::Cancel,
|
|
|
|
base::Unretained(menu_runner_.get())));
|
2017-08-28 09:59:06 +00:00
|
|
|
}
|
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 electron
|