Assets, i18n, Debugging & Sandbox
Essential engineering reference before sharing a widget: bundled assets/, folder-based localization (locales/), preview mode polish, Reduce Motion animations, diagnostics, .deskpack limits, and the invisible security sandbox.
1. Local Image Assets (assets/)#
Place PNG, JPG, WebP, or SVG images inside your package's assets/ folder and reference them offline via <Image asset="..." />:
<Image asset="cover.png" width={56} height={56} cornerRadius={12} scaleMode="fill" clip={true} />
- Asset paths are validated and strictly confined to the package's own
assets/directory. - Keep individual images lean (ideally under 512 KiB); total compressed
.deskpacksize is capped at 8 MiB.
2. Localization (locales/ & t)#
Store translations cleanly in UTF-8 JSON files under a root locales/ folder (locales/<BCP-47>.json) and declare "defaultLocale": "en" in deskpack.json. This keeps your .tsx clean and scales to any number of languages (en, zh-Hans, zh-Hant, ja, ko, de, fr, pt-BR, etc.):
my-widget/
├── deskpack.json # "defaultLocale": "en"
├── index.tsx
└── locales/
├── en.json
├── zh-Hans.json
└── ja.json
Each locales/<tag>.json is a JSON object. An optional _meta block localizes the widget's display name, description, and Edit Widget options labels/choices; all other keys provide UI strings (supporting dot-nested keys and {var} named interpolation):
locales/en.json:
{
"_meta": {
"name": "Hydration",
"description": "Track daily water intake.",
"options": {
"goal": "Daily Goal",
"unit": { "label": "Unit", "choices": { "ml": "ml", "oz": "oz" } }
}
},
"title": "Hydration",
"status": {
"done": "Done for today"
},
"summary": "{done} of {total} completed"
}
locales/zh-Hans.json:
{
"_meta": {
"name": "每日饮水",
"description": "记录每日喝水杯数。",
"options": {
"goal": "每日目标",
"unit": { "label": "单位", "choices": { "ml": "毫升", "oz": "盎司" } }
}
},
"title": "每日饮水",
"status": {
"done": "今天完成啦"
},
"summary": "已完成 {done} / {total} 项"
}
In index.tsx, resolve keys and interpolate variables with t() from useWidget:
const { t, locale, isZh } = useWidget();
// 1. Key lookup (follows active locale & fallback chain)
const title = t("title");
// 2. Dot-nested key lookup
const doneText = t("status.done");
// 3. Named interpolation -> "3 of 5 completed" / "已完成 3 / 5 项"
const summary = t("summary", { done: 3, total: 5 });
- Fallback chain: Exact
locale→ progressively stripped subtags (e.g.zh-Hans-CN→zh-Hans→zh,pt-BR→pt) →deskpack.jsondefaultLocale→"en"→ raw key string. Missing_metafields automatically fall back toname/nameZh,description/descriptionZh, andoptions[key].label/labelZh/labelEnindeskpack.json. - Naming & limits: Filenames must use valid BCP-47 tags (
^[A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$, 2–35 characters, no case-insensitive duplicates). Packages may include up to 64 locale files totaling ≤ 1 MiB decoded. - Single-file shorthand (optional): For tiny single-file drafts without a
locales/folder,t()also accepts inline strings and positional{0}/{1}placeholders:
const { t } = useWidget();
const title = t("每日饮水", "Hydration");
const greeting = t("你好", { en: "Hello", ja: "こんにちは" });
const summary = t("已完成 {0} / {1} 项", "{0} of {1} completed", 3, 5);
3. Preview Mode (isPreview) & First-Frame Performance#
When rendered inside the Widget Library, import preview, or initial thumbnail, useWidget().isPreview is true:
- Timers, network requests, disk storage writes, and host actions are safely skipped.
- Always supply realistic sample values (either via
isPrevieworuseFetch(..., { fallback })) — never show a blank box or an endless spinner. - Aim for first render under 250 ms and keep total nodes well below 1000 for silky 120Hz ProMotion performance.
4. Animations & Reduce Motion#
Attach animation ("spring", "easeInOut", "easeIn", "easeOut", or "linear") and optional animationDuration (0.05–2 seconds) to any primitive so SwiftUI interpolates frame, color, scale, and rotation changes natively:
<Box
width={expanded ? 140 : 72}
height={32}
cornerRadius={16}
background="theme.accent"
animation="spring"
animationDuration={0.32}
/>
When macOS Reduce Motion is enabled, animations are suppressed automatically; you can also inspect useWidget().reduceMotion for custom gentle fallbacks.
5. Debugging & Validation#
- Widget Studio Console & Locale Preview:
console.log,console.warn, andconsole.errorstream live to the Console & Diagnostics panel below the editor, alongside exact TSX syntax error line numbers,locales/*.jsonJSON/missing-key diagnostics, and runtime stack traces. Switch preview languages from the Preview Environment popover in the preview header to verify layout and truncation per locale. - Automatic multi-size verification: Studio probes every size listed in
deskpack.jsonand warns when a missing branch causes two sizes to render identical trees. - CLI validation: Run
npx deskpack validate ./my-widgetin external IDEs to verify manifest schema, TSX transpilation,locales/files, and asset limits before packing.
6. .deskpack Packaging & Security Sandbox#
Deskset combines Rainmeter-style zero-friction editing with silent OS-level safety boundaries:
- Plain, editable source:
.deskpackis a lightweight gzipped JSON archive carrying readabledeskpack.json,index.tsx,locales/, andassets/— zero obfuscation, remixable anytime in Studio or Finder. - Zero-config public networking & read-only telemetry: Call public HTTP(S) APIs with
useFetchand subscribe toclock,calendar,system, andbatterywithuseHostDatawithout manifest declarations. - Silent host-enforced boundaries:
- Automatically blocks SSRF targets (
localhost,127.0.0.1, private LAN ranges,file://, URL credentials, and private-host redirects). - Personal sensitive services (Clipboard, Mail, Authenticator, Reminders, Notes, Find My) remain strictly isolated to built-in trusted widgets.
- No filesystem access, shell execution, or arbitrary binary launch APIs.
- Strict execution timeouts and response size caps prevent runaway scripts from stalling your desktop.
- Automatically blocks SSRF targets (