Assets, i18n & Debugging
REFERENCE

REFERENCE / DEEP DIVE

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="..." />:

TSX
<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 .deskpack size 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.):

TEXT
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:

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:

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:

TSX
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.json defaultLocale → "en" → raw key string. Missing _meta fields automatically fall back to name/nameZh, description/descriptionZh, and options[key].label/labelZh/labelEn in deskpack.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:
TSX
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 isPreview or useFetch(..., { 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:

TSX
<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, and console.error stream live to the Console & Diagnostics panel below the editor, alongside exact TSX syntax error line numbers, locales/*.json JSON/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.json and warns when a missing branch causes two sizes to render identical trees.
  • CLI validation: Run npx deskpack validate ./my-widget in 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: .deskpack is a lightweight gzipped JSON archive carrying readable deskpack.json, index.tsx, locales/, and assets/ — zero obfuscation, remixable anytime in Studio or Finder.
  • Zero-config public networking & read-only telemetry: Call public HTTP(S) APIs with useFetch and subscribe to clock, calendar, system, and battery with useHostData without 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.