Module: Tuile::Component::HasCaption

Included in:
Button, Checkbox, Window
Defined in:
lib/tuile/component/has_caption.rb,
sig/tuile.rbs

Overview

The chrome text a component wears — a Window's border title, a Button's label — as opposed to the value it holds.

button.caption = "Submit"
window.caption = StyledString.styled("Settings", fg: Color::RED)

Tuile's naming split, which decides what a new component gets: caption is chrome, authored by the app; text is the value the user edits (aliased to Tuile::Component::HasValue#value on AbstractStringField). A component may carry both, hence two mixins.

Includers own the rendering — clipping, width arithmetic, decoration such as Window's [key]- shortcut prefix; this holds only the text.

Implementation details

Being a mixin is what lets tree-walking code find "the Button captioned Submit" via is_a?(HasCaption) plus a caption compare, rather than a hardcoded list of classes that happen to respond to caption. Don't collapse it back into per-class accessors.

Instance Method Summary collapse

Instance Method Details

#captionStyledString

Read through this method, never @caption — the ivar stays nil until the first non-empty set (#caption= short-circuits when unchanged).

@return — the caption; empty when never set.

Returns:



28
# File 'lib/tuile/component/has_caption.rb', line 28

def caption = @caption || StyledString::EMPTY

#caption=(new_caption) ⇒ void

This method returns an undefined value.

Sets the caption and invalidates the component. No-op when unchanged. A String is parsed via StyledString.parse (embedded ANSI is honored); a StyledString is used as-is; nil clears it.

@param new_caption

Parameters:



35
36
37
38
39
40
41
# File 'lib/tuile/component/has_caption.rb', line 35

def caption=(new_caption)
  new_caption = StyledString.parse(new_caption)
  return if caption == new_caption

  @caption = new_caption
  invalidate
end