Add async menu.popup on Windows/Linux

This commit is contained in:
Kevin Sawicki 2017-02-15 12:44:39 -08:00
parent 636ef0fd29
commit b091d104f5
2 changed files with 18 additions and 5 deletions

View file

@ -8,14 +8,16 @@
#include "atom/browser/unresponsive_suppressor.h"
#include "content/public/browser/render_widget_host_view.h"
#include "ui/display/screen.h"
#include "ui/views/controls/menu/menu_runner.h"
using views::MenuRunner;
namespace atom {
namespace api {
MenuViews::MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
: Menu(isolate, wrapper) {
: Menu(isolate, wrapper),
weak_factory_(this) {
}
void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item) {
@ -42,10 +44,11 @@ void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item) {
atom::UnresponsiveSuppressor suppressor;
// Show the menu.
views::MenuRunner menu_runner(
menu_runner_.reset(new MenuRunner(
model(),
views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS);
ignore_result(menu_runner.RunMenuAt(
MenuRunner::CONTEXT_MENU | MenuRunner::HAS_MNEMONICS | MenuRunner::ASYNC,
base::Bind(&MenuViews::OnMenuClosed, weak_factory_.GetWeakPtr())));
ignore_result(menu_runner_->RunMenuAt(
static_cast<NativeWindowViews*>(window->window())->widget(),
NULL,
gfx::Rect(location, gfx::Size()),
@ -53,6 +56,10 @@ void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item) {
ui::MENU_SOURCE_MOUSE));
}
void MenuViews::OnMenuClosed() {
menu_runner_.reset();
}
// static
mate::WrappableBase* Menu::New(mate::Arguments* args) {
return new MenuViews(args->isolate(), args->GetThis());

View file

@ -6,7 +6,9 @@
#define ATOM_BROWSER_API_ATOM_API_MENU_VIEWS_H_
#include "atom/browser/api/atom_api_menu.h"
#include "base/memory/weak_ptr.h"
#include "ui/display/screen.h"
#include "ui/views/controls/menu/menu_runner.h"
namespace atom {
@ -18,8 +20,12 @@ class MenuViews : public Menu {
protected:
void PopupAt(Window* window, int x, int y, int positioning_item) override;
void OnMenuClosed();
private:
std::unique_ptr<views::MenuRunner> menu_runner_;
base::WeakPtrFactory<MenuViews> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(MenuViews);
};