feat(desktop): resizable Picture-in-Picture window for MCP Apps (#12417)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -31,7 +31,7 @@ import type {
|
||||
McpUiSizeChangedNotification,
|
||||
} from '@modelcontextprotocol/ext-apps/app-bridge';
|
||||
import type { CallToolResult, JSONRPCRequest, Tool } from '@modelcontextprotocol/sdk/types.js';
|
||||
import { GripHorizontal, Maximize2, PictureInPicture2, X } from 'lucide-react';
|
||||
import { Maximize2, PictureInPicture2, X } from 'lucide-react';
|
||||
import { useCallback, useEffect, useMemo, useReducer, useRef, useState } from 'react';
|
||||
import { callMcpAppTool, readMcpAppResource } from '../../acp/mcp-apps';
|
||||
import { httpBaseFromAcpWebSocketUrl, isLoopbackAcpWebSocketUrl } from '../../acp/url';
|
||||
@@ -43,23 +43,24 @@ import { errorMessage } from '../../utils/conversionUtils';
|
||||
import { defineMessages, useIntl } from '../../i18n';
|
||||
import FlyingBird from '../FlyingBird';
|
||||
import { formatExtensionName } from '../settings/extensions/subcomponents/ExtensionList';
|
||||
import { getContainerDimensions } from './containerDimensions';
|
||||
import {
|
||||
GooseDisplayMode,
|
||||
SandboxPermissions,
|
||||
McpAppToolCancelled,
|
||||
McpAppToolInput,
|
||||
McpAppToolInputPartial,
|
||||
DimensionLayout,
|
||||
OnDisplayModeChange,
|
||||
} from './types';
|
||||
import { useDisplayMode, AVAILABLE_DISPLAY_MODES } from './useDisplayMode';
|
||||
import {
|
||||
useDisplayMode,
|
||||
AVAILABLE_DISPLAY_MODES,
|
||||
PIP_WIDTH,
|
||||
PIP_HEIGHT,
|
||||
PIP_MARGIN_RIGHT,
|
||||
PIP_MARGIN_BOTTOM,
|
||||
} from './useDisplayMode';
|
||||
PIP_SHELL_CLASSES,
|
||||
PipPlaceholder,
|
||||
PipWindow,
|
||||
pipFrameStyle,
|
||||
pipPanelStyle,
|
||||
usePipWindow,
|
||||
} from './PipWindow';
|
||||
|
||||
const i18n = defineMessages({
|
||||
appFallbackTitle: {
|
||||
@@ -82,18 +83,6 @@ const i18n = defineMessages({
|
||||
id: 'mcpAppRenderer.fullscreen',
|
||||
defaultMessage: 'Fullscreen',
|
||||
},
|
||||
close: {
|
||||
id: 'mcpAppRenderer.close',
|
||||
defaultMessage: 'Close',
|
||||
},
|
||||
movePipWindow: {
|
||||
id: 'mcpAppRenderer.movePipWindow',
|
||||
defaultMessage: 'Move Picture-in-Picture window (use arrow keys)',
|
||||
},
|
||||
playingInPip: {
|
||||
id: 'mcpAppRenderer.playingInPip',
|
||||
defaultMessage: 'Playing in Picture-in-Picture',
|
||||
},
|
||||
invalidUrl: {
|
||||
id: 'mcpAppRenderer.invalidUrl',
|
||||
defaultMessage: 'Invalid URL',
|
||||
@@ -112,53 +101,6 @@ const DEFAULT_IFRAME_HEIGHT = 200;
|
||||
const FULLSCREEN_HEADER_HEIGHT = 48;
|
||||
const DEFAULT_SANDBOX_PERMISSIONS = 'allow-scripts allow-same-origin allow-forms';
|
||||
|
||||
const DISPLAY_MODE_LAYOUTS: Record<GooseDisplayMode, DimensionLayout> = {
|
||||
inline: { width: 'fixed', height: 'unbounded' },
|
||||
fullscreen: { width: 'fixed', height: 'fixed' },
|
||||
standalone: { width: 'fixed', height: 'fixed' },
|
||||
pip: { width: 'fixed', height: 'fixed' },
|
||||
// sidecar: { width: 'fixed', height: 'flexible' }, // example on how to use flexible layout
|
||||
};
|
||||
|
||||
function getContainerDimensions(
|
||||
displayMode: GooseDisplayMode,
|
||||
measuredWidth: number,
|
||||
measuredHeight: number
|
||||
): McpUiHostContext['containerDimensions'] {
|
||||
const layout = DISPLAY_MODE_LAYOUTS[displayMode] ?? DISPLAY_MODE_LAYOUTS.inline;
|
||||
|
||||
// Only require a measurement for axes that are fixed or flexible (unbounded axes are omitted).
|
||||
if (
|
||||
(layout.width !== 'unbounded' && measuredWidth <= 0) ||
|
||||
(layout.height !== 'unbounded' && measuredHeight <= 0)
|
||||
)
|
||||
return undefined;
|
||||
|
||||
const widthDimension = (() => {
|
||||
switch (layout.width) {
|
||||
case 'fixed':
|
||||
return { width: measuredWidth };
|
||||
case 'flexible':
|
||||
return { maxWidth: measuredWidth };
|
||||
case 'unbounded':
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
const heightDimension = (() => {
|
||||
switch (layout.height) {
|
||||
case 'fixed':
|
||||
return { height: measuredHeight };
|
||||
case 'flexible':
|
||||
return { maxHeight: measuredHeight };
|
||||
case 'unbounded':
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
return { ...widthDimension, ...heightDimension };
|
||||
}
|
||||
|
||||
async function fetchMcpAppProxyUrl(csp: McpUiResourceCsp | null): Promise<string | null> {
|
||||
try {
|
||||
const acpUrl = await window.electron.getAcpUrl();
|
||||
@@ -261,6 +203,16 @@ interface GooseAppFrameProps {
|
||||
|
||||
const SANDBOX_PROXY_READY_METHOD = 'ui/notifications/sandbox-proxy-ready';
|
||||
|
||||
// In PiP the iframe must fill the window even when the guest's content is
|
||||
// shorter, otherwise the window's own background shows below the app.
|
||||
function iframeHeightFor(guestHeight: number, hostContext: McpUiHostContext): number {
|
||||
const dimensions = hostContext.containerDimensions;
|
||||
if (hostContext.displayMode !== 'pip' || !dimensions || !('maxHeight' in dimensions)) {
|
||||
return guestHeight;
|
||||
}
|
||||
return Math.max(guestHeight, dimensions.maxHeight ?? 0);
|
||||
}
|
||||
|
||||
function GooseAppFrame({
|
||||
html,
|
||||
sandbox,
|
||||
@@ -281,6 +233,7 @@ function GooseAppFrame({
|
||||
}: GooseAppFrameProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const iframeRef = useRef<HTMLIFrameElement | null>(null);
|
||||
const guestHeightRef = useRef<number | null>(null);
|
||||
const bridgeRef = useRef<AppBridge | null>(null);
|
||||
const [connected, setConnected] = useState(false);
|
||||
const [initialized, setInitialized] = useState(false);
|
||||
@@ -382,7 +335,8 @@ function GooseAppFrame({
|
||||
iframe.style.width = `${params.width}px`;
|
||||
}
|
||||
if (params.height !== undefined) {
|
||||
iframe.style.height = `${params.height}px`;
|
||||
guestHeightRef.current = params.height;
|
||||
iframe.style.height = `${iframeHeightFor(params.height, hostContextRef.current)}px`;
|
||||
}
|
||||
};
|
||||
bridge.oninitialized = () => {
|
||||
@@ -454,6 +408,14 @@ function GooseAppFrame({
|
||||
}
|
||||
}, [initialized, hostContext]);
|
||||
|
||||
// Re-apply the PiP floor when the window is resized or the mode changes.
|
||||
useEffect(() => {
|
||||
const iframe = iframeRef.current;
|
||||
const guestHeight = guestHeightRef.current;
|
||||
if (!iframe || guestHeight === null) return;
|
||||
iframe.style.height = `${iframeHeightFor(guestHeight, hostContext)}px`;
|
||||
}, [hostContext]);
|
||||
|
||||
useEffect(() => {
|
||||
const bridge = bridgeRef.current;
|
||||
if (initialized && toolInputPartial && bridge) {
|
||||
@@ -578,11 +540,11 @@ export default function McpAppRenderer({
|
||||
appTitle,
|
||||
changeDisplayMode,
|
||||
inlineHeight,
|
||||
pipPosition,
|
||||
pipHandlers,
|
||||
fullscreenCloseRef,
|
||||
} = dm;
|
||||
|
||||
const pip = usePipWindow({ active: isPip, sessionId });
|
||||
|
||||
const { resolvedTheme, mcpHostStyles } = useTheme();
|
||||
|
||||
// Fetch the MCP Tool definition (name, description, inputSchema) for hostContext.toolInfo.
|
||||
@@ -874,20 +836,28 @@ export default function McpAppRenderer({
|
||||
[isInline]
|
||||
);
|
||||
|
||||
// Track the container's pixel dimensions so we can report them to apps via containerDimensions.
|
||||
// Track pixel dimensions for containerDimensions. Width comes from the content
|
||||
// element so a PiP scrollbar gutter is not counted; height from the container.
|
||||
useEffect(() => {
|
||||
const el = containerRef.current;
|
||||
if (!el) return;
|
||||
const container = containerRef.current;
|
||||
const content = contentRef.current;
|
||||
if (!container || !content) return;
|
||||
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
for (const entry of entries) {
|
||||
const { width, height } = entry.contentRect;
|
||||
setContainerWidth((prev) => (prev !== Math.round(width) ? Math.round(width) : prev));
|
||||
setContainerHeight((prev) => (prev !== Math.round(height) ? Math.round(height) : prev));
|
||||
if (entry.target === content) {
|
||||
const width = Math.round(entry.contentRect.width);
|
||||
setContainerWidth((prev) => (prev !== width ? width : prev));
|
||||
}
|
||||
if (entry.target === container) {
|
||||
const height = Math.round(entry.contentRect.height);
|
||||
setContainerHeight((prev) => (prev !== height ? height : prev));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
observer.observe(el);
|
||||
observer.observe(container);
|
||||
observer.observe(content);
|
||||
return () => observer.disconnect();
|
||||
}, []);
|
||||
|
||||
@@ -1074,30 +1044,8 @@ export default function McpAppRenderer({
|
||||
// Fullscreen controls are rendered by renderFullscreenHeader instead.
|
||||
if (activeDisplayMode === 'fullscreen') return null;
|
||||
|
||||
if (activeDisplayMode === 'pip') {
|
||||
return (
|
||||
<>
|
||||
{appSupportsFullscreen && (
|
||||
<button
|
||||
onClick={() => changeDisplayMode('fullscreen')}
|
||||
className="cursor-pointer rounded-md bg-black/50 p-1 text-white backdrop-blur-sm transition-opacity hover:bg-black/70"
|
||||
title={intl.formatMessage(i18n.fullscreen)}
|
||||
aria-label={intl.formatMessage(i18n.fullscreen)}
|
||||
>
|
||||
<Maximize2 size={14} />
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={() => changeDisplayMode('inline')}
|
||||
className="cursor-pointer rounded-md bg-black/50 p-1 text-white backdrop-blur-sm transition-opacity hover:bg-black/70"
|
||||
title={intl.formatMessage(i18n.close)}
|
||||
aria-label={intl.formatMessage(i18n.close)}
|
||||
>
|
||||
<X size={14} />
|
||||
</button>
|
||||
</>
|
||||
);
|
||||
}
|
||||
// PiP controls are rendered by PipWindow instead.
|
||||
if (activeDisplayMode === 'pip') return null;
|
||||
|
||||
// Inline mode — show controls on hover or keyboard focus
|
||||
return (
|
||||
@@ -1131,8 +1079,7 @@ export default function McpAppRenderer({
|
||||
const containerClasses = cn(
|
||||
'mcp-app-container bg-background-primary [&_iframe]:!w-full',
|
||||
isFillsViewport && 'fixed inset-0 z-[1000] overflow-hidden [&_iframe]:!h-full',
|
||||
isPip &&
|
||||
'fixed z-[900] overflow-y-auto overflow-x-hidden rounded-xl border border-border-primary shadow-2xl',
|
||||
isPip && PIP_SHELL_CLASSES.panel,
|
||||
isInline && 'group/mcp-app relative overflow-hidden',
|
||||
isInline && !isError && 'mt-6 mb-2',
|
||||
isInline && !isError && meta.prefersBorder && 'border border-border-primary rounded-lg',
|
||||
@@ -1143,12 +1090,7 @@ export default function McpAppRenderer({
|
||||
...(isFillsViewport
|
||||
? {}
|
||||
: isPip
|
||||
? {
|
||||
width: `${PIP_WIDTH}px`,
|
||||
height: `${PIP_HEIGHT}px`,
|
||||
right: `${PIP_MARGIN_RIGHT - pipPosition.x}px`,
|
||||
bottom: `${PIP_MARGIN_BOTTOM - pipPosition.y}px`,
|
||||
}
|
||||
? pipPanelStyle(pip.geometry)
|
||||
: {
|
||||
width: '100%',
|
||||
height: `${effectiveInlineHeight}px`,
|
||||
@@ -1165,48 +1107,41 @@ export default function McpAppRenderer({
|
||||
/>
|
||||
)}
|
||||
{isPip && (
|
||||
<div
|
||||
className="mt-6 mb-2 flex items-center justify-center rounded-lg border border-dashed border-border-primary bg-black/[0.02] dark:bg-white/[0.02]"
|
||||
style={{ width: '100%', height: `${inlineHeight}px` }}
|
||||
>
|
||||
<button
|
||||
onClick={() => changeDisplayMode('inline')}
|
||||
className="cursor-pointer flex items-center gap-2 rounded-md px-3 py-1.5 text-xs text-text-secondary transition-colors hover:bg-black/5 hover:text-text-primary dark:hover:bg-white/5"
|
||||
>
|
||||
<PictureInPicture2 size={14} />
|
||||
<span>{intl.formatMessage(i18n.playingInPip)}</span>
|
||||
</button>
|
||||
</div>
|
||||
<PipPlaceholder height={inlineHeight} onReturn={() => changeDisplayMode('inline')} />
|
||||
)}
|
||||
|
||||
{/* Stable app container — never unmounted, only repositioned via CSS */}
|
||||
{/* Stable app shell — never unmounted, only restyled per mode, so the
|
||||
iframe keeps its state. Mode-specific chrome renders as siblings of
|
||||
the container inside this frame, never as its ancestors. */}
|
||||
<div
|
||||
ref={containerRef}
|
||||
className={cn(containerClasses, isFillsViewport && 'flex flex-col', isPip && 'group/pip')}
|
||||
style={containerStyle}
|
||||
className={cn(isPip && PIP_SHELL_CLASSES.frame)}
|
||||
style={isPip ? pipFrameStyle(pip.geometry) : undefined}
|
||||
>
|
||||
{isFullscreen && renderFullscreenHeader()}
|
||||
{isPip && (
|
||||
<div className="pointer-events-none sticky top-1 z-20 flex h-0 items-start justify-between px-1 opacity-0 transition-opacity group-hover/pip:pointer-events-auto group-hover/pip:opacity-100 focus-within:pointer-events-auto focus-within:opacity-100">
|
||||
<div
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={intl.formatMessage(i18n.movePipWindow)}
|
||||
className="pointer-events-auto cursor-grab rounded-md bg-black/50 p-1 text-white backdrop-blur-sm hover:bg-black/70 active:cursor-grabbing"
|
||||
onPointerDown={pipHandlers.onPointerDown}
|
||||
onPointerMove={pipHandlers.onPointerMove}
|
||||
onPointerUp={pipHandlers.onPointerUp}
|
||||
onLostPointerCapture={pipHandlers.onLostPointerCapture}
|
||||
onKeyDown={pipHandlers.onKeyDown}
|
||||
>
|
||||
<GripHorizontal size={14} />
|
||||
</div>
|
||||
<div className="flex gap-1">{renderDisplayModeControls()}</div>
|
||||
</div>
|
||||
<PipWindow
|
||||
{...pip}
|
||||
title={fullscreenTitle}
|
||||
onFullscreen={appSupportsFullscreen ? () => changeDisplayMode('fullscreen') : undefined}
|
||||
onClose={() => changeDisplayMode('inline')}
|
||||
/>
|
||||
)}
|
||||
<div ref={contentRef} className={cn('relative w-full', !isPip && 'flex-1 min-h-0')}>
|
||||
{!isPip && renderDisplayModeControls()}
|
||||
{renderContent()}
|
||||
<div
|
||||
ref={containerRef}
|
||||
className={cn(containerClasses, isFillsViewport && 'flex flex-col')}
|
||||
style={containerStyle}
|
||||
>
|
||||
{isFullscreen && renderFullscreenHeader()}
|
||||
<div
|
||||
ref={contentRef}
|
||||
className={cn(
|
||||
'relative w-full',
|
||||
!isPip && 'flex-1 min-h-0',
|
||||
isPip && PIP_SHELL_CLASSES.content
|
||||
)}
|
||||
>
|
||||
{!isPip && renderDisplayModeControls()}
|
||||
{renderContent()}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
|
||||
@@ -0,0 +1,650 @@
|
||||
/**
|
||||
* Picture-in-Picture display mode for MCP Apps: geometry, pointer and keyboard
|
||||
* gestures, per-session size memory, and the chrome drawn around the app.
|
||||
*
|
||||
* The app's stable container lives in McpAppRenderer and is only restyled when
|
||||
* the mode changes, so the iframe is never remounted. This module supplies the
|
||||
* classes and styles that container takes on in PiP and renders the title bar
|
||||
* and resize zones as siblings of it inside the PiP frame.
|
||||
*
|
||||
* The panel is anchored to the bottom-right corner of the main window. `x`/`y`
|
||||
* are offsets from the default anchored position (positive x moves right,
|
||||
* positive y moves down), so a panel that has never been moved stays glued to
|
||||
* the corner when the main window is resized. The panel sits inside a slightly
|
||||
* larger transparent frame that holds the title bar above it and the resize
|
||||
* zones straddling its edges. Clamping keeps the whole frame inside the viewport.
|
||||
*/
|
||||
|
||||
import { Maximize2, PictureInPicture2, X } from 'lucide-react';
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { defineMessages, useIntl } from '../../i18n';
|
||||
import { cn } from '../../utils';
|
||||
|
||||
const i18n = defineMessages({
|
||||
fullscreen: {
|
||||
id: 'mcpAppRenderer.fullscreen',
|
||||
defaultMessage: 'Fullscreen',
|
||||
},
|
||||
close: {
|
||||
id: 'mcpAppRenderer.close',
|
||||
defaultMessage: 'Close',
|
||||
},
|
||||
movePipWindow: {
|
||||
id: 'mcpAppRenderer.movePipWindow',
|
||||
defaultMessage: 'Move Picture-in-Picture window',
|
||||
},
|
||||
resizePipWindow: {
|
||||
id: 'mcpAppRenderer.resizePipWindow',
|
||||
defaultMessage: 'Resize Picture-in-Picture window',
|
||||
},
|
||||
playingInPip: {
|
||||
id: 'mcpAppRenderer.playingInPip',
|
||||
defaultMessage: 'Playing in Picture-in-Picture',
|
||||
},
|
||||
});
|
||||
|
||||
// ── Geometry ────────────────────────────────────────────────────────────
|
||||
|
||||
export const PIP_MIN_WIDTH = 400;
|
||||
export const PIP_MIN_HEIGHT = 300;
|
||||
export const PIP_MARGIN_RIGHT = 16;
|
||||
// Keeps the PiP window above the chat input area (~120px) plus padding.
|
||||
export const PIP_MARGIN_BOTTOM = 140;
|
||||
|
||||
/** Square hit zone centered on each window corner. */
|
||||
export const PIP_CORNER_ZONE = 24;
|
||||
/** Thickness of the strip centered on each window edge. */
|
||||
export const PIP_EDGE_ZONE = 8;
|
||||
/** Title bar shown above the panel while the window is hovered. */
|
||||
export const PIP_TITLE_BAR_HEIGHT = 32;
|
||||
// Matches the panel's rounded-xl radius so the title bar fills the corner gap.
|
||||
const PIP_TITLE_BAR_OVERLAP = 12;
|
||||
/**
|
||||
* Space between the panel and the frame edges. The top holds the title bar
|
||||
* plus room for the resize zones that straddle the bar's top edge.
|
||||
*/
|
||||
export const PIP_FRAME_INSET = {
|
||||
top: PIP_TITLE_BAR_HEIGHT + PIP_CORNER_ZONE / 2,
|
||||
right: 12,
|
||||
bottom: 12,
|
||||
left: 12,
|
||||
} as const;
|
||||
|
||||
export interface PipGeometry {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
export interface Viewport {
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
export interface Rect {
|
||||
left: number;
|
||||
top: number;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
export const DEFAULT_PIP_GEOMETRY: PipGeometry = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: PIP_MIN_WIDTH,
|
||||
height: PIP_MIN_HEIGHT,
|
||||
};
|
||||
|
||||
export type PipCorner = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
|
||||
export type PipEdge = 'top' | 'right' | 'bottom' | 'left';
|
||||
export type PipResizeHandle = PipCorner | PipEdge;
|
||||
|
||||
export const PIP_CORNERS: readonly PipCorner[] = [
|
||||
'top-left',
|
||||
'top-right',
|
||||
'bottom-left',
|
||||
'bottom-right',
|
||||
];
|
||||
export const PIP_EDGES: readonly PipEdge[] = ['top', 'right', 'bottom', 'left'];
|
||||
export const PIP_RESIZE_HANDLES: readonly PipResizeHandle[] = [...PIP_EDGES, ...PIP_CORNERS];
|
||||
|
||||
const PIP_RESIZE_CURSOR: Record<PipResizeHandle, string> = {
|
||||
top: 'cursor-ns-resize',
|
||||
bottom: 'cursor-ns-resize',
|
||||
left: 'cursor-ew-resize',
|
||||
right: 'cursor-ew-resize',
|
||||
'top-left': 'cursor-nwse-resize',
|
||||
'bottom-right': 'cursor-nwse-resize',
|
||||
'top-right': 'cursor-nesw-resize',
|
||||
'bottom-left': 'cursor-nesw-resize',
|
||||
};
|
||||
|
||||
function clamp(value: number, min: number, max: number): number {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
|
||||
export function getViewport(): Viewport {
|
||||
return { width: window.innerWidth, height: window.innerHeight };
|
||||
}
|
||||
|
||||
/** Top-left corner of the PiP panel in viewport coordinates. */
|
||||
export function pipTopLeft(
|
||||
geometry: PipGeometry,
|
||||
viewport: Viewport
|
||||
): { left: number; top: number } {
|
||||
return {
|
||||
left: viewport.width - PIP_MARGIN_RIGHT + geometry.x - geometry.width,
|
||||
top: viewport.height - PIP_MARGIN_BOTTOM + geometry.y - geometry.height,
|
||||
};
|
||||
}
|
||||
|
||||
/** Frame size and its offset from the viewport's bottom-right corner. */
|
||||
export function pipFrameRect(geometry: PipGeometry): {
|
||||
width: number;
|
||||
height: number;
|
||||
right: number;
|
||||
bottom: number;
|
||||
} {
|
||||
const { top, right, bottom, left } = PIP_FRAME_INSET;
|
||||
return {
|
||||
width: geometry.width + left + right,
|
||||
height: geometry.height + top + bottom,
|
||||
right: PIP_MARGIN_RIGHT - geometry.x - right,
|
||||
bottom: PIP_MARGIN_BOTTOM - geometry.y - bottom,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hit zone for a resize handle, in frame coordinates. The window being resized
|
||||
* is the title bar plus the panel, so the top zones sit on the bar's top edge
|
||||
* rather than on the seam between the bar and the panel.
|
||||
*/
|
||||
export function pipResizeZoneRect(handle: PipResizeHandle, geometry: PipGeometry): Rect {
|
||||
const half = PIP_CORNER_ZONE / 2;
|
||||
const edgeHalf = PIP_EDGE_ZONE / 2;
|
||||
const left = PIP_FRAME_INSET.left;
|
||||
const top = PIP_FRAME_INSET.top - PIP_TITLE_BAR_HEIGHT;
|
||||
const right = left + geometry.width;
|
||||
const bottom = PIP_FRAME_INSET.top + geometry.height;
|
||||
const windowHeight = bottom - top;
|
||||
|
||||
switch (handle) {
|
||||
case 'top-left':
|
||||
return {
|
||||
left: left - half,
|
||||
top: top - half,
|
||||
width: PIP_CORNER_ZONE,
|
||||
height: PIP_CORNER_ZONE,
|
||||
};
|
||||
case 'top-right':
|
||||
return {
|
||||
left: right - half,
|
||||
top: top - half,
|
||||
width: PIP_CORNER_ZONE,
|
||||
height: PIP_CORNER_ZONE,
|
||||
};
|
||||
case 'bottom-left':
|
||||
return {
|
||||
left: left - half,
|
||||
top: bottom - half,
|
||||
width: PIP_CORNER_ZONE,
|
||||
height: PIP_CORNER_ZONE,
|
||||
};
|
||||
case 'bottom-right':
|
||||
return {
|
||||
left: right - half,
|
||||
top: bottom - half,
|
||||
width: PIP_CORNER_ZONE,
|
||||
height: PIP_CORNER_ZONE,
|
||||
};
|
||||
case 'top':
|
||||
return {
|
||||
left: left + half,
|
||||
top: top - edgeHalf,
|
||||
width: geometry.width - PIP_CORNER_ZONE,
|
||||
height: PIP_EDGE_ZONE,
|
||||
};
|
||||
case 'bottom':
|
||||
return {
|
||||
left: left + half,
|
||||
top: bottom - edgeHalf,
|
||||
width: geometry.width - PIP_CORNER_ZONE,
|
||||
height: PIP_EDGE_ZONE,
|
||||
};
|
||||
case 'left':
|
||||
return {
|
||||
left: left - edgeHalf,
|
||||
top: top + half,
|
||||
width: PIP_EDGE_ZONE,
|
||||
height: windowHeight - PIP_CORNER_ZONE,
|
||||
};
|
||||
case 'right':
|
||||
return {
|
||||
left: right - edgeHalf,
|
||||
top: top + half,
|
||||
width: PIP_EDGE_ZONE,
|
||||
height: windowHeight - PIP_CORNER_ZONE,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps size to [minimum, viewport minus frame inset] and position so the
|
||||
* frame stays fully inside the viewport. When the viewport is too small on an
|
||||
* axis, that axis falls back to the default anchored offset.
|
||||
*/
|
||||
export function clampPipGeometry(geometry: PipGeometry, viewport: Viewport): PipGeometry {
|
||||
const inset = PIP_FRAME_INSET;
|
||||
const width = clamp(
|
||||
geometry.width,
|
||||
PIP_MIN_WIDTH,
|
||||
Math.max(PIP_MIN_WIDTH, viewport.width - inset.left - inset.right)
|
||||
);
|
||||
const height = clamp(
|
||||
geometry.height,
|
||||
PIP_MIN_HEIGHT,
|
||||
Math.max(PIP_MIN_HEIGHT, viewport.height - inset.top - inset.bottom)
|
||||
);
|
||||
|
||||
const minX = width + inset.left + PIP_MARGIN_RIGHT - viewport.width;
|
||||
const maxX = PIP_MARGIN_RIGHT - inset.right;
|
||||
const minY = height + inset.top + PIP_MARGIN_BOTTOM - viewport.height;
|
||||
const maxY = PIP_MARGIN_BOTTOM - inset.bottom;
|
||||
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
x: minX > maxX ? 0 : clamp(geometry.x, minX, maxX),
|
||||
y: minY > maxY ? 0 : clamp(geometry.y, minY, maxY),
|
||||
};
|
||||
}
|
||||
|
||||
export function movePipGeometry(
|
||||
origin: PipGeometry,
|
||||
dx: number,
|
||||
dy: number,
|
||||
viewport: Viewport
|
||||
): PipGeometry {
|
||||
return clampPipGeometry({ ...origin, x: origin.x + dx, y: origin.y + dy }, viewport);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resizes by dragging `handle`, keeping the opposite edge(s) fixed and never
|
||||
* pushing the frame past the viewport edges.
|
||||
*/
|
||||
export function resizePipGeometry(
|
||||
origin: PipGeometry,
|
||||
dx: number,
|
||||
dy: number,
|
||||
viewport: Viewport,
|
||||
handle: PipResizeHandle
|
||||
): PipGeometry {
|
||||
const inset = PIP_FRAME_INSET;
|
||||
const { left, top } = pipTopLeft(origin, viewport);
|
||||
const fromLeft = handle.includes('left');
|
||||
const fromRight = handle.includes('right');
|
||||
const fromTop = handle.includes('top');
|
||||
const fromBottom = handle.includes('bottom');
|
||||
|
||||
let width = origin.width;
|
||||
if (fromLeft || fromRight) {
|
||||
const maxWidth = Math.max(
|
||||
PIP_MIN_WIDTH,
|
||||
fromLeft ? left + origin.width - inset.left : viewport.width - inset.right - left
|
||||
);
|
||||
width = clamp(origin.width + (fromLeft ? -dx : dx), PIP_MIN_WIDTH, maxWidth);
|
||||
}
|
||||
|
||||
let height = origin.height;
|
||||
if (fromTop || fromBottom) {
|
||||
const maxHeight = Math.max(
|
||||
PIP_MIN_HEIGHT,
|
||||
fromTop ? top + origin.height - inset.top : viewport.height - inset.bottom - top
|
||||
);
|
||||
height = clamp(origin.height + (fromTop ? -dy : dy), PIP_MIN_HEIGHT, maxHeight);
|
||||
}
|
||||
|
||||
// x/y track the right/bottom edges, so they only change when those edges move.
|
||||
return clampPipGeometry(
|
||||
{
|
||||
width,
|
||||
height,
|
||||
x: fromRight ? origin.x + (width - origin.width) : origin.x,
|
||||
y: fromBottom ? origin.y + (height - origin.height) : origin.y,
|
||||
},
|
||||
viewport
|
||||
);
|
||||
}
|
||||
|
||||
// ── Per-session memory ──────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Remembers the most recent PiP geometry per chat session for the lifetime of
|
||||
* the app process. New sessions start from the defaults.
|
||||
*/
|
||||
const sessionGeometry = new Map<string, PipGeometry>();
|
||||
|
||||
export function loadPipGeometry(sessionId: string | null | undefined): PipGeometry | undefined {
|
||||
return sessionId ? sessionGeometry.get(sessionId) : undefined;
|
||||
}
|
||||
|
||||
export function savePipGeometry(sessionId: string | null | undefined, geometry: PipGeometry) {
|
||||
if (sessionId) sessionGeometry.set(sessionId, geometry);
|
||||
}
|
||||
|
||||
export function clearPipGeometryStore() {
|
||||
sessionGeometry.clear();
|
||||
}
|
||||
|
||||
// ── Gestures ────────────────────────────────────────────────────────────
|
||||
|
||||
export interface PipGestureHandlers {
|
||||
onPointerDown: (e: React.PointerEvent) => void;
|
||||
onPointerMove: (e: React.PointerEvent) => void;
|
||||
onPointerUp: (e: React.PointerEvent) => void;
|
||||
onLostPointerCapture: () => void;
|
||||
onKeyDown: (e: React.KeyboardEvent) => void;
|
||||
}
|
||||
|
||||
type PipGestureApply = (
|
||||
origin: PipGeometry,
|
||||
dx: number,
|
||||
dy: number,
|
||||
viewport: Viewport
|
||||
) => PipGeometry;
|
||||
|
||||
/**
|
||||
* Pointer-drag and arrow-key gesture that applies a (dx, dy) delta to the PiP
|
||||
* geometry. Used for both moving (delta shifts position) and resizing (delta
|
||||
* moves the dragged edge or corner).
|
||||
*/
|
||||
function createPipGesture(
|
||||
geometryRef: React.RefObject<PipGeometry>,
|
||||
setGeometry: (next: PipGeometry) => void,
|
||||
apply: PipGestureApply
|
||||
): PipGestureHandlers {
|
||||
let drag: { startX: number; startY: number; origin: PipGeometry } | null = null;
|
||||
|
||||
return {
|
||||
onPointerDown: (e) => {
|
||||
e.preventDefault();
|
||||
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
|
||||
drag = { startX: e.clientX, startY: e.clientY, origin: geometryRef.current };
|
||||
},
|
||||
onPointerMove: (e) => {
|
||||
if (!drag) return;
|
||||
setGeometry(
|
||||
apply(drag.origin, e.clientX - drag.startX, e.clientY - drag.startY, getViewport())
|
||||
);
|
||||
},
|
||||
onPointerUp: (e) => {
|
||||
(e.currentTarget as HTMLElement).releasePointerCapture(e.pointerId);
|
||||
drag = null;
|
||||
},
|
||||
onLostPointerCapture: () => {
|
||||
drag = null;
|
||||
},
|
||||
onKeyDown: (e) => {
|
||||
const step = e.shiftKey ? 32 : 8;
|
||||
let dx = 0;
|
||||
let dy = 0;
|
||||
switch (e.key) {
|
||||
case 'ArrowUp':
|
||||
dy = -step;
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
dy = step;
|
||||
break;
|
||||
case 'ArrowLeft':
|
||||
dx = -step;
|
||||
break;
|
||||
case 'ArrowRight':
|
||||
dx = step;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
e.preventDefault();
|
||||
setGeometry(apply(geometryRef.current, dx, dy, getViewport()));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ── Hook ────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface PipWindowState {
|
||||
/** Size and position offset from the default bottom-right corner. */
|
||||
geometry: PipGeometry;
|
||||
moveHandlers: PipGestureHandlers;
|
||||
/** One set of handlers per edge and corner. */
|
||||
resizeHandlers: Record<PipResizeHandle, PipGestureHandlers>;
|
||||
}
|
||||
|
||||
interface UsePipWindowOptions {
|
||||
/** Whether the app is currently displayed in PiP. */
|
||||
active: boolean;
|
||||
/** Chat session used to remember size and position between openings. */
|
||||
sessionId?: string | null;
|
||||
}
|
||||
|
||||
export function usePipWindow({ active, sessionId }: UsePipWindowOptions): PipWindowState {
|
||||
const [geometry, setGeometry] = useState<PipGeometry>(DEFAULT_PIP_GEOMETRY);
|
||||
const geometryRef = useRef(geometry);
|
||||
|
||||
const updateGeometry = useCallback(
|
||||
(next: PipGeometry) => {
|
||||
geometryRef.current = next;
|
||||
setGeometry(next);
|
||||
savePipGeometry(sessionId, next);
|
||||
},
|
||||
[sessionId]
|
||||
);
|
||||
|
||||
const moveHandlers = useMemo(
|
||||
() => createPipGesture(geometryRef, updateGeometry, movePipGeometry),
|
||||
[updateGeometry]
|
||||
);
|
||||
const resizeHandlers = useMemo(
|
||||
() =>
|
||||
Object.fromEntries(
|
||||
PIP_RESIZE_HANDLES.map((handle) => [
|
||||
handle,
|
||||
createPipGesture(geometryRef, updateGeometry, (origin, dx, dy, viewport) =>
|
||||
resizePipGeometry(origin, dx, dy, viewport, handle)
|
||||
),
|
||||
])
|
||||
) as Record<PipResizeHandle, PipGestureHandlers>,
|
||||
[updateGeometry]
|
||||
);
|
||||
|
||||
// Entering PiP restores the session's last geometry (or defaults), re-clamped
|
||||
// to the current window so a smaller window never leaves it off-screen.
|
||||
useEffect(() => {
|
||||
if (!active) return;
|
||||
updateGeometry(
|
||||
clampPipGeometry(loadPipGeometry(sessionId) ?? DEFAULT_PIP_GEOMETRY, getViewport())
|
||||
);
|
||||
}, [active, sessionId, updateGeometry]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!active) return;
|
||||
const handleResize = () => {
|
||||
updateGeometry(clampPipGeometry(geometryRef.current, getViewport()));
|
||||
};
|
||||
window.addEventListener('resize', handleResize);
|
||||
return () => window.removeEventListener('resize', handleResize);
|
||||
}, [active, updateGeometry]);
|
||||
|
||||
return { geometry, moveHandlers, resizeHandlers };
|
||||
}
|
||||
|
||||
// ── Shell styling ───────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Classes the stable app shell takes on in PiP. The frame wraps the panel and
|
||||
* carries the hover group for the title bar; the panel is the app container;
|
||||
* the content element scrolls the app.
|
||||
*/
|
||||
export const PIP_SHELL_CLASSES = {
|
||||
frame: 'group/pip pointer-events-none fixed z-[900]',
|
||||
panel:
|
||||
'pointer-events-auto absolute z-10 overflow-hidden rounded-xl border border-border-primary shadow-2xl',
|
||||
content: 'max-h-full overflow-y-auto overflow-x-hidden',
|
||||
} as const;
|
||||
|
||||
export function pipFrameStyle(geometry: PipGeometry): React.CSSProperties {
|
||||
const frame = pipFrameRect(geometry);
|
||||
return {
|
||||
width: `${frame.width}px`,
|
||||
height: `${frame.height}px`,
|
||||
right: `${frame.right}px`,
|
||||
bottom: `${frame.bottom}px`,
|
||||
};
|
||||
}
|
||||
|
||||
export function pipPanelStyle(geometry: PipGeometry): React.CSSProperties {
|
||||
return {
|
||||
top: `${PIP_FRAME_INSET.top}px`,
|
||||
left: `${PIP_FRAME_INSET.left}px`,
|
||||
width: `${geometry.width}px`,
|
||||
height: `${geometry.height}px`,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Chrome ──────────────────────────────────────────────────────────────
|
||||
|
||||
interface PipWindowProps extends PipWindowState {
|
||||
title: string;
|
||||
/** Omitted when the app does not support fullscreen. */
|
||||
onFullscreen?: () => void;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Title bar and resize zones rendered inside the PiP frame as siblings of the
|
||||
* app panel, so the app never sees them and the panel is never remounted.
|
||||
*
|
||||
* The title bar is styled like the fullscreen header. A drag layer fills the
|
||||
* bar so the whole bar moves the window, with the buttons layered over it. The
|
||||
* bar only takes pointer events while the PiP is hovered or focused, so it
|
||||
* never intercepts clicks meant for the chat behind it. It extends under the
|
||||
* panel's rounded top corners so the two join cleanly.
|
||||
*/
|
||||
export function PipWindow({
|
||||
geometry,
|
||||
moveHandlers,
|
||||
resizeHandlers,
|
||||
title,
|
||||
onFullscreen,
|
||||
onClose,
|
||||
}: PipWindowProps) {
|
||||
const intl = useIntl();
|
||||
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
className="pointer-events-none absolute flex translate-y-1 select-none items-center rounded-t-xl border border-b-0 border-border-primary bg-background-primary px-2 opacity-0 transition-[opacity,transform] duration-200 ease-out group-hover/pip:pointer-events-auto group-hover/pip:translate-y-0 group-hover/pip:opacity-100 focus-within:pointer-events-auto focus-within:translate-y-0 focus-within:opacity-100 motion-reduce:transition-none"
|
||||
style={{
|
||||
left: `${PIP_FRAME_INSET.left}px`,
|
||||
right: `${PIP_FRAME_INSET.right}px`,
|
||||
top: `${PIP_FRAME_INSET.top - PIP_TITLE_BAR_HEIGHT}px`,
|
||||
height: `${PIP_TITLE_BAR_HEIGHT + PIP_TITLE_BAR_OVERLAP}px`,
|
||||
paddingBottom: `${PIP_TITLE_BAR_OVERLAP}px`,
|
||||
}}
|
||||
>
|
||||
<div
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={intl.formatMessage(i18n.movePipWindow)}
|
||||
aria-keyshortcuts="ArrowUp ArrowDown ArrowLeft ArrowRight"
|
||||
className="absolute inset-0 cursor-grab rounded-t-xl outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-border-active active:cursor-grabbing"
|
||||
onPointerDown={moveHandlers.onPointerDown}
|
||||
onPointerMove={moveHandlers.onPointerMove}
|
||||
onPointerUp={moveHandlers.onPointerUp}
|
||||
onLostPointerCapture={moveHandlers.onLostPointerCapture}
|
||||
onKeyDown={moveHandlers.onKeyDown}
|
||||
/>
|
||||
<div className="pointer-events-none min-w-0 flex-1" />
|
||||
<span className="pointer-events-none truncate px-3 text-xs font-medium text-text-secondary">
|
||||
{title}
|
||||
</span>
|
||||
<div className="pointer-events-none relative flex flex-1 items-center justify-end gap-0.5">
|
||||
{onFullscreen && (
|
||||
<button
|
||||
onClick={onFullscreen}
|
||||
className="pointer-events-auto cursor-pointer rounded-md p-1 text-text-secondary transition-colors hover:bg-black/10 hover:text-text-primary dark:hover:bg-white/10"
|
||||
title={intl.formatMessage(i18n.fullscreen)}
|
||||
aria-label={intl.formatMessage(i18n.fullscreen)}
|
||||
>
|
||||
<Maximize2 size={14} />
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="pointer-events-auto cursor-pointer rounded-md p-1 text-text-secondary transition-colors hover:bg-black/10 hover:text-text-primary dark:hover:bg-white/10"
|
||||
title={intl.formatMessage(i18n.close)}
|
||||
aria-label={intl.formatMessage(i18n.close)}
|
||||
>
|
||||
<X size={14} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{PIP_RESIZE_HANDLES.map((handle) => {
|
||||
const handlers = resizeHandlers[handle];
|
||||
const zone = pipResizeZoneRect(handle, geometry);
|
||||
// Arrow keys on one handle can reach any size, so only the bottom-right
|
||||
// corner is exposed to keyboard and screen readers.
|
||||
const isKeyboardHandle = handle === 'bottom-right';
|
||||
return (
|
||||
<div
|
||||
key={handle}
|
||||
role={isKeyboardHandle ? 'button' : undefined}
|
||||
tabIndex={isKeyboardHandle ? 0 : undefined}
|
||||
aria-hidden={isKeyboardHandle ? undefined : true}
|
||||
aria-label={isKeyboardHandle ? intl.formatMessage(i18n.resizePipWindow) : undefined}
|
||||
aria-keyshortcuts={
|
||||
isKeyboardHandle ? 'ArrowUp ArrowDown ArrowLeft ArrowRight' : undefined
|
||||
}
|
||||
className={cn(
|
||||
'pointer-events-auto absolute z-20 rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-border-active',
|
||||
PIP_RESIZE_CURSOR[handle]
|
||||
)}
|
||||
style={{
|
||||
left: `${zone.left}px`,
|
||||
top: `${zone.top}px`,
|
||||
width: `${zone.width}px`,
|
||||
height: `${zone.height}px`,
|
||||
}}
|
||||
onPointerDown={handlers.onPointerDown}
|
||||
onPointerMove={handlers.onPointerMove}
|
||||
onPointerUp={handlers.onPointerUp}
|
||||
onLostPointerCapture={handlers.onLostPointerCapture}
|
||||
onKeyDown={isKeyboardHandle ? handlers.onKeyDown : undefined}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** Stands in for the app in the chat flow while it is detached into PiP. */
|
||||
export function PipPlaceholder({ height, onReturn }: { height: number; onReturn: () => void }) {
|
||||
const intl = useIntl();
|
||||
|
||||
return (
|
||||
<div
|
||||
className="mt-6 mb-2 flex items-center justify-center rounded-lg border border-dashed border-border-primary bg-black/[0.02] dark:bg-white/[0.02]"
|
||||
style={{ width: '100%', height: `${height}px` }}
|
||||
>
|
||||
<button
|
||||
onClick={onReturn}
|
||||
className="cursor-pointer flex items-center gap-2 rounded-md px-3 py-1.5 text-xs text-text-secondary transition-colors hover:bg-black/5 hover:text-text-primary dark:hover:bg-white/5"
|
||||
>
|
||||
<PictureInPicture2 size={14} />
|
||||
<span>{intl.formatMessage(i18n.playingInPip)}</span>
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { render, waitFor } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { IntlTestWrapper } from '../../../i18n/test-utils';
|
||||
import McpAppRenderer from '../McpAppRenderer';
|
||||
import type { GooseDisplayMode } from '../types';
|
||||
|
||||
vi.mock('@mcp-ui/client', () => ({
|
||||
AppBridge: class {
|
||||
onmessage = null;
|
||||
connect = vi.fn(() => new Promise(() => {}));
|
||||
close = vi.fn();
|
||||
},
|
||||
PostMessageTransport: class {},
|
||||
}));
|
||||
|
||||
vi.mock('../../../acp/mcp-apps', () => ({
|
||||
readMcpAppResource: vi.fn(async () => ({ text: '<html></html>', _meta: {} })),
|
||||
callMcpAppTool: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../../contexts/ThemeContext', () => ({
|
||||
useTheme: () => ({ resolvedTheme: 'light', mcpHostStyles: {} }),
|
||||
}));
|
||||
|
||||
vi.mock('../../settings/extensions/subcomponents/ExtensionList', () => ({
|
||||
formatExtensionName: (name: string) => name,
|
||||
}));
|
||||
|
||||
/**
|
||||
* Display modes must never remount the app iframe: a remount reloads the app
|
||||
* and loses its state. The shell around the iframe therefore keeps the same
|
||||
* element types in the same positions in every mode, and mode-specific chrome
|
||||
* renders as siblings, never as ancestors, of the app container.
|
||||
*/
|
||||
describe('McpAppRenderer display modes', () => {
|
||||
const electron = window.electron as unknown as Record<string, unknown>;
|
||||
|
||||
beforeEach(() => {
|
||||
electron.getAcpUrl = vi.fn(async () => 'ws://127.0.0.1:3000/acp');
|
||||
electron.getSecretKey = vi.fn(async () => 'secret');
|
||||
window.matchMedia = vi
|
||||
.fn()
|
||||
.mockReturnValue({ matches: false }) as unknown as typeof window.matchMedia;
|
||||
window.ResizeObserver = class {
|
||||
observe() {}
|
||||
unobserve() {}
|
||||
disconnect() {}
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete electron.getAcpUrl;
|
||||
delete electron.getSecretKey;
|
||||
});
|
||||
|
||||
function renderApp(displayMode: GooseDisplayMode) {
|
||||
return (
|
||||
<McpAppRenderer
|
||||
resourceUri="ui://bench/app"
|
||||
extensionName="bench"
|
||||
sessionId="session-a"
|
||||
displayMode={displayMode}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
it('keeps the same iframe attached through inline, pip and fullscreen', async () => {
|
||||
const { container, rerender } = render(renderApp('inline'), { wrapper: IntlTestWrapper });
|
||||
const iframe = await waitFor(() => {
|
||||
const el = container.querySelector('iframe');
|
||||
expect(el).not.toBeNull();
|
||||
return el as HTMLIFrameElement;
|
||||
});
|
||||
|
||||
const removals: Node[] = [];
|
||||
const observer = new MutationObserver((records) => {
|
||||
for (const record of records) {
|
||||
removals.push(...Array.from(record.removedNodes));
|
||||
}
|
||||
});
|
||||
observer.observe(container, { childList: true, subtree: true });
|
||||
|
||||
for (const mode of ['pip', 'fullscreen', 'inline', 'fullscreen', 'pip', 'inline'] as const) {
|
||||
rerender(renderApp(mode));
|
||||
expect(container.querySelector('iframe')).toBe(iframe);
|
||||
expect(iframe.isConnected).toBe(true);
|
||||
}
|
||||
|
||||
observer.takeRecords().forEach((record) => removals.push(...Array.from(record.removedNodes)));
|
||||
observer.disconnect();
|
||||
const detachedIframe = removals.some(
|
||||
(node) => node === iframe || (node instanceof Element && node.contains(iframe))
|
||||
);
|
||||
expect(detachedIframe).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,313 @@
|
||||
import type React from 'react';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
DEFAULT_PIP_GEOMETRY,
|
||||
PIP_CORNERS,
|
||||
PIP_EDGES,
|
||||
PIP_FRAME_INSET,
|
||||
PIP_MARGIN_BOTTOM,
|
||||
PIP_MARGIN_RIGHT,
|
||||
PIP_MIN_HEIGHT,
|
||||
PIP_MIN_WIDTH,
|
||||
PIP_TITLE_BAR_HEIGHT,
|
||||
clampPipGeometry,
|
||||
clearPipGeometryStore,
|
||||
movePipGeometry,
|
||||
pipResizeZoneRect,
|
||||
pipTopLeft,
|
||||
resizePipGeometry,
|
||||
usePipWindow,
|
||||
} from '../PipWindow';
|
||||
|
||||
const viewport = { width: 1200, height: 900 };
|
||||
|
||||
function corners(geometry: typeof DEFAULT_PIP_GEOMETRY) {
|
||||
const { left, top } = pipTopLeft(geometry, viewport);
|
||||
return { left, top, right: left + geometry.width, bottom: top + geometry.height };
|
||||
}
|
||||
|
||||
describe('clampPipGeometry', () => {
|
||||
it('enforces the minimum size', () => {
|
||||
const result = clampPipGeometry({ x: 0, y: 0, width: 100, height: 50 }, viewport);
|
||||
expect(result.width).toBe(PIP_MIN_WIDTH);
|
||||
expect(result.height).toBe(PIP_MIN_HEIGHT);
|
||||
});
|
||||
|
||||
it('caps the size at the viewport minus the frame inset', () => {
|
||||
const result = clampPipGeometry({ x: 0, y: 0, width: 5000, height: 5000 }, viewport);
|
||||
expect(result.width).toBe(viewport.width - PIP_FRAME_INSET.left - PIP_FRAME_INSET.right);
|
||||
expect(result.height).toBe(viewport.height - PIP_FRAME_INSET.top - PIP_FRAME_INSET.bottom);
|
||||
});
|
||||
|
||||
it('keeps the frame inside the viewport on every edge', () => {
|
||||
const farLeftUp = clampPipGeometry({ ...DEFAULT_PIP_GEOMETRY, x: -5000, y: -5000 }, viewport);
|
||||
expect(pipTopLeft(farLeftUp, viewport)).toEqual({
|
||||
left: PIP_FRAME_INSET.left,
|
||||
top: PIP_FRAME_INSET.top,
|
||||
});
|
||||
|
||||
const farRightDown = clampPipGeometry({ ...DEFAULT_PIP_GEOMETRY, x: 5000, y: 5000 }, viewport);
|
||||
expect(farRightDown.x).toBe(PIP_MARGIN_RIGHT - PIP_FRAME_INSET.right);
|
||||
expect(farRightDown.y).toBe(PIP_MARGIN_BOTTOM - PIP_FRAME_INSET.bottom);
|
||||
});
|
||||
|
||||
it('falls back to the anchored offset when the viewport is smaller than the window', () => {
|
||||
const tiny = { width: 300, height: 200 };
|
||||
const result = clampPipGeometry({ x: 50, y: 50, width: 800, height: 600 }, tiny);
|
||||
expect(result).toEqual({ x: 0, y: 0, width: PIP_MIN_WIDTH, height: PIP_MIN_HEIGHT });
|
||||
});
|
||||
});
|
||||
|
||||
describe('movePipGeometry', () => {
|
||||
it('shifts position without changing size and clamps to the viewport', () => {
|
||||
expect(movePipGeometry(DEFAULT_PIP_GEOMETRY, -100, -50, viewport)).toEqual({
|
||||
...DEFAULT_PIP_GEOMETRY,
|
||||
x: -100,
|
||||
y: -50,
|
||||
});
|
||||
|
||||
const clamped = movePipGeometry(DEFAULT_PIP_GEOMETRY, 1000, 1000, viewport);
|
||||
expect(clamped.x).toBe(PIP_MARGIN_RIGHT - PIP_FRAME_INSET.right);
|
||||
expect(clamped.y).toBe(PIP_MARGIN_BOTTOM - PIP_FRAME_INSET.bottom);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resizePipGeometry', () => {
|
||||
const movedAwayFromCorner = { ...DEFAULT_PIP_GEOMETRY, x: -300, y: -200 };
|
||||
|
||||
it('grows from each corner while keeping the opposite corner fixed', () => {
|
||||
const before = corners(movedAwayFromCorner);
|
||||
for (const corner of PIP_CORNERS) {
|
||||
const dx = corner.endsWith('left') ? -120 : 120;
|
||||
const dy = corner.startsWith('top') ? -80 : 80;
|
||||
const after = corners(resizePipGeometry(movedAwayFromCorner, dx, dy, viewport, corner));
|
||||
|
||||
expect(after.right - after.left).toBe(PIP_MIN_WIDTH + 120);
|
||||
expect(after.bottom - after.top).toBe(PIP_MIN_HEIGHT + 80);
|
||||
expect(after[corner.endsWith('left') ? 'right' : 'left']).toBe(
|
||||
before[corner.endsWith('left') ? 'right' : 'left']
|
||||
);
|
||||
expect(after[corner.startsWith('top') ? 'bottom' : 'top']).toBe(
|
||||
before[corner.startsWith('top') ? 'bottom' : 'top']
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('resizes a single axis from each edge, keeping the opposite edge fixed', () => {
|
||||
const before = corners(movedAwayFromCorner);
|
||||
|
||||
const top = corners(resizePipGeometry(movedAwayFromCorner, 999, -50, viewport, 'top'));
|
||||
expect(top).toEqual({ ...before, top: before.top - 50 });
|
||||
|
||||
const bottom = corners(resizePipGeometry(movedAwayFromCorner, 999, 50, viewport, 'bottom'));
|
||||
expect(bottom).toEqual({ ...before, bottom: before.bottom + 50 });
|
||||
|
||||
const left = corners(resizePipGeometry(movedAwayFromCorner, -50, 999, viewport, 'left'));
|
||||
expect(left).toEqual({ ...before, left: before.left - 50 });
|
||||
|
||||
const right = corners(resizePipGeometry(movedAwayFromCorner, 50, 999, viewport, 'right'));
|
||||
expect(right).toEqual({ ...before, right: before.right + 50 });
|
||||
});
|
||||
|
||||
it('never shrinks below the minimum size from any corner', () => {
|
||||
for (const corner of PIP_CORNERS) {
|
||||
const shrunk = resizePipGeometry(
|
||||
movedAwayFromCorner,
|
||||
corner.endsWith('left') ? 500 : -500,
|
||||
corner.startsWith('top') ? 500 : -500,
|
||||
viewport,
|
||||
corner
|
||||
);
|
||||
expect(shrunk.width).toBe(PIP_MIN_WIDTH);
|
||||
expect(shrunk.height).toBe(PIP_MIN_HEIGHT);
|
||||
}
|
||||
});
|
||||
|
||||
it('stops growing at the viewport edges without moving the fixed corner', () => {
|
||||
const before = pipTopLeft(DEFAULT_PIP_GEOMETRY, viewport);
|
||||
const result = resizePipGeometry(DEFAULT_PIP_GEOMETRY, 5000, 5000, viewport, 'bottom-right');
|
||||
const after = pipTopLeft(result, viewport);
|
||||
|
||||
expect(after).toEqual(before);
|
||||
expect(after.left + result.width).toBe(viewport.width - PIP_FRAME_INSET.right);
|
||||
expect(after.top + result.height).toBe(viewport.height - PIP_FRAME_INSET.bottom);
|
||||
|
||||
const topLeft = corners(
|
||||
resizePipGeometry(movedAwayFromCorner, -5000, -5000, viewport, 'top-left')
|
||||
);
|
||||
expect(topLeft.left).toBe(PIP_FRAME_INSET.left);
|
||||
expect(topLeft.top).toBe(PIP_FRAME_INSET.top);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pipResizeZoneRect', () => {
|
||||
const geometry = DEFAULT_PIP_GEOMETRY;
|
||||
// The resizable window is the title bar plus the panel.
|
||||
const panel = {
|
||||
left: PIP_FRAME_INSET.left,
|
||||
top: PIP_FRAME_INSET.top - PIP_TITLE_BAR_HEIGHT,
|
||||
right: PIP_FRAME_INSET.left + geometry.width,
|
||||
bottom: PIP_FRAME_INSET.top + geometry.height,
|
||||
};
|
||||
|
||||
function center(rect: { left: number; top: number; width: number; height: number }) {
|
||||
return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
|
||||
}
|
||||
|
||||
it('centers corner zones on the window corners and edge strips on the edges', () => {
|
||||
expect(center(pipResizeZoneRect('top-left', geometry))).toEqual({
|
||||
x: panel.left,
|
||||
y: panel.top,
|
||||
});
|
||||
expect(center(pipResizeZoneRect('bottom-right', geometry))).toEqual({
|
||||
x: panel.right,
|
||||
y: panel.bottom,
|
||||
});
|
||||
for (const edge of PIP_EDGES) {
|
||||
const c = center(pipResizeZoneRect(edge, geometry));
|
||||
if (edge === 'top' || edge === 'bottom') {
|
||||
expect(c.y).toBe(edge === 'top' ? panel.top : panel.bottom);
|
||||
} else {
|
||||
expect(c.x).toBe(edge === 'left' ? panel.left : panel.right);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps every zone inside the frame without overlapping the corners', () => {
|
||||
const frame = {
|
||||
width: geometry.width + PIP_FRAME_INSET.left + PIP_FRAME_INSET.right,
|
||||
height: geometry.height + PIP_FRAME_INSET.top + PIP_FRAME_INSET.bottom,
|
||||
};
|
||||
for (const handle of [...PIP_EDGES, ...PIP_CORNERS]) {
|
||||
const rect = pipResizeZoneRect(handle, geometry);
|
||||
expect(rect.left).toBeGreaterThanOrEqual(0);
|
||||
expect(rect.top).toBeGreaterThanOrEqual(0);
|
||||
expect(rect.left + rect.width).toBeLessThanOrEqual(frame.width);
|
||||
expect(rect.top + rect.height).toBeLessThanOrEqual(frame.height);
|
||||
}
|
||||
const topLeft = pipResizeZoneRect('top-left', geometry);
|
||||
expect(pipResizeZoneRect('top', geometry).left).toBe(topLeft.left + topLeft.width);
|
||||
});
|
||||
});
|
||||
|
||||
describe('usePipWindow', () => {
|
||||
function setViewport(width: number, height: number) {
|
||||
Object.defineProperty(window, 'innerWidth', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: width,
|
||||
});
|
||||
Object.defineProperty(window, 'innerHeight', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: height,
|
||||
});
|
||||
}
|
||||
|
||||
function pointerEvent(clientX: number, clientY: number): React.PointerEvent {
|
||||
return {
|
||||
pointerId: 1,
|
||||
clientX,
|
||||
clientY,
|
||||
preventDefault: vi.fn(),
|
||||
currentTarget: { setPointerCapture: vi.fn(), releasePointerCapture: vi.fn() },
|
||||
} as unknown as React.PointerEvent;
|
||||
}
|
||||
|
||||
function keyEvent(key: string, shiftKey = false): React.KeyboardEvent {
|
||||
return { key, shiftKey, preventDefault: vi.fn() } as unknown as React.KeyboardEvent;
|
||||
}
|
||||
|
||||
function renderPip(sessionId: string | null = 'session-a') {
|
||||
return renderHook((props: { active: boolean }) => usePipWindow({ ...props, sessionId }), {
|
||||
initialProps: { active: true },
|
||||
});
|
||||
}
|
||||
|
||||
/** Drags the window up and left so there is room to grow from the corner. */
|
||||
function moveAwayFromCorner(hook: ReturnType<typeof renderPip>) {
|
||||
act(() => hook.result.current.moveHandlers.onPointerDown(pointerEvent(800, 600)));
|
||||
act(() => hook.result.current.moveHandlers.onPointerMove(pointerEvent(500, 400)));
|
||||
act(() => hook.result.current.moveHandlers.onPointerUp(pointerEvent(500, 400)));
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
clearPipGeometryStore();
|
||||
setViewport(1200, 900);
|
||||
});
|
||||
|
||||
it('applies pointer drags as deltas from the pointer-down position', () => {
|
||||
const hook = renderPip();
|
||||
moveAwayFromCorner(hook);
|
||||
const { result } = hook;
|
||||
const before = pipTopLeft(result.current.geometry, viewport);
|
||||
|
||||
act(() => result.current.resizeHandlers['bottom-right'].onPointerDown(pointerEvent(1000, 700)));
|
||||
act(() => result.current.resizeHandlers['bottom-right'].onPointerMove(pointerEvent(1150, 780)));
|
||||
act(() => result.current.resizeHandlers['bottom-right'].onPointerUp(pointerEvent(1150, 780)));
|
||||
|
||||
expect(result.current.geometry.width).toBe(PIP_MIN_WIDTH + 150);
|
||||
expect(result.current.geometry.height).toBe(PIP_MIN_HEIGHT + 80);
|
||||
expect(pipTopLeft(result.current.geometry, viewport)).toEqual(before);
|
||||
});
|
||||
|
||||
it('steps with arrow keys, 8px or 32px with shift, and never drops below the minimum', () => {
|
||||
const hook = renderPip();
|
||||
moveAwayFromCorner(hook);
|
||||
const { result } = hook;
|
||||
const handle = () => result.current.resizeHandlers['bottom-right'];
|
||||
|
||||
act(() => handle().onKeyDown(keyEvent('ArrowRight', true)));
|
||||
act(() => handle().onKeyDown(keyEvent('ArrowDown')));
|
||||
expect(result.current.geometry.width).toBe(PIP_MIN_WIDTH + 32);
|
||||
expect(result.current.geometry.height).toBe(PIP_MIN_HEIGHT + 8);
|
||||
|
||||
act(() => handle().onKeyDown(keyEvent('ArrowLeft', true)));
|
||||
act(() => handle().onKeyDown(keyEvent('ArrowLeft', true)));
|
||||
act(() => handle().onKeyDown(keyEvent('ArrowUp')));
|
||||
act(() => handle().onKeyDown(keyEvent('ArrowUp')));
|
||||
expect(result.current.geometry.width).toBe(PIP_MIN_WIDTH);
|
||||
expect(result.current.geometry.height).toBe(PIP_MIN_HEIGHT);
|
||||
});
|
||||
|
||||
it('re-clamps when the main window shrinks', () => {
|
||||
const hook = renderPip();
|
||||
moveAwayFromCorner(hook);
|
||||
const { result } = hook;
|
||||
|
||||
for (let i = 0; i < 3; i++) {
|
||||
act(() =>
|
||||
result.current.resizeHandlers['bottom-right'].onKeyDown(keyEvent('ArrowRight', true))
|
||||
);
|
||||
}
|
||||
expect(result.current.geometry.width).toBe(PIP_MIN_WIDTH + 96);
|
||||
|
||||
setViewport(450, 900);
|
||||
act(() => {
|
||||
window.dispatchEvent(new Event('resize'));
|
||||
});
|
||||
|
||||
expect(result.current.geometry.width).toBe(450 - PIP_FRAME_INSET.left - PIP_FRAME_INSET.right);
|
||||
const { left } = pipTopLeft(result.current.geometry, { width: 450, height: 900 });
|
||||
expect(left).toBeGreaterThanOrEqual(PIP_FRAME_INSET.left);
|
||||
});
|
||||
|
||||
it('reopens at the last geometry within a session and at the defaults for a new session', () => {
|
||||
const first = renderPip('session-a');
|
||||
act(() =>
|
||||
first.result.current.resizeHandlers['bottom-right'].onKeyDown(keyEvent('ArrowRight', true))
|
||||
);
|
||||
act(() => first.result.current.moveHandlers.onKeyDown(keyEvent('ArrowUp', true)));
|
||||
const remembered = first.result.current.geometry;
|
||||
expect(remembered).not.toEqual(DEFAULT_PIP_GEOMETRY);
|
||||
|
||||
first.rerender({ active: false });
|
||||
first.rerender({ active: true });
|
||||
expect(first.result.current.geometry).toEqual(remembered);
|
||||
first.unmount();
|
||||
|
||||
expect(renderPip('session-a').result.current.geometry).toEqual(remembered);
|
||||
expect(renderPip('session-b').result.current.geometry).toEqual(DEFAULT_PIP_GEOMETRY);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { getContainerDimensions } from '../containerDimensions';
|
||||
|
||||
describe('getContainerDimensions', () => {
|
||||
it('reports only width for inline so the guest controls its height', () => {
|
||||
expect(getContainerDimensions('inline', 640, 200)).toEqual({ width: 640 });
|
||||
expect(getContainerDimensions('inline', 640, 0)).toEqual({ width: 640 });
|
||||
});
|
||||
|
||||
it('reports width and height for modes that fill the viewport', () => {
|
||||
expect(getContainerDimensions('fullscreen', 1280, 752)).toEqual({ width: 1280, height: 752 });
|
||||
expect(getContainerDimensions('standalone', 900, 600)).toEqual({ width: 900, height: 600 });
|
||||
});
|
||||
|
||||
it('reports width and maxHeight for pip because the pip window scrolls its content', () => {
|
||||
expect(getContainerDimensions('pip', 398, 298)).toEqual({ width: 398, maxHeight: 298 });
|
||||
});
|
||||
|
||||
it('returns undefined until a fixed axis has been measured', () => {
|
||||
expect(getContainerDimensions('inline', 0, 200)).toBeUndefined();
|
||||
expect(getContainerDimensions('fullscreen', 1280, 0)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { McpUiHostContext } from '@modelcontextprotocol/ext-apps/app-bridge';
|
||||
import type { DimensionLayout, GooseDisplayMode } from './types';
|
||||
|
||||
export const DISPLAY_MODE_LAYOUTS: Record<GooseDisplayMode, DimensionLayout> = {
|
||||
inline: { width: 'fixed', height: 'unbounded' },
|
||||
fullscreen: { width: 'fixed', height: 'fixed' },
|
||||
standalone: { width: 'fixed', height: 'fixed' },
|
||||
pip: { width: 'fixed', height: 'flexible' },
|
||||
};
|
||||
|
||||
export function getContainerDimensions(
|
||||
displayMode: GooseDisplayMode,
|
||||
measuredWidth: number,
|
||||
measuredHeight: number
|
||||
): McpUiHostContext['containerDimensions'] {
|
||||
const layout = DISPLAY_MODE_LAYOUTS[displayMode] ?? DISPLAY_MODE_LAYOUTS.inline;
|
||||
|
||||
// Only require a measurement for axes that are fixed or flexible (unbounded axes are omitted).
|
||||
if (
|
||||
(layout.width !== 'unbounded' && measuredWidth <= 0) ||
|
||||
(layout.height !== 'unbounded' && measuredHeight <= 0)
|
||||
)
|
||||
return undefined;
|
||||
|
||||
const widthDimension = (() => {
|
||||
switch (layout.width) {
|
||||
case 'fixed':
|
||||
return { width: measuredWidth };
|
||||
case 'flexible':
|
||||
return { maxWidth: measuredWidth };
|
||||
case 'unbounded':
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
const heightDimension = (() => {
|
||||
switch (layout.height) {
|
||||
case 'fixed':
|
||||
return { height: measuredHeight };
|
||||
case 'flexible':
|
||||
return { maxHeight: measuredHeight };
|
||||
case 'unbounded':
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
return { ...widthDimension, ...heightDimension };
|
||||
}
|
||||
@@ -2,8 +2,9 @@
|
||||
* useDisplayMode — Manages display mode state for MCP App containers.
|
||||
*
|
||||
* Encapsulates the display mode state machine, capability negotiation,
|
||||
* PiP drag handling, entrance animations, and postMessage interception
|
||||
* for ui/initialize and ui/request-display-mode.
|
||||
* entrance animations, and postMessage interception for ui/initialize and
|
||||
* ui/request-display-mode. Mode-specific chrome and geometry live with each
|
||||
* mode's component (see PipWindow.tsx).
|
||||
*/
|
||||
|
||||
import type { McpUiDisplayMode } from '@modelcontextprotocol/ext-apps/app-bridge';
|
||||
@@ -14,12 +15,6 @@ const DEFAULT_IFRAME_HEIGHT = 200;
|
||||
|
||||
const AVAILABLE_DISPLAY_MODES: McpUiDisplayMode[] = ['inline', 'fullscreen', 'pip'];
|
||||
|
||||
const PIP_WIDTH = 400;
|
||||
const PIP_HEIGHT = 300;
|
||||
const PIP_MARGIN_RIGHT = 16;
|
||||
// Keeps the PiP window above the chat input area (~120px) plus padding.
|
||||
const PIP_MARGIN_BOTTOM = 140;
|
||||
|
||||
interface UseDisplayModeOptions {
|
||||
displayMode: GooseDisplayMode;
|
||||
onDisplayModeChange?: OnDisplayModeChange;
|
||||
@@ -43,23 +38,11 @@ export interface DisplayModeState {
|
||||
/** Remembered inline height for placeholders when detached. */
|
||||
inlineHeight: number;
|
||||
|
||||
/** PiP position offset from the default bottom-right corner. */
|
||||
pipPosition: { x: number; y: number };
|
||||
|
||||
/** PiP drag handle event handlers. */
|
||||
pipHandlers: {
|
||||
onPointerDown: (e: React.PointerEvent) => void;
|
||||
onPointerMove: (e: React.PointerEvent) => void;
|
||||
onPointerUp: (e: React.PointerEvent) => void;
|
||||
onLostPointerCapture: () => void;
|
||||
onKeyDown: (e: React.KeyboardEvent) => void;
|
||||
};
|
||||
|
||||
/** Ref for the fullscreen close button (auto-focused on enter). */
|
||||
fullscreenCloseRef: React.RefObject<HTMLButtonElement | null>;
|
||||
}
|
||||
|
||||
export { AVAILABLE_DISPLAY_MODES, PIP_WIDTH, PIP_HEIGHT, PIP_MARGIN_RIGHT, PIP_MARGIN_BOTTOM };
|
||||
export { AVAILABLE_DISPLAY_MODES };
|
||||
|
||||
export function useDisplayMode({
|
||||
displayMode,
|
||||
@@ -145,90 +128,6 @@ export function useDisplayMode({
|
||||
[onDisplayModeChange, activeDisplayMode, containerRef]
|
||||
);
|
||||
|
||||
// ── PiP drag ──────────────────────────────────────────────────────────
|
||||
|
||||
const [pipPosition, setPipPosition] = useState({ x: 0, y: 0 });
|
||||
const pipPositionRef = useRef(pipPosition);
|
||||
const pipDragRef = useRef<{
|
||||
startX: number;
|
||||
startY: number;
|
||||
originX: number;
|
||||
originY: number;
|
||||
} | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
pipPositionRef.current = pipPosition;
|
||||
}, [pipPosition]);
|
||||
|
||||
const clampPipPosition = useCallback((pos: { x: number; y: number }) => {
|
||||
const minX = PIP_WIDTH + PIP_MARGIN_RIGHT - window.innerWidth;
|
||||
const maxX = PIP_MARGIN_RIGHT;
|
||||
const minY = PIP_HEIGHT + PIP_MARGIN_BOTTOM - window.innerHeight;
|
||||
const maxY = PIP_MARGIN_BOTTOM;
|
||||
return {
|
||||
x: minX > maxX ? 0 : Math.max(minX, Math.min(maxX, pos.x)),
|
||||
y: minY > maxY ? 0 : Math.max(minY, Math.min(maxY, pos.y)),
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handlePipPointerDown = useCallback((e: React.PointerEvent) => {
|
||||
e.preventDefault();
|
||||
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
|
||||
const { x, y } = pipPositionRef.current;
|
||||
pipDragRef.current = { startX: e.clientX, startY: e.clientY, originX: x, originY: y };
|
||||
}, []);
|
||||
|
||||
const handlePipPointerMove = useCallback(
|
||||
(e: React.PointerEvent) => {
|
||||
if (!pipDragRef.current) return;
|
||||
const dx = e.clientX - pipDragRef.current.startX;
|
||||
const dy = e.clientY - pipDragRef.current.startY;
|
||||
setPipPosition(
|
||||
clampPipPosition({
|
||||
x: pipDragRef.current.originX + dx,
|
||||
y: pipDragRef.current.originY + dy,
|
||||
})
|
||||
);
|
||||
},
|
||||
[clampPipPosition]
|
||||
);
|
||||
|
||||
const handlePipPointerUp = useCallback((e: React.PointerEvent) => {
|
||||
(e.currentTarget as HTMLElement).releasePointerCapture(e.pointerId);
|
||||
pipDragRef.current = null;
|
||||
}, []);
|
||||
|
||||
const handlePipLostPointerCapture = useCallback(() => {
|
||||
pipDragRef.current = null;
|
||||
}, []);
|
||||
|
||||
const handlePipKeyDown = useCallback(
|
||||
(e: React.KeyboardEvent) => {
|
||||
const step = e.shiftKey ? 32 : 8;
|
||||
let dx = 0;
|
||||
let dy = 0;
|
||||
switch (e.key) {
|
||||
case 'ArrowUp':
|
||||
dy = -step;
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
dy = step;
|
||||
break;
|
||||
case 'ArrowLeft':
|
||||
dx = -step;
|
||||
break;
|
||||
case 'ArrowRight':
|
||||
dx = step;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
e.preventDefault();
|
||||
setPipPosition((prev) => clampPipPosition({ x: prev.x + dx, y: prev.y + dy }));
|
||||
},
|
||||
[clampPipPosition]
|
||||
);
|
||||
|
||||
// ── Effects ───────────────────────────────────────────────────────────
|
||||
|
||||
// Cache iframe contentWindows for O(1) source matching via MutationObserver.
|
||||
@@ -300,13 +199,6 @@ export function useDisplayMode({
|
||||
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||
}, [activeDisplayMode, changeDisplayMode]);
|
||||
|
||||
// Reset PiP position when entering PiP mode.
|
||||
useEffect(() => {
|
||||
if (activeDisplayMode === 'pip') {
|
||||
setPipPosition({ x: 0, y: 0 });
|
||||
}
|
||||
}, [activeDisplayMode]);
|
||||
|
||||
// ── Derived state ─────────────────────────────────────────────────────
|
||||
|
||||
const isFullscreen = activeDisplayMode === 'fullscreen';
|
||||
@@ -332,15 +224,6 @@ export function useDisplayMode({
|
||||
changeDisplayMode,
|
||||
|
||||
inlineHeight: savedInlineHeight,
|
||||
pipPosition,
|
||||
|
||||
pipHandlers: {
|
||||
onPointerDown: handlePipPointerDown,
|
||||
onPointerMove: handlePipPointerMove,
|
||||
onPointerUp: handlePipPointerUp,
|
||||
onLostPointerCapture: handlePipLostPointerCapture,
|
||||
onKeyDown: handlePipKeyDown,
|
||||
},
|
||||
|
||||
fullscreenCloseRef,
|
||||
};
|
||||
|
||||
@@ -1962,7 +1962,7 @@
|
||||
"defaultMessage": "Ungültige URL"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Bild-in-Bild-Fenster verschieben (Pfeiltasten verwenden)"
|
||||
"defaultMessage": "Bild-in-Bild-Fenster verschieben"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Bild-in-Bild"
|
||||
@@ -1970,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Wiedergabe im Bild-in-Bild-Modus"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Bild-in-Bild-Fenster in der Größe ändern"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# Element gefunden} other{# Elemente gefunden}}"
|
||||
},
|
||||
|
||||
@@ -1983,7 +1983,7 @@
|
||||
"defaultMessage": "Invalid URL"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Move Picture-in-Picture window (use arrow keys)"
|
||||
"defaultMessage": "Move Picture-in-Picture window"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Picture-in-Picture"
|
||||
@@ -1991,6 +1991,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Playing in Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Resize Picture-in-Picture window"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# item found} other{# items found}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "No se pudo actualizar el directorio de trabajo"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "URL no válida"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Mover la ventana de imagen en imagen (usa las teclas de flecha)"
|
||||
"defaultMessage": "Mover la ventana de imagen en imagen"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Imagen en imagen"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Reproduciendo en imagen en imagen"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Cambiar el tamaño de la ventana de imagen en imagen"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# elemento encontrado} other{# elementos encontrados}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "Échec de la mise à jour du répertoire de travail"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "URL non valide"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Déplacer la fenêtre Picture-in-Picture (utilisez les touches fléchées)"
|
||||
"defaultMessage": "Déplacer la fenêtre Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Picture-in-Picture"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Lecture en Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Redimensionner la fenêtre Picture-in-Picture"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# élément trouvé} other{# éléments trouvés}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "कार्यशील निर्देशिका अद्यतन करने में विफल"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "अमान्य URL"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "पिक्चर-इन-पिक्चर विंडो को स्थानांतरित करें (तीर कुंजियों का उपयोग करें)"
|
||||
"defaultMessage": "पिक्चर-इन-पिक्चर विंडो को स्थानांतरित करें"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "चित्र-में-चित्र"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "पिक्चर-इन-पिक्चर में बजाना"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "पिक्चर-इन-पिक्चर विंडो का आकार बदलें"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# आइटम मिला} other{# आइटम मिले}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "Gagal memperbarui direktori kerja"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "URL tidak valid"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Pindahkan jendela Picture-in-Picture (gunakan tombol panah)"
|
||||
"defaultMessage": "Pindahkan jendela Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Picture-in-Picture"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Memutar dalam Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Ubah ukuran jendela Picture-in-Picture"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# item ditemukan} other{# item ditemukan}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "Impossibile aggiornare la cartella di lavoro"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "URL non valido"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Sposta la finestra Picture-in-Picture (usa i tasti freccia)"
|
||||
"defaultMessage": "Sposta la finestra Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Picture-in-Picture"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "In riproduzione in Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Ridimensiona la finestra Picture-in-Picture"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# elemento trovato} other{# elementi trovati}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "作業ディレクトリの更新に失敗しました"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "無効なURL"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "ピクチャ・イン・ピクチャウィンドウを移動(矢印キーを使用)"
|
||||
"defaultMessage": "ピクチャ・イン・ピクチャウィンドウを移動"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "ピクチャ・イン・ピクチャ"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "ピクチャ・イン・ピクチャで再生中"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "ピクチャ・イン・ピクチャウィンドウのサイズを変更"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# 件見つかりました} other{# 件見つかりました}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "작업 디렉터리를 업데이트하지 못했습니다."
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "유효하지 않은 URL"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Picture-in-Picture 창 이동(화살표 키 사용)"
|
||||
"defaultMessage": "Picture-in-Picture 창 이동"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "PIP(Picture-in-Picture)"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "PIP(Picture-in-Picture)에서 재생 중"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Picture-in-Picture 창 크기 조절"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{#개 항목 발견} other{#개 항목 발견}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "Gagal mengemas kini direktori kerja"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "URL tidak sah"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Alihkan tetingkap Gambar-dalam-Gambar (gunakan kekunci anak panah)"
|
||||
"defaultMessage": "Alihkan tetingkap Gambar-dalam-Gambar"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Gambar-dalam-Gambar"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Bermain dalam Gambar-dalam-Gambar"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Ubah saiz tetingkap Gambar-dalam-Gambar"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# item dijumpai} other{# item dijumpai}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "Falha ao atualizar o diretório de trabalho"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "URL inválida"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Mover a janela Picture-in-Picture (use as teclas de seta)"
|
||||
"defaultMessage": "Mover a janela Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Picture-in-Picture"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Reproduzindo em Picture-in-Picture"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Redimensionar a janela Picture-in-Picture"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# item encontrado} other{# itens encontrados}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "Не удалось обновить рабочий каталог"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "Неверный URL"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Переместить окно «картинка в картинке» (используйте стрелки)"
|
||||
"defaultMessage": "Переместить окно «картинка в картинке»"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Картинка в картинке"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Воспроизведение в режиме «картинка в картинке»"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Изменить размер окна «картинка в картинке»"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count, plural, one {Найден # элемент} few {Найдено # элемента} many {Найдено # элементов} other {Найдено # элемента}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "Çalışma dizini güncellenemedi"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "Geçersiz URL"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Resim İçinde Resim penceresini taşıyın (ok tuşlarını kullanın)"
|
||||
"defaultMessage": "Resim İçinde Resim penceresini taşıyın"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Resim İçinde Resim"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Resim İçinde Resim'de Oynatma"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Resim İçinde Resim penceresini yeniden boyutlandırın"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# öğe bulundu} other{# öğe bulundu}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "Không thể cập nhật thư mục làm việc"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "URL không hợp lệ"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "Di chuyển cửa sổ Hình-trong-hình (dùng phím mũi tên)"
|
||||
"defaultMessage": "Di chuyển cửa sổ Hình-trong-hình"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "Hình-trong-hình"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "Đang phát ở chế độ Hình-trong-hình"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "Thay đổi kích cỡ cửa sổ Hình-trong-hình"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{# mục được tìm thấy} other{# mục được tìm thấy}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "更新工作目录失败"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "URL 无效"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "移动画中画窗口(使用方向键)"
|
||||
"defaultMessage": "移动画中画窗口"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "画中画"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "正在以画中画播放"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "调整画中画窗口大小"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{找到 # 项} other{找到 # 项}}"
|
||||
},
|
||||
|
||||
@@ -929,10 +929,6 @@
|
||||
"dirSwitcher.enterPathInvalid": {
|
||||
"defaultMessage": "Working directory must be an absolute path"
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
"dirSwitcher.failedToUpdateWorkingDir": {
|
||||
"defaultMessage": "無法更新工作目錄"
|
||||
},
|
||||
@@ -1966,7 +1962,7 @@
|
||||
"defaultMessage": "無效的 URL"
|
||||
},
|
||||
"mcpAppRenderer.movePipWindow": {
|
||||
"defaultMessage": "移動子母畫面視窗(使用方向鍵)"
|
||||
"defaultMessage": "移動子母畫面視窗"
|
||||
},
|
||||
"mcpAppRenderer.pictureInPicture": {
|
||||
"defaultMessage": "子母畫面"
|
||||
@@ -1974,6 +1970,9 @@
|
||||
"mcpAppRenderer.playingInPip": {
|
||||
"defaultMessage": "正在子母畫面中播放"
|
||||
},
|
||||
"mcpAppRenderer.resizePipWindow": {
|
||||
"defaultMessage": "調整子母畫面視窗大小"
|
||||
},
|
||||
"mentionPopover.itemsFound": {
|
||||
"defaultMessage": "{count,plural,one{找到 # 個項目} other{找到 # 個項目}}"
|
||||
},
|
||||
|
||||
@@ -59,7 +59,7 @@ import type { GooseApp } from './types/apps';
|
||||
import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer';
|
||||
import { WEB_PROTOCOLS } from './utils/urlSecurity';
|
||||
import { openExternalUrl } from './utils/openExternalUrl';
|
||||
import { buildCSP, leaseBackendOrigin } from './utils/csp';
|
||||
import { buildCSP, leaseBackendOrigin, shouldApplyRendererCsp } from './utils/csp';
|
||||
import { resolveWorkingDir } from './utils/workingDir';
|
||||
import {
|
||||
DesktopFileAccess,
|
||||
@@ -2482,9 +2482,13 @@ async function appMain() {
|
||||
}
|
||||
});
|
||||
|
||||
// Add CSP headers to all sessions, recomputed on every response so external
|
||||
// backend settings take effect without restarting the app.
|
||||
// Add CSP headers to the renderer's own documents, recomputed on every
|
||||
// response so external backend settings take effect without restarting the app.
|
||||
rendererSession.webRequest.onHeadersReceived((details, callback) => {
|
||||
if (!shouldApplyRendererCsp(details.resourceType)) {
|
||||
callback({});
|
||||
return;
|
||||
}
|
||||
const currentSettings = getSettings();
|
||||
callback({
|
||||
responseHeaders: {
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { buildConnectSrc, shouldUpgradeInsecureRequests, buildCSP } from '../csp';
|
||||
import {
|
||||
buildConnectSrc,
|
||||
shouldUpgradeInsecureRequests,
|
||||
shouldApplyRendererCsp,
|
||||
buildCSP,
|
||||
} from '../csp';
|
||||
import type { ExternalBackendConfig } from '../settings';
|
||||
|
||||
describe('buildConnectSrc', () => {
|
||||
@@ -146,3 +151,18 @@ describe('buildCSP', () => {
|
||||
expect(csp).toContain("object-src 'none'");
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldApplyRendererCsp', () => {
|
||||
it('applies to the renderer main frame', () => {
|
||||
expect(shouldApplyRendererCsp('mainFrame')).toBe(true);
|
||||
});
|
||||
|
||||
it('skips iframe documents so MCP app guests keep their own policy', () => {
|
||||
expect(shouldApplyRendererCsp('subFrame')).toBe(false);
|
||||
});
|
||||
|
||||
it('leaves non-document responses unchanged', () => {
|
||||
expect(shouldApplyRendererCsp('script')).toBe(true);
|
||||
expect(shouldApplyRendererCsp('xhr')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -84,6 +84,13 @@ export function shouldUpgradeInsecureRequests(externalBackend?: ExternalBackendC
|
||||
}
|
||||
}
|
||||
|
||||
// Documents goose serves into iframes (the MCP app proxy and guest pages)
|
||||
// already carry a policy built from the app's declared domains. A second
|
||||
// header would intersect with it and block every declared resource domain.
|
||||
export function shouldApplyRendererCsp(resourceType: string): boolean {
|
||||
return resourceType !== 'subFrame';
|
||||
}
|
||||
|
||||
export function buildCSP(externalBackend?: ExternalBackendConfig): string {
|
||||
const connectSrc = buildConnectSrc(externalBackend);
|
||||
const upgradeDirective = shouldUpgradeInsecureRequests(externalBackend)
|
||||
|
||||
Reference in New Issue
Block a user