Files
Chronos/src/Titano.App/Settings/EditableField.cs
T
Claude ffbea7cb74 feat(app): library, jobs, settings, server, welcome and about screens
Local-mode composition root (SQLite index, one JobEngine fed by a single
handler registration list), keyset-paged library with thumbnail cache and
hover scrub, live job list with throttled updates and the shell job bar,
settings with live template validation and encoder self-test, server
connection with trust-on-first-use and DPAPI token, first-run welcome,
About with update check, and --capture screenshots with synthetic data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 12:26:58 +00:00

117 lines
4.2 KiB
C#

using System.Collections;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Titano.App.Settings;
/// <summary>
/// A text setting edited in place (DESIGN §4 inputs): the text box edits <see cref="Text"/>, 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 <see cref="INotifyDataErrorInfo"/>) and the message in <see cref="ToolTip"/>.
/// </summary>
public sealed class EditableField : INotifyPropertyChanged, INotifyDataErrorInfo
{
private readonly Func<string, string?> _validate;
private readonly Action<string> _commit;
private readonly Func<string> _toolTip;
private string _text;
private string _committed;
private string? _error;
/// <param name="value">The committed value.</param>
/// <param name="validate">Returns the localized problem of a candidate value, or null when it is valid.</param>
/// <param name="commit">Stores a valid, changed value.</param>
/// <param name="toolTip">The tooltip while the value is valid.</param>
public EditableField(string value, Func<string, string?> validate, Action<string> commit, Func<string> toolTip)
{
_text = value;
_committed = value;
_validate = validate;
_commit = commit;
_toolTip = toolTip;
_error = validate(value);
}
public event PropertyChangedEventHandler? PropertyChanged;
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
/// <summary>Raised after a value was committed.</summary>
public event EventHandler? Committed;
/// <summary>The text being edited.</summary>
public string Text
{
get => _text;
set
{
value ??= string.Empty;
if (_text == value) return;
_text = value;
OnPropertyChanged();
OnPropertyChanged(nameof(IsDirty));
SetError(_validate(value));
}
}
/// <summary>The value in effect.</summary>
public string Value => _committed;
/// <summary>The problem of the current text; null when valid.</summary>
public string? Error => _error;
public bool HasErrors => _error is not null;
public bool IsDirty => _text != _committed;
/// <summary>The problem while invalid, otherwise the field's own tooltip.</summary>
public string ToolTip => _error ?? _toolTip();
/// <summary>Commits a valid text (Enter); false when the text is invalid.</summary>
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;
}
/// <summary>Returns the text to the committed value (Esc).</summary>
public void Revert() => Text = _committed;
/// <summary>Replaces the committed value from outside (reset, import, picker) without calling the commit action.</summary>
public void Load(string value)
{
_committed = value ?? string.Empty;
OnPropertyChanged(nameof(Value));
Text = _committed;
OnPropertyChanged(nameof(IsDirty));
SetError(_validate(_text));
}
/// <summary>Re-evaluates the tooltip after a language change.</summary>
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<string>();
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));
}