From cb3a9e3bdc92336c30e319e084d24c9fcc47584e Mon Sep 17 00:00:00 2001 From: Carlos Alcaide Corvo Date: Wed, 22 Nov 2023 09:21:25 -0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20Add=20custom=20key=20ha?= =?UTF-8?q?ndler=20for=20windows=20(#2001)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 🤖 Add custom key handler for windows Add custom key handler only for Windows OS so we achieve Ctrl+C to copy within the embedded terminal * Await for clipboard promise to complete --- .../app/components/session/tabs/index.js | 19 ++++++++++++++++++- ui/desktop/electron-app/src/ipc/handlers.js | 17 +++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/ui/desktop/app/components/session/tabs/index.js b/ui/desktop/app/components/session/tabs/index.js index c1334a728..67dd62370 100644 --- a/ui/desktop/app/components/session/tabs/index.js +++ b/ui/desktop/app/components/session/tabs/index.js @@ -50,12 +50,29 @@ export default class SessionTerminalTabsComponent extends Component { return; } + const { isWindows } = await this.ipc.invoke('checkOS'); const xterm = new Terminal(terminalOptions); this.terminal = xterm; const fitAddon = new FitAddon(); const termContainer = document.getElementById(`terminal-container`); calculateTerminalContainerHeight(termContainer); + /** + * Custom key event handler for windows to achieve Ctrl+C as on the other OS's + */ + if (isWindows) { + xterm.attachCustomKeyEventHandler(async (arg) => { + if (arg.ctrlKey && arg.code === 'KeyC' && arg.type === 'keydown') { + const selection = xterm.getSelection(); + if (selection) { + await navigator.clipboard.writeText(selection); + return false; + } + } + return true; + }); + } + xterm.loadAddon(fitAddon); xterm.loadAddon(new CanvasAddon()); xterm.open(termContainer); @@ -80,7 +97,7 @@ export default class SessionTerminalTabsComponent extends Component { // Send an SSH command immediately window.terminal.send( `ssh ${proxy_address} -p ${proxy_port} -o NoHostAuthenticationForLocalhost=yes\r`, - this.id + this.id, ); } } diff --git a/ui/desktop/electron-app/src/ipc/handlers.js b/ui/desktop/electron-app/src/ipc/handlers.js index 943db257b..7a171ed1e 100644 --- a/ui/desktop/electron-app/src/ipc/handlers.js +++ b/ui/desktop/electron-app/src/ipc/handlers.js @@ -54,7 +54,7 @@ handle('openExternal', async (href) => { } else { throw new Error( `URLs may only be opened over HTTPS in an external browser. - The URL '${href}' could not be opened.` + The URL '${href}' could not be opened.`, ); } }); @@ -68,7 +68,7 @@ handle('cliExists', () => boundaryCli.exists()); * Establishes a boundary session and returns session details. */ handle('connect', ({ target_id, token, host_id }) => - sessionManager.start(runtimeSettings.clusterUrl, target_id, token, host_id) + sessionManager.start(runtimeSettings.clusterUrl, target_id, token, host_id), ); /** @@ -82,7 +82,7 @@ handle('stop', ({ session_id }) => sessionManager.stopById(session_id)); * `DISABLE_WINDOW_CHROME=true`. */ handle('hasMacOSChrome', () => - process.env.DISABLE_WINDOW_CHROME ? false : isMac() + process.env.DISABLE_WINDOW_CHROME ? false : isMac(), ); /** @@ -91,7 +91,7 @@ handle('hasMacOSChrome', () => * `DISABLE_WINDOW_CHROME=true`. */ handle('showWindowActions', () => - process.env.DISABLE_WINDOW_CHROME ? false : !isMac() + process.env.DISABLE_WINDOW_CHROME ? false : !isMac(), ); /** @@ -117,6 +117,15 @@ handle('closeWindow', () => app.quit()); */ handle('checkCommand', async (command) => which(command, { nothrow: true })); +/** + * Return an object containing helper fields for determining what OS we're running on + */ +handle('checkOS', () => ({ + isLinux: isLinux(), + isMac: isMac(), + isWindows: isWindows(), +})); + /** * Handler to help create terminal windows. We don't use the helper `handle` method * as we need access to the event and don't need to be using `ipcMain.handle`.