Style Registry¶
The Style registry is an optional component in DrawKit that helps an application manage styles that it reuses across different documents. If your application is relatively simple this component is probably of little interest, but something like a GIS application is much more likely to use it, where many documents are likely to contain identical styles. The purpose of the registry (the DKStyleRegistry class) is to store styles in a persistent way so that they can be reused efficiently, and to manage changes to the styles even if used in multiple documents. As a convenience it also stores styles in named categories and has methods to help support a user interface.
Styles can come from several sources - the user can create them on the fly as they use an application, they can be read in with existing documents, read in from user defaults or external files, or generated by code. The style registry aims to consolidate all of these sources into one coherent repository for styles. A user typically identifies a style by its name, but this is just a convenience for user interfaces - internally a style is identified strongly by its uniqueKey. This is a UUID string that the registry uses to say whether a style is the same as another one - they are if their uniqueKey values match, even if their actual content might be quite different.
A style’s uniqueKey (or ID) is set once for all time the first time the style is created. Even if the style is subsequently edited, this key never changes. When a style is registered (and remember, you are not obliged to register any style, or use the registry at all) it is stored against this key. The style’s name doesn’t come into it (though names are automatically disambiguated when registered, for the user’s convenience). Registered styles should generally avoid being edited, and so the registry locks styles that are added to it by default.
When a style is read in from an external source and it is flagged as a registered style, the style needs to be reconsolidated with the current registry. The same style may have changed since the document was saved for example. What should be done? Well, it depends on the application. Some applications might want to keep documents up to date with the latest version of the style, others might want the document’s styles to prevail, other might want to maintain them as two separate styles... there is no one rule that applies to all. Thus the registry provides a mechanism for handling this, but the application itself must decide what to do. DKDrawingDocument provides a default template for doing this, that can be overridden or reimplemented in your application as required.
Given a set of styles read in that are flagged as previously registered, you can ask the registry to test each one and determine whether the style is known, and if so whether it is newer, the same or older than the registered version. You can then reconsolidate the styles as you need to based on this information - and if as a result styles in the file need to be updated, a simple call will do this for you. All this happens as a document or other file is opened.
The registry can build a menu of styles divided into categories if you wish, this can be handy to help support a user interface. Each style contributes an icon-sized swatch to the menu.