From bcf1157b3726612e661581eebe9055357d92879b Mon Sep 17 00:00:00 2001 From: CHaBou Date: Thu, 5 Oct 2017 20:39:39 +0200 Subject: [PATCH] Add new keymaps to jump between tabs (#2330) --- app/config/keymaps.js | 17 ++++++++++++++++- app/keymaps/darwin.json | 3 ++- app/keymaps/linux.json | 1 + app/keymaps/win32.json | 1 + app/menus/menus/window.js | 23 ++++++++++++++++++++++- lib/actions/ui.js | 8 ++++++++ lib/index.js | 4 ++++ 7 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app/config/keymaps.js b/app/config/keymaps.js index 912ea2c8..915d5335 100644 --- a/app/config/keymaps.js +++ b/app/config/keymaps.js @@ -5,10 +5,25 @@ const {defaultPlatformKeyPath} = require('./paths'); const commands = {}; const keys = {}; +const generatePrefixedCommand = function(command, key) { + const baseCmd = command.replace(/:prefix$/, ''); + for (let i = 1; i <= 9; i++) { + // 9 is a special number because it means 'last' + const index = i === 9 ? 'last' : i; + commands[`${baseCmd}:${index}`] = normalize(`${key}+${i}`); + } +}; + const _setKeysForCommands = function(keymap) { for (const command in keymap) { if (command) { - commands[command] = normalize(keymap[command]); + // In case of a command finishing by :prefix + // we need to generate commands and keys + if (command.endsWith(':prefix')) { + generatePrefixedCommand(command, keymap[command]); + } else { + commands[command] = normalize(keymap[command]); + } } } }; diff --git a/app/keymaps/darwin.json b/app/keymaps/darwin.json index 64c81bc9..03e7926e 100644 --- a/app/keymaps/darwin.json +++ b/app/keymaps/darwin.json @@ -14,6 +14,7 @@ "tab:new": "cmd+t", "tab:next": "cmd+shift+]", "tab:prev": "cmd+shift+[", + "tab:jump:prefix": "cmd", "pane:next": "cmd+]", "pane:prev": "cmd+[", "pane:splitVertical": "cmd+d", @@ -28,4 +29,4 @@ "editor:clearBuffer": "cmd+k", "editor:emojis": "cmd+ctrl+space", "plugins:update": "cmd+shift+u" -} \ No newline at end of file +} diff --git a/app/keymaps/linux.json b/app/keymaps/linux.json index d66494f6..78de8fce 100644 --- a/app/keymaps/linux.json +++ b/app/keymaps/linux.json @@ -14,6 +14,7 @@ "tab:new": "ctrl+shift+t", "tab:next": "ctrl+tab", "tab:prev": "ctrl+shift+tab", + "tab:jump:prefix": "ctrl", "pane:next": "ctrl+pageup", "pane:prev": "ctrl+pagedown", "pane:splitVertical": "ctrl+shift+d", diff --git a/app/keymaps/win32.json b/app/keymaps/win32.json index ee94f2c5..6aaded2a 100644 --- a/app/keymaps/win32.json +++ b/app/keymaps/win32.json @@ -14,6 +14,7 @@ "tab:new": "ctrl+shift+t", "tab:next": "ctrl+tab", "tab:prev": "ctrl+shift+tab", + "tab:jump:prefix": "ctrl", "pane:next": "ctrl+pageup", "pane:prev": "ctrl+pagedown", "pane:splitVertical": "ctrl+shift+d", diff --git a/app/menus/menus/window.js b/app/menus/menus/window.js index 69c4ef59..262dd5f3 100644 --- a/app/menus/menus/window.js +++ b/app/menus/menus/window.js @@ -1,4 +1,21 @@ module.exports = commands => { + // Generating tab:jump array + const tabJump = []; + for (let i = 1; i <= 9; i++) { + // 9 is a special number because it means 'last' + const label = i === 9 ? 'Last' : `${i}`; + const tabIndex = i === 9 ? 'last' : i - 1; + tabJump.push({ + label: label, + accelerator: commands[`tab:jump:${label.toLowerCase()}`], + click(item, focusedWindow) { + if (focusedWindow) { + focusedWindow.rpc.emit('move jump req', tabIndex); + } + } + }); + } + return { role: 'window', submenu: [ @@ -34,7 +51,11 @@ module.exports = commands => { focusedWindow.rpc.emit('move right req'); } } - } + }, + { + type: 'separator' + }, + ...tabJump ] }, { diff --git a/lib/actions/ui.js b/lib/actions/ui.js index 42e7a410..bf450278 100644 --- a/lib/actions/ui.js +++ b/lib/actions/ui.js @@ -180,6 +180,14 @@ export function moveRight() { export function moveTo(i) { return (dispatch, getState) => { + if (i === 'last') { + // Finding last tab index + const {termGroups} = getState().termGroups; + i = + Object.keys(termGroups) + .map(uid => termGroups[uid]) + .filter(({parentUid}) => !parentUid).length - 1; + } dispatch({ type: UI_MOVE_TO, index: i, diff --git a/lib/index.js b/lib/index.js index 63e81c63..652f36ac 100644 --- a/lib/index.js +++ b/lib/index.js @@ -99,6 +99,10 @@ rpc.on('move right req', () => { store_.dispatch(uiActions.moveRight()); }); +rpc.on('move jump req', index => { + store_.dispatch(uiActions.moveTo(index)); +}); + rpc.on('next pane req', () => { store_.dispatch(uiActions.moveToNextPane()); });