From 8dcae507eb91c7914371de03f203307aecc67afc Mon Sep 17 00:00:00 2001 From: Nate Bross Date: Thu, 14 Aug 2025 22:10:49 -0500 Subject: [PATCH] fix: bind to TextDocument instead if custom binding behavior --- DocumentTextBindingBehavior.cs | 64 ---------------------------------- MainWindow.axaml | 11 ++---- ViewModels/ClipViewModel.cs | 22 +++++++++++- 3 files changed, 24 insertions(+), 73 deletions(-) delete mode 100644 DocumentTextBindingBehavior.cs diff --git a/DocumentTextBindingBehavior.cs b/DocumentTextBindingBehavior.cs deleted file mode 100644 index 5464491..0000000 --- a/DocumentTextBindingBehavior.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using Avalonia; -using Avalonia.Xaml.Interactivity; -using AvaloniaEdit; -using AvaloniaEdit.Utils; - -namespace SharpFM.Behaviors; - -public class DocumentTextBindingBehavior : Behavior -{ - private TextEditor _textEditor = null!; - - public static readonly StyledProperty TextProperty = - AvaloniaProperty.Register(nameof(Text)); - - public string Text - { - get => GetValue(TextProperty); - set => SetValue(TextProperty, value); - } - - protected override void OnAttached() - { - base.OnAttached(); - - if (AssociatedObject is TextEditor textEditor) - { - _textEditor = textEditor; - _textEditor.TextChanged += TextChanged; - this.GetObservable(TextProperty).Subscribe(TextPropertyChanged); - } - } - - protected override void OnDetaching() - { - base.OnDetaching(); - - if (_textEditor != null) - { - _textEditor.TextChanged -= TextChanged; - } - } - - private void TextChanged(object? sender, EventArgs eventArgs) - { - if (_textEditor != null && _textEditor.Document != null) - { - Text = _textEditor.Document.Text; - } - } - - private void TextPropertyChanged(string text) - { - if (_textEditor != null && _textEditor.Document != null && text != null) - { - var caretOffset = _textEditor.CaretOffset; - _textEditor.Document.Text = text; - if (caretOffset <= text.Length) - { - _textEditor.CaretOffset = caretOffset; - } - } - } -} \ No newline at end of file diff --git a/MainWindow.axaml b/MainWindow.axaml index c5c4913..5e73f42 100644 --- a/MainWindow.axaml +++ b/MainWindow.axaml @@ -3,9 +3,7 @@ xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:AvaloniaEdit="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit" - xmlns:behaviors="clr-namespace:SharpFM.Behaviors;assembly=SharpFM" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" - xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="using:SharpFM.ViewModels" Icon="/Assets/noun-sharp-teeth-monster-4226695.small.png" @@ -169,7 +167,7 @@ Text="Clip Content" /> - + - - - - + WordWrap="False" + Document="{Binding SelectedClip.XmlDocument}" /> diff --git a/ViewModels/ClipViewModel.cs b/ViewModels/ClipViewModel.cs index 82e5b9e..8aea923 100644 --- a/ViewModels/ClipViewModel.cs +++ b/ViewModels/ClipViewModel.cs @@ -1,5 +1,6 @@ using System.ComponentModel; using System.Runtime.CompilerServices; +using AvaloniaEdit.Document; namespace SharpFM.ViewModels; @@ -13,6 +14,8 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") } public FileMakerClip Clip { get; set; } + + private TextDocument? _xmlDocument; public ClipViewModel(FileMakerClip clip) { @@ -39,13 +42,30 @@ public string Name } } + public TextDocument XmlDocument + { + get + { + if (_xmlDocument == null) + { + _xmlDocument = new TextDocument(Clip.XmlData ?? string.Empty); + } + return _xmlDocument; + } + } + public string ClipXml { - get => Clip.XmlData; + get => _xmlDocument?.Text ?? Clip.XmlData; set { Clip.XmlData = value; + if (_xmlDocument != null) + { + _xmlDocument.Text = value ?? string.Empty; + } NotifyPropertyChanged(); + NotifyPropertyChanged(nameof(XmlDocument)); } } } \ No newline at end of file