Skip to content

Commit fcf7b5d

Browse files
committed
feat: Update layout ✨
1 parent 085710f commit fcf7b5d

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

examples/layout.jl

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ function TUI.init!(m, _)
4242
end
4343

4444
function TUI.update!(m::Model, evt::TUI.KeyEvent)
45-
m.quit = true
45+
if TUI.keypress(evt) == "q"
46+
m.quit = true
47+
end
4648
end
4749

4850
function TUI.view(m::Model)
@@ -51,13 +53,13 @@ function TUI.view(m::Model)
5153
block3 = TUI.Block(; title = "Block 3")
5254

5355
horizontal1 = TUI.Layout(;
54-
widgets = [block1, block2],
55-
constraints = [TUI.Percent(50), TUI.Percent(50)],
56+
widgets = [block1, block2, block3],
57+
constraints = [TUI.Percent(30), TUI.Min(5), TUI.Percent(30)],
5658
orientation = :horizontal,
5759
)
5860
horizontal2 = TUI.Layout(;
5961
widgets = [block1, block2, block3],
60-
constraints = [TUI.Max(5), TUI.Percent(30), TUI.Percent(30)],
62+
constraints = [TUI.Min(5), TUI.Percent(30), TUI.Percent(30)],
6163
orientation = :horizontal,
6264
)
6365
TUI.Layout(;

src/layout.jl

+18-23
Original file line numberDiff line numberDiff line change
@@ -120,45 +120,45 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
120120
]
121121
s = Solver()
122122
for (i, c) in enumerate(layout.constraints)
123-
add_constraint(s, variables[i].x >= 1)
124-
add_constraint(s, variables[i].y >= 1)
123+
add_constraint(s, variables[i].x >= area.x)
124+
add_constraint(s, variables[i].y >= area.y)
125125
add_constraint(s, variables[i].w <= area.width)
126126
add_constraint(s, variables[i].h <= area.height)
127127
if c isa Auto
128128
if orientation == :horizontal
129-
add_constraint(s, @constraint variables[i].w == c.value - 1 strength = KiwiConstraintSolver.STRONG)
129+
add_constraint(s, @constraint variables[i].w == c.value strength = KiwiConstraintSolver.STRONG)
130130
else
131-
add_constraint(s, @constraint variables[i].h == c.value - 1 strength = KiwiConstraintSolver.STRONG)
131+
add_constraint(s, @constraint variables[i].h == c.value strength = KiwiConstraintSolver.STRONG)
132132
end
133133
elseif c isa Percent
134134
if orientation == :horizontal
135135
add_constraint(
136136
s,
137-
@constraint variables[i].w == (c.value * width(area) ÷ 100) - 1 strength = KiwiConstraintSolver.MEDIUM
137+
@constraint variables[i].w == (c.value * width(area) ÷ 100) strength = KiwiConstraintSolver.MEDIUM
138138
)
139139
else
140140
add_constraint(
141141
s,
142-
@constraint variables[i].h == (c.value * height(area) ÷ 100) - 1 strength = KiwiConstraintSolver.MEDIUM
142+
@constraint variables[i].h == (c.value * height(area) ÷ 100) strength = KiwiConstraintSolver.MEDIUM
143143
)
144144
end
145145
elseif c isa Fixed
146146
if orientation == :horizontal
147-
add_constraint(s, variables[i].w == c.value - 1)
147+
add_constraint(s, variables[i].w == c.value)
148148
else
149-
add_constraint(s, variables[i].h == c.value - 1)
149+
add_constraint(s, variables[i].h == c.value)
150150
end
151151
elseif c isa Min
152152
if orientation == :horizontal
153-
add_constraint(s, variables[i].w >= c.value - 1)
153+
add_constraint(s, variables[i].w >= c.value)
154154
else
155-
add_constraint(s, variables[i].h >= c.value - 1)
155+
add_constraint(s, variables[i].h >= c.value)
156156
end
157157
elseif c isa Max
158158
if orientation == :horizontal
159-
add_constraint(s, variables[i].w <= c.value - 1)
159+
add_constraint(s, variables[i].w <= c.value)
160160
else
161-
add_constraint(s, variables[i].h <= c.value - 1)
161+
add_constraint(s, variables[i].h <= c.value)
162162
end
163163
end
164164
end
@@ -179,28 +179,23 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
179179
end
180180
end
181181
if orientation == :horizontal
182-
add_constraint(s, sum(variables[i].w for (i, c) in enumerate(layout.constraints)) == area.width)
182+
add_constraint(s, sum(variables[i].w for (i, c) in enumerate(layout.constraints)) == width(area))
183183
else
184-
add_constraint(s, sum(variables[i].h for (i, c) in enumerate(layout.constraints)) == area.height)
184+
add_constraint(s, sum(variables[i].h for (i, c) in enumerate(layout.constraints)) == height(area))
185185
end
186186
for (i, c1) in enumerate(layout.constraints), (j, c2) in enumerate(layout.constraints)
187187
if j <= i
188188
continue
189189
end
190190
if orientation == :horizontal
191-
add_constraint(
192-
s,
193-
KiwiConstraintSolver.Constraint(variables[i].w - variables[j].w, KiwiConstraintSolver.WEAK, :(==)),
194-
)
191+
add_constraint(s, @constraint variables[i].w == variables[j].w strength = KiwiConstraintSolver.WEAK)
195192
else
196-
add_constraint(
197-
s,
198-
KiwiConstraintSolver.Constraint(variables[i].h - variables[j].h, KiwiConstraintSolver.WEAK, :(==)),
199-
)
193+
add_constraint(s, @constraint variables[i].h == variables[j].h strength = KiwiConstraintSolver.WEAK)
200194
end
201195
end
202196
update_variables(s)
203-
[Rect(round(v.x.value), round(v.y.value), round(v.w.value), round(v.h.value)) for v in variables]
197+
rects = [Rect(round(v.x.value), round(v.y.value), round(v.w.value), round(v.h.value)) for v in variables]
198+
rects
204199
end
205200

206201
@testset "layout-rects" begin

src/widgets/layout.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function render(layout::Layout, area::Rect, buf::Buffer)
1616
split(Horizontal(; constraints = layout.constraints), area)
1717
end
1818

19-
for (widget, rect) in zip(layout.widgets, rects)
19+
for (i, (widget, rect)) in enumerate(zip(layout.widgets, rects))
2020
render(widget, rect, buf)
2121
end
2222
end

0 commit comments

Comments
 (0)