Skip to content

Commit 085710f

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

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

Project.toml

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ version = "0.6.0"
77
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
88
Crossterm = "89a1a94d-67fa-4c27-8677-ab6d33b1ad6c"
99
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
10-
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
1110
InlineTest = "bd334432-b1e7-49c7-a2dc-dd9149e4ebd6"
1211
KiwiConstraintSolver = "063317df-b1e6-4e84-b689-d7ba7b3726e4"
1312
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"

examples/layout.jl

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

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

5048
function TUI.view(m::Model)
@@ -53,8 +51,8 @@ function TUI.view(m::Model)
5351
block3 = TUI.Block(; title = "Block 3")
5452

5553
horizontal1 = TUI.Layout(;
56-
widgets = [block1, block2, block3],
57-
constraints = [TUI.Percent(30), TUI.Max(5), TUI.Percent(30)],
54+
widgets = [block1, block2],
55+
constraints = [TUI.Percent(50), TUI.Percent(50)],
5856
orientation = :horizontal,
5957
)
6058
horizontal2 = TUI.Layout(;

src/TerminalUserInterfaces.jl

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ using Crayons
66
using TextWrap
77
using Unicode
88
using InlineTest
9-
using GLPK
109
using KiwiConstraintSolver
1110

1211
export Terminal

src/layout.jl

+26-14
Original file line numberDiff line numberDiff line change
@@ -126,46 +126,58 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
126126
add_constraint(s, variables[i].h <= area.height)
127127
if c isa Auto
128128
if orientation == :horizontal
129-
add_constraint(s, KiwiConstraintSolver.Constraint(variables[i].w - c.value, KiwiConstraintSolver.STRONG, :(==)))
129+
add_constraint(s, @constraint variables[i].w == c.value - 1 strength = KiwiConstraintSolver.STRONG)
130130
else
131-
add_constraint(s, KiwiConstraintSolver.Constraint(variables[i].h - c.value, KiwiConstraintSolver.STRONG, :(==)))
131+
add_constraint(s, @constraint variables[i].h == c.value - 1 strength = KiwiConstraintSolver.STRONG)
132132
end
133133
elseif c isa Percent
134134
if orientation == :horizontal
135135
add_constraint(
136136
s,
137-
KiwiConstraintSolver.Constraint(variables[i].w - (c.value ÷ width(area)), KiwiConstraintSolver.MEDIUM, :(==)),
137+
@constraint variables[i].w == (c.value * width(area) ÷ 100) - 1 strength = KiwiConstraintSolver.MEDIUM
138138
)
139139
else
140140
add_constraint(
141141
s,
142-
KiwiConstraintSolver.Constraint(
143-
variables[i].h - (c.value ÷ height(area)),
144-
KiwiConstraintSolver.MEDIUM,
145-
:(==),
146-
),
142+
@constraint variables[i].h == (c.value * height(area) ÷ 100) - 1 strength = KiwiConstraintSolver.MEDIUM
147143
)
148144
end
149145
elseif c isa Fixed
150146
if orientation == :horizontal
151-
add_constraint(s, variables[i].w == c.value)
147+
add_constraint(s, variables[i].w == c.value - 1)
152148
else
153-
add_constraint(s, variables[i].h == c.value)
149+
add_constraint(s, variables[i].h == c.value - 1)
154150
end
155151
elseif c isa Min
156152
if orientation == :horizontal
157-
add_constraint(s, variables[i].w >= c.value)
153+
add_constraint(s, variables[i].w >= c.value - 1)
158154
else
159-
add_constraint(s, variables[i].h >= c.value)
155+
add_constraint(s, variables[i].h >= c.value - 1)
160156
end
161157
elseif c isa Max
162158
if orientation == :horizontal
163-
add_constraint(s, variables[i].w <= c.value)
159+
add_constraint(s, variables[i].w <= c.value - 1)
164160
else
165-
add_constraint(s, variables[i].h <= c.value)
161+
add_constraint(s, variables[i].h <= c.value - 1)
166162
end
167163
end
168164
end
165+
for (i, c) in enumerate(layout.constraints)
166+
if i == 1
167+
continue
168+
end
169+
if orientation == :horizontal
170+
add_constraint(
171+
s,
172+
@constraint variables[i].x == variables[i-1].x + variables[i-1].w + 1 strength = KiwiConstraintSolver.MEDIUM
173+
)
174+
else
175+
add_constraint(
176+
s,
177+
@constraint variables[i].y == variables[i-1].y + variables[i-1].h + 1 strength = KiwiConstraintSolver.MEDIUM
178+
)
179+
end
180+
end
169181
if orientation == :horizontal
170182
add_constraint(s, sum(variables[i].w for (i, c) in enumerate(layout.constraints)) == area.width)
171183
else

src/terminal.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,15 @@ area(t::Terminal) = t.terminal_size[]
251251

252252
function tui(f::Function; flags...)
253253
r = nothing
254-
err = nothing
255254
tui(true; flags...)
256255
try
257256
r = f()
258257
catch err
258+
tui(false; flags...)
259+
rethrow(err)
259260
finally
260261
tui(false; flags...)
261262
end
262-
!isnothing(err) && throw(err)
263263
return r
264264
end
265265

0 commit comments

Comments
 (0)