Skip to content

Commit c753eec

Browse files
rootz491iamEvanYT
andauthored
Tab auto sleep feature (#50)
* feat: add tab sleep functionality and update related checks * feat: add sleep tab duration settings and related configuration * refactor: consolidate performance settings into a single card --------- Co-authored-by: iamEvan <47493765+iamEvanYT@users.noreply.github.com>
1 parent 8a8645f commit c753eec

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

src/main/browser/tabs/tab-manager.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SplitTabGroup } from "@/browser/tabs/tab-groups/split";
66
import { windowTabsChanged } from "@/ipc/browser/tabs";
77
import { setWindowSpace } from "@/ipc/session/spaces";
88
import { TypedEventEmitter } from "@/modules/typed-event-emitter";
9-
import { shouldArchiveTab } from "@/saving/tabs";
9+
import { shouldArchiveTab, shouldSleepTab } from "@/saving/tabs";
1010
import { getLastUsedSpace, getLastUsedSpaceFromProfile } from "@/sessions/spaces";
1111
import { WebContents } from "electron";
1212
import { TabGroupMode } from "~/types/tabs";
@@ -83,6 +83,9 @@ export class TabManager extends TypedEventEmitter<TabManagerEvents> {
8383
if (!tab.visible && shouldArchiveTab(tab.lastActiveAt)) {
8484
tab.destroy();
8585
}
86+
if (!tab.visible && !tab.asleep && shouldSleepTab(tab.lastActiveAt)) {
87+
tab.putToSleep();
88+
}
8689
}
8790
}, ARCHIVE_CHECK_INTERVAL_MS);
8891

src/main/modules/basic-settings.ts

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ export const ArchiveTabValueMap = {
1515
never: Infinity
1616
};
1717

18+
/**
19+
* Maps sleep tab duration settings to their equivalent values in seconds.
20+
* 'never' is mapped to Infinity.
21+
*/
22+
export const SleepTabValueMap = {
23+
"5m": 5 * 60,
24+
"10m": 10 * 60,
25+
"30m": 30 * 60,
26+
"1h": 60 * 60,
27+
"2h": 2 * 60 * 60,
28+
"4h": 4 * 60 * 60,
29+
"8h": 8 * 60 * 60,
30+
"12h": 12 * 60 * 60,
31+
"24h": 24 * 60 * 60,
32+
never: Infinity
33+
};
34+
1835
export const BasicSettings: BasicSetting[] = [
1936
// [GENERAL] Auto Update
2037
{
@@ -140,6 +157,57 @@ export const BasicSettings: BasicSetting[] = [
140157
]
141158
},
142159

160+
// Sleep Tab After
161+
{
162+
id: "sleepTabAfter",
163+
name: "Sleep Tab After",
164+
showName: false,
165+
type: "enum",
166+
defaultValue: "never",
167+
options: [
168+
{
169+
id: "5m",
170+
name: "5 Minutes"
171+
},
172+
{
173+
id: "10m",
174+
name: "10 Minutes"
175+
},
176+
{
177+
id: "30m",
178+
name: "30 Minutes"
179+
},
180+
{
181+
id: "1h",
182+
name: "1 Hour"
183+
},
184+
{
185+
id: "2h",
186+
name: "2 Hours"
187+
},
188+
{
189+
id: "4h",
190+
name: "4 Hours"
191+
},
192+
{
193+
id: "8h",
194+
name: "8 Hours"
195+
},
196+
{
197+
id: "12h",
198+
name: "12 Hours"
199+
},
200+
{
201+
id: "24h",
202+
name: "24 Hours"
203+
},
204+
{
205+
id: "never",
206+
name: "Never"
207+
}
208+
]
209+
},
210+
143211
// [ADVANCED] Enable mv2 extensions
144212
{
145213
id: "enableMv2Extensions",
@@ -179,11 +247,11 @@ export const BasicSettingCards: BasicSettingCard[] = [
179247
settings: ["sidebarSide", "sidebarCollapseMode"]
180248
},
181249

182-
// Archive Tab After Card
250+
// Performance Settings Card
183251
{
184-
title: "Archive Tab After",
185-
subtitle: "Choose how long tabs should be archived",
186-
settings: ["archiveTabAfter"]
252+
title: "Performance Settings",
253+
subtitle: "Settings to improve performance",
254+
settings: ["archiveTabAfter", "sleepTabAfter"]
187255
},
188256

189257
// Onboarding Card (Internal)

src/main/saving/tabs.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Browser } from "@/browser/browser";
22
import { SLEEP_MODE_URL, Tab } from "@/browser/tabs/tab";
33
import { browser } from "@/index";
44
import { getTabData } from "@/ipc/browser/tabs";
5-
import { ArchiveTabValueMap } from "@/modules/basic-settings";
5+
import { ArchiveTabValueMap, SleepTabValueMap } from "@/modules/basic-settings";
66
import { getDatastore } from "@/saving/datastore";
77
import { getSettingValueById } from "@/saving/settings";
88
import { app } from "electron";
@@ -61,6 +61,17 @@ export function shouldArchiveTab(lastActiveAt: number) {
6161
return diff > archiveTabAfterSeconds;
6262
}
6363

64+
export function shouldSleepTab(lastActiveAt: number) {
65+
const sleepTabAfter = getSettingValueById("sleepTabAfter");
66+
const sleepTabAfterSeconds = SleepTabValueMap[sleepTabAfter as keyof typeof SleepTabValueMap];
67+
68+
if (typeof sleepTabAfterSeconds !== "number") return false;
69+
70+
const now = Math.floor(Date.now() / 1000);
71+
const diff = now - lastActiveAt;
72+
return diff > sleepTabAfterSeconds;
73+
}
74+
6475
export async function loadTabsFromStorage() {
6576
const tabs: { [uniqueId: string]: TabData } = await TabsDataStore.getFullData();
6677

0 commit comments

Comments
 (0)