示例工程 35-multi-display-frameless

35-multi-display-frameless/app/shared/ipc.ts

typescript · 1.9 KB

import type { DisplayInfo, Rect } from "./display";

export const CH = {
  snapshot: "display:snapshot",
  moveTo: "display:move-to",
  center: "display:center",
  setMode: "display:set-mode",
  minimize: "display:minimize",
  toggleMaximize: "display:toggle-maximize",
  closeWindow: "display:close",
  refresh: "display:refresh",
} as const;

export type WindowMode =
  "normal" | "maximized" | "simple" | "kiosk" | "fullscreen";

export type WindowInfo = {
  bounds: Rect;
  contentBounds: Rect;
  mode: WindowMode;
  isMaximized: boolean;
  isFullScreen: boolean;
  isSimpleFullScreen: boolean;
  isKiosk: boolean;
  isResizable: boolean;
  isFocused: boolean;
  scaleFactor: number;
};

export type EventRow = {
  at: number;
  kind: string;
  detail: string;
};

export type Snapshot = {
  platform: string;
  electronVersion: string;
  displays: DisplayInfo[];
  simulated: DisplayInfo[];
  primaryId: number;
  cursor: { x: number; y: number };
  nearestId: number;
  matchingId: number;
  window: WindowInfo;
  lastPlacement: {
    displayId: number;
    bounds: Rect;
    reason: string;
    at: number;
  } | null;
  events: EventRow[];
};

export function assertDisplayIdReq(
  value: unknown,
): asserts value is { displayId: number } {
  if (typeof value !== "object" || value === null) {
    throw new Error("BAD_DISPLAY_REQ");
  }
  const id = (value as { displayId?: unknown }).displayId;
  if (typeof id !== "number" || !Number.isInteger(id) || id <= 0) {
    throw new Error("BAD_DISPLAY_ID");
  }
}

export function assertModeReq(
  value: unknown,
): asserts value is { mode: WindowMode } {
  if (typeof value !== "object" || value === null) {
    throw new Error("BAD_MODE_REQ");
  }
  const mode = (value as { mode?: unknown }).mode;
  if (
    mode !== "normal" &&
    mode !== "maximized" &&
    mode !== "simple" &&
    mode !== "kiosk" &&
    mode !== "fullscreen"
  ) {
    throw new Error("BAD_MODE");
  }
}