salvadordf/WebView4Delphi

Bug in Lazarus regarding the "ContextMenuRequested" event

Closed this issue · 2 comments

Hello.
I encountered an issue using the custom right-click menu callback event "ContextMenuRequested" in Lazarus.
When setting the menu item to select an event proxy object, it causes the browser object (WVBrowser1) address to become invalid.
critical code:
Current browser object:
TempMenuItem.AddCustomItemSelectedEvent(WVBrowser1);

Temporary copy:
TempMenuItem.AddCustomItemSelectedEvent(WVBrowser2);

Operation steps:
After the program runs.

  1. In the browser window, right-click the mouse and the menu will pop up correctly. Execute correctly.
  2. Click on the window (TButton) to trigger an event. Execute correctly. (Switchable window, uncertain when this issue will occur)
  3. In the browser window, right-click the mouse and it does not execute correctly. The window appears unresponsive and freezes.

The reproduction method of this problem can be repeatedly found by right clicking on the browser window, clicking a button, or switching windows

The current temporary solution is to create another TWVBrowser object and only handle the Custom Item Selected event.

Code change from CookieManager example

procedure TMainForm.WVBrowser1ContextMenuRequested(Sender: TObject; const aWebView: ICoreWebView2; const aArgs: ICoreWebView2ContextMenuRequestedEventArgs);
var
  TempArgs: TCoreWebView2ContextMenuRequestedEventArgs;
  TempCollection: TCoreWebView2ContextMenuItemCollection;
  TempMenuItemItf: ICoreWebView2ContextMenuItem;
  TempMenuItem: TCoreWebView2ContextMenuItem;
begin
  TempArgs := TCoreWebView2ContextMenuRequestedEventArgs.Create(aArgs);
  TempCollection := TCoreWebView2ContextMenuItemCollection.Create(TempArgs.MenuItems);
  TempMenuItem := nil;

  try
    TempCollection.RemoveAllMenuItems;

    if WVBrowser1.CoreWebView2Environment.CreateContextMenuItem('Exit', nil, COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, TempMenuItemItf) then
    try
      TempMenuItem := TCoreWebView2ContextMenuItem.Create(TempMenuItemItf);
      FExitCommandID := TempMenuItem.CommandId;
      TempMenuItem.AddCustomItemSelectedEvent(WVBrowser1);// Replace temporary resolution code here
      TempCollection.InsertValueAtIndex(0, TempMenuItemItf);
    finally
      FreeAndNil(TempMenuItem);
    end;
  finally
    FreeAndNil(TempCollection);
    FreeAndNil(TempArgs);
  end;
end;
procedure TMainForm.WVBrowser1CustomItemSelected(Sender: TObject; const aMenuItem: ICoreWebView2ContextMenuItem);
var
  TempMenuItem: TCoreWebView2ContextMenuItem;
begin
  TempMenuItem := TCoreWebView2ContextMenuItem.Create(aMenuItem);

  if (TempMenuItem.CommandId = FExitCommandID) then
    PostMessage(Handle, WM_CLOSE, 0, 0);
  FreeAndNil(TempMenuItem);
end;

First run display window:
window_1

No feedback from the window after right clicking:
window_2

The window is stuck:
window_3

Modify the compressed sample code from CookieManager

Lazarus

CookieManager.zip

Thanks for reporting this issue! :)

I just uploaded the fix and a modified CookieManager demo for Lazarus showing how to add a custom context menu item.

TCoreWebView2ContextMenuItem now adds and removes the browser events correctly.

Notice how the custom menu item is only created once because custom items should be reused whenever possible.

This is a closer code translation of the official code snippet.