feat: added AvaloniaEdit for the text editor with basic highlight an line count, better "about" dialog and adde textmate

This commit is contained in:
2026-04-09 17:09:01 +02:00
parent d6d621dc92
commit cbf0891ba4
51 changed files with 564 additions and 17 deletions

View File

@ -1,16 +1,31 @@
using System.ComponentModel;
using Avalonia.Controls;
using Avalonia.Interactivity;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using GsaEditor.ViewModels;
using TextMateSharp.Grammars;
namespace GsaEditor.Views;
public partial class MainWindow : Window
{
private bool _forceClose;
private TextEditor? _textEditor;
private TextMate.Installation? _textMateInstall;
private readonly RegistryOptions _registryOptions;
public MainWindow()
{
InitializeComponent();
_registryOptions = new RegistryOptions(ThemeName.DarkPlus);
_textEditor = this.FindControl<TextEditor>("TextEditor");
if (_textEditor != null)
{
_textMateInstall = _textEditor.InstallTextMate(_registryOptions);
}
}
protected override void OnLoaded(RoutedEventArgs e)
@ -20,9 +35,59 @@ public partial class MainWindow : Window
if (DataContext is MainWindowViewModel vm)
{
vm.SetWindow(this);
vm.PropertyChanged += ViewModel_PropertyChanged;
}
}
private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (sender is not MainWindowViewModel vm) return;
if (e.PropertyName == nameof(MainWindowViewModel.PreviewText))
{
if (_textEditor != null && vm.PreviewText != null)
{
_textEditor.Text = vm.PreviewText;
}
else if (_textEditor != null)
{
_textEditor.Text = string.Empty;
}
}
if (e.PropertyName == nameof(MainWindowViewModel.SelectedEntry))
{
ApplySyntaxHighlighting(vm);
}
}
private void ApplySyntaxHighlighting(MainWindowViewModel vm)
{
if (_textMateInstall == null || vm.SelectedEntry == null) return;
var alias = vm.SelectedEntry.Alias;
var ext = System.IO.Path.GetExtension(alias).ToLowerInvariant();
// Map file extension to a TextMate language scope
var language = _registryOptions.GetLanguageByExtension(ext);
if (language != null)
{
_textMateInstall.SetGrammar(_registryOptions.GetScopeByLanguageId(language.Id));
}
else
{
_textMateInstall.SetGrammar(null);
}
}
/// <summary>
/// Called by the ViewModel to retrieve the current text from the editor.
/// </summary>
public string? GetEditorText()
{
return _textEditor?.Text;
}
private void TreeView_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (DataContext is MainWindowViewModel vm && sender is TreeView tree)