extensionsWidgets.ts 11.3 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import 'vs/css!./media/extensionsWidgets';
7
import { Disposable, toDisposable, DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
8
import { IExtension, IExtensionsWorkbenchService, IExtensionContainer } from 'vs/workbench/contrib/extensions/common/extensions';
9
import { append, $, addClass, removeNode } from 'vs/base/browser/dom';
10 11
import * as platform from 'vs/base/common/platform';
import { localize } from 'vs/nls';
12
import { IExtensionTipsService, IExtensionManagementServerService } from 'vs/workbench/services/extensionManagement/common/extensionManagement';
13
import { ILabelService } from 'vs/platform/label/common/label';
S
Sandeep Somavarapu 已提交
14
import { extensionButtonProminentBackground, extensionButtonProminentForeground, ExtensionToolTipAction } from 'vs/workbench/contrib/extensions/browser/extensionsActions';
M
Martin Aeschlimann 已提交
15
import { IThemeService, IColorTheme } from 'vs/platform/theme/common/themeService';
S
Sandeep Somavarapu 已提交
16
import { EXTENSION_BADGE_REMOTE_BACKGROUND, EXTENSION_BADGE_REMOTE_FOREGROUND } from 'vs/workbench/common/theme';
17
import { Emitter, Event } from 'vs/base/common/event';
18
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
19
import { CountBadge } from 'vs/base/browser/ui/countBadge/countBadge';
20 21

export abstract class ExtensionWidget extends Disposable implements IExtensionContainer {
S
Sandeep Somavarapu 已提交
22 23 24
	private _extension: IExtension | null = null;
	get extension(): IExtension | null { return this._extension; }
	set extension(extension: IExtension | null) { this._extension = extension; this.update(); }
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	update(): void { this.render(); }
	abstract render(): void;
}

export class Label extends ExtensionWidget {

	constructor(
		private element: HTMLElement,
		private fn: (extension: IExtension) => string,
		@IExtensionsWorkbenchService extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
		super();
		this.render();
	}

	render(): void {
		this.element.textContent = this.extension ? this.fn(this.extension) : '';
	}
}

export class InstallCountWidget extends ExtensionWidget {

	constructor(
		private container: HTMLElement,
		private small: boolean,
		@IExtensionsWorkbenchService extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
		super();
		addClass(container, 'extension-install-count');
		this.render();
	}

	render(): void {
		this.container.innerHTML = '';

		if (!this.extension) {
			return;
		}

		const installCount = this.extension.installCount;

		if (installCount === undefined) {
			return;
		}

		let installLabel: string;

		if (this.small) {
			if (installCount > 1000000) {
				installLabel = `${Math.floor(installCount / 100000) / 10}M`;
			} else if (installCount > 1000) {
				installLabel = `${Math.floor(installCount / 1000)}K`;
			} else {
				installLabel = String(installCount);
			}
		}
		else {
			installLabel = installCount.toLocaleString(platform.locale);
		}

85
		append(this.container, $('span.codicon.codicon-cloud-download'));
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 121 122 123 124
		const count = append(this.container, $('span.count'));
		count.textContent = installLabel;
	}
}

export class RatingsWidget extends ExtensionWidget {

	constructor(
		private container: HTMLElement,
		private small: boolean
	) {
		super();
		addClass(container, 'extension-ratings');

		if (this.small) {
			addClass(container, 'small');
		}

		this.render();
	}

	render(): void {
		this.container.innerHTML = '';

		if (!this.extension) {
			return;
		}

		if (this.extension.rating === undefined) {
			return;
		}

		if (this.small && !this.extension.ratingCount) {
			return;
		}

		const rating = Math.round(this.extension.rating * 2) / 2;

		if (this.small) {
125
			append(this.container, $('span.codicon.codicon-star-full'));
126 127 128 129 130 131

			const count = append(this.container, $('span.count'));
			count.textContent = String(rating);
		} else {
			for (let i = 1; i <= 5; i++) {
				if (rating >= i) {
132
					append(this.container, $('span.codicon.codicon-star-full'));
133
				} else if (rating >= i - 0.5) {
134
					append(this.container, $('span.codicon.codicon-star-half'));
135
				} else {
136
					append(this.container, $('span.codicon.codicon-star-empty'));
137 138 139
				}
			}
		}
S
Sandeep Somavarapu 已提交
140
		this.container.title = this.extension.ratingCount === 1 ? localize('ratedBySingleUser', "Rated by 1 user")
141
			: typeof this.extension.ratingCount === 'number' && this.extension.ratingCount > 1 ? localize('ratedByUsers', "Rated by {0} users", this.extension.ratingCount) : localize('noRating', "No rating");
142 143 144
	}
}

145 146 147 148
export class TooltipWidget extends ExtensionWidget {

	constructor(
		private readonly parent: HTMLElement,
S
Sandeep Somavarapu 已提交
149
		private readonly tooltipAction: ExtensionToolTipAction,
150
		private readonly recommendationWidget: RecommendationWidget,
151
		@ILabelService private readonly labelService: ILabelService
152 153
	) {
		super();
154
		this._register(Event.any<any>(
S
Sandeep Somavarapu 已提交
155
			this.tooltipAction.onDidChange,
156 157
			this.recommendationWidget.onDidChangeTooltip,
			this.labelService.onDidChangeFormatters
158
		)(() => this.render()));
159 160 161 162 163
	}

	render(): void {
		this.parent.title = '';
		this.parent.removeAttribute('aria-label');
164
		this.parent.title = this.getTooltip();
165
		if (this.extension) {
166
			this.parent.setAttribute('aria-label', localize('extension-arialabel', "{0}. Press enter for extension details.", this.extension.displayName));
167 168 169
		}
	}

170 171 172 173
	private getTooltip(): string {
		if (!this.extension) {
			return '';
		}
S
Sandeep Somavarapu 已提交
174 175
		if (this.tooltipAction.label) {
			return this.tooltipAction.label;
176 177 178 179 180 181
		}
		return this.recommendationWidget.tooltip;
	}

}

182 183
export class RecommendationWidget extends ExtensionWidget {

M
Matt Bierner 已提交
184
	private element?: HTMLElement;
185
	private readonly disposables = this._register(new DisposableStore());
186

S
Sandeep Somavarapu 已提交
187
	private _tooltip: string = '';
188 189 190 191 192 193 194 195 196 197
	get tooltip(): string { return this._tooltip; }
	set tooltip(tooltip: string) {
		if (this._tooltip !== tooltip) {
			this._tooltip = tooltip;
			this._onDidChangeTooltip.fire();
		}
	}
	private _onDidChangeTooltip: Emitter<void> = this._register(new Emitter<void>());
	readonly onDidChangeTooltip: Event<void> = this._onDidChangeTooltip.event;

198 199 200 201 202 203 204 205
	constructor(
		private parent: HTMLElement,
		@IThemeService private readonly themeService: IThemeService,
		@IExtensionTipsService private readonly extensionTipsService: IExtensionTipsService
	) {
		super();
		this.render();
		this._register(toDisposable(() => this.clear()));
S
Sandeep Somavarapu 已提交
206
		this._register(this.extensionTipsService.onRecommendationChange(() => this.render()));
207 208 209
	}

	private clear(): void {
210
		this.tooltip = '';
211 212 213 214
		this.parent.setAttribute('aria-label', this.extension ? localize('viewExtensionDetailsAria', "{0}. Press enter for extension details.", this.extension.displayName) : '');
		if (this.element) {
			this.parent.removeChild(this.element);
		}
M
Matt Bierner 已提交
215
		this.element = undefined;
216
		this.disposables.clear();
217 218 219 220 221 222 223
	}

	render(): void {
		this.clear();
		if (!this.extension) {
			return;
		}
S
Sandeep Somavarapu 已提交
224 225
		const extRecommendations = this.extensionTipsService.getAllRecommendationsWithReason();
		if (extRecommendations[this.extension.identifier.id.toLowerCase()]) {
226
			this.element = append(this.parent, $('div.extension-bookmark'));
S
Sandeep Somavarapu 已提交
227
			const recommendation = append(this.element, $('.recommendation'));
228
			append(recommendation, $('span.codicon.codicon-star'));
M
Martin Aeschlimann 已提交
229
			const applyBookmarkStyle = (theme: IColorTheme) => {
S
Sandeep Somavarapu 已提交
230 231 232 233 234
				const bgColor = theme.getColor(extensionButtonProminentBackground);
				const fgColor = theme.getColor(extensionButtonProminentForeground);
				recommendation.style.borderTopColor = bgColor ? bgColor.toString() : 'transparent';
				recommendation.style.color = fgColor ? fgColor.toString() : 'white';
			};
M
Martin Aeschlimann 已提交
235 236
			applyBookmarkStyle(this.themeService.getColorTheme());
			this.themeService.onDidColorThemeChange(applyBookmarkStyle, this, this.disposables);
237
			this.tooltip = extRecommendations[this.extension.identifier.id.toLowerCase()].reasonText;
S
Sandeep Somavarapu 已提交
238
		}
239 240 241 242 243 244
	}

}

export class RemoteBadgeWidget extends ExtensionWidget {

245
	private readonly remoteBadge = this._register(new MutableDisposable<RemoteBadge>());
246

247 248
	private element: HTMLElement;

249
	constructor(
250
		parent: HTMLElement,
251
		private readonly tooltip: boolean,
252
		@IExtensionManagementServerService private readonly extensionManagementServerService: IExtensionManagementServerService,
253
		@IInstantiationService private readonly instantiationService: IInstantiationService
254 255
	) {
		super();
256
		this.element = append(parent, $('.extension-remote-badge-container'));
257 258 259 260 261
		this.render();
		this._register(toDisposable(() => this.clear()));
	}

	private clear(): void {
262 263
		if (this.remoteBadge.value) {
			this.element.removeChild(this.remoteBadge.value.element);
264
		}
265
		this.remoteBadge.clear();
266 267 268 269
	}

	render(): void {
		this.clear();
270
		if (!this.extension || !this.extension.local || !this.extension.server || !(this.extensionManagementServerService.localExtensionManagementServer && this.extensionManagementServerService.remoteExtensionManagementServer) || this.extension.server !== this.extensionManagementServerService.remoteExtensionManagementServer) {
271 272
			return;
		}
273 274
		this.remoteBadge.value = this.instantiationService.createInstance(RemoteBadge, this.tooltip);
		append(this.element, this.remoteBadge.value.element);
275 276
	}
}
277 278 279 280 281 282

class RemoteBadge extends Disposable {

	readonly element: HTMLElement;

	constructor(
283
		private readonly tooltip: boolean,
284 285
		@ILabelService private readonly labelService: ILabelService,
		@IThemeService private readonly themeService: IThemeService,
286
		@IExtensionManagementServerService private readonly extensionManagementServerService: IExtensionManagementServerService
287 288
	) {
		super();
289
		this.element = $('div.extension-badge.extension-remote-badge');
290 291 292 293
		this.render();
	}

	private render(): void {
294
		append(this.element, $('span.codicon.codicon-remote'));
295 296 297 298 299

		const applyBadgeStyle = () => {
			if (!this.element) {
				return;
			}
M
Martin Aeschlimann 已提交
300 301
			const bgColor = this.themeService.getColorTheme().getColor(EXTENSION_BADGE_REMOTE_BACKGROUND);
			const fgColor = this.themeService.getColorTheme().getColor(EXTENSION_BADGE_REMOTE_FOREGROUND);
302 303 304 305
			this.element.style.backgroundColor = bgColor ? bgColor.toString() : '';
			this.element.style.color = fgColor ? fgColor.toString() : '';
		};
		applyBadgeStyle();
M
Martin Aeschlimann 已提交
306
		this._register(this.themeService.onDidColorThemeChange(() => applyBadgeStyle()));
307

308 309
		if (this.tooltip) {
			const updateTitle = () => {
310 311
				if (this.element && this.extensionManagementServerService.remoteExtensionManagementServer) {
					this.element.title = localize('remote extension title', "Extension in {0}", this.extensionManagementServerService.remoteExtensionManagementServer.label);
312 313 314 315 316
				}
			};
			this._register(this.labelService.onDidChangeFormatters(() => updateTitle()));
			updateTitle();
		}
317
	}
S
Sandeep Somavarapu 已提交
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

export class ExtensionPackCountWidget extends ExtensionWidget {

	private element: HTMLElement | undefined;

	constructor(
		private readonly parent: HTMLElement,
	) {
		super();
		this.render();
		this._register(toDisposable(() => this.clear()));
	}

	private clear(): void {
		if (this.element) {
			removeNode(this.element);
		}
	}

	render(): void {
		this.clear();
		if (!this.extension || !this.extension.extensionPack.length) {
			return;
		}
		this.element = append(this.parent, $('.extension-badge.extension-pack-badge'));
		const countBadge = new CountBadge(this.element);
		countBadge.setCount(this.extension.extensionPack.length);
	}
}