Skip to content

Commit e703754

Browse files
committed
menu: add getTabByOrder.
1 parent d325b10 commit e703754

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,22 +701,26 @@ 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[], activeTab: TabData): string {
705709
let {index} = activeTab;
706710
do {
707711
index = (index + 1) % tabs.length;
708-
} while (tabs[index]?.role !== "server");
712+
} while (getTabByOrder(tabs, index)?.role !== "server");
709713

710-
return tabs[index].id;
714+
return getTabByOrder(tabs, index)!.id;
711715
}
712716

713717
function getPreviousServer(tabs: TabData[], activeTab: TabData): string {
714718
let {index} = activeTab;
715719
do {
716-
index = (index - 1 + tabs.length) % tabs.length;
717-
} while (tabs[index]?.role !== "server");
720+
index = (index - 1) % tabs.length;
721+
} while (getTabByOrder(tabs, index)?.role !== "server");
718722

719-
return tabs[index].id;
723+
return getTabByOrder(tabs, index)!.id;
720724
}
721725

722726
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
);
@@ -660,6 +660,13 @@ export class ServerManagerView {
660660
return this.tabs.map((tab) => this.tabForIpc(tab));
661661
}
662662

663+
getTabByOrder(
664+
tabs: ServerOrFunctionalTab[],
665+
order: number,
666+
): ServerOrFunctionalTab | undefined {
667+
return tabs.find((tab) => tab.properties.index === order);
668+
}
669+
663670
async activateTab(id: string, hideOldTab = true): Promise<void> {
664671
const tab = this.getTabById(id);
665672
if (!tab) {
@@ -737,7 +744,10 @@ export class ServerManagerView {
737744
// Issue #188: If the functional tab was not focused, do not activate another tab.
738745
if (wasActive) {
739746
this.activeTab = undefined;
740-
await this.activateTab(this.tabs[0].properties.tabId, false);
747+
await this.activateTab(
748+
this.getTabByOrder(this.tabs, 0)!.properties.tabId,
749+
false,
750+
);
741751
}
742752
}
743753

0 commit comments

Comments
 (0)