-
Notifications
You must be signed in to change notification settings - Fork 2
/
XamlDisplayAvaloniaEditThemeBehavior.cs
51 lines (44 loc) · 1.76 KB
/
XamlDisplayAvaloniaEditThemeBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using Avalonia;
using Avalonia.Logging;
using Avalonia.LogicalTree;
using Avalonia.VisualTree;
using Avalonia.Xaml.Interactivity;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using TextMateSharp.Grammars;
namespace ShowMeTheXaml.Avalonia.AvaloniaEdit;
public class XamlDisplayAvaloniaEditThemeBehavior : Behavior<TextEditor> {
private IDisposable? _disposable;
private RegistryOptions? _registryOptions;
private TextMate.Installation? _textMateInstallation;
/// <inheritdoc />
protected override void OnAttachedToVisualTree() {
base.OnAttachedToVisualTree();
var xamlDisplay = AssociatedObject.FindLogicalAncestorOfType<XamlDisplay>()!;
var themeName = xamlDisplay.GetValue(XamlDisplayAvaloniaEdit.CodeHighlightThemeNameProperty);
try
{
_registryOptions = new RegistryOptions(themeName);
_textMateInstallation = AssociatedObject!.InstallTextMate(_registryOptions);
_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId("xml"));
_disposable = xamlDisplay.GetObservable(XamlDisplayAvaloniaEdit.CodeHighlightThemeNameProperty)
.Subscribe(name => {
_textMateInstallation.SetTheme(_registryOptions.LoadTheme(name));
});
}
catch (Exception e)
{
if (Logger.TryGet(LogEventLevel.Warning, "ShowMeTheXaml.AvaloniaEdit", out var logger))
{
logger.Log(this, "TextMate highlighting can't be loaded. {Exception}", e);
}
}
}
/// <inheritdoc />
protected override void OnDetachedFromVisualTree() {
base.OnDetachedFromVisualTree();
_disposable?.Dispose();
_textMateInstallation?.Dispose();
}
}