Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.
37 changes: 35 additions & 2 deletions appshell/appshell_extensions_gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1098,16 +1098,49 @@ int32 GetMenuItemState(CefRefPtr<CefBrowser> browser, ExtensionString commandId,
return NO_ERROR;
}

int _getMenuItemPosition(GtkWidget* parent, GtkWidget* menuItem)
{
GList* children = gtk_container_get_children(GTK_CONTAINER(parent));
int index = 0;
do {
GtkWidget* widget = (GtkWidget*) children->data;
if (widget == menuItem) {
return index;
}
index++;
} while ((children = g_list_next(children)) != NULL);
g_list_free(children);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it neccessary to free the list?


return -1;
}

int32 SetMenuItemState(CefRefPtr<CefBrowser> browser, ExtensionString command, bool& enabled, bool& checked)
{
// TODO: Implement functionality for checked
NativeMenuModel& model = NativeMenuModel::getInstance(getMenuParent(browser));
int tag = model.getTag(command);
if (tag == kTagNotFound) {
return ERR_NOT_FOUND;
}
GtkWidget* menuItem = (GtkWidget*) model.getOsItem(tag);
gtk_widget_set_sensitive(menuItem, enabled);
GtkWidget* parent = gtk_widget_get_parent(menuItem);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When dealing with pointer it is always good to check for NULL pointers, even if it is obvious. That ways the we can avoid an app crash.

int position = _getMenuItemPosition(parent, menuItem);
const gchar* label = gtk_menu_item_get_label(GTK_MENU_ITEM(menuItem));

GtkWidget* newMenuItem;
if (checked == true) {
newMenuItem = gtk_check_menu_item_new_with_label(label);
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(newMenuItem), true);
} else if (checked == false){
newMenuItem = gtk_menu_item_new_with_label(label);
}
gtk_widget_destroy(menuItem);

InstallMenuHandler(newMenuItem, browser, tag);
model.setOsItem(tag, newMenuItem);
gtk_menu_shell_insert(GTK_MENU_SHELL(parent), newMenuItem, position);
gtk_widget_set_sensitive(newMenuItem, enabled);
gtk_widget_show(newMenuItem);

return NO_ERROR;
}

Expand Down