Skip to content

Commit

Permalink
feat: switch tabs using alt-#num (#1975)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHeer committed Jun 6, 2020
1 parent de709ee commit 17e2860
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 7 deletions.
24 changes: 17 additions & 7 deletions docs/KEYBINDINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,20 @@ Here is an example:

**Misc**

| Id | Default | Description |
| ---------------------- | ---------------------------------------------------- | ---------------------------- |
| `tabs.cycle-forward` | <kbd>CmdOrCtrl</kbd>+<kbd>Tab</kbd> | Cycle through tabs |
| `tabs.cycle-backward` | <kbd>CmdOrCtrl</kbd>+<kbd>Shift</kbd>+<kbd>Tab</kbd> | Cycle backwards through tabs |
| `tabs.switch-to-left` | <kbd>CmdOrCtrl</kbd>+<kbd>PageUp</kbd> | Switch tab to the left |
| `tabs.switch-to-right` | <kbd>CmdOrCtrl</kbd>+<kbd>PageDown</kbd> | Switch tab to the right |
| `file.quick-open` | <kbd>CmdOrCtrl</kbd>+<kbd>P</kbd> | Open quick open dialog |
| Id | Default | Description |
| ------------------------ | ---------------------------------------------------- | ---------------------------- |
| `tabs.cycle-forward` | <kbd>CmdOrCtrl</kbd>+<kbd>Tab</kbd> | Cycle through tabs |
| `tabs.cycle-backward` | <kbd>CmdOrCtrl</kbd>+<kbd>Shift</kbd>+<kbd>Tab</kbd> | Cycle backwards through tabs |
| `tabs.switch-to-left` | <kbd>CmdOrCtrl</kbd>+<kbd>PageUp</kbd> | Switch tab to the left |
| `tabs.switch-to-right` | <kbd>CmdOrCtrl</kbd>+<kbd>PageDown</kbd> | Switch tab to the right |
| `tabs.switch-to-first` | <kbd>Alt</kbd>+<kbd>1</kbd> | Switch tab to the 1st |
| `tabs.switch-to-second` | <kbd>Alt</kbd>+<kbd>2</kbd> | Switch tab to the 2nd |
| `tabs.switch-to-third` | <kbd>Alt</kbd>+<kbd>3</kbd> | Switch tab to the 3rd |
| `tabs.switch-to-fourth` | <kbd>Alt</kbd>+<kbd>4</kbd> | Switch tab to the 4th |
| `tabs.switch-to-fifth` | <kbd>Alt</kbd>+<kbd>5</kbd> | Switch tab to the 5th |
| `tabs.switch-to-sixth` | <kbd>Alt</kbd>+<kbd>6</kbd> | Switch tab to the 6th |
| `tabs.switch-to-seventh` | <kbd>Alt</kbd>+<kbd>7</kbd> | Switch tab to the 7th |
| `tabs.switch-to-eighth` | <kbd>Alt</kbd>+<kbd>8</kbd> | Switch tab to the 8th |
| `tabs.switch-to-ninth` | <kbd>Alt</kbd>+<kbd>9</kbd> | Switch tab to the 9th |
| `tabs.switch-to-tenth` | <kbd>Alt</kbd>+<kbd>0</kbd> | Switch tab to the 10th |
| `file.quick-open` | <kbd>CmdOrCtrl</kbd>+<kbd>P</kbd> | Open quick open dialog |
10 changes: 10 additions & 0 deletions src/main/keyboard/shortcutHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ class Keybindings {
['tabs.cycle-backward', 'CmdOrCtrl+Shift+Tab'],
['tabs.switch-to-left', 'CmdOrCtrl+PageUp'],
['tabs.switch-to-right', 'CmdOrCtrl+PageDown'],
['tabs.switch-to-first', 'Alt+1'],
['tabs.switch-to-second', 'Alt+2'],
['tabs.switch-to-third', 'Alt+3'],
['tabs.switch-to-fourth', 'Alt+4'],
['tabs.switch-to-fifth', 'Alt+5'],
['tabs.switch-to-sixth', 'Alt+6'],
['tabs.switch-to-seventh', 'Alt+7'],
['tabs.switch-to-eighth', 'Alt+8'],
['tabs.switch-to-ninth', 'Alt+9'],
['tabs.switch-to-tenth', 'Alt+0'],
['file.quick-open', 'CmdOrCtrl+P']
])

Expand Down
70 changes: 70 additions & 0 deletions src/main/menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,76 @@ class AppMenu {
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-first'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-first-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-second'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-second-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-third'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-third-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-fourth'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-fourth-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-fifth'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-fifth-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-sixth'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-sixth-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-seventh'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-seventh-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-eighth'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-eighth-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-ninth'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-ninth-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('tabs.switch-to-tenth'),
click: (menuItem, win) => {
win.webContents.send('mt::switch-tenth-tab')
},
id: null
})
shortcutMap.push({
accelerator: this._keybindings.getAccelerator('file.quick-open'),
click: (menuItem, win) => {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/pages/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export default {
dispatch('LISTEN_FOR_NEW_TAB')
dispatch('LISTEN_FOR_CLOSE_TAB')
dispatch('LISTEN_FOR_TAB_CYCLE')
dispatch('LISTEN_FOR_SWITCH_TABS')
dispatch('LINTEN_FOR_PRINT_SERVICE_CLEARUP')
dispatch('LINTEN_FOR_EXPORT_SUCCESS')
dispatch('LISTEN_FOR_FILE_CHANGE')
Expand Down
56 changes: 56 additions & 0 deletions src/renderer/store/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,39 @@ const actions = {
})
},

LISTEN_FOR_SWITCH_TABS ({ commit, state, dispatch }) {
ipcRenderer.on('mt::switch-first-tab', e => {
dispatch('SWITCH_TABS', 1)
})
ipcRenderer.on('mt::switch-second-tab', e => {
dispatch('SWITCH_TABS', 2)
})
ipcRenderer.on('mt::switch-third-tab', e => {
dispatch('SWITCH_TABS', 3)
})
ipcRenderer.on('mt::switch-fourth-tab', e => {
dispatch('SWITCH_TABS', 4)
})
ipcRenderer.on('mt::switch-fifth-tab', e => {
dispatch('SWITCH_TABS', 5)
})
ipcRenderer.on('mt::switch-sixth-tab', e => {
dispatch('SWITCH_TABS', 6)
})
ipcRenderer.on('mt::switch-seventh-tab', e => {
dispatch('SWITCH_TABS', 7)
})
ipcRenderer.on('mt::switch-eighth-tab', e => {
dispatch('SWITCH_TABS', 8)
})
ipcRenderer.on('mt::switch-ninth-tab', e => {
dispatch('SWITCH_TABS', 9)
})
ipcRenderer.on('mt::switch-tenth-tab', e => {
dispatch('SWITCH_TABS', 10)
})
},

CLOSE_TAB ({ dispatch }, file) {
const { isSaved } = file
if (isSaved) {
Expand Down Expand Up @@ -767,6 +800,29 @@ const actions = {
dispatch('UPDATE_LINE_ENDING_MENU')
},

// switch tabs with Alt+#num.
SWITCH_TABS ({ commit, dispatch, state }, num) {
const { tabs, currentFile } = state
if (tabs.length <= 1 || num > tabs.length) {
return
}

const currentIndex = tabs.findIndex(t => t.id === currentFile.id)
if (currentIndex === -1) {
console.error('CYCLE_TABS: Cannot find current tab index.')
return
}

const nextTabIndex = num - 1
const nextTab = tabs[nextTabIndex]
if (!nextTab || !nextTab.id) {
console.error(`CYCLE_TABS: Cannot find next tab (index="${nextTabIndex}").`)
return
}
commit('SET_CURRENT_FILE', nextTab)
dispatch('UPDATE_LINE_ENDING_MENU')
},

/**
* Create a new untitled tab optional from a markdown string.
*
Expand Down

0 comments on commit 17e2860

Please sign in to comment.