2014-10-31 11:17:05 -07:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-07-10 12:07:01 +08:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 12:19:40 -08:00
|
|
|
#include "shell/browser/api/electron_api_menu_views.h"
|
2014-07-10 12:07:01 +08:00
|
|
|
|
2018-09-12 19:25:56 -05:00
|
|
|
#include <memory>
|
2019-11-20 20:17:39 +09:00
|
|
|
#include <utility>
|
2018-09-12 19:25:56 -05:00
|
|
|
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/native_window_views.h"
|
|
|
|
#include "shell/browser/unresponsive_suppressor.h"
|
2016-07-04 15:08:55 +09:00
|
|
|
#include "ui/display/screen.h"
|
2017-02-15 12:44:39 -08:00
|
|
|
|
|
|
|
using views::MenuRunner;
|
2014-07-10 12:07:01 +08:00
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
namespace electron {
|
2014-07-10 12:07:01 +08:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2021-01-26 19:16:21 +01:00
|
|
|
MenuViews::MenuViews(gin::Arguments* args) : Menu(args) {}
|
2014-07-10 12:07:01 +08:00
|
|
|
|
2018-04-17 16:37:22 -07:00
|
|
|
MenuViews::~MenuViews() = default;
|
|
|
|
|
2020-06-29 16:06:20 +09:00
|
|
|
void MenuViews::PopupAt(BaseWindow* window,
|
2018-04-17 21:55:30 -04:00
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int positioning_item,
|
2019-11-20 20:17:39 +09:00
|
|
|
base::OnceClosure callback) {
|
2018-03-06 16:00:18 +09:00
|
|
|
auto* native_window = static_cast<NativeWindowViews*>(window->window());
|
2014-11-28 15:59:03 +08:00
|
|
|
if (!native_window)
|
|
|
|
return;
|
|
|
|
|
2016-01-22 11:25:16 -07:00
|
|
|
// (-1, -1) means showing on mouse location.
|
|
|
|
gfx::Point location;
|
|
|
|
if (x == -1 || y == -1) {
|
2016-07-04 15:08:55 +09:00
|
|
|
location = display::Screen::GetScreen()->GetCursorScreenPoint();
|
2016-01-22 11:25:16 -07:00
|
|
|
} else {
|
2018-03-08 21:48:35 +01:00
|
|
|
gfx::Point origin = native_window->GetContentBounds().origin();
|
2016-01-22 11:25:16 -07:00
|
|
|
location = gfx::Point(origin.x() + x, origin.y() + y);
|
|
|
|
}
|
2014-11-25 16:47:41 +01:00
|
|
|
|
2017-02-15 16:07:46 -08:00
|
|
|
int flags = MenuRunner::CONTEXT_MENU | MenuRunner::HAS_MNEMONICS;
|
|
|
|
|
2016-07-11 15:31:24 +09:00
|
|
|
// Don't emit unresponsive event when showing menu.
|
2019-06-19 14:23:04 -07:00
|
|
|
electron::UnresponsiveSuppressor suppressor;
|
2016-07-11 15:31:24 +09:00
|
|
|
|
2019-11-20 20:17:39 +09: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 11:25:16 -07:00
|
|
|
// Show the menu.
|
2019-11-20 20:17:39 +09: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 11:04:23 +09:00
|
|
|
int32_t window_id = window->weak_map_id();
|
2019-11-20 20:17:39 +09:00
|
|
|
auto close_callback = base::AdaptCallbackForRepeating(
|
|
|
|
base::BindOnce(&MenuViews::OnClosed, weak_factory_.GetWeakPtr(),
|
|
|
|
window_id, std::move(callback_with_ref)));
|
2018-06-18 09:32:55 +02:00
|
|
|
menu_runners_[window_id] =
|
2019-11-20 20:17:39 +09:00
|
|
|
std::make_unique<MenuRunner>(model(), flags, std::move(close_callback));
|
2017-09-14 00:06:43 +03:00
|
|
|
menu_runners_[window_id]->RunMenuAt(
|
2019-09-16 18:12:00 -04:00
|
|
|
native_window->widget(), nullptr, gfx::Rect(location, gfx::Size()),
|
2019-04-20 13:20:37 -04:00
|
|
|
views::MenuAnchorPosition::kTopLeft, ui::MENU_SOURCE_MOUSE);
|
2014-07-10 12:07:01 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 11:15:05 -08:00
|
|
|
void MenuViews::ClosePopupAt(int32_t window_id) {
|
2017-12-21 15:26:32 +09: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 18:48:09 +09:00
|
|
|
}
|
2017-02-16 11:15:05 -08:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:17:39 +09:00
|
|
|
void MenuViews::OnClosed(int32_t window_id, base::OnceClosure callback) {
|
2017-12-21 15:26:32 +09:00
|
|
|
menu_runners_.erase(window_id);
|
2019-11-20 20:17:39 +09:00
|
|
|
std::move(callback).Run();
|
2017-12-21 15:26:32 +09:00
|
|
|
}
|
|
|
|
|
2014-07-10 12:07:01 +08:00
|
|
|
// static
|
2020-04-02 16:07:56 -07: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 12:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
} // namespace electron
|