diff --git a/src/MimaSim/MimaSim/App.axaml b/src/MimaSim/MimaSim/App.axaml index efa38c1f..0a1dee6a 100644 --- a/src/MimaSim/MimaSim/App.axaml +++ b/src/MimaSim/MimaSim/App.axaml @@ -3,7 +3,6 @@ xmlns:local="using:MimaSim" x:Class="MimaSim.App" RequestedThemeVariant="Light"> - @@ -18,6 +17,7 @@ + diff --git a/src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs b/src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs new file mode 100644 index 00000000..e226f4cf --- /dev/null +++ b/src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs @@ -0,0 +1,60 @@ +using System; +using Avalonia; +using Avalonia.Xaml.Interactivity; +using AvaloniaEdit; + +namespace MimaSim.Behaviors; + +public class DocumentTextBindingBehavior : Behavior +{ + private TextEditor _textEditor; + + 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; + //_textEditor.CaretOffset = caretOffset; + } + } +} \ No newline at end of file diff --git a/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml b/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml index 7f1355ab..afda654e 100644 --- a/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml +++ b/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml @@ -2,14 +2,23 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:c="clr-namespace:MimaSim.Controls;assembly=MimaSim" x:CompileBindings="False" + xmlns:c="clr-namespace:MimaSim.Controls;assembly=MimaSim" + xmlns:avaloniaEdit="https://github.com/avaloniaui/avaloniaedit" + xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity" + xmlns:behaviors="clr-namespace:MimaSim.Behaviors" + x:CompileBindings="False" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="MimaSim.Controls.ProgramEditorControl"> - + + + + + diff --git a/src/MimaSim/MimaSim/MimaSim.csproj b/src/MimaSim/MimaSim/MimaSim.csproj index e816ace4..301fa042 100644 --- a/src/MimaSim/MimaSim/MimaSim.csproj +++ b/src/MimaSim/MimaSim/MimaSim.csproj @@ -15,11 +15,13 @@ + +