Skip to content

Commit

Permalink
Warn if WGLMakie detected (#212)
Browse files Browse the repository at this point in the history
WGLMakie incompatibilities are now being tracked in #211. Notify users of this potential problem and guide them towards GLMakie for now.
  • Loading branch information
kbarros authored Jan 4, 2024
1 parent 8cf79e2 commit 96bfca6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
10 changes: 8 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ function build_examples(example_sources, destdir)
# will be stored in the `assets/` directory of the hosted docs.
for source in example_sources
function preprocess(str)
# Notebooks use WGLMakie instead of GLMakie
str = replace(str, r"^using(.*?)GLMakie"m => s"using\1WGLMakie")
# Ideally, notebooks would use WGLMakie instead of GLMakie, but
# there are currently too many bugs to enable by default:
# https://github.com/SunnySuite/Sunny.jl/issues/211
if false
str = replace(str, r"^using(.*?)GLMakie"m => s"using\1WGLMakie")
else
str
end
end
# Build notebooks
Literate.notebook(source, notebooks_path; preprocess, execute=false, credit=false)
Expand Down
3 changes: 3 additions & 0 deletions docs/src/versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## v0.5.8
(Upcoming)

* Many bugs in the WGLMakie backend have become apparent, and are being tracked
at https://github.com/SunnySuite/Sunny.jl/issues/211. Emit a warning if
WGLMakie is detected, suggesting that `using GLMakie` is preferred.
* Various improvements to [`view_crystal`](@ref). A distance parameter is no
longer expected. Cartesian axes now appear as "compass" in bottom-left. Custom
list of reference bonds can be passed. Toggle to view non-magnetic atoms in
Expand Down
58 changes: 38 additions & 20 deletions ext/PlottingExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,33 @@ import Sunny: Vec3, orig_crystal, natoms
using LinearAlgebra
import Makie

const monofont = pkgdir(Sunny, "assets", "fonts", "RobotoMono-Regular.ttf")

let warned = false
global warn_wglmakie() = begin
if !warned && string(Makie.current_backend()) == "WGLMakie"
@info """
Support for the WGLMakie backend is considered experimental. Consider
`using GLMakie` from a fresh Julia session for a more reliable experience.
WGLMakie compatibility is being tracked at:
https://github.com/SunnySuite/Sunny.jl/issues/211
"""
end
warned = true
end
end

function monofont()
if string(Makie.current_backend()) == "WGLMakie"
# WGLMakie does not have yet support font loading, see
# https://github.com/MakieOrg/Makie.jl/issues/3516.
(; )
else
(; font = pkgdir(Sunny, "assets", "fonts", "RobotoMono-Regular.ttf"))
end
end


getindex_cyclic(a, i) = a[mod1(i, length(a))]

Expand Down Expand Up @@ -424,6 +450,8 @@ Like [`plot_spins`](@ref) but will draw into the given Makie Axis, `ax`.
function plot_spins!(ax, sys::System; notifier=Makie.Observable(nothing), arrowscale=1.0, stemcolor=:lightgray, color=:red,
colorfn=nothing, colormap=:viridis, colorrange=nothing, show_cell=true, orthographic=false,
ghost_radius=0, dims=3)
warn_wglmakie()

if dims == 2
sys.latsize[3] == 1 || error("System not two-dimensional in (a₁, a₂)")
elseif dims == 1
Expand Down Expand Up @@ -568,6 +596,8 @@ Launch an interactive crystal viewer.
- `compass`: If true, draw Cartesian axes in bottom left.
"""
function Sunny.view_crystal(cryst::Crystal; refbonds=10, orthographic=false, ghost_radius=nothing, dims=3, compass=true, size=(768, 512))
warn_wglmakie()

fig = Makie.Figure(; size)

# Main scene
Expand Down Expand Up @@ -717,7 +747,7 @@ function Sunny.view_crystal(cryst::Crystal; refbonds=10, orthographic=false, gho

# Add inspector for pop-up information. Putting this last helps with
# visibility (Makie v0.19)
Makie.DataInspector(ax; indicator_color=:gray, fontsize, font=monofont)
Makie.DataInspector(ax; indicator_color=:gray, fontsize, monofont()...)

ℓ0 = characteristic_length_between_atoms(cryst)
orient_camera!(ax, cryst.latvecs; ghost_radius, ℓ0, orthographic, dims)
Expand Down Expand Up @@ -829,25 +859,13 @@ function scatter_bin_centers!(ax,params;axes)
end


# The purpose of __init__() below is to make all the internal functions of
# PlottingExt accessible to developers of Sunny.
#
# The standard and recommended use of Julia package extensions is to add methods
# to existing functions.
# https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions).
# For public exports, we create a function stub in Sunny.jl using the syntax
# `function f end`. Then the implementation is provided in this extension module
# as `function Sunny.f() ... end`.
#
# For non-public functions, however, it is undesirable fill Sunny.jl with stubs
# that will be irrelevant to most users. Access to such internal functions will
# instead be provided through the global variable `Sunny.Plotting`, which is set
# below. Note that `@__MODULE__` references the current extension module, here
# `PlottingExt`.
#
# Without the global variable `Sunny.Plotting`, one would need to use something
# like `Base.get_extension(Sunny, :PlottingExt)` to find the extension module.
function __init__()
# Make the `PlottingExt` module accessible via `Sunny.Plotting` rather than
# the more verbose syntax `Base.get_extension(Sunny, :PlottingExt)`.
#
# For public exports, we create a stub in Sunny.jl, `function f end`, with
# implementation in this extension module: `function Sunny.f() ... end`.
# https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions).
Sunny.Plotting = @__MODULE__
end

Expand Down

0 comments on commit 96bfca6

Please sign in to comment.