scrollableElement.ts 15.5 KB
Newer Older
E
Erich Gamma 已提交
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.
 *--------------------------------------------------------------------------------------------*/
'use strict';

A
Alex Dima 已提交
7 8
import 'vs/css!./media/scrollbars';

9
import * as Browser from 'vs/base/browser/browser';
A
Alex Dima 已提交
10 11 12 13 14
import * as DomUtils from 'vs/base/browser/dom';
import * as Platform from 'vs/base/common/platform';
import {StandardMouseWheelEvent, IMouseEvent} from 'vs/base/browser/mouseEvent';
import {HorizontalScrollbar} from 'vs/base/browser/ui/scrollbar/horizontalScrollbar';
import {VerticalScrollbar} from 'vs/base/browser/ui/scrollbar/verticalScrollbar';
15
import {ScrollableElementCreationOptions, ScrollableElementChangeOptions, ScrollableElementResolvedOptions} from 'vs/base/browser/ui/scrollbar/scrollableElementOptions';
A
Alex Dima 已提交
16
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
17
import {Scrollable, ScrollEvent, INewScrollState, ScrollbarVisibility} from 'vs/base/common/scrollable';
A
Alex Dima 已提交
18 19 20 21
import {Widget} from 'vs/base/browser/ui/widget';
import {TimeoutTimer} from 'vs/base/common/async';
import {FastDomNode, createFastDomNode} from 'vs/base/browser/styleMutator';
import {ScrollbarHost} from 'vs/base/browser/ui/scrollbar/abstractScrollbar';
22
import Event, {Emitter} from 'vs/base/common/event';
A
Alex Dima 已提交
23 24 25 26 27 28 29 30 31 32 33 34

const HIDE_TIMEOUT = 500;
const SCROLL_WHEEL_SENSITIVITY = 50;

export interface IOverviewRulerLayoutInfo {
	parent: HTMLElement;
	insertBefore: HTMLElement;
}

export class ScrollableElement extends Widget {

	private _options: ScrollableElementResolvedOptions;
35
	private _scrollable: Scrollable;
A
Alex Dima 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	private _verticalScrollbar: VerticalScrollbar;
	private _horizontalScrollbar: HorizontalScrollbar;
	private _domNode: HTMLElement;

	private _leftShadowDomNode: FastDomNode;
	private _topShadowDomNode: FastDomNode;
	private _topLeftShadowDomNode: FastDomNode;

	private _listenOnDomNode: HTMLElement;

	private _mouseWheelToDispose: IDisposable[];

	private _isDragging: boolean;
	private _mouseIsOver: boolean;

	private _hideTimeout: TimeoutTimer;
	private _shouldRender: boolean;

54 55 56 57
	private _onScroll = this._register(new Emitter<ScrollEvent>());
	public onScroll: Event<ScrollEvent> = this._onScroll.event;

	constructor(element: HTMLElement, options: ScrollableElementCreationOptions) {
A
Alex Dima 已提交
58 59 60 61
		super();
		element.style.overflow = 'hidden';
		this._options = resolveOptions(options);

62 63 64 65 66 67 68
		this._scrollable = this._register(new Scrollable());
		this._register(this._scrollable.onScroll((e) => {
			this._onDidScroll(e);
			this._onScroll.fire(e);
		}));

		// this._scrollable = this._register(new DelegateScrollable(scrollable, () => this._onScroll()));
A
Alex Dima 已提交
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 120

		let scrollbarHost:ScrollbarHost = {
			onMouseWheel: (mouseWheelEvent: StandardMouseWheelEvent) => this._onMouseWheel(mouseWheelEvent),
			onDragStart: () => this._onDragStart(),
			onDragEnd: () => this._onDragEnd(),
		};
		this._verticalScrollbar = this._register(new VerticalScrollbar(this._scrollable, this._options, scrollbarHost));
		this._horizontalScrollbar = this._register(new HorizontalScrollbar(this._scrollable, this._options, scrollbarHost));

		this._domNode = document.createElement('div');
		this._domNode.className = 'monaco-scrollable-element ' + this._options.className;
		this._domNode.setAttribute('role', 'presentation');
		this._domNode.style.position = 'relative';
		this._domNode.style.overflow = 'hidden';
		this._domNode.appendChild(element);
		this._domNode.appendChild(this._horizontalScrollbar.domNode.domNode);
		this._domNode.appendChild(this._verticalScrollbar.domNode.domNode);

		if (this._options.useShadows) {
			this._leftShadowDomNode = createFastDomNode(document.createElement('div'));
			this._leftShadowDomNode.setClassName('shadow');
			this._domNode.appendChild(this._leftShadowDomNode.domNode);

			this._topShadowDomNode = createFastDomNode(document.createElement('div'));
			this._topShadowDomNode.setClassName('shadow');
			this._domNode.appendChild(this._topShadowDomNode.domNode);

			this._topLeftShadowDomNode = createFastDomNode(document.createElement('div'));
			this._topLeftShadowDomNode.setClassName('shadow top-left-corner');
			this._domNode.appendChild(this._topLeftShadowDomNode.domNode);
		}

		this._listenOnDomNode = this._options.listenOnDomNode || this._domNode;

		this._mouseWheelToDispose = [];
		this._setListeningToMouseWheel(this._options.handleMouseWheel);

		this.onmouseover(this._listenOnDomNode, (e) => this._onMouseOver(e));
		this.onnonbubblingmouseout(this._listenOnDomNode, (e) => this._onMouseOut(e));

		this._hideTimeout = this._register(new TimeoutTimer());
		this._isDragging = false;
		this._mouseIsOver = false;

		this._shouldRender = true;
	}

	public dispose(): void {
		this._mouseWheelToDispose = dispose(this._mouseWheelToDispose);
		super.dispose();
	}

E
Erich Gamma 已提交
121
	/**
A
Alex Dima 已提交
122
	 * Get the generated 'scrollable' dom node
E
Erich Gamma 已提交
123
	 */
A
Alex Dima 已提交
124 125 126 127 128 129 130 131 132 133 134
	public getDomNode(): HTMLElement {
		return this._domNode;
	}

	public getOverviewRulerLayoutInfo(): IOverviewRulerLayoutInfo {
		return {
			parent: this._domNode,
			insertBefore: this._verticalScrollbar.domNode.domNode,
		};
	}

E
Erich Gamma 已提交
135
	/**
A
Alex Dima 已提交
136 137
	 * Delegate a mouse down event to the vertical scrollbar.
	 * This is to help with clicking somewhere else and having the scrollbar react.
E
Erich Gamma 已提交
138
	 */
A
Alex Dima 已提交
139 140 141 142
	public delegateVerticalScrollbarMouseDown(browserEvent: MouseEvent): void {
		this._verticalScrollbar.delegateMouseDown(browserEvent);
	}

143 144
	public updateState(newState:INewScrollState): void {
		this._scrollable.updateState(newState);
A
Alex Dima 已提交
145 146
	}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	public getWidth(): number {
		return this._scrollable.getWidth();
	}
	public getScrollWidth(): number {
		return this._scrollable.getScrollWidth();
	}
	public getScrollLeft(): number {
		return this._scrollable.getScrollLeft();
	}

	public getHeight(): number {
		return this._scrollable.getHeight();
	}
	public getScrollHeight(): number {
		return this._scrollable.getScrollHeight();
	}
	public getScrollTop(): number {
		return this._scrollable.getScrollTop();
A
Alex Dima 已提交
165 166
	}

E
Erich Gamma 已提交
167
	/**
A
Alex Dima 已提交
168
	 * Update the class name of the scrollable element.
E
Erich Gamma 已提交
169
	 */
A
Alex Dima 已提交
170 171 172 173 174 175 176 177 178
	public updateClassName(newClassName: string): void {
		this._options.className = newClassName;
		// Defaults are different on Macs
		if (Platform.isMacintosh) {
			this._options.className += ' mac';
		}
		this._domNode.className = 'monaco-scrollable-element ' + this._options.className;
	}

E
Erich Gamma 已提交
179
	/**
A
Alex Dima 已提交
180 181 182
	 * Update configuration options for the scrollbar.
	 * Really this is Editor.IEditorScrollbarOptions, but base shouldn't
	 * depend on Editor.
E
Erich Gamma 已提交
183
	 */
184
	public updateOptions(newOptions: ScrollableElementChangeOptions): void {
A
Alex Dima 已提交
185 186 187 188
		let massagedOptions = resolveOptions(newOptions);
		this._options.handleMouseWheel = massagedOptions.handleMouseWheel;
		this._options.mouseWheelScrollSensitivity = massagedOptions.mouseWheelScrollSensitivity;
		this._setListeningToMouseWheel(this._options.handleMouseWheel);
189 190 191 192 193 194 195

		this._shouldRender = this._horizontalScrollbar.setCanUseTranslate3d(massagedOptions.canUseTranslate3d) || this._shouldRender;
		this._shouldRender = this._verticalScrollbar.setCanUseTranslate3d(massagedOptions.canUseTranslate3d) || this._shouldRender;

		if (!this._options.lazyRender) {
			this._render();
		}
A
Alex Dima 已提交
196
	}
E
Erich Gamma 已提交
197

A
Alex Dima 已提交
198
	// -------------------- mouse wheel scrolling --------------------
A
Alex Dima 已提交
199

A
Alex Dima 已提交
200 201
	private _setListeningToMouseWheel(shouldListen: boolean): void {
		let isListening = (this._mouseWheelToDispose.length > 0);
E
Erich Gamma 已提交
202

A
Alex Dima 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		if (isListening === shouldListen) {
			// No change
			return;
		}

		// Stop listening (if necessary)
		this._mouseWheelToDispose = dispose(this._mouseWheelToDispose);

		// Start listening (if necessary)
		if (shouldListen) {
			let onMouseWheel = (browserEvent: MouseWheelEvent) => {
				let e = new StandardMouseWheelEvent(browserEvent);
				this._onMouseWheel(e);
			};

			this._mouseWheelToDispose.push(DomUtils.addDisposableListener(this._listenOnDomNode, 'mousewheel', onMouseWheel));
			this._mouseWheelToDispose.push(DomUtils.addDisposableListener(this._listenOnDomNode, 'DOMMouseScroll', onMouseWheel));
		}
	}

	private _onMouseWheel(e: StandardMouseWheelEvent): void {
		if (Platform.isMacintosh && e.browserEvent && this._options.saveLastScrollTimeOnClassName) {
			// Mark dom node with timestamp of wheel event
			let target = <HTMLElement>e.browserEvent.target;
			if (target && target.nodeType === 1) {
				let r = DomUtils.findParentWithClass(target, this._options.saveLastScrollTimeOnClassName);
				if (r) {
					r.setAttribute('last-scroll-time', String(new Date().getTime()));
				}
			}
		}

		let desiredScrollTop = -1;
		let desiredScrollLeft = -1;
E
Erich Gamma 已提交
237

A
Alex Dima 已提交
238 239 240
		if (e.deltaY || e.deltaX) {
			let deltaY = e.deltaY * this._options.mouseWheelScrollSensitivity;
			let deltaX = e.deltaX * this._options.mouseWheelScrollSensitivity;
E
Erich Gamma 已提交
241

A
Alex Dima 已提交
242
			if (this._options.flipAxes) {
G
Grant Mathews 已提交
243
				[deltaY, deltaX] = [deltaX, deltaY];
A
Alex Dima 已提交
244
			}
A
Alex Dima 已提交
245

246
			if (this._options.scrollYToX && !deltaX) {
247
				deltaX = deltaY;
248 249 250
				deltaY = 0;
			}

A
Alex Dima 已提交
251 252 253 254 255 256 257 258 259
			if (Platform.isMacintosh) {
				// Give preference to vertical scrolling
				if (deltaY && Math.abs(deltaX) < 0.2) {
					deltaX = 0;
				}
				if (Math.abs(deltaY) > Math.abs(deltaX) * 0.5) {
					deltaX = 0;
				}
			}
E
Erich Gamma 已提交
260

A
Alex Dima 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274
			if (deltaY) {
				let currentScrollTop = this._scrollable.getScrollTop();
				desiredScrollTop = this._verticalScrollbar.validateScrollPosition((desiredScrollTop !== -1 ? desiredScrollTop : currentScrollTop) - SCROLL_WHEEL_SENSITIVITY * deltaY);
				if (desiredScrollTop === currentScrollTop) {
					desiredScrollTop = -1;
				}
			}
			if (deltaX) {
				let currentScrollLeft = this._scrollable.getScrollLeft();
				desiredScrollLeft = this._horizontalScrollbar.validateScrollPosition((desiredScrollLeft !== -1 ? desiredScrollLeft : currentScrollLeft) - SCROLL_WHEEL_SENSITIVITY * deltaX);
				if (desiredScrollLeft === currentScrollLeft) {
					desiredScrollLeft = -1;
				}
			}
A
Alex Dima 已提交
275

A
Alex Dima 已提交
276 277 278 279 280 281 282 283 284 285 286 287
			if (desiredScrollTop !== -1 || desiredScrollLeft !== -1) {
				if (desiredScrollTop !== -1) {
					this._shouldRender = this._verticalScrollbar.setDesiredScrollPosition(desiredScrollTop) || this._shouldRender;
					desiredScrollTop = -1;
				}
				if (desiredScrollLeft !== -1) {
					this._shouldRender = this._horizontalScrollbar.setDesiredScrollPosition(desiredScrollLeft) || this._shouldRender;
					desiredScrollLeft = -1;
				}
			}
		}

288 289 290 291
		if (this._options.alwaysConsumeMouseWheel || this._shouldRender) {
			e.preventDefault();
			e.stopPropagation();
		}
A
Alex Dima 已提交
292 293
	}

294 295 296
	private _onDidScroll(e:ScrollEvent): void {
		this._shouldRender = this._horizontalScrollbar.onDidScroll(e) || this._shouldRender;
		this._shouldRender = this._verticalScrollbar.onDidScroll(e) || this._shouldRender;
A
Alex Dima 已提交
297 298 299 300 301 302 303 304 305 306 307

		if (this._options.useShadows) {
			this._shouldRender = true;
		}

		this._reveal();

		if (!this._options.lazyRender) {
			this._render();
		}
	}
E
Erich Gamma 已提交
308 309

	/**
A
Alex Dima 已提交
310 311
	 * Render / mutate the DOM now.
	 * Should be used together with the ctor option `lazyRender`.
E
Erich Gamma 已提交
312
	 */
A
Alex Dima 已提交
313 314 315 316
	public renderNow(): void {
		if (!this._options.lazyRender) {
			throw new Error('Please use `lazyRender` together with `renderNow`!');
		}
E
Erich Gamma 已提交
317

A
Alex Dima 已提交
318 319
		this._render();
	}
A
Alex Dima 已提交
320

A
Alex Dima 已提交
321 322 323 324
	private _render(): void {
		if (!this._shouldRender) {
			return;
		}
325

A
Alex Dima 已提交
326
		this._shouldRender = false;
E
Erich Gamma 已提交
327

A
Alex Dima 已提交
328 329
		this._horizontalScrollbar.render();
		this._verticalScrollbar.render();
E
Erich Gamma 已提交
330

A
Alex Dima 已提交
331 332 333
		if (this._options.useShadows) {
			let enableTop = this._scrollable.getScrollTop() > 0;
			let enableLeft = this._scrollable.getScrollLeft() > 0;
E
Erich Gamma 已提交
334

A
Alex Dima 已提交
335 336 337 338 339
			this._leftShadowDomNode.setClassName('shadow' + (enableLeft ? ' left' : ''));
			this._topShadowDomNode.setClassName('shadow' + (enableTop ? ' top' : ''));
			this._topLeftShadowDomNode.setClassName('shadow top-left-corner' + (enableTop ? ' top' : '') + (enableLeft ? ' left' : ''));
		}
	}
E
Erich Gamma 已提交
340

A
Alex Dima 已提交
341
	// -------------------- fade in / fade out --------------------
A
Alex Dima 已提交
342

A
Alex Dima 已提交
343 344 345 346
	private _onDragStart(): void {
		this._isDragging = true;
		this._reveal();
	}
A
Alex Dima 已提交
347

A
Alex Dima 已提交
348 349 350 351
	private _onDragEnd(): void {
		this._isDragging = false;
		this._hide();
	}
A
Alex Dima 已提交
352

A
Alex Dima 已提交
353 354 355 356
	private _onMouseOut(e: IMouseEvent): void {
		this._mouseIsOver = false;
		this._hide();
	}
A
Alex Dima 已提交
357

A
Alex Dima 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	private _onMouseOver(e: IMouseEvent): void {
		this._mouseIsOver = true;
		this._reveal();
	}

	private _reveal(): void {
		this._verticalScrollbar.beginReveal();
		this._horizontalScrollbar.beginReveal();
		this._scheduleHide();
	}

	private _hide(): void {
		if (!this._mouseIsOver && !this._isDragging) {
			this._verticalScrollbar.beginHide();
			this._horizontalScrollbar.beginHide();
		}
	}

	private _scheduleHide(): void {
		this._hideTimeout.cancelAndSet(() => this._hide(), HIDE_TIMEOUT);
A
Alex Dima 已提交
378 379 380
	}
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
export class DomScrollableElement extends ScrollableElement {

	private _element: HTMLElement;

	constructor(element: HTMLElement, options: ScrollableElementCreationOptions) {
		super(element, options);
		this._element = element;
		this.onScroll((e) => {
			if (e.scrollTopChanged) {
				this._element.scrollTop = e.scrollTop;
			}
			if (e.scrollLeftChanged) {
				this._element.scrollLeft = e.scrollLeft;
			}
		});
		this.scanDomNode();
	}

	public scanDomNode(): void {
		// widh, scrollLeft, scrollWidth, height, scrollTop, scrollHeight
		this.updateState({
			width: this._element.clientWidth,
			scrollWidth: this._element.scrollWidth,
			scrollLeft: this._element.scrollLeft,

			height: this._element.clientHeight,
			scrollHeight: this._element.scrollHeight,
			scrollTop: this._element.scrollTop,
		});
	}
}

A
Alex Dima 已提交
413 414
function resolveOptions(opts: ScrollableElementCreationOptions): ScrollableElementResolvedOptions {
	let result: ScrollableElementResolvedOptions = {
415
		canUseTranslate3d: opts.canUseTranslate3d && Browser.canUseTranslate3d,
A
Alex Dima 已提交
416 417 418 419 420
		lazyRender: (typeof opts.lazyRender !== 'undefined' ? opts.lazyRender : false),
		className: (typeof opts.className !== 'undefined' ? opts.className : ''),
		useShadows: (typeof opts.useShadows !== 'undefined' ? opts.useShadows : true),
		handleMouseWheel: (typeof opts.handleMouseWheel !== 'undefined' ? opts.handleMouseWheel : true),
		flipAxes: (typeof opts.flipAxes !== 'undefined' ? opts.flipAxes : false),
421
		alwaysConsumeMouseWheel: (typeof opts.alwaysConsumeMouseWheel !== 'undefined' ? opts.alwaysConsumeMouseWheel : false),
422
		scrollYToX: (typeof opts.scrollYToX !== 'undefined' ? opts.scrollYToX : false),
A
Alex Dima 已提交
423 424 425 426 427
		mouseWheelScrollSensitivity: (typeof opts.mouseWheelScrollSensitivity !== 'undefined' ? opts.mouseWheelScrollSensitivity : 1),
		arrowSize: (typeof opts.arrowSize !== 'undefined' ? opts.arrowSize : 11),

		listenOnDomNode: (typeof opts.listenOnDomNode !== 'undefined' ? opts.listenOnDomNode : null),

A
Alex Dima 已提交
428
		horizontal: (typeof opts.horizontal !== 'undefined' ? opts.horizontal : ScrollbarVisibility.Auto),
A
Alex Dima 已提交
429 430 431 432
		horizontalScrollbarSize: (typeof opts.horizontalScrollbarSize !== 'undefined' ? opts.horizontalScrollbarSize : 10),
		horizontalSliderSize: (typeof opts.horizontalSliderSize !== 'undefined' ? opts.horizontalSliderSize : 0),
		horizontalHasArrows: (typeof opts.horizontalHasArrows !== 'undefined' ? opts.horizontalHasArrows : false),

A
Alex Dima 已提交
433
		vertical: (typeof opts.vertical !== 'undefined' ? opts.vertical : ScrollbarVisibility.Auto),
A
Alex Dima 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		verticalScrollbarSize: (typeof opts.verticalScrollbarSize !== 'undefined' ? opts.verticalScrollbarSize : 10),
		verticalHasArrows: (typeof opts.verticalHasArrows !== 'undefined' ? opts.verticalHasArrows : false),
		verticalSliderSize: (typeof opts.verticalSliderSize !== 'undefined' ? opts.verticalSliderSize : 0),

		saveLastScrollTimeOnClassName: (typeof opts.saveLastScrollTimeOnClassName !== 'undefined' ? opts.saveLastScrollTimeOnClassName : null)
	};

	result.horizontalSliderSize = (typeof opts.horizontalSliderSize !== 'undefined' ? opts.horizontalSliderSize : result.horizontalScrollbarSize);
	result.verticalSliderSize = (typeof opts.verticalSliderSize !== 'undefined' ? opts.verticalSliderSize : result.verticalScrollbarSize);

	// Defaults are different on Macs
	if (Platform.isMacintosh) {
		result.className += ' mac';
	}

	return result;
}