-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Every RoutedEvent should be usable as Attached Event #15274
Merged
maxkatz6
merged 10 commits into
AvaloniaUI:master
from
workgroupengineering:features/Core/Compiler/RoutedEvent
Jun 5, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9bab952
test: Automatic RoutedEvent Handler Generation
workgroupengineering c978871
feat: Every RoutedEvent should be usable as Attached Event
workgroupengineering cccf4c9
fix: Namespace
workgroupengineering d6519c7
Merge branch 'master' into features/Core/Compiler/RoutedEvent
workgroupengineering 7802c92
fix: Address review
workgroupengineering 5332725
feat: Handle Preview event
workgroupengineering c397a57
test: Handle Preview event
workgroupengineering d0046cb
fix: Address Review
workgroupengineering 7faf680
fix: Throw On Fatal
workgroupengineering 51f942a
fix: Error Code
workgroupengineering File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
....Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlTransformRoutedEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using XamlX.Ast; | ||
using XamlX.Emit; | ||
using XamlX.IL; | ||
using XamlX.Transform; | ||
using XamlX.TypeSystem; | ||
|
||
namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; | ||
|
||
internal class AvaloniaXamlIlTransformRoutedEvent : IXamlAstTransformer | ||
{ | ||
public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) | ||
{ | ||
if (node is XamlAstNamePropertyReference prop | ||
&& prop.TargetType is XamlAstClrTypeReference targetRef | ||
&& prop.DeclaringType is XamlAstClrTypeReference declaringRef) | ||
{ | ||
var xkt = context.GetAvaloniaTypes(); | ||
var interactiveType = xkt.Interactivity.Interactive; | ||
var routedEventType = xkt.Interactivity.RoutedEvent; | ||
var AddHandlerT = xkt.Interactivity.AddHandlerT; | ||
|
||
if (interactiveType.IsAssignableFrom(targetRef.Type)) | ||
{ | ||
var eventName = $"{prop.Name}Event"; | ||
if (declaringRef.Type.GetAllFields().FirstOrDefault(f => f.IsStatic && f.Name == eventName) is { } eventField) | ||
{ | ||
if (routedEventType.IsAssignableFrom(eventField.FieldType)) | ||
{ | ||
var instance = new XamlAstClrProperty(prop | ||
, prop.Name | ||
, targetRef.Type | ||
, null | ||
); | ||
instance.Setters.Add(new XamlDirectCallAddHandler(eventField, | ||
targetRef.Type, | ||
xkt.Interactivity.AddHandler, | ||
xkt.Interactivity.RoutedEventHandler | ||
) | ||
); | ||
if (eventField.FieldType.GenericArguments?.Count == 1) | ||
{ | ||
var agrument = eventField.FieldType.GenericArguments[0]; | ||
if (!agrument.Equals(xkt.Interactivity.RoutedEventArgs)) | ||
{ | ||
instance.Setters.Add(new XamlDirectCallAddHandler(eventField, | ||
targetRef.Type, | ||
xkt.Interactivity.AddHandlerT.MakeGenericMethod([agrument]), | ||
xkt.EventHandlerT.MakeGenericType(agrument) | ||
) | ||
); | ||
} | ||
} | ||
return instance; | ||
} | ||
else | ||
{ | ||
context.ReportDiagnostic(new XamlX.XamlDiagnostic( | ||
AvaloniaXamlDiagnosticCodes.TransformError, | ||
XamlX.XamlDiagnosticSeverity.Error, | ||
$"Event definition {prop.Name} found, but its type {eventField.FieldType.GetFqn()} is not compatible with RoutedEvent.", | ||
node)); | ||
} | ||
} | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
private sealed class XamlDirectCallAddHandler : IXamlILOptimizedEmitablePropertySetter | ||
{ | ||
private readonly IXamlField _eventField; | ||
private readonly IXamlType _declaringType; | ||
private readonly IXamlMethod _addMethod; | ||
|
||
public XamlDirectCallAddHandler(IXamlField eventField, | ||
IXamlType declaringType, | ||
IXamlMethod addMethod, | ||
IXamlType routedEventHandler | ||
) | ||
{ | ||
Parameters = [routedEventHandler]; | ||
_eventField = eventField; | ||
_declaringType = declaringType; | ||
_addMethod = addMethod; | ||
} | ||
|
||
public IXamlType TargetType => _declaringType; | ||
public PropertySetterBinderParameters BinderParameters { get; } = new PropertySetterBinderParameters(); | ||
public IReadOnlyList<IXamlType> Parameters { get; } | ||
|
||
public IReadOnlyList<IXamlCustomAttribute> CustomAttributes => []; | ||
|
||
public void Emit(IXamlILEmitter emitter) | ||
=> emitter.EmitCall(_addMethod, true); | ||
|
||
public void EmitWithArguments(XamlEmitContextWithLocals<IXamlILEmitter, XamlILNodeEmitResult> context, | ||
IXamlILEmitter emitter, | ||
IReadOnlyList<IXamlAstValueNode> arguments) | ||
{ | ||
|
||
using (var loc = emitter.LocalsPool.GetLocal(_declaringType)) | ||
emitter | ||
.Ldloc(loc.Local); | ||
|
||
emitter.Ldfld(_eventField); | ||
|
||
for (var i = 0; i < arguments.Count; ++i) | ||
context.Emit(arguments[i], emitter, Parameters[i]); | ||
|
||
emitter.Ldc_I4(5); | ||
emitter.Ldc_I4(0); | ||
|
||
emitter.EmitCall(_addMethod, true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has the same problem, as in my EventSetter PR, since event name might not necessary match field name+"event".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should give ourselves a metric. In the transaction period, an analyzer could be created that signals the lack of convection as happens for avalonia properties.