forked from amkovkov/GranuSightSoftware2
feat: move mainwindow to core and add NavigationService
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
|
||||
namespace GSS2.UI.Core.Services;
|
||||
|
||||
public class NavigationService
|
||||
{
|
||||
private readonly Stack<UserControl> _navigateControls = new Stack<UserControl>();
|
||||
private readonly IApplicationLifetime? _applicationLifetime;
|
||||
|
||||
public NavigationService(IApplicationLifetime? applicationLifetime)
|
||||
{
|
||||
_applicationLifetime = applicationLifetime;
|
||||
}
|
||||
|
||||
public void NavigateTo(UserControl control)
|
||||
{
|
||||
_navigateControls.Push(control);
|
||||
|
||||
switch (_applicationLifetime)
|
||||
{
|
||||
case IClassicDesktopStyleApplicationLifetime desktop
|
||||
when desktop.MainWindow is MainWindow mainWindow:
|
||||
|
||||
mainWindow.SetControl(control);
|
||||
break;
|
||||
|
||||
case ISingleViewApplicationLifetime singleView:
|
||||
singleView.MainView = control;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void NavigateBack()
|
||||
{
|
||||
if (_navigateControls.Count <= 1)
|
||||
return;
|
||||
|
||||
var lastControl = _navigateControls.Pop();
|
||||
var control = _navigateControls.Peek();
|
||||
|
||||
switch (_applicationLifetime)
|
||||
{
|
||||
case IClassicDesktopStyleApplicationLifetime desktop
|
||||
when desktop.MainWindow is MainWindow mainWindow:
|
||||
|
||||
mainWindow.SetControl(control);
|
||||
break;
|
||||
|
||||
case ISingleViewApplicationLifetime singleView:
|
||||
singleView.MainView = control;
|
||||
break;
|
||||
}
|
||||
|
||||
if (lastControl is IDisposable disposable)
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user