Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Remote button not working after hiding #194604

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
128 changes: 65 additions & 63 deletions src/vs/workbench/contrib/remote/browser/explorerViewItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,56 @@
*--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
import { IAction } from 'vs/base/common/actions';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IRemoteExplorerService, REMOTE_EXPLORER_TYPE_KEY } from 'vs/workbench/services/remote/common/remoteExplorerService';
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';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { SelectActionViewItem } from 'vs/base/browser/ui/actionbar/actionViewItems';
import { Action2, MenuId } from 'vs/platform/actions/common/actions';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ContextKeyExpr, IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { Action2, MenuId, MenuRegistry, registerAction2 } from 'vs/platform/actions/common/actions';
import { VIEWLET_ID } from 'vs/workbench/contrib/remote/browser/remoteExplorer';
import { defaultSelectBoxStyles } from 'vs/platform/theme/browser/defaultStyles';
import { getVirtualWorkspaceLocation } from 'vs/platform/workspace/common/virtualWorkspace';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { Disposable } from 'vs/base/common/lifecycle';

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

export class SwitchRemoteViewItem extends SelectActionViewItem<IRemoteSelectItem> {
export const SELECTED_REMOTE_IN_EXPLORER = new RawContextKey<string>('selectedRemoteInExplorer', '');

export class SwitchRemoteViewItem extends Disposable {
private switchRemoteMenu: MenuId;
private options: IRemoteSelectItem[] = [];
private completedRemotes: Set<string> = new Set();
private readonly selectedRemoteContext: IContextKey<string>;

constructor(
action: IAction,
private readonly optionsItems: IRemoteSelectItem[],
@IContextViewService contextViewService: IContextViewService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IRemoteExplorerService private remoteExplorerService: IRemoteExplorerService,
@IWorkbenchEnvironmentService private environmentService: IWorkbenchEnvironmentService,
@IStorageService private readonly storageService: IStorageService,
@IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService
) {
super(null, action, optionsItems, 0, contextViewService, defaultSelectBoxStyles, { ariaLabel: nls.localize('remotes', 'Switch Remote') });
super();
this.selectedRemoteContext = SELECTED_REMOTE_IN_EXPLORER.bindTo(contextKeyService);

this.switchRemoteMenu = new MenuId('workbench.remote.menu.switchRemoteMenu');
this._register(MenuRegistry.appendMenuItem(MenuId.ViewContainerTitle, {
submenu: this.switchRemoteMenu,
title: nls.localize('switchRemote.label', "Switch Remote"),
group: 'navigation',
when: ContextKeyExpr.equals('activeViewlet', VIEWLET_ID),
alexr00 marked this conversation as resolved.
Show resolved Hide resolved
order: 1,
isSelection: true
}));
}

public setSelectionForConnection(): boolean {
let isSetForConnection = false;
if (this.optionsItems.length > 0) {
if (this.options.length > 0) {
let index = 0;
const remoteAuthority = this.environmentService.remoteAuthority;
let virtualWorkspace: string | undefined;
Expand All @@ -56,26 +67,25 @@ export class SwitchRemoteViewItem extends SelectActionViewItem<IRemoteSelectItem
if (explorerType !== undefined) {
index = this.getOptionIndexForExplorerType(explorerType);
}
this.select(index);
this.remoteExplorerService.targetType = this.optionsItems[index].authority;
this.select(this.options[index].authority);
}
return isSetForConnection;
}

public setSelection() {
const index = this.getOptionIndexForExplorerType(this.remoteExplorerService.targetType);
this.select(index);
private select(authority: string[]) {
this.selectedRemoteContext.set(authority[0]);
this.remoteExplorerService.targetType = authority;
}

private getOptionIndexForExplorerType(explorerType: string[]): number {
let index = 0;
for (let optionIterator = 0; (optionIterator < this.optionsItems.length) && (index === 0); optionIterator++) {
for (let authorityIterator = 0; authorityIterator < this.optionsItems[optionIterator].authority.length; authorityIterator++) {
for (let optionIterator = 0; (optionIterator < this.options.length) && (index === 0); optionIterator++) {
for (let authorityIterator = 0; authorityIterator < this.options[optionIterator].authority.length; authorityIterator++) {
for (let i = 0; i < explorerType.length; i++) {
if (this.optionsItems[optionIterator].authority[authorityIterator] === explorerType[i]) {
if (this.options[optionIterator].authority[authorityIterator] === explorerType[i]) {
index = optionIterator;
break;
} else if (this.optionsItems[optionIterator].virtualWorkspace === explorerType[i]) {
} else if (this.options[optionIterator].virtualWorkspace === explorerType[i]) {
index = optionIterator;
break;
}
Expand All @@ -85,47 +95,39 @@ export class SwitchRemoteViewItem extends SelectActionViewItem<IRemoteSelectItem
return index;
}

override render(container: HTMLElement) {
if (this.optionsItems.length > 1) {
super.render(container);
container.classList.add('switch-remote');
}
}

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

static createOptionItems(views: IViewDescriptor[], contextKeyService: IContextKeyService): IRemoteSelectItem[] {
const options: IRemoteSelectItem[] = [];
views.forEach(view => {
if (view.group && view.group.startsWith('targets') && view.remoteAuthority && (!view.when || contextKeyService.contextMatchesRules(view.when))) {
options.push({ text: view.name, authority: isStringArray(view.remoteAuthority) ? view.remoteAuthority : [view.remoteAuthority], virtualWorkspace: view.virtualWorkspace });
public createOptionItems(views: IViewDescriptor[]) {
const startingCount = this.completedRemotes.size;
for (const view of views) {
if (view.group && view.group.startsWith('targets') && view.remoteAuthority && (!view.when || this.contextKeyService.contextMatchesRules(view.when))) {
const text = view.name;
const authority = isStringArray(view.remoteAuthority) ? view.remoteAuthority : [view.remoteAuthority];
if (authority.some(a => this.completedRemotes.has(a))) {
return;
}
const thisCapture = this;
this._register(registerAction2(class extends Action2 {
constructor() {
super({
id: `workbench.action.remoteExplorer.show.${authority[0]}`,
title: text,
toggled: SELECTED_REMOTE_IN_EXPLORER.isEqualTo(authority[0]),
menu: {
id: thisCapture.switchRemoteMenu
}
});
}
async run(): Promise<void> {
thisCapture.select(authority);
}
}));
this.options.push({ text, authority });
for (const authorityOption of authority) {
this.completedRemotes.add(authorityOption);
}
}
});
return options;
}
}

export class SwitchRemoteAction extends Action2 {

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

constructor() {
super({
id: SwitchRemoteAction.ID,
title: SwitchRemoteAction.LABEL,
menu: [{
id: MenuId.ViewContainerTitle,
when: ContextKeyExpr.equals('viewContainer', VIEWLET_ID),
group: 'navigation',
order: 1
}],
});
}

public async run(accessor: ServicesAccessor, args: IRemoteSelectItem): Promise<any> {
accessor.get(IRemoteExplorerService).targetType = args.authority;
}
if (this.completedRemotes.size > startingCount) {
this.setSelectionForConnection();
}
}
}
33 changes: 10 additions & 23 deletions src/vs/workbench/contrib/remote/browser/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import { IExtensionService, isProposedApiEnabled } from 'vs/workbench/services/e
import { FilterViewPaneContainer } from 'vs/workbench/browser/parts/views/viewsViewlet';
import { VIEWLET_ID } from 'vs/workbench/contrib/remote/browser/remoteExplorer';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IViewDescriptor, IViewsRegistry, Extensions, ViewContainerLocation, IViewContainersRegistry, IViewDescriptorService } from 'vs/workbench/common/views';
import { IViewDescriptor, IViewsRegistry, Extensions, ViewContainerLocation, IViewContainersRegistry, IViewDescriptorService, IAddedViewDescriptorRef } from 'vs/workbench/common/views';
import { Registry } from 'vs/platform/registry/common/platform';
import { IExtensionDescription, IRelaxedExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { registerAction2 } from 'vs/platform/actions/common/actions';
import { IProgress, IProgressStep, IProgressService, ProgressLocation } from 'vs/platform/progress/common/progress';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
Expand All @@ -35,8 +34,7 @@ import { ReconnectionWaitEvent, PersistentConnectionEventType } from 'vs/platfor
import Severity from 'vs/base/common/severity';
import { ReloadWindowAction } from 'vs/workbench/browser/actions/windowActions';
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
import { SwitchRemoteViewItem, SwitchRemoteAction } from 'vs/workbench/contrib/remote/browser/explorerViewItems';
import { Action } from 'vs/base/common/actions';
import { SwitchRemoteViewItem } from 'vs/workbench/contrib/remote/browser/explorerViewItems';
import { isStringArray } from 'vs/base/common/types';
import { IRemoteExplorerService } from 'vs/workbench/services/remote/common/remoteExplorerService';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
Expand All @@ -52,7 +50,6 @@ import * as icons from 'vs/workbench/contrib/remote/browser/remoteIcons';
import { ILogService } from 'vs/platform/log/common/log';
import { ITimerService } from 'vs/workbench/services/timer/browser/timerService';
import { getRemoteName } from 'vs/platform/remote/common/remoteHosts';
import { IActionViewItem } from 'vs/base/browser/ui/actionbar/actionbar';
import { getVirtualWorkspaceLocation } from 'vs/platform/workspace/common/virtualWorkspace';
import { IJSONSchema } from 'vs/base/common/jsonSchema';
import { IWalkthroughsService } from 'vs/workbench/contrib/welcomeGettingStarted/browser/gettingStartedService';
Expand Down Expand Up @@ -582,8 +579,8 @@ class RemoteViewPaneContainer extends FilterViewPaneContainer implements IViewMo
helpInformation: HelpInformation[] = [];
private _onDidChangeHelpInformation = new Emitter<void>();
public onDidChangeHelpInformation: Event<void> = this._onDidChangeHelpInformation.event;
private hasSetSwitchForConnection: boolean = false;
private hasRegisteredHelpView: boolean = false;
private remoteSwitcher: SwitchRemoteViewItem | undefined;

constructor(
@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
Expand All @@ -596,11 +593,11 @@ class RemoteViewPaneContainer extends FilterViewPaneContainer implements IViewMo
@IContextMenuService contextMenuService: IContextMenuService,
@IExtensionService extensionService: IExtensionService,
@IRemoteExplorerService private readonly remoteExplorerService: IRemoteExplorerService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IViewDescriptorService viewDescriptorService: IViewDescriptorService
) {
super(VIEWLET_ID, remoteExplorerService.onDidChangeTargetType, configurationService, layoutService, telemetryService, storageService, instantiationService, themeService, contextMenuService, extensionService, contextService, viewDescriptorService);
this.addConstantViewDescriptors([this.helpPanelDescriptor]);
this._register(this.remoteSwitcher = this.instantiationService.createInstance(SwitchRemoteViewItem));
remoteHelpExtPoint.setHandler((extensions) => {
const helpInformation: HelpInformation[] = [];
for (const extension of extensions) {
Expand All @@ -619,6 +616,12 @@ class RemoteViewPaneContainer extends FilterViewPaneContainer implements IViewMo
this.hasRegisteredHelpView = false;
}
});
this.remoteSwitcher.createOptionItems(Registry.as<IViewsRegistry>(Extensions.ViewsRegistry).getViews(this.viewContainer));
}

protected override onDidAddViewDescriptors(added: IAddedViewDescriptorRef[]): ViewPane[] {
this.remoteSwitcher!.createOptionItems(Registry.as<IViewsRegistry>(Extensions.ViewsRegistry).getViews(this.viewContainer));
return super.onDidAddViewDescriptors(added);
}

private _handleRemoteInfoExtensionPoint(extension: IExtensionPointUser<HelpInformation>, helpInformation: HelpInformation[]) {
Expand Down Expand Up @@ -649,29 +652,13 @@ class RemoteViewPaneContainer extends FilterViewPaneContainer implements IViewMo
this.remoteExplorerService.targetType = isStringArray(viewDescriptor.remoteAuthority) ? viewDescriptor.remoteAuthority : [viewDescriptor.remoteAuthority!];
}

public override getActionViewItem(action: Action): IActionViewItem | undefined {
if (action.id === SwitchRemoteAction.ID) {
const optionItems = SwitchRemoteViewItem.createOptionItems(Registry.as<IViewsRegistry>(Extensions.ViewsRegistry).getViews(this.viewContainer), this.contextKeyService);
const item = this.instantiationService.createInstance(SwitchRemoteViewItem, action, optionItems);
if (!this.hasSetSwitchForConnection) {
this.hasSetSwitchForConnection = item.setSelectionForConnection();
} else {
item.setSelection();
}
return item;
}

return super.getActionViewItem(action);
}

getTitle(): string {
const title = nls.localize('remote.explorer', "Remote Explorer");
return title;
}
}

registerAction2(SwitchRemoteAction);

Registry.as<IViewContainersRegistry>(Extensions.ViewContainersRegistry).registerViewContainer(
{
id: VIEWLET_ID,
Expand Down