diff --git a/Makefile b/Makefile index 08b67ba7c..ef344946c 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,12 @@ app: build icons: @go run build/icon/gen.go tray internal/gui/tray.png @go run build/icon/gen.go tray-flap internal/gui/trayflap - @go run build/icon/gen.go app 64 internal/gui/icon.png + @# Windows draws an app's icon to the edges of its square: the exe's + @# icon group (build/windows/winres.json) and the tray's .ico + @for s in 16 20 24 32 40 48 64 128 256; do \ + go run build/icon/gen.go win $$s build/windows/icon-$$s.png; \ + done + @go run build/icon/gen.go win-ico internal/gui/icon-windows.ico @go run build/icon/gen.go app 1024 internal/gui/icon-1024.png @go run build/icon/gen.go app 256 internal/gui/icon-256.png @rm -rf build/darwin/magpie.iconset && mkdir -p build/darwin/magpie.iconset diff --git a/build/icon/gen.go b/build/icon/gen.go index 105daa4df..e3bf4eb81 100644 --- a/build/icon/gen.go +++ b/build/icon/gen.go @@ -11,11 +11,15 @@ // go run build/icon/gen.go tray internal/gui/tray.png # 44px black template icon (macOS menu bar) // go run build/icon/gen.go tray-flap internal/gui/trayflap # its frames when clicked (a size after the folder for a bigger look) // go run build/icon/gen.go app 64 internal/gui/icon.png # app icon at a given size +// go run build/icon/gen.go win 256 build/windows/icon-256.png # the Windows tile, filling its square +// go run build/icon/gen.go win-ico internal/gui/icon-windows.ico # it at the tray's sizes, one .ico // make icons # regenerates all of them plus magpie.icns package main import ( + "bytes" _ "embed" + "encoding/binary" "fmt" "image" "image/color" @@ -58,8 +62,17 @@ func main() { case len(os.Args) == 4 && (os.Args[1] == "app" || os.Args[1] == "app-dark"): n, _ := strconv.Atoi(os.Args[2]) img = app(n, os.Args[1] == "app-dark") + case len(os.Args) == 4 && os.Args[1] == "win": + n, _ := strconv.Atoi(os.Args[2]) + img = win(n) + case len(os.Args) == 3 && os.Args[1] == "win-ico": + if err := winICO(os.Args[2], trayICOSizes); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return default: - fmt.Fprintln(os.Stderr, "usage: gen.go tray | gen.go app|app-dark ") + fmt.Fprintln(os.Stderr, "usage: gen.go tray | gen.go app|app-dark|win | gen.go win-ico ") os.Exit(2) } f, err := os.Create(os.Args[len(os.Args)-1]) @@ -262,13 +275,30 @@ func glyph(n int) image.Image { } // app draws a rounded square, white shading to the palest grey, with the -// bird on it in ink. +// bird on it in ink, on macOS's icon grid: the tile is ~80% of the +// canvas, the rest left clear as the Dock and Finder expect. func app(n int, dark bool) image.Image { S := float64(n) - // macOS icon grid: the squircle occupies ~80% of the canvas inset := S * 0.1 + return tile(n, inset, (S-2*inset)*0.225, dark) +} + +// win is the same tile as Windows draws an app's icon: filling its square, +// as the icons beside it in the taskbar, the tray and Start do. On macOS's +// grid it was a fifth smaller than its neighbours there (dzxx0496). A +// pixel is left round it from 32px up, a 32nd of the side, so the rounded +// corners' soft edge stays inside the square; under 32px that pixel would +// be most of the margin and only blur the edge, so the tile meets it. +func win(n int) image.Image { + inset := float64(n / 32) + return tile(n, inset, (float64(n)-2*inset)*0.2, false) +} + +// tile draws the rounded square inset from each edge of an n-pixel canvas, +// its corners of the given radius, and the bird on it at .8 of its side. +func tile(n int, inset, radius float64, dark bool) image.Image { + S := float64(n) side := S - 2*inset - radius := side * 0.225 k := side * 0.8 / 44 o := inset + side/2 - 22*k ss := 4 @@ -315,6 +345,46 @@ func app(n int, dark bool) image.Image { return img } +// trayICOSizes are the sizes Windows asks a tray icon for (SM_CXSMICON): +// 16px at 100% scaling, 20, 24, 32 and 40 at 125% to 250%, and 48 and 64 +// beyond. Wails picks the one it needs from the .ico, where a single PNG +// was scaled down by the system and blurred. +var trayICOSizes = []int{16, 20, 24, 32, 40, 48, 64} + +// winICO writes win's tile at each size into one .ico, each image a PNG +// (Windows Vista on reads them). +func winICO(out string, sizes []int) error { + var pngs [][]byte + for _, n := range sizes { + var b bytes.Buffer + if err := png.Encode(&b, win(n)); err != nil { + return err + } + pngs = append(pngs, b.Bytes()) + } + var ico bytes.Buffer + le := binary.LittleEndian + ico.Write(le.AppendUint16(le.AppendUint16(le.AppendUint16(nil, 0), 1), uint16(len(sizes)))) + at := 6 + 16*len(sizes) + for i, n := range sizes { + d := byte(n) + if n >= 256 { + d = 0 + } + e := []byte{d, d, 0, 0} + e = le.AppendUint16(e, 1) // planes + e = le.AppendUint16(e, 32) // bits per pixel + e = le.AppendUint32(e, uint32(len(pngs[i]))) + e = le.AppendUint32(e, uint32(at)) + ico.Write(e) + at += len(pngs[i]) + } + for _, p := range pngs { + ico.Write(p) + } + return os.WriteFile(out, ico.Bytes(), 0o644) +} + // coverage rasterises a shape, scaled by k and moved to (ox, oy), onto an n×n // grid of pixels with ss×ss samples each, and gives each pixel's coverage in // 0..1. It fills even-odd, a scanline at a time. diff --git a/build/windows/icon-128.png b/build/windows/icon-128.png index 1988ac6c4..3af5ead6b 100644 Binary files a/build/windows/icon-128.png and b/build/windows/icon-128.png differ diff --git a/build/windows/icon-16.png b/build/windows/icon-16.png index 5eef8ad98..c983ca919 100644 Binary files a/build/windows/icon-16.png and b/build/windows/icon-16.png differ diff --git a/build/windows/icon-20.png b/build/windows/icon-20.png new file mode 100644 index 000000000..b2a559839 Binary files /dev/null and b/build/windows/icon-20.png differ diff --git a/build/windows/icon-24.png b/build/windows/icon-24.png new file mode 100644 index 000000000..ea8ba2ce0 Binary files /dev/null and b/build/windows/icon-24.png differ diff --git a/build/windows/icon-256.png b/build/windows/icon-256.png index 6531d29b4..49ababec7 100644 Binary files a/build/windows/icon-256.png and b/build/windows/icon-256.png differ diff --git a/build/windows/icon-32.png b/build/windows/icon-32.png index 9349bde49..a99ab642f 100644 Binary files a/build/windows/icon-32.png and b/build/windows/icon-32.png differ diff --git a/build/windows/icon-40.png b/build/windows/icon-40.png new file mode 100644 index 000000000..fe3fbbdce Binary files /dev/null and b/build/windows/icon-40.png differ diff --git a/build/windows/icon-48.png b/build/windows/icon-48.png index 7c12f50de..422e00193 100644 Binary files a/build/windows/icon-48.png and b/build/windows/icon-48.png differ diff --git a/build/windows/icon-64.png b/build/windows/icon-64.png index a38774f1a..288e7680c 100644 Binary files a/build/windows/icon-64.png and b/build/windows/icon-64.png differ diff --git a/build/windows/winres.json b/build/windows/winres.json index 902a96b35..c1bf37509 100644 --- a/build/windows/winres.json +++ b/build/windows/winres.json @@ -1,7 +1,7 @@ { "RT_GROUP_ICON": { "#3": { - "0000": ["icon-256.png", "icon-128.png", "icon-64.png", "icon-48.png", "icon-32.png", "icon-16.png"] + "0000": ["icon-256.png", "icon-128.png", "icon-64.png", "icon-48.png", "icon-40.png", "icon-32.png", "icon-24.png", "icon-20.png", "icon-16.png"] } }, "RT_MANIFEST": { diff --git a/internal/gui/app.go b/internal/gui/app.go index 16c33e55e..df996f335 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -42,8 +42,12 @@ var trayIcon []byte // black glyph, tinted by the macOS menu bar //go:embed trayflap/*.png var trayFlap embed.FS -//go:embed icon.png -var appIcon []byte // coloured, for Windows' tray +// winTrayIcon is the tile for Windows' tray, at each size it asks for +// (build/icon/gen.go win-ico): filling its square as the icons beside it +// do, where the Mac's tile with its clear margin looked a size smaller. +// +//go:embed icon-windows.ico +var winTrayIcon []byte // The app's own icon: the Mac's Dock (which it replaces the bundle's .icns // in, at up to 512pt), window icons and the about box. At 64px it was diff --git a/internal/gui/icon-windows.ico b/internal/gui/icon-windows.ico new file mode 100644 index 000000000..1604a89a9 Binary files /dev/null and b/internal/gui/icon-windows.ico differ diff --git a/internal/gui/icon.png b/internal/gui/icon.png deleted file mode 100644 index a38774f1a..000000000 Binary files a/internal/gui/icon.png and /dev/null differ diff --git a/internal/gui/trayglyph.go b/internal/gui/trayglyph.go index 25b357e67..3240c3309 100644 --- a/internal/gui/trayglyph.go +++ b/internal/gui/trayglyph.go @@ -56,7 +56,7 @@ func trayIconFor(goos string, light bool) (icon []byte, template bool) { case "linux": return linuxTrayIcon(light), false } - return appIcon, false + return winTrayIcon, false } // linuxTrayIcon is the glyph for a desktop whose colour scheme preference diff --git a/internal/gui/trayglyph_test.go b/internal/gui/trayglyph_test.go index d5ef84879..8ce4e196d 100644 --- a/internal/gui/trayglyph_test.go +++ b/internal/gui/trayglyph_test.go @@ -57,8 +57,8 @@ func TestTrayIconPerOS(t *testing.T) { if icon, template := trayIconFor("darwin", false); !template || !bytes.Equal(icon, trayIcon) { t.Errorf("darwin: want the template bird") } - if icon, template := trayIconFor("windows", false); template || !bytes.Equal(icon, appIcon) { - t.Errorf("windows: want the app's own icon") + if icon, template := trayIconFor("windows", false); template || !bytes.Equal(icon, winTrayIcon) { + t.Errorf("windows: want the Windows tile") } if img, _ := png.Decode(bytes.NewReader(trayGlyph(trayGlyphLight))); img == nil || img.ColorModel() != color.NRGBAModel { t.Errorf("trayGlyph: want an NRGBA png, got %T", img) diff --git a/internal/gui/wintile_test.go b/internal/gui/wintile_test.go new file mode 100644 index 000000000..0bc36a780 --- /dev/null +++ b/internal/gui/wintile_test.go @@ -0,0 +1,83 @@ +//go:build !nogui + +package gui + +import ( + "bytes" + "encoding/binary" + "encoding/json" + "image" + "image/png" + "os" + "path/filepath" + "testing" +) + +// Windows draws an app's icon to the edges of its square. Made on the +// Mac's grid, magpie's tile was 80% of it and looked a size smaller than +// its neighbours in the taskbar, the tray and Start (dzxx0496). Each +// picture the exe's icon group lists (the taskbar, the window, Start's +// shortcut) and each size of the tray's .ico is a tile over at least +// 15/16 of its side, centred, and opaque in the middle. +func TestWindowsIconsFillTheirSquare(t *testing.T) { + var res struct { + Group map[string]map[string][]string `json:"RT_GROUP_ICON"` + } + b, err := os.ReadFile(filepath.Join("..", "..", "build", "windows", "winres.json")) + if err != nil { + t.Fatal(err) + } + if err := json.Unmarshal(b, &res); err != nil { + t.Fatal(err) + } + files := map[string][]byte{} + for _, f := range res.Group["#3"]["0000"] { + b, err := os.ReadFile(filepath.Join("..", "..", "build", "windows", f)) + if err != nil { + t.Fatal(err) + } + files["build/windows/"+f] = b + } + if len(files) == 0 { + t.Fatal("winres.json lists no icon for group #3") + } + ico := winTrayIcon + le := binary.LittleEndian + if len(ico) < 6 || le.Uint16(ico[2:]) != 1 { + t.Fatal("icon-windows.ico is not an .ico") + } + n := int(le.Uint16(ico[4:])) + sizes := map[int]bool{} + for i := range n { + e := ico[6+16*i:] + size, at := int(le.Uint32(e[8:])), int(le.Uint32(e[12:])) + files["icon-windows.ico#"+string(rune('0'+i))] = ico[at : at+size] + sizes[int(e[0])] = true + } + for _, s := range []int{16, 24, 32} { + if !sizes[s] { + t.Errorf("icon-windows.ico has no %dpx picture", s) + } + } + for name, b := range files { + img, err := png.Decode(bytes.NewReader(b)) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + r := img.Bounds() + box := image.Rectangle{Min: r.Max, Max: r.Min} + for y := r.Min.Y; y < r.Max.Y; y++ { + for x := r.Min.X; x < r.Max.X; x++ { + if _, _, _, a := img.At(x, y).RGBA(); a > 0 { + box = box.Union(image.Rect(x, y, x+1, y+1)) + } + } + } + if box.Dx()*16 < r.Dx()*15 || box.Dy()*16 < r.Dy()*15 || box.Min.X-r.Min.X != r.Max.X-box.Max.X { + t.Errorf("%s: tile %v in %v, want at least 15/16 of the side, centred", name, box, r) + } + if _, _, _, a := img.At(r.Dx()/2, r.Dy()/8).RGBA(); a != 0xffff { + t.Errorf("%s: the tile is not opaque inside", name) + } + } +}