using System.Collections;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Titano.App.Settings;
///
/// A text setting edited in place (DESIGN ยง4 inputs): the text box edits , validated on every
/// keystroke; Enter (or leaving the field) commits a valid value, Esc reverts to the committed one. An invalid value
/// shows the Error border (through ) and the message in .
///
public sealed class EditableField : INotifyPropertyChanged, INotifyDataErrorInfo
{
private readonly Func _validate;
private readonly Action _commit;
private readonly Func _toolTip;
private string _text;
private string _committed;
private string? _error;
/// The committed value.
/// Returns the localized problem of a candidate value, or null when it is valid.
/// Stores a valid, changed value.
/// The tooltip while the value is valid.
public EditableField(string value, Func validate, Action commit, Func toolTip)
{
_text = value;
_committed = value;
_validate = validate;
_commit = commit;
_toolTip = toolTip;
_error = validate(value);
}
public event PropertyChangedEventHandler? PropertyChanged;
public event EventHandler? ErrorsChanged;
/// Raised after a value was committed.
public event EventHandler? Committed;
/// The text being edited.
public string Text
{
get => _text;
set
{
value ??= string.Empty;
if (_text == value) return;
_text = value;
OnPropertyChanged();
OnPropertyChanged(nameof(IsDirty));
SetError(_validate(value));
}
}
/// The value in effect.
public string Value => _committed;
/// The problem of the current text; null when valid.
public string? Error => _error;
public bool HasErrors => _error is not null;
public bool IsDirty => _text != _committed;
/// The problem while invalid, otherwise the field's own tooltip.
public string ToolTip => _error ?? _toolTip();
/// Commits a valid text (Enter); false when the text is invalid.
public bool Commit()
{
if (_error is not null) return false;
if (!IsDirty) return true;
_committed = _text;
_commit(_committed);
OnPropertyChanged(nameof(Value));
OnPropertyChanged(nameof(IsDirty));
Committed?.Invoke(this, EventArgs.Empty);
return true;
}
/// Returns the text to the committed value (Esc).
public void Revert() => Text = _committed;
/// Replaces the committed value from outside (reset, import, picker) without calling the commit action.
public void Load(string value)
{
_committed = value ?? string.Empty;
OnPropertyChanged(nameof(Value));
Text = _committed;
OnPropertyChanged(nameof(IsDirty));
SetError(_validate(_text));
}
/// Re-evaluates the tooltip after a language change.
public void Refresh()
{
SetError(_validate(_text), force: true);
}
public IEnumerable GetErrors(string? propertyName) =>
_error is not null && (string.IsNullOrEmpty(propertyName) || propertyName == nameof(Text)) ? new[] { _error } : Array.Empty();
private void SetError(string? error, bool force = false)
{
if (_error == error && !force) return;
_error = error;
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Text)));
OnPropertyChanged(nameof(Error));
OnPropertyChanged(nameof(HasErrors));
OnPropertyChanged(nameof(ToolTip));
}
private void OnPropertyChanged([CallerMemberName] string? name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}