FontSize helper follows desktop text scale
This commit is contained in:
3 files changed
+22
-4
No files matched your search
@@ -32,6 +32,7 @@ testdata/snapshots/ <os>/<name>-<light|dark>.png
|
||||
|
||||
- Signature: `func Name(c *ui.Context, value *T, opts NameOptions) *ui.Element`. Composites may return a named struct.
|
||||
- Zero `Options` gives the default look. No `map[string]any` config.
|
||||
- Explicit font sizes: `FontSize(c, theme.CaptionSize)`; it applies the desktop text scale.
|
||||
- Colors: `k := mujica.Tokens(c)`. Metrics: `theme.*` consts and `Density(c).Unit()` / `.ControlHeight()`.
|
||||
- Build on MyGo Bases (`ui.ButtonBase`, `ui.CheckboxBase`, `ui.SelectBase`, ...) or MyGo widgets restyled through the theme `Use` sets. Never reach into MyGo internals.
|
||||
- Focus: `internal.Ring(e, k.Focus, radius)` for the 2 DIP ring with 2 DIP gap.
|
||||
|
||||
@@ -82,10 +82,7 @@ func Use(c *ui.Context, s Settings) {
|
||||
if p.HighContrast {
|
||||
k = k.HighContrast()
|
||||
}
|
||||
size := theme.BodySize
|
||||
if p.TextScale > 0 {
|
||||
size *= p.TextScale
|
||||
}
|
||||
size := FontSize(c, theme.BodySize)
|
||||
c.SetTheme(&ui.Theme{
|
||||
Dark: k.Dark,
|
||||
Background: k.Background,
|
||||
@@ -134,3 +131,11 @@ func Motion(c *ui.Context, d time.Duration) time.Duration {
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// FontSize scales a design size by the desktop's text scale.
|
||||
func FontSize(c *ui.Context, size float32) float32 {
|
||||
if s := c.Preferences().TextScale; s > 0 {
|
||||
return size * s
|
||||
}
|
||||
return size
|
||||
}
|
||||
@@ -41,3 +41,15 @@ func TestTokensWithoutUsePanics(t *testing.T) {
|
||||
}()
|
||||
ui.NewTester(func(c *ui.Context) { Tokens(c) }, 100, 100)
|
||||
}
|
||||
|
||||
func TestFontSizeFollowsTextScale(t *testing.T) {
|
||||
var got float32
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
Use(c, Settings{})
|
||||
got = FontSize(c, theme.CaptionSize)
|
||||
}, 100, 100)
|
||||
tt.SetPreferences(ui.Preferences{TextScale: 2})
|
||||
if got != 2*theme.CaptionSize {
|
||||
t.Fatalf("FontSize = %v, want %v", got, 2*theme.CaptionSize)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user