I'm trying to make a color chooser using an NSPopupButton, and at the bottom i'd like a separator and a "Custom…" option. Is it possible to load the colors from array, then tack the separator and "custom" item on bottom? if so, how?
Thanks!
I'm trying to make a color chooser using an NSPopupButton, and at the bottom i'd like a separator and a "Custom…" option. Is it possible to load the colors from array, then tack the separator and "custom" item on bottom? if so, how?
Thanks!
I have created a similar PopUpButton, but am not yet binding the default colors to a fixed array (though I am working on that now). there are two appraoches here - both may be considered hacks by purists, but they do get the job done.
Subclass the NSPopUpButtonCell and override attachPopUpWithFrame to add your own menu items. I have not tried this alongside bound items.
Hard-code the 'Custom ..' object in your array, and have the action of the displayed color panel, add a new item to the array.
This is possible with bindings as of Mac OS X 10.5 using the "content placement" tag; see my answer here:
Not using Bindings, no. You could do the “Custom…” one easily enough, but not the separator.
Why not use an NSColorWell, anyway?
You should really use a NSColorWell instead of hand-crafting your own. One of the reasons why Apple has superior GUIs than other platforms (especially Linux) is because developers use the standard components for doing this kind of thing. Arguments like 'because I think it makes the interface more concise' are the reasons why GIMP is such a prime example of how not to design a GUI.
That said, basically what you're trying to do is define a dynamic menu, rather than a fixed-size list (as one might do in InterfaceBuilder). You can do this via the NSMenu and NSMenuItem classes.
What you'd need to do, is rather than display the menu on-demand, is fill it when the application starts up with the default array. Then, when the array changes (via your model objects) trigger the re-creation of the menu. Alternatively, trap the menu with the menuNeedsUpdate: message.
NSPopupButton
as usualNSPopupButton
, set the Content Placement Tag
value to 1
NSPopupButton
Make sure you have two items in your menu:
Optional: Add a Separator Menu Item between dynamic and static content:
ViewController
as delegate for the NSMenuMake your your ViewController
a NSMenuDelegate
and add the following code in your ViewController
:
extension YourViewController: NSMenuDelegate {
func menuNeedsUpdate(_ menu: NSMenu) {
menu.insertItem(NSMenuItem.separator(), at: menu.numberOfItems-1)
}
}
which will simply properly insert a separator between your dynamic content and your static "Custom..." menu item