2025-02-10 20:53:40 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
2026-02-21 16:31:24 +08:00
|
|
|
|
namespace ShrlAlgoToolkit.RevitAddins.Common.Assists;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
public sealed record SingletonViewAssist<T>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
where T : Window, new()
|
|
|
|
|
|
{
|
|
|
|
|
|
private static T _instance;
|
|
|
|
|
|
private static readonly object Padlock = new();
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
private SingletonViewAssist() { }
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
public static T GetInstance(out bool isNewCreate)
|
|
|
|
|
|
{
|
|
|
|
|
|
isNewCreate = false;
|
|
|
|
|
|
//double-check locking
|
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
isNewCreate = true;
|
|
|
|
|
|
lock (Padlock)
|
|
|
|
|
|
{
|
|
|
|
|
|
_instance ??= new T();
|
|
|
|
|
|
_instance.Closed += OnWindowClosed;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return _instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnWindowClosed(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_instance = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|