2025-12-21 03:19:53 +11:00
|
|
|
using Avalonia;
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Markup.Xaml;
|
2025-12-26 22:49:47 +11:00
|
|
|
using Avalonia.Media.Imaging;
|
2025-12-24 00:55:56 +11:00
|
|
|
using Avalonia.Platform.Storage;
|
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
using Progrart.Controls.TabSystem;
|
2025-12-25 23:00:26 +11:00
|
|
|
using Progrart.Core;
|
|
|
|
|
using Progrart.Core.JSExecution;
|
2026-01-04 21:14:17 +11:00
|
|
|
using Progrart.Core.Storage;
|
2025-12-24 00:55:56 +11:00
|
|
|
using Progrart.Pages;
|
2025-12-30 00:51:01 +11:00
|
|
|
using System;
|
2025-12-25 23:00:26 +11:00
|
|
|
using System.Diagnostics;
|
2026-01-10 03:40:54 +11:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2025-12-24 00:55:56 +11:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-12-21 03:19:53 +11:00
|
|
|
|
|
|
|
|
namespace Progrart;
|
|
|
|
|
|
2025-12-25 23:00:26 +11:00
|
|
|
public partial class ProgrartEditorPage : UserControl, ITabPage, IEditorPage
|
2025-12-21 03:19:53 +11:00
|
|
|
{
|
2025-12-24 00:55:56 +11:00
|
|
|
IStorageFile? file = null;
|
|
|
|
|
TabButton? btn = null;
|
2026-01-02 03:24:33 +11:00
|
|
|
Bitmap? image;
|
2026-01-07 03:19:32 +11:00
|
|
|
string lastSave = "";
|
2025-12-24 00:55:56 +11:00
|
|
|
public ProgrartEditorPage()
|
2025-12-25 23:00:26 +11:00
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2025-12-28 23:23:33 +11:00
|
|
|
CodeEditor.onSaveCmd = Save;
|
2025-12-25 23:00:26 +11:00
|
|
|
}
|
2025-12-24 00:55:56 +11:00
|
|
|
|
|
|
|
|
public void BindButton(TabButton button)
|
|
|
|
|
{
|
|
|
|
|
btn = button;
|
|
|
|
|
button.Title = "Editor Page";
|
|
|
|
|
if (file is not null)
|
|
|
|
|
{
|
|
|
|
|
btn.Title = file.Name;
|
|
|
|
|
var path = file.TryGetLocalPath();
|
|
|
|
|
btn.TooltipText = path;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-07 03:19:32 +11:00
|
|
|
bool isRunning = false;
|
2025-12-24 00:55:56 +11:00
|
|
|
public void Execute(ExecuteArguments? args = null)
|
2025-12-25 23:00:26 +11:00
|
|
|
{
|
2026-01-07 03:19:32 +11:00
|
|
|
if (isRunning) return;
|
|
|
|
|
string src = CodeEditor.Text;
|
|
|
|
|
ProgressRing.IsVisible = true;
|
|
|
|
|
Task.Run(() =>
|
2025-12-25 23:00:26 +11:00
|
|
|
{
|
2026-01-07 03:19:32 +11:00
|
|
|
AvaloniaStorageProvider provider = new AvaloniaStorageProvider(App.CurrentOpenFolder);
|
|
|
|
|
using ProgrartExecutor executor = new ProgrartExecutor(provider);
|
|
|
|
|
try
|
2025-12-28 23:03:33 +11:00
|
|
|
{
|
2026-01-07 03:19:32 +11:00
|
|
|
int Scale = 1024;
|
|
|
|
|
if (args is not null)
|
2025-12-28 23:03:33 +11:00
|
|
|
{
|
2026-01-07 03:19:32 +11:00
|
|
|
if (args.data.TryGetValue("Scale", out var scale))
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(scale, out Scale)) Scale = 1024;
|
|
|
|
|
}
|
2025-12-28 23:03:33 +11:00
|
|
|
}
|
2026-01-07 03:19:32 +11:00
|
|
|
var result = executor.RenderImage(Scale, src, args ?? new ExecuteArguments());
|
|
|
|
|
var data = result.DrawingCore.ToData();
|
|
|
|
|
using MemoryStream stream = new MemoryStream();
|
|
|
|
|
data.SaveTo(stream);
|
|
|
|
|
stream.Flush();
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
Bitmap image = new Bitmap(stream);
|
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
ProgressRing.IsVisible = false;
|
|
|
|
|
PreviewImage.SetImage(image);
|
|
|
|
|
this.image?.Dispose();
|
|
|
|
|
this.image = image;
|
|
|
|
|
isRunning = false;
|
|
|
|
|
});
|
2025-12-28 23:03:33 +11:00
|
|
|
}
|
2026-01-07 03:19:32 +11:00
|
|
|
catch (System.Exception e)
|
2025-12-28 23:03:33 +11:00
|
|
|
{
|
2026-01-07 03:19:32 +11:00
|
|
|
Trace.WriteLine(e);
|
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
ProgressRing.IsVisible = false;
|
|
|
|
|
});
|
|
|
|
|
isRunning = false;
|
2025-12-28 23:03:33 +11:00
|
|
|
}
|
2026-01-07 03:19:32 +11:00
|
|
|
});
|
2025-12-25 23:00:26 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsModified()
|
|
|
|
|
{
|
2026-01-07 03:19:32 +11:00
|
|
|
return lastSave == CodeEditor.Text;
|
2025-12-25 23:00:26 +11:00
|
|
|
}
|
2025-12-24 00:55:56 +11:00
|
|
|
|
2025-12-30 00:51:01 +11:00
|
|
|
public bool IsSameFile(IStorageFile file)
|
|
|
|
|
{
|
|
|
|
|
return (this.file?.Path == file.Path);
|
|
|
|
|
}
|
|
|
|
|
public void LoadDocument(IStorageFile file)
|
2025-12-24 00:55:56 +11:00
|
|
|
{
|
|
|
|
|
this.file = file;
|
|
|
|
|
if (btn is not null)
|
|
|
|
|
{
|
|
|
|
|
btn.Title = file.Name;
|
|
|
|
|
var path = file.TryGetLocalPath();
|
|
|
|
|
btn.TooltipText = path;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
CodeEditor.SetGrammerByExtension(".js");
|
|
|
|
|
}
|
2026-01-02 03:24:33 +11:00
|
|
|
Trace.WriteLine($"File:{file.TryGetLocalPath() ?? "null"}");
|
2025-12-24 00:55:56 +11:00
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
2026-01-10 03:40:54 +11:00
|
|
|
try
|
2025-12-24 00:55:56 +11:00
|
|
|
{
|
2026-01-10 03:40:54 +11:00
|
|
|
using var stream = await file.OpenReadAsync();
|
|
|
|
|
using StreamReader sr = new StreamReader(stream);
|
|
|
|
|
var text = await sr.ReadToEndAsync();
|
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CodeEditor.Text = text;
|
|
|
|
|
lastSave = text;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Trace.WriteLine(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Trace.WriteLine(e);
|
|
|
|
|
}
|
2025-12-24 00:55:56 +11:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 23:02:12 +11:00
|
|
|
public async Task Save()
|
2025-12-24 00:55:56 +11:00
|
|
|
{
|
2025-12-28 23:03:33 +11:00
|
|
|
if (file is not null)
|
|
|
|
|
{
|
2026-01-01 23:02:12 +11:00
|
|
|
string content = "";
|
|
|
|
|
await Dispatcher.UIThread.InvokeAsync(() => { content = CodeEditor.Text; });
|
2026-01-07 03:19:32 +11:00
|
|
|
lastSave = content;
|
2026-01-01 23:02:12 +11:00
|
|
|
await Task.Run(async () =>
|
2025-12-28 23:03:33 +11:00
|
|
|
{
|
|
|
|
|
using var stream = await file.OpenWriteAsync();
|
|
|
|
|
stream.SetLength(0);
|
|
|
|
|
using var sw = new StreamWriter(stream);
|
|
|
|
|
await sw.WriteAsync(content);
|
|
|
|
|
await sw.FlushAsync();
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-24 00:55:56 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetHost(TabHost host)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2025-12-21 03:19:53 +11:00
|
|
|
}
|