Files
2dust_v2rayN/v2rayN/v2rayN.Desktop/Manager/WindowDialog.cs
T
DHR60 9664ee380e ViewModel-First (#9621)
* Interaction

* Remove view action

* ViewModel-first

* MainGirdOrientation Hot Reload

* Fix avalonia preview

* Remove CloseWindowInteraction

* Fix

* Avoid threading issues

* Fix

* ProfilesSelect

* Try fix previewer

* Remove AppEvents.ProfilesRefreshRequested

* Remove AppEvents.SubscriptionsRefreshRequested

* Remove AppEvents.ProxiesReloadRequested

* Remove AppEvents.AdjustMainLvColWidthRequested

* Remove AppEvents.SetDefaultServerRequested

* Remove AppEvents.RoutingsMenuRefreshRequested

* Remove AppEvents.TestServerRequested

* Remove AppEvents.InboundDisplayRequested

* Remove AppEvents.SubscriptionsUpdateRequested

* Remove AppEvents.ShowHideWindowRequested

* Remove AppEvents.ReloadRequested

* Remove AppEvents.AddServerRequested

* Fix
2026-07-14 09:05:05 +08:00

69 lines
1.9 KiB
C#

using v2rayN.Desktop.Base;
using v2rayN.Desktop.Common;
namespace v2rayN.Desktop.Manager;
public class WindowDialog : IWindowDialog
{
public async Task<bool> ShowDialogAsync<TViewModel>(TViewModel vm)
where TViewModel : class
{
var owner = TryGetOwnerWindow();
var view = SimpleViewLocator.Instance.Build(vm);
if (view is not WindowBase<TViewModel> window)
{
return false;
}
window.ViewModel = vm;
if (vm is ServiceLib.Base.ICloseable closeable)
{
closeable.RequestClose += (_, _) => Dispatch(() => window.Close(true));
}
var result = await window.ShowDialog<bool>(owner);
return result;
}
public static Window TryGetOwnerWindow()
{
var desktop = Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime
?? throw new InvalidOperationException("Application lifetime is not of type IClassicDesktopStyleApplicationLifetime.");
var openWindows = desktop.Windows
.Where(w => w.IsVisible)
.ToArray();
if (openWindows.Length == 0)
{
return desktop.MainWindow
?? throw new InvalidOperationException("No open windows and no main window found.");
}
if (openWindows.Length == 1)
{
return openWindows[0];
}
try
{
Window.SortWindowsByZOrder(openWindows);
var activeTopmost = openWindows.Reverse().FirstOrDefault(w => w.IsActive);
return activeTopmost ?? openWindows[^1];
}
catch
{
return desktop.Windows.FirstOrDefault(w => w.IsActive)
?? desktop.MainWindow
?? openWindows[0];
}
}
private static void Dispatch(Action action)
{
Dispatcher.UIThread.InvokeAsync(action);
}
}