Cauldron/Cauldron.Macos/MainWindow.cs

117 lines
2.9 KiB
C#
Raw Normal View History

2023-07-19 19:30:39 +00:00
using System;
2023-07-19 22:09:25 +00:00
using System.Threading;
2023-07-19 19:30:39 +00:00
using AppKit;
using Cauldron.Macos.SourceWriter;
using Cauldron.Macos.SourceWriter.LanguageFormats;
2023-07-19 19:30:39 +00:00
2023-07-21 20:11:56 +00:00
namespace Cauldron.Macos;
2023-07-19 19:30:39 +00:00
public partial class MainWindow : NSWindowController
{
2023-07-19 22:09:25 +00:00
#region Window components
2023-07-19 19:30:39 +00:00
private NSSplitViewController MainContentController
{
get => (this.ContentViewController as NSSplitViewController)
.SplitViewItems[1].ViewController as NSSplitViewController;
}
private NSViewController SidebarController
{
get => (this.ContentViewController as NSSplitViewController)
.SplitViewItems[0].ViewController;
}
2023-07-21 20:11:56 +00:00
private NSOutlineView SidebarList
{
get => (this.SidebarController.View.Subviews[0] as NSScrollView)
.ContentView.DocumentView as NSOutlineView;
}
private SourceTextView ScriptEditorTextBox
2023-07-19 19:30:39 +00:00
{
get => (this.MainContentController
.SplitViewItems[0].ViewController.View as NSScrollView)
.ContentView.DocumentView as SourceTextView;
2023-07-19 19:30:39 +00:00
}
2023-07-22 00:20:46 +00:00
public WebKit.WKWebView ScriptOutputWebView
2023-07-19 19:30:39 +00:00
{
2023-07-22 00:20:46 +00:00
get => this.MainContentController
.SplitViewItems[1].ViewController.View as WebKit.WKWebView;
2023-07-19 19:30:39 +00:00
}
2023-07-19 22:09:25 +00:00
public string ScriptText { get => this.ScriptEditorTextBox.Value; }
#endregion
#region Shared properties
public CancellationTokenSource ScriptCancellationTokenSource { get; set; }
#endregion
2023-07-19 19:30:39 +00:00
2023-07-19 22:09:25 +00:00
public MainWindow (ObjCRuntime.NativeHandle handle) : base (handle) { }
2023-07-19 19:30:39 +00:00
public override void AwakeFromNib()
{
base.AwakeFromNib();
this.RunScriptToolbarButton.Activated += RunScript;
SourceTextView scriptTextBox = this.ScriptEditorTextBox;
2023-07-19 19:30:39 +00:00
scriptTextBox.Font = NSFont.MonospacedSystemFont(new nfloat(14), NSFontWeight.Regular);
scriptTextBox.AutomaticQuoteSubstitutionEnabled = false;
scriptTextBox.AutomaticDashSubstitutionEnabled = false;
scriptTextBox.AutomaticDataDetectionEnabled = false;
scriptTextBox.AutomaticSpellingCorrectionEnabled = false;
scriptTextBox.AutomaticTextCompletionEnabled = false;
scriptTextBox.AutomaticTextReplacementEnabled = false;
scriptTextBox.AutomaticLinkDetectionEnabled = false;
scriptTextBox.Formatter = new LanguageFormatter(scriptTextBox, new CSharpDescriptor());
scriptTextBox.Formatter.Reformat();
2023-07-19 19:30:39 +00:00
}
public void RunScript(object sender, EventArgs e)
{
2023-07-19 22:09:25 +00:00
ScriptRunner.RunScript(this);
}
public void CancelScript(object sender, EventArgs e)
{
this.ScriptCancellationTokenSource?.Cancel();
}
2023-07-19 19:30:39 +00:00
2023-07-21 22:21:13 +00:00
partial void NewTabClicked(AppKit.NSToolbarItem sender)
{
this.CreateNewTab();
}
partial void NewTabMenuItemClicked(AppKit.NSMenuItem sender)
{
this.CreateNewTab();
}
2023-07-19 22:09:25 +00:00
public void SetScriptRunState(bool scriptIsRunning)
{
if (scriptIsRunning)
{
this.RunScriptToolbarButton.Enabled = false;
}
else
2023-07-19 19:30:39 +00:00
{
2023-07-19 22:09:25 +00:00
this.RunScriptToolbarButton.Enabled = true;
}
2023-07-19 19:30:39 +00:00
}
2023-07-21 22:21:13 +00:00
public void CreateNewTab()
{
MainWindow newWindow = this.Storyboard.InstantiateInitialController()
as MainWindow;
this.Window.AddTabbedWindow(newWindow.Window, NSWindowOrderingMode.Above);
this.Window.SelectNextTab(this);
}
2023-07-19 19:30:39 +00:00
}