提交 e434c070 编写于 作者: A Alex Dima

Fixes Microsoft/monaco-editor#806: Emit focus/blur events only when the state changes

上级 7187c21a
...@@ -101,13 +101,7 @@ export abstract class CodeEditorWidget extends CommonCodeEditor implements edito ...@@ -101,13 +101,7 @@ export abstract class CodeEditorWidget extends CommonCodeEditor implements edito
this._focusTracker = new CodeEditorWidgetFocusTracker(domElement); this._focusTracker = new CodeEditorWidgetFocusTracker(domElement);
this._focusTracker.onChange(() => { this._focusTracker.onChange(() => {
let hasFocus = this._focusTracker.hasFocus(); this._editorFocus.setValue(this._focusTracker.hasFocus());
if (hasFocus) {
this._onDidFocusEditor.fire();
} else {
this._onDidBlurEditor.fire();
}
}); });
this.contentWidgets = {}; this.contentWidgets = {};
...@@ -431,13 +425,13 @@ export abstract class CodeEditorWidget extends CommonCodeEditor implements edito ...@@ -431,13 +425,13 @@ export abstract class CodeEditorWidget extends CommonCodeEditor implements edito
const viewEventBus = this._view.getInternalEventBus(); const viewEventBus = this._view.getInternalEventBus();
viewEventBus.onDidGainFocus = () => { viewEventBus.onDidGainFocus = () => {
this._onDidFocusEditorText.fire(); this._editorTextFocus.setValue(true);
// In IE, the focus is not synchronous, so we give it a little help // In IE, the focus is not synchronous, so we give it a little help
this._onDidFocusEditor.fire(); this._editorFocus.setValue(true);
}; };
viewEventBus.onDidScroll = (e) => this._onDidScrollChange.fire(e); viewEventBus.onDidScroll = (e) => this._onDidScrollChange.fire(e);
viewEventBus.onDidLoseFocus = () => this._onDidBlurEditorText.fire(); viewEventBus.onDidLoseFocus = () => this._editorTextFocus.setValue(false);
viewEventBus.onContextMenu = (e) => this._onContextMenu.fire(e); viewEventBus.onContextMenu = (e) => this._onContextMenu.fire(e);
viewEventBus.onMouseDown = (e) => this._onMouseDown.fire(e); viewEventBus.onMouseDown = (e) => this._onMouseDown.fire(e);
viewEventBus.onMouseUp = (e) => this._onMouseUp.fire(e); viewEventBus.onMouseUp = (e) => this._onMouseUp.fire(e);
......
...@@ -68,17 +68,13 @@ export abstract class CommonCodeEditor extends Disposable { ...@@ -68,17 +68,13 @@ export abstract class CommonCodeEditor extends Disposable {
private readonly _onDidLayoutChange: Emitter<editorOptions.EditorLayoutInfo> = this._register(new Emitter<editorOptions.EditorLayoutInfo>()); private readonly _onDidLayoutChange: Emitter<editorOptions.EditorLayoutInfo> = this._register(new Emitter<editorOptions.EditorLayoutInfo>());
public readonly onDidLayoutChange: Event<editorOptions.EditorLayoutInfo> = this._onDidLayoutChange.event; public readonly onDidLayoutChange: Event<editorOptions.EditorLayoutInfo> = this._onDidLayoutChange.event;
protected readonly _onDidFocusEditorText: Emitter<void> = this._register(new Emitter<void>()); protected _editorTextFocus: BooleanEventEmitter = this._register(new BooleanEventEmitter());
public readonly onDidFocusEditorText: Event<void> = this._onDidFocusEditorText.event; public readonly onDidFocusEditorText: Event<void> = this._editorTextFocus.onDidChangeToTrue;
public readonly onDidBlurEditorText: Event<void> = this._editorTextFocus.onDidChangeToFalse;
protected readonly _onDidBlurEditorText: Emitter<void> = this._register(new Emitter<void>()); protected _editorFocus: BooleanEventEmitter = this._register(new BooleanEventEmitter());
public readonly onDidBlurEditorText: Event<void> = this._onDidBlurEditorText.event; public readonly onDidFocusEditor: Event<void> = this._editorFocus.onDidChangeToTrue;
public readonly onDidBlurEditor: Event<void> = this._editorFocus.onDidChangeToFalse;
protected readonly _onDidFocusEditor: Emitter<void> = this._register(new Emitter<void>());
public readonly onDidFocusEditor: Event<void> = this._onDidFocusEditor.event;
protected readonly _onDidBlurEditor: Emitter<void> = this._register(new Emitter<void>());
public readonly onDidBlurEditor: Event<void> = this._onDidBlurEditor.event;
private readonly _onWillType: Emitter<string> = this._register(new Emitter<string>()); private readonly _onWillType: Emitter<string> = this._register(new Emitter<string>());
public readonly onWillType = this._onWillType.event; public readonly onWillType = this._onWillType.event;
...@@ -1047,6 +1043,40 @@ export abstract class CommonCodeEditor extends Disposable { ...@@ -1047,6 +1043,40 @@ export abstract class CommonCodeEditor extends Disposable {
} }
} }
const enum BooleanEventValue {
NotSet,
False,
True
}
export class BooleanEventEmitter extends Disposable {
private readonly _onDidChangeToTrue: Emitter<void> = this._register(new Emitter<void>());
public readonly onDidChangeToTrue: Event<void> = this._onDidChangeToTrue.event;
private readonly _onDidChangeToFalse: Emitter<void> = this._register(new Emitter<void>());
public readonly onDidChangeToFalse: Event<void> = this._onDidChangeToFalse.event;
private _value: BooleanEventValue;
constructor() {
super();
this._value = BooleanEventValue.NotSet;
}
public setValue(_value: boolean) {
let value = (_value ? BooleanEventValue.True : BooleanEventValue.False);
if (this._value === value) {
return;
}
this._value = value;
if (this._value === BooleanEventValue.True) {
this._onDidChangeToTrue.fire();
} else if (this._value === BooleanEventValue.False) {
this._onDidChangeToFalse.fire();
}
}
}
class EditorContextKeysManager extends Disposable { class EditorContextKeysManager extends Disposable {
private _editor: CommonCodeEditor; private _editor: CommonCodeEditor;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册