using System.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.WinUI.Controls; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Automation; namespace XFEWarframe.Desktop.Editing; public sealed class OverviewActionsView : StackPanel { public static readonly DependencyProperty ModelProperty = DependencyProperty.Register(nameof(Model), typeof(OverviewActionsModel), typeof(OverviewActionsView), new PropertyMetadata(null, ModelChanged)); public OverviewActionsModel? Model { get => (OverviewActionsModel?)GetValue(ModelProperty); set => SetValue(ModelProperty, value); } private readonly ProgressBar loading = new() { IsIndeterminate = true }; private readonly InfoBar error = new() { Severity = InfoBarSeverity.Error, IsClosable = false }; private readonly StackPanel groups = new() { Spacing = 8 }; internal IReadOnlyList ActionButtons => groups.Children.OfType().SelectMany(x => x.Items).OfType().Select(x => x.Content).OfType().ToArray(); public OverviewActionsView() { Visibility = Visibility.Collapsed; Spacing = 12; Children.Add(new TextBlock { Text = UiText.Translate("快捷操作"), FontSize = 22, FontWeight = Microsoft.UI.Text.FontWeights.SemiBold }); Children.Add(new TextBlock { Text = UiText.Translate("针对当前账号执行解锁、养成与整理操作。"), TextWrapping = TextWrapping.Wrap }); Children.Add(loading); Children.Add(error); Children.Add(groups); } private static void ModelChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { var view = (OverviewActionsView)sender; if (args.OldValue is OverviewActionsModel old) old.PropertyChanged -= view.Changed; if (args.NewValue is OverviewActionsModel next) next.PropertyChanged += view.Changed; view.Build(); view.Update(); } private void Changed(object? sender, PropertyChangedEventArgs args) { if (args.PropertyName == nameof(OverviewActionsModel.Actions)) Build(); Update(); } private void Update() { var model = Model; loading.Visibility = model?.IsBusy == true ? Visibility.Visible : Visibility.Collapsed; foreach (var button in ActionButtons) button.IsEnabled = model?.IsBusy != true; error.Message = model?.Message ?? ""; error.IsOpen = error.Message.Length > 0; Visibility = model is not null && (model.IsBusy || model.Actions.Count > 0 || error.IsOpen) ? Visibility.Visible : Visibility.Collapsed; } private void Build() { var expanded = groups.Children.OfType().ToDictionary(x => (string)x.Tag, x => x.IsExpanded); groups.Children.Clear(); if (Model is not { } model) return; foreach (var group in model.Actions.GroupBy(x => Group(x.Key)).OrderBy(x => x.Key.Order)) { var section = new SettingsExpander { Header = UiText.Translate(group.Key.Title), Description = UiText.Format("{0} 项操作", group.Count()), Tag = group.Key.Title, IsExpanded = expanded.GetValueOrDefault(group.Key.Title, false) }; foreach (var action in group) { var button = new FormDialogButton { Content = UiText.Translate("执行…"), Tag = action.Key, DialogTitle = action.Label, PrimaryText = UiText.Translate("执行"), Ready = false }; AutomationProperties.SetName(button, action.Label); OverviewOperation? operation = null; button.PrepareCommand = new AsyncRelayCommand(async () => { operation = await model.PrepareAsync(action.Key); button.Ready = operation is not null; if (operation is null) return; var form = new StackPanel { Spacing = 16, MinWidth = 320 }; form.Children.Add(new TextBlock { Text = UiText.Translate("当前玩家") + " · " + operation.Target.Name, FontWeight = Microsoft.UI.Text.FontWeights.SemiBold, TextWrapping = TextWrapping.Wrap }); form.Children.Add(new TextBlock { Text = Description(action), TextWrapping = TextWrapping.Wrap }); if (operation.Fields.Count > 0) form.Children.Add(new EditorForm { Fields = operation.Fields }); button.Form = form; }); button.Validating += (_, _) => { if (operation is not null) foreach (var field in operation.Fields) field.Serialize(); }; button.PrimaryCommand = new AsyncRelayCommand(async () => { if (operation is not null) await model.ExecuteAsync(operation); }); section.Items.Add(new SettingsCard { Header = action.Label, Description = Description(action), Content = button }); } groups.Children.Add(section); } } private static (int Order, string Title) Group(string key) => key switch { "unlock.intrinsics" or "unlock.mods" or "unlock.focus" or "unlock.helminth" or "unlock.helminthRecipes" or "unlock.plexus" => (1, "养成与强化"), "unlock.badges" or "inbox.read" or "syndicate" => (2, "账号整理"), _ => (0, "探索与解锁") }; private static string Description(EditorAction action) => UiText.Translate(action.Key switch { "unlock.intrinsics" => "将航道星舰与漂泊者的内源之力提升至满级。", "unlock.scans" => "补齐资料库扫描记录。", "unlock.ship" => "解锁军械库、铸造厂等飞船功能模块。", "unlock.captura" => "添加尚未拥有的 Captura 场景。", "unlock.missions" => "将普通星图和钢铁之路节点标记为完成。", "unlock.normalMissions" => "仅将普通星图节点标记为完成。", "unlock.bounties" => "解锁已支持的赏金任务链。", "unlock.simaris" => "完成 Simaris 的个人研究扫描记录。", "unlock.helminthRecipes" => "添加尚未拥有的 Helminth 蓝图。", "unlock.mods" => "补齐目录中支持的满级 MOD。", "unlock.badges" => "清除装备与外观上的新获得标记。", "unlock.focus" => "解锁全部五个专精学派。", "unlock.helminth" => "提升 Helminth 等级并解锁其支持的能力。", "unlock.plexus" => "将 Plexus 提升至满级;尚未拥有时先创建。", "inbox.read" => "将当前账号的全部邮件标记为已读。", "syndicate" => "选择当前支持的集团,也可以取消支持。", _ => action.Description }); }