Skip to content

Commit

Permalink
search in :scenic by default for single atom themes
Browse files Browse the repository at this point in the history
  • Loading branch information
vacarsu committed Dec 14, 2021
1 parent 1d06de7 commit ecd7bcd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
49 changes: 46 additions & 3 deletions lib/scenic/themes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ defmodule Scenic.Themes do
end
end

def validate(theme_name) when is_atom(theme_name) do
lib = module().library()
{themes, schema} = Map.get(lib, :scenic)
case Map.get(themes, theme_name) do
nil ->
{
:error,
"""
#{IO.ANSI.red()}Invalid theme specification
Received: #{inspect(theme_name)}
#{IO.ANSI.yellow()}
The theme could not be found in library #{inspect(:scenic)}.
Ensure you got the name correct.
#{IO.ANSI.default_color()}
"""
}
theme ->
case validate(theme, schema) do
{:ok, _} -> {:ok, theme_name}
error -> error
end
end
end

def validate(
%{
text: _,
Expand Down Expand Up @@ -201,9 +225,12 @@ defmodule Scenic.Themes do
#{IO.ANSI.red()}Invalid theme specification
Received: #{inspect(data)}
#{IO.ANSI.yellow()}
Themes can be a tuple represent a theme for example:
Themes can be a tuple representing a theme for example:
{:scenic, :light}, {:scenic, :dark}
Or an atom representing one of scenics default themes:
:primary, :secondary
Or it may also be a map defining colors for the values of
:text, :background, :border, :active, :thumb, :focus
Expand Down Expand Up @@ -249,7 +276,7 @@ defmodule Scenic.Themes do
module()._get_palette()
end

@spec normalize({atom, atom} | map) :: map | nil
@spec normalize({atom, atom} | map | atom) :: map | nil
@doc """
Converts a theme from it's tuple form to it's map form.
"""
Expand All @@ -261,9 +288,17 @@ defmodule Scenic.Themes do
end
end

def normalize(theme_name) when is_atom(theme_name) do
themes = module().library()
case Map.get(themes, :scenic) do
{themes, _schema} -> Map.get(themes, theme_name)
nil -> nil
end
end

def normalize(theme) when is_map(theme), do: theme

@spec preset({atom, atom} | map) :: map | nil
@spec preset({atom, atom} | map | atom) :: map | nil
@doc """
Get a theme.
"""
Expand All @@ -275,6 +310,14 @@ defmodule Scenic.Themes do
end
end

def preset(theme_name) when is_atom(theme_name) do
themes = module().library()
case Map.get(themes, :scenic) do
{themes, _schema} -> Map.get(themes, theme_name)
nil -> nil
end
end

def preset(theme) when is_map(theme), do: theme

@theme_light %{
Expand Down
10 changes: 9 additions & 1 deletion test/scenic/themes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ defmodule Scenic.ThemesTest do
assert Themes.normalize({:scenic, :dark}) == @theme_dark
end

test "normalize returns default scenic theme when an atom is passed" do
assert Themes.normalize(:dark) == @theme_dark
end

test "custom validate method accepts custom named themes" do
assert Themes.validate({:custom_scenic, :custom_dark}) == {:ok, {:custom_scenic, :custom_dark}}
assert Themes.validate({:custom_scenic, :custom_light}) == {:ok, {:custom_scenic, :custom_light}}
Expand Down Expand Up @@ -102,7 +106,11 @@ defmodule Scenic.ThemesTest do

test "validate rejects invalid theme names" do
{:error, msg} = Themes.validate(:invalid)
assert msg =~ "Themes can be a tuple represent a theme for example:"
assert msg =~ "The theme could not be found in library"
end

test "validate defaults to the scenic library when an atom is passed" do
assert Themes.validate(:primary) == {:ok, :primary}
end

test "validate accepts maps of colors" do
Expand Down

0 comments on commit ecd7bcd

Please sign in to comment.