提交 3b8f8a3b 编写于 作者: A Alex Dima

Simplify editor configuration

上级 e31a5085
......@@ -8,7 +8,7 @@ import Event, { Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import * as platform from 'vs/base/common/platform';
import * as browser from 'vs/base/browser/browser';
import { CommonEditorConfiguration } from 'vs/editor/common/config/commonEditorConfig';
import { CommonEditorConfiguration, IEnvConfiguration } from 'vs/editor/common/config/commonEditorConfig';
import { IDimension } from 'vs/editor/common/editorCommon';
import { FontInfo, BareFontInfo } from 'vs/editor/common/config/fontInfo';
import { ElementSizeObserver } from 'vs/editor/browser/config/elementSizeObserver';
......@@ -309,8 +309,12 @@ export class Configuration extends CommonEditorConfiguration {
domNode.setLineHeight(fontInfo.lineHeight);
}
private readonly _elementSizeObserver: ElementSizeObserver;
constructor(options: IEditorOptions, referenceDomElement: HTMLElement = null) {
super(options, new ElementSizeObserver(referenceDomElement, () => this._onReferenceDomElementSizeChanged()));
super(options);
this._elementSizeObserver = this._register(new ElementSizeObserver(referenceDomElement, () => this._onReferenceDomElementSizeChanged()));
this._register(CSSBasedConfiguration.INSTANCE.onDidChange(() => this._onCSSBasedConfigurationChanged()));
......@@ -319,6 +323,8 @@ export class Configuration extends CommonEditorConfiguration {
}
this._register(browser.onDidChangeZoomLevel(_ => this._recomputeOptions()));
this._recomputeOptions();
}
private _onReferenceDomElementSizeChanged(): void {
......@@ -334,11 +340,10 @@ export class Configuration extends CommonEditorConfiguration {
}
public dispose(): void {
this._elementSizeObserver.dispose();
super.dispose();
}
protected _getEditorClassName(theme: string, fontLigatures: boolean, mouseStyle: 'text' | 'default' | 'copy'): string {
private _getExtraEditorClassName(): string {
let extra = '';
if (browser.isIE) {
extra += 'ie ';
......@@ -350,38 +355,21 @@ export class Configuration extends CommonEditorConfiguration {
if (platform.isMacintosh) {
extra += 'mac ';
}
if (fontLigatures) {
extra += 'enable-ligatures ';
}
if (mouseStyle === 'default') {
extra += 'mouse-default ';
} else if (mouseStyle === 'copy') {
extra += 'mouse-copy ';
}
return 'monaco-editor ' + extra + theme;
}
protected getOuterWidth(): number {
return this._elementSizeObserver.getWidth();
}
protected getOuterHeight(): number {
return this._elementSizeObserver.getHeight();
return extra;
}
protected _getCanUseTranslate3d(): boolean {
return browser.canUseTranslate3d();
}
protected _getPixelRatio(): number {
return browser.getPixelRatio();
protected _getEnvConfiguration(): IEnvConfiguration {
return {
extraEditorClassName: this._getExtraEditorClassName(),
outerWidth: this._elementSizeObserver.getWidth(),
outerHeight: this._elementSizeObserver.getHeight(),
canUseTranslate3d: browser.canUseTranslate3d(),
pixelRatio: browser.getPixelRatio(),
zoomLevel: browser.getZoomLevel()
};
}
protected readConfiguration(bareFontInfo: BareFontInfo): FontInfo {
return CSSBasedConfiguration.INSTANCE.readConfiguration(bareFontInfo);
}
protected getZoomLevel(): number {
return browser.getZoomLevel();
}
}
......@@ -6,9 +6,8 @@
import { Disposable } from 'vs/base/common/lifecycle';
import { IDimension } from 'vs/editor/common/editorCommon';
import { IElementSizeObserver } from 'vs/editor/common/config/commonEditorConfig';
export class ElementSizeObserver extends Disposable implements IElementSizeObserver {
export class ElementSizeObserver extends Disposable {
private referenceDomElement: HTMLElement;
private measureReferenceDomElementToken: number;
......
......@@ -51,38 +51,35 @@ export const TabFocus: ITabFocus = new class {
}
};
export interface IElementSizeObserver {
startObserving(): void;
observe(dimension?: editorCommon.IDimension): void;
dispose(): void;
getWidth(): number;
getHeight(): number;
export interface IEnvConfiguration {
extraEditorClassName: string;
outerWidth: number;
outerHeight: number;
canUseTranslate3d: boolean;
pixelRatio: number;
zoomLevel: number;
}
export abstract class CommonEditorConfiguration extends Disposable implements editorCommon.IConfiguration {
protected _rawOptions: editorOptions.IEditorOptions;
protected _validatedOptions: editorOptions.IValidatedEditorOptions;
public editor: editorOptions.InternalEditorOptions;
protected _elementSizeObserver: IElementSizeObserver;
private _isDominatedByLongLines: boolean;
private _lineNumbersDigitCount: number;
private _onDidChange = this._register(new Emitter<editorOptions.IConfigurationChangedEvent>());
public onDidChange: Event<editorOptions.IConfigurationChangedEvent> = this._onDidChange.event;
constructor(options: editorOptions.IEditorOptions, elementSizeObserver: IElementSizeObserver = null) {
constructor(options: editorOptions.IEditorOptions) {
super();
this._rawOptions = options || {};
this._validatedOptions = editorOptions.EditorOptionsValidator.validate(this._rawOptions, EDITOR_DEFAULTS);
this._elementSizeObserver = elementSizeObserver;
this.editor = null;
this._isDominatedByLongLines = false;
this._lineNumbersDigitCount = 1;
this.editor = this._computeInternalOptions();
this._register(EditorZoom.onDidChangeZoomLevel(_ => this._recomputeOptions()));
this._register(TabFocus.onDidChangeTabFocus(_ => this._recomputeOptions()));
}
......@@ -92,17 +89,18 @@ export abstract class CommonEditorConfiguration extends Disposable implements ed
}
protected _recomputeOptions(): void {
this._setOptions(this._computeInternalOptions());
}
const oldOptions = this.editor;
const newOptions = this._computeInternalOptions();
private _setOptions(newOptions: editorOptions.InternalEditorOptions): void {
if (this.editor && this.editor.equals(newOptions)) {
if (oldOptions && oldOptions.equals(newOptions)) {
return;
}
let changeEvent = this.editor.createChangeEvent(newOptions);
this.editor = newOptions;
this._onDidChange.fire(changeEvent);
if (oldOptions) {
this._onDidChange.fire(oldOptions.createChangeEvent(newOptions));
}
}
public getRawOptions(): editorOptions.IEditorOptions {
......@@ -111,16 +109,18 @@ export abstract class CommonEditorConfiguration extends Disposable implements ed
private _computeInternalOptions(): editorOptions.InternalEditorOptions {
const opts = this._validatedOptions;
const bareFontInfo = BareFontInfo.createFromRawSettings(this._rawOptions, this.getZoomLevel());
const partialEnv = this._getEnvConfiguration();
const bareFontInfo = BareFontInfo.createFromRawSettings(this._rawOptions, partialEnv.zoomLevel);
const editorClassName = this._getEditorClassName(opts.viewInfo.theme, opts.viewInfo.fontLigatures, opts.mouseStyle);
const env: editorOptions.IEnvironmentalOptions = {
outerWidth: this.getOuterWidth(),
outerHeight: this.getOuterHeight(),
outerWidth: partialEnv.outerWidth,
outerHeight: partialEnv.outerHeight,
fontInfo: this.readConfiguration(bareFontInfo),
editorClassName: this._getEditorClassName(opts.viewInfo.theme, opts.viewInfo.fontLigatures, opts.mouseStyle),
editorClassName: editorClassName + ' ' + partialEnv.extraEditorClassName,
isDominatedByLongLines: this._isDominatedByLongLines,
lineNumbersDigitCount: this._lineNumbersDigitCount,
canUseTranslate3d: this._getCanUseTranslate3d(),
pixelRatio: this._getPixelRatio(),
canUseTranslate3d: partialEnv.canUseTranslate3d,
pixelRatio: partialEnv.pixelRatio,
tabFocusMode: TabFocus.getTabFocusMode()
};
return editorOptions.InternalEditorOptionsFactory.createInternalEditorOptions(env, opts);
......@@ -155,19 +155,23 @@ export abstract class CommonEditorConfiguration extends Disposable implements ed
return r ? r : 1;
}
protected abstract _getEditorClassName(theme: string, fontLigatures: boolean, mouseDrag: 'text' | 'default' | 'copy'): string;
protected abstract getOuterWidth(): number;
protected abstract getOuterHeight(): number;
protected abstract _getCanUseTranslate3d(): boolean;
private _getEditorClassName(theme: string, fontLigatures: boolean, mouseStyle: 'text' | 'default' | 'copy'): string {
let extra = '';
if (fontLigatures) {
extra += 'enable-ligatures ';
}
if (mouseStyle === 'default') {
extra += 'mouse-default ';
} else if (mouseStyle === 'copy') {
extra += 'mouse-copy ';
}
return 'monaco-editor ' + extra + theme;
}
protected abstract _getPixelRatio(): number;
protected abstract _getEnvConfiguration(): IEnvConfiguration;
protected abstract readConfiguration(styling: BareFontInfo): FontInfo;
protected abstract getZoomLevel(): number;
}
const configurationRegistry = <IConfigurationRegistry>Registry.as(Extensions.Configuration);
......
......@@ -7,6 +7,7 @@
import * as assert from 'assert';
import { EditorZoom } from 'vs/editor/common/config/editorZoom';
import { TestConfiguration } from 'vs/editor/test/common/mocks/testConfiguration';
import { IEnvConfiguration } from "vs/editor/common/config/commonEditorConfig";
suite('Common Editor Config', () => {
test('Zoom Level', () => {
......@@ -52,8 +53,15 @@ suite('Common Editor Config', () => {
});
class TestWrappingConfiguration extends TestConfiguration {
protected getOuterWidth(): number {
return 1000;
protected _getEnvConfiguration(): IEnvConfiguration {
return {
extraEditorClassName: '',
outerWidth: 1000,
outerHeight: 100,
canUseTranslate3d: true,
pixelRatio: 1,
zoomLevel: 0
};
}
}
......
......@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import { CommonEditorConfiguration } from 'vs/editor/common/config/commonEditorConfig';
import { CommonEditorConfiguration, IEnvConfiguration } from 'vs/editor/common/config/commonEditorConfig';
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { FontInfo, BareFontInfo } from 'vs/editor/common/config/fontInfo';
......@@ -12,26 +12,18 @@ export class TestConfiguration extends CommonEditorConfiguration {
constructor(opts: IEditorOptions) {
super(opts);
this._recomputeOptions();
}
protected _getEditorClassName(theme: string, fontLigatures: boolean): string {
return '';
}
protected getOuterWidth(): number {
return 100;
}
protected getOuterHeight(): number {
return 100;
}
protected _getCanUseTranslate3d(): boolean {
return true;
}
protected _getPixelRatio(): number {
return 1;
protected _getEnvConfiguration(): IEnvConfiguration {
return {
extraEditorClassName: '',
outerWidth: 100,
outerHeight: 100,
canUseTranslate3d: true,
pixelRatio: 1,
zoomLevel: 0
};
}
protected readConfiguration(styling: BareFontInfo): FontInfo {
......@@ -48,8 +40,4 @@ export class TestConfiguration extends CommonEditorConfiguration {
maxDigitWidth: 10,
}, true);
}
protected getZoomLevel(): number {
return 0;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册