Once you start down this path, there is no turning back…
The following code will create a Xojo picture of a circle, set it to the first item in the toolbar, which will auto adjust to the OS theme.
Dim p as picture = me.bitmapForCaching( 32, 32 )
Dim g as graphics = p.graphics
g.pensize = 2
g.drawOval 2, 2, g.width - 4, g.height - 4
#if targetMacOS then
// --- Code written by Sam Rowlands, Feb 26th 2021
// Declares extracted from the Ohanaware App Kit. https://ohanaware.com/appkit/
declare Function NSArrayObjectAtIndex lib "Foundation" selector "objectAtIndex:" ( NSArrayInstance as integer, index as UInteger ) as integer
declare Sub NSImageSetTemplate lib "AppKit" selector "setTemplate:" ( NSImageInstance as ptr, assigns value as boolean )
declare Function NSWindowToolbar lib "AppKit" selector "toolbar" ( NSWindowInstance as integer ) as integer
declare Function NSToolbarItems lib "AppKit" selector "items" ( NSToolbarInstance as integer ) as integer
declare sub NSToolbarItemSetImage lib "AppKit" selector "setImage:" ( NSToolbarItemInstance as integer, value as ptr )
// --- Get a NSImage from the Xojo picture.
Dim nsi as ptr = p.copyOSHandle( picture.handleType.MacNSImage )
// --- Mark it as a template image
NSImageSetTemplate( nsi ) = true
// --- Get the toolbar from the window, then a list of the items. If you move this code inside of a control,
// make sure you update it from "me.handle" to "me.truewindow.handle" so it gets the window.
Dim toolbarItemArray as integer = NSToolbarItems( NSWindowToolbar( me.handle ) )
// --- We extract the first item from the list and set our NSImage as it's image.
// To set the image on a different item, change "0" to the index of the item.
NSToolbarItemSetImage( NSArrayObjectAtIndex( toolbarItemArray, 0 ), nsi )