tray: a plugin's menu bar card draws its stored picture, not its name's letter (#5772, guoxudong_ on X)
A plugin's quota card carries its picture as a stored "file:…" name (PluginIcon → StoreIcon), and the Usage page draws it. trayIconFile, which reads the menu bar card's logo, refused every name with a ':' in it, so the card fell back to the name's first letter. A picture the user gave their own provider was refused the same way. trayIconFile now reads a "file:" name as pageIconOf, its sibling for the day page, does: through provider.IconServed (IconFile's check, 16 hex digits and a picture's ending, so no path of the caller's own; the scaled copy for a big PNG/JPEG/WebP), at most maxPageIcon bytes, an SVG's arc flags spaced out as the bundled ones are. Anything else with a ':', '/' or '\' stays refused. Ported from sunny0826's fork (tray-plugin-logo, 080c11f98), reviewed for harm first: no new dependency, network, or path outside the icons folder. Changed from the fork: IconServed with the size cap and SVG arc flags in place of a raw IconFile read; the refusal test writes each file a refused name could reach, so it tests the check, not a missing file. Siblings: pageIconOf (day page) already reads stored pictures; app.js's icon() does too; only the macOS tray draws cell logos, and every tray goes through trayIconFile. Run (mote, darwin arm64, at this tree): - go test ./internal/gui ./internal/provider: ok. - gofmt -l internal/gui: clean. go vet (darwin), GOOS=linux -tags nogui, GOOS=windows on internal/gui and internal/provider: ok. - win-run TestTrayIconFileStored on xiaomi-gpu: ok (internal/gui, -run TestTrayIconFileStored). Broken by hand: - trayusage.go at origin/main: TestTrayIconFileStored and TestTrayImageStoredIcon fail (0 bytes for the stored picture). - IconServed swapped for filepath.Join(IconDir(), own): the test reads "file:mine.png", "file:../providers.json" and "file:0123456789abcdef.exe" and goes red. Not tried: a real plugin's card in the built app's menu bar (the fork's author reports it drawn, light and dark); the full suite runs after the tag. Co-authored-by: Xudong Guo <24563928+sunny0826@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
//go:build darwin && cgo
|
||||
|
||||
package gui
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A picture a plugin gave, stored as a "file:…" one, is drawn in the menu
|
||||
// bar's text colour as any other logo is (TestTrayImageMonoLogos): no
|
||||
// colour in it, its shape what is opaque less what is near white. A tile
|
||||
// of colour with a white glyph cut out (as plugins' logos often are) is
|
||||
// drawn in ink with the glyph cut out, light and dark at 2x.
|
||||
func TestTrayImageStoredIcon(t *testing.T) {
|
||||
bird, err := os.ReadFile("tray.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
||||
|
||||
// a 32x32 green tile with a white bird-shaped glyph in it
|
||||
src := image.NewNRGBA(image.Rect(0, 0, 32, 32))
|
||||
for y := 0; y < 32; y++ {
|
||||
for x := 0; x < 32; x++ {
|
||||
switch {
|
||||
case x >= 8 && x < 24 && y >= 8 && y < 24 && (x+y)%4 != 0:
|
||||
src.SetNRGBA(x, y, color.NRGBA{R: 255, G: 255, B: 255, A: 255}) // the glyph, near white
|
||||
default:
|
||||
src.SetNRGBA(x, y, color.NRGBA{R: 110, G: 195, B: 14, A: 255}) // the tile, green
|
||||
}
|
||||
}
|
||||
}
|
||||
var tile bytes.Buffer
|
||||
if err := png.Encode(&tile, src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kept, err := provider.StoreIcon(tile.Bytes())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
icon, mono := trayIconFile(kept)
|
||||
if mono || len(icon) == 0 {
|
||||
t.Fatalf("trayIconFile(%q) = %d bytes, mono %v", kept, len(icon), mono)
|
||||
}
|
||||
out := os.Getenv("MAGPIE_TRAY_PREVIEW")
|
||||
for _, dark := range []bool{false, true} {
|
||||
b, _, _ := trayImagePNG([]trayCell{{Icon: icon, Mono: mono, Rows: []string{"5%", "7%"}}}, bird, 22, 2, dark, true)
|
||||
img, err := png.Decode(bytes.NewReader(b))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logo := image.Rect(48, 8, 48+28, 36)
|
||||
for y := logo.Min.Y; y < logo.Max.Y; y++ {
|
||||
for x := logo.Min.X; x < logo.Max.X; x++ {
|
||||
r, g, bl, _ := img.At(x, y).RGBA()
|
||||
if max(r, g, bl)-min(r, g, bl) > 0x1800 {
|
||||
t.Fatalf("stored icon, dark %v: colour at %d,%d: %x %x %x", dark, x, y, r>>8, g>>8, bl>>8)
|
||||
}
|
||||
}
|
||||
}
|
||||
ink := inkIn(img, logo, dark)
|
||||
if ink < 40 {
|
||||
t.Errorf("stored icon, dark %v: the logo has %d px of ink", dark, ink)
|
||||
}
|
||||
// the white glyph is cut out of the tile: the ink is less than the
|
||||
// whole box, as Codex's is (TestTrayImageMonoLogos)
|
||||
if ink > 28*28*85/100 {
|
||||
t.Errorf("stored icon, dark %v: %d px of ink, its glyph not cut out", dark, ink)
|
||||
}
|
||||
if out != "" {
|
||||
name := filepath.Join(out, "stored-"+map[bool]string{false: "light", true: "dark"}[dark]+".png")
|
||||
if err := os.WriteFile(name, b, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -756,9 +757,24 @@ const trayRowMax = 10
|
||||
|
||||
// trayIconFile is a card's logo among the page's icons, as its icon() has
|
||||
// it: a "-color" one and the PNGs in their own colours, the others a black
|
||||
// glyph (a mask there); nil when there is no such file (a picture the user
|
||||
// gave their own provider isn't one).
|
||||
// glyph (a mask there); a stored picture ("file:…", one a plugin or the
|
||||
// user gave a provider) read as pageIconOf reads it, through its checked
|
||||
// name only; nil when there is no such file.
|
||||
func trayIconFile(name string) (b []byte, mono bool) {
|
||||
if own, ok := strings.CutPrefix(name, "file:"); ok {
|
||||
f, _ := provider.IconServed(own)
|
||||
if f == "" {
|
||||
return nil, false
|
||||
}
|
||||
b, err := os.ReadFile(f)
|
||||
if err != nil || len(b) > maxPageIcon {
|
||||
return nil, false
|
||||
}
|
||||
if strings.HasSuffix(own, ".svg") {
|
||||
b = svgArcFlags(b)
|
||||
}
|
||||
return b, false
|
||||
}
|
||||
if name == "" || strings.ContainsAny(name, `/\:`) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -161,3 +163,37 @@ func TestSVGArcFlags(t *testing.T) {
|
||||
t.Errorf("codex's logo not spaced out: %.300s", b)
|
||||
}
|
||||
}
|
||||
|
||||
// A card's logo can be a picture the plugin gave (a stored "file:…" one),
|
||||
// which the menu bar reads as the page's icons are read, in its own
|
||||
// colours; anything else under "file:" is refused and the name's letter
|
||||
// drawn in its place.
|
||||
func TestTrayIconFileStored(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
||||
|
||||
png := []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")
|
||||
kept, err := provider.StoreIcon(png)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, mono := trayIconFile(kept)
|
||||
if mono || string(b) != string(png) {
|
||||
t.Errorf("trayIconFile(%q) = %d bytes, mono %v", kept, len(b), mono)
|
||||
}
|
||||
// each file a refused name could reach is there, so a refusal is the
|
||||
// name's check and not a missing file
|
||||
dir := provider.IconDir()
|
||||
for _, f := range []string{filepath.Join(dir, "mine.png"), filepath.Join(dir, "..", "providers.json"), filepath.Join(dir, "0123456789abcdef.exe")} {
|
||||
if err := os.WriteFile(f, png, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"file:mine.png", "file:../providers.json", "file:", "http://x/y.png", "file:0123456789abcdef.exe"} {
|
||||
if b, mono = trayIconFile(bad); b != nil || mono {
|
||||
t.Errorf("trayIconFile(%q) = %d bytes, mono %v", bad, len(b), mono)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user