Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MutationObserver for Monaco Editor AutomaticLayout #90111

Merged
merged 2 commits into from
Feb 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/vs/editor/browser/config/elementSizeObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Disposable } from 'vs/base/common/lifecycle';
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
import { IDimension } from 'vs/editor/common/editorCommon';
import * as dom from 'vs/base/browser/dom';

export class ElementSizeObserver extends Disposable {

private readonly referenceDomElement: HTMLElement | null;
private measureReferenceDomElementToken: any;
private readonly changeCallback: () => void;
private width: number;
private height: number;
private mutationObserver: MutationObserver | null;
private windowSizeListener: IDisposable | null;

constructor(referenceDomElement: HTMLElement | null, dimension: IDimension | undefined, changeCallback: () => void) {
super();
this.referenceDomElement = referenceDomElement;
this.changeCallback = changeCallback;
this.measureReferenceDomElementToken = -1;
this.width = -1;
this.height = -1;
this.mutationObserver = null;
this.windowSizeListener = null;
this.measureReferenceDomElement(false, dimension);
}

Expand All @@ -38,22 +41,40 @@ export class ElementSizeObserver extends Disposable {
}

public startObserving(): void {
if (this.measureReferenceDomElementToken === -1) {
this.measureReferenceDomElementToken = setInterval(() => this.measureReferenceDomElement(true), 100);
if (!this.mutationObserver && this.referenceDomElement) {
this.mutationObserver = new MutationObserver(() => this._onDidMutate());
this.mutationObserver.observe(this.referenceDomElement, {
attributes: true,
});
}
if (!this.windowSizeListener) {
this.windowSizeListener = dom.addDisposableListener(window, 'resize', () => this._onDidResizeWindow());
}
}

public stopObserving(): void {
if (this.measureReferenceDomElementToken !== -1) {
clearInterval(this.measureReferenceDomElementToken);
this.measureReferenceDomElementToken = -1;
if (this.mutationObserver) {
this.mutationObserver.disconnect();
this.mutationObserver = null;
}
if (this.windowSizeListener) {
this.windowSizeListener.dispose();
this.windowSizeListener = null;
}
}

public observe(dimension?: IDimension): void {
this.measureReferenceDomElement(true, dimension);
}

private _onDidMutate(): void {
this.measureReferenceDomElement(true);
}

private _onDidResizeWindow(): void {
this.measureReferenceDomElement(true);
}

private measureReferenceDomElement(callChangeCallback: boolean, dimension?: IDimension): void {
let observedWidth = 0;
let observedHeight = 0;
Expand Down