-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from furesoft/feature/new-editor
feat: Replace Editor With AvalonEdit
- Loading branch information
Showing
4 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using Avalonia; | ||
using Avalonia.Xaml.Interactivity; | ||
using AvaloniaEdit; | ||
|
||
namespace MimaSim.Behaviors; | ||
|
||
public class DocumentTextBindingBehavior : Behavior<TextEditor> | ||
{ | ||
private TextEditor _textEditor; | ||
|
||
public static readonly StyledProperty<string> TextProperty = | ||
AvaloniaProperty.Register<DocumentTextBindingBehavior, string>(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; | ||
Check warning on line 28 in src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs GitHub Actions / build
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters