Browse Source

Fix ActiveInstalledPackage

pull/5/head
JT 2 years ago
parent
commit
862525a86f
  1. 1
      StabilityMatrix/Helper/ProcessRunner.cs
  2. 2
      StabilityMatrix/PyVenvRunner.cs
  3. 34
      StabilityMatrix/ViewModels/LaunchViewModel.cs

1
StabilityMatrix/Helper/ProcessRunner.cs

@ -42,6 +42,7 @@ public static class ProcessRunner
process.StartInfo.UseShellExecute = false; process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true; process.StartInfo.CreateNoWindow = true;
if (environmentVariables != null) if (environmentVariables != null)

2
StabilityMatrix/PyVenvRunner.cs

@ -65,7 +65,7 @@ public class PyVenvRunner: IDisposable
} }
} }
public void RunDetached(string arguments, Action<string?> outputDataReceived, Action<int>? onExit = null, bool unbuffered = true) public void RunDetached(string arguments, Action<string>? outputDataReceived, Action<int>? onExit = null, bool unbuffered = true)
{ {
if (!Exists()) if (!Exists())
{ {

34
StabilityMatrix/ViewModels/LaunchViewModel.cs

@ -22,6 +22,7 @@ public partial class LaunchViewModel : ObservableObject
{ {
private readonly ISettingsManager settingsManager; private readonly ISettingsManager settingsManager;
private PyVenvRunner? venvRunner; private PyVenvRunner? venvRunner;
private bool clearingPackages = false;
[ObservableProperty] [ObservableProperty]
private string consoleInput = ""; private string consoleInput = "";
@ -44,7 +45,12 @@ public partial class LaunchViewModel : ObservableObject
{ {
if (value == selectedPackage) return; if (value == selectedPackage) return;
selectedPackage = value; selectedPackage = value;
settingsManager.SetActiveInstalledPackage(value);
if (!clearingPackages)
{
settingsManager.SetActiveInstalledPackage(value);
}
OnPropertyChanged(); OnPropertyChanged();
} }
} }
@ -56,6 +62,7 @@ public partial class LaunchViewModel : ObservableObject
public LaunchViewModel(ISettingsManager settingsManager) public LaunchViewModel(ISettingsManager settingsManager)
{ {
this.settingsManager = settingsManager; this.settingsManager = settingsManager;
SetProcessRunning(false);
} }
public AsyncRelayCommand LaunchCommand => new(async () => public AsyncRelayCommand LaunchCommand => new(async () =>
@ -82,8 +89,8 @@ public partial class LaunchViewModel : ObservableObject
{ {
await venvRunner.Setup(); await venvRunner.Setup();
} }
var onConsoleOutput = new Action<string?>(s => void OnConsoleOutput(string? s)
{ {
if (s == null) return; if (s == null) return;
Dispatcher.CurrentDispatcher.Invoke(() => Dispatcher.CurrentDispatcher.Invoke(() =>
@ -92,9 +99,9 @@ public partial class LaunchViewModel : ObservableObject
ConsoleOutput += s + "\n"; ConsoleOutput += s + "\n";
ScrollNeeded?.Invoke(this, EventArgs.Empty); ScrollNeeded?.Invoke(this, EventArgs.Empty);
}); });
}); }
var onExit = new Action<int>(i => void OnExit(int i)
{ {
Dispatcher.CurrentDispatcher.Invoke(() => Dispatcher.CurrentDispatcher.Invoke(() =>
{ {
@ -103,17 +110,16 @@ public partial class LaunchViewModel : ObservableObject
ScrollNeeded?.Invoke(this, EventArgs.Empty); ScrollNeeded?.Invoke(this, EventArgs.Empty);
SetProcessRunning(false); SetProcessRunning(false);
}); });
}); }
var args = "\"" + Path.Combine(packagePath, "launch.py") + "\""; var args = "\"" + Path.Combine(packagePath, "launch.py") + "\"";
venvRunner.RunDetached(args, onConsoleOutput, onExit); venvRunner.RunDetached(args, OnConsoleOutput, OnExit);
SetProcessRunning(true); SetProcessRunning(true);
}); });
public void OnLoaded() public void OnLoaded()
{ {
SetProcessRunning(false);
LoadPackages(); LoadPackages();
if (InstalledPackages.Any() && settingsManager.Settings.ActiveInstalledPackage != null) if (InstalledPackages.Any() && settingsManager.Settings.ActiveInstalledPackage != null)
{ {
@ -134,9 +140,13 @@ public partial class LaunchViewModel : ObservableObject
} }
[RelayCommand] [RelayCommand]
private void Stop() private async Task Stop()
{ {
venvRunner?.Dispose(); venvRunner?.Dispose();
if (venvRunner?.Process != null)
{
await venvRunner.Process.WaitForExitAsync();
}
venvRunner = null; venvRunner = null;
SetProcessRunning(false); SetProcessRunning(false);
ConsoleOutput += $"{Environment.NewLine}Stopped process at {DateTimeOffset.Now}{Environment.NewLine}"; ConsoleOutput += $"{Environment.NewLine}Stopped process at {DateTimeOffset.Now}{Environment.NewLine}";
@ -149,13 +159,15 @@ public partial class LaunchViewModel : ObservableObject
{ {
return; return;
} }
clearingPackages = true;
InstalledPackages.Clear(); InstalledPackages.Clear();
foreach (var package in packages) foreach (var package in packages)
{ {
InstalledPackages.Add(package); InstalledPackages.Add(package);
} }
clearingPackages = false;
} }
private void SetProcessRunning(bool running) private void SetProcessRunning(bool running)

Loading…
Cancel
Save