Skip to content

Commit

Permalink
Merge pull request #333 from AvaloniaUI/document-changed-event-ars
Browse files Browse the repository at this point in the history
Pass the old new TextDocument on the DocumentChanged event
  • Loading branch information
danipen authored Apr 27, 2023
2 parents 7f61830 + 5cadf03 commit 2109e29
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/AvaloniaEdit/Document/DocumentChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace AvaloniaEdit.Document
{
public class DocumentChangedEventArgs : EventArgs
{
/// <summary>
/// Gets the old TextDocument.
/// </summary>
public TextDocument OldDocument { get; private set; }
/// <summary>
/// Gets the new TextDocument.
/// </summary>
public TextDocument NewDocument { get; private set; }

/// <summary>
/// Provides data for the <see cref="ITextEditorComponent.DocumentChanged"/> event.
/// </summary>
public DocumentChangedEventArgs(TextDocument oldDocument, TextDocument newDocument)
{
OldDocument = oldDocument;
NewDocument = newDocument;
}
}
}
4 changes: 2 additions & 2 deletions src/AvaloniaEdit/Editing/TextArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public TextDocument Document
}

/// <inheritdoc/>
public event EventHandler DocumentChanged;
public event EventHandler<DocumentChangedEventArgs> DocumentChanged;

/// <summary>
/// Gets if the the document displayed by the text editor is readonly
Expand Down Expand Up @@ -296,7 +296,7 @@ private void OnDocumentChanged(TextDocument oldValue, TextDocument newValue)
// in the new document (e.g. if new document is shorter than the old document).
Caret.Location = new TextLocation(1, 1);
ClearSelection();
DocumentChanged?.Invoke(this, EventArgs.Empty);
DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(oldValue, newValue));
//CommandManager.InvalidateRequerySuggested();
}
#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/AvaloniaEdit/Rendering/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal FontFamily FontFamily
/// <summary>
/// Occurs when the document property has changed.
/// </summary>
public event EventHandler DocumentChanged;
public event EventHandler<DocumentChangedEventArgs> DocumentChanged;

private static void OnDocumentChanged(AvaloniaPropertyChangedEventArgs e)
{
Expand Down Expand Up @@ -159,7 +159,7 @@ private void OnDocumentChanged(TextDocument oldValue, TextDocument newValue)
CachedElements = new TextViewCachedElements();
}
InvalidateMeasure();
DocumentChanged?.Invoke(this, EventArgs.Empty);
DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(oldValue, newValue));
}

private void RecreateCachedElements()
Expand Down
6 changes: 3 additions & 3 deletions src/AvaloniaEdit/TextEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ public TextDocument Document
/// <summary>
/// Occurs when the document property has changed.
/// </summary>
public event EventHandler DocumentChanged;
public event EventHandler<DocumentChangedEventArgs> DocumentChanged;

/// <summary>
/// Raises the <see cref="DocumentChanged"/> event.
/// </summary>
protected virtual void OnDocumentChanged(EventArgs e)
protected virtual void OnDocumentChanged(DocumentChangedEventArgs e)
{
DocumentChanged?.Invoke(this, e);
}
Expand All @@ -148,7 +148,7 @@ private void OnDocumentChanged(TextDocument oldValue, TextDocument newValue)
TextDocumentWeakEventManager.TextChanged.AddHandler(newValue, OnTextChanged);
PropertyChangedWeakEventManager.AddHandler(newValue.UndoStack, OnUndoStackPropertyChangedHandler);
}
OnDocumentChanged(EventArgs.Empty);
OnDocumentChanged(new DocumentChangedEventArgs(oldValue, newValue));
OnTextChanged(EventArgs.Empty);
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/AvaloniaEdit/TextEditorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface ITextEditorComponent : IServiceProvider
/// Occurs when the Document property changes (when the text editor is connected to another
/// document - not when the document content changes).
/// </summary>
event EventHandler DocumentChanged;
event EventHandler<DocumentChangedEventArgs> DocumentChanged;

/// <summary>
/// Gets the options of the text editor.
Expand Down

0 comments on commit 2109e29

Please sign in to comment.