juce::Component user data


#1

I was wondering what is the correct way to add user data to juce components, akin to data- attributes in HTML. The documentation is lacking in terms of what setComponentId(), setName() and properties() are intended to be.

Cheers,
Ig


#2

getProperties which returns either a constant reference or mutable reference to a NamedValueSet. However you might want to consider if you really want to be doing that instead of doing custom subclasses which contain the custom data. (Because NamedValueSet and the JUCE var variant objects have overhead.) What is the intended use case?

compo.getProperties().set("myinfo", "this is my custom information");
var d = compo.getProperties()["myinfo"];

#3

The use case is one of the most trivial: since JUCE uses listeners instead of callbacks, the usual way to know on a listener event what component was triggered is pointer comparison. That works fine in most cases. As soon as you have a large, varying number of, say, buttons, you would prefer a better way to map the component to an action than pointer comparison.

If JUCE used callbacks, one could easily capture in lambdas the id of the component. Having user defined tags for component I don’t think is crazy, it’s very useful in HTML. Of course I am not asking for changes to JUCE, just wondering what were the common patterns. Inheritance seems like overkill for this use case.

When I saw properties I thought it was the right solution but then I saw JUCE is using the same map internally, which gave me pause.

I do recommend expanding on the documentation for ids, names and properties. “Returns the name of the component” is not a useful explanation for getName(), but we are all guilty of this :smile:.

Thanks!

Edited for typos


#4

Well, if you just want to know the name of the Component, you can use getName, but the name has to have been set too of course. (With setName or the constructor argument that is available with some components.)


#5

Certain components also have public std::function members that you can set, such as onClick for buttons:

https://docs.juce.com/master/classButton.html#a30b76ab312dc7f66e67596ae20540ec2