Adding an icon to PopupSwitchMenuItem

Hi , I’m trying to add an icon to a PopupSwitchMenu i created and was wondering if there was a way to do this? The code below i can create a Toggle switch with a text label “Menu Item” but i cannot figure out a way to add an icon before the text.

this.Toggle = new PopupMenu.PopupSwitchMenuItem("Menu Label", false, {});
this.menu.addMenuItem(this.Toggle);

Any help for this would be greatly appreciated.

There is no dedicated API for that, however menu items are just actors.

That is, something like the following works:

this.Toggle.insert_child_below(icon, this.Toggle.label);

But note that the reason that there is no dedicated API is that the combination of switches and labels is an alien pattern in GNOME design (both in gnome-shell and GTK/libadwaita). So you can do it, but it will be a bit weird.

Thank you. Like you said it does look a bit weird but added the reactive false parameter to the Label - Does not look that bad now.

this.menuItem = new PopupMenu.PopupImageMenuItem('Menu Label', icon, {
        reactive    : false, 
        style_class : 'style-class'
});
this.Toggle = new PopupMenu.PopupSwitchMenuItem("", false, {}); 
this.Toggle.insert_child_below(this.menuItem, null);
this.menu.addMenuItem(this.Toggle);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.