「选中任意强调色没反应」: picking an accent in Settings › General seemed
to do nothing. The token did apply. With the real server, in Chromium
and WebKit, under light, dark and system, with and without a background
image, --accent was set by lookCSS and kept. Neither the theme nor the
wallpaper overrode it. But nothing on Settings, the sidebar, the header
tabs or the panel's tabs used --accent: every chosen option is graphite,
var(--fg), by design. So the colour only showed in a few buttons
elsewhere.
The fix is a --mark token, the ink of what is chosen.
- It is var(--fg) by default, so magpie's own look is unchanged, like a
Mac's Graphite.
- accentVars sets it once the user picks an accent. It is moved toward
black (light) or white (dark) until it reads at 4.5:1 on the surfaces a
chosen option sits on: --pill-hover #e9e9ee, and the dark switch thumb
#4c4c52.
- It is used by .seg/.opt/.ptabs/.warm-tabs/.am-seg/.klf-tab/.day-chip/
.apptab .on, the header nav's chosen tab, the side-nav's open-page icon
and Settings icon, and Library's switches and radios. Each is the same
"chosen" role, so all of them follow the accent, not just one.
Tests:
- appearance_test.go's TestAccentReadable checks --mark's contrast on
each surface, in both themes.
- appearance-look.test.cjs adds 8 tests, in Chromium and WebKit, light
and dark:
- Settings' theme and bird options and the side-nav icon at 1200 with a
wallpaper set: graphite by default, the mark for #0d9488 and #db2777,
and back to graphite on Default.
- The header tabs at 900 and the panel's tabs at 380.
Run:
- gofmt clean. go vet ok on darwin, GOOS=linux -tags nogui and
GOOS=windows (at 75bdc85fe, the same change on an older main).
- At 6f5549b1f (this change on 08f77528f), on Linux (WSL magpie-desktop),
since mote's suite queue was hours deep:
- The whole GUI suite, Chromium and WebKit, every language and the
panel widths: 27231 tests, 1258 red in 183 files. The same 183 files
at the parent were 1226 red. The 70 tests red only with the change
were rerun with their 43 files at concurrency 2: 80 red with it, 89
at the parent 783b69b8c. Every one left has a language or width
sibling red at the parent too. None is about colour. They are the
Linux reds #5559 and #4388 track.
- go test -tags nogui ./... -timeout 45m: every package ok except
internal/usage's TestRequestStateKeptAcrossACatalogFetchedAgain,
red the same at the parent (#6572).
- At the pushed head, on mote (macOS):
- TestAccent*, TestWallpaper*, TestLook* ok.
- appearance-look 36/36, i18n-keys 4/4, library-all 26/26.
- segs-press-redraw 16 red and api-key-theme 6/6 red, the same counts
at the parent 6c88d61f0 (#4566, #3922, #6444).
- TestCoreScriptBudget is red (1269382 > 1268961), the same at the
parent. 92e760f3d took it over (#6518, taken). This change adds no
script.
- Broken by hand:
- With the old app.css and library.css, the 8 new GUI tests are red,
8 of 8.
- accentVars without --mark: TestAccentReadable fails ("…;--sel:#efefef",
no mark).
Not tried:
- The whole GUI suite and the Go suite on macOS at the pushed head. mote's
suite.lock queue was hours deep, so they ran on Linux instead.
- The real macOS app.
- Windows WebView2, and Linux WebKitGTK.
- Charts' --c1 series colour still does not follow the accent; that is
left as it was.
212 lines
8.8 KiB
Go
212 lines
8.8 KiB
Go
package gui
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
func appearanceHome(t *testing.T) http.Handler {
|
|
t.Helper()
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
|
|
t.Setenv("LOCALAPPDATA", filepath.Join(h, "AppData", "Local"))
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
|
|
return Handler(&zoomWindows{}, nil)
|
|
}
|
|
|
|
func lookServe(srv http.Handler, method, path, body string) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
|
|
return rec
|
|
}
|
|
|
|
func tinyPNG(t *testing.T) []byte {
|
|
t.Helper()
|
|
img := image.NewRGBA(image.Rect(0, 0, 2, 2))
|
|
img.Set(0, 0, color.RGBA{200, 30, 30, 255})
|
|
var b bytes.Buffer
|
|
if err := png.Encode(&b, img); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b.Bytes()
|
|
}
|
|
|
|
// An accent of the reader's own (CHENGJIAHEGOXIS on X) is saved from
|
|
// Settings, drawn by boot.js before the first paint, kept by a save that
|
|
// doesn't send it, and refused when it isn't a colour.
|
|
func TestAccentSetting(t *testing.T) {
|
|
srv := appearanceHome(t)
|
|
if rec := lookServe(srv, "POST", "/api/settings", `{"theme":"dark","lang":"en","accent":"#0d9488"}`); rec.Code != http.StatusOK {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
if got := settings.Load().Accent; got != "#0d9488" {
|
|
t.Fatalf("saved accent %q", got)
|
|
}
|
|
boot := lookServe(srv, "GET", "/boot.js", "").Body.String()
|
|
if !strings.Contains(boot, `"look":"#0d9488,"`) || !strings.Contains(boot, `e.id="lookCSS"`) || !strings.Contains(boot, "--accent:") {
|
|
t.Fatalf("boot.js draws no accent: %s", boot)
|
|
}
|
|
// a save from somewhere that doesn't know the accent keeps it
|
|
if rec := lookServe(srv, "POST", "/api/settings", `{"theme":"light","lang":"en"}`); rec.Code != http.StatusOK || settings.Load().Accent != "#0d9488" {
|
|
t.Fatalf("a save without the accent changed it: %d %q", rec.Code, settings.Load().Accent)
|
|
}
|
|
for _, bad := range []string{"teal", "#0d948", "#0d9488;}body{display:none", "red;--x:1"} {
|
|
b, _ := json.Marshal(map[string]string{"theme": "light", "lang": "en", "accent": bad})
|
|
if rec := lookServe(srv, "POST", "/api/settings", string(b)); rec.Code == http.StatusOK || settings.Load().Accent != "#0d9488" {
|
|
t.Fatalf("accent %q was taken: %d", bad, rec.Code)
|
|
}
|
|
}
|
|
// and "" is magpie's own again, with nothing drawn over it
|
|
if rec := lookServe(srv, "POST", "/api/settings", `{"theme":"light","lang":"en","accent":""}`); rec.Code != http.StatusOK || settings.Load().Accent != "" {
|
|
t.Fatalf("accent not reset: %d %q", rec.Code, settings.Load().Accent)
|
|
}
|
|
if boot := lookServe(srv, "GET", "/boot.js", "").Body.String(); strings.Contains(boot, "lookCSS") || !strings.Contains(boot, `"look":","`) {
|
|
t.Fatalf("boot.js still draws a look: %s", boot)
|
|
}
|
|
}
|
|
|
|
// Whatever colour is picked, the accent drawn reads as text on the
|
|
// theme's background and cards, and the text on it reads too.
|
|
func TestAccentReadable(t *testing.T) {
|
|
re := regexp.MustCompile(`--accent:(#[0-9a-f]{6});--accent-soft:(#[0-9a-f]{6});--accent-fg:(#[0-9a-f]{6});--sel:#[0-9a-f]{6};--mark:(#[0-9a-f]{6})$`)
|
|
for _, pick := range []string{"#ffffff", "#000000", "#ffff00", "#00ff00", "#0d9488", "#2563eb", "#ea580c", "#52525b", "#808080", "#1a1a1e", "#f4f4f6"} {
|
|
c, _ := parseHex(pick)
|
|
for _, dark := range []bool{false, true} {
|
|
m := re.FindStringSubmatch(accentVars(c, dark))
|
|
if m == nil {
|
|
t.Fatalf("%s: %s", pick, accentVars(c, dark))
|
|
}
|
|
a, _ := parseHex(m[1])
|
|
fg, _ := parseHex(m[3])
|
|
bgs := []rgb{lightBg, white}
|
|
if dark {
|
|
bgs = []rgb{darkBg, darkCard}
|
|
}
|
|
for _, bg := range bgs {
|
|
if r := contrast(a, bg); r < 4.4 {
|
|
t.Errorf("%s dark=%v: accent %s on %s is %.2f:1", pick, dark, m[1], bg.hex(), r)
|
|
}
|
|
}
|
|
if r := contrast(a, fg); r < 4.4 {
|
|
t.Errorf("%s dark=%v: text %s on accent %s is %.2f:1", pick, dark, m[3], m[1], r)
|
|
}
|
|
// what is chosen is in the colour picked (CHENGJIAHEGOXIS on X:
|
|
// a colour picked showed nowhere on Settings), and reads as text
|
|
// on every surface a chosen option sits on
|
|
mark, _ := parseHex(m[4])
|
|
on := []rgb{lightBg, white, lightPick}
|
|
if dark {
|
|
on = []rgb{darkBg, darkCard, darkPick}
|
|
}
|
|
for _, bg := range on {
|
|
if r := contrast(mark, bg); r < 4.4 {
|
|
t.Errorf("%s dark=%v: chosen ink %s on %s is %.2f:1", pick, dark, m[4], bg.hex(), r)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// a colour that reads already is drawn as picked
|
|
c, _ := parseHex("#2563eb")
|
|
if got := accentVars(c, false); !strings.HasPrefix(got, "--accent:#2563eb;") || !strings.Contains(got, ";--mark:#") {
|
|
t.Fatalf("a readable blue was changed: %s", got)
|
|
}
|
|
}
|
|
|
|
// A picture behind the pages is saved under magpie's folder, served back,
|
|
// drawn under a veil by boot.js, and removed with its file; what isn't a
|
|
// picture is refused.
|
|
func TestWallpaper(t *testing.T) {
|
|
srv := appearanceHome(t)
|
|
img := tinyPNG(t)
|
|
body, _ := json.Marshal(map[string][]byte{"data": img})
|
|
rec := lookServe(srv, "POST", "/api/appearance/wallpaper", string(body))
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
var got struct {
|
|
Wallpaper int64 `json:"wallpaper"`
|
|
}
|
|
if json.Unmarshal(rec.Body.Bytes(), &got); got.Wallpaper == 0 || settings.Load().Wallpaper != got.Wallpaper {
|
|
t.Fatalf("answered %d, saved %d", got.Wallpaper, settings.Load().Wallpaper)
|
|
}
|
|
if b, err := os.ReadFile(settings.WallpaperPath()); err != nil || !bytes.Equal(b, img) {
|
|
t.Fatalf("file: %v", err)
|
|
}
|
|
if !strings.HasPrefix(settings.WallpaperPath(), settings.Dir()) {
|
|
t.Fatalf("%s is not under %s", settings.WallpaperPath(), settings.Dir())
|
|
}
|
|
if r := lookServe(srv, "GET", "/api/appearance/wallpaper", ""); r.Code != http.StatusOK || r.Header().Get("Content-Type") != "image/png" || !bytes.Equal(r.Body.Bytes(), img) {
|
|
t.Fatalf("served %d %s", r.Code, r.Header().Get("Content-Type"))
|
|
}
|
|
boot := lookServe(srv, "GET", "/boot.js", "").Body.String()
|
|
if !strings.Contains(boot, "/api/appearance/wallpaper?v=") || !strings.Contains(boot, "color-mix(in srgb,var(--bg) 84%") || !strings.Contains(boot, "body:not(.panel)") {
|
|
t.Fatalf("boot.js draws no picture: %s", boot)
|
|
}
|
|
// /api/appearance/css says the same, for a page whose settings changed
|
|
var css struct{ CSS, Key string }
|
|
if json.Unmarshal(lookServe(srv, "GET", "/api/appearance/css", "").Body.Bytes(), &css); !strings.Contains(css.CSS, "/api/appearance/wallpaper?v=") || css.Key != lookKey(settings.Load()) {
|
|
t.Fatalf("css: %+v", css)
|
|
}
|
|
// a save from Settings keeps it
|
|
if rec := lookServe(srv, "POST", "/api/settings", `{"theme":"dark","lang":"en"}`); rec.Code != http.StatusOK || settings.Load().Wallpaper != got.Wallpaper {
|
|
t.Fatalf("Settings' save dropped the picture: %d", rec.Code)
|
|
}
|
|
// not a picture, or nothing: refused, the picture kept
|
|
for _, bad := range [][]byte{[]byte("<svg xmlns='http://www.w3.org/2000/svg'><script>alert(1)</script></svg>"), []byte("<html>hi</html>"), {}} {
|
|
b, _ := json.Marshal(map[string][]byte{"data": bad})
|
|
if rec := lookServe(srv, "POST", "/api/appearance/wallpaper", string(b)); rec.Code == http.StatusOK {
|
|
t.Fatalf("%q was taken", bad)
|
|
}
|
|
}
|
|
if b, _ := os.ReadFile(settings.WallpaperPath()); !bytes.Equal(b, img) || settings.Load().Wallpaper != got.Wallpaper {
|
|
t.Fatal("a refused picture replaced the one saved")
|
|
}
|
|
// too big: refused before it is read whole
|
|
big := append(append([]byte{}, img...), make([]byte, maxWallpaper)...)
|
|
b, _ := json.Marshal(map[string][]byte{"data": big})
|
|
if rec := lookServe(srv, "POST", "/api/appearance/wallpaper", string(b)); rec.Code == http.StatusOK {
|
|
t.Fatal("a picture over 16 MB was taken")
|
|
}
|
|
// removed: the file goes, nothing is drawn
|
|
if rec := lookServe(srv, "POST", "/api/appearance/wallpaper", `{"remove":true}`); rec.Code != http.StatusOK || settings.Load().Wallpaper != 0 {
|
|
t.Fatalf("remove: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if _, err := os.Stat(settings.WallpaperPath()); !os.IsNotExist(err) {
|
|
t.Fatalf("the file stayed: %v", err)
|
|
}
|
|
if boot := lookServe(srv, "GET", "/boot.js", "").Body.String(); strings.Contains(boot, "wallpaper?v=") {
|
|
t.Fatalf("boot.js still draws a picture: %s", boot)
|
|
}
|
|
if r := lookServe(srv, "GET", "/api/appearance/wallpaper", ""); r.Code != http.StatusNotFound {
|
|
t.Fatalf("a removed picture is served: %d", r.Code)
|
|
}
|
|
}
|
|
|
|
// A stamp whose file is gone (settings from another computer) draws no
|
|
// picture: the page would show its colour over nothing.
|
|
func TestWallpaperStampWithoutFile(t *testing.T) {
|
|
appearanceHome(t)
|
|
if css := lookCSS(settings.Settings{Wallpaper: 1}); css != "" {
|
|
t.Fatalf("drawn without a file: %s", css)
|
|
}
|
|
if css := lookCSS(settings.Settings{}); css != "" {
|
|
t.Fatalf("magpie's own look drew: %s", css)
|
|
}
|
|
}
|