viewOutgoingEvents.ts 5.5 KB
Newer Older
A
Alex Dima 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
A
Alex Dima 已提交
7 8 9
import { Disposable } from 'vs/base/common/lifecycle';
import { MouseTarget } from 'vs/editor/browser/controller/mouseTarget';
import { IEditorMouseEvent, IMouseTarget, IPartialEditorMouseEvent, MouseTargetType } from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
10 11
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
12
import { IScrollEvent } from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
13
import * as viewEvents from 'vs/editor/common/view/viewEvents';
14
import { IViewModel, ICoordinatesConverter } from 'vs/editor/common/viewModel/viewModel';
15
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
A
Alex Dima 已提交
16

A
Alex Dima 已提交
17 18 19
export interface EventCallback<T> {
	(event: T): void;
}
A
Alex Dima 已提交
20

A
Alex Dima 已提交
21
export class ViewOutgoingEvents extends Disposable {
A
Alex Dima 已提交
22

23 24 25 26 27 28 29
	public onDidScroll: EventCallback<IScrollEvent> | null = null;
	public onDidGainFocus: EventCallback<void> | null = null;
	public onDidLoseFocus: EventCallback<void> | null = null;
	public onKeyDown: EventCallback<IKeyboardEvent> | null = null;
	public onKeyUp: EventCallback<IKeyboardEvent> | null = null;
	public onContextMenu: EventCallback<IEditorMouseEvent> | null = null;
	public onMouseMove: EventCallback<IEditorMouseEvent> | null = null;
A
Alex Dima 已提交
30
	public onMouseLeave: EventCallback<IPartialEditorMouseEvent> | null = null;
31
	public onMouseDown: EventCallback<IEditorMouseEvent> | null = null;
32
	public onMouseUp: EventCallback<IEditorMouseEvent> | null = null;
33
	public onMouseDrag: EventCallback<IEditorMouseEvent> | null = null;
A
Alex Dima 已提交
34
	public onMouseDrop: EventCallback<IPartialEditorMouseEvent> | null = null;
35
	public onMouseWheel: EventCallback<IMouseWheelEvent> | null = null;
A
Alex Dima 已提交
36

37
	private readonly _viewModel: IViewModel;
A
Alex Dima 已提交
38 39 40

	constructor(viewModel: IViewModel) {
		super();
A
Alex Dima 已提交
41
		this._viewModel = viewModel;
A
Alex Dima 已提交
42 43
	}

A
Alex Dima 已提交
44
	public emitScrollChanged(e: viewEvents.ViewScrollChangedEvent): void {
A
Alex Dima 已提交
45 46 47
		if (this.onDidScroll) {
			this.onDidScroll(e);
		}
A
Alex Dima 已提交
48 49 50
	}

	public emitViewFocusGained(): void {
A
Alex Dima 已提交
51
		if (this.onDidGainFocus) {
R
Rob Lourens 已提交
52
			this.onDidGainFocus(undefined);
A
Alex Dima 已提交
53
		}
A
Alex Dima 已提交
54 55 56
	}

	public emitViewFocusLost(): void {
A
Alex Dima 已提交
57
		if (this.onDidLoseFocus) {
R
Rob Lourens 已提交
58
			this.onDidLoseFocus(undefined);
A
Alex Dima 已提交
59
		}
A
Alex Dima 已提交
60 61 62
	}

	public emitKeyDown(e: IKeyboardEvent): void {
A
Alex Dima 已提交
63 64 65
		if (this.onKeyDown) {
			this.onKeyDown(e);
		}
A
Alex Dima 已提交
66 67 68
	}

	public emitKeyUp(e: IKeyboardEvent): void {
A
Alex Dima 已提交
69 70 71
		if (this.onKeyUp) {
			this.onKeyUp(e);
		}
A
Alex Dima 已提交
72 73 74
	}

	public emitContextMenu(e: IEditorMouseEvent): void {
A
Alex Dima 已提交
75 76 77
		if (this.onContextMenu) {
			this.onContextMenu(this._convertViewToModelMouseEvent(e));
		}
A
Alex Dima 已提交
78 79 80
	}

	public emitMouseMove(e: IEditorMouseEvent): void {
A
Alex Dima 已提交
81 82 83
		if (this.onMouseMove) {
			this.onMouseMove(this._convertViewToModelMouseEvent(e));
		}
A
Alex Dima 已提交
84 85
	}

A
Alex Dima 已提交
86
	public emitMouseLeave(e: IPartialEditorMouseEvent): void {
A
Alex Dima 已提交
87 88 89
		if (this.onMouseLeave) {
			this.onMouseLeave(this._convertViewToModelMouseEvent(e));
		}
A
Alex Dima 已提交
90 91 92
	}

	public emitMouseDown(e: IEditorMouseEvent): void {
A
Alex Dima 已提交
93 94 95
		if (this.onMouseDown) {
			this.onMouseDown(this._convertViewToModelMouseEvent(e));
		}
A
Alex Dima 已提交
96 97
	}

98 99 100 101 102 103
	public emitMouseUp(e: IEditorMouseEvent): void {
		if (this.onMouseUp) {
			this.onMouseUp(this._convertViewToModelMouseEvent(e));
		}
	}

104
	public emitMouseDrag(e: IEditorMouseEvent): void {
A
Alex Dima 已提交
105 106 107
		if (this.onMouseDrag) {
			this.onMouseDrag(this._convertViewToModelMouseEvent(e));
		}
108 109
	}

A
Alex Dima 已提交
110
	public emitMouseDrop(e: IPartialEditorMouseEvent): void {
A
Alex Dima 已提交
111 112 113
		if (this.onMouseDrop) {
			this.onMouseDrop(this._convertViewToModelMouseEvent(e));
		}
114 115
	}

116 117 118 119 120 121
	public emitMouseWheel(e: IMouseWheelEvent): void {
		if (this.onMouseWheel) {
			this.onMouseWheel(e);
		}
	}

A
Alex Dima 已提交
122 123 124
	private _convertViewToModelMouseEvent(e: IEditorMouseEvent): IEditorMouseEvent;
	private _convertViewToModelMouseEvent(e: IPartialEditorMouseEvent): IPartialEditorMouseEvent;
	private _convertViewToModelMouseEvent(e: IEditorMouseEvent | IPartialEditorMouseEvent): IEditorMouseEvent | IPartialEditorMouseEvent {
A
Alex Dima 已提交
125 126 127 128 129 130 131 132 133 134
		if (e.target) {
			return {
				event: e.event,
				target: this._convertViewToModelMouseTarget(e.target)
			};
		}
		return e;
	}

	private _convertViewToModelMouseTarget(target: IMouseTarget): IMouseTarget {
135 136 137 138
		return ViewOutgoingEvents.convertViewToModelMouseTarget(target, this._viewModel.coordinatesConverter);
	}

	public static convertViewToModelMouseTarget(target: IMouseTarget, coordinatesConverter: ICoordinatesConverter): IMouseTarget {
139 140 141 142
		return new ExternalMouseTarget(
			target.element,
			target.type,
			target.mouseColumn,
143 144
			target.position ? coordinatesConverter.convertViewPositionToModelPosition(target.position) : null,
			target.range ? coordinatesConverter.convertViewRangeToModelRange(target.range) : null,
145 146
			target.detail
		);
A
Alex Dima 已提交
147 148
	}
}
149 150 151

class ExternalMouseTarget implements IMouseTarget {

A
Alex Dima 已提交
152
	public readonly element: Element | null;
153 154
	public readonly type: MouseTargetType;
	public readonly mouseColumn: number;
A
Alex Dima 已提交
155 156
	public readonly position: Position | null;
	public readonly range: Range | null;
157 158
	public readonly detail: any;

A
Alex Dima 已提交
159
	constructor(element: Element | null, type: MouseTargetType, mouseColumn: number, position: Position | null, range: Range | null, detail: any) {
160 161 162 163 164 165 166 167 168 169 170 171
		this.element = element;
		this.type = type;
		this.mouseColumn = mouseColumn;
		this.position = position;
		this.range = range;
		this.detail = detail;
	}

	public toString(): string {
		return MouseTarget.toString(this);
	}
}