Skip to content

Commit

Permalink
add vertical orientation option to Toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed Oct 3, 2024
1 parent 2bb4de0 commit 2af0828
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
10 changes: 9 additions & 1 deletion ReferenceTests/src/tests/figures_and_makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,12 @@ end
Makie.Checkbox(f[2, 4], checked = false, checkboxcolor_unchecked = :yellow)
Makie.Checkbox(f[2, 5], checked = true, checkboxcolor_checked = :orange)
f
end
end

@reference_test "Toggle" begin
f = Figure()
th = Makie.Toggle(f[1,1])
th.orientation[] = :vertical
tv = Makie.Toggle(f[2,1], orientation=:vertical)
tv.orientation[] = :horizontal
end
29 changes: 25 additions & 4 deletions src/makielayout/blocks/toggle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,39 @@ function initialize_block!(t::Toggle)

topscene = t.blockscene

on(t.orientation) do or
if or == :horizontal
t.width[] = 32
t.height[] = 18
elseif or == :vertical
t.width[] = 18
t.height[] = 32
else
error("orientation must be either :horizontal or :vertical")
end
end
notify(t.orientation)

markersize = lift(topscene, t.layoutobservables.computedbbox) do bbox
min(width(bbox), height(bbox))
end

button_endpoint_inactive = lift(topscene, markersize) do ms
button_endpoint_inactive = lift(topscene, markersize, t.orientation) do ms, or
bbox = t.layoutobservables.computedbbox[]
Point2f(left(bbox) + ms / 2, bottom(bbox) + ms / 2)
if or == :horizontal
Point2f(left(bbox) + ms / 2, bottom(bbox) + ms / 2)
elseif or == :vertical
Point2f(left(bbox) + ms / 2, bottom(bbox) + ms / 2)
end
end

button_endpoint_active = lift(topscene, markersize) do ms
button_endpoint_active = lift(topscene, markersize, t.orientation) do ms, or
bbox = t.layoutobservables.computedbbox[]
Point2f(right(bbox) - ms / 2, bottom(bbox) + ms / 2)
if or == :horizontal
Point2f(right(bbox) - ms / 2, bottom(bbox) + ms / 2)
elseif or == :vertical
Point2f(left(bbox) + ms / 2, top(bbox) - ms / 2)
end
end

buttonvertices = lift(topscene, markersize, t.cornersegments) do ms, cs
Expand Down
6 changes: 4 additions & 2 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1159,9 +1159,9 @@ end
"The vertical alignment of the toggle in its suggested bounding box."
valign = :center
"The width of the toggle."
width = 32
width = Auto()
"The height of the toggle."
height = 18
height = Auto()
"Controls if the parent layout can adjust to this element's width"
tellwidth = true
"Controls if the parent layout can adjust to this element's height"
Expand All @@ -1185,6 +1185,8 @@ end
rimfraction = 0.33
"The align mode of the toggle in its parent GridLayout."
alignmode = Inside()
"The orientation of the toggle (:horizontal or :vertical)."
orientation = :horizontal
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,10 @@ end
end
@test isempty(limits.listeners)
end

@testset "Toggle" begin
f = Figure()
Toggle(f[1,1])
Toggle(f[2,1], orientation=:vertical)
@test_throws ErrorException Toggle(f[3,1], orientation=:diagonal)
end

0 comments on commit 2af0828

Please sign in to comment.