It is and it’s used to determine what kind of object to listen to.
I think I’ve read somewhere that SU allows observers to not inherit from the observer classes except for one rare case where it is used to distinguish observers with similar callbacks, but I can’t remember where. Also the API docs (currently) says observers should be inherited from the API observer classes, so I think it’s safest to adhere to that.
It is, and it’s limited to objects there are only one of in each model (pages, selection, model). This was my solution to knowing what object to listen to.
It would be nice though to let the same “observer” observer multiple things. For instance, typically when listening to selection change you also want to listen to model change too, as it swaps the relevant selection.
Maybe there could be an additional argument for observer class(es), and the wrapper could create new observer objects on the fly and delegate them to the “observer” you supply in the first argument. This would allow a single object to function as multiple observers and reduce a lot of boilerplate.
class MyObservantClass
def onOpenModel(model); end
def onSelectionBulkChange(selection); end
# etc...
end
PersistentNotifier.add_observer(
MyObservantClass.new,
[Sketchup::ModelObserver, Sketchup::SelectionObserver]
)
I’ve thought a bit about passing passing a block that is called e.g. when a model is loaded and that the return value from is used to specify what to observer, but I’m not sure how to design it. Maybe something like this?
PersistentNotifier.add_observer(
MyObservantClass.new,
Sketchup::EntitiesObserver,
[
[Sketchup::ModelObserver, [:onOpenModel, :onOpenModel, :onActivePathChanged], Proc.new { |m| m.active_entities } ]
]
)