Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
HIllya51 committed Jan 6, 2025
1 parent ce22ac8 commit 1ca1315
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
76 changes: 41 additions & 35 deletions cpp/winsharedutils/webview2_extra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,22 @@ DECLARE_API void *add_WebMessageReceived(void *m_host, void (*callback)(const wc
struct contextcallbackdatas
{
EventRegistrationToken contextMenuRequestedToken;
std::wstring label;
std::vector<std::pair<std::wstring, void (*)(const wchar_t *)>> menus;
};
#endif
// https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/how-to/context-menus?tabs=cpp
// https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2_11?view=webview2-1.0.2849.39
DECLARE_API void *add_ContextMenuRequested(void *m_host, int index, const wchar_t *label, void (*callback)(const wchar_t *))
DECLARE_API void add_menu_list(void *ptr, int index, const wchar_t *label, void (*callback)(const wchar_t *))
{
if (!ptr)
return;
auto token = reinterpret_cast<contextcallbackdatas *>(ptr);
token->menus.insert(token->menus.begin() + index, std::make_pair(label, callback));
}
DECLARE_API void *add_ContextMenuRequested(void *m_host)
{
#ifndef WINXP
contextcallbackdatas *data = new contextcallbackdatas;
data->label = label; // 持久化
[=]()
{
wil::com_ptr<ICoreWebView2Controller> m_controller(reinterpret_cast<ICoreWebView2Controller *>(m_host));
Expand Down Expand Up @@ -235,40 +241,40 @@ DECLARE_API void *add_ContextMenuRequested(void *m_host, int index, const wchar_
UINT32 itemsCount;
CHECK_FAILURE(items->get_Count(&itemsCount));
// Adding a custom context menu item for the page that will display the page's URI.
wil::com_ptr<ICoreWebView2ContextMenuItem> newMenuItem;
if (data->label.size())
{
CHECK_FAILURE(webviewEnvironment_5->CreateContextMenuItem(
data->label.c_str(),
nullptr,
COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, &newMenuItem));
newMenuItem->add_CustomItemSelected(
Callback<ICoreWebView2CustomItemSelectedEventHandler>(
[=](
ICoreWebView2ContextMenuItem *sender,
IUnknown *args)
{
wil::unique_cotaskmem_string selecttext;
CHECK_FAILURE(target->get_SelectionText(&selecttext));
callback(selecttext.get());
return S_OK;
})
.Get(),
nullptr);
}
else
UINT idx = 0;
for (auto &&[label, callback] : data->menus)
{
CHECK_FAILURE(webviewEnvironment_5->CreateContextMenuItem(
L"",
nullptr,
COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_SEPARATOR, &newMenuItem));
wil::com_ptr<ICoreWebView2ContextMenuItem> newMenuItem;
if (label.size())
{
CHECK_FAILURE(webviewEnvironment_5->CreateContextMenuItem(
label.c_str(),
nullptr,
COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, &newMenuItem));
newMenuItem->add_CustomItemSelected(
Callback<ICoreWebView2CustomItemSelectedEventHandler>(
[=, &callback](
ICoreWebView2ContextMenuItem *sender,
IUnknown *args)
{
wil::unique_cotaskmem_string selecttext;
CHECK_FAILURE(target->get_SelectionText(&selecttext));
callback(selecttext.get());
return S_OK;
})
.Get(),
nullptr);
}
else
{
CHECK_FAILURE(webviewEnvironment_5->CreateContextMenuItem(
L"",
nullptr,
COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_SEPARATOR, &newMenuItem));
}
CHECK_FAILURE(items->InsertValueAtIndex(idx++, newMenuItem.get()));
}
UINT idx;
if (index == -1)
idx = itemsCount;
else
idx = index;
CHECK_FAILURE(items->InsertValueAtIndex(idx, newMenuItem.get()));

return S_OK;
})
.Get(),
Expand Down
11 changes: 3 additions & 8 deletions py/LunaTranslator/gui/usefulwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,7 @@ def __del__(self):
winsharedutils.remove_WebMessageReceived(
self.get_controller(), self.m_webMessageReceivedToken
)
for m in self.addmenu_infos:
winsharedutils.remove_ContextMenuRequested(self.get_controller(), m)
winsharedutils.remove_ContextMenuRequested(self.get_controller(), self.menudata)

def bind(self, fname, func):
self.webview.bind(fname, func)
Expand All @@ -1210,19 +1209,15 @@ def get_hwnd(self):
def add_menu(self, index, label, callback):
__ = winsharedutils.add_ContextMenuRequested_cb(callback)
self.callbacks.append(__)
self.addmenu_infos.append(
winsharedutils.add_ContextMenuRequested(
self.get_controller(), index, label, __
)
)
winsharedutils.add_menu_list(self.menudata, index, label, __)

def __init__(self, parent=None, debug=True, usedarklight=True) -> None:
super().__init__(parent)
self.webview = None
self.callbacks = []
self.addmenu_infos = []
self.webview = Webview(debug=debug, window=int(self.winId()))
self.m_webMessageReceivedToken = None
self.menudata = winsharedutils.add_ContextMenuRequested(self.get_controller())
self.zoomfunc = winsharedutils.add_ZoomFactorChanged_CALLBACK(
self.on_ZoomFactorChanged.emit
)
Expand Down
9 changes: 3 additions & 6 deletions py/LunaTranslator/winsharedutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,12 @@ def cb(ptr, size):
remove_WebMessageReceived.argtypes = c_void_p, c_void_p
add_ContextMenuRequested_cb = CFUNCTYPE(c_void_p, c_wchar_p)
add_ContextMenuRequested = utilsdll.add_ContextMenuRequested
add_ContextMenuRequested.argtypes = (
c_void_p,
c_int,
c_wchar_p,
add_ContextMenuRequested_cb,
)
add_ContextMenuRequested.argtypes = (c_void_p,)
add_ContextMenuRequested.restype = c_void_p
remove_ContextMenuRequested = utilsdll.remove_ContextMenuRequested
remove_ContextMenuRequested.argtypes = c_void_p, c_void_p
add_menu_list = utilsdll.add_menu_list
add_menu_list.argtypes = (c_void_p, c_int, c_wchar_p, add_ContextMenuRequested_cb)
StartCaptureAsync_cb = CFUNCTYPE(None, c_void_p, c_size_t)
StartCaptureAsync = utilsdll.StartCaptureAsync
StartCaptureAsync.argtypes = (StartCaptureAsync_cb,)
Expand Down

0 comments on commit 1ca1315

Please sign in to comment.