Browse Source

Merge pull request #26 from ionite34/toast

pull/5/head
Ionite 2 years ago committed by GitHub
parent
commit
fc1cb0df42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      StabilityMatrix/App.xaml.cs
  2. 6
      StabilityMatrix/Helper/ProcessRunner.cs
  3. 4
      StabilityMatrix/Models/BasePackage.cs
  4. 11
      StabilityMatrix/Models/Packages/A3WebUI.cs
  5. 4
      StabilityMatrix/PyRunner.cs
  6. 13
      StabilityMatrix/PyVenvRunner.cs
  7. 2
      StabilityMatrix/StabilityMatrix.csproj
  8. 2
      StabilityMatrix/ViewModels/ExceptionWindowViewModel.cs
  9. 2
      StabilityMatrix/ViewModels/InstallerViewModel.cs
  10. 29
      StabilityMatrix/ViewModels/LaunchViewModel.cs

1
StabilityMatrix/App.xaml.cs

@ -13,7 +13,6 @@ using StabilityMatrix.Services;
using StabilityMatrix.ViewModels;
using Wpf.Ui.Contracts;
using Wpf.Ui.Services;
using SetupBuilderExtensions = NLog.SetupBuilderExtensions;
namespace StabilityMatrix
{

6
StabilityMatrix/Helper/ProcessRunner.cs

@ -32,6 +32,7 @@ public static class ProcessRunner
public static Process StartProcess(
string fileName,
string arguments,
string? workingDirectory = null,
Action<string?>? outputDataReceived = null,
Dictionary<string, string>? environmentVariables = null)
{
@ -52,6 +53,11 @@ public static class ProcessRunner
}
}
if (workingDirectory != null)
{
process.StartInfo.WorkingDirectory = workingDirectory;
}
if (outputDataReceived != null)
{
process.OutputDataReceived += (_, args) => outputDataReceived(args.Data);

4
StabilityMatrix/Models/BasePackage.cs

@ -28,7 +28,7 @@ public abstract class BasePackage
public event EventHandler<string>? InstallComplete;
public event EventHandler<string>? ConsoleOutput;
public event EventHandler<int>? Exited;
public event EventHandler? StartupComplete;
public event EventHandler<string>? StartupComplete;
public void OnDownloadProgressChanged(int progress) => DownloadProgressChanged?.Invoke(this, progress);
public void OnDownloadComplete(string path) => DownloadComplete?.Invoke(this, path);
@ -36,7 +36,7 @@ public abstract class BasePackage
public void OnInstallComplete(string path) => InstallComplete?.Invoke(this, path);
public void OnConsoleOutput(string output) => ConsoleOutput?.Invoke(this, output);
public void OnExit(int exitCode) => Exited?.Invoke(this, exitCode);
public void OnStartupComplete() => StartupComplete?.Invoke(this, EventArgs.Empty);
public void OnStartupComplete(string url) => StartupComplete?.Invoke(this, url);
public string ByAuthor => $"By {Author}";
}

11
StabilityMatrix/Models/Packages/A3WebUI.cs

@ -26,6 +26,7 @@ public class A3WebUI : BasePackage
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private string webUrl = string.Empty;
public override async Task DownloadPackage()
{
@ -106,7 +107,11 @@ public class A3WebUI : BasePackage
if (s == null) return;
if (s.Contains("model loaded", StringComparison.OrdinalIgnoreCase))
{
OnStartupComplete();
OnStartupComplete(webUrl);
}
if (s.Contains("Running on", StringComparison.OrdinalIgnoreCase))
{
webUrl = s.Split(" ")[5];
}
Debug.WriteLine($"process stdout: {s}");
OnConsoleOutput($"{s}\n");
@ -118,9 +123,9 @@ public class A3WebUI : BasePackage
OnConsoleOutput($"Venv process exited with code {i}");
}
var args = $"\"{Path.Combine(installedPackagePath, LaunchCommand)}\"";
var args = $"\"{Path.Combine(installedPackagePath, LaunchCommand)}{arguments}\"";
venvRunner.RunDetached(args.TrimEnd(), HandleConsoleOutput, HandleExit);
venvRunner.RunDetached(args.TrimEnd(), HandleConsoleOutput, HandleExit, workingDirectory: installedPackagePath);
}
public override Task Shutdown()

4
StabilityMatrix/PyRunner.cs

@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using Python.Runtime;
using StabilityMatrix.Exceptions;
using StabilityMatrix.Helper;
namespace StabilityMatrix;

13
StabilityMatrix/PyVenvRunner.cs

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using NLog;
using NLog.Fluent;
using StabilityMatrix.Helper;
namespace StabilityMatrix;
@ -69,7 +68,8 @@ 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, string workingDirectory = "")
{
if (!Exists())
{
@ -77,19 +77,22 @@ public class PyVenvRunner : IDisposable
}
Logger.Debug($"Launching RunDetached at {PythonPath} with args {arguments}");
if (unbuffered)
{
var env = new Dictionary<string, string>
{
{"PYTHONUNBUFFERED", "1"}
};
Process = ProcessRunner.StartProcess(PythonPath, "-u " + arguments, outputDataReceived, env);
Process = ProcessRunner.StartProcess(PythonPath, "-u " + arguments, workingDirectory, outputDataReceived,
env);
}
else
{
Process = ProcessRunner.StartProcess(PythonPath, arguments, outputDataReceived);
Process = ProcessRunner.StartProcess(PythonPath, arguments, outputDataReceived: outputDataReceived,
workingDirectory: workingDirectory);
}
if (onExit != null)
{
Process.Exited += (_, _) => onExit(Process.ExitCode);

2
StabilityMatrix/StabilityMatrix.csproj

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<SelfContained>true</SelfContained>

2
StabilityMatrix/ViewModels/ExceptionWindowViewModel.cs

@ -1,6 +1,4 @@
using System;
using System.Windows;
using CommunityToolkit.Mvvm.Input;
namespace StabilityMatrix.ViewModels;

2
StabilityMatrix/ViewModels/InstallerViewModel.cs

@ -3,9 +3,7 @@ using System.Collections.ObjectModel;
using System.Threading.Tasks;
using StabilityMatrix.Helper;
using StabilityMatrix.Models;
using System.ComponentModel;
using System.Diagnostics;
using StabilityMatrix.Models.Packages;
using System.Linq;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;

29
StabilityMatrix/ViewModels/LaunchViewModel.cs

@ -1,20 +1,15 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Toolkit.Uwp.Notifications;
using StabilityMatrix.Helper;
using StabilityMatrix.Models;
using Wpf.Ui.Appearance;
namespace StabilityMatrix.ViewModels;
@ -64,6 +59,16 @@ public partial class LaunchViewModel : ObservableObject
{
this.settingsManager = settingsManager;
SetProcessRunning(false);
ToastNotificationManagerCompat.OnActivated += ToastNotificationManagerCompatOnOnActivated;
}
private void ToastNotificationManagerCompatOnOnActivated(ToastNotificationActivatedEventArgsCompat e)
{
if (e.Argument.StartsWith("http"))
{
Process.Start(new ProcessStartInfo(e.Argument) { UseShellExecute = true });
}
}
public AsyncRelayCommand LaunchCommand => new(async () =>
@ -95,14 +100,12 @@ public partial class LaunchViewModel : ObservableObject
SetProcessRunning(true);
});
private void RunningPackageOnStartupComplete(object? sender, EventArgs e)
private void RunningPackageOnStartupComplete(object? sender, string url)
{
Process.Start(new ProcessStartInfo
{
FileName = "http://localhost:7860",
UseShellExecute = true
});
new ToastContentBuilder()
.AddText("Stable Diffusion Web UI ready to go!")
.AddButton("Launch Web UI", ToastActivationType.Foreground, url)
.Show();
}
public void OnLoaded()

Loading…
Cancel
Save