sash.ts 6.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';
B
Benjamin Pasero 已提交
9 10 11 12
import {IDisposable, disposeAll} from 'vs/base/common/lifecycle';
import {Builder, $} from 'vs/base/browser/builder';
import {isIPad} from 'vs/base/browser/browser';
import types = require('vs/base/common/types');
E
Erich Gamma 已提交
13
import DOM = require('vs/base/browser/dom');
B
Benjamin Pasero 已提交
14 15 16
import {Gesture, EventType, GestureEvent} from 'vs/base/browser/touch';
import {EventEmitter} from 'vs/base/common/eventEmitter';
import {StandardMouseEvent} from 'vs/base/browser/mouseEvent';
E
Erich Gamma 已提交
17 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 50

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;
	instantDiffX: number;
	startY: number;
	currentY: number;
	instantDiffY: number;
}

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

export enum Orientation {
	VERTICAL,
	HORIZONTAL
}

B
Benjamin Pasero 已提交
51
export class Sash extends EventEmitter {
E
Erich Gamma 已提交
52

B
Benjamin Pasero 已提交
53 54
	private $e: Builder;
	private gesture: Gesture;
E
Erich Gamma 已提交
55 56 57 58 59 60 61 62 63 64 65
	private layoutProvider: ISashLayoutProvider;
	private isDisabled: boolean;
	private hidden: boolean;
	private orientation: Orientation;
	private size: number;

	constructor(container: HTMLElement, layoutProvider: ISashLayoutProvider, options: ISashOptions = {}) {
		super();

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

B
Benjamin Pasero 已提交
66
		this.gesture = new Gesture(this.$e.getHTMLElement());
E
Erich Gamma 已提交
67 68

		this.$e.on('mousedown', (e: MouseEvent) => { this.onMouseDown(e); });
B
Benjamin Pasero 已提交
69
		this.$e.on(EventType.Start, (e: GestureEvent) => { this.onTouchStart(e); });
E
Erich Gamma 已提交
70 71 72 73 74 75

		this.orientation = options.orientation || Orientation.VERTICAL;
		this.$e.addClass(this.orientation === Orientation.HORIZONTAL ? 'horizontal' : 'vertical');

		this.size = options.baseSize || 5;

B
Benjamin Pasero 已提交
76
		if (isIPad) {
E
Erich Gamma 已提交
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
			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');
		}

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

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

	public getHTMLElement(): HTMLElement {
		return this.$e.getHTMLElement();
	}

	private onMouseDown(e: MouseEvent): void {
		DOM.EventHelper.stop(e, false);

		if (this.isDisabled) {
			return;
		}

B
Benjamin Pasero 已提交
103 104 105
		let mouseDownEvent = new StandardMouseEvent(e);
		let startX = mouseDownEvent.posx;
		let startY = mouseDownEvent.posy;
E
Erich Gamma 已提交
106

B
Benjamin Pasero 已提交
107
		let startEvent: ISashEvent = {
E
Erich Gamma 已提交
108 109 110 111 112 113 114 115 116 117 118
			startX: startX,
			currentX: startX,
			instantDiffX: 0,
			startY: startY,
			currentY: startY,
			instantDiffY: 0
		};

		this.$e.addClass('active');
		this.emit('start', startEvent);

B
Benjamin Pasero 已提交
119
		let overlayDiv = $('div').style({
E
Erich Gamma 已提交
120 121 122 123 124 125 126 127 128
			position: 'absolute',
			top: 0,
			left: 0,
			width: '100%',
			height: '100%',
			zIndex: 1000000,
			cursor: this.orientation === Orientation.VERTICAL ? 'ew-resize' : 'ns-resize'
		});

B
Benjamin Pasero 已提交
129
		let $window = $(window);
E
Erich Gamma 已提交
130

B
Benjamin Pasero 已提交
131 132
		let lastCurrentX = startX;
		let lastCurrentY = startY;
E
Erich Gamma 已提交
133 134 135

		$window.on('mousemove', (e: MouseEvent) => {
			DOM.EventHelper.stop(e, false);
B
Benjamin Pasero 已提交
136
			let mouseMoveEvent = new StandardMouseEvent(e);
E
Erich Gamma 已提交
137

B
Benjamin Pasero 已提交
138
			let event: ISashEvent = {
E
Erich Gamma 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
				startX: startX,
				currentX: mouseMoveEvent.posx,
				instantDiffX: mouseMoveEvent.posx - lastCurrentX,
				startY: startY,
				currentY: mouseMoveEvent.posy,
				instantDiffY: mouseMoveEvent.posy - lastCurrentY
			};

			lastCurrentX = mouseMoveEvent.posx;
			lastCurrentY = mouseMoveEvent.posy;

			this.emit('change', event);
		}).once('mouseup', (e: MouseEvent) => {
			DOM.EventHelper.stop(e, false);
			this.$e.removeClass('active');
			this.emit('end');

			$window.off('mousemove');
			overlayDiv.destroy();
		});

		overlayDiv.appendTo(document.body);
	}

B
Benjamin Pasero 已提交
163
	private onTouchStart(event: GestureEvent): void {
E
Erich Gamma 已提交
164 165
		DOM.EventHelper.stop(event);

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

B
Benjamin Pasero 已提交
168 169
		let startX = event.pageX;
		let startY = event.pageY;
E
Erich Gamma 已提交
170 171 172 173 174 175 176 177 178 179

		this.emit('start', {
			startX: startX,
			currentX: startX,
			instantDiffX: 0,
			startY: startY,
			currentY: startY,
			instantDiffY: 0
		});

B
Benjamin Pasero 已提交
180 181
		let lastCurrentX = startX;
		let lastCurrentY = startY;
E
Erich Gamma 已提交
182

B
Benjamin Pasero 已提交
183 184
		listeners.push(DOM.addDisposableListener(this.$e.getHTMLElement(), EventType.Change, (event: GestureEvent) => {
			if (types.isNumber(event.pageX) && types.isNumber(event.pageY)) {
E
Erich Gamma 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
				this.emit('change', {
					startX: startX,
					currentX: event.pageX,
					instantDiffX: event.pageX - lastCurrentX,
					startY: startY,
					currentY: event.pageY,
					instantDiffY: event.pageY - lastCurrentY
				});

				lastCurrentX = event.pageX;
				lastCurrentY = event.pageY;
			}
		}));

B
Benjamin Pasero 已提交
199
		listeners.push(DOM.addDisposableListener(this.$e.getHTMLElement(), EventType.End, (event: GestureEvent) => {
E
Erich Gamma 已提交
200
			this.emit('end');
B
Benjamin Pasero 已提交
201
			disposeAll(listeners);
E
Erich Gamma 已提交
202 203 204 205
		}));
	}

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

		if (this.orientation === Orientation.VERTICAL) {
B
Benjamin Pasero 已提交
209
			let verticalProvider = (<IVerticalSashLayoutProvider>this.layoutProvider);
E
Erich Gamma 已提交
210 211 212 213 214 215 216 217 218 219
			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 已提交
220
			let horizontalProvider = (<IHorizontalSashLayoutProvider>this.layoutProvider);
E
Erich Gamma 已提交
221 222 223
			style = { top: horizontalProvider.getHorizontalSashTop(this) - (this.size / 2) + 'px' };

			if (horizontalProvider.getHorizontalSashLeft) {
J
Joao Moreno 已提交
224
				style.left = horizontalProvider.getHorizontalSashLeft(this) + 'px';
E
Erich Gamma 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 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
			}

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

	public dispose(): void {
		if (this.$e) {
			this.$e.destroy();
			this.$e = null;
		}

		super.dispose();
	}
}