port from perforce
This commit is contained in:
66
intromat/Intromat/Controls/SourceEditor.cs
Normal file
66
intromat/Intromat/Controls/SourceEditor.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using ICSharpCode.AvalonEdit;
|
||||
|
||||
namespace Intromat.Controls
|
||||
{
|
||||
public class SourceEditor : TextEditor, INotifyPropertyChanged
|
||||
{
|
||||
/// <summary>
|
||||
/// The bindable text property dependency property
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(SourceEditor), new FrameworkPropertyMetadata
|
||||
{
|
||||
DefaultValue = default(string),
|
||||
BindsTwoWayByDefault = true,
|
||||
PropertyChangedCallback = OnDependencyPropertyChanged
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// A bindable Text property
|
||||
/// </summary>
|
||||
public new string Text
|
||||
{
|
||||
get => (string)GetValue(TextProperty);
|
||||
set
|
||||
{
|
||||
SetValue(TextProperty, value);
|
||||
RaisePropertyChanged("Text");
|
||||
}
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
protected static void OnDependencyPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
var target = (SourceEditor)obj;
|
||||
if (target.Document == null)
|
||||
return;
|
||||
|
||||
var caretOffset = target.CaretOffset;
|
||||
var newValue = args.NewValue ?? "";
|
||||
|
||||
target.Document.Text = (string)newValue;
|
||||
target.CaretOffset = Math.Min(caretOffset, target.Document.Text.Length);
|
||||
}
|
||||
|
||||
protected override void OnTextChanged(EventArgs e)
|
||||
{
|
||||
if (Document != null)
|
||||
Text = Document.Text;
|
||||
|
||||
base.OnTextChanged(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises a property changed event
|
||||
/// </summary>
|
||||
/// <param name="property">The name of the property that updates</param>
|
||||
public void RaisePropertyChanged(string property)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(property));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user