CHENGJIAHEGOXIS asked for colours beyond the three themes and for a local picture. Settings › General gains two rows under Appearance: - Accent color: magpie's own, six quiet presets (Blue, Teal, Green, Orange, Pink, Graphite) and a swatch that opens the system's colour picker. Saved as settings.accent (#rrggbb only, checked in saveOwn; `magpie settings set accent "#0d9488"` works through settingsapi.Keys). The server draws it (appearance.go accentVars): darkened on the light theme and lightened on the dark one until it reads as text at 4.5:1 on the background and cards, with white or near-black text on it, whichever reads better. Never under Omarchy, whose theme is the look. - Background image: Choose… takes a PNG, JPEG, GIF or WebP of 16 MB at most, sent as base64 JSON to POST /api/appearance/wallpaper, saved as `wallpaper` in magpie's folder (edit.WriteAtomic); settings.Wallpaper is its stamp. A thumbnail, Change and Remove show; Remove deletes the file. It is drawn behind the window's pages (never the tray panel) under a veil of the theme's background at 84%, so text reads as on the plain page; the cards stay opaque, and body's colour stays the theme's, which Windows' title bar tint reads. boot.js puts the look's stylesheet in before the first paint and says which look it drew (bootPrefs.look); applyPrefs asks /api/appearance/css only when the settings' look differs. The rows, with the Colorblind, Reduce motion and Loading bird rows, are drawn by appearance.js, imported when Settings draws, so the core scripts stay under their budgets with no raise (app.js 2,393 under, the core 503 under). A save of Settings that doesn't send accent keeps it (Merge guard), and the wallpaper stamp is kept by every Settings save. fonts.test.cjs: the two new rows push the font row to the foot of its 560×620 window, where Playwright's click scrolled it in by a pixel and the test read that as the page moving; the reader now scrolls it in first with the wheel. settings_keep_test sets Wallpaper; the three /api/appearance routes are named in the CLI parity test's notYet. Run (mote, at this change on origin/main 406ad72f1; after the rebase onfa3ee5a7e, which touched only the Library plugin and the site here, the core budget, TestEveryRoute*, TestSettings*, TestAccent*, TestWallpaper*, appearance-look and i18n-keys again, the same result): - gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on internal/gui, settings, settingsapi: clean. - go test -count=1 ./internal/gui/... ./internal/settings/... ./internal/settingsapi/... ./internal/cli/...: ok. (go vet ./internal/cli too.) - GUI, chromium and webkit: appearance-look (picking a preset, the reader's own and back; boot.js's look not fetched again; choosing and removing a picture; the rows keep their height while appearance.js loads; en, zh, zh-TW, ja, de at 420 and 1000px: rows fit, words translated), fonts, settings-general, settings-sections, settings-groups, reduce-motion, skeleton-bird, i18n-en-no-cjk: green. Red the same at origin/main7693cf263without this change, all filed: api-key-theme (#6444, filed now), i18n-keys "No folder" (#6422), settings-search and settings-lines-wrap de 440px (#6353). Broken by hand, each red, then restored: - Merge guard removed: TestAccentSetting red ("a save without the accent changed it"). - accentInk = 1.0: TestAccentReadable red. - applyPrefs' lookPaint call removed: appearance-look's accent and picture tests red in chromium. - the thumbnail made 66px tall: the row-height test red. Not tried: the real app on Windows, Linux or Omarchy (title bar tint with a picture, WebKitGTK drawing the layers), the system's real colour picker dialog (the test sets the input's value), a 16 MB picture in the real web views, and the whole GUI suite (queued on mote's suite lock after the tag).
199 lines
8.3 KiB
Go
199 lines
8.3 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})`)
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
// a colour that reads already is drawn as picked
|
|
c, _ := parseHex("#2563eb")
|
|
if got := accentVars(c, false); !strings.HasPrefix(got, "--accent:#2563eb;") {
|
|
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)
|
|
}
|
|
}
|