Skip to content

Commit

Permalink
Merge pull request #87 from janrito/tab-gropus
Browse files Browse the repository at this point in the history
Tab groups & drag decayed tabs
  • Loading branch information
janrito authored Dec 6, 2022
2 parents 8c3267b + e8aca49 commit c60cdad
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 94 deletions.
60 changes: 30 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@
"postcss-url": "^10.1.3",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.8.1",
"stylelint": "^14.15.0",
"stylelint": "^14.16.0",
"stylelint-config-prettier": "^9.0.4",
"stylelint-config-standard": "^29.0.0",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-prettier": "^2.0.0",
"stylelint-processor-styled-components": "^1.10.0",
"svelte": "^3.53.1",
"svelte-check": "^2.10.0",
"svelte-check": "^2.10.1",
"svelte-dnd-action": "^0.9.22",
"svelte-preprocess": "^4.10.7",
"tailwindcss": "^3.2.4",
Expand Down
53 changes: 39 additions & 14 deletions source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
closeTab,
compileValidURLPatterns,
findURLPattern,
lifetimeIdToTabId,
sampleLifetime,
tabIdToLifetimeId,
} from "./lib/utils.js";
import decayedTabs from "./stores/decayed-tabs";
import settings from "./stores/settings";
Expand All @@ -20,8 +22,9 @@ const decayTab = debounce(tabId => {
decayedTabs.add(tab);
closeTab(tabId);
tabLifetimes.update(currentTabLifetimes => {
if (currentTabLifetimes[tabId]) {
delete currentTabLifetimes[tabId];
const lifetimeId = tabIdToLifetimeId(tabId);
if (currentTabLifetimes[lifetimeId]) {
delete currentTabLifetimes[lifetimeId];
}
return currentTabLifetimes;
});
Expand All @@ -31,10 +34,16 @@ const decayTab = debounce(tabId => {
const clearTabLifetimes = (tabIds, currentTabLifetimes) => {
const tabIdsSet = new Set(tabIds);
tabIdsSet.forEach(tabId => {
const { timerId } = currentTabLifetimes[tabId];
const lifetimeId = tabIdToLifetimeId(tabId);
const { timerId } = currentTabLifetimes[lifetimeId];
clearTimeout(timerId);
});
return Object.fromEntries(Object.entries(currentTabLifetimes).filter(key => !tabIdsSet.has(key)));
return Object.fromEntries(
Object.entries(currentTabLifetimes).filter(pair => {
const [lifetimeId] = pair;
return !tabIdsSet.has(lifetimeIdToTabId(lifetimeId));
})
);
};

const setNewTabLifetime = (tab, currentTabLifetimes, { tabDecayExceptions, tabDecayHalfLife }) => {
Expand All @@ -43,17 +52,23 @@ const setNewTabLifetime = (tab, currentTabLifetimes, { tabDecayExceptions, tabDe
if (tab.pinned || matchingExceptionPattern) {
return currentTabLifetimes;
}
const { lifetime } = currentTabLifetimes[tab.id] || {
const lifetimeId = tabIdToLifetimeId(tab.id);
const { timerId, lifetime } = currentTabLifetimes[lifetimeId] || {
lifetime: sampleLifetime(tabDecayHalfLife),
};
const delay = calculateDelay(lifetime, tab.lastAccessed);

if (delay > MAX_DELAY_TO_SCHEDULE) {
// only set the decay timer on tabs that are likely to decay soon.
currentTabLifetimes[lifetimeId] = { timerId: undefined, lifetime };
return currentTabLifetimes;
}
const timerId = setTimeout(decayTab, delay, tab.id);
currentTabLifetimes[tab.id] = { timerId, lifetime };
if (timerId) {
// clearing current timer is already set
clearTimeout(timerId);
}
const newTimerId = setTimeout(decayTab, delay, tab.id);
currentTabLifetimes[lifetimeId] = { timerId: newTimerId, lifetime };
return currentTabLifetimes;
};

Expand All @@ -67,21 +82,28 @@ const updateTabLifetimes = debounce(

let currentTabLifetimes = get(tabLifetimes);

if (forceOnAll) {
currentTabLifetimes = clearTabLifetimes(
Object.keys(currentTabLifetimes).map(lifetimeId => lifetimeIdToTabId(lifetimeId)),
currentTabLifetimes
);
}

browser.tabs.query(TAB_QUERY).then(tabs => {
const tabIds = new Set([]);
tabs.map(tab => {
// keep track of all existing tabs
tabIds.add(tab.id);
const lifetimeId = tabIdToLifetimeId(tab.id);

const isSet = currentTabLifetimes[tab.id] !== undefined;
const isSet = currentTabLifetimes[lifetimeId] !== undefined;

if (isSet && tab.active) {
// If active on window, clear it and return
currentTabLifetimes = clearTabLifetimes([tab.id], currentTabLifetimes);
return;
}

if (isSet && (forceOnSet.has(tab.id) || forceOnAll)) {
if (isSet && forceOnSet.has(tab.id)) {
// clear all tabs in force list
currentTabLifetimes = clearTabLifetimes([tab.id], currentTabLifetimes);
}
Expand All @@ -98,10 +120,13 @@ const updateTabLifetimes = debounce(

// clear lifetimes for tabs that don't exist any more
// – for example those closed manually
currentTabLifetimes = clearTabLifetimes(
Object.keys(currentTabLifetimes).filter(tabId => !tabIds.has(Number(tabId))),
currentTabLifetimes
);
const nonExistentTabIds = Object.keys(currentTabLifetimes)
.map(lifetimeId => lifetimeIdToTabId(lifetimeId))
.filter(tabId => !tabIds.has(tabId));

if (nonExistentTabIds.length) {
currentTabLifetimes = clearTabLifetimes(nonExistentTabIds, currentTabLifetimes);
}

tabLifetimes.update(() => currentTabLifetimes);
});
Expand Down
1 change: 0 additions & 1 deletion source/components/Card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</style>

<script>
import browser from "webextension-polyfill";
import delay from "lodash/delay";
import { createEventDispatcher } from "svelte";
Expand Down
Loading

0 comments on commit c60cdad

Please sign in to comment.