-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
676 additions
and
402 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
using Autofac; | ||
|
||
using Avalonia; | ||
using Avalonia.Controls.Templates; | ||
|
||
using Movere.Services; | ||
using Movere.ViewModels; | ||
|
||
namespace Movere.Avalonia.Services | ||
{ | ||
public abstract class DialogHostBase : IMovereDialogHost | ||
{ | ||
private readonly IContainer _container; | ||
|
||
private protected DialogHostBase(Application application, IDataTemplate? dataTemplate = null) | ||
{ | ||
var containerBuilder = new ContainerBuilder() | ||
{ | ||
Properties = | ||
{ | ||
["Application"] = application | ||
} | ||
}; | ||
|
||
containerBuilder | ||
.RegisterAssemblyModules(typeof(WindowDialogHost).Assembly); | ||
|
||
containerBuilder | ||
.RegisterInstance<IDialogHost>(this); | ||
|
||
_container = containerBuilder.Build(); | ||
|
||
var resolver = _container.Resolve<ViewResolver>(); | ||
|
||
DataTemplate = dataTemplate is null | ||
? resolver | ||
: new FuncDataTemplate( | ||
x => dataTemplate.Match(x) || resolver.Match(x), | ||
(x, _) => dataTemplate.Match(x) ? dataTemplate.Build(x) : resolver.Build(x) | ||
); | ||
} | ||
|
||
protected IDataTemplate DataTemplate { get; } | ||
|
||
IContainer IMovereDialogHost.Container => | ||
_container; | ||
|
||
public abstract IObservable<TResult> ShowDialog<TContent, TResult>( | ||
Func<IDialogView<TResult>, IDialogWindowViewModel<TContent>> viewModelFactory | ||
); | ||
|
||
public ValueTask DisposeAsync() => | ||
_container.DisposeAsync(); | ||
} | ||
} |
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,74 @@ | ||
using System; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
using System.Reactive.Subjects; | ||
|
||
using Avalonia; | ||
using Avalonia.Controls.Primitives; | ||
using Avalonia.Controls.Templates; | ||
|
||
using Movere.Services; | ||
using Movere.ViewModels; | ||
using Movere.Views; | ||
|
||
namespace Movere.Avalonia.Services | ||
{ | ||
internal sealed class OverlayDialogHost(Application application, Visual target, IDataTemplate? dataTemplate = null) | ||
: DialogHostBase(application, dataTemplate) | ||
{ | ||
private sealed class DialogView<TResult> : IDialogView<TResult> | ||
{ | ||
private readonly OverlayLayer _layer; | ||
private readonly DialogOverlay _overlay; | ||
|
||
private readonly ISubject<TResult> _resultSubject = new Subject<TResult>(); | ||
|
||
public DialogView(OverlayLayer layer, DialogOverlay overlay) | ||
{ | ||
_layer = layer; | ||
_overlay = overlay; | ||
|
||
Result = _resultSubject.AsObservable(); | ||
} | ||
|
||
public IObservable<TResult> Result { get; } | ||
|
||
public void Close(TResult result) | ||
{ | ||
if (_overlay.OnClosing()) | ||
{ | ||
_layer.Children.Remove(_overlay); | ||
|
||
_resultSubject.OnNext(result); | ||
_resultSubject.OnCompleted(); | ||
} | ||
} | ||
} | ||
|
||
public override IObservable<TResult> ShowDialog<TContent, TResult>( | ||
Func<IDialogView<TResult>, IDialogWindowViewModel<TContent>> viewModelFactory | ||
) | ||
{ | ||
var layer = OverlayLayer.GetOverlayLayer(target); | ||
|
||
if (layer is null) | ||
{ | ||
return Observable.Create<TResult>( | ||
observer => | ||
{ | ||
observer.OnError(new Exception()); | ||
return Disposable.Empty; | ||
} | ||
); | ||
} | ||
|
||
var overlay = new DialogOverlay() { DataTemplates = { DataTemplate } }; | ||
var view = new DialogView<TResult>(layer, overlay); | ||
|
||
overlay.DataContext = viewModelFactory(view); | ||
layer.Children.Add(overlay); | ||
|
||
return view.Result; | ||
} | ||
} | ||
} |
Oops, something went wrong.