Skip to content

Commit

Permalink
Allow multiselect and multidelete in ports view
Browse files Browse the repository at this point in the history
  • Loading branch information
alexr00 committed May 5, 2021
1 parent 04a3fb2 commit e8c4f47
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions src/vs/workbench/contrib/remote/browser/tunnelView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ const TunnelPrivacyContextKey = new RawContextKey<TunnelPrivacy | undefined>('tu
const TunnelViewFocusContextKey = new RawContextKey<boolean>('tunnelViewFocus', false, nls.localize('tunnel.focusContext', "Whether the Ports view has focus."));
const TunnelViewSelectionKeyName = 'tunnelViewSelection';
const TunnelViewSelectionContextKey = new RawContextKey<ITunnelItem | undefined>(TunnelViewSelectionKeyName, undefined, true);
const TunnelViewMultiSelectionKeyName = 'tunnelViewMultiSelection';
const TunnelViewMultiSelectionContextKey = new RawContextKey<ITunnelItem[] | undefined>(TunnelViewMultiSelectionKeyName, undefined, true);
const PortChangableContextKey = new RawContextKey<boolean>('portChangable', false, true);
const WebContextKey = new RawContextKey<boolean>('isWeb', isWeb, true);

Expand All @@ -679,6 +681,7 @@ export class TunnelPanel extends ViewPane {
private tunnelPrivacyContext: IContextKey<TunnelPrivacy | undefined>;
private tunnelViewFocusContext: IContextKey<boolean>;
private tunnelViewSelectionContext: IContextKey<ITunnelItem | undefined>;
private tunnelViewMultiSelectionContext: IContextKey<ITunnelItem[] | undefined>;
private portChangableContextKey: IContextKey<boolean>;
private isEditing: boolean = false;
private titleActions: IAction[] = [];
Expand Down Expand Up @@ -711,6 +714,7 @@ export class TunnelPanel extends ViewPane {
this.tunnelPrivacyContext = TunnelPrivacyContextKey.bindTo(contextKeyService);
this.tunnelViewFocusContext = TunnelViewFocusContextKey.bindTo(contextKeyService);
this.tunnelViewSelectionContext = TunnelViewSelectionContextKey.bindTo(contextKeyService);
this.tunnelViewMultiSelectionContext = TunnelViewMultiSelectionContextKey.bindTo(contextKeyService);
this.portChangableContextKey = PortChangableContextKey.bindTo(contextKeyService);

const overlayContextKeyService = this._register(this.contextKeyService.createOverlay([['view', TunnelPanel.ID]]));
Expand Down Expand Up @@ -762,7 +766,7 @@ export class TunnelPanel extends ViewPane {
return item.label;
}
},
multipleSelectionSupport: false,
multipleSelectionSupport: true,
accessibilityProvider: {
getAriaLabel: (item: ITunnelItem) => {
if (item instanceof TunnelItem) {
Expand All @@ -783,6 +787,7 @@ export class TunnelPanel extends ViewPane {
this._register(this.table.onContextMenu(e => this.onContextMenu(e, actionRunner)));
this._register(this.table.onMouseDblClick(e => this.onMouseDblClick(e)));
this._register(this.table.onDidChangeFocus(e => this.onFocusChanged(e)));
this._register(this.table.onDidChangeSelection(e => this.onSelectionChanged(e)));
this._register(this.table.onDidFocus(() => this.tunnelViewFocusContext.set(true)));
this._register(this.table.onDidBlur(() => this.tunnelViewFocusContext.set(false)));

Expand Down Expand Up @@ -879,6 +884,15 @@ export class TunnelPanel extends ViewPane {
}
}

private onSelectionChanged(event: ITableEvent<ITunnelItem>) {
const elements = event.elements;
if (elements.length > 1) {
this.tunnelViewMultiSelectionContext.set(elements);
} else {
this.tunnelViewMultiSelectionContext.set(undefined);
}
}

private onContextMenu(event: ITableContextMenuEvent<ITunnelItem>, actionRunner: ActionRunner): void {
if ((event.element !== undefined) && !(event.element instanceof TunnelItem)) {
return;
Expand Down Expand Up @@ -1097,11 +1111,20 @@ namespace ClosePortAction {

export function inlineHandler(): ICommandHandler {
return async (accessor, arg) => {
const context = (arg !== undefined || arg instanceof TunnelItem) ? arg : accessor.get(IContextKeyService).getContextKeyValue(TunnelViewSelectionKeyName);
if (context instanceof TunnelItem) {
const remoteExplorerService = accessor.get(IRemoteExplorerService);
await remoteExplorerService.close({ host: context.remoteHost, port: context.remotePort });
const contextKeyService = accessor.get(IContextKeyService);
let ports = contextKeyService.getContextKeyValue<ITunnelItem[] | undefined>(TunnelViewMultiSelectionKeyName);
if (!ports) {
const context = (arg !== undefined || arg instanceof TunnelItem) ?
arg : contextKeyService.getContextKeyValue<ITunnelItem | undefined>(TunnelViewSelectionKeyName);
if (context) {
ports = [context];
}
}
if (!ports) {
return;
}
const remoteExplorerService = accessor.get(IRemoteExplorerService);
return Promise.all(ports.map(port => remoteExplorerService.close({ host: port.remoteHost, port: port.remotePort })));
};
}

Expand Down Expand Up @@ -1351,10 +1374,14 @@ namespace MakePortPrivateAction {

const tunnelViewCommandsWeightBonus = 10; // give our commands a little bit more weight over other default list/tree commands

const isForwardedExpr = TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded);
const isForwardedOrDetectedExpr = ContextKeyExpr.or(isForwardedExpr, TunnelTypeContextKey.isEqualTo(TunnelType.Detected));
const isNotMultiSelectionExpr = TunnelViewMultiSelectionContextKey.isEqualTo(undefined);

KeybindingsRegistry.registerCommandAndKeybindingRule({
id: LabelTunnelAction.ID,
weight: KeybindingWeight.WorkbenchContrib + tunnelViewCommandsWeightBonus,
when: ContextKeyExpr.and(TunnelViewFocusContextKey, TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded)),
when: ContextKeyExpr.and(TunnelViewFocusContextKey, isForwardedExpr, isNotMultiSelectionExpr),
primary: KeyCode.F2,
mac: {
primary: KeyCode.Enter
Expand Down Expand Up @@ -1382,7 +1409,7 @@ CommandsRegistry.registerCommand(OpenPortInBrowserCommandPaletteAction.ID, OpenP
KeybindingsRegistry.registerCommandAndKeybindingRule({
id: CopyAddressAction.INLINE_ID,
weight: KeybindingWeight.WorkbenchContrib + tunnelViewCommandsWeightBonus,
when: ContextKeyExpr.or(ContextKeyExpr.and(TunnelViewFocusContextKey, TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded)), ContextKeyExpr.and(TunnelViewFocusContextKey, TunnelTypeContextKey.isEqualTo(TunnelType.Detected))),
when: ContextKeyExpr.and(TunnelViewFocusContextKey, isForwardedOrDetectedExpr, isNotMultiSelectionExpr),
primary: KeyMod.CtrlCmd | KeyCode.KEY_C,
handler: CopyAddressAction.inlineHandler()
});
Expand Down Expand Up @@ -1427,7 +1454,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
id: OpenPortInBrowserAction.ID,
title: OpenPortInBrowserAction.LABEL,
},
when: ContextKeyExpr.or(TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded), TunnelTypeContextKey.isEqualTo(TunnelType.Detected))
when: ContextKeyExpr.and(isForwardedOrDetectedExpr, isNotMultiSelectionExpr)
}));
MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
group: '._open',
Expand All @@ -1438,7 +1465,8 @@ MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
},
when: ContextKeyExpr.and(
ContextKeyExpr.or(WebContextKey.negate(), TunnelPrivacyContextKey.isEqualTo(TunnelPrivacy.Public)),
ContextKeyExpr.or(TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded), TunnelTypeContextKey.isEqualTo(TunnelType.Detected)))
isForwardedOrDetectedExpr,
isNotMultiSelectionExpr)
}));
// The group 0_manage is used by extensions, so try not to change it
MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
Expand All @@ -1449,7 +1477,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
title: LabelTunnelAction.LABEL,
icon: labelPortIcon
},
when: TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded)
when: ContextKeyExpr.and(isForwardedExpr, isNotMultiSelectionExpr)
}));
MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
group: '2_localaddress',
Expand All @@ -1458,7 +1486,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
id: CopyAddressAction.INLINE_ID,
title: CopyAddressAction.INLINE_LABEL,
},
when: ContextKeyExpr.or(TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded), TunnelTypeContextKey.isEqualTo(TunnelType.Detected))
when: ContextKeyExpr.and(isForwardedOrDetectedExpr, isNotMultiSelectionExpr)
}));
MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
group: '2_localaddress',
Expand All @@ -1467,7 +1495,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
id: ChangeLocalPortAction.ID,
title: ChangeLocalPortAction.LABEL,
},
when: ContextKeyExpr.and(TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded), PortChangableContextKey)
when: ContextKeyExpr.and(isForwardedExpr, PortChangableContextKey, isNotMultiSelectionExpr)
}));
MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
group: '2_localaddress',
Expand All @@ -1476,7 +1504,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
id: MakePortPublicAction.ID,
title: MakePortPublicAction.LABEL,
},
when: TunnelPrivacyContextKey.isEqualTo(TunnelPrivacy.Private)
when: ContextKeyExpr.and(TunnelPrivacyContextKey.isEqualTo(TunnelPrivacy.Private), isNotMultiSelectionExpr)
}));
MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
group: '2_localaddress',
Expand All @@ -1485,7 +1513,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
id: MakePortPrivateAction.ID,
title: MakePortPrivateAction.LABEL,
},
when: TunnelPrivacyContextKey.isEqualTo(TunnelPrivacy.Public)
when: ContextKeyExpr.and(TunnelPrivacyContextKey.isEqualTo(TunnelPrivacy.Public), isNotMultiSelectionExpr)
}));
MenuRegistry.appendMenuItem(MenuId.TunnelContext, ({
group: '3_forward',
Expand Down Expand Up @@ -1524,7 +1552,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelPortInline, ({
title: LabelTunnelAction.LABEL,
icon: labelPortIcon
},
when: TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded)
when: isForwardedExpr
}));
MenuRegistry.appendMenuItem(MenuId.TunnelPortInline, ({
group: '0_manage',
Expand All @@ -1544,7 +1572,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelLocalAddressInline, ({
title: CopyAddressAction.INLINE_LABEL,
icon: copyAddressIcon
},
when: ContextKeyExpr.or(TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded), TunnelTypeContextKey.isEqualTo(TunnelType.Detected))
when: isForwardedOrDetectedExpr
}));
MenuRegistry.appendMenuItem(MenuId.TunnelLocalAddressInline, ({
order: 0,
Expand All @@ -1553,7 +1581,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelLocalAddressInline, ({
title: OpenPortInBrowserAction.LABEL,
icon: openBrowserIcon
},
when: ContextKeyExpr.or(TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded), TunnelTypeContextKey.isEqualTo(TunnelType.Detected))
when: isForwardedOrDetectedExpr
}));
MenuRegistry.appendMenuItem(MenuId.TunnelLocalAddressInline, ({
order: 1,
Expand All @@ -1564,7 +1592,7 @@ MenuRegistry.appendMenuItem(MenuId.TunnelLocalAddressInline, ({
},
when: ContextKeyExpr.and(
ContextKeyExpr.or(WebContextKey.negate(), TunnelPrivacyContextKey.isEqualTo(TunnelPrivacy.Public)),
ContextKeyExpr.or(TunnelTypeContextKey.isEqualTo(TunnelType.Forwarded), TunnelTypeContextKey.isEqualTo(TunnelType.Detected)))
isForwardedOrDetectedExpr)
}));

export const portWithRunningProcessForeground = registerColor('ports.iconRunningProcessForeground', {
Expand Down

0 comments on commit e8c4f47

Please sign in to comment.