electron/shell/browser/api/electron_api_menu_views.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

92 lines
3 KiB
C++
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/api/electron_api_menu_views.h"
#include <memory>
#include <utility>
#include "shell/browser/native_window_views.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;
2022-06-29 19:55:47 +00:00
namespace electron::api {
MenuViews::MenuViews(gin::Arguments* args) : Menu(args) {}
MenuViews::~MenuViews() = default;
void MenuViews::PopupAt(BaseWindow* window,
2018-04-18 01:55:30 +00:00
int x,
int y,
int positioning_item,
ui::MenuSourceType source_type,
base::OnceClosure callback) {
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 {
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
int flags = MenuRunner::CONTEXT_MENU | MenuRunner::HAS_MNEMONICS;
// 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.
//
// 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.
int32_t window_id = window->weak_map_id();
auto close_callback = base::AdaptCallbackForRepeating(
base::BindOnce(&MenuViews::OnClosed, weak_factory_.GetWeakPtr(),
window_id, std::move(callback_with_ref)));
auto& runner = menu_runners_[window_id] =
std::make_unique<MenuRunner>(model(), flags, std::move(close_callback));
runner->RunMenuAt(native_window->widget(), nullptr,
gfx::Rect{location, gfx::Size{}},
views::MenuAnchorPosition::kTopLeft, source_type);
}
void MenuViews::ClosePopupAt(int32_t window_id) {
if (auto iter = menu_runners_.find(window_id); iter != menu_runners_.end()) {
2017-12-21 06:26:32 +00:00
// Close the runner for the window.
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 09:48:09 +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);
std::move(callback).Run();
2017-12-21 06:26:32 +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;
}
2022-06-29 19:55:47 +00:00
} // namespace electron::api