using System.ComponentModel;
using System.Text;
namespace TinyChat;
///
/// A string builder decorator that notifies with INotifyPropertyChanged when its content changes.
///
public class NotifyingStringBuilder : INotifyPropertyChanged
{
private readonly StringBuilder _inner = new();
///
/// Occurs when the string builder content changes.
///
public event PropertyChangedEventHandler? PropertyChanged;
///
/// Gets the length of the current StringBuilder.
///
public int Length => _inner.Length;
///
/// Gets or sets the capacity of the StringBuilder.
///
public int Capacity
{
get => _inner.Capacity;
set => _inner.Capacity = value;
}
///
/// Gets or sets the character at the specified index.
///
public char this[int index]
{
get => _inner[index];
set
{
_inner[index] = value;
OnPropertyChanged();
}
}
///
/// Appends a string to the StringBuilder.
///
public NotifyingStringBuilder Append(string? value)
{
_inner.Append(value);
OnPropertyChanged();
return this;
}
///
/// Appends a character to the StringBuilder.
///
public NotifyingStringBuilder Append(char value)
{
_inner.Append(value);
OnPropertyChanged();
return this;
}
///
/// Appends an object's string representation to the StringBuilder.
///
public NotifyingStringBuilder Append(object? value)
{
_inner.Append(value);
OnPropertyChanged();
return this;
}
///
/// Appends a line to the StringBuilder.
///
public NotifyingStringBuilder AppendLine()
{
_inner.AppendLine();
OnPropertyChanged();
return this;
}
///
/// Appends a line with the specified string to the StringBuilder.
///
public NotifyingStringBuilder AppendLine(string? value)
{
_inner.AppendLine(value);
OnPropertyChanged();
return this;
}
///
/// Inserts a string at the specified index.
///
public NotifyingStringBuilder Insert(int index, string? value)
{
_inner.Insert(index, value);
OnPropertyChanged();
return this;
}
///
/// Removes characters from the StringBuilder.
///
public NotifyingStringBuilder Remove(int startIndex, int length)
{
_inner.Remove(startIndex, length);
OnPropertyChanged();
return this;
}
///
/// Clears the StringBuilder.
///
public NotifyingStringBuilder Clear()
{
_inner.Clear();
OnPropertyChanged();
return this;
}
///
/// Returns the string representation of the StringBuilder.
///
public override string ToString() => _inner.ToString();
///
/// Raises the PropertyChanged event.
///
protected virtual void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}