Skip to content

Commit ab53348

Browse files
authored
Store available themes in a dictionary (#139)
1 parent dc6731a commit ab53348

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/InteractBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export latex, alert, confirm, highlight, notifications, accordion, tabulator, ma
7878

7979
export onchange
8080

81-
export settheme!, resettheme!, gettheme, NativeHTML
81+
export settheme!, resettheme!, gettheme, availablethemes, NativeHTML
8282

8383
export slap_design!
8484

src/backends.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ libraries(::WidgetTheme) = [style_css]
22

33
Base.@deprecate_binding backend Widgets.backends
44

5+
const themes = OrderedDict{Symbol, WidgetTheme}(:nativehtml => NativeHTML())
6+
7+
registertheme!(key::Union{Symbol, AbstractString}, val::WidgetTheme) = setindex!(themes, val, Symbol(key))
8+
9+
"""
10+
`settheme!(s::Union{Symbol, AbstractString})`
11+
12+
Set theme of Interact globally. See `availablethemes()` to know what themes are currently available.
13+
"""
14+
function settheme!(s::Union{Symbol, AbstractString})
15+
theme = get(themes, Symbol(s)) do
16+
error("Theme $s is not available.")
17+
end
18+
settheme!(theme)
19+
end
20+
521
settheme!(b::WidgetTheme) = isa(Widgets.get_backend(), WidgetTheme) && Widgets.set_backend!(b)
622
gettheme() = isa(Widgets.get_backend(), WidgetTheme) ? Widgets.get_backend() : nothing
23+
availablethemes() = sort(collect(keys(themes)))
724
resettheme!() = isa(Widgets.get_backend(), WidgetTheme) && Widgets.reset_backend!()

test/test_theme.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
struct MyTheme<:InteractBase.WidgetTheme; end
2+
InteractBase.registertheme!(:mytheme, MyTheme())
23

34
@testset "theme" begin
45
@test gettheme() == NativeHTML()
56
settheme!(MyTheme())
67
@test gettheme() == MyTheme()
78
resettheme!()
89
@test gettheme() == NativeHTML()
10+
settheme!("mytheme")
11+
@test gettheme() == MyTheme()
12+
settheme!(:nativehtml)
13+
@test gettheme() == NativeHTML()
14+
@test availablethemes() == [:mytheme, :nativehtml]
15+
@test_throws ErrorException settheme!("not a theme")
916
end

0 commit comments

Comments
 (0)