feat: added AvaloniaEdit for the text editor with basic highlight an line count, better "about" dialog and adde textmate
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
xmlns:vm="using:GsaEditor.ViewModels"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:aedit="https://github.com/avaloniaui/avaloniaedit"
|
||||
mc:Ignorable="d" d:DesignWidth="1100" d:DesignHeight="700"
|
||||
x:Class="GsaEditor.Views.MainWindow"
|
||||
x:CompileBindings="False"
|
||||
@ -199,13 +200,12 @@
|
||||
<Button Content="Cancel" Command="{Binding CancelEditCommand}"
|
||||
IsVisible="{Binding IsEditingText}"/>
|
||||
</StackPanel>
|
||||
<TextBox Text="{Binding PreviewText, Mode=TwoWay}"
|
||||
IsReadOnly="{Binding !IsEditingText}"
|
||||
FontFamily="Consolas, Courier New, monospace"
|
||||
FontSize="13"
|
||||
AcceptsReturn="True"
|
||||
TextWrapping="NoWrap"
|
||||
Margin="10,0,10,10"/>
|
||||
<aedit:TextEditor Name="TextEditor"
|
||||
IsReadOnly="{Binding !IsEditingText}"
|
||||
FontFamily="Consolas, Courier New, monospace"
|
||||
FontSize="13"
|
||||
ShowLineNumbers="True"
|
||||
Margin="10,0,10,10"/>
|
||||
</DockPanel>
|
||||
|
||||
<!-- Image Preview -->
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user