diff --git a/demo/Ursa.Demo/App.axaml.cs b/demo/Ursa.Demo/App.axaml.cs index 01d6f16..92e928f 100644 --- a/demo/Ursa.Demo/App.axaml.cs +++ b/demo/Ursa.Demo/App.axaml.cs @@ -18,7 +18,10 @@ public partial class App : Application { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - desktop.MainWindow = new MainSplashWindow(); + desktop.MainWindow = new MvvmSplashWindow() + { + DataContext = new SplashViewModel() + }; } else if (ApplicationLifetime is ISingleViewApplicationLifetime singleView) { diff --git a/demo/Ursa.Demo/ViewModels/SplashViewModel.cs b/demo/Ursa.Demo/ViewModels/SplashViewModel.cs new file mode 100644 index 0000000..c8a7178 --- /dev/null +++ b/demo/Ursa.Demo/ViewModels/SplashViewModel.cs @@ -0,0 +1,38 @@ +using System; +using Avalonia.Threading; +using CommunityToolkit.Mvvm.ComponentModel; +using Irihi.Avalonia.Shared.Contracts; + +namespace Ursa.Demo.ViewModels; + +public partial class SplashViewModel: ObservableObject, IDialogContext +{ + [ObservableProperty] private double _progress; + private Random _r = new(); + + public SplashViewModel() + { + DispatcherTimer.Run(OnUpdate, TimeSpan.FromMilliseconds(200), DispatcherPriority.Default); + } + + private bool OnUpdate() + { + Progress += 10 * _r.NextDouble(); + if (Progress <= 100) + { + return true; + } + else + { + RequestClose?.Invoke(this, null); + return false; + } + } + + public void Close() + { + RequestClose?.Invoke(this, null); + } + + public event EventHandler? RequestClose; +} \ No newline at end of file diff --git a/demo/Ursa.Demo/Views/MvvmSplashWindow.axaml b/demo/Ursa.Demo/Views/MvvmSplashWindow.axaml new file mode 100644 index 0000000..b7868e2 --- /dev/null +++ b/demo/Ursa.Demo/Views/MvvmSplashWindow.axaml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + diff --git a/demo/Ursa.Demo/Views/MvvmSplashWindow.axaml.cs b/demo/Ursa.Demo/Views/MvvmSplashWindow.axaml.cs new file mode 100644 index 0000000..c25f646 --- /dev/null +++ b/demo/Ursa.Demo/Views/MvvmSplashWindow.axaml.cs @@ -0,0 +1,24 @@ +using System.Threading.Tasks; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using Ursa.Controls; +using Ursa.Demo.ViewModels; + +namespace Ursa.Demo.Views; + +public partial class MvvmSplashWindow : SplashWindow +{ + public MvvmSplashWindow() + { + InitializeComponent(); + } + + protected override async Task CreateNextWindow() + { + return new MainWindow() + { + DataContext = new MainViewViewModel() + }; + } +} \ No newline at end of file diff --git a/src/Ursa/Windows/SplashWindow.cs b/src/Ursa/Windows/SplashWindow.cs index 064d906..b82cc7d 100644 --- a/src/Ursa/Windows/SplashWindow.cs +++ b/src/Ursa/Windows/SplashWindow.cs @@ -3,6 +3,7 @@ using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Interactivity; using Avalonia.Threading; +using Irihi.Avalonia.Shared.Contracts; namespace Ursa.Controls; @@ -18,6 +19,24 @@ public abstract class SplashWindow: Window get => GetValue(CountDownProperty); set => SetValue(CountDownProperty, value); } + + static SplashWindow() + { + DataContextProperty.Changed.AddClassHandler((window, e) => + window.OnDataContextChange(e)); + } + + private void OnDataContextChange(AvaloniaPropertyChangedEventArgs args) + { + if (args.OldValue.Value is IDialogContext oldContext) oldContext.RequestClose -= OnContextRequestClose; + + if (args.NewValue.Value is IDialogContext newContext) newContext.RequestClose += OnContextRequestClose; + } + + private void OnContextRequestClose(object? sender, object? args) + { + Close(args); + } protected override void OnLoaded(RoutedEventArgs e) { @@ -49,6 +68,11 @@ public abstract class SplashWindow: Window } nextWindow.Show(); Close(); + if (DataContext is IDialogContext idc) + { + idc.Close(); + idc.RequestClose -= OnContextRequestClose; + } return; } }