Trying to add icons to desktopPopUpMenu rows - but its not displaying the icon.
Example - adding this to the mouseEnter on a desktopPopUpMenu - but the result only shows the text not the icon.
Sub MouseEnter() Handles MouseEnter
//re-build menu
Me.RemoveAllRows
Var allClassifs() As String = Array("this", "that", "the other")
For Each classif As String In allClassifs
Var submenu As New desktopMenuItem(classif)
Var icon As new picture(10,10)
icon.graphics.DrawingColor = Color.red
icon.graphics.FillOval(0,0,10,10)
submenu.Icon = icon
Me.AddRow(submenu)
Next
End Sub
Replying to my own question. I think I’ve come across this before and it’s because for some reason the icon goes out of scope on popping the control. The returned selectedItem shows the icon as nil. I thought the solution was to subclass the control and add an array that keeps the menuItems in scope. But whilst this shows the menuItems keeping the icon in scope, the image is still not displayed in the drop-down.
If memory serves, it’s because DesktopPopupMenus don’t actually support icons, but the DesktopPopupMenu reuses the DesktopMenuItem class which was originally meant for actual menus (menubar, contextmenu) where icons are supported. There a few properties like that. Even if it did retain the value then it still would have no effect.
In the end I used some MBS routines (actually I think I’ve been here before). Resultant routine:
Private Sub rebuild_menu(pu as DesktopPopupMenu)
Var allClassifs() As String = Array("this", "that", "the other")
Var allColors() As Color = Array(Color.red, Color.green, Color.blue)
pu.RemoveAllRows
//get NS button
var popup As NSPopUpButtonMBS = pu.NSPopUpButtonMBS
var menu As NSMenuMBS = popup.menu
For i As Integer = 0 To allClassifs.LastIndex
Var submenu As New desktopMenuItem(allClassifs(i))
pu.AddRow(submenu)
//get NS menuitem
Var nsSub As NSMenuItemMBS = menu.item(i)
Var icon As New picture(10,10)
icon.graphics.DrawingColor = allColors(i)
icon.graphics.FillOval(0,0,10,10)
//add picture as nsImage
nsSub.image = New nsImageMBS(icon)
Next
End Sub
But of course, it needs a few more tweaks to work with both retina and non-retina displays. See MBS Example Popupmenu Retina Icons.