Skip to content

Commit 6e1b180

Browse files
committed
initial AvaloniaEdit for rendering
1 parent 5bdf802 commit 6e1b180

5 files changed

Lines changed: 127 additions & 21 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using Avalonia;
3+
using Avalonia.Xaml.Interactivity;
4+
using AvaloniaEdit;
5+
using AvaloniaEdit.Utils;
6+
7+
namespace SharpFM.App.Behaviors;
8+
9+
public class DocumentTextBindingBehavior : Behavior<TextEditor>
10+
{
11+
private TextEditor _textEditor = null!;
12+
13+
public static readonly StyledProperty<string> TextProperty =
14+
AvaloniaProperty.Register<DocumentTextBindingBehavior, string>(nameof(Text));
15+
16+
public string Text
17+
{
18+
get => GetValue(TextProperty);
19+
set => SetValue(TextProperty, value);
20+
}
21+
22+
protected override void OnAttached()
23+
{
24+
base.OnAttached();
25+
26+
if (AssociatedObject is TextEditor textEditor)
27+
{
28+
_textEditor = textEditor;
29+
_textEditor.TextChanged += TextChanged;
30+
this.GetObservable(TextProperty).Subscribe(TextPropertyChanged);
31+
}
32+
}
33+
34+
protected override void OnDetaching()
35+
{
36+
base.OnDetaching();
37+
38+
if (_textEditor != null)
39+
{
40+
_textEditor.TextChanged -= TextChanged;
41+
}
42+
}
43+
44+
private void TextChanged(object sender, EventArgs eventArgs)
45+
{
46+
if (_textEditor != null && _textEditor.Document != null)
47+
{
48+
Text = _textEditor.Document.Text;
49+
}
50+
}
51+
52+
private void TextPropertyChanged(string text)
53+
{
54+
if (_textEditor != null && _textEditor.Document != null && text != null)
55+
{
56+
var caretOffset = _textEditor.CaretOffset;
57+
_textEditor.Document.Text = text;
58+
//_textEditor.CaretOffset = caretOffset;
59+
}
60+
}
61+
}

SharpFM.App/MainWindow.axaml

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:AvaloniaEdit="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit"
77
mc:Ignorable="d" d:DesignWidth="700" d:DesignHeight="500"
8+
xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity"
9+
xmlns:behaviors="clr-namespace:SharpFM.App.Behaviors;assembly=SharpFM.App"
810
x:DataType="vm:MainWindowViewModel"
911
x:Class="SharpFM.App.MainWindow"
1012
Title="SharpFM">
1113
<Grid>
1214
<Grid.ColumnDefinitions>
1315
<ColumnDefinition Width="100" />
1416
<ColumnDefinition Width="200" />
15-
<ColumnDefinition Width="300" />
16-
<ColumnDefinition Width="300" />
17+
<ColumnDefinition Width="*" />
1718
</Grid.ColumnDefinitions>
1819

19-
<Button
20-
Grid.Column="0"
21-
Command="{Binding PasteTextCommand}">
22-
Paste
20+
<StackPanel Grid.Column="0" >
21+
<Button Command="{Binding NewEmptyItemCommand}">
22+
New
2323
</Button>
2424

25+
<Button Command="{Binding PasteTextCommand}">
26+
Paste
27+
</Button>
28+
</StackPanel>
2529
<ListBox
2630
Grid.Column="1"
2731
ItemsSource="{Binding Keys}"
@@ -41,18 +45,17 @@
4145
</ListBox.ItemTemplate>
4246
</ListBox>
4347

44-
<StackPanel Grid.Column="2"
45-
Grid.ColumnSpan="2">
46-
47-
<TextBox
48-
Height="250"
49-
Text="{Binding SelectedClip.XmlData}" />
48+
<AvaloniaEdit:TextEditor
49+
Grid.Column="2"
50+
x:Name="avaloniaEditor"
51+
SyntaxHighlighting="Xml"
52+
WordWrap="False"
53+
ShowLineNumbers="True"
54+
FontFamily="Cascadia Code,Consolas,Menlo,Monospace">
55+
<i:Interaction.Behaviors>
56+
<behaviors:DocumentTextBindingBehavior Text="{Binding SelectedClip.XmlData, Mode=TwoWay}"/>
57+
</i:Interaction.Behaviors>
58+
</AvaloniaEdit:TextEditor>
5059

51-
<AvaloniaEdit:TextEditor
52-
Height="250"
53-
x:Name="avaloniaEditor"
54-
ShowLineNumbers="True"
55-
FontFamily="Cascadia Code,Consolas,Menlo,Monospace"/>
56-
</StackPanel>
5760
</Grid>
5861
</Window>

SharpFM.App/MainWindow.axaml.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22
using System.Collections.ObjectModel;
33
using Avalonia.Controls;
44
using AvaloniaEdit;
5+
using AvaloniaEdit.TextMate;
56
using SharpFM.Core;
7+
using TextMateSharp.Grammars;
68

79
namespace SharpFM.App;
810

911
public partial class MainWindow : Window
1012
{
13+
private RegistryOptions _registryOptions;
14+
private int _currentTheme = (int)ThemeName.DarkPlus;
15+
private readonly TextMate.Installation _textMateInstallation;
16+
private readonly TextEditor _textEditor;
17+
1118
public MainWindow()
1219
{
1320
InitializeComponent();
1421

15-
var editor = this.FindControl<TextEditor>("avaloniaEditor") ?? throw new Exception("no control");
16-
editor.Document = new AvaloniaEdit.Document.TextDocument("Hello world");
22+
_textEditor = this.FindControl<TextEditor>("avaloniaEditor") ?? throw new Exception("no control");
23+
24+
_registryOptions = new RegistryOptions(
25+
(ThemeName)_currentTheme);
26+
27+
_textMateInstallation = _textEditor.InstallTextMate(_registryOptions);
28+
Language xmlLang = _registryOptions.GetLanguageByExtension(".xml");
29+
_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(xmlLang.Id));
30+
}
31+
32+
protected override void OnClosed(EventArgs e)
33+
{
34+
base.OnClosed(e);
35+
36+
_textMateInstallation.Dispose();
1737
}
1838
}

SharpFM.App/SharpFM.App.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.2" />
1717
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.2" />
1818
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.0.1" />
19+
<PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.1" />
20+
<PackageReference Include="TextMateSharp.Grammars" Version="1.0.55" />
1921

2022
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
2123
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.2" />

SharpFM.App/ViewModels/MainWindowViewModel.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ public MainWindowViewModel()
2020

2121
[ObservableProperty] private string? _text;
2222

23+
[RelayCommand]
24+
private void NewEmptyItem()
25+
{
26+
ErrorMessages?.Clear();
27+
try
28+
{
29+
Keys.Add(new FileMakerClip("New", "", Array.Empty<byte>()));
30+
}
31+
catch (Exception e)
32+
{
33+
ErrorMessages?.Add(e.Message);
34+
}
35+
}
36+
2337
[RelayCommand]
2438
private async Task PasteText(CancellationToken token)
2539
{
@@ -62,7 +76,7 @@ private async Task DoGetClipboardDataAsync()
6276

6377
var clip = new FileMakerClip("new-clip", format, dataObj);
6478

65-
if(clip is null) { continue; }
79+
if (clip is null) { continue; }
6680

6781
// don't bother adding a duplicate. For some reason entries were getting entered twice per clip
6882
// this is not the most efficient method to detect it, but it works well enough for now
@@ -85,4 +99,10 @@ private async Task DoGetClipboardDataAsync()
8599

86100
[ObservableProperty]
87101
private FileMakerClip? _selectedClip;
102+
103+
public string SelectedXml
104+
{
105+
get => SelectedClip?.XmlData ?? "";
106+
set => SelectedClip!.XmlData = value;
107+
}
88108
}

0 commit comments

Comments
 (0)