Skip to content
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

WIP: Typed bindings #5510

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Avalonia.Base/AvaloniaObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,44 @@ public static IDisposable Bind<T>(
priority);
}

/// <summary>
/// Binds a property on an <see cref="IAvaloniaObject"/> to an <see cref="IBinding{T}"/>.
/// </summary>
/// <param name="target">The object.</param>
/// <param name="property">The property to bind.</param>
/// <param name="binding">The binding.</param>
/// <returns>An <see cref="IDisposable"/> which can be used to cancel the binding.</returns>
public static IDisposable Bind<T>(
this IAvaloniaObject target,
StyledPropertyBase<T> property,
IBinding<T> binding)
{
target = target ?? throw new ArgumentNullException(nameof(target));
property = property ?? throw new ArgumentNullException(nameof(property));
binding = binding ?? throw new ArgumentNullException(nameof(binding));

return binding.Bind(target, property);
}

/// <summary>
/// Binds a property on an <see cref="IAvaloniaObject"/> to an <see cref="IBinding{T}"/>.
/// </summary>
/// <param name="target">The object.</param>
/// <param name="property">The property to bind.</param>
/// <param name="binding">The binding.</param>
/// <returns>An <see cref="IDisposable"/> which can be used to cancel the binding.</returns>
public static IDisposable Bind<T>(
this IAvaloniaObject target,
DirectPropertyBase<T> property,
IBinding<T> binding)
{
target = target ?? throw new ArgumentNullException(nameof(target));
property = property ?? throw new ArgumentNullException(nameof(property));
binding = binding ?? throw new ArgumentNullException(nameof(binding));

return binding.Bind(target, property);
}

/// <summary>
/// Binds a property on an <see cref="IAvaloniaObject"/> to an <see cref="IBinding"/>.
/// </summary>
Expand Down
82 changes: 82 additions & 0 deletions src/Avalonia.Base/Data/Core/Parsers/ExpressionChainVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

#nullable enable

namespace Avalonia.Data.Core.Parsers
{
public class ExpressionChainVisitor<TIn> : ExpressionVisitor
{
private readonly LambdaExpression _rootExpression;
private List<Func<TIn, object>> _links = new List<Func<TIn, object>>();
private Expression? _head;

public ExpressionChainVisitor(LambdaExpression expression)
{
_rootExpression = expression;
}

public static Func<TIn, object>[] Build<TOut>(Expression<Func<TIn, TOut>> expression)
{
var visitor = new ExpressionChainVisitor<TIn>(expression);
visitor.Visit(expression);
return visitor._links.ToArray();
}

protected override Expression VisitBinary(BinaryExpression node)
{
var result = base.VisitBinary(node);
if (node.Left == _head)
_head = node;
return result;
}

protected override Expression VisitMember(MemberExpression node)
{
var result = base.VisitMember(node);

if (node.Expression is object &&
node.Expression == _head &&
node.Expression.Type.IsValueType == false)
{
var link = Expression.Lambda<Func<TIn, object>>(node.Expression, _rootExpression.Parameters);
_links.Add(link.Compile());
_head = node;
}

return result;
}

protected override Expression VisitMethodCall(MethodCallExpression node)
{
var result = base.VisitMethodCall(node);

if (node.Object is object &&
node.Object == _head &&
node.Type.IsValueType == false)
{
var link = Expression.Lambda<Func<TIn, object>>(node.Object, _rootExpression.Parameters);
_links.Add(link.Compile());
_head = node;
}

return result;
}

protected override Expression VisitParameter(ParameterExpression node)
{
if (node == _rootExpression.Parameters[0])
_head = node;
return base.VisitParameter(node);
}

protected override Expression VisitUnary(UnaryExpression node)
{
var result = base.VisitUnary(node);
if (node.Operand == _head)
_head = node;
return result;
}
}
}
144 changes: 144 additions & 0 deletions src/Avalonia.Base/Data/Core/TypedBindingExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using System;
using System.Linq.Expressions;
using Avalonia.Data.Core.Parsers;

#nullable enable

namespace Avalonia.Data.Core
{
/// <summary>
/// Provides factory methods for creating <see cref="TypedBindingExpression{TIn, TOut}"/>
/// objects from C# lambda expressions.
/// </summary>
public static class TypedBindingExpression
{
public static TypedBindingExpression<TIn, TOut> OneWay<TIn, TOut>(
TIn root,
Expression<Func<TIn, TOut>> read,
Optional<TOut> fallbackValue = default)
where TIn : class
{
return OneWay(new Single<TIn>(root), read, fallbackValue);
}

public static TypedBindingExpression<TIn, TOut> OneWay<TIn, TOut>(
IObservable<TIn> root,
Expression<Func<TIn, TOut>> read,
Optional<TOut> fallbackValue = default)
where TIn : class
{
return new TypedBindingExpression<TIn, TOut>(
root,
read.Compile(),
null,
ExpressionChainVisitor<TIn>.Build(read),
fallbackValue);
}

public static TypedBindingExpression<TIn, TConverted> OneWay<TIn, TOut, TConverted>(
TIn root,
Expression<Func<TIn, TOut>> read,
Func<TOut, TConverted> convert,
Optional<TConverted> fallbackValue = default)
where TIn : class
{
return OneWay(new Single<TIn>(root), read, convert, fallbackValue);
}

public static TypedBindingExpression<TIn, TConverted> OneWay<TIn, TOut, TConverted>(
IObservable<TIn> root,
Expression<Func<TIn, TOut>> read,
Func<TOut, TConverted> convert,
Optional<TConverted> fallbackValue = default)
where TIn : class
{
var compiledRead = read.Compile();

return new TypedBindingExpression<TIn, TConverted>(
root,
x => convert(compiledRead(x)),
null,
ExpressionChainVisitor<TIn>.Build(read),
fallbackValue);
}

public static TypedBindingExpression<TIn, TOut> TwoWay<TIn, TOut>(
TIn root,
Expression<Func<TIn, TOut>> read,
Action<TIn, TOut> write,
Optional<TOut> fallbackValue = default)
where TIn : class
{
return TwoWay(new Single<TIn>(root), read, write, fallbackValue);
}

public static TypedBindingExpression<TIn, TOut> TwoWay<TIn, TOut>(
IObservable<TIn> root,
Expression<Func<TIn, TOut>> read,
Action<TIn, TOut> write,
Optional<TOut> fallbackValue = default)
where TIn : class
{
return new TypedBindingExpression<TIn, TOut>(
root,
read.Compile(),
write,
ExpressionChainVisitor<TIn>.Build(read),
fallbackValue);
}

public static TypedBindingExpression<TIn, TConverted> TwoWay<TIn, TOut, TConverted>(
TIn root,
Expression<Func<TIn, TOut>> read,
Action<TIn, TOut> write,
Func<TOut, TConverted> convert,
Func<TConverted, TOut> convertBack,
Optional<TConverted> fallbackValue = default)
where TIn : class
{
return TwoWay(new Single<TIn>(root), read, write, convert, convertBack, fallbackValue);
}

public static TypedBindingExpression<TIn, TConverted> TwoWay<TIn, TOut, TConverted>(
IObservable<TIn> root,
Expression<Func<TIn, TOut>> read,
Action<TIn, TOut> write,
Func<TOut, TConverted> convert,
Func<TConverted, TOut> convertBack,
Optional<TConverted> fallbackValue = default)
where TIn : class
{
var compiledRead = read.Compile();

return new TypedBindingExpression<TIn, TConverted>(
root,
x => convert(compiledRead(x)),
(o, v) => write(o, convertBack(v)),
ExpressionChainVisitor<TIn>.Build(read),
fallbackValue);
}

private class Single<T> : IObservable<T>, IDisposable where T : class
{
private WeakReference<T> _value;

public Single(T value) => _value = new WeakReference<T>(value);

public IDisposable Subscribe(IObserver<T> observer)
{
if (_value.TryGetTarget(out var value))
{
observer.OnNext(value);
}
else
{
observer.OnNext(default);
}

return this;
}

public void Dispose() { }
}
}
}
Loading