@@ -118,11 +118,25 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
118
118
]
119
119
s = Solver ()
120
120
for (i, c) in enumerate (layout. constraints)
121
- add_constraint (s, variables[i]. x >= area. x)
122
- add_constraint (s, variables[i]. y >= area. y)
123
- add_constraint (s, variables[i]. w <= area. width)
124
- add_constraint (s, variables[i]. h <= area. height)
125
- if c isa Auto
121
+ if c isa Fixed
122
+ if orientation == :horizontal
123
+ add_constraint (s, variables[i]. w == c. value)
124
+ else
125
+ add_constraint (s, variables[i]. h == c. value)
126
+ end
127
+ elseif c isa Min
128
+ if orientation == :horizontal
129
+ add_constraint (s, variables[i]. w >= c. value)
130
+ else
131
+ add_constraint (s, variables[i]. h >= c. value)
132
+ end
133
+ elseif c isa Max
134
+ if orientation == :horizontal
135
+ add_constraint (s, variables[i]. w <= c. value)
136
+ else
137
+ add_constraint (s, variables[i]. h <= c. value)
138
+ end
139
+ elseif c isa Auto
126
140
if orientation == :horizontal
127
141
add_constraint (s, @constraint variables[i]. w == c. value strength = KiwiConstraintSolver. STRONG)
128
142
else
@@ -140,42 +154,33 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
140
154
@constraint variables[i]. h == (c. value * height (area) ÷ 100 ) strength = KiwiConstraintSolver. MEDIUM
141
155
)
142
156
end
143
- elseif c isa Fixed
144
- if orientation == :horizontal
145
- add_constraint (s, variables[i] . w == c . value )
146
- else
147
- add_constraint (s, variables[i]. h == c . value )
148
- end
149
- elseif c isa Min
157
+ end
158
+ end
159
+ for (i, c) in enumerate (layout . constraints )
160
+ if i == 1
161
+ add_constraint (s, variables[i]. x >= area . x )
162
+ add_constraint (s, variables[i] . y >= area . y)
163
+ else
150
164
if orientation == :horizontal
151
- add_constraint (s, variables[i]. w >= c . value )
165
+ add_constraint (s, variables[i]. x == variables[i - 1 ] . x + variables[i - 1 ] . w + 1 )
152
166
else
153
- add_constraint (s, variables[i]. h >= c . value )
167
+ add_constraint (s, variables[i]. y == variables[i - 1 ] . y + variables[i - 1 ] . h + 1 )
154
168
end
155
- elseif c isa Max
169
+ end
170
+ if i == length (layout. constraints)
156
171
if orientation == :horizontal
157
- add_constraint (s, variables[i]. w <= c. value)
172
+ add_constraint (
173
+ s,
174
+ @constraint variables[i]. x + variables[i]. w == width (area) strength = KiwiConstraintSolver. STRONG
175
+ )
158
176
else
159
- add_constraint (s, variables[i]. h <= c. value)
177
+ add_constraint (
178
+ s,
179
+ @constraint variables[i]. y + variables[i]. h == height (area) strength = KiwiConstraintSolver. STRONG
180
+ )
160
181
end
161
182
end
162
183
end
163
- for (i, c) in enumerate (layout. constraints)
164
- if i == 1
165
- continue
166
- end
167
- if orientation == :horizontal
168
- add_constraint (
169
- s,
170
- @constraint variables[i]. x == variables[i- 1 ]. x + variables[i- 1 ]. w + 1 strength = KiwiConstraintSolver. MEDIUM
171
- )
172
- else
173
- add_constraint (
174
- s,
175
- @constraint variables[i]. y == variables[i- 1 ]. y + variables[i- 1 ]. h + 1 strength = KiwiConstraintSolver. MEDIUM
176
- )
177
- end
178
- end
179
184
if orientation == :horizontal
180
185
add_constraint (s, sum (variables[i]. w for (i, c) in enumerate (layout. constraints)) == width (area))
181
186
else
@@ -192,10 +197,33 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
192
197
end
193
198
end
194
199
update_variables (s)
195
- rects = [Rect (round (v. x. value), round (v. y. value), round (v. w. value), round (v. h. value)) for v in variables]
200
+ rects = if orientation == :horizontal
201
+ [Rect (round (v. x. value), top (area), round (v. w. value), height (area)) for v in variables]
202
+ else
203
+ [Rect (left (area), round (v. y. value), width (area), round (v. h. value)) for v in variables]
204
+ end
196
205
rects
197
206
end
198
207
208
+ function testing ()
209
+ # TUI.tui(; alternate_screen = false) do
210
+ # t = TUI.Terminal()
211
+ # TUI.render(t, TUI.Block(), r1)
212
+ # TUI.render(t, TUI.Block(), r2)
213
+ # TUI.render(t, TUI.Block(), r3)
214
+ # TUI.draw(t)
215
+ # end
216
+ end
217
+
218
+ @testset " layout-rects-gaps" begin
219
+ target = Rect (; x = 2 , y = 2 , width = 10 , height = 10 )
220
+ constraints = [Percent (10 ), Max (5 ), Min (1 )]
221
+ r1, r2, r3 = split (Horizontal (constraints), target)
222
+ @test r1 == Rect (; x = 2 , y = 2 , width = 1 , height = 10 )
223
+ @test r2 == Rect (; x = 4 , y = 2 , width = 4 , height = 10 )
224
+ @test r3 == Rect (; x = 10 , y = 2 , width = 4 , height = 10 )
225
+ end
226
+
199
227
@testset " layout-rects-fixed" begin
200
228
constraints = [Fixed (50 ), Fixed (50 )]
201
229
r1, r2 = split (Horizontal (constraints), Rect (0 , 0 , 100 , 1 ))
0 commit comments