CEF-115 : OnContextMenuCommand issue with chrome runtime

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

CEF-115 : OnContextMenuCommand issue with chrome runtime

Postby tapineb371 » Thu Mar 14, 2024 12:17 am

I'm trying to customize the context menu items in both the cases where chrome runtime is (enabled & disabled).

Code: Select all
void CEFBrowserHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
                            CefRefPtr<CefFrame> frame,
                            CefRefPtr<CefContextMenuParams> params,
                            CefRefPtr<CefMenuModel> model)
{
    CEF_REQUIRE_UI_THREAD();
    // clearing the whole context menu and adding only required context menu items
    model->Clear();
    model->AddItem(MENU_ID_CUT, "Cut");
    model->AddItem(MENU_ID_COPY, "Copy");
    model->AddItem(MENU_ID_PASTE, "Paste");
}



Code: Select all
bool CEFBrowserHandler::OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
                            CefRefPtr<CefFrame> frame,
                            CefRefPtr<CefContextMenuParams> params,
                            int command_id,
                            EventFlags event_flags) {
    CEF_REQUIRE_UI_THREAD();

    switch (command_id) {
        case MENU_ID_CUT:
            frame->Cut();
            return true;
        case MENU_ID_COPY:
            frame->Copy();
            return true;
        case MENU_ID_PASTE:
            frame->Paste();
            return true;
        default:
            // Unknown command, let the default handler proceed.
            return false;
    }
}


I am clearing all the menu items and enabling CUT, COPY and PASTE.

This is not working when chrome runtime is ENABLED.

When chrome runtime is disabled it works as expected.

Thanks for any help in advance.
tapineb371
Techie
 
Posts: 14
Joined: Mon Sep 04, 2023 8:49 pm

Re: CEF-115 : OnContextMenuCommand issue with chrome runtime

Postby ndesktop » Thu Mar 14, 2024 2:21 am

What does it means is not working? The functions are not called, called but not adding the menu items, adding the menu items but not the expected command IDs?
CEF is building its context menu based on CEF command identifiers, while Chrome has its own.
For example, CEF has MENU_ID_BACK, while Chrome has IDC_BACK, CEF's MENU_ID_FORWARD maps to IDC_FORWARD etc.

How I solved this was to modify my CEF fork and expose a CefMapChromeCommandId function, and extract the actual Chrome command ID and go on (remove, add, invoke etc.)
This is how the map I am using looks like (excerpt from CEF fork):
Code: Select all
static const struct cef_chrome_map_cmd_id {
    int cef_command_id;
    int chrome_command_id;
  } kCefToChromeCommandIds[] = {
    { MENU_ID_BACK, IDC_BACK },
    { MENU_ID_FORWARD, IDC_FORWARD },
    { MENU_ID_RELOAD, IDC_RELOAD },
    { MENU_ID_RELOAD_NOCACHE, IDC_RELOAD_BYPASSING_CACHE },
    { MENU_ID_STOPLOAD, IDC_STOP },

    { MENU_ID_PRINT, IDC_PRINT },

    { MENU_ID_CUT, IDC_CONTENT_CONTEXT_CUT },
    { MENU_ID_COPY, IDC_CONTENT_CONTEXT_COPY },
    { MENU_ID_PASTE, IDC_CONTENT_CONTEXT_PASTE },
    { MENU_ID_DELETE, IDC_CONTENT_CONTEXT_DELETE },
    { MENU_ID_SELECT_ALL, IDC_CONTENT_CONTEXT_SELECTALL },
    { MENU_ID_UNDO, IDC_CONTENT_CONTEXT_UNDO },
    { MENU_ID_REDO, IDC_CONTENT_CONTEXT_REDO },
  };
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 165 guests