mirror of
https://github.com/2dust/v2rayN.git
synced 2026-11-14 08:24:59 +03:00
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
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user