WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

Working with context menu

Open ajtruckle opened this issue 1 year ago • 2 comments

I am confused.

Take this handler snippet:

// ===============================================================
// Refresh AJT 23.2.0 JIRA MSA 210
if (!m_itemRefresh || bFullInit)
{
	m_itemRefresh.reset();
	wil::com_ptr<IStream> iconStream;
	CHECK_FAILURE(SHCreateStreamOnFileEx(
		theApp.GetCommonAppDataFolder() + L"WebView2\\refresh" + strIconSuffix, STGM_READ, FILE_ATTRIBUTE_NORMAL, FALSE,
		nullptr, &iconStream));

	CString strMenuText;
	strMenuText.LoadString(IDS_STR_MENU_REFRESH);
	CHECK_FAILURE(webviewEnvironment->CreateContextMenuItem(
		strMenuText, iconStream.get(),
		COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, &m_itemRefresh));

	CHECK_FAILURE(m_itemRefresh->add_CustomItemSelected(
		Callback<ICoreWebView2CustomItemSelectedEventHandler>(
			[appWindow = this, target](ICoreWebView2ContextMenuItem* sender, IUnknown* args)
			{
				appWindow->GetParentWindow()->SendMessage(WM_COMMAND, ID_VIEW_REFRESH, NULL);

				return S_OK;
			})
		.Get(), nullptr));
}
CHECK_FAILURE(items->InsertValueAtIndex(0, m_itemRefresh.get()));
// ===============================================================

Now, if my context is:

CDialog > WebView2 > Right-click > Refresh

Works. GetParentWindow() is the pointer to CDialog.

Now, if my context is:

CDialog > TabCtrl > WebView2 Child Container > WebView2 > Right-click > Refresh

It doesn't work. I realised this was because GetParentWindow() is going to be WebView2 Child Container. So I tried two things:

1/ GetParentWindow()->GetParent()>GetParent()->PostMessage ... The main dialog code is intercepted. But the moment the AfxMessageBox call happens in main dialog the app appears to freeze. I can see no popup. The app feels it is running this but I can't respond to the popup message box.

2/ When my WV2 is created, and before setting up the handlers, I pass it my WebView2 Child Container pointer. Then in the event handler it uses that pointer. Then, in WebView2 Child container I catch that, and post (or send) up the chain to CDialog. The result is the same, the moment a AfxMessageBox is encounter, app is freezing.

I have spent all afternoon and evening trying to get around this. The basic scenario is fine.

My latter scenario is a dialog, which has a TabCtrl, which has 3 tabs, each with a WebView2 Child Container, housing a WebView2.

I appreciate help. Thanks.

Win32.

ajtruckle avatar Feb 27 '25 21:02 ajtruckle

Can you please share a sample app where I can repro this?

chetanpandey1266 avatar Mar 18 '25 09:03 chetanpandey1266

@chetanpandey1266 I had no choice but to change my logic to not have an embedded WebView2 as a child control. So I resized by tab bar to just be a tab bar and a single webview2 on my dialog. This way the parent mechanics does not break.

I am afraid that I don't have a sample app that I can share. But in essence:

  1. Use the TabCtrl I referred to as a child element on a CDialog.
  2. Add some tab items, with a WebView2 as a child in each.
  3. Those WebView2 instances should have a context menu handler.

There lies the issue. The parent of the WebView2 is the tab control. Hence the inheritance issue.

I decided to keep it as I had before, with no actual tab children.

ajtruckle avatar Mar 18 '25 09:03 ajtruckle