diff --git a/StabilityMatrix/App.xaml b/StabilityMatrix/App.xaml index d64634c9..cc215de6 100644 --- a/StabilityMatrix/App.xaml +++ b/StabilityMatrix/App.xaml @@ -3,6 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:StabilityMatrix" xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" + Exit="App_OnExit" Startup="App_OnStartup" > diff --git a/StabilityMatrix/App.xaml.cs b/StabilityMatrix/App.xaml.cs index 1f7736ef..fa605bd4 100644 --- a/StabilityMatrix/App.xaml.cs +++ b/StabilityMatrix/App.xaml.cs @@ -20,6 +20,8 @@ namespace StabilityMatrix /// public partial class App : Application { + private ServiceProvider? serviceProvider; + private void App_OnStartup(object sender, StartupEventArgs e) { var serviceCollection = new ServiceCollection(); @@ -50,9 +52,14 @@ namespace StabilityMatrix log.AddNLog(logConfig); }); - var provider = serviceCollection.BuildServiceProvider(); - var window = provider.GetRequiredService(); + serviceProvider = serviceCollection.BuildServiceProvider(); + var window = serviceProvider.GetRequiredService(); window.Show(); } + + private void App_OnExit(object sender, ExitEventArgs e) + { + serviceProvider?.GetRequiredService().OnShutdown(); + } } } diff --git a/StabilityMatrix/ViewModels/LaunchViewModel.cs b/StabilityMatrix/ViewModels/LaunchViewModel.cs index 9ab0cbad..5583be21 100644 --- a/StabilityMatrix/ViewModels/LaunchViewModel.cs +++ b/StabilityMatrix/ViewModels/LaunchViewModel.cs @@ -28,7 +28,6 @@ public partial class LaunchViewModel : ObservableObject public string consoleOutput = ""; private InstalledPackage? selectedPackage; - public InstalledPackage? SelectedPackage { get => selectedPackage; @@ -42,6 +41,8 @@ public partial class LaunchViewModel : ObservableObject } public ObservableCollection InstalledPackages = new(); + public Process? CurrentRunningProcess { get; set; } + public event EventHandler ScrollNeeded; public LaunchViewModel(ISettingsManager settingsManager) @@ -97,6 +98,7 @@ public partial class LaunchViewModel : ObservableObject var args = "\"" + Path.Combine(packagePath, "launch.py") + "\""; venv.RunDetached(args, onConsoleOutput, onExit); + CurrentRunningProcess = venv.Process; }); public void OnLoaded() @@ -104,10 +106,18 @@ public partial class LaunchViewModel : ObservableObject LoadPackages(); if (InstalledPackages.Any()) { - SelectedPackage = InstalledPackages[0]; + SelectedPackage = + InstalledPackages[ + InstalledPackages.IndexOf(InstalledPackages.FirstOrDefault(x => + x.Id == settingsManager.Settings.ActiveInstalledPackage))]; } } + public void OnShutdown() + { + CurrentRunningProcess?.Kill(); + } + private void LoadPackages() { var packages = settingsManager.Settings.InstalledPackages;