Refactored preset parsing #307
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In this PR I am refactored current preset parsing system to make it easier to use and scale.
Problems with current parsing
CodingKeysenum.ItemType,ActionType,LongActionType,GeneralParameter- initial parsingBarItemDefinition- storage for items aboveTL;DR: Current system is good enough, but probably we can do it more scalable?
Proposal
I propose to make all widgets to be based on a single base class CustomTouchBarItem which would satisfy
Decodableinterface and put all parameter parsing intoinit(from decoder: Decoder).Pros
Cons
Implementation details
All classes are inherited from a base class
CustomTouchBarItemwhich satisfyDecodable. Each widget inherited from this class overrideclass var typeIdentifierto expose its type identifier (for exampleappleScriptTitledButtonorbrightnessDown). Class also need to overrideinit(from: Decoder)and parse all custom params. To make this class be visible for the parser you need to add this class intoBarItemDefinition.typesinItemParsing.swift. Here is example how it looks like:Implementation limitations
typeIdentifierto be class var because I wanted to access it before creating class (need inItemParsing.swift). Because of this I cannot enforce that everyone has overrided it and if someone would forget about it it wouldn't be able to load his class even if it is registered.NSCustomTouchBarItem, but not so simple to inherit from some other types likeNSSliderorNSPopoverTouchBarItem. However it can be done (BrightnessViewController,GroupBarItem)super.init(from: decoder). In this case it wouldn't decode basing params like width or text. Again, no way to enforce it.Testing
While implementing I have tested all supported widgets. No regression was found. I would provide test preset later
TODO