Skip to content

Commit 9f6961f

Browse files
feat(preview): add icon support and theme helpers for Makie UI
1 parent b8c58da commit 9f6961f

1 file changed

Lines changed: 42 additions & 13 deletions

File tree

src/datamodel/preview.jl

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ _is_gl_backend() = current_backend_symbol() == :gl
1111

1212

1313
########################
14-
# Small utility glue #
15-
########################
16-
const _TAU = 2π
14+
# Fancy icons
15+
const MI_REFRESH = "\uE5D5" # Material Icons: 'refresh'
16+
const MI_SAVE = "\uE161" # Material Icons: 'save'
17+
const ICON_TTF = joinpath(@__DIR__, "..", "..", "assets", "fonts", "material-icons", "MaterialIcons-Regular.ttf")
1718

1819
# finite & nonnegative
1920
_valid_finite(x, y) = isfinite(x) && isfinite(y)
@@ -272,7 +273,7 @@ end
272273
# polygons (Float32 points; filter non-finite)
273274
function _annulus_poly(rin::Real, rex::Real, x0::Real, y0::Real; N::Int = 256)
274275
N 32 || throw(ArgumentError("N too small for a smooth annulus"))
275-
θo = range(0, _TAU; length = N);
276+
θo = range(0, 2π; length = N);
276277
θi = reverse(θo)
277278
xo = x0 .+ rex .* cos.(θo);
278279
yo = y0 .+ rex .* sin.(θo)
@@ -285,7 +286,7 @@ function _annulus_poly(rin::Real, rex::Real, x0::Real, y0::Real; N::Int = 256)
285286
end
286287

287288
function _circle_poly(r::Real, x0::Real, y0::Real; N::Int = 128)
288-
θ = range(0, _TAU; length = N)
289+
θ = range(0, 2π; length = N)
289290
x = x0 .+ r .* cos.(θ);
290291
y = y0 .+ r .* sin.(θ)
291292
pts = Makie.Point2f.(vcat(x, x[1]), vcat(y, y[1]))
@@ -378,6 +379,30 @@ function _plot_layer_makie!(ax, layer, label::String;
378379
return ()
379380
end
380381

382+
function apply_default_theme!()
383+
bg = _is_static_backend() ? :white : :gray90
384+
set_theme!(backgroundcolor = bg, fonts = (; icons = ICON_TTF))
385+
end
386+
387+
# --- 3) tiny helper to build "icon + text" labels ergonomically ---
388+
"""
389+
with_icon(icon; text="", isize=14, tsize=12, color=:black, gap=4,
390+
dy_icon=-0.18, dy_text=0.0)
391+
392+
- `dy_icon`, `dy_text`: vertical tweaks in *em* units (fraction of that part's fontsize).
393+
Negative moves down, positive moves up.
394+
"""
395+
with_icon(icon::AbstractString; text::AbstractString = "",
396+
isize::Int = 16, tsize::Int = 12, color = :black, gap::Int = 2,
397+
dy_icon::Float64 = -0.18, dy_text::Float64 = 0.0) =
398+
text == "" ?
399+
rich(icon; font = :icons, fontsize = isize, color = color, offset = (0, dy_icon)) :
400+
rich(
401+
rich(icon; font = :icons, fontsize = isize, color = color, offset = (0, dy_icon)),
402+
rich(" "^gap; font = :regular, fontsize = tsize, color = color),
403+
rich(text; font = :regular, fontsize = tsize, color = color, offset = (0, dy_text)),
404+
)
405+
381406
###############################################
382407
# CableDesign cross-section (Makie version) #
383408
###############################################
@@ -397,9 +422,9 @@ function preview(design::CableDesign;
397422

398423
ensure_backend!(backend)
399424

400-
401-
backgroundcolor = (_is_static_backend() ? :white : :gray90)
402-
set_theme!(backgroundcolor = backgroundcolor)
425+
# backgroundcolor = (_is_static_backend() ? :white : :gray90)
426+
# set_theme!(backgroundcolor = backgroundcolor)
427+
apply_default_theme!()
403428

404429
fig =
405430
isnothing(axis) ? Makie.Figure(size = size, figure_padding = (10, 10, 10, 10)) :
@@ -592,9 +617,10 @@ function preview(system::LineCableSystem;
592617
)
593618

594619
ensure_backend!(backend)
595-
backgroundcolor = (_is_static_backend() ? :white : :gray90)
620+
# backgroundcolor = (_is_static_backend() ? :white : :gray90)
596621

597-
set_theme!(backgroundcolor = backgroundcolor)
622+
# set_theme!(backgroundcolor = backgroundcolor)
623+
apply_default_theme!()
598624

599625
fig =
600626
isnothing(axis) ? Makie.Figure(size = size, figure_padding = (10, 10, 10, 10)) :
@@ -819,8 +845,11 @@ function _add_save_svg_button!(parent_cell, system;
819845
save_dir::AbstractString = pwd(),
820846
)
821847
btn = Makie.Button(
822-
parent_cell, label = "Save SVG", halign = :center,
823-
valign = :top, width = Makie.Relative(1.0),
848+
parent_cell,
849+
label = with_icon(MI_SAVE; text = "Save SVG"),
850+
halign = :center,
851+
valign = :top,
852+
width = Makie.Relative(1.0),
824853
)
825854
Makie.on(btn.clicks) do _
826855
@async begin
@@ -883,7 +912,7 @@ end
883912
function _add_reset_button!(parent_cell, ax, fig)
884913
btn = Makie.Button(
885914
parent_cell,
886-
label = "Reset view",
915+
label = with_icon(MI_REFRESH; text = "Reset view"),
887916
halign = :center,
888917
valign = :top,
889918
width = Makie.Relative(1.0),

0 commit comments

Comments
 (0)