explorerViewItems.ts 5.1 KB
Newer Older
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.
 *--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
A
Alex Ross 已提交
7
import { IAction } from 'vs/base/common/actions';
8
import { IThemeService } from 'vs/platform/theme/common/themeService';
9
import { attachSelectBoxStyler } from 'vs/platform/theme/common/styler';
10
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
11
import { IRemoteExplorerService, REMOTE_EXPLORER_TYPE_KEY } from 'vs/workbench/services/remote/common/remoteExplorerService';
12 13 14 15
import { ISelectOptionItem } from 'vs/base/browser/ui/selectBox/selectBox';
import { IViewDescriptor } from 'vs/workbench/common/views';
import { isStringArray } from 'vs/base/common/types';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
16
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
A
Alex Ross 已提交
17
import { ContextKeyEqualsExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
18
import { SelectActionViewItem } from 'vs/base/browser/ui/actionbar/actionViewItems';
A
Alex Ross 已提交
19 20 21
import { Action2, MenuId } from 'vs/platform/actions/common/actions';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { VIEWLET_ID } from 'vs/workbench/contrib/remote/browser/remoteExplorer';
22 23 24 25 26 27 28 29 30 31

export interface IRemoteSelectItem extends ISelectOptionItem {
	authority: string[];
}

export class SwitchRemoteViewItem extends SelectActionViewItem {

	constructor(
		action: IAction,
		private readonly optionsItems: IRemoteSelectItem[],
32
		@IThemeService themeService: IThemeService,
33
		@IContextViewService contextViewService: IContextViewService,
A
Alex Ross 已提交
34 35
		@IRemoteExplorerService private remoteExplorerService: IRemoteExplorerService,
		@IWorkbenchEnvironmentService private environmentService: IWorkbenchEnvironmentService,
36
		@IStorageService private readonly storageService: IStorageService
37 38
	) {
		super(null, action, optionsItems, 0, contextViewService, { ariaLabel: nls.localize('remotes', 'Switch Remote') });
39
		this._register(attachSelectBoxStyler(this.selectBox, themeService));
40 41
	}

A
Alex Ross 已提交
42 43
	public setSelectionForConnection(): boolean {
		let isSetForConnection = false;
44 45
		if (this.optionsItems.length > 0) {
			let index = 0;
A
Alex Ross 已提交
46 47
			const remoteAuthority = this.environmentService.remoteAuthority;
			isSetForConnection = true;
48 49 50
			const explorerType: string[] | undefined = remoteAuthority ? [remoteAuthority.split('+')[0]] :
				this.storageService.get(REMOTE_EXPLORER_TYPE_KEY, StorageScope.WORKSPACE)?.split(',') ?? this.storageService.get(REMOTE_EXPLORER_TYPE_KEY, StorageScope.GLOBAL)?.split(',');
			if (explorerType !== undefined) {
A
Alex Ross 已提交
51
				index = this.getOptionIndexForExplorerType(explorerType);
52 53
			}
			this.select(index);
A
Alex Ross 已提交
54
			this.remoteExplorerService.targetType = this.optionsItems[index].authority;
55
		}
A
Alex Ross 已提交
56 57 58 59 60 61
		return isSetForConnection;
	}

	public setSelection() {
		const index = this.getOptionIndexForExplorerType(this.remoteExplorerService.targetType);
		this.select(index);
62 63
	}

A
Alex Ross 已提交
64
	private getOptionIndexForExplorerType(explorerType: string[]): number {
65 66
		let index = 0;
		for (let optionIterator = 0; (optionIterator < this.optionsItems.length) && (index === 0); optionIterator++) {
A
Alex Ross 已提交
67
			for (let authorityIterator = 0; authorityIterator < this.optionsItems[optionIterator].authority.length; authorityIterator++) {
68
				for (let i = 0; i < explorerType.length; i++) {
A
Alex Ross 已提交
69
					if (this.optionsItems[optionIterator].authority[authorityIterator] === explorerType[i]) {
70 71 72
						index = optionIterator;
						break;
					}
73 74 75 76 77 78
				}
			}
		}
		return index;
	}

79
	render(container: HTMLElement) {
80 81
		if (this.optionsItems.length > 1) {
			super.render(container);
82
			container.classList.add('switch-remote');
83
		}
84 85 86 87 88 89
	}

	protected getActionContext(_: string, index: number): any {
		return this.optionsItems[index];
	}

90
	static createOptionItems(views: IViewDescriptor[], contextKeyService: IContextKeyService): IRemoteSelectItem[] {
91 92
		let options: IRemoteSelectItem[] = [];
		views.forEach(view => {
93
			if (view.group && view.group.startsWith('targets') && view.remoteAuthority && (!view.when || contextKeyService.contextMatchesRules(view.when))) {
94 95 96 97 98 99 100
				options.push({ text: view.name, authority: isStringArray(view.remoteAuthority) ? view.remoteAuthority : [view.remoteAuthority] });
			}
		});
		return options;
	}
}

A
Alex Ross 已提交
101
export class SwitchRemoteAction extends Action2 {
102 103 104 105

	public static readonly ID = 'remote.explorer.switch';
	public static readonly LABEL = nls.localize('remote.explorer.switch', "Switch Remote");

A
Alex Ross 已提交
106 107 108 109 110 111 112 113 114 115 116
	constructor() {
		super({
			id: SwitchRemoteAction.ID,
			title: SwitchRemoteAction.LABEL,
			menu: [{
				id: MenuId.ViewContainerTitle,
				when: ContextKeyEqualsExpr.create('viewContainer', VIEWLET_ID),
				group: 'navigation',
				order: 1
			}],
		});
117 118
	}

A
Alex Ross 已提交
119 120
	public async run(accessor: ServicesAccessor, args: IRemoteSelectItem): Promise<any> {
		accessor.get(IRemoteExplorerService).targetType = args.authority;
121 122
	}
}