gui: a provider icon always draws — the page's files are served with their own types, not the ones Windows' registry gives, which another program may have changed (an SVG not served as one draws no mask icon), and a mask icon that fails to load becomes the generic one, as a picture of the user's own does. Issue #97

This commit is contained in:
yetone
2026-09-27 03:44:55 +08:00
parent cf60231465
commit 54e8bc6e1a
3 changed files with 34 additions and 0 deletions
+13
View File
@@ -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).
+7
View File
@@ -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;
}
+14
View File
@@ -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)
}
}
}