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.
|
|
|
|
|
2019-06-19 13:46:59 -07: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
|
|
|
|
2024-07-29 12:42:57 -05:00
|
|
|
#include "shell/browser/api/electron_api_base_window.h"
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/native_window_views.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
|
|
|
|
2022-06-29 12:55:47 -07:00
|
|
|
namespace electron::api {
|
2014-07-10 12:07:01 +08:00
|
|
|
|
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,
|
2024-11-04 14:47:15 -05:00
|
|
|
ui::mojom::MenuSourceType source_type,
|
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;
|
|
|
|
|
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)));
|
2024-01-05 05:18:31 -06:00
|
|
|
auto& runner = menu_runners_[window_id] =
|
2019-11-20 20:17:39 +09:00
|
|
|
std::make_unique<MenuRunner>(model(), flags, std::move(close_callback));
|
2024-01-05 05:18:31 -06:00
|
|
|
runner->RunMenuAt(native_window->widget(), nullptr,
|
|
|
|
gfx::Rect{location, gfx::Size{}},
|
|
|
|
views::MenuAnchorPosition::kTopLeft, source_type);
|
2014-07-10 12:07:01 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 11:15:05 -08:00
|
|
|
void MenuViews::ClosePopupAt(int32_t window_id) {
|
2024-01-05 05:18:31 -06:00
|
|
|
if (auto iter = menu_runners_.find(window_id); iter != menu_runners_.end()) {
|
2017-12-21 15:26:32 +09:00
|
|
|
// Close the runner for the window.
|
2024-01-05 05:18:31 -06:00
|
|
|
iter->second->Cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window_id == -1) {
|
|
|
|
// When -1 is passed in, close all opened runners.
|
|
|
|
// Note: `Cancel()` invalidaes iters, so move() to a temp before looping
|
|
|
|
auto tmp = std::move(menu_runners_);
|
|
|
|
for (auto& [id, runner] : tmp)
|
|
|
|
runner->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
|
|
|
}
|
|
|
|
|
2022-06-29 12:55:47 -07:00
|
|
|
} // namespace electron::api
|