OutGridTree/AvaloniaManager.cs
+42
-0
diff --git a/OutGridTree/AvaloniaManager.cs b/OutGridTree/AvaloniaManager.cs
new file mode 100644
index 0000000..accb3b5
@@ -0,0 +1,42 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Threading;
namespace OutGridTree;
internal static class AvaloniaManager
{
private static readonly Lazy<Task> appBuilder = new(
static () =>
{
var tcs = new TaskCompletionSource<object>();
var t = new Thread(() =>
AppBuilder
.Configure<Application>()
.UsePlatformDetect()
.AfterPlatformServicesSetup(_ => tcs.SetResult(new()))
.Start((app, _) => app.Run(CancellationToken.None), [])
);
t.Start();
return tcs.Task;
},
LazyThreadSafetyMode.ExecutionAndPublication
);
public static TWindow OpenWindow<TWindow>()
where TWindow : Window, new()
{
appBuilder.Value.Wait();
return Dispatcher.UIThread.Invoke(() =>
{
var window = new TWindow();
window.Show();
return window;
});
}
}
OutGridTree/GridTreeWindow.cs
+22
-0
diff --git a/OutGridTree/GridTreeWindow.cs b/OutGridTree/GridTreeWindow.cs
new file mode 100644
index 0000000..49f1adb
@@ -0,0 +1,22 @@
using Avalonia.Controls;
using Avalonia.Layout;
namespace OutGridTree;
internal sealed class GridTreeWindow : Window
{
public GridTreeWindow()
{
Title = "Out-GridTree";
Width = 1080;
Height = 720;
WindowStartupLocation = WindowStartupLocation.CenterScreen;
Content = new TextBlock()
{
Text = "Out-GridTree",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
}
}
OutGridTree/OutGridTree.cs
+3
-42
diff --git a/OutGridTree/OutGridTree.cs b/OutGridTree/OutGridTree.cs
index fa65f3e..0527352 100644
@@ -1,10 +1,5 @@
using System;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Threading;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
namespace OutGridTree;
@@ -15,44 +10,11 @@ public sealed class OutGridTree : Cmdlet
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public PSObject InputObject { get; set; } = AutomationNull.Value;
private ClassicDesktopStyleApplicationLifetime? lifetime;
private Application? application;
private Thread? uiThread;
private GridTreeWindow? window;
protected override void BeginProcessing()
{
uiThread = new Thread(RunUi);
uiThread.Start();
}
private void RunUi()
{
lifetime = new ClassicDesktopStyleApplicationLifetime();
AppBuilder
.Configure(() =>
{
application = new();
return application;
})
.UsePlatformDetect()
.SetupWithLifetime(lifetime);
lifetime.MainWindow = new Window() { Title = "Out-GridTree" };
if (application is null)
{
ThrowTerminatingError(
new(
new InvalidOperationException("Could not initialize UI system."),
"Initialize UI system",
ErrorCategory.InvalidOperation,
null
)
);
return;
}
application.Run(lifetime.MainWindow);
window = AvaloniaManager.OpenWindow<GridTreeWindow>();
}
protected override void ProcessRecord()
@@ -65,7 +27,6 @@ public sealed class OutGridTree : Cmdlet
{
// TODO: Wait for window to close
WriteDebug("End processing");
uiThread?.Join();
WriteDebug("Exited");
}
@@ -73,7 +34,7 @@ public sealed class OutGridTree : Cmdlet
{
// TODO: Close window
WriteDebug("Stop processing");
lifetime?.Shutdown();
window?.Close();
WriteDebug("Shutdown");
}
}