using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using LanMountainDesktop.Launcher.Services;
namespace LanMountainDesktop.Launcher.Views;
///
/// 启动画面窗口 - 简洁设计
///
public partial class SplashWindow : Window, ISplashStageReporter
{
public SplashWindow()
{
AvaloniaXamlLoader.Load(this);
}
///
/// 更新进度和状态
///
public void Report(string stage, string message)
{
Dispatcher.UIThread.Post(() =>
{
var statusText = this.GetControl("StatusText");
var progressIndicator = this.GetControl("ProgressIndicator");
// 更新状态文本
statusText.Text = message;
// 根据阶段更新进度
var progress = ResolveProgress(stage);
if (progress > 0)
{
progressIndicator.IsIndeterminate = false;
progressIndicator.Value = progress;
}
else
{
progressIndicator.IsIndeterminate = true;
}
});
}
///
/// 更新进度(0-100)
///
public void UpdateProgress(int percent, string? message = null)
{
Dispatcher.UIThread.Post(() =>
{
var statusText = this.GetControl("StatusText");
var progressIndicator = this.GetControl("ProgressIndicator");
if (!string.IsNullOrEmpty(message))
{
statusText.Text = message;
}
progressIndicator.IsIndeterminate = false;
progressIndicator.Value = Math.Clamp(percent, 0, 100);
});
}
///
/// 更新状态文本
///
public void UpdateStatus(string message)
{
Dispatcher.UIThread.Post(() =>
{
var statusText = this.GetControl("StatusText");
statusText.Text = message;
});
}
///
/// 根据阶段名称解析进度值
///
private static int ResolveProgress(string stage)
{
return stage.ToLowerInvariant() switch
{
"initializing" => 10,
"update" => 30,
"plugins" => 50,
"launch" => 70,
"ready" => 100,
_ => 0
};
}
}