Skip to content

Commit 530b08f

Browse files
committed
menu: add getTabByOrder.
1 parent fc21dce commit 530b08f

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

app/main/menu.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -701,24 +701,28 @@ async function checkForUpdate(): Promise<void> {
701701
await appUpdater(true);
702702
}
703703

704+
function getTabByOrder(tabs: TabData[], order: number): TabData | undefined {
705+
return tabs.find((tab) => tab.index === order);
706+
}
707+
704708
function getNextServer(tabs: TabData[], activeTabId: string): string {
705709
const activeTab = tabs.find((tab) => tab.id === activeTabId)!;
706710
let {index} = activeTab;
707711
do {
708712
index = (index + 1) % tabs.length;
709-
} while (tabs[index]?.role !== "server");
713+
} while (getTabByOrder(tabs, index)?.role !== "server");
710714

711-
return tabs[index].id;
715+
return getTabByOrder(tabs, index)!.id;
712716
}
713717

714718
function getPreviousServer(tabs: TabData[], activeTabId: string): string {
715719
const activeTab = tabs.find((tab) => tab.id === activeTabId)!;
716720
let {index} = activeTab;
717721
do {
718-
index = (index - 1 + tabs.length) % tabs.length;
719-
} while (tabs[index]?.role !== "server");
722+
index = (index - 1) % tabs.length;
723+
} while (getTabByOrder(tabs, index)?.role !== "server");
720724

721-
return tabs[index].id;
725+
return getTabByOrder(tabs, index)!.id;
722726
}
723727

724728
export function setMenu(properties: MenuProperties): void {

app/renderer/js/main.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ export class ServerManagerView {
333333
}
334334

335335
// Open last active tab
336-
const firstTab = this.tabs[0];
336+
const firstTab = this.getTabByOrder(this.tabs, 0)!;
337337
const lastActiveTabId = ConfigUtil.getConfigItem(
338338
"lastActiveTabId",
339339
firstTab.properties.tabId,
@@ -346,14 +346,14 @@ export class ServerManagerView {
346346
// `webview.load()` for lastActiveTabId before the others
347347
await this.activateTab(lastActiveTabId);
348348
await Promise.all(
349-
servers.map(async (server, i) => {
349+
servers.map(async (server) => {
350350
// After the lastActiveTabId is activated, we load the others in the background
351351
// without activating them, to prevent flashing of server icons
352352
if (server.id === lastActiveTabId) {
353353
return;
354354
}
355355

356-
const tab = this.tabs[i];
356+
const tab = this.getTabById(server.id);
357357
if (tab instanceof ServerTab) (await tab.webview).load();
358358
}),
359359
);
@@ -657,6 +657,13 @@ export class ServerManagerView {
657657
}));
658658
}
659659

660+
getTabByOrder(
661+
tabs: ServerOrFunctionalTab[],
662+
order: number,
663+
): ServerOrFunctionalTab | undefined {
664+
return tabs.find((tab) => tab.properties.index === order);
665+
}
666+
660667
async activateTab(id: string, hideOldTab = true): Promise<void> {
661668
const tab = this.getTabById(id);
662669
if (!tab) {
@@ -729,7 +736,10 @@ export class ServerManagerView {
729736

730737
// Issue #188: If the functional tab was not focused, do not activate another tab.
731738
if (this.activeTabId === tabId) {
732-
await this.activateTab(this.tabs[0].properties.tabId, false);
739+
await this.activateTab(
740+
this.getTabByOrder(this.tabs, 0)!.properties.tabId,
741+
false,
742+
);
733743
}
734744
}
735745

0 commit comments

Comments
 (0)