Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 200 additions & 15 deletions shell/src/app/shell/composer-workspace/composer-dockview.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,75 @@ import {Errors} from '../../debug/errors/errors';
import {RawMessages} from '../../debug/raw-messages/raw-messages';
import {ComposerPanelId} from './composer-panel-id';

/**
* Default container width in pixels used when the host container width is unmeasured
* (e.g., prior to DOM layout calculation or in headless test environments).
* Chosen as 1280px to represent a realistic, standard non-square 16:10 / 16:9 desktop viewport width.
*/
export const DEFAULT_CONTAINER_WIDTH = 1280;

/**
* Default container height in pixels used when the host container height is unmeasured
* (e.g., prior to DOM layout calculation or in headless test environments).
* Chosen as 800px to represent a realistic desktop browser viewport height.
*/
export const DEFAULT_CONTAINER_HEIGHT = 800;

/**
* Initial target ratio of the debug drawer height relative to total container height (~28%).
* Keeps the debug drawer compact at the bottom while preserving the majority of vertical
* screen real estate for the primary interactive A2UI preview and canvas.
*/
const DEBUG_DRAWER_HEIGHT_RATIO = 0.28;

/**
* Maximum proportion of workspace width initially allocated to the Gemini Assistant panel (1/3).
* Ensures the chat assistant stays comfortably readable without crowding or dominating the
* central canvas and preview workspace.
*/
const CHAT_PANEL_MAX_WIDTH_FRACTION = 1 / 3;

/**
* Minimum width constraints for primary panels in pixels.
*/
const CHAT_PANEL_MIN_WIDTH = 200;
const PREVIEW_PANEL_MIN_WIDTH = 300;

/**
* Minimum height constraints for panels in pixels.
*/
const PREVIEW_PANEL_MIN_HEIGHT = 150;
const DEBUG_DRAWER_MIN_HEIGHT = 100;

/**
* Debounce delay in milliseconds before persisting updated Dockview layout to localStorage.
*/
const LAYOUT_SAVE_DEBOUNCE_MS = 1000;

/**
* Initialization options for ComposerDockview.
*/
export interface DockviewManagerInitOptions {
/** The root DOM container element hosting the Dockview component instance. */
rootEl: HTMLElement;
/** Whether the dark theme CSS class should be initially applied. */
isDarkTheme: boolean;
/** Callback invoked whenever the actively focused panel ID changes. */
onActivePanelChange?: (panelId: string | undefined) => void;
}

/**
* Service encapsulating Dockview initialization, dynamic component instantiation,
* layout persistence, DOM resize/tab-overflow handling, and tab event delegation.
*
* Layout Architecture:
* - **Gemini Assistant (Chat)**: Left vertical panel, constrained initially to <= 1/3 viewport width.
* - **Rendered A2UI Preview & A2UI JSON Editor**: Central workspace area split to the right of Chat.
* Both panels are placed side-by-side with an equal 50/50 width split, with "Rendered A2UI Preview"
* on the left and "A2UI JSON Editor" on the right.
* - **Debug Drawer**: Bottom drawer positioned below the Preview (`direction: 'below'`), occupying ~28%
* of container height. Combines "Data Model" (initially active), "Events", "Errors", and "Raw Messages"
* in a single tabbed group (`direction: 'within'`, `inactive: true`).
*/
@Injectable()
export class ComposerDockview {
Expand Down Expand Up @@ -83,6 +143,8 @@ export class ComposerDockview {

/**
* Initializes Dockview, builds layout, and sets up event listeners.
*
* @param options Configuration options including root element, theme, and callbacks.
*/
initialize(options: DockviewManagerInitOptions): void {
const {rootEl, isDarkTheme, onActivePanelChange} = options;
Expand All @@ -96,14 +158,21 @@ export class ComposerDockview {
});

this.dockviewApi.onDidActivePanelChange(event => {
if (!this.isInitialized) return;
onActivePanelChange?.(event.panel?.id);
this.checkTabOverflow();
this.cdr.markForCheck();
});

this.buildDockviewLayout();
// Compute realistic initial viewport dimensions, falling back to desktop defaults if unmeasured
const width = rootEl.clientWidth || DEFAULT_CONTAINER_WIDTH;
const height = rootEl.clientHeight || DEFAULT_CONTAINER_HEIGHT;

const layoutRestored = this.buildDockviewLayout(width, height);

// Debounced layout persistence to localStorage
this.dockviewApi.onDidLayoutChange(() => {
if (!this.isInitialized) return;
this.checkTabOverflow();
if (this.saveTimeout !== undefined) {
clearTimeout(this.saveTimeout);
Expand All @@ -113,7 +182,7 @@ export class ComposerDockview {
LocalStorageKey.DOCKVIEW_LAYOUT,
JSON.stringify(this.dockviewApi.toJSON()),
);
}, 1000);
}, LAYOUT_SAVE_DEBOUNCE_MS);
});

this.dockviewApi.onDidAddPanel(() => this.checkTabOverflow());
Expand All @@ -123,17 +192,19 @@ export class ComposerDockview {
this.resizeObserver.observe(rootEl);

// Initial layout pass
const width = rootEl.clientWidth || 1000;
const height = rootEl.clientHeight || 1000;
this.dockviewApi.layout(width, height);
if (!layoutRestored) {
this.enforceInitialProportions(width, height);
}
this.isInitialized = true;
Comment thread
jgindin marked this conversation as resolved.
Resolved
this.checkTabOverflow();

// Register capture-phase pointerdown and click event delegation
// Register capture-phase pointerdown and click event delegation for robust tab clicks
const handleTabInteraction = (event: Event) => this.handleTabInteraction(event);
rootEl.addEventListener('pointerdown', handleTabInteraction, true);
rootEl.addEventListener('click', handleTabInteraction, true);

// Teardown observers, timers, listeners, and dynamic components on destroy
this.destroyRef.onDestroy(() => {
if (this.animationFrameId !== undefined) {
cancelAnimationFrame(this.animationFrameId);
Expand All @@ -149,36 +220,67 @@ export class ComposerDockview {
});
}

/**
* Checks whether a given panel is currently visible in the active layout.
*
* @param panelId Panel identifier to query.
* @returns `true` if the panel is currently visible.
*/
isPanelVisible(panelId: ComposerPanelId): boolean {
return !!this.dockviewApi?.getGroupPanel(panelId)?.api.isVisible;
}

/**
* Programmatically opens and activates the specified panel tab.
*
* @param panelId Panel identifier to activate.
*/
openPanel(panelId: ComposerPanelId): void {
const panel = this.dockviewApi?.getGroupPanel(panelId);
if (panel && !panel.api.isActive) {
panel.api.setActive();
}
}

/**
* Sets the tab title of a panel.
*
* @param panelId Panel identifier whose title will be updated.
* @param title New title string.
*/
setPanelTitle(panelId: ComposerPanelId, title: string): void {
const panel = this.dockviewApi?.getGroupPanel(panelId);
if (panel) {
panel.api.setTitle(title);
}
}

/**
* Updates the theme stylesheet class attached to the Dockview container.
*
* @param isDark `true` for dark theme, `false` for light theme.
*/
updateTheme(isDark: boolean): void {
this.dockviewApi?.updateOptions({
className: isDark ? 'dockview-theme-dark' : 'dockview-theme-light',
});
}

/**
* Clears accumulated diagnostic logs in the debug child component instances.
*/
clearAllLogs(): void {
this.rawMessagesInstance?.clearLogs();
this.eventsInstance?.clearLogs();
this.errorsInstance?.clearLogs();
}

/**
* Inspects all tab containers to detect horizontal overflow, toggling the
* `.has-tab-overflow` class on the container element for scrolling affordance.
*
* @param element Optional root element to inspect (defaults to `this.rootEl`).
*/
checkTabOverflow(element?: HTMLElement): void {
const targetEl = element ?? this.rootEl;
if (!targetEl) return;
Expand All @@ -199,6 +301,12 @@ export class ComposerDockview {
});
}

/**
* Instantiates an Angular component dynamically within a Dockview panel container.
*
* @param panelId Identifier of the composer panel to create.
* @returns Dockview panel content renderer interface.
*/
private createPanelComponent(panelId: ComposerPanelId) {
let type: Type<unknown> | undefined;
switch (panelId) {
Expand Down Expand Up @@ -261,7 +369,21 @@ export class ComposerDockview {
};
}

private buildDockviewLayout(): void {
/**
* Constructs the default Dockview layout hierarchy or restores a valid stored layout from localStorage.
*
* Layout Design:
* 1. **Chat Panel (Gemini Assistant)**: Anchored left. Width <= 1/3 viewport (`chatWidth`).
* 2. **Rendered A2UI Preview & A2UI JSON Editor**: Placed right of Chat side-by-side with an
* equal 50/50 width split between Rendered Preview (left) and JSON Editor (right).
* 3. **Debug Drawer Group**: Placed below Rendered Preview (`direction: 'below'`), sized to ~28%
* container height. Houses Data Model (active), Events, Errors, and Raw Messages (`inactive: true`).
*
* @param width Viewport width in pixels.
* @param height Viewport height in pixels.
* @returns `true` if saved layout was restored; `false` if initial default layout was constructed.
*/
private buildDockviewLayout(width: number, height: number): boolean {
this.storage.removeItem(LocalStorageKey.ACTIVE_DRAFT);

const savedLayout = this.storage.getItem(LocalStorageKey.DOCKVIEW_LAYOUT);
Expand Down Expand Up @@ -298,27 +420,33 @@ export class ComposerDockview {
}

if (!layoutRestored) {
const chatWidth = Math.floor(width * CHAT_PANEL_MAX_WIDTH_FRACTION);
const debugHeight = Math.round(height * DEBUG_DRAWER_HEIGHT_RATIO);
const previewHeight = height - debugHeight;
const halfRightWidth = Math.floor((width - chatWidth) / 2);

// 1. Chat panel (Gemini Assistant) on the left
this.dockviewApi.addPanel({
id: ComposerPanelId.Chat,
component: ComposerPanelId.Chat,
title: 'Gemini Assistant',
initialWidth: chatWidth,
minimumWidth: CHAT_PANEL_MIN_WIDTH,
});

// 2. Rendered preview split to the right of Chat
this.dockviewApi.addPanel({
id: ComposerPanelId.Rendered,
component: ComposerPanelId.Rendered,
title: 'Rendered A2UI Preview',
position: {direction: 'right', referencePanel: ComposerPanelId.Chat},
});
this.dockviewApi.addPanel({
id: ComposerPanelId.Raw,
component: ComposerPanelId.Raw,
title: 'A2UI JSON Editor',
position: {
direction: 'right',
referencePanel: ComposerPanelId.Rendered,
},
initialWidth: halfRightWidth,
initialHeight: previewHeight,
minimumWidth: PREVIEW_PANEL_MIN_WIDTH,
minimumHeight: PREVIEW_PANEL_MIN_HEIGHT,
});

// 3. Debug drawer split below Rendered preview
this.dockviewApi.addPanel({
id: ComposerPanelId.DataModel,
component: ComposerPanelId.DataModel,
Expand All @@ -327,7 +455,11 @@ export class ComposerDockview {
direction: 'below',
referencePanel: ComposerPanelId.Rendered,
},
initialHeight: debugHeight,
minimumHeight: DEBUG_DRAWER_MIN_HEIGHT,
});

// 4. Secondary debug tabs placed within Data Model group (inactive initially)
this.dockviewApi.addPanel({
id: ComposerPanelId.Events,
component: ComposerPanelId.Events,
Expand All @@ -336,6 +468,7 @@ export class ComposerDockview {
direction: 'within',
referencePanel: ComposerPanelId.DataModel,
},
inactive: true,
});
this.dockviewApi.addPanel({
id: ComposerPanelId.Errors,
Expand All @@ -345,6 +478,7 @@ export class ComposerDockview {
direction: 'within',
referencePanel: ComposerPanelId.DataModel,
},
inactive: true,
});
this.dockviewApi.addPanel({
id: ComposerPanelId.RawMessages,
Expand All @@ -354,10 +488,61 @@ export class ComposerDockview {
direction: 'within',
referencePanel: ComposerPanelId.DataModel,
},
inactive: true,
});

// 5. Raw JSON editor placed to the right of Rendered preview
this.dockviewApi.addPanel({
id: ComposerPanelId.Raw,
component: ComposerPanelId.Raw,
title: 'A2UI JSON Editor',
position: {
direction: 'right',
referencePanel: ComposerPanelId.Rendered,
},
initialWidth: halfRightWidth,
minimumWidth: PREVIEW_PANEL_MIN_WIDTH,
minimumHeight: PREVIEW_PANEL_MIN_HEIGHT,
});
Comment thread
jgindin marked this conversation as resolved.
Resolved

// Explicitly activate default primary tabs
const chatPanel = this.dockviewApi.getGroupPanel(ComposerPanelId.Chat);
chatPanel?.api.setActive();
}

return layoutRestored;
}

/**
* Enforces target width and height splitview proportions on initial layout panels.
*
* @param width Total container width in pixels.
* @param height Total container height in pixels.
*/
private enforceInitialProportions(width: number, height: number): void {
const chatWidth = Math.floor(width * CHAT_PANEL_MAX_WIDTH_FRACTION);
const debugHeight = Math.round(height * DEBUG_DRAWER_HEIGHT_RATIO);
const halfRightWidth = Math.floor((width - chatWidth) / 2);

const chatPanel = this.dockviewApi?.getGroupPanel(ComposerPanelId.Chat);
chatPanel?.api.setSize({width: chatWidth});

const dataModelPanel = this.dockviewApi?.getGroupPanel(ComposerPanelId.DataModel);
dataModelPanel?.api.setSize({height: debugHeight});

const rawPanel = this.dockviewApi?.getGroupPanel(ComposerPanelId.Raw);
const renderedPanel = this.dockviewApi?.getGroupPanel(ComposerPanelId.Rendered);
if (rawPanel && renderedPanel && rawPanel.group !== renderedPanel.group) {
rawPanel.api.setSize({width: halfRightWidth});
renderedPanel.api.setSize({width: halfRightWidth});
}
}

/**
* Handles click and pointerdown events on tab elements to ensure active tab selection.
*
* @param event The DOM event triggered on tab click.
*/
private handleTabInteraction(event: Event): void {
const tabEl = event
.composedPath()
Expand Down
Loading
Loading