Skip to content

Commit ecd7bcd

Browse files
committed
search in :scenic by default for single atom themes
1 parent 1d06de7 commit ecd7bcd

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

lib/scenic/themes.ex

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,30 @@ defmodule Scenic.Themes do
151151
end
152152
end
153153

154+
def validate(theme_name) when is_atom(theme_name) do
155+
lib = module().library()
156+
{themes, schema} = Map.get(lib, :scenic)
157+
case Map.get(themes, theme_name) do
158+
nil ->
159+
{
160+
:error,
161+
"""
162+
#{IO.ANSI.red()}Invalid theme specification
163+
Received: #{inspect(theme_name)}
164+
#{IO.ANSI.yellow()}
165+
The theme could not be found in library #{inspect(:scenic)}.
166+
Ensure you got the name correct.
167+
#{IO.ANSI.default_color()}
168+
"""
169+
}
170+
theme ->
171+
case validate(theme, schema) do
172+
{:ok, _} -> {:ok, theme_name}
173+
error -> error
174+
end
175+
end
176+
end
177+
154178
def validate(
155179
%{
156180
text: _,
@@ -201,9 +225,12 @@ defmodule Scenic.Themes do
201225
#{IO.ANSI.red()}Invalid theme specification
202226
Received: #{inspect(data)}
203227
#{IO.ANSI.yellow()}
204-
Themes can be a tuple represent a theme for example:
228+
Themes can be a tuple representing a theme for example:
205229
{:scenic, :light}, {:scenic, :dark}
206230
231+
Or an atom representing one of scenics default themes:
232+
:primary, :secondary
233+
207234
Or it may also be a map defining colors for the values of
208235
:text, :background, :border, :active, :thumb, :focus
209236
@@ -249,7 +276,7 @@ defmodule Scenic.Themes do
249276
module()._get_palette()
250277
end
251278

252-
@spec normalize({atom, atom} | map) :: map | nil
279+
@spec normalize({atom, atom} | map | atom) :: map | nil
253280
@doc """
254281
Converts a theme from it's tuple form to it's map form.
255282
"""
@@ -261,9 +288,17 @@ defmodule Scenic.Themes do
261288
end
262289
end
263290

291+
def normalize(theme_name) when is_atom(theme_name) do
292+
themes = module().library()
293+
case Map.get(themes, :scenic) do
294+
{themes, _schema} -> Map.get(themes, theme_name)
295+
nil -> nil
296+
end
297+
end
298+
264299
def normalize(theme) when is_map(theme), do: theme
265300

266-
@spec preset({atom, atom} | map) :: map | nil
301+
@spec preset({atom, atom} | map | atom) :: map | nil
267302
@doc """
268303
Get a theme.
269304
"""
@@ -275,6 +310,14 @@ defmodule Scenic.Themes do
275310
end
276311
end
277312

313+
def preset(theme_name) when is_atom(theme_name) do
314+
themes = module().library()
315+
case Map.get(themes, :scenic) do
316+
{themes, _schema} -> Map.get(themes, theme_name)
317+
nil -> nil
318+
end
319+
end
320+
278321
def preset(theme) when is_map(theme), do: theme
279322

280323
@theme_light %{

test/scenic/themes_test.exs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ defmodule Scenic.ThemesTest do
7070
assert Themes.normalize({:scenic, :dark}) == @theme_dark
7171
end
7272

73+
test "normalize returns default scenic theme when an atom is passed" do
74+
assert Themes.normalize(:dark) == @theme_dark
75+
end
76+
7377
test "custom validate method accepts custom named themes" do
7478
assert Themes.validate({:custom_scenic, :custom_dark}) == {:ok, {:custom_scenic, :custom_dark}}
7579
assert Themes.validate({:custom_scenic, :custom_light}) == {:ok, {:custom_scenic, :custom_light}}
@@ -102,7 +106,11 @@ defmodule Scenic.ThemesTest do
102106

103107
test "validate rejects invalid theme names" do
104108
{:error, msg} = Themes.validate(:invalid)
105-
assert msg =~ "Themes can be a tuple represent a theme for example:"
109+
assert msg =~ "The theme could not be found in library"
110+
end
111+
112+
test "validate defaults to the scenic library when an atom is passed" do
113+
assert Themes.validate(:primary) == {:ok, :primary}
106114
end
107115

108116
test "validate accepts maps of colors" do

0 commit comments

Comments
 (0)