Файли
openidea/src/workbench/iosSimulators.ts
T
LU XIANGRUIтаwildcatDownstairs 2a7975a077 fix(xcode): select the root scheme and the backend's default simulator in Xcode projects (#7366)
An Xcode project with an npm package in a subfolder opened with that package's dev script selected, and the simulator chooser showed an iPad when nothing was booted.

- defaultSelection scored a root Xcode scheme the same as any run/dev script, so list order decided. A root scheme (kind xcode, no cwd) now scores like a Spring Boot application; schemes in subfolders and other project types keep their order, and a saved selection still wins.
- currentIosSimulator fell back to the first listed simulator, while the backend's pick_simulator takes a booted simulator, then the newest iPhone. The toolbar now applies the same rule, and pick_simulator ranks over simulator::parse_list so both sides agree on ties.

Fixes #7354

Co-authored-by: wildcatDownstairs <[email protected]>
2026-12-02 12:35:54 +08:00

129 рядки
4.4 KiB
TypeScript

/**
* The iOS Simulators of an Xcode project's window and which one launches use. The Run feature
* shows and changes the selection; Xcode run configurations read it through `iosSimulatorFor`.
*/
import { create } from "zustand";
import { tx } from "@/platform/i18n";
import { call, commands, type IosSimulator, type RunConfig } from "@/platform/ipc";
import { notifications } from "./contributions";
const SELECTED_KEY = "openidea.ios.simulator:";
interface IosSimulatorsState {
/** null: not an Xcode project, not macOS, or not loaded yet. */
list: IosSimulator[] | null;
error: string | null;
/** The UDID chosen in the toolbar; kept while that simulator is missing. */
preferred: string | null;
root: string | null;
/** A boot or shutdown under way, by UDID. */
busy: string | null;
}
export const useIosSimulators = create<IosSimulatorsState>(() => ({
list: null,
error: null,
preferred: null,
root: null,
busy: null,
}));
function storedSimulator(root: string): string | null {
try {
return localStorage.getItem(SELECTED_KEY + root);
} catch {
return null;
}
}
/**
* The toolbar's simulator: the chosen one while listed, else the backend's default
* (`pick_simulator`): a booted simulator, then the newest iPhone. The list comes booted first,
* then newest runtime first.
*/
export function currentIosSimulator(state = useIosSimulators.getState()): IosSimulator | null {
const list = state.list ?? [];
return (
list.find((s) => s.udid === state.preferred) ??
list.find((s) => !s.physical && s.state === "Booted") ??
list.find((s) => !s.physical && s.name.startsWith("iPhone")) ??
list[0] ??
null
);
}
/**
* The simulator UDID to launch `rc` on: `params.simulator` when the configuration pins one,
* else the toolbar's choice; null when there is none.
*/
export function iosSimulatorFor(rc: RunConfig): string | null {
return rc.params?.simulator?.trim() || currentIosSimulator()?.udid || null;
}
/** Lists the simulators again; `simctl` has no change feed, so the chooser asks on every opening. */
export async function refreshIosSimulators() {
const root = useIosSimulators.getState().root;
try {
const list = await call(commands.iosSimulators());
if (useIosSimulators.getState().root === root) useIosSimulators.setState({ list, error: null });
} catch (err) {
// Shown only once a listing has confirmed an Xcode project: before that the call also fails
// in Restricted Mode, on remote hosts and wherever the command is missing.
const state = useIosSimulators.getState();
if (state.root === root && state.list !== null)
useIosSimulators.setState({ error: err instanceof Error ? err.message : String(err) });
}
}
/** Loads the project's simulators until the returned function is called. */
export function watchIosSimulators(root: string): () => void {
useIosSimulators.setState({ root, preferred: storedSimulator(root), list: null, error: null, busy: null });
void refreshIosSimulators();
return () => useIosSimulators.setState({ list: null, error: null, root: null, busy: null });
}
export function selectIosSimulator(udid: string) {
const { root } = useIosSimulators.getState();
useIosSimulators.setState({ preferred: udid });
try {
if (root) localStorage.setItem(SELECTED_KEY + root, udid);
} catch {
/* the choice lasts for this session */
}
}
async function act(s: IosSimulator, boot: boolean) {
useIosSimulators.setState({ busy: s.udid });
try {
await call(boot ? commands.iosSimulatorBoot(s.udid) : commands.iosSimulatorShutdown(s.udid));
} catch (err) {
notifications().notify({
group: "run",
level: "error",
key: "ios.simulator",
title: boot ? tx`Could not boot ${s.name}` : tx`Could not shut down ${s.name}`,
body: err instanceof Error ? err.message : String(err),
});
} finally {
useIosSimulators.setState({ busy: null });
await refreshIosSimulators();
}
}
export const bootIosSimulator = (s: IosSimulator) => act(s, true);
export const shutDownIosSimulator = (s: IosSimulator) => act(s, false);
export async function openSimulatorApp() {
try {
await call(commands.iosSimulatorOpenApp());
} catch (err) {
notifications().notify({
group: "run",
level: "error",
key: "ios.simulator",
title: tx("Could not open Simulator"),
body: err instanceof Error ? err.message : String(err),
});
}
}