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

Right click to copy selection and paste on Windows #17496

Merged
merged 5 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions src/vs/workbench/parts/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const TERMINAL_DEFAULT_SHELL_LINUX = !platform.isWindows ? (process.env.S
export const TERMINAL_DEFAULT_SHELL_OSX = !platform.isWindows ? (process.env.SHELL || 'sh') : 'sh';
export const TERMINAL_DEFAULT_SHELL_WINDOWS = processes.getWindowsShell();

export const TERMINAL_DEFAULT_RIGHT_CLICK_COPY_PASTE = platform.isWindows;

/** A context key that is set when the integrated terminal has focus. */
export const KEYBINDING_CONTEXT_TERMINAL_FOCUS = new RawContextKey<boolean>('terminalFocus', undefined);
/** A context key that is set when the integrated terminal does not have focus. */
Expand All @@ -44,6 +46,7 @@ export interface ITerminalConfiguration {
osx: string[],
windows: string[]
},
rightClickCopyPaste: boolean,
cursorBlinking: boolean,
fontFamily: string,
fontLigatures: boolean,
Expand All @@ -62,6 +65,7 @@ export interface ITerminalConfigHelper {
getFont(): ITerminalFont;
getFontLigaturesEnabled(): boolean;
getCursorBlink(): boolean;
getRightClickCopyPaste(): boolean;
getCommandsToSkipShell(): string[];
getScrollback(): number;
getCwd(): string;
Expand Down Expand Up @@ -146,11 +150,21 @@ export interface ITerminalInstance {
*/
dispose(): void;

/**
* Check if anything is selected in terminal.
*/
hasSelection(): boolean;

/**
* Copies the terminal selection to the clipboard.
*/
copySelection(): void;

/**
* Clear current selection.
*/
clearSelection(): void;

/**
* Focuses the terminal instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as platform from 'vs/base/common/platform';
import nls = require('vs/nls');
import { Extensions, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry';
import { GlobalQuickOpenAction } from 'vs/workbench/browser/parts/quickopen/quickopen.contribution';
import { ITerminalService, KEYBINDING_CONTEXT_TERMINAL_FOCUS, KEYBINDING_CONTEXT_TERMINAL_TEXT_SELECTED, TERMINAL_PANEL_ID, TERMINAL_DEFAULT_SHELL_LINUX, TERMINAL_DEFAULT_SHELL_OSX, TERMINAL_DEFAULT_SHELL_WINDOWS } from 'vs/workbench/parts/terminal/common/terminal';
import { ITerminalService, KEYBINDING_CONTEXT_TERMINAL_FOCUS, KEYBINDING_CONTEXT_TERMINAL_TEXT_SELECTED, TERMINAL_PANEL_ID, TERMINAL_DEFAULT_SHELL_LINUX, TERMINAL_DEFAULT_SHELL_OSX, TERMINAL_DEFAULT_SHELL_WINDOWS, TERMINAL_DEFAULT_RIGHT_CLICK_COPY_PASTE } from 'vs/workbench/parts/terminal/common/terminal';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actionRegistry';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
Expand Down Expand Up @@ -73,6 +73,11 @@ configurationRegistry.registerConfiguration({
},
'default': []
},
'terminal.integrated.rightClickCopyPaste': {
'description': nls.localize('terminal.integrated.rightClickCopyPaste', "When set, this will prevent the context menu from appearing when right clicking within the terminal, instead it will copy when there is a selection and paste when there is no selection."),
'type': 'boolean',
'default': TERMINAL_DEFAULT_RIGHT_CLICK_COPY_PASTE
},
'terminal.integrated.fontFamily': {
'description': nls.localize('terminal.integrated.fontFamily', "Controls the font family of the terminal, this defaults to editor.fontFamily's value."),
'type': 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class KillTerminalAction extends Action {

/**
* Copies the terminal selection. Note that since the command palette takes focus from the terminal,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems obsolete to me. Let me know if i'm missing something.

* this can only be triggered via a keybinding.
* this cannot be triggered through the command palette.
*/
export class CopyTerminalSelectionAction extends Action {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export class TerminalConfigHelper implements ITerminalConfigHelper {
return terminalConfig.cursorBlinking;
}

public getRightClickCopyPaste(): boolean {
let config = this._configurationService.getConfiguration<ITerminalConfiguration>();
return config.terminal.integrated.rightClickCopyPaste;
}

public getShell(): IShell {
let config = this._configurationService.getConfiguration<ITerminalConfiguration>();
let shell: IShell = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ export class TerminalInstance implements ITerminalInstance {
this.updateConfig();
}

public hasSelection(): boolean {
return !document.getSelection().isCollapsed;
}

public copySelection(): void {
if (document.activeElement.classList.contains('xterm')) {
document.execCommand('copy');
Expand All @@ -203,6 +207,10 @@ export class TerminalInstance implements ITerminalInstance {
}
}

public clearSelection(): void {
document.getSelection().empty();
}

public dispose(): void {
this._isExiting = true;

Expand Down
39 changes: 22 additions & 17 deletions src/vs/workbench/parts/terminal/electron-browser/terminalPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,30 @@ export class TerminalPanel extends Panel {
// occurs on the selection itself.
this._terminalService.getActiveInstance().focus();
} else if (event.which === 3) {
// Trigger the context menu on right click
let anchor: HTMLElement | { x: number, y: number } = this._parentDomElement;
if (event instanceof MouseEvent) {
if (this._terminalService.configHelper.getRightClickCopyPaste()) {
let terminal = this._terminalService.getActiveInstance();
if (terminal.hasSelection()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that this is actually my code haha, as long as the context menu appears in the correct spot after this change (when rightClickCopyPaste is false) then this was fine to remove 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it still works with the change

terminal.copySelection();
terminal.clearSelection();
} else {
terminal.paste();
}
} else {
const standardEvent = new StandardMouseEvent(event);
anchor = { x: standardEvent.posx, y: standardEvent.posy };
}

this._contextMenuService.showContextMenu({
getAnchor: () => anchor,
getActions: () => TPromise.as(this._getContextMenuActions()),
getActionsContext: () => this._parentDomElement,
getKeyBinding: (action) => {
const opts = this._keybindingService.lookupKeybindings(action.id);
if (opts.length > 0) {
return opts[0]; // only take the first one
let anchor: { x: number, y: number } = { x: standardEvent.posx, y: standardEvent.posy };
this._contextMenuService.showContextMenu({
getAnchor: () => anchor,
getActions: () => TPromise.as(this._getContextMenuActions()),
getActionsContext: () => this._parentDomElement,
getKeyBinding: (action) => {
const opts = this._keybindingService.lookupKeybindings(action.id);
if (opts.length > 0) {
return opts[0]; // only take the first one
}
return null;
}
return null;
}
});
});
}
}
}));
this._register(DOM.addDisposableListener(this._parentDomElement, 'click', (event) => {
Expand Down