using System;
using System.Collections.Generic;
using Avalonia.Controls;
namespace Nitrox.Launcher.Models.Extensions;
///
/// Avalonia doesn't provide a public API to close the window non-programmatically so this is a hack to support it.
///
public static class CloseByUserExtensions
{
private static readonly Dictionary isClosingByUser = [];
///
/// Closes the window non-programmatically (by user).
///
public static void CloseByUser(this Window window)
{
if (window == null)
{
return;
}
window.Closed += WindowOnClosed;
isClosingByUser[window] = true;
window.Close();
static void WindowOnClosed(object sender, EventArgs e)
{
if (sender is not Window window)
{
return;
}
window.Closed -= WindowOnClosed;
isClosingByUser.Remove(window);
}
}
///
/// Closes the window programmatically.
///
public static void CloseByCode(this Window window)
{
if (window == null)
{
return;
}
isClosingByUser[window] = false;
window.Close();
}
public static bool IsClosingByUser(this Window closingWindow, WindowClosingEventArgs closingArgs = null)
{
if (closingWindow is not null && isClosingByUser.TryGetValue(closingWindow, out bool isByUser))
{
return isByUser;
}
if (closingArgs is { IsProgrammatic: false })
{
return true;
}
return false;
}
}