Skip to content

Typed StyleProp StyleProperty Keys - #7252

Open
Aeshus wants to merge 8 commits into
space-wizards:masterfrom
Aeshus:style-prop
Open

Aeshus wants to merge 8 commits into
space-wizards:masterfrom
Aeshus:style-prop

Conversation

@Aeshus

@Aeshus Aeshus commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

About the PR

The way that StyleProperties are currently set/read is based purely on string keys mapped to arbitrary objects. This means that there's no way to really confirm that the value being set in the E().Prop("Key", 10) is correct at compile-time when the Control is trying to read TryGetStyleProperty<Color>("Key", out _).

This helps ensure that stylesheets being written are more accurate.

(Big diff is mostly formatting/fixing up tests + fixing obsolete usages. Fildrance asked me revive up my old attempt and finally get that merged.)

Technical details

A new StyleProp<T> record struct that's just a wrapper around a string Name. The type is then used as a key for all of the StyleProperty-related functions.

Note I do not provide a new manual constructor for new StyleProperty(StyleProp<T>, object?) as it is functionally obsolete when we have MutableSelectors, and it would try to unnecessarily box values.

Generics are fun and the T type is possibly null, which necessitated a fix to how null values were read from style properties. There were already Controls attempting to read nullable values (such as the many ShaderInstance? properties), but they couldn't actually read them as the code would try do val is T and it be treated as missing and the Try would skip saving it. This meant that a null value used to essentially mean "ignore" (so it could hide lesser-matching rules), but is problematic for caching and stylesheet updates.

Release Notes

  • New feature: StyleProp<T> struct that is a type-safe wrapper around style property keys.
  • Breaking change: TryGetStyleProperty and StylePropertyDefault throw in DEBUG if there there is an incompatible value stored at the key.
  • Breaking change: TryGetStyleProperty return true for a successful lookup with a null value, even for non-nullable reference types given it was set to null in the stylesheet.
  • Breaking change: StylePropertyDefault return null for a successful lookup rather than the defaultValue, even for non-nullable reference types given it was set to null in the stylesheet.

Migrations

All of the Controls are now using StyleProp{T}. This should be source-compatible and require no changes if you were using one of the old constant StyleProperty* keys and weren't storing the wrong types.

To handle most of the Obsoletions with the old string-based API, check the Control and use the specific StyleProp{T} for the property you are trying to set.

It's possible to now get DEBUG crashes if you're storing a type for a StyleProp{T}.Name that is being read by a different-typed StyleProp{T1}. We don't currently have much homogeneity around our key names and type associations, so we would generally recommend to instead scope your StyleProperties for your controls. (They may be coalesced together at some point in the future).

Usage of the new StyleProperty(string, object?) constructor is now obsolete. Please use the StylesheetHelpers MutableSelectors instead to build your stylesheet rules, which are able to provide type safety.

Tried to keep it more minimal this time. Also threw in a debug assert to
scream at content when it screws up.
I noticed that we have a bunch of Controls that want to take nullable
values, but the old version on master just wouldn't take a nullable
object at build time.

BUT, due to the C# compiler screaming, I now have a bunch of object?
types lying around, which now means you can pass it into .Prop. To
handle actually being able to pass nullable values in and the fact we've
already been wanting to read nullable values, I now check if it's
nullable on top of checking if it can be cast
@Aeshus
Aeshus requested a review from DrSmugleaf as a code owner November 27, 2026 20:03
@Aeshus Aeshus added T: Bugfix Type: Bugs and/or bugfixes T: New Feature Type: New feature A: UI Things like game screens, menus, and windows. labels Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A: UI Things like game screens, menus, and windows. T: Bugfix Type: Bugs and/or bugfixes T: New Feature Type: New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant