scrollableElement.ts 15.0 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 9 10 11 12 13 14 15 16
import 'vs/css!./media/scrollbars';

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';
import {ScrollableElementCreationOptions, ScrollableElementResolvedOptions} from 'vs/base/browser/ui/scrollbar/scrollableElementOptions';
import {visibilityFromString} from 'vs/base/browser/ui/scrollbar/scrollbarVisibilityController';
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
17
import {Scrollable, ScrollEvent, INewScrollState} 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
	 */
A
Alex Dima 已提交
184 185 186 187 188 189 190
	public updateOptions(newOptions: ScrollableElementCreationOptions): void {
		// only support handleMouseWheel changes for now
		let massagedOptions = resolveOptions(newOptions);
		this._options.handleMouseWheel = massagedOptions.handleMouseWheel;
		this._options.mouseWheelScrollSensitivity = massagedOptions.mouseWheelScrollSensitivity;
		this._setListeningToMouseWheel(this._options.handleMouseWheel);
	}
E
Erich Gamma 已提交
191

A
Alex Dima 已提交
192
	// -------------------- mouse wheel scrolling --------------------
A
Alex Dima 已提交
193

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

A
Alex Dima 已提交
197 198 199 200 201 202 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
		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 已提交
231

A
Alex Dima 已提交
232 233 234
		if (e.deltaY || e.deltaX) {
			let deltaY = e.deltaY * this._options.mouseWheelScrollSensitivity;
			let deltaX = e.deltaX * this._options.mouseWheelScrollSensitivity;
E
Erich Gamma 已提交
235

A
Alex Dima 已提交
236 237 238 239
			if (this._options.flipAxes) {
				deltaY = e.deltaX;
				deltaX = e.deltaY;
			}
A
Alex Dima 已提交
240

A
Alex Dima 已提交
241 242 243 244 245 246 247 248 249
			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 已提交
250

A
Alex Dima 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264
			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 已提交
265

A
Alex Dima 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
			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;
				}
			}
		}

		e.preventDefault();
		e.stopPropagation();
	}

282 283 284
	private _onDidScroll(e:ScrollEvent): void {
		this._shouldRender = this._horizontalScrollbar.onDidScroll(e) || this._shouldRender;
		this._shouldRender = this._verticalScrollbar.onDidScroll(e) || this._shouldRender;
A
Alex Dima 已提交
285 286 287 288 289 290 291 292 293 294 295

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

		this._reveal();

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

	/**
A
Alex Dima 已提交
298 299
	 * Render / mutate the DOM now.
	 * Should be used together with the ctor option `lazyRender`.
E
Erich Gamma 已提交
300
	 */
A
Alex Dima 已提交
301 302 303 304
	public renderNow(): void {
		if (!this._options.lazyRender) {
			throw new Error('Please use `lazyRender` together with `renderNow`!');
		}
E
Erich Gamma 已提交
305

A
Alex Dima 已提交
306 307
		this._render();
	}
A
Alex Dima 已提交
308

A
Alex Dima 已提交
309 310 311 312
	private _render(): void {
		if (!this._shouldRender) {
			return;
		}
313

A
Alex Dima 已提交
314
		this._shouldRender = false;
E
Erich Gamma 已提交
315

A
Alex Dima 已提交
316 317
		this._horizontalScrollbar.render();
		this._verticalScrollbar.render();
E
Erich Gamma 已提交
318

A
Alex Dima 已提交
319 320 321
		if (this._options.useShadows) {
			let enableTop = this._scrollable.getScrollTop() > 0;
			let enableLeft = this._scrollable.getScrollLeft() > 0;
E
Erich Gamma 已提交
322

A
Alex Dima 已提交
323 324 325 326 327
			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 已提交
328

A
Alex Dima 已提交
329
	// -------------------- fade in / fade out --------------------
A
Alex Dima 已提交
330

A
Alex Dima 已提交
331 332 333 334
	private _onDragStart(): void {
		this._isDragging = true;
		this._reveal();
	}
A
Alex Dima 已提交
335

A
Alex Dima 已提交
336 337 338 339
	private _onDragEnd(): void {
		this._isDragging = false;
		this._hide();
	}
A
Alex Dima 已提交
340

A
Alex Dima 已提交
341 342 343 344
	private _onMouseOut(e: IMouseEvent): void {
		this._mouseIsOver = false;
		this._hide();
	}
A
Alex Dima 已提交
345

A
Alex Dima 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	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 已提交
366 367 368
	}
}

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
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 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
function resolveOptions(opts: ScrollableElementCreationOptions): ScrollableElementResolvedOptions {
	let result: ScrollableElementResolvedOptions = {
		forbidTranslate3dUse: (typeof opts.forbidTranslate3dUse !== 'undefined' ? opts.forbidTranslate3dUse : false),
		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),
		mouseWheelScrollSensitivity: (typeof opts.mouseWheelScrollSensitivity !== 'undefined' ? opts.mouseWheelScrollSensitivity : 1),
		arrowSize: (typeof opts.arrowSize !== 'undefined' ? opts.arrowSize : 11),

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

		horizontal: visibilityFromString(typeof opts.horizontal !== 'undefined' ? opts.horizontal : 'auto'),
		horizontalScrollbarSize: (typeof opts.horizontalScrollbarSize !== 'undefined' ? opts.horizontalScrollbarSize : 10),
		horizontalSliderSize: (typeof opts.horizontalSliderSize !== 'undefined' ? opts.horizontalSliderSize : 0),
		horizontalHasArrows: (typeof opts.horizontalHasArrows !== 'undefined' ? opts.horizontalHasArrows : false),

		vertical: visibilityFromString(typeof opts.vertical !== 'undefined' ? opts.vertical : 'auto'),
		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;
}