Implement default keyboard shortcuts for switching repository Tabs (esp. Ctrl+(Shift)+Tab)
Created originally on Bitbucket by thomasdd (Thomas D.)
In the entire tabbed IT universe, Ctrl+Tab / Ctrl+Shift+Tab can be used to switch between the last activated tabs. Browers like Firefox, MDI-Editors like Notepad++, Windows itself with Alt+Tab / Alt+Shift+Tab, you name it: It just works EVERYWHERE. But sadly, NOT in THG...
I have never discovered Ctrl+1, Ctrl+2 etc. for Tab switching in THG until I found #3543 (closed) today. Unfortunately, having those keyboard shortcuts on file menu does not make them discoverable enough. Worse, they are not very practical. Anyone having more than 9 repositories can't use them to switch between any tabs beyond tab 9. And depending on workflows, arrangement of tabs may change in between, which spoils the muscle memory. Before I figure out if that tab is tab 7 or tab 8 so that I can press Ctrl+7 or 8 respectively, it's faster to just click on it anyway. But for just switching between 7 and 8 which I happen to use today, and 9 and 10 tomorrow, just using Ctrl+Tab is perfectly memorable as a no-brainer, every time.
In #3543 (closed), @yuja objected:
But "ordered by last activation" will increase the complexity of the big Workbench class. It'll be necessary to factor out tab-related functions first.
I think that's not true. "Ordered by last activation" is quite simple, just keep a single array with tab indices, ordered by tab activation, last activated first. Here's a rough sketch how (pseudo code leaning on javascript syntax; I don't know Python):
let globalTabSwitchSeqArray = [];
function onTabFocusEventHandler() {
let currentTabIndex = tabsObject.getCurrentTabIndex(); // Index: 1st tab = 0 etc.
if (!globalTabSwitchSeqArray[currentTabIndex]) {
// add new tab id to start of tab switch array
globalTabSwitchSeqArray.unshift(currentTabIndex); // when opening e.g. 3rd tab: [2, 1, 0]
} else {
// existing tab focused, e.g. Tab 2, move it to begin of tab-switch-array
array_move(globalTabSwitchSeqArray, globalTabSwitchSeqArray.indexOf(currentTabIndex), 0);
// new tab switch order: [1,2,0]
}
}
function workbenchOnKeyPress (event) {
if (event.key == "Tab" && event.CtrlKey) {
if (event.ShiftKey) {
// backwards in activation sequence: activate last tab in the sequence
targetIndex = globalTabSwitchSeqArray.length-1;
switchToTab(globalTabSwitchSeqArray[targetIndex]);
} else {
// forward in activation sequence: activate 2nd tab in the sequence
switchToTab(globalTabSwitchSeqArray[1]);
}
}
}
function array_move(arr, old_index, new_index) {
if (new_index >= arr.length) {
var k = new_index - arr.length + 1;
while (k--) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr; // for testing
};
I haven't tested this in any way, but hopefully it's enough to get the idea.