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
81 changes: 57 additions & 24 deletions src/vs/workbench/contrib/canvases/electron-browser/canvasEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { URI } from '../../../../base/common/uri.js';
import { localize } from '../../../../nls.js';
import { AccessibleContentProvider, AccessibleViewProviderId, AccessibleViewType, IAccessibleViewService } from '../../../../platform/accessibility/browser/accessibleView.js';
import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.js';
import { IBrowserViewBounds } from '../../../../platform/browserView/common/browserView.js';
import { IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
import { IEditorOptions } from '../../../../platform/editor/common/editor.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
Expand Down Expand Up @@ -67,6 +68,7 @@ export class CanvasEditor extends EditorPane {
private readonly presentationVisible = observableValue(this, false);
private readonly semanticChanged = this._register(new Emitter<void>());
private loadSequence = 0;
private layoutSequence = 0;
private loadingKey: string | undefined;
private loadedKey: string | undefined;
private semanticSequence = 0;
Expand Down Expand Up @@ -216,8 +218,10 @@ export class CanvasEditor extends EditorPane {
}
this.modelLifetime.clear();
this.model = model;
this.layoutSequence++;
this.presentationVisible.set(false, undefined);
if (model) {
// Attach hidden: only `layout` may reveal the native view, after sending visible bounds.
// Attach hidden: only `layout` may reveal the native view, after its visible bounds are applied.
this.host.setVisible(false);
}
this.host.setModel(model);
Expand Down Expand Up @@ -289,45 +293,74 @@ export class CanvasEditor extends EditorPane {
}

override layout(): void {
if (!this.model) {
const model = this.model;
if (!model) {
return;
}
const sequence = ++this.layoutSequence;
const { rect, bounds, visible } = this._measure();
this.container.style.left = `${bounds.x - rect.left}px`;
this.container.style.top = `${bounds.y - rect.top}px`;
this.container.style.width = `${bounds.width}px`;
this.container.style.height = `${bounds.height}px`;
if (!visible) {
// Keep the last valid native bounds; empty or off-screen bounds must never be revealed.
this.presentationVisible.set(false, undefined);
this.host.setVisible(false);
this.host.layout();
return;
}
void this._layoutAndReveal(model, bounds, sequence);
}

/**
* Reveals the native view only after it has applied `bounds`, or it shows at stale bounds.
* A newer layout, a hide or a model change supersedes the reveal.
*/
private async _layoutAndReveal(model: IBrowserViewModel, bounds: IBrowserViewBounds, sequence: number): Promise<void> {
let applied = false;
try {
await model.layout(bounds);
applied = true;
} catch (error) {
this.logService.error('[CanvasEditor] Failed to layout canvas', error);
}
if (sequence !== this.layoutSequence || this.model !== model) {
return;
}
// The pane can be hidden or moved off-screen without a new layout while the bounds are applied.
const visible = applied && this._measure().visible;
this.presentationVisible.set(visible, undefined);
if (!visible) {
this.host.setVisible(false);
}
// Refreshing overlays can reveal a visible host that is no longer obscured, so it must follow the bounds.
this.host.layout();
if (visible) {
this.host.setVisible(true);
}
}

Comment on lines +322 to +343
private _measure(): { readonly rect: DOMRect; readonly bounds: IBrowserViewBounds; readonly visible: boolean } {
const rect = this.wrapper.getBoundingClientRect();
const zoomFactor = getZoomFactor(this.window);
const snap = (value: number) => Math.floor(value * zoomFactor) / zoomFactor;
const x = snap(rect.left);
const y = snap(rect.top);
const width = snap(rect.width);
const height = snap(rect.height);
const bounds = { x: snap(rect.left), y: snap(rect.top), width: snap(rect.width), height: snap(rect.height), windowId: this.group.windowId, zoomFactor, cornerRadius: 0 };
const visible = this.editorVisible.get()
&& width > 0
&& height > 0
&& bounds.width > 0
&& bounds.height > 0
&& rect.right > 0
&& rect.bottom > 0
&& rect.left < this.window.innerWidth
&& rect.top < this.window.innerHeight;
this.presentationVisible.set(visible, undefined);
this.container.style.left = `${x - rect.left}px`;
this.container.style.top = `${y - rect.top}px`;
this.container.style.width = `${width}px`;
this.container.style.height = `${height}px`;
if (!visible) {
// Keep the last valid native bounds; empty or off-screen bounds must never be revealed.
this.host.setVisible(false);
this.host.layout();
return;
}
// Bounds must reach the native view before it is revealed, or it shows at stale bounds.
void this.model.layout({ x, y, width, height, windowId: this.group.windowId, zoomFactor, cornerRadius: 0 })
.catch(error => this.logService.error('[CanvasEditor] Failed to layout canvas', error));
this.host.setVisible(true);
this.host.layout();
return { rect, bounds, visible };
}

protected override setEditorVisible(visible: boolean): void {
this.editorVisible.set(visible, undefined);
this.pendingVisibleLayout.clear();
if (!visible) {
this.layoutSequence++;
this.presentationVisible.set(false, undefined);
this.host?.setVisible(false);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import assert from 'assert';
import { $, getWindow } from '../../../../../base/browser/dom.js';
import { $, getWindow, scheduleAtNextAnimationFrame } from '../../../../../base/browser/dom.js';
import { DeferredPromise, timeout } from '../../../../../base/common/async.js';
import { CancellationToken } from '../../../../../base/common/cancellation.js';
import { Emitter, Event } from '../../../../../base/common/event.js';
Expand Down Expand Up @@ -38,6 +38,8 @@ suite('CanvasEditor', () => {
const visible: boolean[] = [];
const attached: (IBrowserViewModel | undefined)[] = [];
const log: string[] = [];
const errors: string[] = [];
let pendingLayouts: DeferredPromise<void>[] | undefined;
let disposed = 0;
let created = 0;
const openExternal = store.add(new Emitter<string>());
Expand All @@ -53,7 +55,15 @@ suite('CanvasEditor', () => {
onDidNavigate: Event.None,
onDidRequestOpenExternal: openExternal.event,
onWillDispose: Event.None,
layout: async bounds => { log.push(`model.layout:${bounds.width}x${bounds.height}`); },
layout: bounds => {
log.push(`model.layout:${bounds.width}x${bounds.height}`);
if (!pendingLayouts) {
return Promise.resolve();
}
const layout = new DeferredPromise<void>();
pendingLayouts.push(layout);
return layout.p;
},
dispose: () => disposed++,
});
let view = Promise.resolve(model);
Expand All @@ -75,6 +85,7 @@ suite('CanvasEditor', () => {
});
instantiationService.stub(ILogService, {
warn: message => warningLogged.complete(String(message)),
error: message => errors.push(String(message)),
});
if (harnessOptions?.isSessionsWindow !== undefined) {
instantiationService.stub(IWorkbenchEnvironmentService, upcastPartial<IWorkbenchEnvironmentService>({ isSessionsWindow: harnessOptions.isSessionsWindow }));
Expand All @@ -90,7 +101,7 @@ suite('CanvasEditor', () => {
}
},
setVisible: value => { visible.push(value); log.push(`host.visible:${value}`); },
layout: () => { },
layout: () => { log.push('host.layout'); },
tryFocus: () => false,
dispose: () => { },
});
Expand All @@ -102,11 +113,21 @@ suite('CanvasEditor', () => {
resource: URI.parse('canvas:/preview'), instanceId: 'preview', title: 'Preview', source: URI.parse('https://example.test'),
}));
return {
editor, input, model, visible, attached, ownerVisible, openExternal, opened, modelAttached, openerCalled, warningLogged, parent, log,
editor, input, model, visible, attached, ownerVisible, openExternal, opened, modelAttached, openerCalled, warningLogged, parent, log, errors,
get disposed() { return disposed; },
get created() { return created; },
setView: (value: Promise<IBrowserViewModel>) => view = value,
open: () => editor.setInput(input, undefined, { newInGroup: true }, CancellationToken.None),
/** Native layouts stay pending until the returned promises are settled. */
deferLayouts: (): DeferredPromise<void>[] => {
pendingLayouts = [];
return pendingLayouts;
},
/** Shows the pane and lets its scheduled layout run, so no layout is pending afterwards. */
show: async () => {
editor.setVisible(true);
await new Promise<void>(resolve => store.add(scheduleAtNextAnimationFrame(getWindow(parent), () => resolve())));
},
};
}

Expand All @@ -123,13 +144,14 @@ suite('CanvasEditor', () => {
test('keeps an attached model hidden while its pane is visible inside a hidden editor part', async () => {
const harness = createHarness();
harness.parent.style.display = 'none';
harness.editor.setVisible(true);
await harness.show();
await harness.open();
await timeout(0);
await harness.modelAttached.p;
harness.editor.layout();
const whileHidden = [...harness.log];
harness.parent.style.display = '';
harness.editor.layout();
await timeout(0);
assert.deepStrictEqual({
attached: harness.attached.includes(harness.model),
shownWhileHidden: whileHidden.includes('host.visible:true'),
Expand All @@ -139,7 +161,7 @@ suite('CanvasEditor', () => {
attached: true,
shownWhileHidden: false,
boundsWhileHidden: [],
reveal: ['model.layout:400x200', 'host.visible:true'],
reveal: ['model.layout:400x200', 'host.layout', 'host.visible:true'],
});
});

Expand All @@ -152,29 +174,139 @@ suite('CanvasEditor', () => {
'host.visible:false',
'host.model:attached',
'model.layout:400x200',
'host.layout',
'host.visible:true',
]);
});

test('waits for layout before revealing an attached model when its pane becomes visible again', async () => {
const harness = createHarness();
harness.editor.setVisible(true);
await harness.show();
await harness.open();
await harness.modelAttached.p;
await timeout(0);
harness.editor.setVisible(false);
const layouts = harness.deferLayouts();
const showStart = harness.log.length;
harness.editor.setVisible(true);
const layoutStart = harness.log.length;
harness.editor.layout();
await harness.show();
const pending = harness.log.slice(showStart);
await layouts[0].complete();
await timeout(0);
assert.deepStrictEqual({
afterShow: harness.log.slice(showStart, layoutStart),
afterLayout: harness.log.slice(layoutStart),
pending,
applied: harness.log.slice(showStart + pending.length),
}, {
afterShow: [],
afterLayout: ['model.layout:400x200', 'host.visible:true'],
pending: ['model.layout:400x200'],
applied: ['host.layout', 'host.visible:true'],
});
});

test('reveals an attached model only after its native bounds are applied', async () => {
const harness = createHarness();
const layouts = harness.deferLayouts();
await harness.show();
await harness.open();
await harness.modelAttached.p;
await timeout(0);
const pending = [...harness.log];
await layouts[0].complete();
await timeout(0);
assert.deepStrictEqual({ pending, applied: harness.log.slice(pending.length) }, {
pending: ['host.visible:false', 'host.model:attached', 'model.layout:400x200'],
applied: ['host.layout', 'host.visible:true'],
});
});

test('refreshes overlays of a resized visible model only after its new bounds are applied', async () => {
const harness = createHarness();
await harness.show();
await harness.open();
await harness.modelAttached.p;
await timeout(0);
const layouts = harness.deferLayouts();
const resizeStart = harness.log.length;
harness.parent.style.width = '300px';
harness.editor.layout();
const pending = harness.log.slice(resizeStart);
await layouts[0].complete();
await timeout(0);
assert.deepStrictEqual({ pending, applied: harness.log.slice(resizeStart + pending.length) }, {
pending: ['model.layout:300x200'],
applied: ['host.layout', 'host.visible:true'],
});
});

test('keeps an attached model hidden when its native bounds cannot be applied', async () => {
const harness = createHarness();
const layouts = harness.deferLayouts();
await harness.show();
await harness.open();
await harness.modelAttached.p;
await layouts[0].error(new Error('view is gone'));
await timeout(0);
assert.deepStrictEqual({ log: harness.log, errors: harness.errors }, {
log: ['host.visible:false', 'host.model:attached', 'model.layout:400x200', 'host.visible:false', 'host.layout'],
errors: ['[CanvasEditor] Failed to layout canvas'],
});
});

test('reveals an attached model only for its latest pending layout', async () => {
const harness = createHarness();
const layouts = harness.deferLayouts();
await harness.show();
await harness.open();
await harness.modelAttached.p;
harness.parent.style.width = '300px';
harness.editor.layout();
const sent = [...harness.log];
await layouts[0].complete();
await timeout(0);
const afterStale = harness.log.slice(sent.length);
await layouts[1].complete();
await timeout(0);
assert.deepStrictEqual({ sent, afterStale, afterLatest: harness.log.slice(sent.length + afterStale.length) }, {
sent: ['host.visible:false', 'host.model:attached', 'model.layout:400x200', 'model.layout:300x200'],
afterStale: [],
afterLatest: ['host.layout', 'host.visible:true'],
});
});

test('does not reveal a pending layout after its pane is hidden', async () => {
const harness = createHarness();
const layouts = harness.deferLayouts();
await harness.show();
await harness.open();
await harness.modelAttached.p;
harness.editor.setVisible(false);
await layouts[0].complete();
await timeout(0);
assert.deepStrictEqual(harness.log, ['host.visible:false', 'host.model:attached', 'model.layout:400x200', 'host.visible:false']);
});

test('does not reveal a pending layout after its model is detached', async () => {
const harness = createHarness();
const layouts = harness.deferLayouts();
await harness.show();
await harness.open();
await harness.modelAttached.p;
harness.editor.clearInput();
await layouts[0].complete();
await timeout(0);
assert.deepStrictEqual(harness.log, ['host.visible:false', 'host.model:attached', 'model.layout:400x200', 'host.model:none']);
});

test('keeps a pending layout hidden when its editor part is hidden before the bounds are applied', async () => {
const harness = createHarness();
const layouts = harness.deferLayouts();
await harness.show();
await harness.open();
await harness.modelAttached.p;
harness.parent.style.display = 'none';
await layouts[0].complete();
await timeout(0);
assert.deepStrictEqual(harness.log, ['host.visible:false', 'host.model:attached', 'model.layout:400x200', 'host.visible:false', 'host.layout']);
});

test('does not create a view while canvas state has no live source', async () => {
const harness = createHarness();
const canvas = harness.input.canvas.get();
Expand Down
Loading