using XFEWorkflow.Abstractions;
namespace XFEWorkflow.Services;
internal static class AccountDialog
{
internal static ContentDialog? Current { get; private set; }
public static async Task<bool> ShowAsync(XamlRoot root)
{
var services = AppServices.Current; await services.Ready;
using var lifetime = new CancellationTokenSource();
var name = new TextBox { Header = "账号", PlaceholderText = "输入 Space 账号", MaxLength = 64 };
var password = new PasswordBox { Header = "密码" };
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(name, "SpaceLoginAccount");
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(password, "SpaceLoginPassword");
var error = new InfoBar { Severity = InfoBarSeverity.Error, IsClosable = false };
var progress = new ProgressRing { Width = 24, Height = 24, IsActive = false, Visibility = Visibility.Collapsed, HorizontalAlignment = HorizontalAlignment.Left };
var content = new StackPanel { Spacing = 16, MinWidth = 320 };
content.Children.Add(new TextBlock { Text = "登录 Space,管理资料、插件与工作流发布。", TextWrapping = TextWrapping.Wrap });
content.Children.Add(new TextBlock { Text = services.State.SpaceAddress, TextWrapping = TextWrapping.Wrap, Style = (Style)Application.Current.Resources["CaptionTextBlockStyle"] });
content.Children.Add(name); content.Children.Add(password);
content.Children.Add(new TextBlock { Text = "没有账号?填写账号和密码后选择注册。新密码至少 12 位。", TextWrapping = TextWrapping.Wrap, Style = (Style)Application.Current.Resources["CaptionTextBlockStyle"] });
content.Children.Add(error); content.Children.Add(progress);
var dialog = new ContentDialog { Title = "登录 / 注册", Content = content, PrimaryButtonText = "登录", SecondaryButtonText = "注册", CloseButtonText = "取消", DefaultButton = ContentDialogButton.Primary, XamlRoot = root };
var success = false; var busy = false;
async Task SubmitAsync(ContentDialogButtonClickEventArgs args, bool register)
{
args.Cancel = true;
if (busy) return;
var deferral = args.GetDeferral(); busy = true;
dialog.IsPrimaryButtonEnabled = dialog.IsSecondaryButtonEnabled = name.IsEnabled = password.IsEnabled = false;
error.IsOpen = false; progress.IsActive = true; progress.Visibility = Visibility.Visible;
try
{
var account = name.Text.Trim();
if (account.Length == 0 || password.Password.Length == 0) throw new ArgumentException("请输入账号和密码。");
if (register && password.Password.Length < 12) throw new ArgumentException("注册密码至少需要 12 位。");
if (register) await services.Space.RequestAsync("register", new() { ["account"] = account, ["password"] = password.Password, ["nickname"] = account }, lifetime.Token);
await services.Space.LoginAsync(account, password.Password, lifetime.Token);
success = true; args.Cancel = false;
}
catch (OperationCanceledException) when (lifetime.IsCancellationRequested) { }
catch (Exception ex) { error.Message = ex.Message; error.IsOpen = true; }
finally
{
dialog.IsPrimaryButtonEnabled = dialog.IsSecondaryButtonEnabled = name.IsEnabled = password.IsEnabled = true;
progress.IsActive = false; progress.Visibility = Visibility.Collapsed; busy = false; deferral.Complete();
}
}
dialog.PrimaryButtonClick += async (_, args) => await SubmitAsync(args, false);
dialog.SecondaryButtonClick += async (_, args) => await SubmitAsync(args, true);
dialog.Closing += (_, _) => lifetime.Cancel();
try
{
Current = dialog;
await dialog.ShowAsync(); return success;
}
finally { password.Password = ""; Current = null; }
}
}
using XFEWorkflow.Abstractions;
namespace XFEWorkflow.Services;
internal static class AccountDialog
{
internal static ContentDialog? Current { get; private set; }
public static async Task<bool> ShowAsync(XamlRoot root)
{
var services = AppServices.Current; await services.Ready;
using var lifetime = new CancellationTokenSource();
var name = new TextBox { Header = "账号", PlaceholderText = "输入 Space 账号", MaxLength = 64 };
var password = new PasswordBox { Header = "密码" };
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(name, "SpaceLoginAccount");
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(password, "SpaceLoginPassword");
var error = new InfoBar { Severity = InfoBarSeverity.Error, IsClosable = false };
var progress = new ProgressRing { Width = 24, Height = 24, IsActive = false, Visibility = Visibility.Collapsed, HorizontalAlignment = HorizontalAlignment.Left };
var content = new StackPanel { Spacing = 16, MinWidth = 320 };
content.Children.Add(new TextBlock { Text = "登录 Space,管理资料、插件与工作流发布。", TextWrapping = TextWrapping.Wrap });
content.Children.Add(new TextBlock { Text = services.State.SpaceAddress, TextWrapping = TextWrapping.Wrap, Style = (Style)Application.Current.Resources["CaptionTextBlockStyle"] });
content.Children.Add(name); content.Children.Add(password);
content.Children.Add(new TextBlock { Text = "没有账号?填写账号和密码后选择注册。新密码至少 12 位。", TextWrapping = TextWrapping.Wrap, Style = (Style)Application.Current.Resources["CaptionTextBlockStyle"] });
content.Children.Add(error); content.Children.Add(progress);
var dialog = new ContentDialog { Title = "登录 / 注册", Content = content, PrimaryButtonText = "登录", SecondaryButtonText = "注册", CloseButtonText = "取消", DefaultButton = ContentDialogButton.Primary, XamlRoot = root };
var success = false; var busy = false;
async Task SubmitAsync(ContentDialogButtonClickEventArgs args, bool register)
{
args.Cancel = true;
if (busy) return;
var deferral = args.GetDeferral(); busy = true;
dialog.IsPrimaryButtonEnabled = dialog.IsSecondaryButtonEnabled = name.IsEnabled = password.IsEnabled = false;
error.IsOpen = false; progress.IsActive = true; progress.Visibility = Visibility.Visible;
try
{
var account = name.Text.Trim();
if (account.Length == 0 || password.Password.Length == 0) throw new ArgumentException("请输入账号和密码。");
if (register && password.Password.Length < 12) throw new ArgumentException("注册密码至少需要 12 位。");
if (register) await services.Space.RequestAsync("register", new() { ["account"] = account, ["password"] = password.Password, ["nickname"] = account }, lifetime.Token);
await services.Space.LoginAsync(account, password.Password, lifetime.Token);
success = true; args.Cancel = false;
}
catch (OperationCanceledException) when (lifetime.IsCancellationRequested) { }
catch (Exception ex) { error.Message = ex.Message; error.IsOpen = true; }
finally
{
dialog.IsPrimaryButtonEnabled = dialog.IsSecondaryButtonEnabled = name.IsEnabled = password.IsEnabled = true;
progress.IsActive = false; progress.Visibility = Visibility.Collapsed; busy = false; deferral.Complete();
}
}
dialog.PrimaryButtonClick += async (_, args) => await SubmitAsync(args, false);
dialog.SecondaryButtonClick += async (_, args) => await SubmitAsync(args, true);
dialog.Closing += (_, _) => lifetime.Cancel();
try
{
Current = dialog;
await dialog.ShowAsync(); return success;
}
finally { password.Password = ""; Current = null; }
}
}