import type { BrowserWindow } from "@domicile-desktop/sdk/desktop/browser"; import type { Domicile } from "@domicile-desktop/sdk/desktop/desktop"; import { WEBVIEW_GUEST_FOCUS_EVENT } from "@domicile-desktop/sdk/webview-element"; import { useCallback, useEffect, useMemo, useState, useSyncExternalStore, } from "react"; import { css } from "../../styled-system/css"; type Props = { domicile: Pick; /** A click in a view's page: see `BrowserWindow`'s `onReach`. */ onReach: () => void; /** Nothing can click the window, so a view taking focus is no reach. */ unclickable: boolean; /** The engine id of the window whose views these are. */ window: string; }; /** * An Electron app's views (`WebContentsView`, `BrowserView`) in its window: * each a `` over the window's page, where the app put it, bottom * first. Manganese honors the app's layout; the engine's bounds are a hint * another shell may lay out its own way. See "Views" in * docs/SHELL-BROWSER-WINDOWS.md. */ export const PageViews = ({ domicile, onReach, unclickable, window, }: Props) => { const subscribe = useCallback( (changed: () => void) => domicile.browser.on("change", changed), [domicile], ); // The SDK keeps the same list until the engine's changes, so this settles. const all = useSyncExternalStore(subscribe, () => domicile.browser.views); const views = useMemo( () => all?.filter(({ parent }) => parent === window) ?? [], [all, window], ); return views.map((view) => ( )); }; const PageView = ({ onReach, unclickable, view, }: Omit & { view: BrowserWindow }) => { const [element, setElement] = useState(null); useEffect(() => { if (element === null) { return undefined; } else { const reached = () => { if (!unclickable) { onReach(); } }; element.addEventListener(WEBVIEW_GUEST_FOCUS_EVENT, reached); return () => { element.removeEventListener(WEBVIEW_GUEST_FOCUS_EVENT, reached); }; } }, [element, onReach, unclickable]); return (