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).
238 lines
7.5 KiB
Go
238 lines
7.5 KiB
Go
package gui
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"math"
|
||
"net/http"
|
||
"os"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/edit"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
)
|
||
|
||
// Settings › General's Accent color and Background image
|
||
// (CHENGJIAHEGOXIS on X): a colour of the user's own for magpie's
|
||
// buttons, links and selection, and a picture of their own behind the
|
||
// window's pages. Both are drawn by lookCSS, a stylesheet boot.js puts in
|
||
// the page before it paints and /api/appearance/css hands a page whose
|
||
// settings changed (appearance.js).
|
||
|
||
// maxWallpaper is the largest picture taken.
|
||
const maxWallpaper = 16 << 20
|
||
|
||
// wallpaperTypes are the pictures every webview magpie runs in draws.
|
||
var wallpaperTypes = []string{"image/png", "image/jpeg", "image/gif", "image/webp"}
|
||
|
||
// wallpaperType is b's type when it is a picture magpie takes.
|
||
func wallpaperType(b []byte) (string, error) {
|
||
if len(b) == 0 || len(b) > maxWallpaper {
|
||
return "", fmt.Errorf("a background image is %d MB at most", maxWallpaper>>20)
|
||
}
|
||
ct := http.DetectContentType(b)
|
||
for _, t := range wallpaperTypes {
|
||
if ct == t {
|
||
return ct, nil
|
||
}
|
||
}
|
||
return "", errors.New("a background image is a PNG, JPEG, GIF or WebP picture")
|
||
}
|
||
|
||
// lookKey is what the page compares to know its look is the saved one,
|
||
// as app.js's applyPrefs writes it: the accent and the wallpaper's stamp.
|
||
func lookKey(s settings.Settings) string {
|
||
w := ""
|
||
if s.Wallpaper != 0 {
|
||
w = strconv.FormatInt(s.Wallpaper, 10)
|
||
}
|
||
return s.Accent + "," + w
|
||
}
|
||
|
||
// lookCSS is the stylesheet that draws s's accent and wallpaper, "" for
|
||
// magpie's own look. It never applies under Omarchy, whose theme is the
|
||
// look there.
|
||
func lookCSS(s settings.Settings) string {
|
||
var b strings.Builder
|
||
if c, ok := parseHex(s.Accent); ok {
|
||
light, dark := accentVars(c, false), accentVars(c, true)
|
||
fmt.Fprintf(&b, "html:not(.omarchy):root{%s}\n", light)
|
||
fmt.Fprintf(&b, "@media (prefers-color-scheme: dark){html:not(.omarchy):root:not([data-theme=\"light\"]){%s}}\n", dark)
|
||
fmt.Fprintf(&b, "html:not(.omarchy):root[data-theme=\"dark\"]{%s}\n", dark)
|
||
}
|
||
if s.Wallpaper != 0 {
|
||
if fi, err := os.Stat(settings.WallpaperPath()); err == nil && fi.Mode().IsRegular() {
|
||
// the picture fills the window behind the pages, under a veil of
|
||
// the theme's own background (one colour, the top layer), so text
|
||
// on it reads as on the plain page; the cards over it stay opaque.
|
||
// The background's colour is still the theme's, which Windows'
|
||
// title bar is painted from (tintTitleBar)
|
||
fmt.Fprintf(&b, "html:not(.omarchy) body:not(.panel){background:linear-gradient(%[1]s,%[1]s),url(\"/api/appearance/wallpaper?v=%[2]d\") center/cover no-repeat var(--bg)}\n", wallVeil, s.Wallpaper)
|
||
b.WriteString("html:not(.omarchy) body:not(.panel) .sidebar{background:transparent}\n")
|
||
}
|
||
}
|
||
return b.String()
|
||
}
|
||
|
||
type rgb [3]float64
|
||
|
||
func parseHex(s string) (rgb, bool) {
|
||
if len(s) != 7 || s[0] != '#' {
|
||
return rgb{}, false
|
||
}
|
||
n, err := strconv.ParseUint(s[1:], 16, 32)
|
||
if err != nil {
|
||
return rgb{}, false
|
||
}
|
||
return rgb{float64(n >> 16 & 255), float64(n >> 8 & 255), float64(n & 255)}, true
|
||
}
|
||
|
||
func (c rgb) hex() string {
|
||
return fmt.Sprintf("#%02x%02x%02x", int(math.Round(c[0])), int(math.Round(c[1])), int(math.Round(c[2])))
|
||
}
|
||
|
||
// mix is c moved p of the way to o.
|
||
func (c rgb) mix(o rgb, p float64) rgb {
|
||
return rgb{c[0] + (o[0]-c[0])*p, c[1] + (o[1]-c[1])*p, c[2] + (o[2]-c[2])*p}
|
||
}
|
||
|
||
// luminance is WCAG's relative luminance.
|
||
func (c rgb) luminance() float64 {
|
||
ch := func(v float64) float64 {
|
||
v /= 255
|
||
if v <= 0.03928 {
|
||
return v / 12.92
|
||
}
|
||
return math.Pow((v+0.055)/1.055, 2.4)
|
||
}
|
||
return 0.2126*ch(c[0]) + 0.7152*ch(c[1]) + 0.0722*ch(c[2])
|
||
}
|
||
|
||
// contrast is WCAG's contrast ratio of a and b.
|
||
func contrast(a, b rgb) float64 {
|
||
la, lb := a.luminance(), b.luminance()
|
||
if la < lb {
|
||
la, lb = lb, la
|
||
}
|
||
return (la + 0.05) / (lb + 0.05)
|
||
}
|
||
|
||
var (
|
||
white = rgb{255, 255, 255}
|
||
black = rgb{0, 0, 0}
|
||
// the themes' backgrounds (app.css --bg and --card), what the accent
|
||
// is read against as text
|
||
lightBg = rgb{0xf4, 0xf4, 0xf6}
|
||
darkBg = rgb{0x1a, 0x1a, 0x1e}
|
||
darkCard = rgb{0x23, 0x23, 0x27}
|
||
accentInk = 4.5 // WCAG AA for text: the accent is a link's colour too
|
||
// how much of the theme's background lies over the picture
|
||
wallVeil = "color-mix(in srgb,var(--bg) 84%,transparent)"
|
||
)
|
||
|
||
// accentOn is c, darkened (light theme) or lightened (dark theme) just
|
||
// enough to read as text on that theme's background and cards.
|
||
func accentOn(c rgb, dark bool) rgb {
|
||
toward, against := black, lightBg
|
||
if dark {
|
||
toward, against = white, darkCard
|
||
}
|
||
out := c
|
||
for p := 0.0; p <= 1 && contrast(out, against) < accentInk; p += 0.02 {
|
||
out = c.mix(toward, p)
|
||
}
|
||
return out
|
||
}
|
||
|
||
// accentVars are the theme's --accent, its soft fill, the text on it and
|
||
// the selection, from c.
|
||
func accentVars(c rgb, dark bool) string {
|
||
a := accentOn(c, dark)
|
||
fg := white
|
||
if contrast(a, rgb{0x14, 0x14, 0x1a}) > contrast(a, white) {
|
||
fg = rgb{0x14, 0x14, 0x1a}
|
||
}
|
||
soft, sel := white.mix(a, 0.09), white.mix(a, 0.11)
|
||
if dark {
|
||
soft, sel = darkBg.mix(a, 0.14), darkBg.mix(a, 0.2)
|
||
}
|
||
return fmt.Sprintf("--accent:%s;--accent-soft:%s;--accent-fg:%s;--sel:%s", a.hex(), soft.hex(), fg.hex(), sel.hex())
|
||
}
|
||
|
||
// appearanceRoutes serve the look's stylesheet and the wallpaper, and take
|
||
// a picture chosen or removed.
|
||
func appearanceRoutes(mux *http.ServeMux) {
|
||
mux.HandleFunc("GET /api/appearance/css", func(rw http.ResponseWriter, r *http.Request) {
|
||
s := settings.Load()
|
||
writeJSON(rw, map[string]string{"css": lookCSS(s), "key": lookKey(s)})
|
||
})
|
||
mux.HandleFunc("GET /api/appearance/wallpaper", func(rw http.ResponseWriter, r *http.Request) {
|
||
b, err := os.ReadFile(settings.WallpaperPath())
|
||
if err != nil {
|
||
http.NotFound(rw, r)
|
||
return
|
||
}
|
||
ct, err := wallpaperType(b)
|
||
if err != nil {
|
||
http.NotFound(rw, r)
|
||
return
|
||
}
|
||
rw.Header().Set("Content-Type", ct)
|
||
rw.Header().Set("Cache-Control", "private, max-age=31536000, immutable") // its address changes with it (?v=)
|
||
rw.Header().Set("X-Content-Type-Options", "nosniff")
|
||
rw.Write(b)
|
||
})
|
||
// the picture comes as base64 in JSON, as the app's web view drops a
|
||
// File sent as the body; {"remove": true} takes it away
|
||
mux.HandleFunc("POST /api/appearance/wallpaper", func(rw http.ResponseWriter, r *http.Request) {
|
||
var in struct {
|
||
Data []byte `json:"data"`
|
||
Remove bool `json:"remove"`
|
||
}
|
||
if err := json.NewDecoder(http.MaxBytesReader(rw, r.Body, maxWallpaper*4/3+64<<10)).Decode(&in); err != nil {
|
||
fail(rw, fmt.Errorf("a background image is a PNG, JPEG, GIF or WebP picture of %d MB at most", maxWallpaper>>20))
|
||
return
|
||
}
|
||
if err := setWallpaper(in.Data, in.Remove); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
writeJSON(rw, settingsState())
|
||
})
|
||
}
|
||
|
||
// setWallpaper saves b as the picture behind the pages, or removes it.
|
||
func setWallpaper(b []byte, remove bool) error {
|
||
if !remove {
|
||
if _, err := wallpaperType(b); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
s, err := settings.LoadChecked()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if remove {
|
||
s.Wallpaper = 0
|
||
if err := settings.Save(s); err != nil {
|
||
return err
|
||
}
|
||
if err := os.Remove(settings.WallpaperPath()); err != nil && !os.IsNotExist(err) {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
if err := os.MkdirAll(settings.Dir(), 0o700); err != nil {
|
||
return err
|
||
}
|
||
if err := edit.WriteAtomic(settings.WallpaperPath(), b); err != nil {
|
||
return err
|
||
}
|
||
// a new stamp, so the page asks for the new picture, not its cached one
|
||
s.Wallpaper = max(time.Now().UnixMilli(), s.Wallpaper+1)
|
||
return settings.Save(s)
|
||
}
|