using XFEExtension.NetCore.InputSimulator; using XFEWorkflow.Services; namespace XFEWorkflow.Controls; public class AutomationSettingsSection : UserControl { protected AppServices Services => AppServices.Current; protected StackPanel Body { get; } = new() { Spacing = 14, Padding = new(0, 12, 0, 24) }; protected InfoBar Message { get; } = new() { IsClosable = true, Visibility = Visibility.Collapsed }; public AutomationSettingsSection() { var root = new Grid { RowSpacing = 10 }; root.RowDefinitions.Add(new()); root.RowDefinitions.Add(new() { Height = GridLength.Auto }); root.Children.Add(new ScrollViewer { Content = Body, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled }); Grid.SetRow(Message, 1); root.Children.Add(Message); Content = root; Message.Closed += (_, _) => { if (!Message.IsOpen) Message.Visibility = Visibility.Collapsed; }; } protected static TextBlock Caption(string text) => new() { Text = text, Style = (Style)Application.Current.Resources["ManagementCaptionStyle"] }; protected static Border Card(UIElement content) => new() { Style = (Style)Application.Current.Resources["ManagementCardStyle"], Child = content }; protected Button Action(string text, Func action, string? id = null) { var button = new Button { Content = text }; if (id is not null) Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(button, id); button.Click += async (_, _) => { button.IsEnabled = false; try { await Guard(action); } finally { button.IsEnabled = true; } }; return button; } protected async Task Guard(Func action) { try { await Services.Ready; await action(); } catch (Exception ex) { Show(Services.Redactor.Redact(ex.Message), InfoBarSeverity.Error); } } protected void Show(string text, InfoBarSeverity severity = InfoBarSeverity.Success) { Message.Message = text; Message.Severity = severity; Message.Visibility = Visibility.Visible; Message.IsOpen = true; } } public sealed class CredentialSettings : AutomationSettingsSection { private readonly ListView references = new() { MaxHeight = 240 }; private readonly TextBox name = new() { Header = "凭据引用名称", PlaceholderText = "ai.key / wecom.webhook" }; private readonly PasswordBox value = new() { Header = "凭据内容" }; public CredentialSettings() { Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(name, "CredentialName"); Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(value, "CredentialValue"); var saved = new StackPanel { Spacing = 12 }; saved.Children.Add(new TextBlock { Text = "已保存的凭据", FontSize = 18, FontWeight = Microsoft.UI.Text.FontWeights.SemiBold }); saved.Children.Add(Caption("Windows 当前用户加密保存;工作流只保存引用名,日志隐藏已管理的敏感值。")); saved.Children.Add(references); saved.Children.Add(Action("删除选中凭据", async () => { if (references.SelectedItem is not string reference) throw new InvalidOperationException("请先选择凭据"); await Services.Credentials.RemoveAsync(reference); Refresh(); Show("凭据已删除,历史日志保留脱敏结果"); }, "DeleteCredential")); Body.Children.Add(Card(saved)); var editor = new StackPanel { Spacing = 12 }; editor.Children.Add(new TextBlock { Text = "添加或更新凭据", FontSize = 18, FontWeight = Microsoft.UI.Text.FontWeights.SemiBold }); editor.Children.Add(name); editor.Children.Add(value); editor.Children.Add(Action("保存凭据", async () => { if (string.IsNullOrWhiteSpace(name.Text) || value.Password.Length == 0) throw new ArgumentException("请输入凭据名称和内容"); var secret = value.Password; await Services.Credentials.SetAsync(name.Text.Trim(), secret); Services.Redactor.Add(secret); value.Password = ""; Refresh(); Show("凭据已加密保存"); }, "SaveCredential")); editor.Children.Add(Caption("在节点属性中选择凭据引用,或在 C# 中通过上下文读取凭据。")); Body.Children.Add(Card(editor)); references.SelectionChanged += (_, _) => { if (references.SelectedItem is string reference) name.Text = reference; }; Loaded += async (_, _) => await Guard(() => { Refresh(); return Task.CompletedTask; }); } private void Refresh() => references.ItemsSource = Services.Credentials.References.Where(x => !x.StartsWith("space.session.", StringComparison.Ordinal)).Order().ToArray(); } public sealed class DesktopInputSettings : AutomationSettingsSection { private readonly ComboBox backend = new() { HorizontalAlignment = HorizontalAlignment.Stretch }; private readonly TextBlock driver = new() { TextWrapping = TextWrapping.Wrap }; private bool updating; private static bool HidSupported => OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000) && System.Runtime.InteropServices.RuntimeInformation.OSArchitecture == System.Runtime.InteropServices.Architecture.X64; public DesktopInputSettings() { backend.Items.Add(new ComboBoxItem { Content = "标准 Windows 输入", Tag = "standard" }); backend.Items.Add(new ComboBoxItem { Content = "HID 驱动输入", Tag = "hid", IsEnabled = HidSupported }); Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(backend, "DesktopInputBackend"); var input = new StackPanel { Spacing = 12 }; input.Children.Add(new TextBlock { Text = "输入后端", FontSize = 18, FontWeight = Microsoft.UI.Text.FontWeights.SemiBold }); input.Children.Add(Caption("标准输入支持 Unicode 中文和多屏绝对坐标。桌面自动化运行时使用独占输入锁。")); input.Children.Add(backend); Body.Children.Add(Card(input)); var hid = new StackPanel { Spacing = 12 }; hid.Children.Add(new TextBlock { Text = "HID 驱动", FontSize = 18, FontWeight = Microsoft.UI.Text.FontWeights.SemiBold }); hid.Children.Add(driver); hid.Children.Add(Caption("仅支持 Windows 11 x64。当前驱动支持物理按键和相对鼠标,不支持 Unicode 文本与绝对定位。")); Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(driver, "DriverStatus"); var driverActions = new AdaptiveWrapPanel(); driverActions.Children.Add(Action("检测驱动", () => { RefreshDriver(); return Task.CompletedTask; })); var install = Action("安装 / 更新 HID(UAC)", async () => { await Task.Run(DriverDeployment.InstallEmbeddedDriver); RefreshDriver(); }); install.IsEnabled = HidSupported; driverActions.Children.Add(install); hid.Children.Add(driverActions); Body.Children.Add(Card(hid)); var stop = new StackPanel { Spacing = 12 }; stop.Children.Add(new TextBlock { Text = "安全停止", FontSize = 18, FontWeight = Microsoft.UI.Text.FontWeights.SemiBold }); stop.Children.Add(Caption("全局快捷键 Ctrl+Alt+F12:停止任务、释放按键并暂停自动调度。")); var stopActions = new AdaptiveWrapPanel(); stopActions.Children.Add(Action("紧急停止并释放按键", async () => { Services.Scheduler.Suspend(); Services.ScreenTools.CancelSelection(); Services.Desktop.EmergencyStop(); await Services.Runs.StopAllAsync(); Show("任务已停止,按键已释放,自动调度已暂停"); }, "SettingsEmergencyStop")); stopActions.Children.Add(Action("恢复自动调度", async () => { await Services.Scheduler.ResumeAsync(); Show("自动调度已恢复"); }, "SettingsResumeSchedule")); stop.Children.Add(stopActions); Body.Children.Add(Card(stop)); backend.SelectionChanged += async (_, _) => { if (updating || backend.SelectedItem is not ComboBoxItem item) return; var previous = Services.State.DesktopBackend; await Guard(async () => { try { using var lease = Services.Runs.TryAcquireDesktopLease() ?? throw new InvalidOperationException("桌面输入正在使用,请先停止相关工作流"); if (Services.Runs.ActiveWorkflowIds.Count > 0) throw new InvalidOperationException("请先停止正在运行的工作流"); var selected = (string)item.Tag; if (selected == "hid" && !HidSupported) throw new PlatformNotSupportedException("HID 仅支持 Windows 11 x64"); Services.Desktop.Backend = selected; Services.State.DesktopBackend = selected; await Services.State.SaveAsync(); Show("输入后端已切换"); } catch { Services.Desktop.Backend = previous; Services.State.DesktopBackend = previous; SelectBackend(previous); throw; } }); }; Loaded += async (_, _) => await Guard(() => { SelectBackend(Services.State.DesktopBackend); RefreshDriver(); return Task.CompletedTask; }); } private void RefreshDriver() => driver.Text = HidSupported ? DriverDeployment.CheckDriver().Message : "此系统不支持 HID 驱动"; private void SelectBackend(string selected) { updating = true; backend.SelectedItem = backend.Items.OfType().FirstOrDefault(x => (string)x.Tag == selected); updating = false; } }