tuile 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +77 -64
- data/DECISIONS.md +619 -14
- data/README.md +6 -0
- data/book/03-layout.md +153 -8
- data/book/05-focus.md +2 -0
- data/book/07-components.md +105 -10
- data/book/README.md +3 -1
- data/examples/sampler.rb +282 -132
- data/ideas/arrow-key-navigation.md +205 -0
- data/ideas/new-components.md +17 -8
- data/lib/tuile/component/big_decimal_field.rb +199 -0
- data/lib/tuile/component/checkbox.rb +10 -9
- data/lib/tuile/component/combo_box.rb +8 -26
- data/lib/tuile/component/float_field.rb +161 -0
- data/lib/tuile/component/layout/box.rb +316 -0
- data/lib/tuile/component/layout/horizontal.rb +40 -0
- data/lib/tuile/component/layout/vertical.rb +41 -0
- data/lib/tuile/component/layout.rb +149 -1
- data/lib/tuile/component/list_dropdown.rb +69 -18
- data/lib/tuile/component/select.rb +251 -0
- data/lib/tuile/styled_string.rb +13 -3
- data/lib/tuile/version.rb +1 -1
- data/lib/tuile.rb +4 -0
- data/sig/tuile.rbs +882 -29
- metadata +8 -1
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tuile
|
|
4
|
+
class Component
|
|
5
|
+
# A single-line field whose {#value} is a `Float` (or `nil` when empty) —
|
|
6
|
+
# the {IntegerField} twin, one Ruby type over. Give it a single-row {#rect}:
|
|
7
|
+
#
|
|
8
|
+
# field = Component::FloatField.new
|
|
9
|
+
# field.on_value_change = ->(x) { puts x.inspect } # Float or nil, per change
|
|
10
|
+
# field.value = 19.99 # field shows "19.99"
|
|
11
|
+
# field.clear # empties it; value => nil
|
|
12
|
+
#
|
|
13
|
+
# Only `0`–`9`, one leading `-` and one `.` can be typed; any other
|
|
14
|
+
# printable key is dropped without moving the caret. Up/Down step by `1.0`
|
|
15
|
+
# (an empty field counting as `0.0`). A `Float` is a binary double, so this
|
|
16
|
+
# is the wrong field for money — hold that as `Integer` cents in an
|
|
17
|
+
# {IntegerField} — and range checks (`min`/`max`) belong to a forms layer,
|
|
18
|
+
# not here.
|
|
19
|
+
#
|
|
20
|
+
# == Implementation details
|
|
21
|
+
# {#value} is a *derived parse*: the buffer is the single source of truth,
|
|
22
|
+
# recomputed on read and left exactly as typed (`"007"` keeps its zeros).
|
|
23
|
+
# It reads `nil` for a buffer that isn't a number (`""`, a lone `"-"`) but
|
|
24
|
+
# `1.0` / `0.5` for a half-typed `"1."` / `".5"`, so reaching for the
|
|
25
|
+
# decimal point doesn't blink the value to `nil` and back through
|
|
26
|
+
# {#on_value_change} — which fires per keystroke, but only on a real *value*
|
|
27
|
+
# change (`"7"`→`"07"` is silent). The parse also accepts the exponent
|
|
28
|
+
# `Float#to_s` writes for extreme magnitudes, so `value = 1e-5` round-trips
|
|
29
|
+
# through the `"1.0e-05"` it displays, though no key types an `e`.
|
|
30
|
+
#
|
|
31
|
+
# It *composes* a {TextField} (its single {HasContent} child) rather than
|
|
32
|
+
# subclassing one, so its face carries only the typed {HasValue} seam, never
|
|
33
|
+
# the widget's `String`-typed `text`.
|
|
34
|
+
#
|
|
35
|
+
# UI-thread-confined, like every component (see {Screen}).
|
|
36
|
+
class FloatField < Component
|
|
37
|
+
include HasContent
|
|
38
|
+
include HasValue
|
|
39
|
+
|
|
40
|
+
# A buffer {#value} parses: an optional sign, digits with an optional
|
|
41
|
+
# fractional part (either side may be empty, but not both), and the
|
|
42
|
+
# exponent {#value=} can write.
|
|
43
|
+
# @return [Regexp]
|
|
44
|
+
NUMERIC = /\A-?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?\z/
|
|
45
|
+
private_constant :NUMERIC
|
|
46
|
+
|
|
47
|
+
def initialize
|
|
48
|
+
super()
|
|
49
|
+
@last_value = nil
|
|
50
|
+
field = TextField.new
|
|
51
|
+
field.on_change = ->(_text) { fire_if_changed }
|
|
52
|
+
field.on_key = method(:field_key)
|
|
53
|
+
self.content = field
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @return [Float, nil] the parsed buffer; `nil` when empty or not a
|
|
57
|
+
# number (e.g. a lone `"-"`).
|
|
58
|
+
def value
|
|
59
|
+
text = content.text
|
|
60
|
+
text.match?(NUMERIC) ? text.to_f : nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Writes `new_value` into the buffer and parks the caret at its end; fires
|
|
64
|
+
# {#on_value_change} only if the value actually changed.
|
|
65
|
+
# @param new_value [Numeric, nil] `nil` empties the field; anything else
|
|
66
|
+
# is coerced with `Float()`, so an `Integer` `3` shows as `"3.0"`.
|
|
67
|
+
# @raise [ArgumentError] on a non-numeric `String`, a NaN or an infinity.
|
|
68
|
+
# @raise [TypeError] on a value `Float()` won't take at all (an `Array`).
|
|
69
|
+
# @return [void]
|
|
70
|
+
def value=(new_value)
|
|
71
|
+
content.text = new_value.nil? ? "" : coerce(new_value).to_s
|
|
72
|
+
content.caret = content.text.length
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# `nil`, not `""`: a numeric field with no parseable number is empty.
|
|
76
|
+
# @return [nil]
|
|
77
|
+
def empty_value = nil
|
|
78
|
+
|
|
79
|
+
# @return [Point, nil] the field's caret (the hardware cursor is delegated
|
|
80
|
+
# to the inner field).
|
|
81
|
+
def cursor_position = content.cursor_position
|
|
82
|
+
|
|
83
|
+
# Fired when ENTER is pressed in the field; see {TextField#on_enter}.
|
|
84
|
+
# @return [Proc, Method, nil] no-arg callable, or nil.
|
|
85
|
+
def on_enter = content.on_enter
|
|
86
|
+
|
|
87
|
+
# @param callback [Proc, Method, nil]
|
|
88
|
+
# @return [void]
|
|
89
|
+
def on_enter=(callback)
|
|
90
|
+
content.on_enter = callback
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
protected
|
|
94
|
+
|
|
95
|
+
# Places the wrapped field across the whole rect ({HasContent} hook).
|
|
96
|
+
# @param field [Component]
|
|
97
|
+
# @return [void]
|
|
98
|
+
def layout(field) = (field.rect = rect)
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
# @param new_value [Numeric]
|
|
103
|
+
# @return [Float]
|
|
104
|
+
# @raise [ArgumentError] on a NaN or an infinity — `Float::NAN.to_s` is
|
|
105
|
+
# `"NaN"`, which no parse reads back, so writing one would silently
|
|
106
|
+
# turn the value `nil`.
|
|
107
|
+
def coerce(new_value)
|
|
108
|
+
float = Float(new_value)
|
|
109
|
+
raise ArgumentError, "value must be finite, got #{float}" unless float.finite?
|
|
110
|
+
|
|
111
|
+
float
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The field's key interceptor, consulted *before* the field acts on the
|
|
115
|
+
# key — which is what lets a rejected character be swallowed without the
|
|
116
|
+
# caret ever moving.
|
|
117
|
+
# @param key [String]
|
|
118
|
+
# @return [Boolean] true to consume the key.
|
|
119
|
+
def field_key(key)
|
|
120
|
+
case key
|
|
121
|
+
when Keys::UP_ARROW then step(1.0)
|
|
122
|
+
when Keys::DOWN_ARROW then step(-1.0)
|
|
123
|
+
else return Keys.printable?(key) && !accepts?(key)
|
|
124
|
+
end
|
|
125
|
+
true
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Nudges {#value} by `delta`, treating an empty/un-parseable field as
|
|
129
|
+
# `0.0`.
|
|
130
|
+
# @param delta [Float]
|
|
131
|
+
# @return [void]
|
|
132
|
+
def step(delta) = (self.value = (value || 0.0) + delta)
|
|
133
|
+
|
|
134
|
+
# Whether `char` may be inserted. Deliberately shallow: it keeps the
|
|
135
|
+
# buffer *typeable* rather than always-valid — a transient `"-"` or
|
|
136
|
+
# `"1."` has to be reachable — and {#value} decides what parses.
|
|
137
|
+
# @param char [String] a single printable character.
|
|
138
|
+
# @return [Boolean]
|
|
139
|
+
def accepts?(char)
|
|
140
|
+
case char
|
|
141
|
+
when /\A[0-9]\z/ then true
|
|
142
|
+
when "-" then content.caret.zero? && !content.text.start_with?("-")
|
|
143
|
+
when "." then !content.text.include?(".")
|
|
144
|
+
else false
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Re-emits {#on_value_change} with the freshly-parsed {#value}, but only
|
|
149
|
+
# when it differs from the last one fired — so a buffer edit that leaves
|
|
150
|
+
# the value unchanged (`"7"`→`"07"`) stays silent.
|
|
151
|
+
# @return [void]
|
|
152
|
+
def fire_if_changed
|
|
153
|
+
v = value
|
|
154
|
+
return if v == @last_value
|
|
155
|
+
|
|
156
|
+
@last_value = v
|
|
157
|
+
on_value_change&.call(v)
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tuile
|
|
4
|
+
class Component
|
|
5
|
+
class Layout
|
|
6
|
+
# Abstract base of the one-dimensional box layouts. Children are stacked
|
|
7
|
+
# along a *main* axis in the order they were added, each getting the extent
|
|
8
|
+
# its constraint asks for; across the *cross* axis they are sized one at a
|
|
9
|
+
# time, since nothing competes with them there. {Vertical} and {Horizontal}
|
|
10
|
+
# pick which axis is which.
|
|
11
|
+
#
|
|
12
|
+
# class LoginForm < Tuile::Component::Layout::Vertical
|
|
13
|
+
# def initialize
|
|
14
|
+
# super(spacing: 1, padding: Insets[top: 1])
|
|
15
|
+
# add(@prompt = Tuile::Component::Label.new, Fixed[4])
|
|
16
|
+
# add(@user = Tuile::Component::TextField.new, Fixed[1], cross: Fixed[30])
|
|
17
|
+
# add(@log = Tuile::Component::TextView.new, Expand[1])
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# The constraint names need no prefix inside a subclass — Ruby finds them on
|
|
22
|
+
# `Layout`, an ancestor. Component classes are not on that chain and still do.
|
|
23
|
+
#
|
|
24
|
+
# Children pack from the start edge, so with no {Expand} among them the
|
|
25
|
+
# slack is simply left at the end: there is no filler component to add.
|
|
26
|
+
# Nest boxes to vary the gap — a `Vertical.new(spacing: 0)` inside a
|
|
27
|
+
# `Vertical.new(spacing: 1)` groups two rows tightly within a looser stack.
|
|
28
|
+
#
|
|
29
|
+
# == Implementation details
|
|
30
|
+
#
|
|
31
|
+
# Every child-list mutation re-runs the whole pass, because in a box the
|
|
32
|
+
# children move: removing one shifts everything after it, and adding one
|
|
33
|
+
# shrinks every {Expand} share. ({Absolute} can skip this — there, siblings
|
|
34
|
+
# are independent.)
|
|
35
|
+
#
|
|
36
|
+
# Main-axis resolution order, against
|
|
37
|
+
# `available = extent - padding - spacing * (children - 1)`:
|
|
38
|
+
#
|
|
39
|
+
# 1. {Fixed} takes its cells, clamped to what is still unassigned.
|
|
40
|
+
# 2. {Percent} takes its share *of `available`*, likewise clamped.
|
|
41
|
+
# 3. {Expand} children split the residue by weight; the integer remainder
|
|
42
|
+
# goes to the earliest of them, one cell each.
|
|
43
|
+
#
|
|
44
|
+
# So over-subscription starves in declaration order rather than raising:
|
|
45
|
+
# a child with nothing left gets an empty rect and paints nothing. Padding
|
|
46
|
+
# wider than the layout does the same to every child.
|
|
47
|
+
class Box < Layout
|
|
48
|
+
# Constraints for a child wired in through `add_child` instead of {#add}.
|
|
49
|
+
# @return [Hash{Symbol => Object}]
|
|
50
|
+
DEFAULT_PLACEMENT = { main: Fixed[1], cross: Percent[100], align: :start }.freeze
|
|
51
|
+
|
|
52
|
+
# Where a child narrower than the cross extent sits within it.
|
|
53
|
+
# @return [Array<Symbol>]
|
|
54
|
+
ALIGNMENTS = %i[start center end].freeze
|
|
55
|
+
|
|
56
|
+
# @param spacing [Integer] blank cells between adjacent children; `>= 0`.
|
|
57
|
+
# @param padding [Insets, Integer] inset from this layout's own rect; an
|
|
58
|
+
# Integer is coerced to a uniform {Insets}.
|
|
59
|
+
# @raise [ArgumentError] on a negative `spacing` or an unusable `padding`.
|
|
60
|
+
def initialize(spacing: 0, padding: 0)
|
|
61
|
+
super()
|
|
62
|
+
@spacing = validate_spacing(spacing)
|
|
63
|
+
@padding = Insets.coerce(padding)
|
|
64
|
+
# Identity-keyed: two == children are still two distinct slots.
|
|
65
|
+
@placements = {}.compare_by_identity
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @return [Integer] blank cells between adjacent children.
|
|
69
|
+
attr_reader :spacing
|
|
70
|
+
|
|
71
|
+
# @return [Insets] inset from this layout's own rect.
|
|
72
|
+
attr_reader :padding
|
|
73
|
+
|
|
74
|
+
# @param cells [Integer] blank cells between adjacent children; `>= 0`.
|
|
75
|
+
# @raise [ArgumentError] on a negative value.
|
|
76
|
+
# @return [void]
|
|
77
|
+
def spacing=(cells)
|
|
78
|
+
cells = validate_spacing(cells)
|
|
79
|
+
return if @spacing == cells
|
|
80
|
+
|
|
81
|
+
@spacing = cells
|
|
82
|
+
relayout
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @param insets [Insets, Integer] an Integer becomes a uniform inset.
|
|
86
|
+
# @raise [ArgumentError] on anything else.
|
|
87
|
+
# @return [void]
|
|
88
|
+
def padding=(insets)
|
|
89
|
+
insets = Insets.coerce(insets)
|
|
90
|
+
return if @padding == insets
|
|
91
|
+
|
|
92
|
+
@padding = insets
|
|
93
|
+
relayout
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Adds a child — or every element of an Enumerable, all with the same
|
|
97
|
+
# constraints — and re-runs the layout.
|
|
98
|
+
#
|
|
99
|
+
# add(field, Fixed[1], cross: Fixed[30], align: :center)
|
|
100
|
+
# add([ok, cancel], Fixed[1])
|
|
101
|
+
#
|
|
102
|
+
# @param child [Component, Enumerable<Component>]
|
|
103
|
+
# @param main [Fixed, Percent, Expand] extent along the main axis.
|
|
104
|
+
# @param cross [Fixed, Percent] extent across it.
|
|
105
|
+
# @param align [Symbol] one of {ALIGNMENTS} — where a child narrower than
|
|
106
|
+
# the cross extent sits. {Vertical} / {Horizontal} say which edge
|
|
107
|
+
# `:start` is.
|
|
108
|
+
# @raise [ArgumentError] on an unknown constraint or alignment, or an
|
|
109
|
+
# {Expand} passed as `cross` (see {Expand}).
|
|
110
|
+
# @raise [TypeError] if `child` is not a {Component}.
|
|
111
|
+
# @return [void]
|
|
112
|
+
def add(child, main = Fixed[1], cross: Percent[100], align: :start)
|
|
113
|
+
if child.is_a? Enumerable
|
|
114
|
+
child.each { add(_1, main, cross:, align:) }
|
|
115
|
+
return
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
validate_main(main)
|
|
119
|
+
validate_cross(cross)
|
|
120
|
+
validate_align(align)
|
|
121
|
+
add_child(child)
|
|
122
|
+
@placements[child] = { main:, cross:, align: }
|
|
123
|
+
relayout
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Removes the child, forgets its constraints, and closes the gap it left
|
|
127
|
+
# by re-running the layout.
|
|
128
|
+
# @param child [Component]
|
|
129
|
+
# @return [void]
|
|
130
|
+
def remove(child)
|
|
131
|
+
super
|
|
132
|
+
@placements.delete(child)
|
|
133
|
+
relayout
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# @param new_rect [Rect]
|
|
137
|
+
# @return [void]
|
|
138
|
+
def rect=(new_rect)
|
|
139
|
+
super
|
|
140
|
+
relayout
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
private
|
|
144
|
+
|
|
145
|
+
# Recomputes and assigns every child's rect. Silent until this layout has
|
|
146
|
+
# a rect of its own — {#add} runs during construction, long before a
|
|
147
|
+
# parent assigns one.
|
|
148
|
+
# @return [void]
|
|
149
|
+
def relayout
|
|
150
|
+
return if rect.empty?
|
|
151
|
+
|
|
152
|
+
inner = inner_rect
|
|
153
|
+
if inner.empty?
|
|
154
|
+
children.each { _1.rect = Rect.new(rect.left, rect.top, 0, 0) }
|
|
155
|
+
else
|
|
156
|
+
place_children(inner)
|
|
157
|
+
end
|
|
158
|
+
invalidate
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# @return [Rect] {#rect} with {#padding} taken off each edge; may be
|
|
162
|
+
# {Rect#empty? empty}.
|
|
163
|
+
def inner_rect
|
|
164
|
+
Rect.new(rect.left + padding.left, rect.top + padding.top,
|
|
165
|
+
rect.width - padding.horizontal, rect.height - padding.vertical)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# @param inner [Rect] {#inner_rect}, known non-empty.
|
|
169
|
+
# @return [void]
|
|
170
|
+
def place_children(inner)
|
|
171
|
+
sizes = main_sizes(inner)
|
|
172
|
+
available = cross_extent(inner)
|
|
173
|
+
offset = 0
|
|
174
|
+
children.each_with_index do |child, i|
|
|
175
|
+
cross_offset, cross_size = cross_placement(child, available)
|
|
176
|
+
child.rect = build_rect(inner, offset, sizes[i], cross_offset, cross_size)
|
|
177
|
+
offset += sizes[i] + spacing
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# @param inner [Rect] {#inner_rect}.
|
|
182
|
+
# @return [Array<Integer>] main-axis extent per child, in child order.
|
|
183
|
+
def main_sizes(inner)
|
|
184
|
+
count = children.size
|
|
185
|
+
return [] if count.zero?
|
|
186
|
+
|
|
187
|
+
available = [main_extent(inner) - (spacing * (count - 1)), 0].max
|
|
188
|
+
sizes = Array.new(count, 0)
|
|
189
|
+
expanding = []
|
|
190
|
+
unassigned = available
|
|
191
|
+
|
|
192
|
+
children.each_with_index do |child, i|
|
|
193
|
+
case (constraint = placement(child)[:main])
|
|
194
|
+
when Expand then expanding << i
|
|
195
|
+
when Fixed then unassigned -= (sizes[i] = constraint.cells.clamp(0, unassigned))
|
|
196
|
+
else unassigned -= (sizes[i] = percent_of(available, constraint).clamp(0, unassigned))
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
distribute_expand(sizes, expanding, unassigned) unless expanding.empty?
|
|
201
|
+
sizes
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Splits `slack` between the {Expand} children by weight, writing the
|
|
205
|
+
# results into `sizes`.
|
|
206
|
+
# @param sizes [Array<Integer>] mutated in place.
|
|
207
|
+
# @param indices [Array<Integer>] child indices carrying an {Expand}.
|
|
208
|
+
# @param slack [Integer] cells left over; a negative value yields zeroes.
|
|
209
|
+
# @return [void]
|
|
210
|
+
def distribute_expand(sizes, indices, slack)
|
|
211
|
+
slack = 0 if slack.negative?
|
|
212
|
+
weights = indices.map { placement(children[_1])[:main].weight }
|
|
213
|
+
total = weights.sum
|
|
214
|
+
shares = weights.map { slack * _1 / total }
|
|
215
|
+
# Under one cell is lost per floor, so the remainder can't outrun the
|
|
216
|
+
# share count — the earliest Expand children each take one.
|
|
217
|
+
(slack - shares.sum).times { |i| shares[i] += 1 }
|
|
218
|
+
indices.each_with_index { |child_index, i| sizes[child_index] = shares[i] }
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# @param child [Component]
|
|
222
|
+
# @param available [Integer] cross extent of {#inner_rect}.
|
|
223
|
+
# @return [Array(Integer, Integer)] offset from `inner`'s start edge, and
|
|
224
|
+
# extent, along the cross axis.
|
|
225
|
+
def cross_placement(child, available)
|
|
226
|
+
spec = placement(child)
|
|
227
|
+
size = case (constraint = spec[:cross])
|
|
228
|
+
when Fixed then constraint.cells.clamp(0, available)
|
|
229
|
+
else percent_of(available, constraint).clamp(0, available)
|
|
230
|
+
end
|
|
231
|
+
[align_offset(spec[:align], available - size), size]
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# @param align [Symbol] one of {ALIGNMENTS}.
|
|
235
|
+
# @param slack [Integer] unused cells across the axis.
|
|
236
|
+
# @return [Integer]
|
|
237
|
+
def align_offset(align, slack)
|
|
238
|
+
case align
|
|
239
|
+
when :center then slack / 2
|
|
240
|
+
when :end then slack
|
|
241
|
+
else 0
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# @param extent [Integer]
|
|
246
|
+
# @param constraint [Percent]
|
|
247
|
+
# @return [Integer]
|
|
248
|
+
def percent_of(extent, constraint) = (extent * constraint.percent / 100.0).round
|
|
249
|
+
|
|
250
|
+
# @param child [Component]
|
|
251
|
+
# @return [Hash{Symbol => Object}] the child's `main`/`cross`/`align`.
|
|
252
|
+
def placement(child) = @placements[child] || DEFAULT_PLACEMENT
|
|
253
|
+
|
|
254
|
+
# @param rect [Rect]
|
|
255
|
+
# @return [Integer] the extent along the main axis.
|
|
256
|
+
def main_extent(rect) = raise(NotImplementedError, "#{self.class} must implement main_extent")
|
|
257
|
+
|
|
258
|
+
# @param rect [Rect]
|
|
259
|
+
# @return [Integer] the extent along the cross axis.
|
|
260
|
+
def cross_extent(rect) = raise(NotImplementedError, "#{self.class} must implement cross_extent")
|
|
261
|
+
|
|
262
|
+
# @param inner [Rect] {#inner_rect}, the origin both offsets are relative to.
|
|
263
|
+
# @param main_offset [Integer] cells along the main axis.
|
|
264
|
+
# @param main_size [Integer] extent along the main axis.
|
|
265
|
+
# @param cross_offset [Integer] cells along the cross axis.
|
|
266
|
+
# @param cross_size [Integer] extent along the cross axis.
|
|
267
|
+
# @return [Rect] absolute screen rect for one child.
|
|
268
|
+
def build_rect(inner, main_offset, main_size, cross_offset, cross_size)
|
|
269
|
+
raise NotImplementedError, "#{self.class} must implement build_rect"
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# @param cells [Integer]
|
|
273
|
+
# @raise [ArgumentError] unless `cells` is a non-negative Integer.
|
|
274
|
+
# @return [Integer] `cells`.
|
|
275
|
+
def validate_spacing(cells)
|
|
276
|
+
unless cells.is_a?(Integer) && !cells.negative?
|
|
277
|
+
raise ArgumentError, "spacing expects a non-negative Integer, got #{cells.inspect}"
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
cells
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# @param constraint [Object]
|
|
284
|
+
# @raise [ArgumentError] unless it is a {Fixed}, {Percent} or {Expand}.
|
|
285
|
+
# @return [void]
|
|
286
|
+
def validate_main(constraint)
|
|
287
|
+
return if [Fixed, Percent, Expand].any? { constraint.is_a?(_1) }
|
|
288
|
+
|
|
289
|
+
raise ArgumentError, "expected Fixed, Percent or Expand, got #{constraint.inspect}"
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# @param constraint [Object]
|
|
293
|
+
# @raise [ArgumentError] unless it is a {Fixed} or {Percent}.
|
|
294
|
+
# @return [void]
|
|
295
|
+
def validate_cross(constraint)
|
|
296
|
+
if constraint.is_a? Expand
|
|
297
|
+
raise ArgumentError, "Expand is main-axis only — a child has no siblings competing " \
|
|
298
|
+
"across the axis; use Fixed or Percent for cross:"
|
|
299
|
+
end
|
|
300
|
+
return if constraint.is_a?(Fixed) || constraint.is_a?(Percent)
|
|
301
|
+
|
|
302
|
+
raise ArgumentError, "expected Fixed or Percent for cross:, got #{constraint.inspect}"
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# @param align [Object]
|
|
306
|
+
# @raise [ArgumentError] unless it is one of {ALIGNMENTS}.
|
|
307
|
+
# @return [void]
|
|
308
|
+
def validate_align(align)
|
|
309
|
+
return if ALIGNMENTS.include?(align)
|
|
310
|
+
|
|
311
|
+
raise ArgumentError, "expected one of #{ALIGNMENTS.inspect}, got #{align.inspect}"
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tuile
|
|
4
|
+
class Component
|
|
5
|
+
class Layout
|
|
6
|
+
# Lays children out left to right. The main axis is horizontal, so a
|
|
7
|
+
# child's positional constraint is its **width** and `cross:` is its
|
|
8
|
+
# **height**; `align: :start` is the top edge, `:end` the bottom.
|
|
9
|
+
#
|
|
10
|
+
# split = Component::Layout::Horizontal.new
|
|
11
|
+
# split.add(sidebar, Component::Layout::Fixed[30])
|
|
12
|
+
# split.add(main, Component::Layout::Expand[1]) # takes the rest of the row
|
|
13
|
+
#
|
|
14
|
+
# Inside a subclass the constraints need no prefix at all — see {Box}.
|
|
15
|
+
#
|
|
16
|
+
# See {Box} for the constraint vocabulary and how the space is divided.
|
|
17
|
+
class Horizontal < Box
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
# @param rect [Rect]
|
|
21
|
+
# @return [Integer]
|
|
22
|
+
def main_extent(rect) = rect.width
|
|
23
|
+
|
|
24
|
+
# @param rect [Rect]
|
|
25
|
+
# @return [Integer]
|
|
26
|
+
def cross_extent(rect) = rect.height
|
|
27
|
+
|
|
28
|
+
# @param inner [Rect]
|
|
29
|
+
# @param main_offset [Integer] columns right of `inner`'s left.
|
|
30
|
+
# @param main_size [Integer] width.
|
|
31
|
+
# @param cross_offset [Integer] rows down from `inner`'s top.
|
|
32
|
+
# @param cross_size [Integer] height.
|
|
33
|
+
# @return [Rect]
|
|
34
|
+
def build_rect(inner, main_offset, main_size, cross_offset, cross_size)
|
|
35
|
+
Rect.new(inner.left + main_offset, inner.top + cross_offset, main_size, cross_size)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tuile
|
|
4
|
+
class Component
|
|
5
|
+
class Layout
|
|
6
|
+
# Stacks children top to bottom. The main axis is vertical, so a child's
|
|
7
|
+
# positional constraint is its **height** and `cross:` is its **width**;
|
|
8
|
+
# `align: :start` is the left edge, `:end` the right.
|
|
9
|
+
#
|
|
10
|
+
# form = Component::Layout::Vertical.new(spacing: 1)
|
|
11
|
+
# form.add(caption, Component::Layout::Fixed[1])
|
|
12
|
+
# form.add(field, Component::Layout::Fixed[1], cross: Component::Layout::Fixed[30])
|
|
13
|
+
# form.add(log, Component::Layout::Expand[1]) # takes whatever is left below
|
|
14
|
+
#
|
|
15
|
+
# Inside a subclass the constraints need no prefix at all — see {Box}.
|
|
16
|
+
#
|
|
17
|
+
# See {Box} for the constraint vocabulary and how the space is divided.
|
|
18
|
+
class Vertical < Box
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
# @param rect [Rect]
|
|
22
|
+
# @return [Integer]
|
|
23
|
+
def main_extent(rect) = rect.height
|
|
24
|
+
|
|
25
|
+
# @param rect [Rect]
|
|
26
|
+
# @return [Integer]
|
|
27
|
+
def cross_extent(rect) = rect.width
|
|
28
|
+
|
|
29
|
+
# @param inner [Rect]
|
|
30
|
+
# @param main_offset [Integer] rows down from `inner`'s top.
|
|
31
|
+
# @param main_size [Integer] height.
|
|
32
|
+
# @param cross_offset [Integer] columns right of `inner`'s left.
|
|
33
|
+
# @param cross_size [Integer] width.
|
|
34
|
+
# @return [Rect]
|
|
35
|
+
def build_rect(inner, main_offset, main_size, cross_offset, cross_size)
|
|
36
|
+
Rect.new(inner.left + cross_offset, inner.top + main_offset, cross_size, main_size)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|