Skip to content

Commit

Permalink
Store available themes in a dictionary (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
piever authored Apr 3, 2019
1 parent dc6731a commit ab53348
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/InteractBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export latex, alert, confirm, highlight, notifications, accordion, tabulator, ma

export onchange

export settheme!, resettheme!, gettheme, NativeHTML
export settheme!, resettheme!, gettheme, availablethemes, NativeHTML

export slap_design!

Expand Down
17 changes: 17 additions & 0 deletions src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ libraries(::WidgetTheme) = [style_css]

Base.@deprecate_binding backend Widgets.backends

const themes = OrderedDict{Symbol, WidgetTheme}(:nativehtml => NativeHTML())

registertheme!(key::Union{Symbol, AbstractString}, val::WidgetTheme) = setindex!(themes, val, Symbol(key))

"""
`settheme!(s::Union{Symbol, AbstractString})`
Set theme of Interact globally. See `availablethemes()` to know what themes are currently available.
"""
function settheme!(s::Union{Symbol, AbstractString})
theme = get(themes, Symbol(s)) do
error("Theme $s is not available.")
end
settheme!(theme)
end

settheme!(b::WidgetTheme) = isa(Widgets.get_backend(), WidgetTheme) && Widgets.set_backend!(b)
gettheme() = isa(Widgets.get_backend(), WidgetTheme) ? Widgets.get_backend() : nothing
availablethemes() = sort(collect(keys(themes)))
resettheme!() = isa(Widgets.get_backend(), WidgetTheme) && Widgets.reset_backend!()
7 changes: 7 additions & 0 deletions test/test_theme.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
struct MyTheme<:InteractBase.WidgetTheme; end
InteractBase.registertheme!(:mytheme, MyTheme())

@testset "theme" begin
@test gettheme() == NativeHTML()
settheme!(MyTheme())
@test gettheme() == MyTheme()
resettheme!()
@test gettheme() == NativeHTML()
settheme!("mytheme")
@test gettheme() == MyTheme()
settheme!(:nativehtml)
@test gettheme() == NativeHTML()
@test availablethemes() == [:mytheme, :nativehtml]
@test_throws ErrorException settheme!("not a theme")
end

0 comments on commit ab53348

Please sign in to comment.