Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

add IconDraw like copilot suggested

Alan Trebugeais31469adf61cb
diff --git a/src/lib/extensions/extensionHost.ts b/src/lib/extensions/extensionHost.ts
index e43f26f8..1e030070 100644
--- a/src/lib/extensions/extensionHost.ts
+++ b/src/lib/extensions/extensionHost.ts
@@ -6,7 +6,7 @@
*/
import { createExtensionModuleUrl, resolveExtensionRelativeFileUrl } from "./fileUrls";
-import * as PhosphorIcons from "@phosphor-icons/react";
+import { resolveIconPath } from "./iconDraw";
import type {
ContributedCursorStyle,
ContributedFrame,
@@ -985,71 +985,7 @@ export class ExtensionHost {
color: string,
weight: "thin" | "light" | "regular" | "bold" | "fill" = "regular",
): void {
- const cacheKey = `${name}:${weight}`;
- let path = host.iconPathCache.get(cacheKey);
-
- if (!path) {
- const iconLibrary = PhosphorIcons as Record<string, unknown>;
- const resolvedIconName =
- typeof iconLibrary[name] !== "undefined"
- ? name
- : typeof iconLibrary[`${name}Icon`] !== "undefined"
- ? `${name}Icon`
- : null;
- const Icon = resolvedIconName
- ? (iconLibrary[resolvedIconName] as {
- render?: (props: { weight: string }, ref: unknown) => unknown;
- })
- : null;
- if (!Icon) {
- console.warn(`[extensions] Icon ${name} not found in Phosphor library`);
- return;
- }
-
- try {
- const element = Icon.render?.({ weight }, null) as
- | { props?: { weights?: Map<string, { props?: { children?: unknown } }> } }
- | undefined;
- const weights = element?.props?.weights;
- const definition = weights?.get(weight);
- const children = definition?.props?.children;
-
- let pathElement = children;
- if (Array.isArray(children)) {
- pathElement = children.find(
- (c: unknown) =>
- typeof c === "object" &&
- c !== null &&
- (("type" in c && (c as { type?: unknown }).type === "path") ||
- ("props" in c &&
- typeof (c as { props?: unknown }).props === "object" &&
- (c as { props?: { d?: unknown } }).props?.d)),
- );
- }
-
- const pathData =
- typeof pathElement === "object" &&
- pathElement !== null &&
- "props" in pathElement &&
- typeof (pathElement as { props?: unknown }).props === "object"
- ? (pathElement as { props?: { d?: string } }).props?.d
- : undefined;
-
- if (pathData) {
- path = new Path2D(pathData);
- host.iconPathCache.set(cacheKey, path);
- } else {
- console.warn(`[extensions] No path data found for ${name}:${weight}`, {
- iconName: resolvedIconName,
- element,
- children,
- });
- }
- } catch (err) {
- console.error(`[extensions] Failed to extract path for icon ${name}:`, err);
- return;
- }
- }
+ const path = resolveIconPath(name, weight, host.iconPathCache);
if (path) {
ctx.save();
diff --git a/src/lib/extensions/iconDraw.ts b/src/lib/extensions/iconDraw.ts
new file mode 100644
index 00000000..4634db74
--- /dev/null
+++ b/src/lib/extensions/iconDraw.ts
@@ -0,0 +1,92 @@
+import * as PhosphorIcons from "@phosphor-icons/react";
+
+type IconWeight = "thin" | "light" | "regular" | "bold" | "fill";
+
+function resolveIconComponent(name: string): {
+ iconName: string;
+ icon: { render?: (props: { weight: string }, ref: unknown) => unknown };
+} | null {
+ const iconLibrary = PhosphorIcons as Record<string, unknown>;
+ const iconName =
+ typeof iconLibrary[name] !== "undefined"
+ ? name
+ : typeof iconLibrary[`${name}Icon`] !== "undefined"
+ ? `${name}Icon`
+ : null;
+ if (!iconName) {
+ return null;
+ }
+
+ return {
+ iconName,
+ icon: iconLibrary[iconName] as {
+ render?: (props: { weight: string }, ref: unknown) => unknown;
+ },
+ };
+}
+
+function collectPathData(node: unknown, output: string[]): void {
+ if (!node) return;
+ if (Array.isArray(node)) {
+ for (const child of node) collectPathData(child, output);
+ return;
+ }
+ if (typeof node !== "object") return;
+
+ const maybeNode = node as {
+ type?: unknown;
+ props?: { d?: unknown; children?: unknown };
+ };
+ if (maybeNode.type === "path" && typeof maybeNode.props?.d === "string") {
+ output.push(maybeNode.props.d);
+ }
+ collectPathData(maybeNode.props?.children, output);
+}
+
+export function resolveIconPath(
+ name: string,
+ weight: IconWeight,
+ cache: Map<string, Path2D>,
+): Path2D | null {
+ const cacheKey = `${name}:${weight}`;
+ const cached = cache.get(cacheKey);
+ if (cached) {
+ return cached;
+ }
+
+ const resolved = resolveIconComponent(name);
+ if (!resolved) {
+ console.warn(`[extensions] Icon ${name} not found in Phosphor library`);
+ return null;
+ }
+
+ try {
+ const element = resolved.icon.render?.({ weight }, null) as
+ | { props?: { weights?: Map<string, { props?: { children?: unknown } }> } }
+ | undefined;
+ const weights = element?.props?.weights;
+ const definition = weights?.get(weight);
+ const children = definition?.props?.children;
+ const pathDataList: string[] = [];
+ collectPathData(children, pathDataList);
+
+ if (pathDataList.length === 0) {
+ console.warn(`[extensions] No path data found for ${name}:${weight}`, {
+ iconName: resolved.iconName,
+ element,
+ children,
+ });
+ return null;
+ }
+
+ const combinedPath = new Path2D();
+ for (const pathData of pathDataList) {
+ combinedPath.addPath(new Path2D(pathData));
+ }
+ cache.set(cacheKey, combinedPath);
+ return combinedPath;
+ } catch (err) {
+ console.error(`[extensions] Failed to extract path for icon ${name}:`, err);
+ return null;
+ }
+}