sash.ts 13.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./sash';
S
Sandeep Somavarapu 已提交
9
import { IDisposable, Disposable, dispose } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
10 11
import { isIPad } from 'vs/base/browser/browser';
import { isMacintosh } from 'vs/base/common/platform';
12
import * as types from 'vs/base/common/types';
13
import { EventType, GestureEvent, Gesture } from 'vs/base/browser/touch';
J
Johannes Rieken 已提交
14
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
M
Matt Bierner 已提交
15
import { Event, Emitter } from 'vs/base/common/event';
16 17
import { getElementsByTagName, EventHelper, createStyleSheet, addDisposableListener, Dimension, append, $, addClass, removeClass } from 'vs/base/browser/dom';
import { domEvent } from 'vs/base/browser/event';
E
Erich Gamma 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

export interface ISashLayoutProvider { }

export interface IVerticalSashLayoutProvider extends ISashLayoutProvider {
	getVerticalSashLeft(sash: Sash): number;
	getVerticalSashTop?(sash: Sash): number;
	getVerticalSashHeight?(sash: Sash): number;
}

export interface IHorizontalSashLayoutProvider extends ISashLayoutProvider {
	getHorizontalSashTop(sash: Sash): number;
	getHorizontalSashLeft?(sash: Sash): number;
	getHorizontalSashWidth?(sash: Sash): number;
}

export interface ISashEvent {
	startX: number;
	currentX: number;
	startY: number;
	currentY: number;
38
	altKey: boolean;
E
Erich Gamma 已提交
39 40 41 42
}

export interface ISashOptions {
	orientation?: Orientation;
43 44
	orthogonalStartSash?: Sash;
	orthogonalEndSash?: Sash;
E
Erich Gamma 已提交
45 46 47 48 49 50 51
}

export enum Orientation {
	VERTICAL,
	HORIZONTAL
}

I
isidor 已提交
52
export class Sash {
E
Erich Gamma 已提交
53

54
	private el: HTMLElement;
E
Erich Gamma 已提交
55 56 57
	private layoutProvider: ISashLayoutProvider;
	private hidden: boolean;
	private orientation: Orientation;
58
	private disposables: IDisposable[] = [];
E
Erich Gamma 已提交
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	private _enabled = true;
	get enabled(): boolean { return this._enabled; }
	set enabled(enabled: boolean) {
		this._enabled = enabled;

		if (enabled) {
			removeClass(this.el, 'disabled');
		} else {
			addClass(this.el, 'disabled');
		}

		this._onDidEnablementChange.fire(enabled);
	}

	private readonly _onDidEnablementChange = new Emitter<boolean>();
	readonly onDidEnablementChange: Event<boolean> = this._onDidEnablementChange.event;

	private readonly _onDidStart = new Emitter<ISashEvent>();
	readonly onDidStart: Event<ISashEvent> = this._onDidStart.event;

	private readonly _onDidChange = new Emitter<ISashEvent>();
	readonly onDidChange: Event<ISashEvent> = this._onDidChange.event;

	private readonly _onDidReset = new Emitter<void>();
	readonly onDidReset: Event<void> = this._onDidReset.event;

	private readonly _onDidEnd = new Emitter<void>();
	readonly onDidEnd: Event<void> = this._onDidEnd.event;

	private orthogonalStartSashDisposables: IDisposable[] = [];
	private _orthogonalStartSash: Sash | undefined;
	get orthogonalStartSash(): Sash | undefined { return this._orthogonalStartSash; }
	set orthogonalStartSash(sash: Sash | undefined) {
		this.orthogonalStartSashDisposables = dispose(this.orthogonalStartSashDisposables);

		if (sash) {
			sash.onDidEnablementChange(this.onOrthogonalStartSashEnablementChange, this, this.orthogonalStartSashDisposables);
			this.onOrthogonalStartSashEnablementChange(sash.enabled);
		} else {
			this.onOrthogonalStartSashEnablementChange(false);
		}

		this._orthogonalStartSash = sash;
	}

	private orthogonalEndSashDisposables: IDisposable[] = [];
	private _orthogonalEndSash: Sash | undefined;
	get orthogonalEndSash(): Sash | undefined { return this._orthogonalEndSash; }
	set orthogonalEndSash(sash: Sash | undefined) {
		this.orthogonalEndSashDisposables = dispose(this.orthogonalEndSashDisposables);

		if (sash) {
			sash.onDidEnablementChange(this.onOrthogonalEndSashEnablementChange, this, this.orthogonalEndSashDisposables);
			this.onOrthogonalEndSashEnablementChange(sash.enabled);
		} else {
			this.onOrthogonalEndSashEnablementChange(false);
		}

		this._orthogonalEndSash = sash;
	}
J
Joao Moreno 已提交
120

E
Erich Gamma 已提交
121
	constructor(container: HTMLElement, layoutProvider: ISashLayoutProvider, options: ISashOptions = {}) {
122
		this.el = append(container, $('.monaco-sash'));
E
Erich Gamma 已提交
123

B
Benjamin Pasero 已提交
124
		if (isMacintosh) {
125
			addClass(this.el, 'mac');
B
Benjamin Pasero 已提交
126 127
		}

128 129
		domEvent(this.el, 'mousedown')(this.onMouseDown, this, this.disposables);
		domEvent(this.el, 'dblclick')(this.onMouseDoubleClick, this, this.disposables);
J
Joao Moreno 已提交
130

131 132
		Gesture.addTarget(this.el);
		domEvent(this.el, EventType.Start)(this.onTouchStart, this, this.disposables);
E
Erich Gamma 已提交
133

B
Benjamin Pasero 已提交
134
		if (isIPad) {
135 136
			// see also http://ux.stackexchange.com/questions/39023/what-is-the-optimum-button-size-of-touch-screen-applications
			addClass(this.el, 'touch');
E
Erich Gamma 已提交
137 138
		}

B
Benjamin Pasero 已提交
139
		this.setOrientation(options.orientation || Orientation.VERTICAL);
E
Erich Gamma 已提交
140 141 142

		this.hidden = false;
		this.layoutProvider = layoutProvider;
J
Joao Moreno 已提交
143

144 145
		this.orthogonalStartSash = options.orthogonalStartSash;
		this.orthogonalEndSash = options.orthogonalEndSash;
E
Erich Gamma 已提交
146 147
	}

148
	setOrientation(orientation: Orientation): void {
B
Benjamin Pasero 已提交
149 150 151
		this.orientation = orientation;

		if (this.orientation === Orientation.HORIZONTAL) {
152 153
			addClass(this.el, 'horizontal');
			removeClass(this.el, 'vertical');
B
Benjamin Pasero 已提交
154
		} else {
155 156
			removeClass(this.el, 'horizontal');
			addClass(this.el, 'vertical');
B
Benjamin Pasero 已提交
157 158 159 160 161 162 163
		}

		if (this.layoutProvider) {
			this.layout();
		}
	}

E
Erich Gamma 已提交
164
	private onMouseDown(e: MouseEvent): void {
165
		EventHelper.stop(e, false);
E
Erich Gamma 已提交
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		if (!(e as any).__orthogonalSashEvent) {
			let orthogonalSash: Sash | undefined;

			if (this.orientation === Orientation.VERTICAL) {
				if (e.offsetY <= 2) {
					orthogonalSash = this.orthogonalStartSash;
				} else if (e.offsetY >= this.el.clientHeight - 2) {
					orthogonalSash = this.orthogonalEndSash;
				}
			} else {
				if (e.offsetX <= 2) {
					orthogonalSash = this.orthogonalStartSash;
				} else if (e.offsetX >= this.el.clientWidth - 2) {
					orthogonalSash = this.orthogonalEndSash;
				}
			}

			if (orthogonalSash) {
				(e as any).__orthogonalSashEvent = true;
				orthogonalSash.onMouseDown(e);
			}
		}

		if (!this.enabled) {
E
Erich Gamma 已提交
191 192 193
			return;
		}

194 195 196
		const iframes = getElementsByTagName('iframe');
		for (const iframe of iframes) {
			iframe.style.pointerEvents = 'none'; // disable mouse events on iframes as long as we drag the sash
197
		}
198

199 200 201
		const mouseDownEvent = new StandardMouseEvent(e);
		const startX = mouseDownEvent.posx;
		const startY = mouseDownEvent.posy;
202
		const altKey = mouseDownEvent.altKey;
E
Erich Gamma 已提交
203

204
		const startEvent: ISashEvent = {
E
Erich Gamma 已提交
205 206 207
			startX: startX,
			currentX: startX,
			startY: startY,
208 209
			currentY: startY,
			altKey
E
Erich Gamma 已提交
210 211
		};

212
		addClass(this.el, 'active');
I
isidor 已提交
213
		this._onDidStart.fire(startEvent);
E
Erich Gamma 已提交
214

215
		// fix https://github.com/Microsoft/vscode/issues/21675
216
		const globalStyle = createStyleSheet(this.el);
217 218 219 220 221
		if (this.orientation === Orientation.HORIZONTAL) {
			globalStyle.innerHTML = `* { cursor: ${isMacintosh ? 'row-resize' : 'ns-resize'}; }`;
		} else {
			globalStyle.innerHTML = `* { cursor: ${isMacintosh ? 'col-resize' : 'ew-resize'}; }`;
		}
E
Erich Gamma 已提交
222

223 224 225
		const disposables: IDisposable[] = [];

		const onMouseMove = (e: MouseEvent) => {
226
			EventHelper.stop(e, false);
227
			const mouseMoveEvent = new StandardMouseEvent(e as MouseEvent);
E
Erich Gamma 已提交
228

229
			const event: ISashEvent = {
E
Erich Gamma 已提交
230 231 232
				startX: startX,
				currentX: mouseMoveEvent.posx,
				startY: startY,
233 234
				currentY: mouseMoveEvent.posy,
				altKey
E
Erich Gamma 已提交
235 236
			};

I
isidor 已提交
237
			this._onDidChange.fire(event);
238 239 240
		};

		const onMouseUp = (e: MouseEvent) => {
241
			EventHelper.stop(e, false);
242

243
			this.el.removeChild(globalStyle);
244

245
			removeClass(this.el, 'active');
I
isidor 已提交
246
			this._onDidEnd.fire();
E
Erich Gamma 已提交
247

248
			dispose(disposables);
249

250 251 252
			const iframes = getElementsByTagName('iframe');
			for (const iframe of iframes) {
				iframe.style.pointerEvents = 'auto';
253
			}
254 255 256 257 258 259 260 261
		};

		domEvent(window, 'mousemove')(onMouseMove, null, disposables);
		domEvent(window, 'mouseup')(onMouseUp, null, disposables);
	}

	private onMouseDoubleClick(event: MouseEvent): void {
		this._onDidReset.fire();
E
Erich Gamma 已提交
262 263
	}

B
Benjamin Pasero 已提交
264
	private onTouchStart(event: GestureEvent): void {
265
		EventHelper.stop(event);
E
Erich Gamma 已提交
266

267
		const listeners: IDisposable[] = [];
E
Erich Gamma 已提交
268

269 270
		const startX = event.pageX;
		const startY = event.pageY;
271 272
		const altKey = event.altKey;

E
Erich Gamma 已提交
273

I
isidor 已提交
274
		this._onDidStart.fire({
E
Erich Gamma 已提交
275 276 277
			startX: startX,
			currentX: startX,
			startY: startY,
278 279
			currentY: startY,
			altKey
E
Erich Gamma 已提交
280 281
		});

282
		listeners.push(addDisposableListener(this.el, EventType.Change, (event: GestureEvent) => {
B
Benjamin Pasero 已提交
283
			if (types.isNumber(event.pageX) && types.isNumber(event.pageY)) {
I
isidor 已提交
284
				this._onDidChange.fire({
E
Erich Gamma 已提交
285 286 287
					startX: startX,
					currentX: event.pageX,
					startY: startY,
288 289
					currentY: event.pageY,
					altKey
E
Erich Gamma 已提交
290 291 292 293
				});
			}
		}));

294
		listeners.push(addDisposableListener(this.el, EventType.End, (event: GestureEvent) => {
I
isidor 已提交
295
			this._onDidEnd.fire();
J
Joao Moreno 已提交
296
			dispose(listeners);
E
Erich Gamma 已提交
297 298 299
		}));
	}

300 301
	layout(): void {
		const size = isIPad ? 20 : 4;
J
Joao Moreno 已提交
302

E
Erich Gamma 已提交
303
		if (this.orientation === Orientation.VERTICAL) {
304
			const verticalProvider = (<IVerticalSashLayoutProvider>this.layoutProvider);
305
			this.el.style.left = verticalProvider.getVerticalSashLeft(this) - (size / 2) + 'px';
E
Erich Gamma 已提交
306 307

			if (verticalProvider.getVerticalSashTop) {
308
				this.el.style.top = verticalProvider.getVerticalSashTop(this) + 'px';
E
Erich Gamma 已提交
309 310 311
			}

			if (verticalProvider.getVerticalSashHeight) {
312
				this.el.style.height = verticalProvider.getVerticalSashHeight(this) + 'px';
E
Erich Gamma 已提交
313 314
			}
		} else {
315
			const horizontalProvider = (<IHorizontalSashLayoutProvider>this.layoutProvider);
316
			this.el.style.top = horizontalProvider.getHorizontalSashTop(this) - (size / 2) + 'px';
E
Erich Gamma 已提交
317 318

			if (horizontalProvider.getHorizontalSashLeft) {
319
				this.el.style.left = horizontalProvider.getHorizontalSashLeft(this) + 'px';
E
Erich Gamma 已提交
320 321 322
			}

			if (horizontalProvider.getHorizontalSashWidth) {
323
				this.el.style.width = horizontalProvider.getHorizontalSashWidth(this) + 'px';
E
Erich Gamma 已提交
324 325 326 327
			}
		}
	}

328
	show(): void {
E
Erich Gamma 已提交
329
		this.hidden = false;
330 331
		this.el.style.removeProperty('display');
		this.el.setAttribute('aria-hidden', 'false');
E
Erich Gamma 已提交
332 333
	}

334
	hide(): void {
E
Erich Gamma 已提交
335
		this.hidden = true;
336 337
		this.el.style.display = 'none';
		this.el.setAttribute('aria-hidden', 'true');
E
Erich Gamma 已提交
338 339
	}

340
	isHidden(): boolean {
E
Erich Gamma 已提交
341 342 343
		return this.hidden;
	}

344 345 346 347 348 349
	private onOrthogonalStartSashEnablementChange(enabled: boolean): void {
		if (enabled) {
			addClass(this.el, 'orthogonal-start');
		} else {
			removeClass(this.el, 'orthogonal-start');
		}
J
Joao Moreno 已提交
350 351
	}

352 353 354 355 356 357
	private onOrthogonalEndSashEnablementChange(enabled: boolean): void {
		if (enabled) {
			addClass(this.el, 'orthogonal-end');
		} else {
			removeClass(this.el, 'orthogonal-end');
		}
J
Joao Moreno 已提交
358 359
	}

360 361 362
	dispose(): void {
		this.orthogonalStartSashDisposables = dispose(this.orthogonalStartSashDisposables);
		this.orthogonalEndSashDisposables = dispose(this.orthogonalEndSashDisposables);
J
Joao Moreno 已提交
363

364 365
		if (this.el && this.el.parentElement) {
			this.el.parentElement.removeChild(this.el);
E
Erich Gamma 已提交
366
		}
367 368 369

		this.el = null;
		this.disposables = dispose(this.disposables);
E
Erich Gamma 已提交
370 371
	}
}
S
Sandeep Somavarapu 已提交
372 373 374 375 376 377 378 379 380 381 382 383

/**
 * A simple Vertical Sash that computes the position of the sash when it is moved between the given dimension.
 * Triggers onPositionChange event when the position is changed
 */
export class VSash extends Disposable implements IVerticalSashLayoutProvider {
	private sash: Sash;
	private ratio: number;
	private startPosition: number;
	private position: number;
	private dimension: Dimension;

M
Matt Bierner 已提交
384
	private readonly _onPositionChange: Emitter<number> = new Emitter<number>();
385
	get onPositionChange(): Event<number> { return this._onPositionChange.event; }
S
Sandeep Somavarapu 已提交
386

B
Benjamin Pasero 已提交
387
	constructor(container: HTMLElement, private minWidth: number) {
S
Sandeep Somavarapu 已提交
388
		super();
389

S
Sandeep Somavarapu 已提交
390 391 392
		this.ratio = 0.5;
		this.sash = new Sash(container, this);

I
isidor 已提交
393 394 395 396
		this._register(this.sash.onDidStart(() => this.onSashDragStart()));
		this._register(this.sash.onDidChange((e: ISashEvent) => this.onSashDrag(e)));
		this._register(this.sash.onDidEnd(() => this.onSashDragEnd()));
		this._register(this.sash.onDidReset(() => this.onSashReset()));
S
Sandeep Somavarapu 已提交
397 398
	}

399
	getVerticalSashTop(): number {
S
Sandeep Somavarapu 已提交
400 401 402
		return 0;
	}

403
	getVerticalSashLeft(): number {
S
Sandeep Somavarapu 已提交
404 405 406
		return this.position;
	}

407
	getVerticalSashHeight(): number {
S
Sandeep Somavarapu 已提交
408 409 410
		return this.dimension.height;
	}

411
	setDimenesion(dimension: Dimension) {
S
Sandeep Somavarapu 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
		this.dimension = dimension;
		this.compute(this.ratio);
	}

	private onSashDragStart(): void {
		this.startPosition = this.position;
	}

	private onSashDrag(e: ISashEvent): void {
		this.compute((this.startPosition + (e.currentX - e.startX)) / this.dimension.width);
	}

	private compute(ratio: number) {
		this.computeSashPosition(ratio);
		this.ratio = this.position / this.dimension.width;
		this._onPositionChange.fire(this.position);
	}

	private onSashDragEnd(): void {
		this.sash.layout();
	}

	private onSashReset(): void {
S
Sandeep Somavarapu 已提交
435
		this.compute(0.5);
S
Sandeep Somavarapu 已提交
436 437 438 439 440
		this._onPositionChange.fire(this.position);
		this.sash.layout();
	}

	private computeSashPosition(sashRatio: number = this.ratio) {
441
		const contentWidth = this.dimension.width;
S
Sandeep Somavarapu 已提交
442
		let sashPosition = Math.floor((sashRatio || 0.5) * contentWidth);
443
		const midPoint = Math.floor(0.5 * contentWidth);
S
Sandeep Somavarapu 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

		if (contentWidth > this.minWidth * 2) {
			if (sashPosition < this.minWidth) {
				sashPosition = this.minWidth;
			}
			if (sashPosition > contentWidth - this.minWidth) {
				sashPosition = contentWidth - this.minWidth;
			}
		} else {
			sashPosition = midPoint;
		}
		if (this.position !== sashPosition) {
			this.position = sashPosition;
			this.sash.layout();
		}
	}
460
}