Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kwarg to rotate Toggle #4471

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- Add kwarg to rotate Toggle [#4445](https://github.com/MakieOrg/Makie.jl/pull/4445)
- Allow plots to move between scenes in SpecApi [#4132](https://github.com/MakieOrg/Makie.jl/pull/4132).
- Added empty constructor to all backends for `Screen` allowing `display(Makie.current_backend().Screen(), fig)` [#4561](https://github.com/MakieOrg/Makie.jl/pull/4561).
- Added `subsup` and `left_subsup` functions that offer stacked sub- and superscripts for `rich` text which means this style can be used with arbitrary fonts and is not limited to fonts supported by MathTeXEngine.jl [#4489](https://github.com/MakieOrg/Makie.jl/pull/4489).
Expand Down
8 changes: 8 additions & 0 deletions ReferenceTests/src/tests/figures_and_makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,14 @@ end
tb = Textbox(gl[1, 3], bordercolor = :black, cornerradius = 20,
fontsize =10, textcolor = :red, boxcolor = :lightblue)
Makie.set!(tb, "some string")
end

@reference_test "Toggle" begin
f = Figure()
scatter(f[1,1], [1,2,3], axis = (; height=200))
th = Makie.Toggle(f[1,2], valign=:top)
th.orientation[] = pi/2
tv = Makie.Toggle(f[1,3], valign=:top, orientation=pi/2)
tv.orientation[] = 0
f
end
29 changes: 19 additions & 10 deletions src/makielayout/blocks/toggle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@

topscene = t.blockscene

markersize = lift(topscene, t.layoutobservables.computedbbox) do bbox
min(width(bbox), height(bbox))
onany(topscene, t.orientation, t.length, t.markersize) do or, len, ms
theta = or == :horizontal ? 0 : or == :vertical ? pi/2 : or
t.width[] = (len - ms) * cos(theta) + ms
t.height[] = (len - ms) * sin(theta) + ms

Check warning on line 8 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L5-L8

Added lines #L5 - L8 were not covered by tests
end

button_endpoint_inactive = lift(topscene, markersize) do ms
bbox = t.layoutobservables.computedbbox[]
button_endpoint_inactive = lift(topscene, t.markersize, t.layoutobservables.computedbbox) do ms, bbox

Check warning on line 11 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L11

Added line #L11 was not covered by tests

Point2f(left(bbox) + ms / 2, bottom(bbox) + ms / 2)
end

button_endpoint_active = lift(topscene, markersize) do ms
bbox = t.layoutobservables.computedbbox[]
Point2f(right(bbox) - ms / 2, bottom(bbox) + ms / 2)
button_endpoint_active = lift(topscene, t.markersize, t.layoutobservables.computedbbox) do ms, bbox
Point2f(right(bbox) - ms / 2, top(bbox) - ms / 2)

Check warning on line 17 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L16-L17

Added lines #L16 - L17 were not covered by tests
end

buttonvertices = lift(topscene, markersize, t.cornersegments) do ms, cs
roundedrectvertices(t.layoutobservables.computedbbox[], ms * 0.499, cs)
buttonvertices = lift(topscene, t.length, t.markersize, t.cornersegments, t.orientation, t.layoutobservables.computedbbox) do len, ms, cs, or, bbox
rect0 = GeometryBasics.HyperRectangle(-ms/2, -ms/2, len, ms)
rrv = roundedrectvertices(rect0, ms * 0.499, cs)
theta = or == :horizontal ? 0 : or == :vertical ? pi/2 : or
costheta, sintheta = cos(theta), sin(theta)
rotmatrix = GeometryBasics.@SMatrix [costheta -sintheta

Check warning on line 25 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L20-L25

Added lines #L20 - L25 were not covered by tests
sintheta costheta]
tranvector = Ref(GeometryBasics.@SVector [left(bbox)+ms/2, bottom(bbox)+ms/2])
Ref(rotmatrix) .* rrv .+ tranvector

Check warning on line 28 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L27-L28

Added lines #L27 - L28 were not covered by tests
end

# trigger bbox
notify(t.length)

Check warning on line 32 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L32

Added line #L32 was not covered by tests
notify(t.layoutobservables.suggestedbbox)

framecolor = Observable{Any}(t.active[] ? t.framecolor_active[] : t.framecolor_inactive[])
Expand All @@ -37,7 +46,7 @@
end

buttonfactor = Observable(1.0)
buttonsize = lift(topscene, markersize, t.rimfraction, buttonfactor) do ms, rf, bf
buttonsize = lift(topscene, t.markersize, t.rimfraction, buttonfactor) do ms, rf, bf

Check warning on line 49 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L49

Added line #L49 was not covered by tests
ms * (1 - rf) * bf
end

Expand Down
35 changes: 31 additions & 4 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1152,16 +1152,37 @@ const CHECKMARK_BEZIER = scale(BezierPath(
end
end

"""
A switch with two states.

## Constructors

```julia
Toggle(fig_or_scene; kwargs...)
```

## Examples

```julia
t_horizontal = Toggle(fig[1, 1])
t_vertical = Toggle(fig[2, 1], orientation = :vertical)
t_diagonal = Toggle(fig[3, 1], orientation = pi/4)
on(t_vertical.active) do switch_is_on
switch_is_on ? println("good morning!") : println("good night")
end
```

"""
@Block Toggle begin
@attributes begin
"The horizontal alignment of the toggle in its suggested bounding box."
halign = :center
"The vertical alignment of the toggle in its suggested bounding box."
valign = :center
"The width of the toggle."
width = 32
"The height of the toggle."
height = 18
"The width of the bounding box. Use `length` and `markersize` to set the dimensions of the toggle."
width = Auto()
"The height of the bounding box. Use `length` and `markersize` to set the dimensions of the toggle."
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 +1206,12 @@ end
rimfraction = 0.33
"The align mode of the toggle in its parent GridLayout."
alignmode = Inside()
"The orientation of the toggle. Can be :horizontal, :vertical, or -pi to pi. 0 is horizontal with \"on\" being to the right."
orientation = :horizontal
"The length of the toggle."
length = 32
"The size of the button."
markersize = 18
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 @@ -547,3 +547,10 @@ end
end
@test isempty(limits.listeners)
end

@testset "Toggle" begin
f = Figure()
Toggle(f[1,1])
Toggle(f[2,1], orientation=:vertical)
Toggle(f[3,1], orientation=pi/4)
end
Loading