tuile 0.10.0 → 0.12.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 +109 -64
- data/DECISIONS.md +1299 -22
- data/README.md +18 -13
- data/TERMINOLOGY.md +61 -0
- data/book/02-repaint.md +1 -1
- data/book/03-layout.md +154 -9
- data/book/05-focus.md +2 -0
- data/book/06-theming.md +1 -1
- data/book/07-components.md +202 -37
- data/book/README.md +3 -1
- data/examples/file_commander.rb +5 -4
- data/examples/sampler.rb +320 -133
- data/ideas/arrow-key-navigation.md +205 -0
- data/ideas/new-components.md +26 -12
- data/lib/tuile/buffer.rb +7 -7
- data/lib/tuile/component/big_decimal_field.rb +199 -0
- data/lib/tuile/component/button.rb +1 -1
- data/lib/tuile/component/checkbox.rb +11 -10
- data/lib/tuile/component/checkbox_group.rb +31 -26
- data/lib/tuile/component/combo_box.rb +18 -33
- data/lib/tuile/component/float_field.rb +161 -0
- data/lib/tuile/component/info_window.rb +1 -1
- data/lib/tuile/component/label.rb +14 -14
- 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.rb +291 -216
- data/lib/tuile/component/list_dropdown.rb +82 -24
- data/lib/tuile/component/notification.rb +317 -0
- data/lib/tuile/component/picker_window.rb +3 -3
- data/lib/tuile/component/popup.rb +8 -10
- data/lib/tuile/component/progress_bar.rb +1 -1
- data/lib/tuile/component/radio_group.rb +32 -30
- data/lib/tuile/component/select.rb +251 -0
- data/lib/tuile/component/text_area/wrapped_text.rb +320 -0
- data/lib/tuile/component/text_area.rb +79 -273
- data/lib/tuile/component/text_field.rb +1 -1
- data/lib/tuile/component/text_view.rb +191 -177
- data/lib/tuile/component/window.rb +8 -8
- data/lib/tuile/component.rb +5 -5
- data/lib/tuile/screen.rb +1 -1
- data/lib/tuile/styled_string.rb +25 -15
- data/lib/tuile/version.rb +1 -1
- data/lib/tuile/vertical_scroll_bar.rb +6 -6
- data/lib/tuile.rb +4 -0
- data/sig/tuile.rbs +1670 -406
- metadata +11 -1
|
@@ -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
|
|
@@ -3,7 +3,13 @@
|
|
|
3
3
|
module Tuile
|
|
4
4
|
class Component
|
|
5
5
|
# A layout doesn't paint anything by itself: its job is to position child
|
|
6
|
-
# components.
|
|
6
|
+
# components. Two families, both top-down (see book ch3):
|
|
7
|
+
#
|
|
8
|
+
# - {Absolute} — you override {Component#rect=} and compute every child's
|
|
9
|
+
# rectangle yourself. Total control, and the base for anything unusual.
|
|
10
|
+
# - {Box} / {Vertical} / {Horizontal} — you declare each child's extent as
|
|
11
|
+
# a {Fixed}, {Percent} or {Expand} constraint and the layout does the
|
|
12
|
+
# arithmetic. Sugar over the same `rect=` assignment, for the common case.
|
|
7
13
|
#
|
|
8
14
|
# Children that fully tile the layout's rect repaint themselves and
|
|
9
15
|
# cover everything; children that leave gaps (e.g. a form with widgets
|
|
@@ -11,6 +17,148 @@ module Tuile
|
|
|
11
17
|
# the background is cleared and children are re-invalidated so they
|
|
12
18
|
# paint over a clean surface.
|
|
13
19
|
class Layout < Component
|
|
20
|
+
# How much space a child gets along one axis of a {Box}: exactly {#cells},
|
|
21
|
+
# clamped to whatever is still unassigned.
|
|
22
|
+
#
|
|
23
|
+
# add(prompt, Fixed[4]) # 4 rows in a Vertical
|
|
24
|
+
# add(field, Fixed[1], cross: Fixed[30]) # 1 row, 30 columns wide
|
|
25
|
+
#
|
|
26
|
+
# `Fixed[0]` hides the child — it gets an empty rect and paints nothing.
|
|
27
|
+
#
|
|
28
|
+
# @!attribute [r] cells
|
|
29
|
+
# @return [Integer] cell count along the axis.
|
|
30
|
+
class Fixed < Data.define(:cells)
|
|
31
|
+
# @param cells [Integer] cell count along the axis; `>= 0`.
|
|
32
|
+
# @raise [ArgumentError] unless `cells` is a non-negative Integer.
|
|
33
|
+
def initialize(cells:)
|
|
34
|
+
unless cells.is_a?(Integer) && !cells.negative?
|
|
35
|
+
raise ArgumentError, "Fixed expects a non-negative Integer, got #{cells.inspect}"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
super
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# A percentage of the space *available* along a {Box}'s axis — measured
|
|
43
|
+
# after {Box#padding} and {Box#spacing} have come off, so two `Percent[50]`
|
|
44
|
+
# children fit exactly rather than overflowing by the gap between them.
|
|
45
|
+
#
|
|
46
|
+
# add(left, Percent[60])
|
|
47
|
+
# add(right, Percent[40])
|
|
48
|
+
#
|
|
49
|
+
# @!attribute [r] percent
|
|
50
|
+
# @return [Numeric] percentage of the available extent, `0..100`.
|
|
51
|
+
class Percent < Data.define(:percent)
|
|
52
|
+
# @param percent [Numeric] percentage of the available extent, `0..100`.
|
|
53
|
+
# @raise [ArgumentError] unless `percent` is a Numeric in `0..100`.
|
|
54
|
+
def initialize(percent:)
|
|
55
|
+
unless percent.is_a?(Numeric) && percent.between?(0, 100)
|
|
56
|
+
raise ArgumentError, "Percent expects a Numeric in 0..100, got #{percent.inspect}"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
super
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# A share of whatever a {Box} has left once its {Fixed} and {Percent}
|
|
64
|
+
# children have taken theirs, split between the `Expand` children in
|
|
65
|
+
# proportion to their weights:
|
|
66
|
+
#
|
|
67
|
+
# add(header, Fixed[1])
|
|
68
|
+
# add(body, Expand[2]) # gets twice…
|
|
69
|
+
# add(side, Expand[1]) # …what this one gets
|
|
70
|
+
#
|
|
71
|
+
# Main axis only — {Box#add} rejects one passed as `cross:`, where a child
|
|
72
|
+
# has no siblings to compete with and so nothing for a weight to mean.
|
|
73
|
+
#
|
|
74
|
+
# @!attribute [r] weight
|
|
75
|
+
# @return [Integer] relative share of the leftover space.
|
|
76
|
+
class Expand < Data.define(:weight)
|
|
77
|
+
# @param weight [Integer] relative share; `>= 1`.
|
|
78
|
+
# @raise [ArgumentError] unless `weight` is a positive Integer.
|
|
79
|
+
def initialize(weight:)
|
|
80
|
+
unless weight.is_a?(Integer) && weight.positive?
|
|
81
|
+
raise ArgumentError, "Expand expects a positive Integer weight, got #{weight.inspect}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
super
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Per-edge padding for a {Box}, in cells:
|
|
89
|
+
#
|
|
90
|
+
# Insets[top: 1] # one blank row above the children
|
|
91
|
+
# Insets[top: 1, left: 2, right: 2] # unnamed edges default to 0
|
|
92
|
+
# Insets.coerce(1) # uniform on all four edges
|
|
93
|
+
#
|
|
94
|
+
# Keyword-only: AWT and JavaFX order these same four numbers differently,
|
|
95
|
+
# so a positional form would be a coin flip.
|
|
96
|
+
#
|
|
97
|
+
# @!attribute [r] top
|
|
98
|
+
# @return [Integer] cells inset from the top edge.
|
|
99
|
+
# @!attribute [r] right
|
|
100
|
+
# @return [Integer] cells inset from the right edge.
|
|
101
|
+
# @!attribute [r] bottom
|
|
102
|
+
# @return [Integer] cells inset from the bottom edge.
|
|
103
|
+
# @!attribute [r] left
|
|
104
|
+
# @return [Integer] cells inset from the left edge.
|
|
105
|
+
class Insets < Data.define(:top, :right, :bottom, :left)
|
|
106
|
+
# @param positional [Array] must be empty — see the class doc.
|
|
107
|
+
# @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
|
|
108
|
+
# @raise [ArgumentError] if any positional argument is given.
|
|
109
|
+
# @return [Insets]
|
|
110
|
+
def self.new(*positional, **kwargs)
|
|
111
|
+
raise ArgumentError, "Insets is keyword-only, got #{positional.inspect}" unless positional.empty?
|
|
112
|
+
|
|
113
|
+
super(**kwargs)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Needed because `Data`'s inherited `[]` never dispatches through a `new`
|
|
117
|
+
# override, so the guard above alone would miss `Insets[1, 2, 3, 4]`.
|
|
118
|
+
# @param positional [Array] must be empty.
|
|
119
|
+
# @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
|
|
120
|
+
# @raise [ArgumentError] if any positional argument is given.
|
|
121
|
+
# @return [Insets]
|
|
122
|
+
def self.[](*positional, **kwargs) = new(*positional, **kwargs)
|
|
123
|
+
|
|
124
|
+
# @param value [Insets, Integer] an Integer becomes a uniform inset.
|
|
125
|
+
# @raise [ArgumentError] on anything else, or a negative Integer.
|
|
126
|
+
# @return [Insets]
|
|
127
|
+
def self.coerce(value)
|
|
128
|
+
return value if value.is_a?(Insets)
|
|
129
|
+
unless value.is_a?(Integer) && !value.negative?
|
|
130
|
+
raise ArgumentError, "expected Insets or a non-negative Integer, got #{value.inspect}"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
new(top: value, right: value, bottom: value, left: value)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# @param top [Integer] cells inset from the top edge; `>= 0`.
|
|
137
|
+
# @param right [Integer] cells inset from the right edge; `>= 0`.
|
|
138
|
+
# @param bottom [Integer] cells inset from the bottom edge; `>= 0`.
|
|
139
|
+
# @param left [Integer] cells inset from the left edge; `>= 0`.
|
|
140
|
+
# @raise [ArgumentError] unless every edge is a non-negative Integer.
|
|
141
|
+
def initialize(top: 0, right: 0, bottom: 0, left: 0)
|
|
142
|
+
{ top:, right:, bottom:, left: }.each do |edge, cells|
|
|
143
|
+
unless cells.is_a?(Integer) && !cells.negative?
|
|
144
|
+
raise ArgumentError, "Insets #{edge}: expected a non-negative Integer, got #{cells.inspect}"
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
super
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# @return [Integer] `left` + `right`.
|
|
152
|
+
def horizontal = left + right
|
|
153
|
+
|
|
154
|
+
# @return [Integer] `top` + `bottom`.
|
|
155
|
+
def vertical = top + bottom
|
|
156
|
+
|
|
157
|
+
# No padding on any edge.
|
|
158
|
+
# @return [Insets]
|
|
159
|
+
ZERO = new
|
|
160
|
+
end
|
|
161
|
+
|
|
14
162
|
# Layouts are focusable containers — like {Window} and {Popup}, they
|
|
15
163
|
# don't accept input themselves but they need to participate in the
|
|
16
164
|
# {HasContent} focus cascade so a Popup wrapping a Layout wrapping a
|