|
| 1 | +import sharp from "sharp"; |
| 2 | +import { nativeImage, app, Tray, type Menu } from "electron"; |
| 3 | + |
| 4 | +// Stolen from the T3Wordmark component in the web app |
| 5 | +const T3_WORDMARK_VIEW_BOX = "15.5309 37 94.3941 56.96"; |
| 6 | +const T3_WORDMARK_PATH_STRING = |
| 7 | + "M33.4509 93V47.56H15.5309V37H64.3309V47.56H46.4109V93H33.4509ZM86.7253 93.96C82.832 93.96 78.9653 93.4533 75.1253 92.44C71.2853 91.3733 68.032 89.88 65.3653 87.96L70.4053 78.04C72.5386 79.5867 75.0186 80.8133 77.8453 81.72C80.672 82.6267 83.5253 83.08 86.4053 83.08C89.6586 83.08 92.2186 82.44 94.0853 81.16C95.952 79.88 96.8853 78.12 96.8853 75.88C96.8853 73.7467 96.0586 72.0667 94.4053 70.84C92.752 69.6133 90.0853 69 86.4053 69H80.4853V60.44L96.0853 42.76L97.5253 47.4H68.1653V37H107.365V45.4L91.8453 63.08L85.2853 59.32H89.0453C95.9253 59.32 101.125 60.8667 104.645 63.96C108.165 67.0533 109.925 71.0267 109.925 75.88C109.925 79.0267 109.099 81.9867 107.445 84.76C105.792 87.48 103.259 89.6933 99.8453 91.4C96.432 93.1067 92.0586 93.96 86.7253 93.96Z"; |
| 8 | + |
| 9 | +const T3_TRAY_IMAGE_OPTICAL_Y_OFFSET_1X = 1.6; // vertically centering the wordmark looks weird, so we offset it slightly |
| 10 | +const TRAY_SIZE_1X = 16; |
| 11 | + |
| 12 | +/** |
| 13 | + * Rasterizes an SVG to a square template image. |
| 14 | + * @see: https://developer.apple.com/documentation/appkit/nsimage/istemplate |
| 15 | + * @param viewBox The viewBox of the SVG to rasterize |
| 16 | + * @param path The path of the SVG to rasterize |
| 17 | + * @param size The size of the resulting image |
| 18 | + * @param opticalYOffset The optical Y offset of the resulting image |
| 19 | + * @returns The resulting image as a PNG buffer |
| 20 | + */ |
| 21 | +async function rasterizeSvgToSquareTemplateImage( |
| 22 | + viewBox: string, |
| 23 | + path: string, |
| 24 | + size: number, |
| 25 | + opticalYOffset: number, |
| 26 | +) { |
| 27 | + // Template images "should consist of only black and clear colors" (see above-linked documentation). |
| 28 | + const svg = `<svg viewBox="${viewBox}" xmlns="http://www.w3.org/2000/svg"><path d="${path}" fill="black" /></svg>`; |
| 29 | + return await sharp(Buffer.from(svg), { |
| 30 | + density: 2000, |
| 31 | + }) |
| 32 | + .resize({ |
| 33 | + width: size, |
| 34 | + height: size, |
| 35 | + fit: "contain", |
| 36 | + position: "centre", |
| 37 | + background: { r: 0, g: 0, b: 0, alpha: 0 }, |
| 38 | + }) |
| 39 | + .extend({ |
| 40 | + top: opticalYOffset, |
| 41 | + bottom: 0, |
| 42 | + left: 0, |
| 43 | + right: 0, |
| 44 | + background: { r: 0, g: 0, b: 0, alpha: 0 }, |
| 45 | + }) |
| 46 | + .extract({ left: 0, top: 0, width: size, height: size }) |
| 47 | + .png() |
| 48 | + .toBuffer(); |
| 49 | +} |
| 50 | + |
| 51 | +async function createTrayTemplateImage() { |
| 52 | + const rasterizeT3Wordmark = async (size: number) => { |
| 53 | + const opticalYOffset = Math.max(0, Math.round((T3_TRAY_IMAGE_OPTICAL_Y_OFFSET_1X * size) / TRAY_SIZE_1X)); |
| 54 | + return await rasterizeSvgToSquareTemplateImage( |
| 55 | + T3_WORDMARK_VIEW_BOX, |
| 56 | + T3_WORDMARK_PATH_STRING, |
| 57 | + size, |
| 58 | + opticalYOffset, |
| 59 | + ); |
| 60 | + }; |
| 61 | + const image = nativeImage.createEmpty(); |
| 62 | + const addRepresentation = async (scaleFactor: number, size: number) => { |
| 63 | + image.addRepresentation({ |
| 64 | + scaleFactor: scaleFactor, |
| 65 | + width: size, |
| 66 | + height: size, |
| 67 | + buffer: await rasterizeT3Wordmark(size), |
| 68 | + }); |
| 69 | + }; |
| 70 | + await addRepresentation(1, TRAY_SIZE_1X); |
| 71 | + await addRepresentation(2, TRAY_SIZE_1X * 2); |
| 72 | + image.setTemplateImage(true); |
| 73 | + return image; |
| 74 | +} |
| 75 | + |
| 76 | +async function createTray(contextMenu: Menu): Promise<Tray | null> { |
| 77 | + // macOS only (for now) |
| 78 | + if (process.platform !== "darwin") return null; |
| 79 | + |
| 80 | + const image = await createTrayTemplateImage(); |
| 81 | + const tray = new Tray(image); |
| 82 | + tray.setToolTip(app.getName()); |
| 83 | + tray.setContextMenu(contextMenu); |
| 84 | + return tray; |
| 85 | +} |
| 86 | + |
| 87 | +export { createTray }; |
0 commit comments