Add VisibleWhen and EnabledWhen attributes to SimpleEffectDialog#1960
Conversation
This allows attributes in SimpleEffectDialog to automatically set their enabled state/visibility based on a condition
377baba to
ff842ab
Compare
|
Note that I originally wanted to use lambdas in the attributes, but C# doesn't seem to support lambdas inside attributes. I'm also not sure if I used reflectors properly since it was my first time using them, so I'd appreciate feedback on that part 😁 |
27c67f4 to
8382d2d
Compare
|
This is great! A couple minor suggestions / thoughts:
|
This also makes CreateConditionDelegate (and EvaluateCondition) hard-fail if the proprety/method is missing
I had tested on a weak machine that changing values fast on effects using VisibleWhen doesn't cause slowdowns, but using delegates doesn't increase code complexity, so I went with it as it's an easy performance gain |
| MethodInfo? method = type.GetMethod (methodName, BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null); | ||
| if (method is not null && method.ReturnType == typeof (bool)) { | ||
| return (bool) method.Invoke (source, null)!; | ||
| return () => (bool) method.Invoke (source, null)!; |
There was a problem hiding this comment.
Thanks! This can actually go a step further - I should've mentioned this more clearly in my last comment :)
You can use MethodInfo.CreateDelegate () , which creates a strongly-typed delegate that's more like calling the method directly performance-wise
There was a problem hiding this comment.
Good to know, thanks!
I updated the implementation to use CreateDelegate
|
Looks good , thanks! |
|
Hmm @cameronwhite is right, |
|
@Matthieu-LAURENT39, @cameronwhite, from reading the pull request, I think there is not that much I could add for now. It's great performance-wise: reflection is just being used to create the delegate, not to call it. On the other hand, the method |
This adds two new attributes for use with
SimpleEffectDialog:VisibleWhenandEnabledWhen.These allow hiding or disabling a field based on a condition that will be rechecked whenever EffectData changes.
This was started for use with #1815, but other effects should be able to benefit from it.
I went through the current list of effects and retrofitted it on the existing ones, so this PR also includes that.
Example from one of the updated effects (Voronoi Diagram):