diff --git a/theme.go b/theme.go index b5e42a3..a7f15fa 100644 --- a/theme.go +++ b/theme.go @@ -2,6 +2,7 @@ package mujica import ( + "fmt" "time" "github.com/egoist/mygo/ui" @@ -51,6 +52,15 @@ type themeKey struct{} // Use applies s to the window. Call it first in every frame of the view. func Use(c *ui.Context, s Settings) { + if s.Mode < System || s.Mode > Dark { + panic(fmt.Sprintf("mujica: invalid Mode %d", s.Mode)) + } + if s.Density < theme.Default || s.Density > theme.Comfortable { + panic(fmt.Sprintf("mujica: invalid Density %d", s.Density)) + } + if s.Locale != "" && s.Locale != EnUS && s.Locale != ZhCN { + panic("mujica: unsupported Locale " + string(s.Locale)) + } dark := c.Theme().Dark switch s.Mode { case Light: @@ -99,6 +109,7 @@ func Use(c *ui.Context, s Settings) { Spacing: s.Density.Unit(), FontSize: size, }) + c.Root().LineHeight(theme.LineHeight) *ui.Local(c.Root(), themeKey{}, func() windowTheme { return windowTheme{} }) = windowTheme{k, s.Density, s.Locale, s.Messages, true} } diff --git a/theme_test.go b/theme_test.go new file mode 100644 index 0000000..59c0bb0 --- /dev/null +++ b/theme_test.go @@ -0,0 +1,43 @@ +package mujica + +import ( + "testing" + + "github.com/egoist/mygo/ui" + + "mujicaui/theme" +) + +func TestUseLineHeight(t *testing.T) { + var h float32 + tt := ui.NewTester(func(c *ui.Context) { + Use(c, Settings{}) + h = ui.Text(c, "one\ntwo").Bounds().H + }, 300, 200) + tt.Frame() + if want := 2 * theme.BodySize * theme.LineHeight; h < want-0.5 || h > want+0.5 { + t.Fatalf("two lines are %v DIPs, want %v", h, want) + } +} + +func TestUseRejectsInvalidSettings(t *testing.T) { + for _, s := range []Settings{{Mode: 9}, {Density: 9}, {Locale: "fr-FR"}} { + func() { + defer func() { + if recover() == nil { + t.Errorf("%+v accepted", s) + } + }() + ui.NewTester(func(c *ui.Context) { Use(c, s) }, 100, 100) + }() + } +} + +func TestTokensWithoutUsePanics(t *testing.T) { + defer func() { + if recover() == nil { + t.Fatal("Tokens before Use did not panic") + } + }() + ui.NewTester(func(c *ui.Context) { Tokens(c) }, 100, 100) +}