2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-07-10 04:07:01 +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/api/electron_api_menu_views.h"
|
2014-07-10 04:07:01 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2019-11-20 11:17:39 +00:00
|
|
|
#include <utility>
|
2018-09-13 00:25:56 +00:00
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window_views.h"
|
|
|
|
#include "shell/browser/unresponsive_suppressor.h"
|
2016-07-04 06:08:55 +00:00
|
|
|
#include "ui/display/screen.h"
|
2017-02-15 20:44:39 +00:00
|
|
|
|
|
|
|
using views::MenuRunner;
|
2014-07-10 04:07:01 +00:00
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::api {
|
2014-07-10 04:07:01 +00:00
|
|
|
|
2021-01-26 18:16:21 +00:00
|
|
|
MenuViews::MenuViews(gin::Arguments* args) : Menu(args) {}
|
2014-07-10 04:07:01 +00:00
|
|
|
|
2018-04-17 23:37:22 +00:00
|
|
|
MenuViews::~MenuViews() = default;
|
|
|
|
|
2020-06-29 07:06:20 +00:00
|
|
|
void MenuViews::PopupAt(BaseWindow* window,
|
2018-04-18 01:55:30 +00:00
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int positioning_item,
|
2019-11-20 11:17:39 +00:00
|
|
|
base::OnceClosure callback) {
|
2018-03-06 07:00:18 +00:00
|
|
|
auto* native_window = static_cast<NativeWindowViews*>(window->window());
|
2014-11-28 07:59:03 +00:00
|
|
|
if (!native_window)
|
|
|
|
return;
|
|
|
|
|
2016-01-22 18:25:16 +00:00
|
|
|
// (-1, -1) means showing on mouse location.
|
|
|
|
gfx::Point location;
|
|
|
|
if (x == -1 || y == -1) {
|
2016-07-04 06:08:55 +00:00
|
|
|
location = display::Screen::GetScreen()->GetCursorScreenPoint();
|
2016-01-22 18:25:16 +00:00
|
|
|
} else {
|
2018-03-08 20:48:35 +00:00
|
|
|
gfx::Point origin = native_window->GetContentBounds().origin();
|
2016-01-22 18:25:16 +00:00
|
|
|
location = gfx::Point(origin.x() + x, origin.y() + y);
|
|
|
|
}
|
2014-11-25 15:47:41 +00:00
|
|
|
|
2017-02-16 00:07:46 +00:00
|
|
|
int flags = MenuRunner::CONTEXT_MENU | MenuRunner::HAS_MNEMONICS;
|
|
|
|
|
2016-07-11 06:31:24 +00:00
|
|
|
// Don't emit unresponsive event when showing menu.
|
|
|
|
electron::UnresponsiveSuppressor suppressor;
|
|
|
|
|
2019-11-20 11:17:39 +00:00
|
|
|
// Make sure the Menu object would not be garbage-collected until the callback
|
|
|
|
// has run.
|
|
|
|
base::OnceClosure callback_with_ref = BindSelfToClosure(std::move(callback));
|
|
|
|
|
2016-01-22 18:25:16 +00:00
|
|
|
// Show the menu.
|
2019-11-20 11:17:39 +00:00
|
|
|
//
|
|
|
|
// Note that while views::MenuRunner accepts RepeatingCallback as close
|
|
|
|
// callback, it is fine passing OnceCallback to it because we reset the
|
|
|
|
// menu runner immediately when the menu is closed.
|
2018-04-14 02:04:23 +00:00
|
|
|
int32_t window_id = window->weak_map_id();
|
2019-11-20 11:17:39 +00:00
|
|
|
auto close_callback = base::AdaptCallbackForRepeating(
|
|
|
|
base::BindOnce(&MenuViews::OnClosed, weak_factory_.GetWeakPtr(),
|
|
|
|
window_id, std::move(callback_with_ref)));
|
2018-06-18 07:32:55 +00:00
|
|
|
menu_runners_[window_id] =
|
2019-11-20 11:17:39 +00:00
|
|
|
std::make_unique<MenuRunner>(model(), flags, std::move(close_callback));
|
2017-09-13 21:06:43 +00:00
|
|
|
menu_runners_[window_id]->RunMenuAt(
|
2019-09-16 22:12:00 +00:00
|
|
|
native_window->widget(), nullptr, gfx::Rect(location, gfx::Size()),
|
2019-04-20 17:20:37 +00:00
|
|
|
views::MenuAnchorPosition::kTopLeft, ui::MENU_SOURCE_MOUSE);
|
2014-07-10 04:07:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 19:15:05 +00:00
|
|
|
void MenuViews::ClosePopupAt(int32_t window_id) {
|
2017-12-21 06:26:32 +00:00
|
|
|
auto runner = menu_runners_.find(window_id);
|
|
|
|
if (runner != menu_runners_.end()) {
|
|
|
|
// Close the runner for the window.
|
|
|
|
runner->second->Cancel();
|
|
|
|
} else if (window_id == -1) {
|
|
|
|
// Or just close all opened runners.
|
|
|
|
for (auto it = menu_runners_.begin(); it != menu_runners_.end();) {
|
|
|
|
// The iterator is invalidated after the call.
|
|
|
|
(it++)->second->Cancel();
|
|
|
|
}
|
2017-12-20 09:48:09 +00:00
|
|
|
}
|
2017-02-16 19:15:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 11:17:39 +00:00
|
|
|
void MenuViews::OnClosed(int32_t window_id, base::OnceClosure callback) {
|
2017-12-21 06:26:32 +00:00
|
|
|
menu_runners_.erase(window_id);
|
2019-11-20 11:17:39 +00:00
|
|
|
std::move(callback).Run();
|
2017-12-21 06:26:32 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 04:07:01 +00:00
|
|
|
// static
|
2020-04-02 23:07:56 +00:00
|
|
|
gin::Handle<Menu> Menu::New(gin::Arguments* args) {
|
|
|
|
auto handle = gin::CreateHandle(args->isolate(),
|
|
|
|
static_cast<Menu*>(new MenuViews(args)));
|
|
|
|
gin_helper::CallMethod(args->isolate(), handle.get(), "_init");
|
|
|
|
return handle;
|
2014-07-10 04:07:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::api
|