electron/shell/browser/api/electron_api_menu_mac.h
trop[bot] e2b9cedf0f
feat: add support for associating a Menu with a WebFrameMain (#46350)
feat: add support for associating a Menu with a WebFrameMain (#45138)

* feat: add support for associating a Menu with a WebFrameMain

This allows certain OS level features to activate such as Writing Tools, Autofill.. and Services.

There appears to be a bug in macOS where the responder chain isn't traversed if the menu is not popped up using an event, as such we spoof a fake mouse event at the write coordinates in the right window and use that to open the menu.

* build: fix build on non-mac

* build: oops missed a header

* fix: safely handle optional T* by checking nullptr too

* build: fix gn check and build errors

* docs: suggested changes

* feat: default `frame` to `window.webContents.mainFrame` when possible

* fix: avoid deref nullptr view

* Revert "feat: default `frame` to `window.webContents.mainFrame` when possible"

This reverts commit 2e888368199317d67f6ad931a7e9eff0295c4b1b.

* fix: lint

* Remove redundant scoped objects

This code, including the comments, matches almost exactly the behavior of this argument to the function.

* Add ScopedPumpMessagesInPrivateModes patch

* More null pointer safety

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Samuel Attard <sam@electronjs.org>
2025-04-01 09:00:46 -05:00

60 lines
1.7 KiB
Objective-C

// Copyright (c) 2013 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_MENU_MAC_H_
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_MENU_MAC_H_
#include "shell/browser/api/electron_api_menu.h"
#include <map>
#import "shell/browser/ui/cocoa/electron_menu_controller.h"
namespace electron {
class NativeWindow;
class WebFrameMain;
namespace api {
class MenuMac : public Menu {
protected:
explicit MenuMac(gin::Arguments* args);
~MenuMac() override;
// Menu
void PopupAt(BaseWindow* window,
std::optional<WebFrameMain*> frame,
int x,
int y,
int positioning_item,
ui::mojom::MenuSourceType source_type,
base::OnceClosure callback) override;
void PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
const base::WeakPtr<WebFrameMain>& frame,
int32_t window_id,
int x,
int y,
int positioning_item,
base::OnceClosure callback);
void ClosePopupAt(int32_t window_id) override;
std::u16string GetAcceleratorTextAtForTesting(int index) const override;
private:
friend class Menu;
void ClosePopupOnUI(int32_t window_id);
void OnClosed(int32_t window_id, base::OnceClosure callback);
ElectronMenuController* __strong menu_controller_;
// window ID -> open context menu
std::map<int32_t, ElectronMenuController*> popup_controllers_;
base::WeakPtrFactory<MenuMac> weak_factory_{this};
};
} // namespace api
} // namespace electron
#endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_MENU_MAC_H_