дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
Flutter projects get a Flutter Widget Preview tool window that shows the project's @Preview widgets and updates them as files are saved. The first time the window opens, it runs flutter widget-preview start --machine --web-server in the project root, passing the project's DevTools server for the preview inspector. It then embeds the page reported by widget_preview.started on 127.0.0.1, in the IDE's light or dark theme. The server stops when the project closes or loses trust, and it is killed with the app. Refresh reloads the page, or restarts a server that exited or failed. An older flutter without widget-preview reports that Flutter 3.38 or later is needed. Remote windows say it is not available yet. DevTools and Widget Preview now share one ToolServer, which starts, waits for and stops these processes. Both take the configured Flutter SDK (dart / flutter) before the search path. Related to #4096. Co-authored-by: wildcatDownstairs <[email protected]>
191 рядки
6.4 KiB
TypeScript
191 рядки
6.4 KiB
TypeScript
/**
|
|
* Flutter DevTools as tool windows, one per Android Studio window: the same pages, with the same
|
|
* parameters, of the project's DevTools server.
|
|
*/
|
|
import { create } from "zustand";
|
|
import { setContext } from "@/platform/context";
|
|
import { call, commands, onIdeEvent } from "@/platform/ipc";
|
|
import { useProject } from "@/platform/project";
|
|
import { lazyComponent } from "@/ui";
|
|
import { registerToolWindow } from "@/workbench/contributions";
|
|
import { trackPropertyEditorCaret } from "./propertyEditor";
|
|
|
|
export interface DevToolsWindow {
|
|
id: string;
|
|
title: string;
|
|
icon: string;
|
|
/** Path of the page; empty for DevTools' own navigation. */
|
|
page: string;
|
|
/** Screens left out of that navigation. */
|
|
hide?: string;
|
|
/** Which app the page connects to: one page per app, the latest app, or none. */
|
|
app: "each" | "latest" | "none";
|
|
}
|
|
|
|
// In Android Studio's stripe order.
|
|
export const DEVTOOLS_WINDOWS: readonly DevToolsWindow[] = [
|
|
{
|
|
id: "flutter-deep-links",
|
|
title: "Flutter Deep Links",
|
|
icon: "toolwindow-flutter-deep-links",
|
|
page: "deep-links",
|
|
app: "none",
|
|
},
|
|
{
|
|
id: "flutter-devtools",
|
|
title: "Flutter DevTools",
|
|
icon: "toolwindow-flutter-devtools",
|
|
page: "",
|
|
hide: "home,inspector,deep-links,extensions,debugger",
|
|
app: "latest",
|
|
},
|
|
{
|
|
id: "flutter-devtools-extensions",
|
|
title: "Flutter DevTools Extensions",
|
|
icon: "toolwindow-flutter-extensions",
|
|
page: "",
|
|
hide: "all-except-extensions",
|
|
app: "latest",
|
|
},
|
|
{
|
|
id: "flutter-inspector",
|
|
title: "Flutter Inspector",
|
|
icon: "toolwindow-flutter-inspector",
|
|
page: "inspector",
|
|
app: "each",
|
|
},
|
|
];
|
|
|
|
export interface DevToolsLook {
|
|
theme: "light" | "dark";
|
|
/** Hex digits, without `#`. */
|
|
background?: string;
|
|
}
|
|
|
|
/** The IDE's look, for a page about to load. Like Android Studio, a loaded page keeps its own. */
|
|
export function currentDevToolsLook(root = document.documentElement): DevToolsLook {
|
|
const background = /^#([0-9a-f]{6})$/i.exec(getComputedStyle(root).getPropertyValue("--bg-editor").trim())?.[1];
|
|
return { theme: root.dataset.theme === "light" ? "light" : "dark", ...(background ? { background } : {}) };
|
|
}
|
|
|
|
/** A DevTools page; with `embedded`, the form for a tool window. */
|
|
export function devToolsUrl(
|
|
server: string,
|
|
window: Pick<DevToolsWindow, "page" | "hide">,
|
|
{ uri, embedded }: { uri?: string; embedded?: DevToolsLook },
|
|
): string {
|
|
const query = new URLSearchParams({ ide: "OpenIDEA" });
|
|
if (embedded) {
|
|
query.set("embedMode", window.hide ? "many" : "one");
|
|
if (window.hide) query.set("hide", window.hide);
|
|
query.set("theme", embedded.theme);
|
|
if (embedded.background) query.set("backgroundColor", embedded.background);
|
|
}
|
|
if (uri) query.set("uri", uri);
|
|
return `${server}/${window.page}?${query}`;
|
|
}
|
|
|
|
export const WIDGET_PREVIEW_WINDOW = "flutter-widget-preview";
|
|
|
|
/** The Widget Preview page in the IDE's look; like DevTools, a loaded page keeps its own. */
|
|
export function widgetPreviewUrl(server: string, look: DevToolsLook): string {
|
|
return `${server}/?${new URLSearchParams({ theme: look.theme })}`;
|
|
}
|
|
|
|
/** Bumped by Refresh, per window: the page loads again, on a server started anew if it is gone. */
|
|
export const useDevToolsRefresh = create<Record<string, number>>(() => ({}));
|
|
|
|
export function refreshDevTools(id: string) {
|
|
useDevToolsRefresh.setState((s) => ({ [id]: (s[id] ?? 0) + 1 }));
|
|
}
|
|
|
|
const view = (
|
|
name:
|
|
| "FlutterDevToolsView"
|
|
| "FlutterDevToolsTabs"
|
|
| "FlutterDevToolsRefresh"
|
|
| "FlutterWidgetPreviewView"
|
|
| "FlutterWidgetPreviewRefresh",
|
|
) => lazyComponent(() => import("../ui/FlutterDevTools").then((m) => m[name]));
|
|
|
|
/** A Flutter project's `pubspec.yaml`, by the rule the run and debug backends use. */
|
|
export const isFlutterPubspec = (text: string) => text.includes("sdk: flutter");
|
|
|
|
/**
|
|
* Keeps the tool windows, the Property Editor's and Widget Preview's too, registered while the
|
|
* project's root `pubspec.yaml` depends on Flutter.
|
|
*/
|
|
export function registerDevToolsWindows(): () => void {
|
|
let dispose: (() => void)[] | null = null;
|
|
const sync = (flutter: boolean) => {
|
|
setContext("flutterDevTools", flutter);
|
|
if (!flutter) {
|
|
dispose?.forEach((d) => d());
|
|
dispose = null;
|
|
return;
|
|
}
|
|
dispose ??= [
|
|
...DEVTOOLS_WINDOWS.map((w, i) =>
|
|
registerToolWindow({
|
|
id: w.id,
|
|
title: w.title,
|
|
icon: w.icon,
|
|
anchor: "right",
|
|
order: 40 + i,
|
|
// Only the Inspector is on the stripe until the others are first opened.
|
|
...(w.id === "flutter-inspector" ? {} : { stripe: "onUse" as const }),
|
|
component: view("FlutterDevToolsView"),
|
|
headerActions: view("FlutterDevToolsRefresh"),
|
|
...(w.app === "each" ? { headerContent: view("FlutterDevToolsTabs") } : {}),
|
|
}),
|
|
),
|
|
registerToolWindow({
|
|
id: "flutter-property-editor",
|
|
title: "Flutter Property Editor",
|
|
icon: "toolwindow-flutter-property-editor",
|
|
anchor: "right",
|
|
order: 40 + DEVTOOLS_WINDOWS.length,
|
|
stripe: "onUse",
|
|
component: lazyComponent(() => import("../ui/FlutterPropertyEditor").then((m) => m.FlutterPropertyEditorView)),
|
|
}),
|
|
// Its server starts when the window first opens.
|
|
registerToolWindow({
|
|
id: WIDGET_PREVIEW_WINDOW,
|
|
title: "Flutter Widget Preview",
|
|
icon: "toolwindow-flutter-widget-preview",
|
|
anchor: "right",
|
|
order: 41 + DEVTOOLS_WINDOWS.length,
|
|
stripe: "onUse",
|
|
component: view("FlutterWidgetPreviewView"),
|
|
headerActions: view("FlutterWidgetPreviewRefresh"),
|
|
}),
|
|
trackPropertyEditorCaret(),
|
|
];
|
|
};
|
|
let pubspec: string | null = null;
|
|
let check = 0;
|
|
const load = () => {
|
|
const root = useProject.getState().project?.root;
|
|
const seq = ++check;
|
|
pubspec = root ? `${root}/pubspec.yaml` : null;
|
|
if (!pubspec) return sync(false);
|
|
call(commands.fsReadText(pubspec)).then(
|
|
(text) => seq === check && sync(isFlutterPubspec(text)),
|
|
() => seq === check && sync(false),
|
|
);
|
|
};
|
|
load();
|
|
const offProject = useProject.subscribe((s, prev) => {
|
|
if (s.project?.root !== prev.project?.root) load();
|
|
});
|
|
const offFs = onIdeEvent("fsChanged", (e) => {
|
|
if (pubspec && [...e.created, ...e.modified, ...e.removed].includes(pubspec)) load();
|
|
});
|
|
return () => {
|
|
offProject();
|
|
offFs();
|
|
check++;
|
|
sync(false);
|
|
};
|
|
}
|