Class: Tuile::Component::IntegerField
- Inherits:
-
AbstractWrappingField
- Object
- Component
- AbstractWrappingField
- Tuile::Component::IntegerField
- Includes:
- HasBadInput
- Defined in:
- lib/tuile/component/integer_field.rb,
sig/tuile.rbs
Overview
A single-line field whose #value is an Integer (or nil when empty).
The buffer only ever holds 0–9 and a single leading -: a key that
would break that is dropped without moving the caret, and so is a paste
that would ("12abc34" lands nothing, rather than sieving through as
"1234"). Up/Down step the value by one (an empty field counting as 0).
An empty or otherwise un-parseable buffer reads back as nil:
field = Component::IntegerField.new
field.on_value_change = ->(n) { puts n.inspect } # Integer or nil, per change
field.value = 42 # field shows "42"
field.value # => 42
field.clear # empties it; value => nil
It wraps a TextField rather than subclassing one, so its face carries
only the typed HasValue value seam and never the widget's String-typed
text; AbstractWrappingField supplies the wrapping. Give it a
single-row #rect.
The value is a derived parse of the buffer
#value is Integer(buffer, 10) (or nil), recomputed on read — the
buffer is the single source of truth, #value= just writes it. So "-"
alone and "" both read as nil, and on_value_change fires eagerly
once per real value change: typing 0→7 in "07" shifts the buffer
but not the value (7), so it does not fire. No normalization — a typed
"007" stays "007" on screen though its value is 7.
min/max, a + sign, and thousands separators are deliberately out of
scope (range and formatting are a forms concern).
UI-thread-confined, like every component (see Screen).
Defined Under Namespace
Classes: Field
Constant Summary collapse
- BAD_INPUT_MESSAGE =
Returns what #bad_input_message reports for a buffer that is typeable but not an integer.
"not a whole number"
Instance Attribute Summary
Attributes included from HasValidation
Attributes inherited from AbstractWrappingField
Attributes included from HasValue
Instance Method Summary collapse
-
#bad_input? ⇒ Boolean
@return — true iff the field is holding input its value cannot represent.
-
#bad_input_message ⇒ String?
The lone
"-"the filter has to admit is the field's whole bad-input residue; an empty buffer is empty, not bad (HasBadInput). -
#bad_input_settled? ⇒ Boolean
Whether bad input may paint the well yet.
-
#empty_value ⇒ void
nil, not"": an integer field with no parseable number is empty. -
#error_bg_color ⇒ Color?
The invalid well, picked up by everything this component paints — including the inner face of a composed field and the List of a group, neither of which forwards anything: both declare no background of their own, so the ordinary chain walks up to this (overrides #error_bg_color).
-
#error_ink? ⇒ Boolean
Widens HasValidation#error_ink?: bad input paints the invalid well too, with no verdict written — once #bad_input_settled? says the report may be shown.
-
#error_message ⇒ StyledString?
@return — why the field is invalid, or
nilwhen it is not;niluntil something sets it. -
#error_message= ⇒ void
Sets the verdict and repaints the field in Theme#error_color;
nilclears it. -
#initialize ⇒ IntegerField
constructor
A new instance of IntegerField.
-
#inspect_details ⇒ ::Array[String]
Adds
error_message=…to #inspect, omitted while valid — so a Testing.get failure dump says which field is already flagged. -
#step(delta) ⇒ void
Nudges #value by
delta, treating an empty/un-parseable field as0. -
#value ⇒ Integer?
@return — the parsed buffer;
nilwhen empty or not a valid integer (e.g. a lone"-"). -
#value=(new_value) ⇒ void
Writes
new_valueinto the buffer and parks the caret at its end; fires HasValue#on_value_change only if the value actually changed.
Methods inherited from AbstractWrappingField
#active=, #clear, #commit, #commit_and_notify, #cursor_position, #default_bg_color, #empty?, #fire_if_changed, #focusable?, #handle_editor_change, #handle_focus, #handle_key?, #layout, #notify_on_edit?, #placeholder, #placeholder=, #rect=
Methods included from HasPlaceholder
Methods included from HasValue
Constructor Details
#initialize ⇒ IntegerField
Returns a new instance of IntegerField.
67 68 69 70 71 72 |
# File 'lib/tuile/component/integer_field.rb', line 67 def initialize super(Field.new) # Not the general on_key interceptor: that slot stays free for the app. editor.on_key_up = -> { step(1) } editor.on_key_down = -> { step(-1) } end |
Instance Method Details
#bad_input? ⇒ Boolean
@return — true iff the field is holding input its value cannot represent.
9461 |
# File 'sig/tuile.rbs', line 9461
def bad_input?: () -> bool
|
#bad_input_message ⇒ String?
The lone "-" the filter has to admit is the field's whole bad-input
residue; an empty buffer is empty, not bad (HasBadInput).
98 |
# File 'lib/tuile/component/integer_field.rb', line 98 def = value.nil? && !editor.text.empty? ? BAD_INPUT_MESSAGE : nil |
#bad_input_settled? ⇒ Boolean
Whether bad input may paint the well yet. true here, so the well is
as continuous as the report: a FloatField reddens at the half-typed
"1.", which is a fair warning while the residue is one or two
transient buffers. Override it to latch where the grammar makes
every prefix bad input, or the well is red for the whole time the
user types a correct value:
def bad_input_settled? = @settled # set on commit, cleared on an edit
It gates the ink only: #bad_input? is a pull, and a save gate
asking at a click must get the answer settled or not (design/decisions.md
D_bad_input).
9480 |
# File 'sig/tuile.rbs', line 9480
def bad_input_settled?: () -> bool
|
#empty_value ⇒ void
This method returns an undefined value.
nil, not "": an integer field with no parseable number is empty.
93 |
# File 'lib/tuile/component/integer_field.rb', line 93 def empty_value = nil |
#error_bg_color ⇒ Color?
The invalid well, picked up by everything this component paints — including the inner face of a composed field and the List of a group, neither of which forwards anything: both declare no background of their own, so the ordinary chain walks up to this (overrides Tuile::Component#error_bg_color).
9501 |
# File 'sig/tuile.rbs', line 9501
def error_bg_color: () -> Color?
|
#error_ink? ⇒ Boolean
Widens HasValidation#error_ink?: bad input paints the invalid well too, with no verdict written — once #bad_input_settled? says the report may be shown.
9466 |
# File 'sig/tuile.rbs', line 9466
def error_ink?: () -> bool
|
#error_message ⇒ StyledString?
@return — why the field is invalid, or nil when it
is not; nil until something sets it.
9484 |
# File 'sig/tuile.rbs', line 9484
def error_message: () -> StyledString?
|
#error_message= ⇒ void
This method returns an undefined value.
Sets the verdict and repaints the field in Theme#error_color; nil
clears it. No-op (no repaint, no listener) when unchanged. A String is
parsed via StyledString.parse, as HasCaption#caption= does.
Safe on a detached field — an app validates a form it assembled but has not mounted, and Tuile::Component#invalidate is already a no-op there.
@param new_message
9494 |
# File 'sig/tuile.rbs', line 9494
def error_message=: ((String | StyledString)? new_message) -> void
|
#inspect_details ⇒ ::Array[String]
Adds error_message=… to Tuile::Component#inspect, omitted while valid — so
a Testing.get failure dump says which field is already flagged.
9505 |
# File 'sig/tuile.rbs', line 9505
def inspect_details: () -> ::Array[String]
|
#step(delta) ⇒ void
This method returns an undefined value.
Nudges #value by delta, treating an empty/un-parseable field as 0.
@param delta
105 |
# File 'lib/tuile/component/integer_field.rb', line 105 def step(delta) = (self.value = (value || 0) + delta) |
#value ⇒ Integer?
@return — the parsed buffer; nil when empty or not a
valid integer (e.g. a lone "-").
76 77 78 79 80 |
# File 'lib/tuile/component/integer_field.rb', line 76 def value Integer(editor.text, 10) rescue ArgumentError nil end |
#value=(new_value) ⇒ void
This method returns an undefined value.
Writes new_value into the buffer and parks the caret at its end; fires
HasValue#on_value_change only if the value actually changed.
@param new_value — nil empties the field.
86 87 88 89 |
# File 'lib/tuile/component/integer_field.rb', line 86 def value=(new_value) editor.text = new_value.nil? ? "" : new_value.to_s editor.caret = editor.text.length end |