using System.Windows; using System.Windows.Automation; using System.Windows.Automation.Peers; using Titano.Ui.Controls; namespace Titano.App.Shell; /// /// The login form (BRIEF ยง12.7). It calls back the connection with the credentials and stays open with the /// error sentence until the sign-in succeeds or the user cancels. The password never leaves the PasswordBox /// except for that call. A failed sign-in is announced to screen readers and stays as the password box's help text. /// public sealed partial class LoginDialog : TitanoWindow { private readonly Func> _signIn; private LoginDialog(string server, string? username, Func> signIn) { _signIn = signIn; InitializeComponent(); ServerText.Text = server; User.Text = username ?? string.Empty; Loaded += (_, _) => { if (User.Text.Length == 0) User.Focus(); else Password.Focus(); }; } /// True when the user signed in. public static bool Ask(Window? owner, string server, string? username, Func> signIn) { var dialog = new LoginDialog(server, username, signIn); if (owner is { IsVisible: true }) dialog.Owner = owner; else dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen; return dialog.ShowDialog() == true; } private void OnCancel(object sender, RoutedEventArgs e) => DialogResult = false; private async void OnSignIn(object sender, RoutedEventArgs e) { SignIn.IsEnabled = false; Error.Visibility = Visibility.Collapsed; AutomationProperties.SetHelpText(Password, string.Empty); try { var error = await _signIn(User.Text, Password.Password); if (error is null) { DialogResult = true; return; } Error.Text = error; Error.Visibility = Visibility.Visible; AutomationProperties.SetHelpText(Password, error); Password.Clear(); Password.Focus(); UIElementAutomationPeer.CreatePeerForElement(this)?.RaiseNotificationEvent( AutomationNotificationKind.ActionCompleted, AutomationNotificationProcessing.ImportantMostRecent, error, "Titano.SignIn"); } finally { SignIn.IsEnabled = true; } } }