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

Simplify editor configuration

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