From 54e8bc6e1a27e837e7652b5ce89e8059228db59f Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 27 Sep 2026 03:44:55 +0800 Subject: [PATCH] =?UTF-8?q?gui:=20a=20provider=20icon=20always=20draws=20?= =?UTF-8?q?=E2=80=94=20the=20page's=20files=20are=20served=20with=20their?= =?UTF-8?q?=20own=20types,=20not=20the=20ones=20Windows'=20registry=20give?= =?UTF-8?q?s,=20which=20another=20program=20may=20have=20changed=20(an=20S?= =?UTF-8?q?VG=20not=20served=20as=20one=20draws=20no=20mask=20icon),=20and?= =?UTF-8?q?=20a=20mask=20icon=20that=20fails=20to=20load=20becomes=20the?= =?UTF-8?q?=20generic=20one,=20as=20a=20picture=20of=20the=20user's=20own?= =?UTF-8?q?=20does.=20Issue=20#97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/api.go | 13 +++++++++++++ internal/gui/assets/app.js | 7 +++++++ internal/gui/assets_test.go | 14 ++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 internal/gui/assets_test.go diff --git a/internal/gui/api.go b/internal/gui/api.go index aed444244..6331a6989 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -8,6 +8,7 @@ import ( "context" "embed" "encoding/json" + "mime" "net/http" "os" "strings" @@ -120,6 +121,18 @@ func settingsState() settingsJSON { // page changes that; set by the process that has the app. var onDock func(bool) +// assetTypes are the page's own files' types. The file server takes them +// from Windows' registry, which another program may have changed — an SVG +// served as something else draws no icon. +var assetTypes = map[string]string{".svg": "image/svg+xml", ".png": "image/png", ".css": "text/css; charset=utf-8", + ".js": "text/javascript; charset=utf-8", ".html": "text/html; charset=utf-8"} + +func init() { + for ext, t := range assetTypes { + mime.AddExtensionType(ext, t) + } +} + // Handler serves the embedded UI and the JSON API. // gw is the gateway this process serves, or nil when another magpie has it // (for now: see startBackend). diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index c175a0cad..b29b0ccc1 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -112,6 +112,13 @@ function icon(name) { const m = el("span", "mask"); m.style.setProperty("--i", `url(icons/${name}.svg)`); e.append(m); + // a mask that fails to load draws nothing, and says nothing + const probe = new Image(); + probe.onerror = () => { + e.classList.add("generic"); + e.replaceChildren(...icon("generic").childNodes); + }; + probe.src = `icons/${name}.svg`; } return e; } diff --git a/internal/gui/assets_test.go b/internal/gui/assets_test.go new file mode 100644 index 000000000..59284816f --- /dev/null +++ b/internal/gui/assets_test.go @@ -0,0 +1,14 @@ +package gui + +import ( + "mime" + "testing" +) + +func TestAssetTypesHoldAgainstTheRegistry(t *testing.T) { + for ext, want := range assetTypes { + if got := mime.TypeByExtension(ext); got != want { + t.Errorf("%s served as %q, not %q", ext, got, want) + } + } +}