Skip to content

Commit 0074268

Browse files
authored
Code cleanup (#161)
1 parent a839aed commit 0074268

File tree

5 files changed

+60
-27
lines changed

5 files changed

+60
-27
lines changed

source/extension.svelte

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
66
import openInTab from './lib/open-in-tab.js';
77
import trimName from './lib/trim-name.js';
8+
import pickBestIcon from './libs/icons.js';
89
910
export let id;
1011
export let name;
1112
export let shortName;
1213
export let enabled;
1314
export let installType;
1415
export let homepageUrl;
15-
export let updateUrl; // Optional
16+
export let updateUrl = undefined; // eslint-disable-line no-undef-init -- Optional svelte property
1617
export let optionsUrl;
17-
export let icons; // Optional
18+
export let icons = undefined; // eslint-disable-line no-undef-init -- Optional svelte property
1819
export let showExtras;
1920
export let undoStack;
2021
@@ -46,22 +47,6 @@
4647
function onUninstallClick() {
4748
chrome.management.uninstall(id);
4849
}
49-
50-
function getIcon(icons, size = 16) {
51-
// Get retina size if necessary
52-
size *= window.devicePixelRatio;
53-
54-
if (icons) {
55-
// Get a large icon closest to the desired size
56-
for (const icon of icons.toReversed()) {
57-
if (icon.size >= size) {
58-
return icon.url;
59-
}
60-
}
61-
}
62-
// Fallback icon
63-
return 'icons/puzzle.svg';
64-
}
6550
</script>
6651
6752
<li class:disabled={!enabled} class="ext type-{installType}">
@@ -71,7 +56,7 @@
7156
on:click={toggleExtension}
7257
on:contextmenu
7358
>
74-
<img alt="" src={getIcon(icons, 16)} />{realName}
59+
<img alt="" src={pickBestIcon(icons, 16)} />{realName}
7560
</button>
7661
{#if optionsUrl && enabled}
7762
<a href={optionsUrl} title={getI18N('gotoOpt')} on:click={openInTab}>

source/lib/single-window.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function preventMultipleWindows() {
2+
chrome.runtime.sendMessage('thisTownIsTooSmallForTheTwoOfUs').catch(() => {
3+
// No other windows open, good!
4+
});
5+
chrome.runtime.onMessage.addListener(message => {
6+
if (message === 'thisTownIsTooSmallForTheTwoOfUs') {
7+
window.close();
8+
}
9+
});
10+
}

source/libs/icons.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default function pickBestIcon(icons, size = 16) {
2+
// Get retina size if necessary
3+
size *= globalThis.devicePixelRatio;
4+
5+
if (!icons?.length) {
6+
return 'icons/puzzle.svg';
7+
}
8+
9+
const smallestToLargest = icons.toSorted((a, b) => a.size - b.size);
10+
11+
// Find the smallest icon that is larger than the requested size
12+
for (const icon of smallestToLargest) {
13+
if (icon.size >= size) {
14+
return icon.url;
15+
}
16+
}
17+
18+
// If it's not available (e.g. requested 32, available only 16), get the largest one
19+
return smallestToLargest.at(-1).url;
20+
}

source/libs/icons.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import assert from 'node:assert';
2+
import test from 'node:test';
3+
import pickBestIcon from './icons.js';
4+
5+
const manifest = [
6+
{url: '16.svg', size: 16},
7+
{url: '32.svg', size: 32},
8+
];
9+
10+
test('pickBestIcon', () => {
11+
globalThis.devicePixelRatio = 1;
12+
assert.equal(pickBestIcon(), 'icons/puzzle.svg');
13+
assert.equal(pickBestIcon(undefined), 'icons/puzzle.svg');
14+
assert.equal(pickBestIcon([]), 'icons/puzzle.svg');
15+
assert.equal(pickBestIcon(manifest), '16.svg');
16+
assert.equal(pickBestIcon(manifest, 16), '16.svg');
17+
assert.equal(pickBestIcon(manifest, 1), '16.svg');
18+
assert.equal(pickBestIcon(manifest, 32), '32.svg');
19+
assert.equal(pickBestIcon(manifest, 48), '32.svg');
20+
21+
globalThis.devicePixelRatio = 2;
22+
assert.equal(pickBestIcon(manifest, 16), '32.svg');
23+
assert.equal(pickBestIcon(manifest, 32), '32.svg');
24+
});

source/main.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import App from './app.svelte';
22
import fitWindow from './lib/fit-window.js';
3+
import preventMultipleWindows from './lib/single-window.js';
34

45
// eslint-disable-next-line no-new -- Tell this to Svelte
56
new App({
@@ -11,11 +12,4 @@ if (autoFit) {
1112
fitWindow();
1213
}
1314

14-
chrome.runtime.sendMessage('thisTownIsTooSmallForTheTwoOfUs').catch(() => {
15-
// No other windows open, good!
16-
});
17-
chrome.runtime.onMessage.addListener(message => {
18-
if (message === 'thisTownIsTooSmallForTheTwoOfUs') {
19-
window.close();
20-
}
21-
});
15+
preventMultipleWindows();

0 commit comments

Comments
 (0)