Files
magpie/internal/gui/fonts.go
T
yetone 7b63feb908 gui: a browser tab's font rows show on a nogui magpie, and Text size no longer flashes
Norcleeh (Discord #general, after v0.1.1788): in magpie's web GUI the
interface-font and code-font rows "flash once and disappear" on refresh.

Cause, reproduced on a real nogui `magpie web` on mote in Chromium and
WebKit, at 127.0.0.1, localhost and the LAN address: a nogui build
(Docker, magpie-cli) has fonts.Available false, so boot.js carried no
`fonts` flag and app.js never showed the font rows at all. What flashed
was the Text size row: index.html shows it, and renderSettings hid it
for a browser tab 40-120ms later (`#textSizeRow.hidden = web`).

Fix:
- A browser tab always gets the font rows. Its /api/fonts answers the
  server's families plus a common list (Arial, Menlo, Consolas, PingFang
  SC, Microsoft YaHei, Noto Sans CJK SC, JetBrains Mono, ...) and never
  errors for a tab; fonts.js keeps only what this browser can draw, and
  "System default" is always offered, so with nothing kept the rows still
  show with it.
- The app-only rows (Text size, tray, login, tray usage) carry
  .app-only, which `body.web .app-only` hides before first paint, so none
  of them paints in a tab.
- The boot `fonts` flag is gone. app.js and core both got smaller; no
  budget changed.
- docs/subsystems/gui-shell.md "Desktop fonts" says so.

Run (mote-run, at this head rebased on 98e23350f):
- go build ./... and -tags nogui; gofmt -l clean; GOOS=linux go vet
  -tags nogui ./internal/gui and GOOS=windows go vet ./internal/gui
  ./internal/fonts ok (at the pre-rebase head).
- go test -tags nogui -run 'Font|Budget|Boot' ./internal/gui
  ./internal/fonts ok (and with cgo before the rebase); the new
  TestBrowserTabFontsWithoutInstalledFonts covers nogui, a failed read
  and a merge with the server's own families.
- fonts, i18n-keys, settings-general, text-size, web-page: 76/76.
- Before the rebase, 20 files (fonts, font-cascade, win-fonts,
  login-allow, omarchy, settings-general/-search/-sections/-groups/
  -lines-wrap/-tabs-stick, text-size, tray-usages, web-page,
  web-app-standalone, gui-de, gui-ja, gui-zh-tw, i18n-keys) 259 tests,
  all ok but i18n-keys' "Runs as" (#3897, green now on main).
- Whole GUI suite before the rebase, --test-concurrency=4 beside other
  sessions' load: 13,626 tests, 160 red in 31 files. Those 31 files
  run alone at head and at base 0a7633136: 166 vs 157 red, the same
  tests at other widths/languages (routing-*, segs-press-redraw's
  webkit 900/440px, usage-*); mobile-web's "matches origin/main" needs
  .git, which a mote-run copy lacks; old-webkit's :has() is on base.
  segs-press-redraw alone: 7 red at head, 8 at base.
- New GUI test, both engines, en and zh: "a browser tab's font rows show
  on a nogui magpie and nothing flashes" records every rAF frame. Red on
  the old assets (uiFontRow never shows). Broken by hand: without the
  .app-only CSS rule it goes red with textSizeRow painting ~20 frames
  then going away (the reported flash); with
  `row.hidden = web && fontList?.length === 0` it goes red.
- Real servers on mote, both engines, 3 origins: nogui shows the rows
  (19 families in Chromium, 21 in WebKit), Text size never shown; a cgo
  desktop server 204/181 families.

Not tried: a Windows server with the fix (old code showed the rows),
WebKit over a LAN tunnel, Brave/Safari fingerprinting modes, the full
Go suite.
2026-10-28 12:25:39 +08:00

98 lines
3.6 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"slices"
"strings"
"sync"
"github.com/yetone/magpie/internal/fonts"
"github.com/yetone/magpie/internal/settings"
)
// onFonts updates existing webviews even while the other one has focus.
var onFonts func()
func sameFont(a, b *fonts.Face) bool {
return a == nil && b == nil || a != nil && b != nil && *a == *b
}
func fontSettingsJS(s settings.Settings) string {
b, _ := json.Marshal(map[string]any{"uiFont": s.UIFont, "codeFont": s.CodeFont})
return "window.receiveFonts?.(" + string(b) + ");"
}
// The fonts installed on the computer magpie runs on, for its windows and
// for a browser tab (which keeps the families it draws itself, fonts.js).
// A browser tab is also offered common families: it may be on another
// computer, and a nogui build (Docker, magpie-cli) reads no fonts at all,
// where its rows were left out and only Text size flashed by (Norcleeh).
// A failed refresh leaves the last successful collection intact.
func fontRoutes(mux *http.ServeMux, web, off bool, discover func() ([]fonts.Family, error)) {
var mu sync.Mutex
var cached []fonts.Family
mux.HandleFunc("GET /api/fonts", func(rw http.ResponseWriter, r *http.Request) {
if off && !web {
http.Error(rw, "installed fonts can't be read by this magpie", http.StatusNotFound)
return
}
mu.Lock()
defer mu.Unlock()
if !off && (cached == nil || r.URL.Query().Get("refresh") == "1") {
found, err := discover()
switch {
case err == nil && found == nil:
cached = []fonts.Family{}
case err == nil:
cached = found
case !web: // a tab still has the common families
http.Error(rw, "could not read installed fonts", http.StatusServiceUnavailable)
return
}
}
if web {
writeJSON(rw, withCommon(cached))
return
}
writeJSON(rw, cached)
})
}
// Families most computers have, by system and script; the tab keeps those
// it draws. Each is offered in its regular and bold weights.
var commonFamilies = []string{
"Arial", "Avenir Next", "Calibri", "Cambria", "Cascadia Code", "Cascadia Mono", "Consolas", "Courier New",
"DejaVu Sans", "DejaVu Sans Mono", "Fira Code", "Fira Sans", "Georgia", "Helvetica", "Helvetica Neue",
"Hiragino Sans", "Hiragino Sans GB", "IBM Plex Mono", "IBM Plex Sans", "Inter", "JetBrains Mono",
"Liberation Mono", "Liberation Sans", "Lucida Grande", "LXGW WenKai", "Malgun Gothic", "Maple Mono", "Menlo",
"Microsoft JhengHei", "Microsoft YaHei", "Monaco", "Noto Sans", "Noto Sans CJK SC", "Noto Sans Mono",
"Noto Serif", "Noto Serif CJK SC", "Palatino", "PingFang SC", "PingFang TC", "Roboto", "Roboto Mono",
"Sarasa Gothic SC", "Segoe UI", "SF Mono", "SimHei", "SimSun", "Songti SC", "Source Code Pro",
"Source Han Sans SC", "Tahoma", "Times New Roman", "Trebuchet MS", "Ubuntu", "Ubuntu Mono", "Verdana",
"WenQuanYi Micro Hei", "Yu Gothic",
}
// withCommon adds the common families the list lacks, by name in any case,
// and keeps it in the order fonts.List gives (case-insensitive names).
func withCommon(found []fonts.Family) []fonts.Family {
out := slices.Clone(found)
have := map[string]bool{}
for _, f := range found {
have[strings.ToLower(f.Name)] = true
}
for _, name := range commonFamilies {
if have[strings.ToLower(name)] {
continue
}
face := fonts.Face{Family: name, Name: "Regular", Weight: 400, Style: "normal", Stretch: 100}
bold := face
bold.Name, bold.Weight = "Bold", 700
out = append(out, fonts.Family{Name: name, Styles: []fonts.Face{face, bold}})
}
slices.SortStableFunc(out, func(a, b fonts.Family) int {
return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
})
return out
}