Skip to content

Commit

Permalink
Improved support for themes
Browse files Browse the repository at this point in the history
  • Loading branch information
essenciary committed Nov 6, 2024
1 parent 0639cce commit d7b2c2f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
File renamed without changes.
File renamed without changes.
11 changes: 7 additions & 4 deletions src/Layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,12 @@ function set_theme!(theme::Symbol)
end
Stipple.Theme.set_theme(theme)

Stipple.Theme.THEME_INDEX[]
nothing
end
const set_theme = set_theme!


const DEFAULT_USER_THEME_FILE = joinpath("css", "theme.css")
const DEFAULT_USER_THEME_FILE = joinpath("css", Stipple.THEMES_FOLDER, "theme.css")


"""
Expand Down Expand Up @@ -360,10 +361,12 @@ function theme(; core_theme::Bool = true) :: Vector{String}
unique!(THEMES[])

user_theme_file = joinpath(Genie.config.server_document_root, DEFAULT_USER_THEME_FILE)
# @show isfile(user_theme_file), user_theme_file
if isfile(user_theme_file)
register_theme(:usertheme, "/" * join(splitpath(DEFAULT_USER_THEME_FILE), "/")) # make this a relative URL
set_theme!(:usertheme)
else
# @show Stipple.Theme.get_theme()
set_theme!(Stipple.Theme.get_theme()) # set the default theme
end

Expand All @@ -382,9 +385,9 @@ end
"""
Register a theme with a given name and path to the CSS file.
"""
function register_theme(name::Symbol, theme::String)
function register_theme(name::Symbol, theme::String; apply_theme = false)
Stipple.Theme.register_theme(name, theme)
set_theme!(name)
apply_theme && set_theme!(name)
end
function register_theme(theme::String)
register_theme(Symbol(theme), theme)
Expand Down
10 changes: 6 additions & 4 deletions src/Stipple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,8 @@ const DEPS = OrderedCollections.LittleDict{Union{Any,AbstractString}, Function}(
Registers the `routes` for all the required JavaScript dependencies (scripts).
"""

const THEMES_FOLDER = "themes"

@nospecialize

function deps_routes(channel::String = Stipple.channel_js_name; core_theme::Bool = true) :: Nothing
Expand All @@ -815,15 +817,15 @@ function deps_routes(channel::String = Stipple.channel_js_name; core_theme::Bool
:css) |> Genie.Renderer.respond
end

Genie.Router.route(Genie.Assets.asset_route(Stipple.assets_config, :css, file="theme-default-light")) do
Genie.Router.route(Genie.Assets.asset_route(Stipple.assets_config, :css, path=THEMES_FOLDER, file="theme-default-light")) do
Genie.Renderer.WebRenderable(
Genie.Assets.embedded(Genie.Assets.asset_file(cwd=dirname(@__DIR__), type="css", file="theme-default-light")),
Genie.Assets.embedded(Genie.Assets.asset_file(cwd=dirname(@__DIR__), type="css", path=THEMES_FOLDER, file="theme-default-light")),
:css) |> Genie.Renderer.respond
end

Genie.Router.route(Genie.Assets.asset_route(Stipple.assets_config, :css, file="theme-default-dark")) do
Genie.Router.route(Genie.Assets.asset_route(Stipple.assets_config, :css, path=THEMES_FOLDER, file="theme-default-dark")) do
Genie.Renderer.WebRenderable(
Genie.Assets.embedded(Genie.Assets.asset_file(cwd=dirname(@__DIR__), type="css", file="theme-default-dark")),
Genie.Assets.embedded(Genie.Assets.asset_file(cwd=dirname(@__DIR__), type="css", path=THEMES_FOLDER, file="theme-default-dark")),
:css) |> Genie.Renderer.respond
end

Expand Down
38 changes: 32 additions & 6 deletions src/Theme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,46 @@ function register_theme(name::Symbol, theme::String)
end


function theme_exists(theme::Symbol)
return haskey(THEMES, theme)
end


function theme_exists!(theme::Symbol)
if ! theme_exists(theme)
error("Theme not found: $theme")
end

return true
end


"""
Get the URL or path to the theme's stylesheet.
"""
function to_path(theme::Symbol = CURRENT_THEME[]) :: String
theme_path = theme_exists!(theme) && THEMES[theme]

return if startswith(theme_path, "http://") || startswith(theme_path, "https://") # external URL
theme_path
elseif startswith(theme_path, "/") # relative URL
theme_path
else # asset path
Genie.Assets.asset_path(Stipple.assets_config, :css, path=Stipple.THEMES_FOLDER, file=theme_path)
end
end


"""
Get the current theme as a stylesheet to be loaded into Stipple.Layout.THEMES[]
"""
function to_asset(theme::Symbol = CURRENT_THEME[]) :: Function
theme_path = if haskey(THEMES, theme)
THEMES[theme]
else
error("Theme not found: $theme")
end
theme_path = theme_exists!(theme) && THEMES[theme]

return if startswith(theme_path, "http://") || startswith(theme_path, "https://") || startswith(theme_path, "/")
() -> stylesheet(theme_path)
else
() -> stylesheet(Genie.Assets.asset_path(Stipple.assets_config, :css, file=theme_path))
() -> stylesheet(Genie.Assets.asset_path(Stipple.assets_config, :css, path=Stipple.THEMES_FOLDER, file=theme_path))
end
end

Expand Down

0 comments on commit d7b2c2f

Please sign in to comment.