示例工程 34-global-shortcuts-autostart
34-global-shortcuts-autostart/app/shared/ipc.ts
import type { AttemptVerdict } from "./accelerator";
import type { AutostartPlan, BlockerKind } from "./power-policy";
export const CH = {
snapshot: "hub:snapshot",
register: "hub:register",
trigger: "hub:trigger",
setResidency: "hub:set-residency",
closeWindow: "hub:close-window",
showWindow: "hub:show-window",
startBlocker: "hub:start-blocker",
stopBlocker: "hub:stop-blocker",
applyAutostart: "hub:apply-autostart",
quit: "hub:quit",
refresh: "hub:refresh",
} as const;
export type ShortcutId = "toggle-window" | "quick-note" | "focus-search";
export type ShortcutStatus = "registered" | "conflict" | "invalid" | "spare";
export type ShortcutRow = {
id: ShortcutId;
label: string;
accelerator: string;
registered: boolean;
status: ShortcutStatus;
detail: string;
};
export type RegisterRow = {
at: number;
accelerator: string;
verdict: AttemptVerdict;
detail: string;
};
export type BlockerRow = {
id: number;
kind: BlockerKind;
startedAt: number;
};
export type EventRow = {
at: number;
kind: string;
detail: string;
};
export type AutostartInfo = {
platform: string;
openAtLogin: boolean;
wasOpenedAtLogin: boolean;
executableWillLaunchAtLogin: boolean;
status: string;
launchArgs: string[];
isAutostartLaunch: boolean;
isHiddenLaunch: boolean;
packaged: boolean;
};
export type ResidencyInfo = {
enabled: boolean;
hiddenCount: number;
showCount: number;
visible: boolean;
};
export type PowerInfo = {
idleSeconds: number;
onBattery: boolean;
blockers: BlockerRow[];
lastDecision: string;
};
export type Snapshot = {
platform: string;
shortcuts: ShortcutRow[];
attempts: RegisterRow[];
lastAttempt: RegisterRow | null;
events: EventRow[];
notes: string[];
residency: ResidencyInfo;
power: PowerInfo;
autostart: AutostartInfo;
plan: AutostartPlan;
planOpenAtLogin: boolean;
};
export function assertRegisterReq(
value: unknown,
): asserts value is { id: ShortcutId; accelerator: string } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_REGISTER_REQ");
}
const req = value as { id?: unknown; accelerator?: unknown };
if (
req.id !== "toggle-window" &&
req.id !== "quick-note" &&
req.id !== "focus-search"
) {
throw new Error("BAD_ACTION");
}
if (
typeof req.accelerator !== "string" ||
req.accelerator.length === 0 ||
req.accelerator.length > 80
) {
throw new Error("BAD_ACCELERATOR");
}
}
export function assertActionReq(
value: unknown,
): asserts value is { id: ShortcutId } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_ACTION_REQ");
}
const id = (value as { id?: unknown }).id;
if (id !== "toggle-window" && id !== "quick-note" && id !== "focus-search") {
throw new Error("BAD_ACTION");
}
}
export function assertResidencyReq(
value: unknown,
): asserts value is { enabled: boolean } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_RESIDENCY_REQ");
}
if (typeof (value as { enabled?: unknown }).enabled !== "boolean") {
throw new Error("BAD_RESIDENCY_REQ");
}
}
export function assertBlockerReq(
value: unknown,
): asserts value is { kind: BlockerKind } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_BLOCKER_REQ");
}
const kind = (value as { kind?: unknown }).kind;
if (kind !== "prevent-app-suspension" && kind !== "prevent-display-sleep") {
throw new Error("BAD_BLOCKER_KIND");
}
}
export function assertBlockerIdReq(
value: unknown,
): asserts value is { id: number } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_BLOCKER_ID_REQ");
}
const id = (value as { id?: unknown }).id;
if (typeof id !== "number" || !Number.isInteger(id) || id < 0) {
throw new Error("BAD_BLOCKER_ID");
}
}
export function assertAutostartReq(
value: unknown,
): asserts value is { enabled: boolean; dryRun: boolean } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_AUTOSTART_REQ");
}
const req = value as { enabled?: unknown; dryRun?: unknown };
if (typeof req.enabled !== "boolean" || typeof req.dryRun !== "boolean") {
throw new Error("BAD_AUTOSTART_REQ");
}
}