Skip to content

[Problem/Bug]: Context Menu Icon is Cached #5203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jussij opened this issue Apr 13, 2025 · 5 comments
Open

[Problem/Bug]: Context Menu Icon is Cached #5203

jussij opened this issue Apr 13, 2025 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@jussij
Copy link

jussij commented Apr 13, 2025

What happened?

I have this code that adds a menu item to the context menu of the WebVeiw2 based on the current dark mode setting.

HRESULT WebBrowser::OnCreateWebViewControllerCompleted(HRESULT result, ICoreWebView2Controller *controller)
{
  if (result == S_OK)
  {
    if (controller != nullptr)
    {
      m_pImpl->m_webController = controller;
      CHECK_FAILURE(controller->get_CoreWebView2(&m_pImpl->m_webView));

      if (!m_pImpl->m_webView)
        return E_FAIL;

      CHECK_FAILURE(m_pImpl->m_webView->QueryInterface(IID_PPV_ARGS(&m_pImpl->m_webView2)));
      CHECK_FAILURE(m_pImpl->m_webView->QueryInterface(IID_PPV_ARGS(&m_pImpl->m_webView2_16)));

      m_pImpl->m_webView2_16->add_ContextMenuRequested(
        Callback<ICoreWebView2ContextMenuRequestedEventHandler>(
          [this](
            ICoreWebView2 * sender,
            ICoreWebView2ContextMenuRequestedEventArgs * args)
      {
        wil::com_ptr<ICoreWebView2ContextMenuItemCollection> items;
        CHECK_FAILURE(args->get_MenuItems(&items));
        wil::com_ptr<ICoreWebView2ContextMenuTarget> target;
        CHECK_FAILURE(args->get_ContextMenuTarget(&target));
        COREWEBVIEW2_CONTEXT_MENU_TARGET_KIND context_kind;
        CHECK_FAILURE(target->get_Kind(&context_kind));

        if (context_kind == COREWEBVIEW2_CONTEXT_MENU_TARGET_KIND_PAGE)
        {
          wil::com_ptr<IStream> iconStream;
          wil::com_ptr<ICoreWebView2ContextMenuItem> newMenuItem;

          // pick a resource matching the current dark mode
          Resource &resource = (DarkTheme::IsDark()) ? m_resourceDM : m_resourceLM;

          // an IStream for the menu item is created by loading a PNG file from resource
          resource.createStream(&iconStream);

          auto label = (DarkTheme::IsDark()) ? L"Export to PDF (dark)" : L"Export to PDF (light)";

          CHECK_FAILURE(m_pImpl->m_webViewEnvironment9->CreateContextMenuItem(label, iconStream.get(), COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, &newMenuItem));

          CHECK_FAILURE(items->InsertValueAtIndex(5, newMenuItem.get()));
        }

        return S_OK;
      })
      .Get(),
      &m_contextMenuRequestedToken);

    }
  }
  return S_OK;
}

The WebBrowser is a control contained in a modeless popup dialog and the popup menu is working as expect as shown below:

Image

When the modeless dialog is left open and the dark mode is changed, the WebVeiw2 control response to the change in dark mode as expected.

However, when the context menu is displayed, while the menu item label now reflects the new dark mode, the icon does not change as shown below:

Image

If the modeless dialog is closed and reopened the context menu icon then synchs up with the current dark mode as shown in the earlier image.

So, it would appear that the icon is somehow being cached somehow, despite it being created each time the context menu is requested.

Why is it behaving the way it does, and can this be fixed?

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

No response

SDK Version

No response

Framework

Win32

Operating System

Windows 10

OS Version

19045.5737

Repro steps

The issue is described in the comment and the code.

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

@jussij jussij added the bug Something isn't working label Apr 13, 2025
@chetanpandey1266
Copy link

Hi @jussij Do you have a sample app where I can repro this issue?

@jussij
Copy link
Author

jussij commented Apr 14, 2025

I don't have any simple app that demonstrates this. What I will try is adding this context menu to one of the WebView2 sample apps to see if I can replicate the issue there.

But having thought about this, I think the dark mode portion of the bug report is irrelevant. If the code was changed to just cycle between one of two menus using a static counter and a mod 2, the same issue should be observer. As the context menu is repeatedly displayed, I suspect the labels would change to match the state of the counter mod2, but the icon would stay the same.

In any case, I'll try to create a simple app to reproduce the issue and if I manage this, I'll put it on GitHub and post a link here.

@jussij
Copy link
Author

jussij commented Apr 15, 2025

I've modified one of the WebView2 sample projects to replicate this issue.

That modified project can be found here: https://github.com/jussij/WebView2SampleWinComp.git

My changes are prefix by this comment: //jussij

Most of the changes are made to this file: AppWindow.cpp

Building and running this application loads a web page that displays a 'WebView sample page' in the center of the application.

Right clicking on that central region brings up a context menu containing a new menu item that was added in the AppWindow.cpp code at line: 295

If you dismiss and redisplay the popup, you'll notice the menu text cycles through two menu labels (light and dark), but the menu icon does not. The image below shows how the text of the label changes, but the green icon always remains green:

Image

And if you now change this line of code found in the AppWindow.cpp file:

static int counter = 0;

making it this:

static int counter = 1;

Now the context menu always displays the blue icon:

Image

So, while the code adding the new menu item can control the text of the new menu item, it has no control over the icon. The icon appears to always take the value of the first icon used, as if the image has been cached.

@chetanpandey1266
Copy link

Is this a private repo? I'm unable to access it.

@jussij
Copy link
Author

jussij commented Apr 18, 2025

My mistake. I forgot to make it public. You should now have access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants