-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Every RoutedEvent should be usable as Attached Event
- Loading branch information
1 parent
9adf6f2
commit 3f82e64
Showing
3 changed files
with
164 additions
and
3 deletions.
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
113 changes: 113 additions & 0 deletions
113
....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,113 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; | ||
using XamlX.Ast; | ||
using XamlX.Emit; | ||
using XamlX.IL; | ||
using XamlX.Transform; | ||
using XamlX.TypeSystem; | ||
|
||
namespace Avalonia.Markup.Xaml.Loader.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(new[] { agrument }), | ||
context.Configuration.TypeSystem.FindType("System.EventHandler`1").MakeGenericType(agrument) | ||
) | ||
); | ||
} | ||
} | ||
return instance; | ||
} | ||
else | ||
{ | ||
//TODO: Throw Exception? | ||
} | ||
} | ||
} | ||
} | ||
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 = new[] { routedEventHandler }; | ||
_eventField = eventField; | ||
_declaringType = declaringType; | ||
_addMethod = addMethod; | ||
} | ||
|
||
public IXamlType TargetType => _declaringType; | ||
public PropertySetterBinderParameters BinderParameters { get; } = new PropertySetterBinderParameters(); | ||
public IReadOnlyList<IXamlType> Parameters { get; } | ||
|
||
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