mirror of
https://github.com/BlossomiShymae/Needlework.Net.git
synced 2025-12-06 18:20:47 +01:00
WIP
This commit is contained in:
@@ -3,9 +3,10 @@ using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Styling;
|
||||
using AvaloniaEdit;
|
||||
using AvaloniaEdit.Highlighting;
|
||||
using AvaloniaEdit.Indentation.CSharp;
|
||||
using AvaloniaEdit.TextMate;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Needlework.Net.Desktop.Extensions;
|
||||
using Needlework.Net.Desktop.Messages;
|
||||
using Needlework.Net.Desktop.ViewModels;
|
||||
using SukiUI;
|
||||
using System.Text.Json;
|
||||
@@ -13,35 +14,58 @@ using TextMateSharp.Grammars;
|
||||
|
||||
namespace Needlework.Net.Desktop.Views;
|
||||
|
||||
public partial class ConsoleView : UserControl
|
||||
public partial class ConsoleView : UserControl, IRecipient<ResponseUpdatedMessage>, IRecipient<ContentRequestMessage>
|
||||
{
|
||||
private TextEditor? _responseEditor;
|
||||
private TextEditor? _requestEditor;
|
||||
|
||||
public ConsoleView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void Receive(ResponseUpdatedMessage message)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(message.Value))
|
||||
{
|
||||
var text = JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(message.Value), App.JsonSerializerOptions);
|
||||
if (text.Length >= App.MaxCharacters)
|
||||
{
|
||||
WeakReferenceMessenger.Default.Send(new OopsiesWindowRequestedMessage(text), nameof(ConsoleView));
|
||||
_responseEditor!.Text = string.Empty;
|
||||
}
|
||||
else _responseEditor!.Text = text;
|
||||
}
|
||||
else _responseEditor!.Text = message.Value;
|
||||
}
|
||||
|
||||
public void Receive(ContentRequestMessage message)
|
||||
{
|
||||
message.Reply(_requestEditor!.Text);
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
_responseEditor = this.FindControl<TextEditor>("ResponseEditor");
|
||||
_responseEditor!.TextArea.IndentationStrategy = new CSharpIndentationStrategy(_responseEditor.Options);
|
||||
_responseEditor!.TextArea.RightClickMovesCaret = true;
|
||||
_responseEditor!.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("JavaScript");
|
||||
_requestEditor = this.FindControl<TextEditor>("RequestEditor");
|
||||
_responseEditor?.ApplyJsonEditorSettings();
|
||||
_requestEditor?.ApplyJsonEditorSettings();
|
||||
|
||||
((ConsoleViewModel)DataContext!)!.ResponseBodyUpdated += ConsoleView_ResponseBodyUpdated;
|
||||
WeakReferenceMessenger.Default.Register<ResponseUpdatedMessage, string>(this, nameof(ConsoleViewModel));
|
||||
WeakReferenceMessenger.Default.Register<ContentRequestMessage, string>(this, "ConsoleRequestEditor");
|
||||
|
||||
OnBaseThemeChanged(Application.Current!.ActualThemeVariant);
|
||||
SukiTheme.GetInstance().OnBaseThemeChanged += OnBaseThemeChanged;
|
||||
}
|
||||
|
||||
private void ConsoleView_ResponseBodyUpdated(object? sender, TextUpdatedEventArgs e)
|
||||
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(e.Text))
|
||||
_responseEditor!.Text = JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(e.Text), App.JsonSerializerOptions);
|
||||
else _responseEditor!.Text = e.Text;
|
||||
base.OnDetachedFromVisualTree(e);
|
||||
|
||||
WeakReferenceMessenger.Default.UnregisterAll(this);
|
||||
SukiTheme.GetInstance().OnBaseThemeChanged -= OnBaseThemeChanged;
|
||||
}
|
||||
|
||||
private void OnBaseThemeChanged(ThemeVariant currentTheme)
|
||||
@@ -49,8 +73,11 @@ public partial class ConsoleView : UserControl
|
||||
var registryOptions = new RegistryOptions(
|
||||
currentTheme == ThemeVariant.Dark ? ThemeName.DarkPlus : ThemeName.LightPlus);
|
||||
|
||||
var textMateInstallation = _responseEditor.InstallTextMate(registryOptions);
|
||||
textMateInstallation.SetGrammar(registryOptions.GetScopeByLanguageId(registryOptions
|
||||
var responseTmi = _responseEditor.InstallTextMate(registryOptions);
|
||||
responseTmi.SetGrammar(registryOptions.GetScopeByLanguageId(registryOptions
|
||||
.GetLanguageByExtension(".json").Id));
|
||||
var requestTmi = _requestEditor.InstallTextMate(registryOptions);
|
||||
requestTmi.SetGrammar(registryOptions.GetScopeByLanguageId(registryOptions
|
||||
.GetLanguageByExtension(".json").Id));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user