committed by
GitHub
146 changed files with 1938 additions and 1169 deletions
@ -0,0 +1,106 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
namespace Avalonia.Utilities |
|||
{ |
|||
/// <summary>
|
|||
/// A task-like operation that is guaranteed to finish continuations synchronously,
|
|||
/// can be used for parametrized one-shot events
|
|||
/// </summary>
|
|||
public struct SynchronousCompletionAsyncResult<T> : INotifyCompletion |
|||
{ |
|||
private readonly SynchronousCompletionAsyncResultSource<T> _source; |
|||
private readonly T _result; |
|||
private readonly bool _isValid; |
|||
internal SynchronousCompletionAsyncResult(SynchronousCompletionAsyncResultSource<T> source) |
|||
{ |
|||
_source = source; |
|||
_result = default; |
|||
_isValid = true; |
|||
} |
|||
|
|||
public SynchronousCompletionAsyncResult(T result) |
|||
{ |
|||
_result = result; |
|||
_source = null; |
|||
_isValid = true; |
|||
} |
|||
|
|||
static void ThrowNotInitialized() => |
|||
throw new InvalidOperationException("This SynchronousCompletionAsyncResult was not initialized"); |
|||
|
|||
public bool IsCompleted |
|||
{ |
|||
get |
|||
{ |
|||
if (!_isValid) |
|||
ThrowNotInitialized(); |
|||
return _source == null || _source.IsCompleted; |
|||
} |
|||
} |
|||
|
|||
public T GetResult() |
|||
{ |
|||
if (!_isValid) |
|||
ThrowNotInitialized(); |
|||
return _source == null ? _result : _source.Result; |
|||
} |
|||
|
|||
|
|||
public void OnCompleted(Action continuation) |
|||
{ |
|||
if (!_isValid) |
|||
ThrowNotInitialized(); |
|||
if (_source == null) |
|||
continuation(); |
|||
else |
|||
_source.OnCompleted(continuation); |
|||
} |
|||
|
|||
public SynchronousCompletionAsyncResult<T> GetAwaiter() => this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Source for incomplete SynchronousCompletionAsyncResult
|
|||
/// </summary>
|
|||
/// <typeparam name="T"></typeparam>
|
|||
public class SynchronousCompletionAsyncResultSource<T> |
|||
{ |
|||
private T _result; |
|||
internal bool IsCompleted { get; private set; } |
|||
public SynchronousCompletionAsyncResult<T> AsyncResult => new SynchronousCompletionAsyncResult<T>(this); |
|||
|
|||
internal T Result => IsCompleted ? |
|||
_result : |
|||
throw new InvalidOperationException("Asynchronous operation is not yet completed"); |
|||
|
|||
private List<Action> _continuations; |
|||
|
|||
internal void OnCompleted(Action continuation) |
|||
{ |
|||
if(_continuations==null) |
|||
_continuations = new List<Action>(); |
|||
_continuations.Add(continuation); |
|||
} |
|||
|
|||
public void SetResult(T result) |
|||
{ |
|||
if (IsCompleted) |
|||
throw new InvalidOperationException("Asynchronous operation is already completed"); |
|||
_result = result; |
|||
IsCompleted = true; |
|||
if(_continuations!=null) |
|||
foreach (var c in _continuations) |
|||
c(); |
|||
_continuations = null; |
|||
} |
|||
|
|||
public void TrySetResult(T result) |
|||
{ |
|||
if(IsCompleted) |
|||
return; |
|||
SetResult(result); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System; |
|||
|
|||
namespace Avalonia.Controls.Templates |
|||
{ |
|||
public static class FuncTemplateNameScopeExtensions |
|||
{ |
|||
public static T RegisterInNameScope<T>(this T control, INameScope scope) |
|||
where T : StyledElement |
|||
{ |
|||
scope.Register(control.Name, control); |
|||
return control; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,87 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using Avalonia; |
|||
using Avalonia.VisualTree; |
|||
using Avalonia.Controls; |
|||
using System.Threading; |
|||
using System.Reactive.Disposables; |
|||
using System.Reactive.Subjects; |
|||
using System.Reactive.Linq; |
|||
using System.Reactive; |
|||
using ReactiveUI; |
|||
using System; |
|||
using Avalonia.Controls.ApplicationLifetimes; |
|||
using Splat; |
|||
|
|||
namespace Avalonia.ReactiveUI |
|||
{ |
|||
/// <summary>
|
|||
/// A ReactiveUI AutoSuspendHelper which initializes suspension hooks for
|
|||
/// Avalonia applications. Call its constructor in your app's composition root,
|
|||
/// before calling the RxApp.SuspensionHost.SetupDefaultSuspendResume method.
|
|||
/// </summary>
|
|||
public sealed class AutoSuspendHelper : IEnableLogger, IDisposable |
|||
{ |
|||
private readonly Subject<IDisposable> _shouldPersistState = new Subject<IDisposable>(); |
|||
private readonly Subject<Unit> _isLaunchingNew = new Subject<Unit>(); |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="AutoSuspendHelper"/> class.
|
|||
/// </summary>
|
|||
/// <param name="lifetime">Pass in the Application.ApplicationLifetime property.</param>
|
|||
public AutoSuspendHelper(IApplicationLifetime lifetime) |
|||
{ |
|||
RxApp.SuspensionHost.IsResuming = Observable.Never<Unit>(); |
|||
RxApp.SuspensionHost.IsLaunchingNew = _isLaunchingNew; |
|||
|
|||
if (lifetime is IControlledApplicationLifetime controlled) |
|||
{ |
|||
this.Log().Debug("Using IControlledApplicationLifetime events to handle app exit."); |
|||
controlled.Exit += (sender, args) => OnControlledApplicationLifetimeExit(); |
|||
RxApp.SuspensionHost.ShouldPersistState = _shouldPersistState; |
|||
} |
|||
else if (lifetime != null) |
|||
{ |
|||
var type = lifetime.GetType().FullName; |
|||
var message = $"Don't know how to detect app exit event for {type}."; |
|||
throw new NotSupportedException(message); |
|||
} |
|||
else |
|||
{ |
|||
var message = "ApplicationLifetime is null. " |
|||
+ "Ensure you are initializing AutoSuspendHelper " |
|||
+ "when Avalonia application initialization is completed."; |
|||
throw new ArgumentNullException(message); |
|||
} |
|||
|
|||
var errored = new Subject<Unit>(); |
|||
AppDomain.CurrentDomain.UnhandledException += (o, e) => errored.OnNext(Unit.Default); |
|||
RxApp.SuspensionHost.ShouldInvalidateState = errored; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Call this method in your App.OnFrameworkInitializationCompleted method.
|
|||
/// </summary>
|
|||
public void OnFrameworkInitializationCompleted() => _isLaunchingNew.OnNext(Unit.Default); |
|||
|
|||
/// <summary>
|
|||
/// Disposes internally stored observers.
|
|||
/// </summary>
|
|||
public void Dispose() |
|||
{ |
|||
_shouldPersistState.Dispose(); |
|||
_isLaunchingNew.Dispose(); |
|||
} |
|||
|
|||
private void OnControlledApplicationLifetimeExit() |
|||
{ |
|||
this.Log().Debug("Received IControlledApplicationLifetime exit event."); |
|||
var manual = new ManualResetEvent(false); |
|||
_shouldPersistState.OnNext(Disposable.Create(() => manual.Set())); |
|||
|
|||
manual.WaitOne(); |
|||
this.Log().Debug("Completed actions on IControlledApplicationLifetime exit event."); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
using System.Threading.Tasks; |
|||
using Avalonia.Utilities; |
|||
|
|||
namespace Avalonia.Controls |
|||
{ |
|||
public class ChildNameScope : INameScope |
|||
{ |
|||
private readonly INameScope _parentScope; |
|||
private readonly NameScope _inner = new NameScope(); |
|||
|
|||
public ChildNameScope(INameScope parentScope) |
|||
{ |
|||
_parentScope = parentScope; |
|||
} |
|||
|
|||
public void Register(string name, object element) => _inner.Register(name, element); |
|||
|
|||
public SynchronousCompletionAsyncResult<object> FindAsync(string name) |
|||
{ |
|||
var found = Find(name); |
|||
if (found != null) |
|||
return new SynchronousCompletionAsyncResult<object>(found); |
|||
// Not found and both current and parent scope are in completed state
|
|||
if(IsCompleted) |
|||
return new SynchronousCompletionAsyncResult<object>(null); |
|||
return DoFindAsync(name); |
|||
} |
|||
|
|||
public SynchronousCompletionAsyncResult<object> DoFindAsync(string name) |
|||
{ |
|||
var src = new SynchronousCompletionAsyncResultSource<object>(); |
|||
|
|||
void ParentSearch() |
|||
{ |
|||
var parentSearch = _parentScope.FindAsync(name); |
|||
if (parentSearch.IsCompleted) |
|||
src.SetResult(parentSearch.GetResult()); |
|||
else |
|||
parentSearch.OnCompleted(() => src.SetResult(parentSearch.GetResult())); |
|||
} |
|||
if (!_inner.IsCompleted) |
|||
{ |
|||
// Guaranteed to be incomplete at this point
|
|||
var innerSearch = _inner.FindAsync(name); |
|||
innerSearch.OnCompleted(() => |
|||
{ |
|||
var value = innerSearch.GetResult(); |
|||
if (value != null) |
|||
src.SetResult(value); |
|||
else ParentSearch(); |
|||
}); |
|||
} |
|||
else |
|||
ParentSearch(); |
|||
|
|||
return src.AsyncResult; |
|||
} |
|||
|
|||
public object Find(string name) |
|||
{ |
|||
var found = _inner.Find(name); |
|||
if (found != null) |
|||
return found; |
|||
if (_inner.IsCompleted) |
|||
return _parentScope.Find(name); |
|||
return null; |
|||
} |
|||
|
|||
public void Complete() => _inner.Complete(); |
|||
|
|||
public bool IsCompleted => _inner.IsCompleted && _parentScope.IsCompleted; |
|||
} |
|||
} |
|||
@ -0,0 +1,64 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Reactive.Disposables; |
|||
using System.Reactive.Threading.Tasks; |
|||
using System.Reflection; |
|||
using System.Threading.Tasks; |
|||
using Avalonia.LogicalTree; |
|||
using Avalonia.Reactive; |
|||
using Avalonia.Utilities; |
|||
|
|||
namespace Avalonia.Controls |
|||
{ |
|||
public class NameScopeLocator |
|||
{ |
|||
/// <summary>
|
|||
/// Tracks a named control relative to another control.
|
|||
/// </summary>
|
|||
/// <param name="relativeTo">
|
|||
/// The control relative from which the other control should be found.
|
|||
/// </param>
|
|||
/// <param name="name">The name of the control to find.</param>
|
|||
public static IObservable<object> Track(INameScope scope, string name) |
|||
{ |
|||
return new NeverEndingSynchronousCompletionAsyncResultObservable<object>(scope.FindAsync(name)); |
|||
} |
|||
|
|||
// This class is implemented in such weird way because for some reason
|
|||
// our binding system doesn't expect OnCompleted to be ever called and
|
|||
// seems to treat it as binding cancellation or something
|
|||
|
|||
private class NeverEndingSynchronousCompletionAsyncResultObservable<T> : IObservable<T> |
|||
{ |
|||
private T _value; |
|||
private SynchronousCompletionAsyncResult<T>? _asyncResult; |
|||
|
|||
public NeverEndingSynchronousCompletionAsyncResultObservable(SynchronousCompletionAsyncResult<T> task) |
|||
{ |
|||
if (task.IsCompleted) |
|||
_value = task.GetResult(); |
|||
else |
|||
_asyncResult = task; |
|||
} |
|||
|
|||
public IDisposable Subscribe(IObserver<T> observer) |
|||
{ |
|||
if (_asyncResult?.IsCompleted == true) |
|||
{ |
|||
_value = _asyncResult.Value.GetResult(); |
|||
_asyncResult = null; |
|||
} |
|||
|
|||
if (_asyncResult != null) |
|||
_asyncResult.Value.OnCompleted(() => |
|||
{ |
|||
observer.OnNext(_asyncResult.Value.GetResult()); |
|||
}); |
|||
else |
|||
observer.OnNext(_value); |
|||
|
|||
return Disposable.Empty; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1 +1 @@ |
|||
Subproject commit 894b2c02827fd5eb16a338de5d5b6c9fbc60fef5 |
|||
Subproject commit c2ec091f79fb4e1eea629bc823c9c24da7050022 |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue