Skip to content

Commit b440edd

Browse files
authored
Add Property::matches method. (linebender#1595)
This is a slightly different flavor of the existing `Property::prop_changed` method. The difference being that `matches` doesn't decide what flag must be set. This is important for more generic properties that can have varied uses. For example, one widget might use a thickness property in a way that affects layout, but for another widget it will only affect paint. This will become even more relevant with linebender#1593 introducing `Widget::pre_paint` as then we will have three different paint methods. Widgets can disagree which of the three need to re-run if `ContentColor` changes. I am guessing that it will make sense to standardize on `matches` instead of `prop_changed`, however this PR does not do that. For now I just want to introduce it so we can get some real use of it in and see how it goes. --- I'm aware that it's a slim method that barely does anything, however I think it's still a sufficient ergonomics win. `property_type == TypeId::of::<ContentColor>()` has less tab completion in it and is awkward to remember. `ContentColor::matches(property_type)` is easier to remember and can be fully auto-completed with the input parameter not even needing a tab. Whether it should be part of the `Property` trait is a question. The benefit is that it'll definitely be implemented for every property. The downside is that widgets will need to import the `Property` trait for the method to be available. I'm leaning towards the trait, because the import needs to happen only once per widget so isn't that bad. When a property is missing this method it will be a bad experience for someone who is used to it and has never directly written the `TypeId::of` turbofish.
1 parent 12e09b0 commit b440edd

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

masonry_core/src/core/properties.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub trait Property: Default + Send + Sync + 'static {
3131
///
3232
/// Ideally, when const generics are stable, we'll want to use `const Default` directly in the default impl.
3333
fn static_default() -> &'static Self;
34+
35+
/// Returns `true` if the given `property_type` matches this property.
36+
#[inline(always)]
37+
fn matches(property_type: TypeId) -> bool {
38+
property_type == TypeId::of::<Self>()
39+
}
3440
}
3541

3642
// TODO - Implement Debug.

0 commit comments

Comments
 (0)