fix(tray): let the system draw a black/white mark, not the colour wheel

Reported: "收到系统小图标时采用黑/白单色图标." The tray was handed
`app.default_window_icon()`, the 512 px gradient wheel, and `tray-icon`
scales whatever it is given to 18 pt: on the menu bar and in the Windows
notification area that is a blob, not a mark.

The new icon is drawn flat and alpha-only by `scripts/build-tray-icon.py`,
in the shape of the ring the app itself paints so the two read as one
product, and `icon_as_template` lets macOS tint it black or white per the
menu bar. Windows has no tinting to ask of, so it gets the same glyph at a
size it can actually render.

Co-Authored-By: Codex <noreply@openai.com>
main
mula.liu 2026-11-19 17:45:07 +08:00
parent 85de468bc3
commit 287b3af09d
3 changed files with 103 additions and 4 deletions

View File

@ -0,0 +1,90 @@
#!/usr/bin/env python3
"""Redraw src-tauri/icons/tray-template.png, the mark the system tray shows.
scripts/build-tray-icon.py [--out PATH]
The tray is drawn by the shell, not by Shift, and at 16-18 px the app's colour
wheel is a blob: macOS tints a *template* image black or white to match the menu
bar, and the Windows notification area simply shrinks whatever it is given. So
this file is a flat single-tint mark whose only real channel is alpha — the same
shape as the radial ring the app itself draws, so the two read as one product.
Only Pillow is needed. The mark is drawn at 4x and averaged down so the wedge
edges and the cross arms stay smooth at every size the shell asks for.
"""
import argparse
import math
import pathlib
from PIL import Image, ImageDraw
SIZE = 88 # source edge in px; tray-icon renders the image 18 pt tall
SS = 4 # supersample on top of SIZE
CANVAS = SIZE * SS
CENTER = CANVAS / 2
OUTER = 38.0 # wedge ring, outer radius
INNER = 21.0 # wedge ring, inner radius
HUB = 12.6 # solid centre disc
GAP = 5.2 # degrees of air between wedges
PLUS_ARM = 7.8 # half-length of the cross cut out of the hub
PLUS_BAR = 2.8 # half-thickness of that cross
TINT = (128, 128, 128) # unread by macOS, ignored except as a base for Windows
def ring_segment(radius_out, radius_in, start, stop):
"""Corners of a filled annular sector, outer arc forward then inner back."""
steps = max(2, int(stop - start) + 1)
points = [
(CENTER + radius_out * math.cos(math.radians(a)), CENTER + radius_out * math.sin(math.radians(a)))
for a in [start + (stop - start) * i / steps for i in range(steps + 1)]
]
points += [
(CENTER + radius_in * math.cos(math.radians(a)), CENTER + radius_in * math.sin(math.radians(a)))
for a in [start + (stop - start) * i / steps for i in range(steps, -1, -1)]
]
return points
def render_mask() -> Image.Image:
mask = Image.new("L", (CANVAS, CANVAS), 0)
draw = ImageDraw.Draw(mask)
for wedge in range(6):
start = -90 + wedge * 60 + GAP / 2
stop = -90 + (wedge + 1) * 60 - GAP / 2
draw.polygon(ring_segment(OUTER * SS, INNER * SS, start, stop), fill=255)
draw.ellipse(
[CENTER - HUB * SS, CENTER - HUB * SS, CENTER + HUB * SS, CENTER + HUB * SS], fill=255
)
width = int(PLUS_BAR * SS)
draw.line(
[CENTER - PLUS_ARM * SS, CENTER, CENTER + PLUS_ARM * SS, CENTER], fill=0, width=width
)
draw.line(
[CENTER, CENTER - PLUS_ARM * SS, CENTER, CENTER + PLUS_ARM * SS], fill=0, width=width
)
return mask.resize((SIZE, SIZE), Image.LANCZOS)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--out",
type=pathlib.Path,
default=pathlib.Path(__file__).resolve().parent.parent
/ "src-tauri"
/ "icons"
/ "tray-template.png",
)
args = parser.parse_args()
icon = Image.new("RGBA", (SIZE, SIZE), TINT + (0,))
icon.putalpha(render_mask())
args.out.parent.mkdir(parents=True, exist_ok=True)
icon.save(args.out)
print(f"wrote {args.out} ({SIZE}x{SIZE}, alpha-only monochrome mark)")
if __name__ == "__main__":
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

@ -225,16 +225,28 @@ pub fn refresh_tray_language(app: &AppHandle) -> Result<(), String> {
Ok(())
}
/// The wheel mark, drawn flat and alpha-only so the shell can tint it.
/// Regenerate with `scripts/build-tray-icon.py`.
const TRAY_ICON: &[u8] = include_bytes!("../icons/tray-template.png");
fn setup_tray(app: &AppHandle) -> tauri::Result<()> {
let persisted = settings::load(app);
let language = locale::preferred(Some(&persisted.language));
set_tray_language(language);
let menu = tray_menu(app, language)?;
// The system draws this one, not the app's colour wheel: a flat mark that
// macOS tints black or white for the menu bar, and that stays legible at the
// 16 px the Windows notification area allows. The coloured icon is unreadable
// at both sizes, which is what "use a black/white system icon" asked for.
let icon = tauri::image::Image::from_bytes(TRAY_ICON)?;
let mut tray = TrayIconBuilder::with_id("shift")
let tray = TrayIconBuilder::with_id("shift")
.menu(&menu)
.show_menu_on_left_click(true)
.tooltip("Shift")
.icon(icon)
// Template images are a macOS idea; Windows has no tinting to ask of.
.icon_as_template(cfg!(target_os = "macos"))
.on_menu_event(|app, event| match event.id().as_ref() {
"open" => show_main(app),
"capture" => start_capture_from_event(app, "screenshot"),
@ -251,9 +263,6 @@ fn setup_tray(app: &AppHandle) -> tauri::Result<()> {
"quit" => app.exit(0),
_ => {}
});
if let Some(icon) = app.default_window_icon() {
tray = tray.icon(icon.clone());
}
tray.build(app)?;
Ok(())
}