using System.Windows;
using Titano.App.Infrastructure;
using Titano.Ui.Controls;
namespace Titano.App.Shell;
/// Modal confirmation for trusting a certificate and for destructive actions (BRIEF §12.1).
public sealed partial class ConfirmDialog : TitanoWindow
{
public ConfirmDialog(ConfirmRequest request)
{
DataContext = request;
InitializeComponent();
Action.SetResourceReference(StyleProperty, request.IsDestructive ? "Button.Destructive" : "Button.Primary");
if (request.TypedConfirmation is null)
{
Loaded += (_, _) => Safe.Focus();
return;
}
Typing.Visibility = Visibility.Visible;
Loaded += (_, _) => Typed.Focus();
EnableAction(false);
}
/// True when the user chose the action.
public static bool Ask(Window? owner, ConfirmRequest request)
{
var dialog = new ConfirmDialog(request);
if (owner is { IsVisible: true }) dialog.Owner = owner;
else dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
return dialog.ShowDialog() == true;
}
private void OnSafe(object sender, RoutedEventArgs e) => DialogResult = false;
private void OnAction(object sender, RoutedEventArgs e) => DialogResult = true;
private void OnTyped(object sender, System.Windows.Controls.TextChangedEventArgs e) =>
EnableAction(string.Equals(Typed.Text.Trim(), ((ConfirmRequest)DataContext).TypedConfirmation, StringComparison.CurrentCultureIgnoreCase));
/// The action waits for the typed confirmation; until then its tooltip says what to type (DESIGN §4 disabled states).
private void EnableAction(bool enabled)
{
var request = (ConfirmRequest)DataContext;
Action.IsEnabled = enabled;
Action.ToolTip = enabled ? request.ActionToolTip : request.TypedPrompt;
}
}