using Avalonia.Media; using Avalonia.Threading; using HeroIconsAvalonia.Enums; using AuroraDesk.ViewModels.Base; using ReactiveUI; using System; using System.Globalization; using System.Reactive; using System.Threading; using System.Threading.Tasks; namespace AuroraDesk.ViewModels.Pages; /// /// DialogHost 示例页面的 ViewModel /// public class DialogHostPageViewModel : RoutableViewModel { private bool _isDialogOpen; private string _dialogTitle = "提示"; private string _dialogMessage = "请选择需要执行的操作"; private string _lastActionMessage = string.Empty; private bool _rememberChoice; private bool _isRememberChoiceVisible = true; private IconType _dialogIcon = IconType.InformationCircle; private IBrush _dialogAccentBrush = new SolidColorBrush(Color.FromRgb(59, 130, 246)); private string _primaryButtonText = "确认"; private string _secondaryButtonText = "取消"; private bool _isPrimaryButtonVisible = true; private bool _useAutoClose; private TimeSpan _autoCloseDelay = TimeSpan.FromSeconds(2); private CancellationTokenSource? _autoCloseCts; /// /// 构造函数 /// /// 宿主 Screen public DialogHostPageViewModel(IScreen hostScreen) : base(hostScreen, "DialogHost") { ShowDialogCommand = ReactiveCommand.Create(ShowDialog); ConfirmDialogCommand = ReactiveCommand.Create(ConfirmDialog); CancelDialogCommand = ReactiveCommand.Create(CancelDialog); } /// /// 是否展示对话框 /// public bool IsDialogOpen { get => _isDialogOpen; set => this.RaiseAndSetIfChanged(ref _isDialogOpen, value); } /// /// 对话框标题 /// public string DialogTitle { get => _dialogTitle; set => this.RaiseAndSetIfChanged(ref _dialogTitle, value); } /// /// 对话框消息 /// public string DialogMessage { get => _dialogMessage; set => this.RaiseAndSetIfChanged(ref _dialogMessage, value); } /// /// 记住选择 /// public bool RememberChoice { get => _rememberChoice; set => this.RaiseAndSetIfChanged(ref _rememberChoice, value); } /// /// 最近一次操作反馈 /// public string LastActionMessage { get => _lastActionMessage; set => this.RaiseAndSetIfChanged(ref _lastActionMessage, value); } /// /// 对话框图标 /// public IconType DialogIcon { get => _dialogIcon; set => this.RaiseAndSetIfChanged(ref _dialogIcon, value); } /// /// 对话框强调色 /// public IBrush DialogAccentBrush { get => _dialogAccentBrush; set => this.RaiseAndSetIfChanged(ref _dialogAccentBrush, value); } /// /// 主操作按钮文本 /// public string PrimaryButtonText { get => _primaryButtonText; set => this.RaiseAndSetIfChanged(ref _primaryButtonText, value); } /// /// 次操作按钮文本 /// public string SecondaryButtonText { get => _secondaryButtonText; set { this.RaiseAndSetIfChanged(ref _secondaryButtonText, value); this.RaisePropertyChanged(nameof(HasSecondaryButton)); } } /// /// 是否展示次操作按钮 /// public bool HasSecondaryButton => !string.IsNullOrWhiteSpace(_secondaryButtonText); /// /// 主操作按钮是否可见 /// public bool IsPrimaryButtonVisible { get => _isPrimaryButtonVisible; set => this.RaiseAndSetIfChanged(ref _isPrimaryButtonVisible, value); } /// /// 是否展示“记住选择”选项 /// public bool IsRememberChoiceVisible { get => _isRememberChoiceVisible; set => this.RaiseAndSetIfChanged(ref _isRememberChoiceVisible, value); } /// /// 是否启用自动关闭 /// public bool UseAutoClose { get => _useAutoClose; private set => this.RaiseAndSetIfChanged(ref _useAutoClose, value); } /// /// 自动关闭延时时间 /// public TimeSpan AutoCloseDelay { get => _autoCloseDelay; private set => this.RaiseAndSetIfChanged(ref _autoCloseDelay, value); } /// /// 展示对话框命令 /// public ReactiveCommand ShowDialogCommand { get; } /// /// 确认操作命令 /// public ReactiveCommand ConfirmDialogCommand { get; } /// /// 取消操作命令 /// public ReactiveCommand CancelDialogCommand { get; } private void ShowDialog(string? parameter) { CancelAutoCloseTimer(); var (variant, delayOverride) = ParseParameter(parameter); ConfigureVariant(variant, delayOverride); RememberChoice = false; IsDialogOpen = true; LastActionMessage = string.Empty; if (UseAutoClose) { StartAutoCloseTimer(); } } private void ConfirmDialog() { CancelAutoCloseTimer(); IsDialogOpen = false; var rememberSuffix = IsRememberChoiceVisible && RememberChoice ? "(记住选择)" : string.Empty; LastActionMessage = $"已确认:{DialogTitle}{rememberSuffix}"; } private void CancelDialog() { CancelAutoCloseTimer(); if (!HasSecondaryButton) { return; } IsDialogOpen = false; var rememberSuffix = IsRememberChoiceVisible && RememberChoice ? "(记住选择)" : string.Empty; LastActionMessage = $"已取消:{DialogTitle}{rememberSuffix}"; } private void ConfigureVariant(string? variant, TimeSpan? delayOverride) { var key = string.IsNullOrWhiteSpace(variant) ? "info" : variant.Trim().ToLowerInvariant(); UseAutoClose = false; AutoCloseDelay = delayOverride ?? TimeSpan.FromSeconds(2); IsPrimaryButtonVisible = true; SecondaryButtonText = "取消"; PrimaryButtonText = "确认"; IsRememberChoiceVisible = true; switch (key) { case "success": DialogTitle = "操作成功"; DialogMessage = "所有任务都已顺利完成。您可以继续后续流程。"; DialogIcon = IconType.CheckCircle; DialogAccentBrush = new SolidColorBrush(Color.FromRgb(34, 197, 94)); PrimaryButtonText = string.Empty; SecondaryButtonText = string.Empty; IsRememberChoiceVisible = false; IsPrimaryButtonVisible = false; UseAutoClose = true; break; case "error": case "failure": DialogTitle = "操作失败"; DialogMessage = "执行过程中出现错误,请检查配置或稍后重试。"; DialogIcon = IconType.XCircle; DialogAccentBrush = new SolidColorBrush(Color.FromRgb(239, 68, 68)); PrimaryButtonText = string.Empty; SecondaryButtonText = string.Empty; IsRememberChoiceVisible = false; IsPrimaryButtonVisible = false; UseAutoClose = true; break; case "warning": DialogTitle = "风险提示"; DialogMessage = "当前设置可能带来潜在风险,请在继续之前确认信息。"; DialogIcon = IconType.ExclamationTriangle; DialogAccentBrush = new SolidColorBrush(Color.FromRgb(234, 179, 8)); PrimaryButtonText = string.Empty; SecondaryButtonText = string.Empty; IsRememberChoiceVisible = false; IsPrimaryButtonVisible = false; UseAutoClose = true; break; case "confirm": DialogTitle = "确认操作"; DialogMessage = "此操作会影响当前设置,请确认是否继续。"; DialogIcon = IconType.QuestionMarkCircle; DialogAccentBrush = new SolidColorBrush(Color.FromRgb(59, 130, 246)); PrimaryButtonText = "确认"; SecondaryButtonText = "取消"; IsRememberChoiceVisible = false; break; case "info": case "general": default: DialogTitle = "通用提示"; DialogMessage = "执行此操作将应用更改,是否继续?您也可以勾选记住选择。"; DialogIcon = IconType.InformationCircle; DialogAccentBrush = new SolidColorBrush(Color.FromRgb(59, 130, 246)); PrimaryButtonText = "确认"; SecondaryButtonText = "取消"; IsRememberChoiceVisible = true; break; } } private (string variant, TimeSpan? delayOverride) ParseParameter(string? parameter) { if (string.IsNullOrWhiteSpace(parameter)) { return ("info", null); } var parts = parameter.Split(new[] { ':', '|', ';', ',' }, StringSplitOptions.RemoveEmptyEntries); var variant = parts.Length > 0 ? parts[0].Trim() : "info"; if (parts.Length > 1 && double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out var seconds) && seconds > 0) { return (variant, TimeSpan.FromSeconds(seconds)); } return (variant, null); } private void StartAutoCloseTimer() { CancelAutoCloseTimer(); var delay = AutoCloseDelay; var titleSnapshot = DialogTitle; _autoCloseCts = new CancellationTokenSource(); var token = _autoCloseCts.Token; Task.Run(async () => { try { await Task.Delay(delay, token); if (!token.IsCancellationRequested) { await Dispatcher.UIThread.InvokeAsync(() => { IsDialogOpen = false; LastActionMessage = $"已自动关闭:{titleSnapshot}"; }); } } catch (TaskCanceledException) { // 忽略取消 } }, token); } private void CancelAutoCloseTimer() { if (_autoCloseCts != null) { _autoCloseCts.Cancel(); _autoCloseCts.Dispose(); _autoCloseCts = null; } } }