diff --git a/shell/src/app/shell/composer-workspace/composer-dockview.service.ts b/shell/src/app/shell/composer-workspace/composer-dockview.service.ts index 055662c8a..71d8653e3 100644 --- a/shell/src/app/shell/composer-workspace/composer-dockview.service.ts +++ b/shell/src/app/shell/composer-workspace/composer-dockview.service.ts @@ -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 { @@ -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; @@ -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); @@ -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()); @@ -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; 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); @@ -149,10 +220,21 @@ 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) { @@ -160,6 +242,12 @@ export class ComposerDockview { } } + /** + * 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) { @@ -167,18 +255,32 @@ export class ComposerDockview { } } + /** + * 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; @@ -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 | undefined; switch (panelId) { @@ -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); @@ -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, @@ -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, @@ -336,6 +468,7 @@ export class ComposerDockview { direction: 'within', referencePanel: ComposerPanelId.DataModel, }, + inactive: true, }); this.dockviewApi.addPanel({ id: ComposerPanelId.Errors, @@ -345,6 +478,7 @@ export class ComposerDockview { direction: 'within', referencePanel: ComposerPanelId.DataModel, }, + inactive: true, }); this.dockviewApi.addPanel({ id: ComposerPanelId.RawMessages, @@ -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, + }); + + // 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() diff --git a/shell/src/app/shell/composer-workspace/composer-workspace.spec.ts b/shell/src/app/shell/composer-workspace/composer-workspace.spec.ts index 88c3bc92d..09e023fa2 100644 --- a/shell/src/app/shell/composer-workspace/composer-workspace.spec.ts +++ b/shell/src/app/shell/composer-workspace/composer-workspace.spec.ts @@ -16,7 +16,11 @@ import {ComponentFixture, TestBed} from '@angular/core/testing'; import {ComposerPanelId, ComposerWorkspace} from './composer-workspace'; -import {ComposerDockview} from './composer-dockview.service'; +import { + ComposerDockview, + DEFAULT_CONTAINER_WIDTH, + DEFAULT_CONTAINER_HEIGHT, +} from './composer-dockview.service'; import {LocalStorageInteractions} from '../../storage/local-storage-interactions/local-storage-interactions'; import {LocalStorageKey} from '../../storage/models/local-storage-keys'; import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; @@ -449,14 +453,17 @@ describe('ComposerWorkspace Dashboard', () => { const widthSpy = vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(1200); const heightSpy = vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockReturnValue(800); - const newFixture = TestBed.createComponent(ComposerWorkspace); - newFixture.detectChanges(); - await newFixture.whenStable(); + try { + const newFixture = TestBed.createComponent(ComposerWorkspace); + newFixture.detectChanges(); + await newFixture.whenStable(); - expect(layoutSpy).toHaveBeenCalledWith(1200, 800); - layoutSpy.mockRestore(); - widthSpy.mockRestore(); - heightSpy.mockRestore(); + expect(layoutSpy).toHaveBeenCalledWith(1200, 800); + } finally { + layoutSpy.mockRestore(); + widthSpy.mockRestore(); + heightSpy.mockRestore(); + } }); it('activates panel and triggers change detection when pointerdown occurs on tab element', () => { @@ -624,5 +631,111 @@ describe('ComposerWorkspace Dashboard', () => { expect(trackSpy).toHaveBeenCalledWith({panelId: ComposerPanelId.Events}); }); + + describe('Initial Layout Configuration', () => { + it('groups Data Model, Events, Errors, and Raw Messages together with Data Model active', () => { + const manager = fixture.debugElement.injector.get(ComposerDockview); + const dataModelPanel = manager.api.getGroupPanel(ComposerPanelId.DataModel); + const eventsPanel = manager.api.getGroupPanel(ComposerPanelId.Events); + const errorsPanel = manager.api.getGroupPanel(ComposerPanelId.Errors); + const rawMessagesPanel = manager.api.getGroupPanel(ComposerPanelId.RawMessages); + + expect(dataModelPanel).toBeDefined(); + expect(eventsPanel).toBeDefined(); + expect(errorsPanel).toBeDefined(); + expect(rawMessagesPanel).toBeDefined(); + + const debugGroup = dataModelPanel!.group; + expect(eventsPanel!.group).toBe(debugGroup); + expect(errorsPanel!.group).toBe(debugGroup); + expect(rawMessagesPanel!.group).toBe(debugGroup); + + const debugPanelIds = debugGroup.panels.map(p => p.id); + expect(debugPanelIds).toEqual([ + ComposerPanelId.DataModel, + ComposerPanelId.Events, + ComposerPanelId.Errors, + ComposerPanelId.RawMessages, + ]); + expect(debugGroup.activePanel?.id).toBe(ComposerPanelId.DataModel); + }); + + it('allocates less vertical space to the debug drawer group relative to the preview group', () => { + const manager = fixture.debugElement.injector.get(ComposerDockview); + const renderedPanel = manager.api.getGroupPanel(ComposerPanelId.Rendered); + const dataModelPanel = manager.api.getGroupPanel(ComposerPanelId.DataModel); + + expect(renderedPanel).toBeDefined(); + expect(dataModelPanel).toBeDefined(); + + const previewHeight = renderedPanel!.group.height; + const debugHeight = dataModelPanel!.group.height; + + expect(debugHeight).toBeLessThan(previewHeight); + expect(debugHeight).toBeLessThanOrEqual(Math.round(DEFAULT_CONTAINER_HEIGHT * 0.35)); + expect(dataModelPanel!.group.width).toBeGreaterThan(renderedPanel!.group.width); + }); + + it('balances initial widths equally between Rendered A2UI Preview and A2UI JSON Editor', () => { + const manager = fixture.debugElement.injector.get(ComposerDockview); + const renderedPanel = manager.api.getGroupPanel(ComposerPanelId.Rendered); + const rawPanel = manager.api.getGroupPanel(ComposerPanelId.Raw); + const dataModelPanel = manager.api.getGroupPanel(ComposerPanelId.DataModel); + + expect(renderedPanel).toBeDefined(); + expect(rawPanel).toBeDefined(); + expect(dataModelPanel).toBeDefined(); + expect(rawPanel!.group.width).toBe(renderedPanel!.group.width); + expect(dataModelPanel!.group.width).toBe( + renderedPanel!.group.width + rawPanel!.group.width, + ); + }); + + it('limits Gemini Assistant initial width to not exceed 1/3 of the overall page width', () => { + const manager = fixture.debugElement.injector.get(ComposerDockview); + const chatPanel = manager.api.getGroupPanel(ComposerPanelId.Chat); + + expect(chatPanel).toBeDefined(); + const chatWidth = chatPanel!.group.width; + + expect(chatWidth).toBeLessThanOrEqual(Math.ceil(DEFAULT_CONTAINER_WIDTH / 3)); + }); + + it('enforces Gemini Assistant initial width and debug drawer initial height on layout initialization', async () => { + const storageSpy = vi.spyOn(Storage.prototype, 'getItem').mockReturnValue(null); + const widthSpy = vi + .spyOn(HTMLElement.prototype, 'clientWidth', 'get') + .mockReturnValue(1200); + const heightSpy = vi + .spyOn(HTMLElement.prototype, 'clientHeight', 'get') + .mockReturnValue(900); + + try { + const newFixture = TestBed.createComponent(ComposerWorkspace); + newFixture.detectChanges(); + await newFixture.whenStable(); + + const manager = newFixture.debugElement.injector.get(ComposerDockview); + const chatPanel = manager.api.getGroupPanel(ComposerPanelId.Chat); + const dataModelPanel = manager.api.getGroupPanel(ComposerPanelId.DataModel); + const renderedPanel = manager.api.getGroupPanel(ComposerPanelId.Rendered); + + expect(chatPanel).toBeDefined(); + expect(dataModelPanel).toBeDefined(); + expect(renderedPanel).toBeDefined(); + + // Chat <= 1/3 of 1200 (400) + expect(chatPanel!.group.width).toBeLessThanOrEqual(400); + + // Debug height ~28% of 900 (252), preview ~648 + expect(dataModelPanel!.group.height).toBeLessThan(renderedPanel!.group.height); + expect(dataModelPanel!.group.height).toBeLessThanOrEqual(300); + } finally { + storageSpy.mockRestore(); + widthSpy.mockRestore(); + heightSpy.mockRestore(); + } + }); + }); }); });