Add callback parameter to Menu.popup
This commit is contained in:
		
					parent
					
						
							
								2e3d940749
							
						
					
				
			
			
				commit
				
					
						7b01a8b860
					
				
			
		
					 6 changed files with 31 additions and 16 deletions
				
			
		| 
						 | 
				
			
			@ -54,7 +54,8 @@ class Menu : public mate::TrackableObject<Menu>,
 | 
			
		|||
  void MenuWillShow(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;
 | 
			
		||||
 | 
			
		||||
  std::unique_ptr<AtomMenuModel> model_;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,18 +22,20 @@ class MenuMac : public Menu {
 | 
			
		|||
 protected:
 | 
			
		||||
  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,
 | 
			
		||||
                 int32_t window_id,
 | 
			
		||||
                 int x,
 | 
			
		||||
                 int y,
 | 
			
		||||
                 int positioning_item);
 | 
			
		||||
                 int positioning_item,
 | 
			
		||||
                 const base::Closure& callback);
 | 
			
		||||
  void ClosePopupAt(int32_t window_id) override;
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  friend class Menu;
 | 
			
		||||
 | 
			
		||||
  void OnClosed(int32_t window_id);
 | 
			
		||||
  void OnClosed(int32_t window_id, const base::Closure& callback);
 | 
			
		||||
 | 
			
		||||
  scoped_nsobject<AtomMenuController> menu_controller_;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,14 +27,15 @@ MenuMac::MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
 | 
			
		|||
      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();
 | 
			
		||||
  if (!native_window)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  auto popup = base::Bind(&MenuMac::PopupOnUI, weak_factory_.GetWeakPtr(),
 | 
			
		||||
                          native_window->GetWeakPtr(), window->ID(), x, y,
 | 
			
		||||
                          positioning_item);
 | 
			
		||||
                          positioning_item, callback);
 | 
			
		||||
  BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, popup);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -42,7 +43,8 @@ void MenuMac::PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
 | 
			
		|||
                        int32_t window_id,
 | 
			
		||||
                        int x,
 | 
			
		||||
                        int y,
 | 
			
		||||
                        int positioning_item) {
 | 
			
		||||
                        int positioning_item,
 | 
			
		||||
                        const base::Closure& callback) {
 | 
			
		||||
  if (!native_window)
 | 
			
		||||
    return;
 | 
			
		||||
  brightray::InspectableWebContents* web_contents =
 | 
			
		||||
| 
						 | 
				
			
			@ -50,8 +52,8 @@ void MenuMac::PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
 | 
			
		|||
  if (!web_contents)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  auto close_callback = base::Bind(&MenuMac::ClosePopupAt,
 | 
			
		||||
                                   weak_factory_.GetWeakPtr(), window_id);
 | 
			
		||||
  auto close_callback = base::Bind(
 | 
			
		||||
      &MenuMac::OnClosed, weak_factory_.GetWeakPtr(), window_id, callback);
 | 
			
		||||
  popup_controllers_[window_id] = base::scoped_nsobject<AtomMenuController>(
 | 
			
		||||
      [[AtomMenuController alloc] initWithModel:model()
 | 
			
		||||
                          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);
 | 
			
		||||
  callback.Run();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,7 +20,8 @@ MenuViews::MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
 | 
			
		|||
      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());
 | 
			
		||||
  if (!native_window)
 | 
			
		||||
    return;
 | 
			
		||||
| 
						 | 
				
			
			@ -48,7 +49,7 @@ void MenuViews::PopupAt(Window* window, int x, int y, int positioning_item) {
 | 
			
		|||
  // Show the menu.
 | 
			
		||||
  int32_t window_id = window->ID();
 | 
			
		||||
  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(
 | 
			
		||||
      model(), flags, close_callback));
 | 
			
		||||
  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);
 | 
			
		||||
  callback.Run();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,11 +21,12 @@ class MenuViews : public Menu {
 | 
			
		|||
  MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
 | 
			
		||||
 | 
			
		||||
 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;
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  void OnClosed(int32_t window_id);
 | 
			
		||||
  void OnClosed(int32_t window_id, const base::Closure& callback);
 | 
			
		||||
 | 
			
		||||
  // window ID -> open context menu
 | 
			
		||||
  std::map<int32_t, std::unique_ptr<views::MenuRunner>> menu_runners_;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -49,6 +49,7 @@ Menu.prototype._init = function () {
 | 
			
		|||
Menu.prototype.popup = function (window, x, y, positioningItem) {
 | 
			
		||||
  let [newX, newY, newPosition, newWindow] = [x, y, positioningItem, window]
 | 
			
		||||
  let opts
 | 
			
		||||
  let callback
 | 
			
		||||
 | 
			
		||||
  // menu.popup(x, y, positioningItem)
 | 
			
		||||
  if (window != null && !(window instanceof BrowserWindow)) {
 | 
			
		||||
| 
						 | 
				
			
			@ -58,9 +59,11 @@ Menu.prototype.popup = function (window, x, y, positioningItem) {
 | 
			
		|||
  // menu.popup({})
 | 
			
		||||
  if (window != null && window.constructor === Object) {
 | 
			
		||||
    opts = window
 | 
			
		||||
    callback = arguments[1]
 | 
			
		||||
  // menu.popup(window, {})
 | 
			
		||||
  } else if (x && typeof x === 'object') {
 | 
			
		||||
    opts = x
 | 
			
		||||
    callback = arguments[2]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (opts) {
 | 
			
		||||
| 
						 | 
				
			
			@ -68,6 +71,9 @@ Menu.prototype.popup = function (window, x, y, positioningItem) {
 | 
			
		|||
    newY = opts.y
 | 
			
		||||
    newPosition = opts.positioningItem
 | 
			
		||||
  }
 | 
			
		||||
  if (typeof callback !== 'function') {
 | 
			
		||||
    callback = () => {}
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // set defaults
 | 
			
		||||
  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 }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue