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

Finalize terminal/context, terminal/title/context #192809

Merged
merged 6 commits into from
Sep 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use an ActionRunner to transform context for term ctx menu actions
  • Loading branch information
Tyriar committed Sep 15, 2023
commit 456dd7a43b43cf07b61f335ef97b189ad8744f07
19 changes: 15 additions & 4 deletions src/vs/workbench/api/common/extHostTerminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,23 @@ export abstract class BaseExtHostTerminalService extends Disposable implements I
this._proxy.$registerProcessSupport(supportsProcesses);
this._extHostCommands.registerArgumentProcessor({
processArgument: arg => {
const deserialize = (arg: any) => {
const cast = arg as ISerializedTerminalInstanceContext;
return this._getTerminalById(cast.instanceId)?.value;
};
switch (arg?.$mid) {
case MarshalledId.TerminalContext: {
const cast = arg as ISerializedTerminalInstanceContext;
return cast.instanceIds.map(id => this._getTerminalById(id));
case MarshalledId.TerminalContext: return deserialize(arg);
default: {
if (Array.isArray(arg)) {
return arg.map(e => {
switch (e?.$mid) {
case MarshalledId.TerminalContext: return deserialize(e);
default: return e;
}
});
Tyriar marked this conversation as resolved.
Show resolved Hide resolved
}
return arg;
}
default: return arg;
}
}
});
Expand Down
40 changes: 30 additions & 10 deletions src/vs/workbench/contrib/terminal/browser/terminalContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,50 @@
*--------------------------------------------------------------------------------------------*/

import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { IAction } from 'vs/base/common/actions';
import { ActionRunner, IAction } from 'vs/base/common/actions';
import { asArray } from 'vs/base/common/arrays';
import { MarshalledId } from 'vs/base/common/marshallingIds';
import { SingleOrMany } from 'vs/base/common/types';
import { createAndFillInContextMenuActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { IMenu } from 'vs/platform/actions/common/actions';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ISerializedTerminalInstanceContext } from 'vs/workbench/contrib/terminal/common/terminal';

class IInstanceContext {
private _instanceIds: number[];
class InstanceContext {
private _instanceId: number;

constructor(
instances: SingleOrMany<ITerminalInstance> | undefined
) {
this._instanceIds = (!instances ? [] : Array.isArray(instances) ? instances : [instances]).map(e => e.instanceId);
constructor(instance: ITerminalInstance) {
this._instanceId = instance.instanceId;
}

toJSON(): ISerializedTerminalInstanceContext {
return {
$mid: MarshalledId.TerminalContext,
instanceIds: this._instanceIds
instanceId: this._instanceId
};
}
}

export function openContextMenu(event: MouseEvent, contextInstances: SingleOrMany<ITerminalInstance> | undefined, menu: IMenu, contextMenuService: IContextMenuService, extraActions?: IAction[]): void {
class TerminalContextActionRunner extends ActionRunner {
constructor(
private readonly _commandService: ICommandService
) {
super();
}

override run(action: IAction, context?: InstanceContext): Promise<void> {
if (Array.isArray(context) && context.every(e => e instanceof InstanceContext)) {
// arg1: The (first) focused instance
// arg2: All selected instances
return this._commandService.executeCommand(action.id, context?.[0], context);
}
return super.run(action, context);
}
}

export function openContextMenu(event: MouseEvent, contextInstances: SingleOrMany<ITerminalInstance> | undefined, menu: IMenu, commandService: ICommandService, contextMenuService: IContextMenuService, extraActions?: IAction[]): void {
const standardEvent = new StandardMouseEvent(event);

const actions: IAction[] = [];
Expand All @@ -41,9 +58,12 @@ export function openContextMenu(event: MouseEvent, contextInstances: SingleOrMan
actions.push(...extraActions);
}

const context: InstanceContext[] = contextInstances ? asArray(contextInstances).map(e => new InstanceContext(e)) : [];

contextMenuService.showContextMenu({
actionRunner: new TerminalContextActionRunner(commandService),
getAnchor: () => standardEvent,
getActions: () => actions,
getActionsContext: () => new IInstanceContext(contextInstances),
getActionsContext: () => context,
});
}
6 changes: 4 additions & 2 deletions src/vs/workbench/contrib/terminal/browser/terminalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { INotificationService } from 'vs/platform/notification/common/notificati
import { openContextMenu } from 'vs/workbench/contrib/terminal/browser/terminalContextMenu';
import { ACTIVE_GROUP } from 'vs/workbench/services/editor/common/editorService';
import { IWorkbenchLayoutService, Parts } from 'vs/workbench/services/layout/browser/layoutService';
import { ICommandService } from 'vs/platform/commands/common/commands';

export class TerminalEditor extends EditorPane {

Expand All @@ -55,6 +56,7 @@ export class TerminalEditor extends EditorPane {
@IContextKeyService contextKeyService: IContextKeyService,
@IMenuService menuService: IMenuService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@ICommandService private readonly _commandService: ICommandService,
@IContextMenuService private readonly _contextMenuService: IContextMenuService,
@INotificationService private readonly _notificationService: INotificationService,
@ITerminalProfileService private readonly _terminalProfileService: ITerminalProfileService,
Expand Down Expand Up @@ -138,7 +140,7 @@ export class TerminalEditor extends EditorPane {

// copyPaste: Shift+right click should open context menu
if (rightClickBehavior === 'copyPaste' && event.shiftKey) {
openContextMenu(event, this._editorInput?.terminalInstance, this._instanceMenu, this._contextMenuService);
openContextMenu(event, this._editorInput?.terminalInstance, this._instanceMenu, this._commandService, this._contextMenuService);
return;
}

Expand Down Expand Up @@ -176,7 +178,7 @@ export class TerminalEditor extends EditorPane {
else
if (!this._cancelContextMenu && rightClickBehavior !== 'copyPaste' && rightClickBehavior !== 'paste') {
if (!this._cancelContextMenu) {
openContextMenu(event, this._editorInput?.terminalInstance, this._instanceMenu, this._contextMenuService);
openContextMenu(event, this._editorInput?.terminalInstance, this._instanceMenu, this._commandService, this._contextMenuService);
}
event.preventDefault();
event.stopImmediatePropagation();
Expand Down
17 changes: 14 additions & 3 deletions src/vs/workbench/contrib/terminal/browser/terminalTabbedView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { TerminalStorageKeys } from 'vs/workbench/contrib/terminal/common/termin
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey';
import { getInstanceHoverInfo } from 'vs/workbench/contrib/terminal/browser/terminalTooltip';
import { IHoverService } from 'vs/workbench/services/hover/browser/hover';
import { ICommandService } from 'vs/platform/commands/common/commands';

const $ = dom.$;

Expand Down Expand Up @@ -73,6 +74,7 @@ export class TerminalTabbedView extends Disposable {
@ITerminalGroupService private readonly _terminalGroupService: ITerminalGroupService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@INotificationService private readonly _notificationService: INotificationService,
@ICommandService private readonly _commandService: ICommandService,
@IContextMenuService private readonly _contextMenuService: IContextMenuService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IMenuService menuService: IMenuService,
Expand Down Expand Up @@ -354,7 +356,7 @@ export class TerminalTabbedView extends Disposable {
else if (rightClickBehavior === 'copyPaste' || rightClickBehavior === 'paste') {
// copyPaste: Shift+right click should open context menu
if (rightClickBehavior === 'copyPaste' && event.shiftKey) {
openContextMenu(event, terminal, this._instanceMenu, this._contextMenuService);
openContextMenu(event, terminal, this._instanceMenu, this._commandService, this._contextMenuService);
return;
}

Expand Down Expand Up @@ -388,7 +390,7 @@ export class TerminalTabbedView extends Disposable {
}
terminalContainer.focus();
if (!this._cancelContextMenu) {
openContextMenu(event, this._terminalGroupService.activeInstance!, this._instanceMenu, this._contextMenuService);
openContextMenu(event, this._terminalGroupService.activeInstance!, this._instanceMenu, this._commandService, this._contextMenuService);
}
event.preventDefault();
event.stopImmediatePropagation();
Expand All @@ -404,7 +406,16 @@ export class TerminalTabbedView extends Disposable {
if (!emptyList) {
this._terminalGroupService.lastAccessedMenu = 'tab-list';
}
openContextMenu(event, this._tabList.getFocusedElements(), emptyList ? this._tabsListEmptyMenu : this._tabsListMenu, this._contextMenuService, emptyList ? this._getTabActions() : undefined);

// Put the focused item first as it's used as the first positional argument
const selectedInstances = this._tabList.getSelectedElements();
const focusedInstance = this._tabList.getFocusedElements()?.[0];
if (focusedInstance) {
selectedInstances.splice(selectedInstances.findIndex(e => e.instanceId === focusedInstance.instanceId), 1);
selectedInstances.unshift(focusedInstance);
}

openContextMenu(event, selectedInstances, emptyList ? this._tabsListEmptyMenu : this._tabsListMenu, this._commandService, this._contextMenuService, emptyList ? this._getTabActions() : undefined);
}
event.preventDefault();
event.stopImmediatePropagation();
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export interface ITerminalStatusHoverAction {
*/
export interface ISerializedTerminalInstanceContext {
$mid: MarshalledId.TerminalContext;
instanceIds: number[];
instanceId: number;
}

export const QUICK_LAUNCH_PROFILE_CHOICE = 'workbench.action.terminal.profile.choice';
Expand Down
Loading