From 81bec044dfadde2a90bc22a9689da36e55851fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Mon, 5 Oct 2026 12:25:48 -0400 Subject: [PATCH] FontSize helper follows desktop text scale --- AGENTS.md | 1 + theme.go | 13 +++++++++---- theme_test.go | 12 ++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 27aa41b..4b10753 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -32,6 +32,7 @@ testdata/snapshots/ /-.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. diff --git a/theme.go b/theme.go index a7f15fa..3a5b690 100644 --- a/theme.go +++ b/theme.go @@ -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 +} diff --git a/theme_test.go b/theme_test.go index 59c0bb0..cd9718c 100644 --- a/theme_test.go +++ b/theme_test.go @@ -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) + } +}