Class ScyllaValidatedTextField
A text field that vets its own content, shows when it is wrong, and only reports values that passed.
Inheritance
Implements
Inherited Members
Namespace: Scylla.Core.Editor
Assembly: ScyllaCore.Editor.dll
Syntax
public class ScyllaValidatedTextField : VisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle
Remarks
Three things recur every time editor code puts a text field in front of a value that has rules: a re-entrancy guard, so pushing a corrected value back does not retrigger the handler that corrected it; a validation step that runs before anything downstream hears about the change; and some visible sign that what is typed will not be accepted. The first two are reimplemented independently in three places in Chroma alone, and the third is usually missing, so bad input is silently ignored and the user is left wondering why nothing happened.
Two events rather than one, because a caller usually wants to act on the good case and explain the bad one, and folding them together forces every caller to re-run the validator to tell which happened.
Validation runs per keystroke to drive the invalid styling, but ValueCommitted fires only on commit, which is Return or focus loss. Reporting mid-typing would mean a caller renaming an asset does it once per character.
Constructors
ScyllaValidatedTextField(string, Func<string, bool>)
Initializes a validated field.
Declaration
public ScyllaValidatedTextField(string label = null, Func<string, bool> validator = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | label | Optional label shown before the input. |
| Func<string, bool> | validator | Optional predicate. |
Fields
USS_CLASS
USS class on the root element.
Declaration
public const string USS_CLASS = "scylla-validated-field"
Field Value
| Type | Description |
|---|---|
| string |
USS_CLASS_INVALID
USS class added to the root while the content fails validation.
Declaration
public const string USS_CLASS_INVALID = "scylla-validated-field--invalid"
Field Value
| Type | Description |
|---|---|
| string |
Properties
Field
Gets the underlying field, for callers needing to set a tooltip or max length.
Declaration
public TextField Field { get; }
Property Value
| Type | Description |
|---|---|
| TextField |
IsValid
Gets a value indicating whether the current text passes the validator.
Declaration
public bool IsValid { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Validator
Gets or sets the predicate deciding whether the current text is acceptable.
null accepts everything.
Declaration
public Func<string, bool> Validator { get; set; }
Property Value
| Type | Description |
|---|---|
| Func<string, bool> |
Value
Gets or sets the text. Setting it never raises ValueCommitted, so a caller pushing a value in does not hear it echoed back.
Declaration
public string Value { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
Commit()
Raises the appropriate commit event for the current text.
Declaration
protected void Commit()
FilterInput(string)
Rewrites text as it is typed, before validation sees it.
Declaration
protected virtual string FilterInput(string raw)
Parameters
| Type | Name | Description |
|---|---|---|
| string | raw | The text as typed. |
Returns
| Type | Description |
|---|---|
| string | The text to keep in the field. |
Remarks
The default returns the input untouched. A subclass overrides this to strip characters that can never be legal, which is friendlier than letting the user type something the field will refuse on commit.
A live filter must only remove what is unconditionally wrong. Rules about the shape of the whole value belong in the validator, because a value can be legitimately invalid while the user is midway through typing a valid one.
Revalidate()
Re-runs the validator against the current text and refreshes the invalid styling, without raising either event.
Declaration
public void Revalidate()
Remarks
Call this when the rules change rather than the text, for example when a rename dialog learns which names are already taken.
Events
ValidationFailed
Raised on commit when the text fails the validator, carrying the rejected text.
Declaration
public event Action<string> ValidationFailed
Event Type
| Type | Description |
|---|---|
| Action<string> |
ValueCommitted
Raised on commit when the text passes the validator.
Declaration
public event Action<string> ValueCommitted
Event Type
| Type | Description |
|---|---|
| Action<string> |