scrollable.ts 10.1 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';

J
Johannes Rieken 已提交
7 8
import { Disposable } from 'vs/base/common/lifecycle';
import Event, { Emitter } from 'vs/base/common/event';
9

10 11 12 13 14 15
export enum ScrollbarVisibility {
	Auto = 1,
	Hidden = 2,
	Visible = 3
}

16 17
export interface ScrollEvent {
	width: number;
18
	scrollWidth: number;
19 20 21
	scrollLeft: number;

	height: number;
22
	scrollHeight: number;
23
	scrollTop: number;
24

25
	widthChanged: boolean;
26
	scrollWidthChanged: boolean;
27
	scrollLeftChanged: boolean;
28

29 30 31 32
	heightChanged: boolean;
	scrollHeightChanged: boolean;
	scrollTopChanged: boolean;
}
33

34
export class ScrollState implements IScrollDimensions, IScrollPosition {
A
Alex Dima 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	_scrollStateBrand: void;

	public readonly width: number;
	public readonly scrollWidth: number;
	public readonly scrollLeft: number;
	public readonly height: number;
	public readonly scrollHeight: number;
	public readonly scrollTop: number;

	constructor(
		width: number,
		scrollWidth: number,
		scrollLeft: number,
		height: number,
		scrollHeight: number,
		scrollTop: number
	) {
		width = width | 0;
		scrollWidth = scrollWidth | 0;
		scrollLeft = scrollLeft | 0;
		height = height | 0;
		scrollHeight = scrollHeight | 0;
		scrollTop = scrollTop | 0;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

		if (width < 0) {
			width = 0;
		}
		if (scrollLeft + width > scrollWidth) {
			scrollLeft = scrollWidth - width;
		}
		if (scrollLeft < 0) {
			scrollLeft = 0;
		}

		if (height < 0) {
			height = 0;
		}
		if (scrollTop + height > scrollHeight) {
			scrollTop = scrollHeight - height;
		}
		if (scrollTop < 0) {
			scrollTop = 0;
		}
78

A
Alex Dima 已提交
79 80 81 82 83 84 85
		this.width = width;
		this.scrollWidth = scrollWidth;
		this.scrollLeft = scrollLeft;
		this.height = height;
		this.scrollHeight = scrollHeight;
		this.scrollTop = scrollTop;
	}
86

A
Alex Dima 已提交
87 88 89 90 91 92 93 94 95 96
	public equals(other: ScrollState): boolean {
		return (
			this.width === other.width
			&& this.scrollWidth === other.scrollWidth
			&& this.scrollLeft === other.scrollLeft
			&& this.height === other.height
			&& this.scrollHeight === other.scrollHeight
			&& this.scrollTop === other.scrollTop
		);
	}
97

98
	public withScrollDimensions(update: INewScrollDimensions): ScrollState {
99 100 101
		return new ScrollState(
			(typeof update.width !== 'undefined' ? update.width : this.width),
			(typeof update.scrollWidth !== 'undefined' ? update.scrollWidth : this.scrollWidth),
102
			this.scrollLeft,
103 104
			(typeof update.height !== 'undefined' ? update.height : this.height),
			(typeof update.scrollHeight !== 'undefined' ? update.scrollHeight : this.scrollHeight),
105 106 107 108 109 110 111 112 113 114 115
			this.scrollTop
		);
	}

	public withScrollPosition(update: INewScrollPosition): ScrollState {
		return new ScrollState(
			this.width,
			this.scrollWidth,
			(typeof update.scrollLeft !== 'undefined' ? update.scrollLeft : this.scrollLeft),
			this.height,
			this.scrollHeight,
116 117 118 119
			(typeof update.scrollTop !== 'undefined' ? update.scrollTop : this.scrollTop)
		);
	}

A
Alex Dima 已提交
120 121 122 123
	public createScrollEvent(previous: ScrollState): ScrollEvent {
		let widthChanged = (this.width !== previous.width);
		let scrollWidthChanged = (this.scrollWidth !== previous.scrollWidth);
		let scrollLeftChanged = (this.scrollLeft !== previous.scrollLeft);
124

A
Alex Dima 已提交
125 126 127
		let heightChanged = (this.height !== previous.height);
		let scrollHeightChanged = (this.scrollHeight !== previous.scrollHeight);
		let scrollTopChanged = (this.scrollTop !== previous.scrollTop);
128

A
Alex Dima 已提交
129 130 131 132
		return {
			width: this.width,
			scrollWidth: this.scrollWidth,
			scrollLeft: this.scrollLeft,
133

A
Alex Dima 已提交
134 135 136
			height: this.height,
			scrollHeight: this.scrollHeight,
			scrollTop: this.scrollTop,
137 138 139 140

			widthChanged: widthChanged,
			scrollWidthChanged: scrollWidthChanged,
			scrollLeftChanged: scrollLeftChanged,
141

142 143 144
			heightChanged: heightChanged,
			scrollHeightChanged: scrollHeightChanged,
			scrollTopChanged: scrollTopChanged,
A
Alex Dima 已提交
145 146 147 148 149
		};
	}

}

150 151 152 153 154 155 156
export interface IScrollDimensions {
	readonly width: number;
	readonly scrollWidth: number;
	readonly height: number;
	readonly scrollHeight: number;
}
export interface INewScrollDimensions {
A
Alex Dima 已提交
157 158 159 160
	width?: number;
	scrollWidth?: number;
	height?: number;
	scrollHeight?: number;
161 162 163 164 165 166 167 168
}

export interface IScrollPosition {
	readonly scrollLeft: number;
	readonly scrollTop: number;
}
export interface INewScrollPosition {
	scrollLeft?: number;
A
Alex Dima 已提交
169 170 171 172 173 174 175
	scrollTop?: number;
}

export class Scrollable extends Disposable {

	_scrollableBrand: void;

176
	private readonly _smoothScrollDuration: number;
A
Alex Dima 已提交
177
	private _state: ScrollState;
178
	private _smoothScrolling: SmoothScrollingOperation;
A
Alex Dima 已提交
179 180 181 182

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

183
	constructor(smoothScrollDuration: number) {
A
Alex Dima 已提交
184 185
		super();

186
		this._smoothScrollDuration = smoothScrollDuration;
A
Alex Dima 已提交
187
		this._state = new ScrollState(0, 0, 0, 0, 0, 0);
188 189 190 191 192 193 194 195 196 197 198 199 200
		this._smoothScrolling = null;
	}

	public dispose(): void {
		if (this._smoothScrolling) {
			this._smoothScrolling.dispose();
			this._smoothScrolling = null;
		}
		super.dispose();
	}

	public validateScrollPosition(scrollPosition: INewScrollPosition): IScrollPosition {
		return this._state.withScrollPosition(scrollPosition);
A
Alex Dima 已提交
201 202
	}

203
	public getScrollDimensions(): IScrollDimensions {
A
Alex Dima 已提交
204 205 206
		return this._state;
	}

207 208 209 210 211 212
	public setScrollDimensions(dimensions: INewScrollDimensions): void {
		const newState = this._state.withScrollDimensions(dimensions);
		this._setState(newState);

		// TODO@smooth: [MUST] validate outstanding animated scroll position request target
		// (in case it becomes invalid)
A
Alex Dima 已提交
213 214
	}

215 216 217 218 219 220 221 222 223
	/**
	 * Returns the final scroll position that the instance will have once the smooth scroll animation concludes.
	 * If no scroll animation is occuring, it will return the current scroll position instead.
	 */
	public getFutureScrollPosition(): IScrollPosition {
		if (this._smoothScrolling) {
			return this._smoothScrolling.to;
		}
		return this._state;
A
Alex Dima 已提交
224 225
	}

226
	/**
227 228
	 * Returns the current scroll position.
	 * Note: This result might be an intermediate scroll position, as there might be an ongoing smooth scroll animation.
229
	 */
230 231
	public getCurrentScrollPosition(): IScrollPosition {
		return this._state;
232 233
	}

234 235 236
	public setScrollPositionNow(update: INewScrollPosition): void {
		// no smooth scrolling requested
		const newState = this._state.withScrollPosition(update);
237

238 239 240 241 242
		// Terminate any outstanding smooth scrolling
		if (this._smoothScrolling) {
			this._smoothScrolling.dispose();
			this._smoothScrolling = null;
		}
A
Alex Dima 已提交
243

244 245
		this._setState(newState);
	}
246

247 248 249 250
	public setScrollPositionSmooth(update: INewScrollPosition): void {
		if (this._smoothScrollDuration === 0) {
			// Smooth scrolling not supported.
			return this.setScrollPositionNow(update);
251
		}
252 253 254 255 256 257 258

		if (this._smoothScrolling) {
			const oldSmoothScrolling = this._smoothScrolling;
			const newSmoothScrolling = oldSmoothScrolling.combine(this._state, update, this._smoothScrollDuration);
			if (oldSmoothScrolling.softEquals(newSmoothScrolling)) {
				// No change
				return;
259
			}
260 261 262 263
			oldSmoothScrolling.dispose();
			this._smoothScrolling = newSmoothScrolling;
		} else {
			this._smoothScrolling = new SmoothScrollingOperation(this._state, update, this._smoothScrollDuration);
264
		}
265 266 267 268 269 270

		// Begin smooth scrolling animation
		this._smoothScrolling.animationFrameToken = requestAnimationFrame(() => {
			this._smoothScrolling.animationFrameToken = -1;
			this._performSmoothScrolling();
		});
271 272
	}

273 274 275
	private _performSmoothScrolling(): void {
		const update = this._smoothScrolling.tick();
		const newState = this._state.withScrollPosition(update);
A
Alex Dima 已提交
276

277
		this._setState(newState);
278

279 280 281 282 283 284
		if (!update.isDone) {
			// Continue smooth scrolling animation
			this._smoothScrolling.animationFrameToken = requestAnimationFrame(() => {
				this._smoothScrolling.animationFrameToken = -1;
				this._performSmoothScrolling();
			});
285 286 287
		}
	}

288
	private _setState(newState: ScrollState): void {
289
		const oldState = this._state;
290 291 292 293
		if (oldState.equals(newState)) {
			// no change
			return;
		}
A
Alex Dima 已提交
294 295
		this._state = newState;
		this._onScroll.fire(this._state.createScrollEvent(oldState));
296
	}
E
Erich Gamma 已提交
297
}
298

299 300 301 302 303 304 305 306 307 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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
class SmoothScrollingUpdate implements IScrollPosition {

	public readonly scrollLeft: number;
	public readonly scrollTop: number;
	public readonly isDone: boolean;

	constructor(scrollLeft: number, scrollTop: number, isDone: boolean) {
		this.scrollLeft = scrollLeft;
		this.scrollTop = scrollTop;
		this.isDone = isDone;
	}

}

class SmoothScrollingOperation {

	public readonly from: IScrollPosition;
	public readonly to: IScrollPosition;
	public readonly duration: number;
	private readonly _startTime: number;
	public animationFrameToken: number;

	constructor(from: ScrollState, to: INewScrollPosition, duration: number) {
		this.from = from;
		this.to = from.withScrollPosition(to);
		this.duration = duration;
		this._startTime = Date.now();
		this.animationFrameToken = -1;
	}

	public softEquals(other: SmoothScrollingOperation): boolean {
		return (
			this.to.scrollLeft === other.to.scrollLeft
			&& this.to.scrollTop === other.to.scrollTop
		);
	}

	public dispose(): void {
		if (this.animationFrameToken !== -1) {
			cancelAnimationFrame(this.animationFrameToken);
			this.animationFrameToken = -1;
		}
	}

	public tick(): SmoothScrollingUpdate {
		const completion = (Date.now() - this._startTime) / this.duration;

		if (completion < 1) {
			const newScrollLeft = this.from.scrollLeft + (this.to.scrollLeft - this.from.scrollLeft) * completion;
			const newScrollTop = this.from.scrollTop + (this.to.scrollTop - this.from.scrollTop) * completion;
			return new SmoothScrollingUpdate(newScrollLeft, newScrollTop, false);
		}

		return new SmoothScrollingUpdate(this.to.scrollLeft, this.to.scrollTop, true);
	}

	public combine(from: ScrollState, to: INewScrollPosition, duration: number): SmoothScrollingOperation {
		// Combine our scrollLeft/scrollTop with incoming scrollLeft/scrollTop
		to = {
			scrollLeft: (typeof to.scrollLeft === 'undefined' ? this.to.scrollLeft : to.scrollLeft),
			scrollTop: (typeof to.scrollTop === 'undefined' ? this.to.scrollTop : to.scrollTop)
		};

		// TODO@smooth: This is our opportunity to combine animations
		return new SmoothScrollingOperation(from, to, duration);
	}
}