fix(web): add install icon for PWA (#8248)

* fix(web): round PWA install icons

Closes #8247

* fix(web): harden install icon generation

Reject missing SVG definitions and bind clipping to the tile.
Cover rounded geometry and keep the existing icon notes current.
This commit is contained in:
EricMoin authored and GitHub committed 2026-11-19 21:13:37 +08:00
1 parent 101f26de4e
commit 608960ebfc
8 files changed
+110 -32

No files matched your search

@@ -9,11 +9,11 @@
en: 726b732fcaf49153
zh: b04aad2d0a65a42d
/agent-note-web-install-icons-and-launch-display/decision:
en: 65c6eb6a3ad5ab30
zh: 46ca48685f38269c
en: 0fe629065ee094ea
zh: 1b2fd7f87d08fb88
/agent-note-web-install-icons-and-launch-display/alternatives-considered:
en: 588d709bd7c8c95b
zh: 871b618b86247926
/agent-note-web-install-icons-and-launch-display/consequences:
en: 79695897b5237603
zh: b61304c9527e8dfc
en: 5d71d8d265f8dadf
zh: 9c3435f30caadd1e
@@ -10,7 +10,7 @@ A launcher masks the icon it puts on the home screen and chooses that icon itsel
## Decision
`apps/web/public/` carries four install icons: a 192px and a 512px `any` raster, a 512px `maskable` raster, and a 180px `apple-touch-icon`. [`render-app-icons.ts`](../../../../apps/web/scripts/render-app-icons.ts) renders all four from the official application tile in `apps/desktop/resources/icon.svg`, and `pnpm --filter @deepseek-ai/dsh-web-frontend run render:app-icons` regenerates the committed files. Each variant preserves the whale's geometry, gradients, opacity, and filters. Rendering first produces a bitmap at no less than the 1024px source extent and four times the output edge, then reduces it with Lanczos3. This separate rasterization retains the original shading while smoothing the small icon's curves. Every variant crops the tile's desktop drop shadow. The maskable variant scales the whale about the tile centre by `MASKABLE_ARTWORK_SCALE` while the tile's gradient keeps its own extent, so the border a mask may crop is still the real tile rather than a flat colour meeting it at a seam.
`apps/web/public/` carries four install icons: a 192px and a 512px `any` raster, a 512px `maskable` raster, and a 180px `apple-touch-icon`. [`render-app-icons.ts`](../../../../apps/web/scripts/render-app-icons.ts) renders all four from the official application tile in `apps/desktop/resources/icon.svg`, and `pnpm --filter @deepseek-ai/dsh-web-frontend run render:app-icons` regenerates the committed files. Each variant preserves the whale's geometry, gradients, opacity, and filters. Rendering first produces a bitmap at no less than the 1024px source extent and four times the output edge, then reduces it with Lanczos3. This separate rasterization retains the original shading while smoothing the small icon's curves. Every variant crops the tile's desktop drop shadow. The two `any` variants have transparent rounded corners with a radius of `230/1024` of the tile edge, matching `apps/desktop/resources/icon-macos.svg`, and leave the whale unscaled. The Apple touch and maskable variants retain opaque square backgrounds for platform masking. The maskable variant scales the whale about the tile centre by `MASKABLE_ARTWORK_SCALE` while the tile's gradient keeps its own extent, so the border a mask may crop is still the real tile rather than a flat colour meeting it at a seam.
[`pwa-manifest.e2e.ts`](../../../../apps/web/tests/pwa-manifest.e2e.ts) uses the browser image decoder to measure the committed maskable whale’s ink radius and rejects artwork outside the platform safe zone or smaller than the intended scale. The unscaled tile exceeds that zone and serves as the negative control.
@@ -32,7 +32,7 @@ This supersedes the icon entry and the display mode in the [archived Web install
## Consequences
The repository carries four committed rasters, all rendered by one script from `apps/desktop/resources/icon.svg`. Changing the desktop tile means rerunning that script and reviewing the PNG diff. Unit tests inspect committed PNG dimensions without loading the native renderer. Browser tests execute the generator and decode its output and all four built PNGs, checking declared edges, opaque corners, and artwork extents against the source tile with a two-pixel rasterization tolerance. Maskable checks bound its artwork above and below, with the unscaled tile as the safe-zone negative control. Tests also compare fractional coverage along the Apple icon’s curves against direct small-size SVG rendering, which retains the source’s hard pixel edges.
The repository carries four committed rasters, all rendered by one script from `apps/desktop/resources/icon.svg`. Changing the desktop tile means rerunning that script and reviewing the PNG diff. Unit tests check committed PNG dimensions, the rounded SVG's clip rectangle, its radius against the macOS artwork, and rejection of sources missing required SVG elements, without loading the native renderer. Browser tests execute the generator and decode its output and all four built PNGs, checking declared edges, transparent corners on the `any` icons, opaque corners on the Apple touch and maskable icons, and artwork extents against the source tile with a two-pixel rasterization tolerance. Maskable checks bound its artwork above and below, with the unscaled tile as the safe-zone negative control. Tests also compare fractional coverage along the Apple icon’s curves against direct small-size SVG rendering, which retains the source’s hard pixel edges.
The manifest's `theme_color` is one value and cannot follow the in-application theme, so a user whose theme differs from the system scheme sees that value until `ThemePresenter` writes the synchronized node shortly after boot. The presenter stays the only owner of `meta[name="theme-color"]`: a second static node wins document order and desynchronizes surrounding browser chrome from the rendered palette, which `settings-chrome.e2e.ts` pins.
@@ -10,7 +10,7 @@ Status: implemented
## Decision
`apps/web/public/` 承载四个安装图标:192px 与 512px 的 `any` 位图、512px 的 `maskable` 位图,以及 180px 的 `apple-touch-icon`。[`render-app-icons.ts`](../../../../apps/web/scripts/render-app-icons.ts) 从 `apps/desktop/resources/icon.svg` 的官方应用底板渲染这四个文件,`pnpm --filter @deepseek-ai/dsh-web-frontend run render:app-icons` 重新生成已提交的文件。每个变体都保留鲸鱼的几何路径、渐变、透明度和滤镜。渲染先生成不小于源图 1024px、且边长至少为输出尺寸四倍的位图,再以 Lanczos3 下采样。分开的栅格化步骤在平滑小图标曲线的同时保留原有明暗细节。每个变体都会裁掉底板的桌面投影。maskable 变体围绕底板中心按 `MASKABLE_ARTWORK_SCALE` 缩放鲸鱼,而底板渐变保持自身范围,因此可能被蒙层裁掉的那圈边框仍是真实底板,而不是与其相接的纯色。
`apps/web/public/` 承载四个安装图标:192px 与 512px 的 `any` 位图、512px 的 `maskable` 位图,以及 180px 的 `apple-touch-icon`。[`render-app-icons.ts`](../../../../apps/web/scripts/render-app-icons.ts) 从 `apps/desktop/resources/icon.svg` 的官方应用底板渲染这四个文件,`pnpm --filter @deepseek-ai/dsh-web-frontend run render:app-icons` 重新生成已提交的文件。每个变体都保留鲸鱼的几何路径、渐变、透明度和滤镜。渲染先生成不小于源图 1024px、且边长至少为输出尺寸四倍的位图,再以 Lanczos3 下采样。分开的栅格化步骤在平滑小图标曲线的同时保留原有明暗细节。每个变体都会裁掉底板的桌面投影。两个 `any` 变体采用透明圆角,圆角半径为底板边长的 `230/1024`,与 `apps/desktop/resources/icon-macos.svg` 一致,鲸鱼保持原尺寸。Apple touch 和 maskable 变体保留不透明的方形背景,供平台裁切。maskable 变体围绕底板中心按 `MASKABLE_ARTWORK_SCALE` 缩放鲸鱼,而底板渐变保持自身范围,因此可能被蒙层裁掉的那圈边框仍是真实底板,而不是与其相接的纯色。
[`pwa-manifest.e2e.ts`](../../../../apps/web/tests/pwa-manifest.e2e.ts) 使用浏览器图像解码器测量已提交 maskable 图标的鲸鱼墨迹半径,拒绝越出平台安全区或小于预期比例的图案。未缩放图块超出该安全区,用作反向对照。
@@ -32,7 +32,7 @@ manifest 请求 `display: "standalone"`,把 `theme_color` 与 `background_colo
## Consequences
仓库携带四张已提交的栅格图,均由同一个脚本从 `apps/desktop/resources/icon.svg` 渲染。桌面图块变化时,需要重新运行该脚本并审查 PNG diff。单元测试直接检查已提交 PNG 的尺寸,不加载原生渲染器。浏览器测试执行生成器,并解码生成结果和全部四张构建后的 PNG,以两个像素的栅格化容差检查声明边长、不透明角落及相对于源图块的图案范围。maskable 检查同时限制图案范围的上界和下界,并以未缩放图块作为安全区反向对照。测试也将 Apple 图标曲线边缘的部分像素覆盖与直接小尺寸渲染的 SVG 对比;后者保留源图的硬像素边缘。
仓库携带四张已提交的栅格图,均由同一个脚本从 `apps/desktop/resources/icon.svg` 渲染。桌面图块变化时,需要重新运行该脚本并审查 PNG diff。单元测试检查已提交 PNG 的尺寸、圆角 SVG 的裁切矩形、与 macOS 素材一致的圆角半径,以及源图缺少所需 SVG 元素时的拒绝行为,不加载原生渲染器。浏览器测试执行生成器,并解码生成结果和全部四张构建后的 PNG,检查声明边长、`any` 图标的透明圆角、Apple touch 与 maskable 图标的不透明角落,以及相对于源图块的图案范围;图案范围允许两个像素的栅格化容差。maskable 检查同时限制图案范围的上界和下界,并以未缩放图块作为安全区反向对照。测试也将 Apple 图标曲线边缘的部分像素覆盖与直接小尺寸渲染的 SVG 对比;后者保留源图的硬像素边缘。
manifest 的 `theme_color` 是单个值,无法跟随应用内主题;因此当用户选择的主题与系统方案不同时,该值会一直生效,直到 `ThemePresenter` 在启动后不久写入同步节点。该 presenter 仍是 `meta[name="theme-color"]` 的唯一所有者:第二个静态节点会在文档顺序上胜出,使周边浏览器界面与已渲染的配色脱节,这一点由 `settings-chrome.e2e.ts` 固定。
Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 84 KiB

+39 -20
View File
@@ -2,17 +2,15 @@
* Render the install icons of the Web application from the official
* application tile.
*
* A browser installs a Web App only with a raster 192px and a 512px icon, masks
* the icon it puts on the home screen, and has no manifest support for iOS,
* which reads `apple-touch-icon` instead. The whale favicon is the bare mark
* drawn edge to edge: handed to a launcher directly it loses its fins to the
* mask, and it carries neither the tile nor a background of its own. This
* script rasterizes the official application tile (`apps/desktop/resources/icon.svg`,
* the artwork the desktop application ships) into the committed PNGs under
* `public/`. Every variant crops the tile's drop shadow, which belongs to a
* desktop window rather than a launcher; the maskable variant additionally
* scales the whale inside the safe zone a platform mask may crop, leaving the
* tile's own gradient to fill the icon so no mask meets a seam.
* This script rasterizes the official application tile
* (`apps/desktop/resources/icon.svg`) into the committed PNGs under `public/`.
* The bare favicon has no background, and its edge-to-edge fins can be cropped
* by a launcher mask. The tile's drop shadow belongs to a desktop window, so
* every install variant crops it. The generic 192px and 512px icons have
* transparent rounded corners matching the macOS tile. Safari prefers the
* linked Apple touch icon, which stays square for system masking. The maskable
* variant scales the whale inside the platform's safe zone while retaining
* the tile's own gradient to the edges, so no platform mask meets a seam.
*
* The committed PNGs are the output; rerun
* `pnpm --filter @deepseek-ai/dsh-web-frontend run render:app-icons` after the
@@ -34,6 +32,9 @@ export const ICON_OUTPUT_DIRECTORY = fileURLToPath(new URL('../public/', import.
/** Tile edge in source units; every render density is expressed against it. */
const TILE_EDGE = 1024
/** Corner radius in source units, matching `apps/desktop/resources/icon-macos.svg`. */
const TILE_CORNER_RADIUS = 230
/** Tile inset in the source canvas: the drop shadow occupies the surrounding band. */
const TILE_INSET = { x: 40, y: 32 } as const
@@ -60,6 +61,9 @@ export const MASKABLE_SAFE_RADIUS_RATIO = 0.4
/** Root element of the source document, rewritten to crop the canvas to the tile. */
const SOURCE_ROOT = /^<svg[^>]*>/u
/** Source definitions element, including attributes preserved during clip insertion. */
const SOURCE_DEFINITIONS = /<defs(?:\s[^>]*)?>/u
/** The tile group's drop shadow; stripped because a launcher icon has no window around it. */
const TILE_SHADOW_GROUP = '<g filter="url(#filter0_d_1637_371)">'
@@ -72,18 +76,18 @@ export interface AppIconVariant {
readonly file: string
/** Square edge in pixels. */
readonly size: number
/** Which rendering the platform receives. */
readonly kind: 'tile' | 'maskable'
/** Target rendering; `tile` is an opaque square for system masking. */
readonly kind: 'rounded' | 'tile' | 'maskable'
}
/**
* Install icons the Web application ships. Chrome requires the 192px and 512px
* raster pair to offer installation, prefers a maskable icon for the home
* screen, and iOS reads the touch icon alone.
* raster pair to offer installation. Platforms that apply their own masks
* receive a separate opaque maskable icon; Safari prefers the linked Apple touch icon.
*/
export const APP_ICON_VARIANTS: readonly AppIconVariant[] = [
{ file: 'icon-192.png', size: 192, kind: 'tile' },
{ file: 'icon-512.png', size: 512, kind: 'tile' },
{ file: 'icon-192.png', size: 192, kind: 'rounded' },
{ file: 'icon-512.png', size: 512, kind: 'rounded' },
{ file: 'icon-maskable-512.png', size: 512, kind: 'maskable' },
{ file: 'apple-touch-icon.png', size: 180, kind: 'tile' },
]
@@ -102,13 +106,14 @@ function aboutCenter(scale: number): string {
}
/** Replace the source root so the icon canvas is exactly the tile. */
function reparent(source: string): string {
function reparent(source: string, tileGroup = '<g>'): string {
const trimmed = source.trim()
const root = SOURCE_ROOT.exec(trimmed)
if (root === null) throw new Error('app icons: the tile source has no root element')
if (!trimmed.includes(TILE_SHADOW_GROUP)) throw new Error('app icons: the tile source has no shadowed tile group')
if (!trimmed.includes(TILE_ARTWORK_GROUP)) throw new Error('app icons: the tile source has no artwork group')
return `${croppedRoot()}${trimmed.slice(root[0].length).replace(TILE_SHADOW_GROUP, '<g>')}`
if (!SOURCE_DEFINITIONS.test(trimmed)) throw new Error('app icons: the tile source has no definitions element')
return `${croppedRoot()}${trimmed.slice(root[0].length).replace(TILE_SHADOW_GROUP, tileGroup)}`
}
/**
@@ -120,6 +125,19 @@ export function tileDocument(source: string): string {
return reparent(source)
}
/**
* Round the generic install tile for launchers that display it without a mask.
* @param source - complete `icon.svg` document.
* @returns an SVG document with transparent rounded corners and unscaled artwork.
*/
export function roundedDocument(source: string): string {
return reparent(source, '<g clip-path="url(#install-tile-clip)">')
.replace(SOURCE_DEFINITIONS, '$&<clipPath id="install-tile-clip"><rect'
+ ` x="${String(TILE_INSET.x)}" y="${String(TILE_INSET.y)}"`
+ ` width="${String(TILE_EDGE)}" height="${String(TILE_EDGE)}"`
+ ` rx="${String(TILE_CORNER_RADIUS)}"/></clipPath>`)
}
/**
* Rewrite the tile source into a maskable icon. The tile keeps its own gradient
* and extent, so the icon's border is the real tile rather than an extension;
@@ -137,7 +155,7 @@ export function maskableDocument(source: string): string {
/**
* Rasterize one rewritten document at a square edge.
* @param document - output of {@link tileDocument} or {@link maskableDocument}.
* @param document - output of {@link tileDocument}, {@link roundedDocument} or {@link maskableDocument}.
* @param size - square edge in pixels.
* @returns the PNG bytes.
*/
@@ -157,6 +175,7 @@ async function renderPng(document: string, size: number): Promise<Buffer> {
function documentForVariant(source: string, kind: AppIconVariant['kind']): string {
switch (kind) {
case 'rounded': return roundedDocument(source)
case 'tile': return tileDocument(source)
case 'maskable': return maskableDocument(source)
default: return assertNever(kind)
+41 -1
View File
@@ -4,7 +4,7 @@ import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import {
APP_ICON_VARIANTS, ICON_OUTPUT_DIRECTORY, MASKABLE_ARTWORK_SCALE, TILE_SOURCE,
maskableDocument, tileDocument,
maskableDocument, roundedDocument, tileDocument,
} from '../scripts/render-app-icons.ts'
import { pngSize } from './png-size.ts'
@@ -29,6 +29,46 @@ describe('web install icons', () => {
expect(maskable).toContain(`transform="translate(552 544) scale(${MASKABLE_ARTWORK_SCALE}) translate(-552 -544)"`)
})
it('rounds the full tile with the macOS corner geometry and unscaled artwork', async () => {
const source = await readFile(TILE_SOURCE, 'utf8')
const rounded = roundedDocument(source)
const macos = await readFile(new URL('../../desktop/resources/icon-macos.svg', import.meta.url), 'utf8')
expect(rounded).toContain('viewBox="40 32 1024 1024"')
expect(rounded).toContain('<g clip-path="url(#install-tile-clip)">')
expect(rounded).toContain('<clipPath id="install-tile-clip"><rect x="40" y="32" width="1024" height="1024" rx="230"/></clipPath>')
expect(rounded).not.toContain('<g filter="url(#filter0_d_1637_371)">')
expect(rounded).not.toContain(`scale(${MASKABLE_ARTWORK_SCALE})`)
expect(rounded).toContain(source.match(/<path\b[^>]*>/u)![0])
expect(rounded).toContain('<g filter="url(#filter1_ddi_1637_371)">')
const corner = rounded.match(/<clipPath\b[^>]*><rect\b[^>]*\brx="([^"]+)"/u)![1]
const tileRects = [...macos.matchAll(/<rect\b[^>]*\bwidth="1024"[^>]*\bheight="1024"[^>]*>/gu)]
expect(tileRects).toHaveLength(3)
expect(tileRects.map(rect => rect[0].match(/\brx="([^"]+)"/u)?.[1]))
.toEqual([corner, corner, corner])
})
it('clips the tile when another group precedes it', async () => {
const source = await readFile(TILE_SOURCE, 'utf8')
const precedingGroup = '<g><rect width="1" height="1"/></g>'
const rounded = roundedDocument(source.replace(
'<g filter="url(#filter0_d_1637_371)">',
`${precedingGroup}<g filter="url(#filter0_d_1637_371)">`,
))
expect(rounded).toContain(`${precedingGroup}<g clip-path="url(#install-tile-clip)">`)
})
it('preserves attributes on the source definitions', async () => {
const source = await readFile(TILE_SOURCE, 'utf8')
expect(roundedDocument(source.replace('<defs>', '<defs id="tile-definitions">')))
.toContain('<defs id="tile-definitions"><clipPath id="install-tile-clip">')
})
it('rejects a source without the definitions needed by the rounded tile', async () => {
const source = await readFile(TILE_SOURCE, 'utf8')
expect(() => roundedDocument(source.replace(/<defs>[\s\S]*<\/defs>/u, '')))
.toThrow(/no definitions element/)
})
it('rejects non-PNG bytes and incomplete image headers', () => {
expect(() => pngSize(Buffer.from('<svg/>'))).toThrow(/PNG IHDR/)
expect(() => pngSize(Buffer.alloc(33))).toThrow(/PNG IHDR/)
+22 -3
View File
@@ -25,14 +25,29 @@ async function pixels(page: Page, url: string, size: number) {
for (let y = 0; y < size; y += 1) {
for (let x = 0; x < size; x += 1) {
const at = (y * size + x) * 4
if (data[at + 3] === 0 || data[at]! + data[at + 1]! + data[at + 2]! >= 384) continue
// Measure contrast over white so transparent corners cannot count as whale ink.
const opacity = data[at + 3]! / 255
const brightness = (data[at]! + data[at + 1]! + data[at + 2]!) * opacity + 765 * (1 - opacity)
if (brightness >= 384) continue
ink += 1
radius = Math.max(radius, Math.hypot(x - (size - 1) / 2, y - (size - 1) / 2))
}
}
const alpha = (x: number, y: number): number => data[(y * size + x) * 4 + 3]!
const cornerSamples = (points: readonly (readonly [number, number])[]): number[] => points.flatMap(([x, y]) => {
const atX = Math.round(x * (size - 1))
const atY = Math.round(y * (size - 1))
return [alpha(atX, atY), alpha(size - 1 - atX, atY),
alpha(atX, size - 1 - atY), alpha(size - 1 - atX, size - 1 - atY)]
})
return {
width: image.naturalWidth, height: image.naturalHeight, radius, ink,
corners: [0, size - 1, (size - 1) * size, size * size - 1].map(index => data[index * 4 + 3]),
// These points straddle the 230/1024-radius curve at diagonal and shallow angles.
outsideCurve: cornerSamples([[0.05, 0.05], [0.1, 0.01]]),
insideCurve: cornerSamples([[0.08, 0.08], [0.2, 0.03]]),
edgeCenters: [alpha(size / 2, 0), alpha(size - 1, size / 2),
alpha(size / 2, size - 1), alpha(0, size / 2)],
}
}, { url, size })
}
@@ -79,7 +94,7 @@ it('ships install metadata with the built web application', async () => {
})
})
it('decodes every generated and shipped icon with its full artwork and opaque corners', async () => {
it('decodes every generated and shipped icon with full artwork and its intended corner treatment', async () => {
const browser = await chromium.launch({ headless: true })
try {
const page = await browser.newPage()
@@ -95,10 +110,14 @@ it('decodes every generated and shipped icon with its full artwork and opaque co
const reference = await pixels(page, svgUrl(tileDocument(source)), variant.size)
expect(reference.ink).toBeGreaterThan(0)
const scale = variant.kind === 'maskable' ? MASKABLE_ARTWORK_SCALE : 1
const cornerAlpha = variant.kind === 'rounded' ? 0 : 255
for (const bytes of [png, await readFile(join(DIST_ROOT, variant.file))]) {
const image = await pixels(page, pngUrl(bytes), variant.size)
expect([image.width, image.height], variant.file).toEqual([variant.size, variant.size])
expect(image.corners, variant.file).toEqual([255, 255, 255, 255])
expect(image.corners, variant.file).toEqual(Array.from({ length: 4 }, () => cornerAlpha))
expect(image.outsideCurve, variant.file).toEqual(Array.from({ length: 8 }, () => cornerAlpha))
expect(image.insideCurve, variant.file).toEqual(Array.from({ length: 8 }, () => 255))
expect(image.edgeCenters, variant.file).toEqual([255, 255, 255, 255])
expect(image.ink, variant.file).toBeGreaterThan(0)
// Two pixels allow rasterizer and downsampling differences without
// accepting missing or undersized artwork in the published PNG.