Merge pull request #587 from irihitech/splash

Add Splash Window
This commit is contained in:
Zhang Dian
2025-02-25 00:09:39 +08:00
committed by GitHub
13 changed files with 345 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
@@ -21,10 +22,7 @@ public partial class App : Application
// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
BindingPlugins.DataValidators.RemoveAt(0);
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
};
desktop.MainWindow = new MainSplashWindow();
}
base.OnFrameworkInitializationCompleted();

View File

@@ -0,0 +1,12 @@
<u:SplashWindow xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
mc:Ignorable="d" d:DesignWidth="800"
d:DesignHeight="450"
CountDown="0:0:2"
x:Class="Sandbox.Views.MainSplashWindow"
Title="MainSplashWindow">
Welcome to Avalonia Splash Window!
</u:SplashWindow>

View File

@@ -0,0 +1,27 @@
using System;
using System.Threading.Tasks;
using System.Timers;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using Sandbox.ViewModels;
using Ursa.Controls;
namespace Sandbox.Views;
public partial class MainSplashWindow : SplashWindow
{
public MainSplashWindow()
{
InitializeComponent();
}
protected override async Task<Window> CreateNextWindow()
{
return new MainWindow()
{
DataContext = new MainWindowViewModel()
};
}
}

View File

@@ -18,9 +18,9 @@ public partial class App : Application
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow()
desktop.MainWindow = new MvvmSplashWindow()
{
DataContext = new MainViewViewModel(),
DataContext = new SplashViewModel()
};
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleView)

View File

@@ -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(20), DispatcherPriority.Default);
}
private bool OnUpdate()
{
Progress += 10 * _r.NextDouble();
if (Progress <= 100)
{
return true;
}
else
{
RequestClose?.Invoke(this, true);
return false;
}
}
public void Close()
{
RequestClose?.Invoke(this, false);
}
public event EventHandler<object?>? RequestClose;
}

View File

@@ -0,0 +1,43 @@
<u:SplashWindow
x:Class="Ursa.Demo.Views.MainSplashWindow"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:iri="https://irihi.tech/shared"
xmlns:u="https://irihi.tech/ursa"
Title="MainSplashWindow"
Width="400"
Height="400">
<Grid
HorizontalAlignment="Center"
VerticalAlignment="Center"
ColumnDefinitions="Auto, Auto"
RowDefinitions="Auto, Auto">
<iri:IrihiLogo
Grid.Row="0"
Grid.Column="0"
Width="64"
Margin="0,0,16,0"
Fill="{DynamicResource SemiColorText2}" />
<StackPanel Grid.Row="0" Grid.Column="1">
<TextBlock
Classes="H2"
Text="铱泓科技"
Theme="{DynamicResource TitleTextBlock}" />
<TextBlock FontWeight="Bold" Text="IRIHI Technology" />
</StackPanel>
<StackPanel
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2">
<TextBlock
Margin="0,12,0,0"
HorizontalAlignment="Center"
FontSize="14"
Text="聚焦生产力的美学进化" />
<TextBlock
HorizontalAlignment="Center"
FontSize="14"
Text="Aesthetic Evolution of Productivity" />
</StackPanel>
</Grid>
</u:SplashWindow>

View File

@@ -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 MainSplashWindow : SplashWindow
{
public MainSplashWindow()
{
InitializeComponent();
}
protected override async Task<Window?> CreateNextWindow()
{
return new MainWindow()
{
DataContext = new MainViewViewModel()
};
}
}

View File

@@ -13,6 +13,7 @@
d:DesignWidth="800"
x:CompileBindings="True"
x:DataType="viewModels:MainWindowViewModel"
WindowStartupLocation="CenterScreen"
Icon="/Assets/Ursa.ico"
IsFullScreenButtonVisible="{OnPlatform True, macOS=False}"
IsManagedResizerVisible="{OnPlatform False, Linux=True}"

View File

@@ -0,0 +1,55 @@
<u:SplashWindow
x:Class="Ursa.Demo.Views.MvvmSplashWindow"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:iri="https://irihi.tech/shared"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
xmlns:viewModels="clr-namespace:Ursa.Demo.ViewModels"
Title="MvvmSplashWindow"
Width="400"
Height="400"
x:DataType="viewModels:SplashViewModel"
CountDown="{x:Null}"
mc:Ignorable="d">
<Grid
HorizontalAlignment="Center"
VerticalAlignment="Center"
ColumnDefinitions="Auto, Auto"
RowDefinitions="Auto, Auto, Auto">
<iri:IrihiLogo
Grid.Row="0"
Grid.Column="0"
Width="64"
Margin="0,0,16,0"
Fill="{DynamicResource SemiColorText2}" />
<StackPanel Grid.Row="0" Grid.Column="1">
<TextBlock
Classes="H2"
Text="铱泓科技"
Theme="{DynamicResource TitleTextBlock}" />
<TextBlock FontWeight="Bold" Text="IRIHI Technology" />
</StackPanel>
<ProgressBar
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Margin="0,16,0,0"
Value="{Binding Progress}" />
<StackPanel
Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="2">
<TextBlock
Margin="0,12,0,0"
HorizontalAlignment="Center"
FontSize="14"
Text="聚焦生产力的美学进化" />
<TextBlock
HorizontalAlignment="Center"
FontSize="14"
Text="Aesthetic Evolution of Productivity" />
</StackPanel>
</Grid>
</u:SplashWindow>

View File

@@ -0,0 +1,28 @@
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<Window?> CreateNextWindow()
{
if (this.DialogResult is true)
{
return new MainWindow()
{
DataContext = new MainViewViewModel()
};
}
return null;
}
}

View File

@@ -0,0 +1,26 @@
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa">
<ControlTheme x:Key="{x:Type u:SplashWindow}" TargetType="u:SplashWindow">
<Setter Property="Background" Value="{DynamicResource WindowDefaultBackground}" />
<Setter Property="TransparencyBackgroundFallback" Value="{DynamicResource WindowDefaultBackground}" />
<Setter Property="Foreground" Value="{DynamicResource WindowDefaultForeground}" />
<Setter Property="FontSize" Value="{DynamicResource DefaultFontSize}" />
<Setter Property="FontFamily" Value="{DynamicResource DefaultFontFamily}" />
<Setter Property="ExtendClientAreaChromeHints" Value="NoChrome" />
<Setter Property="ExtendClientAreaTitleBarHeightHint" Value="0" />
<Setter Property="ExtendClientAreaToDecorationsHint" Value="True" />
<Setter Property="WindowStartupLocation" Value="CenterScreen" />
<Setter Property="SystemDecorations">
<OnPlatform>
<On Options="Default, Windows, macOS">
<SystemDecorations>Full</SystemDecorations>
</On>
<On Options="Linux">
<SystemDecorations>None</SystemDecorations>
</On>
</OnPlatform>
</Setter>
</ControlTheme>
</ResourceDictionary>

View File

@@ -56,5 +56,6 @@
<ResourceInclude Source="UrsaWindow.axaml"/>
<ResourceInclude Source="PinCode.axaml" />
<ResourceInclude Source="PathPicker.axaml"/>
<ResourceInclude Source="SplashWindow.axaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

View File

@@ -0,0 +1,86 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Irihi.Avalonia.Shared.Contracts;
namespace Ursa.Controls;
public abstract class SplashWindow: Window
{
protected override Type StyleKeyOverride => typeof(SplashWindow);
public static readonly StyledProperty<TimeSpan?> CountDownProperty = AvaloniaProperty.Register<SplashWindow, TimeSpan?>(
nameof(CountDown));
public TimeSpan? CountDown
{
get => GetValue(CountDownProperty);
set => SetValue(CountDownProperty, value);
}
static SplashWindow()
{
DataContextProperty.Changed.AddClassHandler<SplashWindow, object?>((window, e) =>
window.OnDataContextChange(e));
}
private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> 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)
{
DialogResult = args;
Close();
}
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
if (CountDown != null && CountDown != TimeSpan.Zero)
{
DispatcherTimer.RunOnce(Close, CountDown.Value);
}
}
protected object? DialogResult { get; private set; }
protected virtual Task<bool> CanClose() => Task.FromResult(true);
protected abstract Task<Window?> CreateNextWindow();
private bool _canClose;
protected override sealed async void OnClosing(WindowClosingEventArgs e)
{
VerifyAccess();
if (!_canClose)
{
e.Cancel = true;
_canClose = await CanClose();
if (_canClose)
{
var nextWindow = await CreateNextWindow();
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime && nextWindow is not null)
{
lifetime.MainWindow = nextWindow;
}
nextWindow?.Show();
Close();
if (DataContext is IDialogContext idc)
{
// unregister in advance in case developer try to raise event again.
idc.RequestClose -= OnContextRequestClose;
idc.Close();
}
return;
}
}
base.OnClosing(e);
}
}