From 287b3af09d93a2c6a351f2de0de2791fd2f0302a Mon Sep 17 00:00:00 2001 From: "mula.liu" Date: Thu, 19 Nov 2026 17:45:07 +0800 Subject: [PATCH] fix(tray): let the system draw a black/white mark, not the colour wheel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- scripts/build-tray-icon.py | 90 ++++++++++++++++++++++++++++++ src-tauri/icons/tray-template.png | Bin 0 -> 4086 bytes src-tauri/src/lib.rs | 17 ++++-- 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100755 scripts/build-tray-icon.py create mode 100644 src-tauri/icons/tray-template.png diff --git a/scripts/build-tray-icon.py b/scripts/build-tray-icon.py new file mode 100755 index 0000000..07c0fea --- /dev/null +++ b/scripts/build-tray-icon.py @@ -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() diff --git a/src-tauri/icons/tray-template.png b/src-tauri/icons/tray-template.png new file mode 100644 index 0000000000000000000000000000000000000000..1586eec16487d5a8c3c3f6b0b97ea4161d5f9bdf GIT binary patch literal 4086 zcmV zdx%}t6~=#e?o1w|5@RG)p;QgTYBf@A5~CD_n8awH6be-fHc1;rskUlMTTrxEXbbgK zrDC-40ag2FL5(ppL_1T=guT;J~+&s z^VoZ>@9e$y+H0?UZ|%k#Z{#3)hraO^;C~$l3GOla$6J6Bs70Sg3eN49*q;clWsVU) zhI8(XN$tydW`zBO`0*yVMj&X2d{QO@lYrg89zQq9&+qZ`dFUs+pm8I6ya*Pl4L<+| zfiD4H1ZD%L15%?278ElkN>Y#aI(u$CQ+! zUjR-8769J{j`4f)Fqru~)sN%-+&BGi19%Vk6R;L|3HY~PQ;%sdMi?uCMXVG%6Icp- z1DKw$3mNhAb*vZs*pKbNpMm><=e)czBf8ganiOte1T4pt zd=^-NkvojpU)jn&?8VokqXx{4%AM=y;(PiD#K&b$_qv|-x|jPs4S%ku9Q3mAenP9A z53B?J0L&vO7!xYHoWelja~5iyG2yCfp3AV#Woo#LJ?qK6AXo|x0W*PBG%KVos+L(6 zBZ<#t%Q!^q7u*r5CO96gug1FMGD1sMckTqW#OHS9lgS!(*`9M*WuiKj zFBer3+>Z% zTVd5&4=1$1!!y@9g(f3In9N%+9fqign0Xz@CdDMrw1a%CFEB045r z1~DB^A-I_x6;u|AxlRNAh}o+~2j#R|=P+<67Fj#iKgaQkUO=&mshFYYN&|?P2s|x}J;auJ}pmhs!`McRFynpHnNU!aT&&RL!v705)Oq+`s#M zulnEl{&yA@Po9It6r@ialB_~Dm2)|8Cl&&gxhzCGEwB!J0GtKf3M}^X-OhUA=U>L+ zwyS~HNylsdlKET5CLfEjIP=LpkZm;a{H>~Mo15^Bb;3BM*Nq}~{rtECL zm$MxB8gMi43SrcaW5cq4^~|+~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/src/lib.rs b/src-tauri/src/lib.rs index 8c93818..fe7b5eb 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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(()) }