Theme: 1.5 line height, reject invalid settings

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 12:03:00 -04:00
1 parent 119f9047e9
commit 6c5d482aa5
2 files changed
+54

No files matched your search

+11
View File
@@ -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}
}
+43
View File
@@ -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)
}