sash.ts 9.8 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 10
import { IDisposable, Disposable, dispose } from 'vs/base/common/lifecycle';
import { Builder, $, Dimension } from 'vs/base/browser/builder';
J
Johannes Rieken 已提交
11 12
import { isIPad } from 'vs/base/browser/browser';
import { isMacintosh } from 'vs/base/common/platform';
B
Benjamin Pasero 已提交
13
import types = require('vs/base/common/types');
E
Erich Gamma 已提交
14
import DOM = require('vs/base/browser/dom');
15
import { EventType, GestureEvent, Gesture } from 'vs/base/browser/touch';
J
Johannes Rieken 已提交
16
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
S
Sandeep Somavarapu 已提交
17
import Event, { Emitter } from 'vs/base/common/event';
E
Erich Gamma 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

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;
}

export interface ISashOptions {
	baseSize?: number;
	orientation?: Orientation;
}

export enum Orientation {
	VERTICAL,
	HORIZONTAL
}

I
isidor 已提交
50
export class Sash {
E
Erich Gamma 已提交
51

B
Benjamin Pasero 已提交
52
	private $e: Builder;
E
Erich Gamma 已提交
53 54 55 56 57 58
	private layoutProvider: ISashLayoutProvider;
	private isDisabled: boolean;
	private hidden: boolean;
	private orientation: Orientation;
	private size: number;

I
isidor 已提交
59 60 61 62 63
	private _onDidStart = new Emitter<ISashEvent>();
	private _onDidChange = new Emitter<ISashEvent>();
	private _onDidReset = new Emitter<void>();
	private _onDidEnd = new Emitter<void>();

E
Erich Gamma 已提交
64 65 66 67
	constructor(container: HTMLElement, layoutProvider: ISashLayoutProvider, options: ISashOptions = {}) {

		this.$e = $('.monaco-sash').appendTo(container);

B
Benjamin Pasero 已提交
68 69 70 71
		if (isMacintosh) {
			this.$e.addClass('mac');
		}

72
		this.$e.on(DOM.EventType.MOUSE_DOWN, (e) => { this.onMouseDown(e as MouseEvent); });
I
isidor 已提交
73
		this.$e.on(DOM.EventType.DBLCLICK, (e) => this._onDidReset.fire());
74
		Gesture.addTarget(this.$e.getHTMLElement());
75
		this.$e.on(EventType.Start, (e) => { this.onTouchStart(e as GestureEvent); });
E
Erich Gamma 已提交
76 77 78

		this.size = options.baseSize || 5;

B
Benjamin Pasero 已提交
79
		if (isIPad) {
E
Erich Gamma 已提交
80 81 82 83
			this.size *= 4; // see also http://ux.stackexchange.com/questions/39023/what-is-the-optimum-button-size-of-touch-screen-applications
			this.$e.addClass('touch');
		}

B
Benjamin Pasero 已提交
84
		this.setOrientation(options.orientation || Orientation.VERTICAL);
E
Erich Gamma 已提交
85 86 87 88 89 90

		this.isDisabled = false;
		this.hidden = false;
		this.layoutProvider = layoutProvider;
	}

I
isidor 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	public get onDidStart(): Event<ISashEvent> {
		return this._onDidStart.event;
	}

	public get onDidChange(): Event<ISashEvent> {
		return this._onDidChange.event;
	}

	public get onDidReset(): Event<void> {
		return this._onDidReset.event;
	}

	public get onDidEnd(): Event<void> {
		return this._onDidEnd.event;
	}

B
Benjamin Pasero 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	public setOrientation(orientation: Orientation): void {
		this.orientation = orientation;

		this.$e.removeClass('horizontal', 'vertical');
		this.$e.addClass(this.getOrientation());

		if (this.orientation === Orientation.HORIZONTAL) {
			this.$e.size(null, this.size);
		} else {
			this.$e.size(this.size);
		}

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

M
Maxime Quandalle 已提交
124 125 126 127
	private getOrientation(): 'horizontal' | 'vertical' {
		return this.orientation === Orientation.HORIZONTAL ? 'horizontal' : 'vertical';
	}

E
Erich Gamma 已提交
128 129 130 131 132 133 134
	private onMouseDown(e: MouseEvent): void {
		DOM.EventHelper.stop(e, false);

		if (this.isDisabled) {
			return;
		}

135 136 137 138
		const iframes = $(DOM.getElementsByTagName('iframe'));
		if (iframes) {
			iframes.style('pointer-events', 'none'); // disable mouse events on iframes as long as we drag the sash
		}
139

B
Benjamin Pasero 已提交
140 141 142
		let mouseDownEvent = new StandardMouseEvent(e);
		let startX = mouseDownEvent.posx;
		let startY = mouseDownEvent.posy;
E
Erich Gamma 已提交
143

B
Benjamin Pasero 已提交
144
		let startEvent: ISashEvent = {
E
Erich Gamma 已提交
145 146 147
			startX: startX,
			currentX: startX,
			startY: startY,
B
Benjamin Pasero 已提交
148
			currentY: startY
E
Erich Gamma 已提交
149 150 151
		};

		this.$e.addClass('active');
I
isidor 已提交
152
		this._onDidStart.fire(startEvent);
E
Erich Gamma 已提交
153

B
Benjamin Pasero 已提交
154
		let $window = $(window);
Y
Yuki Ueda 已提交
155
		let containerCSSClass = `${this.getOrientation()}-cursor-container${isMacintosh ? '-mac' : ''}`;
E
Erich Gamma 已提交
156

157
		$window.on('mousemove', (e) => {
E
Erich Gamma 已提交
158
			DOM.EventHelper.stop(e, false);
159
			let mouseMoveEvent = new StandardMouseEvent(e as MouseEvent);
E
Erich Gamma 已提交
160

B
Benjamin Pasero 已提交
161
			let event: ISashEvent = {
E
Erich Gamma 已提交
162 163 164
				startX: startX,
				currentX: mouseMoveEvent.posx,
				startY: startY,
B
Benjamin Pasero 已提交
165
				currentY: mouseMoveEvent.posy
E
Erich Gamma 已提交
166 167
			};

I
isidor 已提交
168
			this._onDidChange.fire(event);
169
		}).once('mouseup', (e) => {
E
Erich Gamma 已提交
170 171
			DOM.EventHelper.stop(e, false);
			this.$e.removeClass('active');
I
isidor 已提交
172
			this._onDidEnd.fire();
E
Erich Gamma 已提交
173 174

			$window.off('mousemove');
Y
Yuki Ueda 已提交
175
			document.body.classList.remove(containerCSSClass);
176

177 178 179 180
			const iframes = $(DOM.getElementsByTagName('iframe'));
			if (iframes) {
				iframes.style('pointer-events', 'auto');
			}
E
Erich Gamma 已提交
181 182
		});

Y
Yuki Ueda 已提交
183
		document.body.classList.add(containerCSSClass);
E
Erich Gamma 已提交
184 185
	}

B
Benjamin Pasero 已提交
186
	private onTouchStart(event: GestureEvent): void {
E
Erich Gamma 已提交
187 188
		DOM.EventHelper.stop(event);

B
Benjamin Pasero 已提交
189
		let listeners: IDisposable[] = [];
E
Erich Gamma 已提交
190

B
Benjamin Pasero 已提交
191 192
		let startX = event.pageX;
		let startY = event.pageY;
E
Erich Gamma 已提交
193

I
isidor 已提交
194
		this._onDidStart.fire({
E
Erich Gamma 已提交
195 196 197
			startX: startX,
			currentX: startX,
			startY: startY,
B
Benjamin Pasero 已提交
198
			currentY: startY
E
Erich Gamma 已提交
199 200
		});

B
Benjamin Pasero 已提交
201 202
		listeners.push(DOM.addDisposableListener(this.$e.getHTMLElement(), EventType.Change, (event: GestureEvent) => {
			if (types.isNumber(event.pageX) && types.isNumber(event.pageY)) {
I
isidor 已提交
203
				this._onDidChange.fire({
E
Erich Gamma 已提交
204 205 206
					startX: startX,
					currentX: event.pageX,
					startY: startY,
B
Benjamin Pasero 已提交
207
					currentY: event.pageY
E
Erich Gamma 已提交
208 209 210 211
				});
			}
		}));

B
Benjamin Pasero 已提交
212
		listeners.push(DOM.addDisposableListener(this.$e.getHTMLElement(), EventType.End, (event: GestureEvent) => {
I
isidor 已提交
213
			this._onDidEnd.fire();
J
Joao Moreno 已提交
214
			dispose(listeners);
E
Erich Gamma 已提交
215 216 217 218
		}));
	}

	public layout(): void {
B
Benjamin Pasero 已提交
219
		let style: { top?: string; left?: string; height?: string; width?: string; };
E
Erich Gamma 已提交
220 221

		if (this.orientation === Orientation.VERTICAL) {
B
Benjamin Pasero 已提交
222
			let verticalProvider = (<IVerticalSashLayoutProvider>this.layoutProvider);
E
Erich Gamma 已提交
223 224 225 226 227 228 229 230 231 232
			style = { left: verticalProvider.getVerticalSashLeft(this) - (this.size / 2) + 'px' };

			if (verticalProvider.getVerticalSashTop) {
				style.top = verticalProvider.getVerticalSashTop(this) + 'px';
			}

			if (verticalProvider.getVerticalSashHeight) {
				style.height = verticalProvider.getVerticalSashHeight(this) + 'px';
			}
		} else {
B
Benjamin Pasero 已提交
233
			let horizontalProvider = (<IHorizontalSashLayoutProvider>this.layoutProvider);
E
Erich Gamma 已提交
234 235 236
			style = { top: horizontalProvider.getHorizontalSashTop(this) - (this.size / 2) + 'px' };

			if (horizontalProvider.getHorizontalSashLeft) {
J
Joao Moreno 已提交
237
				style.left = horizontalProvider.getHorizontalSashLeft(this) + 'px';
E
Erich Gamma 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
			}

			if (horizontalProvider.getHorizontalSashWidth) {
				style.width = horizontalProvider.getHorizontalSashWidth(this) + 'px';
			}
		}

		this.$e.style(style);
	}

	public show(): void {
		this.hidden = false;
		this.$e.show();
	}

	public hide(): void {
		this.hidden = true;
		this.$e.hide();
	}

	public isHidden(): boolean {
		return this.hidden;
	}

	public enable(): void {
		this.$e.removeClass('disabled');
		this.isDisabled = false;
	}

	public disable(): void {
		this.$e.addClass('disabled');
		this.isDisabled = true;
	}

J
Joao Moreno 已提交
272 273 274 275
	get enabled(): boolean {
		return !this.isDisabled;
	}

E
Erich Gamma 已提交
276 277 278 279 280 281 282
	public dispose(): void {
		if (this.$e) {
			this.$e.destroy();
			this.$e = null;
		}
	}
}
S
Sandeep Somavarapu 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

/**
 * 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;

	private _onPositionChange: Emitter<number> = new Emitter<number>();
	public get onPositionChange(): Event<number> { return this._onPositionChange.event; }

B
Benjamin Pasero 已提交
299
	constructor(container: HTMLElement, private minWidth: number) {
S
Sandeep Somavarapu 已提交
300 301 302 303
		super();
		this.ratio = 0.5;
		this.sash = new Sash(container, this);

I
isidor 已提交
304 305 306 307
		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 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	}

	public getVerticalSashTop(): number {
		return 0;
	}

	public getVerticalSashLeft(): number {
		return this.position;
	}

	public getVerticalSashHeight(): number {
		return this.dimension.height;
	}

	public setDimenesion(dimension: Dimension) {
		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 已提交
346
		this.compute(0.5);
S
Sandeep Somavarapu 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
		this._onPositionChange.fire(this.position);
		this.sash.layout();
	}

	private computeSashPosition(sashRatio: number = this.ratio) {
		let contentWidth = this.dimension.width;
		let sashPosition = Math.floor((sashRatio || 0.5) * contentWidth);
		let midPoint = Math.floor(0.5 * contentWidth);

		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();
		}
	}
}