diff --git a/Papercut.Core/RoslynHost.cs b/Papercut.Core/RoslynHost.cs
index 0146bbf..abbb3a1 100644
--- a/Papercut.Core/RoslynHost.cs
+++ b/Papercut.Core/RoslynHost.cs
@@ -1,5 +1,4 @@
-using System;
-using Microsoft.CodeAnalysis.CSharp.Scripting;
+using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
namespace Papercut.Core;
@@ -20,21 +19,8 @@ public class RoslynHost
ScriptOptions options = ScriptOptions.Default
.AddImports(imports);
- try
- {
- var result = await CSharpScript.RunAsync(code, options, globals,
- cancellationToken: cancellationToken);
- }
- catch (CompilationErrorException ex)
- {
- Console.WriteLine(code);
- Console.WriteLine(ex);
- }
- catch (Exception ex)
- {
- Console.WriteLine(code);
- Console.WriteLine(ex);
- }
+ await CSharpScript.RunAsync(code, options, globals,
+ cancellationToken: cancellationToken);
}
}
diff --git a/Papercut.Macos/AppDelegate.cs b/Papercut.Macos/AppDelegate.cs
index 868b9ee..0373dbf 100644
--- a/Papercut.Macos/AppDelegate.cs
+++ b/Papercut.Macos/AppDelegate.cs
@@ -1,10 +1,10 @@
-using AppKit;
+using AppKit;
using Foundation;
namespace Papercut.Macos;
[Register("AppDelegate")]
-public class AppDelegate : NSApplicationDelegate
+public partial class AppDelegate : NSApplicationDelegate
{
public AppDelegate()
{
@@ -19,4 +19,17 @@ public class AppDelegate : NSApplicationDelegate
{
// Insert code here to tear down your application
}
+
+ partial void RunScriptMenuItemClicked(AppKit.NSMenuItem sender)
+ {
+ ScriptRunner.RunScript(
+ NSApplication.SharedApplication.KeyWindow.WindowController
+ as MainWindow);
+ }
+
+ [Action("validateMenuItem:")]
+ public bool ValidateMenuItem(AppKit.NSMenuItem sender)
+ {
+ return true;
+ }
}
diff --git a/Papercut.Macos/ScriptViewController.designer.cs b/Papercut.Macos/AppDelegate.designer.cs
similarity index 61%
rename from Papercut.Macos/ScriptViewController.designer.cs
rename to Papercut.Macos/AppDelegate.designer.cs
index 1171a34..61298c4 100644
--- a/Papercut.Macos/ScriptViewController.designer.cs
+++ b/Papercut.Macos/AppDelegate.designer.cs
@@ -9,18 +9,13 @@ using System.CodeDom.Compiler;
namespace Papercut.Macos
{
- [Register ("ScriptViewController")]
- partial class ScriptViewController
+ partial class AppDelegate
{
- [Outlet]
- AppKit.NSTextView ScriptTextBox { get; set; }
+ [Action ("RunScriptMenuItemClicked:")]
+ partial void RunScriptMenuItemClicked (AppKit.NSMenuItem sender);
void ReleaseDesignerOutlets ()
{
- if (ScriptTextBox != null) {
- ScriptTextBox.Dispose ();
- ScriptTextBox = null;
- }
}
}
}
diff --git a/Papercut.Macos/Main.storyboard b/Papercut.Macos/Main.storyboard
index a65a1f4..0cc9b9c 100644
--- a/Papercut.Macos/Main.storyboard
+++ b/Papercut.Macos/Main.storyboard
@@ -6,17 +6,17 @@
-
+
-
+
-
@@ -686,7 +423,7 @@
-
+
@@ -746,7 +483,7 @@
-
+
@@ -781,6 +518,16 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/Papercut.Macos/MainWindow.cs b/Papercut.Macos/MainWindow.cs
index 365f31a..4d19979 100644
--- a/Papercut.Macos/MainWindow.cs
+++ b/Papercut.Macos/MainWindow.cs
@@ -1,12 +1,13 @@
using System;
-using System.Threading.Tasks;
+using System.Threading;
using AppKit;
-using Papercut.Core;
namespace Papercut.Macos;
public partial class MainWindow : NSWindowController
{
+ #region Window components
+
private NSSplitViewController MainContentController
{
get => (this.ContentViewController as NSSplitViewController)
@@ -26,15 +27,25 @@ public partial class MainWindow : NSWindowController
.ContentView.DocumentView as NSTextView;
}
- private NSTextView ScriptOutputTextBox
+ public NSTextView ScriptOutputTextBox
{
get => (this.MainContentController
.SplitViewItems[1].ViewController.View as NSScrollView)
.ContentView.DocumentView as NSTextView;
}
+ public string ScriptText { get => this.ScriptEditorTextBox.Value; }
- public MainWindow (IntPtr handle) : base (handle) { }
+ #endregion
+
+ #region Shared properties
+
+ public CancellationTokenSource ScriptCancellationTokenSource { get; set; }
+
+ #endregion
+
+
+ public MainWindow (ObjCRuntime.NativeHandle handle) : base (handle) { }
public override void AwakeFromNib()
{
@@ -55,27 +66,23 @@ public partial class MainWindow : NSWindowController
public void RunScript(object sender, EventArgs e)
{
- this.RunScriptToolbarButton.Enabled = false;
- this.ScriptOutputTextBox.Value = "";
- TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();
+ ScriptRunner.RunScript(this);
+ }
- PapercutWriter writer = new(obj =>
+ public void CancelScript(object sender, EventArgs e)
+ {
+ this.ScriptCancellationTokenSource?.Cancel();
+ }
+
+ public void SetScriptRunState(bool scriptIsRunning)
+ {
+ if (scriptIsRunning)
{
- if (obj is string str)
- {
- this.BeginInvokeOnMainThread(() =>
- this.ScriptOutputTextBox.Value += str + "\n");
- }
-
- return Task.CompletedTask;
- });
-
- string script = this.ScriptEditorTextBox.Value;
-
- Task _ = Task
- .Run(() => RoslynHost.RunScript(script, Array.Empty(),
- new RoslynHostGlobals(writer)))
- .ContinueWith((t) => this.RunScriptToolbarButton.Enabled = true,
- uiThread);
+ this.RunScriptToolbarButton.Enabled = false;
+ }
+ else
+ {
+ this.RunScriptToolbarButton.Enabled = true;
+ }
}
}
diff --git a/Papercut.Macos/Papercut.Macos.csproj b/Papercut.Macos/Papercut.Macos.csproj
index 0336e74..e1d105f 100644
--- a/Papercut.Macos/Papercut.Macos.csproj
+++ b/Papercut.Macos/Papercut.Macos.csproj
@@ -36,8 +36,8 @@
MainWindow.cs
-
- ScriptViewController.cs
+
+ AppDelegate.cs
diff --git a/Papercut.Macos/ScriptRunner.cs b/Papercut.Macos/ScriptRunner.cs
new file mode 100644
index 0000000..a2aed13
--- /dev/null
+++ b/Papercut.Macos/ScriptRunner.cs
@@ -0,0 +1,51 @@
+using System;
+using Papercut.Core;
+using System.Threading;
+using System.Threading.Tasks;
+using AppKit;
+
+namespace Papercut.Macos;
+
+public static class ScriptRunner
+{
+ public static void RunScript(MainWindow window)
+ {
+ window.SetScriptRunState(true);
+ window.ScriptOutputTextBox.Value = "";
+ TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();
+
+ PapercutWriter writer = new(obj =>
+ {
+ if (obj is string str)
+ {
+ window.BeginInvokeOnMainThread(() =>
+ window.ScriptOutputTextBox.Value += str + "\n");
+ }
+
+ return Task.CompletedTask;
+ });
+
+ window.ScriptCancellationTokenSource = new CancellationTokenSource();
+
+ string script = window.ScriptText;
+
+ Task task = Task
+ .Run(async () =>
+ {
+ try
+ {
+ await RoslynHost.RunScript(script, Array.Empty(),
+ new RoslynHostGlobals(writer),
+ window.ScriptCancellationTokenSource.Token);
+ }
+ catch (Exception ex)
+ {
+ window.BeginInvokeOnMainThread(() =>
+ window.ScriptOutputTextBox.Value += ex.ToString());
+ }
+ }, window.ScriptCancellationTokenSource.Token)
+ .ContinueWith((t) => window.SetScriptRunState(false),
+ uiThread);
+ }
+}
+
diff --git a/Papercut.Macos/ScriptViewController.cs b/Papercut.Macos/ScriptViewController.cs
deleted file mode 100644
index 8766301..0000000
--- a/Papercut.Macos/ScriptViewController.cs
+++ /dev/null
@@ -1,18 +0,0 @@
-// This file has been autogenerated from a class added in the UI designer.
-
-using System;
-
-using Foundation;
-using AppKit;
-using System.Diagnostics;
-
-namespace Papercut.Macos
-{
- public partial class ScriptViewController : NSViewController
- {
- public ScriptViewController (IntPtr handle) : base (handle)
- {
- }
-
- }
-}