Closing Preference Panel with ColorWell open


I just noticed some odd behavior with the ColorWell in the preference panel.

If I use the ColorWell to select a new color, and then close the preference panel without closing the ColorWell color selection window, the colorwell selection window will remain open on the desktop without a connection to a window. Is there a way to make this ColorWell selection window close with the preference panel?


Have a look at NSColorPanel. You’ll notice there’s a class method, +sharedColorPanel, and it’s a subclass of NSPanel (and NSWindow). So you can close it with -orderOut:.

NSWindow has a delegate method called -windowShouldClose:, which may be handy as well.



I did it this way:

1.) I made the File’s Owner (PreferenceController) the delegate of the preference panel
2.) I implemented -windowShouldClose for the PreferenceController:

- (void)windowShouldClose:(id)sender { BOOL cp = [NSColorPanel sharedColorPanelExists]; if (cp) { NSColorPanel *scp = [NSColorPanel sharedColorPanel]; [scp orderOut:sender]; } }
It works as expected - I hope it’s correct?! :stuck_out_tongue:



Looks good to me!