explorerViewItems.ts 4.8 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';
7
import { IAction, Action } 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 16
import { ISelectOptionItem } from 'vs/base/browser/ui/selectBox/selectBox';
import { IViewDescriptor } from 'vs/workbench/common/views';
import { startsWith } from 'vs/base/common/strings';
import { isStringArray } from 'vs/base/common/types';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
17
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
18
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
19
import { SelectActionViewItem } from 'vs/base/browser/ui/actionbar/actionViewItems';
20 21 22 23 24 25 26 27 28 29

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

export class SwitchRemoteViewItem extends SelectActionViewItem {

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

		this.setSelectionForConnection(optionsItems, environmentService, remoteExplorerService);
	}

	private setSelectionForConnection(optionsItems: IRemoteSelectItem[], environmentService: IWorkbenchEnvironmentService, remoteExplorerService: IRemoteExplorerService) {
		if (this.optionsItems.length > 0) {
			let index = 0;
45
			const remoteAuthority = environmentService.configuration.remoteAuthority;
46 47 48
			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) {
49
				index = this.getOptionIndexForExplorerType(optionsItems, explorerType);
50 51
			}
			this.select(index);
52
			remoteExplorerService.targetType = optionsItems[index].authority;
53 54 55
		}
	}

56
	private getOptionIndexForExplorerType(optionsItems: IRemoteSelectItem[], explorerType: string[]): number {
57 58 59
		let index = 0;
		for (let optionIterator = 0; (optionIterator < this.optionsItems.length) && (index === 0); optionIterator++) {
			for (let authorityIterator = 0; authorityIterator < optionsItems[optionIterator].authority.length; authorityIterator++) {
60 61 62 63 64
				for (let i = 0; i < explorerType.length; i++) {
					if (optionsItems[optionIterator].authority[authorityIterator] === explorerType[i]) {
						index = optionIterator;
						break;
					}
65 66 67 68 69 70
				}
			}
		}
		return index;
	}

71
	render(container: HTMLElement) {
72 73
		if (this.optionsItems.length > 1) {
			super.render(container);
74
			container.classList.add('switch-remote');
75
		}
76 77 78 79 80 81
	}

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

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

export class SwitchRemoteAction extends Action {

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

	constructor(
		id: string, label: string,
		@IRemoteExplorerService private readonly remoteExplorerService: IRemoteExplorerService
	) {
		super(id, label);
	}

	public async run(item: IRemoteSelectItem): Promise<any> {
106
		this.remoteExplorerService.targetType = item.authority;
107 108
	}
}