Add callback parameter to Menu.popup

This commit is contained in:
Cheng Zhao 2018-01-01 16:26:19 +09:00
parent 2e3d940749
commit 7b01a8b860
6 changed files with 31 additions and 16 deletions

View file

@ -54,7 +54,8 @@ class Menu : public mate::TrackableObject<Menu>,
void MenuWillShow(ui::SimpleMenuModel* source) override; void MenuWillShow(ui::SimpleMenuModel* source) override;
void MenuClosed(ui::SimpleMenuModel* source) override; void MenuClosed(ui::SimpleMenuModel* source) override;
virtual void PopupAt(Window* window, int x, int y, int positioning_item) = 0; virtual void PopupAt(Window* window, int x, int y, int positioning_item,
const base::Closure& callback) = 0;
virtual void ClosePopupAt(int32_t window_id) = 0; virtual void ClosePopupAt(int32_t window_id) = 0;
std::unique_ptr<AtomMenuModel> model_; std::unique_ptr<AtomMenuModel> model_;

View file

@ -22,18 +22,20 @@ class MenuMac : public Menu {
protected: protected:
MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper); MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
void PopupAt(Window* window, int x, int y, int positioning_item) override; void PopupAt(Window* window, int x, int y, int positioning_item,
const base::Closure& callback) override;
void PopupOnUI(const base::WeakPtr<NativeWindow>& native_window, void PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
int32_t window_id, int32_t window_id,
int x, int x,
int y, int y,
int positioning_item); int positioning_item,
const base::Closure& callback);
void ClosePopupAt(int32_t window_id) override; void ClosePopupAt(int32_t window_id) override;
private: private:
friend class Menu; friend class Menu;
void OnClosed(int32_t window_id); void OnClosed(int32_t window_id, const base::Closure& callback);
scoped_nsobject<AtomMenuController> menu_controller_; scoped_nsobject<AtomMenuController> menu_controller_;

View file

@ -27,14 +27,15 @@ MenuMac::MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
weak_factory_(this) { weak_factory_(this) {
} }
void MenuMac::PopupAt(Window* window, int x, int y, int positioning_item) { void MenuMac::PopupAt(Window* window, int x, int y, int positioning_item,
const base::Closure& callback) {
NativeWindow* native_window = window->window(); NativeWindow* native_window = window->window();
if (!native_window) if (!native_window)
return; return;
auto popup = base::Bind(&MenuMac::PopupOnUI, weak_factory_.GetWeakPtr(), auto popup = base::Bind(&MenuMac::PopupOnUI, weak_factory_.GetWeakPtr(),
native_window->GetWeakPtr(), window->ID(), x, y, native_window->GetWeakPtr(), window->ID(), x, y,
positioning_item); positioning_item, callback);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, popup); BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, popup);
} }
@ -42,7 +43,8 @@ void MenuMac::PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
int32_t window_id, int32_t window_id,
int x, int x,
int y, int y,
int positioning_item) { int positioning_item,
const base::Closure& callback) {
if (!native_window) if (!native_window)
return; return;
brightray::InspectableWebContents* web_contents = brightray::InspectableWebContents* web_contents =
@ -50,8 +52,8 @@ void MenuMac::PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
if (!web_contents) if (!web_contents)
return; return;
auto close_callback = base::Bind(&MenuMac::ClosePopupAt, auto close_callback = base::Bind(
weak_factory_.GetWeakPtr(), window_id); &MenuMac::OnClosed, weak_factory_.GetWeakPtr(), window_id, callback);
popup_controllers_[window_id] = base::scoped_nsobject<AtomMenuController>( popup_controllers_[window_id] = base::scoped_nsobject<AtomMenuController>(
[[AtomMenuController alloc] initWithModel:model() [[AtomMenuController alloc] initWithModel:model()
useDefaultAccelerator:NO]); useDefaultAccelerator:NO]);
@ -122,8 +124,9 @@ void MenuMac::ClosePopupAt(int32_t window_id) {
} }
} }
void MenuMac::OnClosed(int32_t window_id) { void MenuMac::OnClosed(int32_t window_id, const base::Closure& callback) {
popup_controllers_.erase(window_id); popup_controllers_.erase(window_id);
callback.Run();
} }
// static // static

View file

@ -20,7 +20,8 @@ MenuViews::MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
weak_factory_(this) { weak_factory_(this) {
} }
void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item) { void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item,
const base::Closure& callback) {
NativeWindow* native_window = static_cast<NativeWindow*>(window->window()); NativeWindow* native_window = static_cast<NativeWindow*>(window->window());
if (!native_window) if (!native_window)
return; return;
@ -48,7 +49,7 @@ void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item) {
// Show the menu. // Show the menu.
int32_t window_id = window->ID(); int32_t window_id = window->ID();
auto close_callback = base::Bind( auto close_callback = base::Bind(
&MenuViews::OnClosed, weak_factory_.GetWeakPtr(), window_id); &MenuViews::OnClosed, weak_factory_.GetWeakPtr(), window_id, callback);
menu_runners_[window_id] = std::unique_ptr<MenuRunner>(new MenuRunner( menu_runners_[window_id] = std::unique_ptr<MenuRunner>(new MenuRunner(
model(), flags, close_callback)); model(), flags, close_callback));
menu_runners_[window_id]->RunMenuAt( menu_runners_[window_id]->RunMenuAt(
@ -73,8 +74,9 @@ void MenuViews::ClosePopupAt(int32_t window_id) {
} }
} }
void MenuViews::OnClosed(int32_t window_id) { void MenuViews::OnClosed(int32_t window_id, const base::Closure& callback) {
menu_runners_.erase(window_id); menu_runners_.erase(window_id);
callback.Run();
} }
// static // static

View file

@ -21,11 +21,12 @@ class MenuViews : public Menu {
MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper); MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
protected: protected:
void PopupAt(Window* window, int x, int y, int positioning_item) override; void PopupAt(Window* window, int x, int y, int positioning_item,
const base::Closure& callback) override;
void ClosePopupAt(int32_t window_id) override; void ClosePopupAt(int32_t window_id) override;
private: private:
void OnClosed(int32_t window_id); void OnClosed(int32_t window_id, const base::Closure& callback);
// window ID -> open context menu // window ID -> open context menu
std::map<int32_t, std::unique_ptr<views::MenuRunner>> menu_runners_; std::map<int32_t, std::unique_ptr<views::MenuRunner>> menu_runners_;

View file

@ -49,6 +49,7 @@ Menu.prototype._init = function () {
Menu.prototype.popup = function (window, x, y, positioningItem) { Menu.prototype.popup = function (window, x, y, positioningItem) {
let [newX, newY, newPosition, newWindow] = [x, y, positioningItem, window] let [newX, newY, newPosition, newWindow] = [x, y, positioningItem, window]
let opts let opts
let callback
// menu.popup(x, y, positioningItem) // menu.popup(x, y, positioningItem)
if (window != null && !(window instanceof BrowserWindow)) { if (window != null && !(window instanceof BrowserWindow)) {
@ -58,9 +59,11 @@ Menu.prototype.popup = function (window, x, y, positioningItem) {
// menu.popup({}) // menu.popup({})
if (window != null && window.constructor === Object) { if (window != null && window.constructor === Object) {
opts = window opts = window
callback = arguments[1]
// menu.popup(window, {}) // menu.popup(window, {})
} else if (x && typeof x === 'object') { } else if (x && typeof x === 'object') {
opts = x opts = x
callback = arguments[2]
} }
if (opts) { if (opts) {
@ -68,6 +71,9 @@ Menu.prototype.popup = function (window, x, y, positioningItem) {
newY = opts.y newY = opts.y
newPosition = opts.positioningItem newPosition = opts.positioningItem
} }
if (typeof callback !== 'function') {
callback = () => {}
}
// set defaults // set defaults
if (typeof newX !== 'number') newX = -1 if (typeof newX !== 'number') newX = -1
@ -88,7 +94,7 @@ Menu.prototype.popup = function (window, x, y, positioningItem) {
} }
} }
this.popupAt(newWindow, newX, newY, newPosition) this.popupAt(newWindow, newX, newY, newPosition, callback)
return { browserWindow: newWindow, x: newX, y: newY, position: newPosition } return { browserWindow: newWindow, x: newX, y: newY, position: newPosition }
} }