* Fix duplicate insertion of shared StyleInstance into the same ValueStore
StyleBase cached a single shared StyleInstance per ControlTheme and reused
it for every attach, regardless of the FrameType it was attached as and
regardless of whether it was already present in the target's ValueStore.
The same ControlTheme can legitimately be attached to one control twice
(once as its own Theme and once via the templated parent's theme), and on
re-attach / re-application of styling the cached shared instance was
inserted a second time into the same store, triggering
- Cache shared instances per FrameType (Theme / TemplatedParentTheme)
- Only reuse a shared instance if it is not already in the target store
* Cache shared style instances per FrameType; drop redundant ContainsFrame
Address review feedback on #22276:
- Replace the two shared-instance fields with a fixed-size array indexed
by FrameType, giving one cached instance per frame type (Style,
TemplatedParentTheme, Theme). InlineDictionary was considered but
requires a class type key, so an array indexed by the enum is the
equivalent zero-allocation structure.
- Remove the ContainsFrame linear scan: with one instance per frame type
a duplicate insert of the same instance into the same ValueStore is no
longer possible, because OnControlThemeChanged /
OnTemplatedParentControlThemeChanged always RemoveFrames(type) before
re-attaching. This is now proven by unit tests.
- Add unit tests:
- ControlTheme attached as both Theme and TemplatedParentTheme to the
same control creates two distinct instances.
- Re-attaching after RemoveFrames does not duplicate the instance.
- The same instance is shared between different controls for the same
frame type.
* Avoid per-style array allocation for the single shared instance case
Address review feedback: the vast majority of styles only ever have a
single shared instance, so keep a plain field + FrameType for that case
and only allocate the FrameType-indexed array lazily once a second frame
type is shared. This removes the extra array allocation added to every
style by the previous implementation.
* Address review nits: succinct comment, avoid eager GetSharedInstance call
- Shorten the shared-instance comment per the AI guidelines.
- Invert the condition so GetSharedInstance is only called when
canShareInstance is true.
* Comment shortened to two lines :)