Closing NSColorWell by code

I’d like to use NSColorWell in my apps. I copy/pasted it from macOSLib and it works as expected. That is, if/when I want to close the panel, I have to manually close it.

Actually I’d like to get rid of it when I close the window containing the NSColorWell-canvas. After searching here and there I found the following snippet:

if ([[NSColorPanel sharedColorPanel] isVisible]) {
'[[NSColorPanel sharedColorPanel] orderOut:nil];

I’d appreciate if somebody could translate it into Xojo-code.

Look for “orderOut” in Forum. E.g. Yosemite and Key Window contains some code.

I found the code, but it applies to NSWindows, while NSColorWell is a NSControl.
I guess it is safer to let things as they are.
Thank you.

Technically the NSColorWell instigates the shared instance of NSColorPanel which is a NSPanel, in turn is a sub class of NSWindow…

Yes, a pretty messy combination to unravel.

But only because you’re unfamiliar with it. I do think it’s probably about time Xojo added support for this control; I just don’t know how it would map to Windows.

The below code should do it, but I’ll confess that I haven’t really tested it.

#if TargetMacOS then declare function NSClassFromString lib "AppKit" ( className as CFStringRef ) as ptr declare function sharedColorPanel lib "AppKit" selector "sharedColorPanel" ( classRef as Ptr ) as Ptr declare sub close lib "AppKit" selector "close" ( panel as Ptr ) close( sharedColorPanel( NSClassFromString( "NSColorPanel" ) ) ) #endif

Yes, putting the code in the cancelClose event of the window containing NSColorWell(s), the panel when visible gets closed, and when the panel is not visible no problem happens.
Thank you, Sam; much appreciated.