CefMenuModel
Header: cef_menu_model.h
Interfaces: CefMenuModel, CefMenuModelDelegate
Category: Core APIs
Overview
Foundational interfaces: process bootstrap, browser lifecycle, frames, DOM, requests, and data objects.
CefMenuModel
Source File: include/cef_menu_model.h
Process / Thread Context: Browser Process / UI Thread (per class comment: "The methods of this class can only be accessed on the browser process the UI thread.")
Purpose: USER-CALLED library interface for building and modifying context/application menus. Supports items, separators, check/radio items, sub-menus, accelerators, colors and fonts. Built-in command IDs are listed in cef_menu_id_t; user-defined IDs should be in [MENU_ID_USER_FIRST, MENU_ID_USER_LAST].
Methods
static CefRefPtr<CefMenuModel> CreateMenuModel(CefRefPtr<CefMenuModelDelegate> delegate)
Parameters:
delegate: Delegate that receives menu events (seeCefMenuModelDelegate).
Return Value: A new menu model bound to delegate.
Usage Instruction: Use to build context menus in CefContextMenuHandler::RunContextMenu or application menus.
Threading Constraint: Browser process UI thread.
bool IsSubMenu()
Return Value: True if this model is a sub-menu of another model.
Threading Constraint: Threading constraint not specified in source.
bool Clear()
Return Value: Removes all items. Returns true on success.
Threading Constraint: Threading constraint not specified in source.
size_t GetCount()
Return Value: Number of items.
Threading Constraint: Threading constraint not specified in source.
bool AddSeparator()
Return Value: Appends a separator.
Threading Constraint: Threading constraint not specified in source.
bool AddItem(int command_id, const CefString& label)
Return Value: Appends a normal item.
Threading Constraint: Threading constraint not specified in source.
bool AddCheckItem(int command_id, const CefString& label)
Return Value: Appends a check item.
Threading Constraint: Threading constraint not specified in source.
bool AddRadioItem(int command_id, const CefString& label, int group_id)
Return Value: Appends a radio item. Only one item per group_id may be checked at a time.
Threading Constraint: Threading constraint not specified in source.
CefRefPtr<CefMenuModel> AddSubMenu(int command_id, const CefString& label)
Return Value: Appends a sub-menu and returns the new sub-menu model.
Threading Constraint: Threading constraint not specified in source.
bool InsertSeparatorAt(size_t index)
bool InsertItemAt(size_t index, int command_id, const CefString& label)
bool InsertCheckItemAt(size_t index, int command_id, const CefString& label)
bool InsertRadioItemAt(size_t index, int command_id, const CefString& label, int group_id)
CefRefPtr<CefMenuModel> InsertSubMenuAt(size_t index, int command_id, const CefString& label)
Return Value: Returns the new sub-menu.
Threading Constraint: Threading constraint not specified in source.
bool Remove(int command_id)
Return Value: Removes by command ID. True on success.
Threading Constraint: Threading constraint not specified in source.
bool RemoveAt(size_t index)
Return Value: Removes by index. True on success.
Threading Constraint: Threading constraint not specified in source.
int GetIndexOf(int command_id)
Return Value: Returns index of command_id or -1 if not found.
Threading Constraint: Threading constraint not specified in source.
int GetCommandIdAt(size_t index)
Return Value: Returns command ID at index, or -1 if invalid range or separator.
Threading Constraint: Threading constraint not specified in source.
bool SetCommandIdAt(size_t index, int command_id)
Return Value: Sets command ID at index.
Threading Constraint: Threading constraint not specified in source.
CefString GetLabel(int command_id)** / **GetLabelAt(size_t index)
bool SetLabel(int command_id, const CefString& label)** / **SetLabelAt(size_t index, const CefString& label)
bool SetGroupId(int command_id, int group_id)** / **SetGroupIdAt(size_t index, int group_id)
bool IsVisible(int command_id)** / **IsVisibleAt(size_t index)
bool SetVisible(int command_id, bool visible)** / **SetVisibleAt(size_t index, bool visible)
bool IsEnabled(int command_id)** / **IsEnabledAt(size_t index)
bool SetEnabled(int command_id, bool enabled)** / **SetEnabledAt(size_t index, bool enabled)
bool IsChecked(int command_id)** / **IsCheckedAt(size_t index)
bool SetChecked(int command_id, bool checked)** / **SetCheckedAt(size_t index, bool checked)
bool RemoveAccelerator(int command_id)** / **RemoveAcceleratorAt(size_t index)
bool SetColor(int command_id, cef_menu_color_type_t color_type, cef_color_t color)
Return Value: Sets explicit color for the specified item and color type.
Threading Constraint: Threading constraint not specified in source.
bool SetColorAt(int index, cef_menu_color_type_t color_type, cef_color_t color)
Return Value: Sets color by index; index == -1 sets default for items without explicit color.
Threading Constraint: Threading constraint not specified in source.
bool GetColor(int command_id, cef_menu_color_type_t color_type, cef_color_t& color)
Return Value: Returns the explicitly set color in color; 0 if not set.
Threading Constraint: Threading constraint not specified in source.
bool GetColorAt(int index, cef_menu_color_type_t color_type, cef_color_t& color)
Return Value: Returns the explicitly set color (or default with index == -1).
Threading Constraint: Threading constraint not specified in source.
bool SetFontList(int command_id, const CefString& font_list)
Return Value: Sets font for specified item. Empty font_list uses system font.
Threading Constraint: Threading constraint not specified in source.
bool SetFontListAt(int index, const CefString& font_list)
Return Value: Sets font at index; index == -1 sets default font.
Threading Constraint: Threading constraint not specified in source.
Usage Example
// Build a context menu in CefContextMenuHandler::RunContextMenu:
class MyMenuDelegate : public CefMenuModelDelegate { /* ... see below ... */ };
bool RunContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model,
CefRefPtr<CefRunContextMenuCallback> callback) override {
CefRefPtr<CefMenuModel> myMenu =
CefMenuModel::CreateMenuModel(new MyMenuDelegate());
myMenu->AddItem(MENU_ID_USER_FIRST + 1, "Open Link");
myMenu->AddCheckItem(MENU_ID_USER_FIRST + 2, "Auto-Reload");
myMenu->AddSeparator();
CefRefPtr<CefMenuModel> sub = myMenu->AddSubMenu(MENU_ID_USER_FIRST + 3, "More");
sub->AddItem(MENU_ID_USER_FIRST + 4, "Sub-action");
myMenu->SetAccelerator(MENU_ID_USER_FIRST + 1, 'O', false, true, false);
myMenu->SetChecked(MENU_ID_USER_FIRST + 2, auto_reload_);
callback->ContinueContextMenu(myMenu);
return true;
}CefMenuModelDelegate
Source File: include/cef_menu_model_delegate.h
Process / Thread Context: Browser Process / UI Thread (per class comment: "The methods of this class will be called on the browser process UI thread unless otherwise indicated.")
Purpose: USER-IMPLEMENTED delegate interface receiving events from a CefMenuModel (command execution, mouse outside menu, keyboard submenu events, menu show/close, label formatting).
Usage Example
class MyMenuDelegate : public CefMenuModelDelegate {
public:
void ExecuteCommand(CefRefPtr<CefMenuModel> menu_model,
int command_id,
cef_event_flags_t event_flags) override {
switch (command_id) {
case MENU_ID_USER_FIRST + 1: OpenLink(); break;
case MENU_ID_USER_FIRST + 2:
auto_reload_ = !auto_reload_;
menu_model->SetChecked(command_id, auto_reload_);
break;
}
}
void MenuClosed(CefRefPtr<CefMenuModel> menu_model) override {
// release per-invocation state
}
bool auto_reload_ = false;
};