Skip to content

Commit 5efc102

Browse files
committed
refactor per jkrumbiegel's review
1 parent 08ec9d1 commit 5efc102

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

Diff for: src/makielayout/blocks.jl

+24-11
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,11 @@ function REPL.fielddoc(t::Type{<:Block}, s::Symbol)
617617
end
618618

619619
"""
620-
function tooltip!(b::Block, str::AbstractString; placement=:above, kwargs...)
620+
function tooltip!(b::Block, str::AbstractString; placement=:above, visible=:always, depth=9e3, kwargs...)
621621
622-
Adds a tooltip to a block. See `tooltip` for more details. To make it only
623-
appear when hovering, set the `visible` property of the tooltip to the
624-
`hovering` property of the block.
622+
Adds a tooltip to a block. `visible` can be `:always`, `:hover`, or `:never`.
623+
`depth` should be large to ensure that the tooltip is in front. See `tooltip`
624+
for more details.
625625
626626
# Examples
627627
```julia-repl
@@ -630,20 +630,23 @@ julia> f = Figure()
630630
julia> t = Toggle(f[1,1])
631631
Toggle()
632632
633-
julia> tooltip!(t, "I'm a Toggle", visible = t.hovering)
633+
julia> tooltip!(t, "I'm a Toggle", visible = :hover)
634634
Plot{Makie.tooltip, Tuple{Vec{2, Float32}, String}}
635635
636636
julia> b = Button(f[2,1])
637637
Button()
638638
639-
julia> tt = tooltip!(b, "I'm a Button", placement = :below)
639+
julia> v = Observable(:never)
640+
Observable(:never)
641+
642+
julia> tt = tooltip!(b, "I'm a Button", placement = :below, visible = v)
640643
Plot{Makie.tooltip, Tuple{Vec{2, Float32}, String}}
641644
642-
julia> on(h -> tt.visible[]=h, b.hovering)
643-
ObserverFunction defined at REPL[7]:1 operating on Observable(false)
645+
julia> v[] = :always
646+
:always
644647
```
645648
"""
646-
function tooltip!(b::Block, str::AbstractString; placement=:above, kwargs...)
649+
function tooltip!(b::Block, str::AbstractString; placement=:above, visible=:always, depth=9e3, kwargs...)
647650
position = lift(b.layoutobservables.computedbbox) do bbox
648651
if placement == :above
649652
bbox.origin + Point2f((bbox.widths[1]/2, bbox.widths[2]))
@@ -656,11 +659,21 @@ function tooltip!(b::Block, str::AbstractString; placement=:above, kwargs...)
656659
elseif placement == :center
657660
bbox.origin + Point2f((bbox.widths[1]/2, bbox.widths[2]/2))
658661
else
659-
@error("invalid value for tooltip_placement, using :above")
662+
@error("invalid value for tooltip placement, using :above")
660663
bbox.origin + Point2f((bbox.widths[1]/2, bbox.widths[2]))
661664
end
662665
end
663666
tt = tooltip!(b.blockscene, position, str; placement, kwargs...)
664-
translate!(tt, 0, 0, 9e3)
667+
translate!(tt, 0, 0, depth)
668+
onany(b.blockscene.events.mouseposition, visible) do mp, v
669+
tt.visible[] = if v == :never
670+
false
671+
elseif v == :hover
672+
mp in b.layoutobservables.computedbbox[]
673+
else
674+
v == :always || @error("invalid value for tooltip visible, using :always")
675+
true
676+
end
677+
end
665678
return tt
666679
end

0 commit comments

Comments
 (0)