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

Fixes for 0.20 release #3401

Merged
merged 3 commits into from Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions MakieCore/src/recipes.jl
Expand Up @@ -35,8 +35,9 @@ function _create_plot end
function _create_plot! end


plot(args...; kw...) = _create_plot(plotfunc(plottype(map(to_value, args)...)), Dict{Symbol, Any}(kw), args...)
plot!(args...; kw...) = _create_plot!(plotfunc(plottype(map(to_value, args)...)), Dict{Symbol, Any}(kw), args...)

plot(args...; kw...) = _create_plot(plot, Dict{Symbol, Any}(kw), args...)
plot!(args...; kw...) = _create_plot!(plot, Dict{Symbol, Any}(kw), args...)

"""
Each argument can be named for a certain plot type `P`. Falls back to `arg1`, `arg2`, etc.
Expand Down Expand Up @@ -231,4 +232,4 @@ e.g.:
plottype(x::Array{<: AbstractFloat, 3}) = Volume
```
"""
plottype(plot_args...) = Plot{plot, Tuple{map(typeof, plot_args)...}} # default to dispatch to type recipes!
plottype(plot_args...) = Plot{plot} # default to dispatch to type recipes!
10 changes: 7 additions & 3 deletions src/figureplotting.jl
Expand Up @@ -237,11 +237,15 @@ function fig_keywords!(kws)
return figkws
end

# Narrows down the default plotfunc early on, if `plot` is used
default_plot_func(f::F, args) where {F} = f
default_plot_func(::typeof(plot), args) = plotfunc(plottype(map(to_value, args)...))

# Don't inline these, since they will get called from `scatter!(args...; kw...)` which gets specialized to all kw args
@noinline function MakieCore._create_plot(F, attributes::Dict, args...)
figarg, pargs = plot_args(args...)
figkws = fig_keywords!(attributes)
plot = Plot{F}(pargs, attributes)
plot = Plot{default_plot_func(F, pargs)}(pargs, attributes)
ax = create_axis_like(plot, figkws, figarg)
plot!(ax, plot)
return figurelike_return(ax, plot)
Expand All @@ -250,14 +254,14 @@ end
@noinline function MakieCore._create_plot!(F, attributes::Dict, args...)
figarg, pargs = plot_args(args...)
figkws = fig_keywords!(attributes)
plot = Plot{F}(pargs, attributes)
plot = Plot{default_plot_func(F, pargs)}(pargs, attributes)
ax = create_axis_like!(plot, figkws, figarg)
plot!(ax, plot)
return figurelike_return!(ax, plot)
end

@noinline function MakieCore._create_plot!(F, attributes::Dict, scene::SceneLike, args...)
plot = Plot{F}(args, attributes)
plot = Plot{default_plot_func(F, args)}(args, attributes)
plot!(scene, plot)
return plot
end
Expand Down
42 changes: 23 additions & 19 deletions src/interfaces.jl
Expand Up @@ -147,8 +147,8 @@ function Plot{Func}(args::Tuple, plot_attributes::Dict) where {Func}

ArgTyp = MakieCore.argtypes(converted)
converted_obs = map(Observable, converted)
plot = Plot{plotfunc(PNew),ArgTyp}(plot_attributes, obs_args, converted_obs)
return plot
FinalPlotFunc = plotfunc(plottype(PNew, converted...))
return Plot{FinalPlotFunc,ArgTyp}(plot_attributes, obs_args, converted_obs)
end

"""
Expand Down Expand Up @@ -178,11 +178,27 @@ used_attributes(args...) = ()


## generic definitions
# If the Plot has no plot func, calculate them
plottype(::Type{<: Plot{Any}}, argvalues...) = plottype(argvalues...)
plottype(::Type{Any}, argvalues...) = plottype(argvalues...)
# If it has something more concrete than Any, use it directly
plottype(P::Type{<: Plot{T}}, argvalues...) where T = P
# Chose the more specific plot type from arguments or input type
# Note the plottype(Scatter, Plot{plot}) will return Scatter
# And plottype(args...) falls back to Plot{plot}
plottype(P::Type{<: Plot{T}}, argvalues...) where T = plottype(P, plottype(argvalues...))
plottype(P::Type{<:Plot{T}}) where {T} = P
plottype(P1::Type{<:Plot{T1}}, ::Type{<:Plot{T2}}) where {T1, T2} = P1
plottype(::Type{Plot{plot}}, ::Type{Plot{plot}}) = Plot{plot}
"""
plottype(P1::Type{<: Plot{T1}}, P2::Type{<: Plot{T2}})

Chooses the more concrete plot type
```julia
function convert_arguments(P::PlotFunc, args...)
ptype = plottype(P, Lines)
...
end
```
"""
plottype(::Type{Plot{plot}}, P::Type{<:Plot{T}}) where {T} = P
plottype(P::Type{<:Plot{T}}, ::Type{Plot{plot}}) where {T} = P


## specialized definitions for types
plottype(::AbstractVector, ::AbstractVector, ::AbstractVector) = Scatter
Expand All @@ -201,19 +217,7 @@ plottype(::GeometryBasics.AbstractPolygon) = Poly
plottype(::AbstractVector{<:GeometryBasics.AbstractPolygon}) = Poly
plottype(::MultiPolygon) = Lines

"""
plottype(P1::Type{<: Plot{T1}}, P2::Type{<: Plot{T2}})

Chooses the more concrete plot type
```julia
function convert_arguments(P::PlotFunc, args...)
ptype = plottype(P, Lines)
...
end
```
"""
plottype(::Type{<: Plot{Any}}, P::Type{<: Plot{T}}) where T = P
plottype(P::Type{<: Plot{T}}, ::Type{<: Plot}) where T = P

# all the plotting functions that get a plot type
const PlotFunc = Union{Type{Any},Type{<:AbstractPlot}}
Expand Down
10 changes: 7 additions & 3 deletions src/makielayout/blocks.jl
Expand Up @@ -65,7 +65,7 @@ macro Block(_name::Union{Expr, Symbol}, body::Expr = Expr(:block))

function Makie.default_attribute_values(::Type{$(name)}, scene::Union{Scene, Nothing})
sceneattrs = scene === nothing ? Attributes() : theme(scene)
curdeftheme = fast_deepcopy($(Makie).CURRENT_DEFAULT_THEME)
curdeftheme = Makie.fast_deepcopy($(Makie).CURRENT_DEFAULT_THEME)
$(make_attr_dict_expr(attrs, :sceneattrs, :curdeftheme))
end

Expand Down Expand Up @@ -277,7 +277,11 @@ function _block(T::Type{<:Block}, fig_or_scene::Union{Figure, Scene}, args...; b
end

function block_defaults(blockname::Symbol, attribute_kwargs::Dict, scene::Union{Nothing, Scene})
default_attrs = default_attribute_values(getfield(Makie, blockname), scene)
return block_defaults(getfield(Makie, blockname), attribute_kwargs, scene)
end
function block_defaults(::Type{B}, attribute_kwargs::Dict, scene::Union{Nothing, Scene}) where {B <: Block}
default_attrs = default_attribute_values(B, scene)
blockname = nameof(B)
typekey_scene_attrs = get(theme(scene), blockname, Attributes())
typekey_attrs = theme(blockname; default=Attributes())::Attributes
attributes = Dict{Symbol,Any}()
Expand Down Expand Up @@ -321,7 +325,7 @@ function _block(T::Type{<:Block}, fig_or_scene::Union{Figure,Scene}, args, kwdic
if kwdict_complete
attributes = attribute_kwargs
else
attributes = block_defaults(nameof(T), attribute_kwargs, topscene)
attributes = block_defaults(T, attribute_kwargs, topscene)
end
# create basic layout observables and connect attribute observables further down
# after creating the block with its observable fields
Expand Down
4 changes: 2 additions & 2 deletions src/scenes.jl
Expand Up @@ -137,8 +137,8 @@ function Observables.on(@nospecialize(f), @nospecialize(scene::Union{Plot,Scene}
return to_deregister
end

function Observables.onany(@nospecialize(f), @nospecialize(scene::Union{Plot,Scene}), @nospecialize(observables...); priority=0)
to_deregister = onany(f, observables...; priority=priority)
function Observables.onany(@nospecialize(f), @nospecialize(scene::Union{Plot,Scene}), @nospecialize(observables...); update=false, priority=0)
to_deregister = onany(f, observables...; priority=priority, update=update)
append!(scene.deregister_callbacks::Vector{Observables.ObserverFunction}, to_deregister)
return to_deregister
end
Expand Down
2 changes: 1 addition & 1 deletion src/specapi.jl
Expand Up @@ -735,7 +735,7 @@ function plot!(fig::Union{Figure, GridLayoutBase.GridPosition}, plot::Plot{Makie
return fig
end

function apply_convert!(P, attributes::Attributes, x::GridLayoutSpec)
function apply_convert!(P, ::Attributes, x::GridLayoutSpec)
return (Plot{plot}, (x,))
end

Expand Down
28 changes: 28 additions & 0 deletions test/pipeline.jl
Expand Up @@ -117,3 +117,31 @@ end
pl2 = lines!(ax, 1:4; color=Cycled(1))
@test pl2.calculated_colors[] == cpalette[1]
end

function test_default(arg)
_, _, pl1 = plot(arg)

fig = Figure()
_, pl2 = plot(fig[1, 1], arg)

fig = Figure()
ax = Axis(fig[1, 1])
pl3 = plot!(ax, arg)
return [pl1, pl2, pl3]
end

@testset "plot defaults" begin
plots = test_default([10, 15, 20])
@test all(x-> x isa Scatter, plots)

plots = test_default(rand(4, 4))
@test all(x -> x isa Heatmap, plots)

poly = Polygon(decompose(Point, Circle(Point2f(0), 1.0f0)))

plots = test_default(poly)
@test all(x -> x isa Poly, plots)

plots = test_default(rand(4, 4, 4))
@test all(x -> x isa Volume, plots)
end