@ponderdev/justfits 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -10
- package/THIRD-PARTY-NOTICES.md +21 -10
- package/agents/skills/justfits/SKILL.md +102 -0
- package/package.json +19 -10
- package/scripts/link-skill.cjs +99 -0
- package/src/binaries.cjs +26 -10
- package/src/cli.cjs +216 -12
- package/src/engine.cjs +105 -17
- package/src/heif.cjs +156 -0
- package/src/kit/scan.cjs +105 -0
- package/src/kit/window.cjs +93 -0
- package/src/main.cjs +86 -10
- package/src/plan.js +11 -0
- package/src/preload.cjs +14 -1
- package/src/renderer/app.js +483 -74
- package/src/renderer/index.html +35 -59
- package/src/renderer/kit/kit.css +767 -0
- package/src/renderer/kit/kit.js +740 -0
- package/src/renderer/licenses.html +44 -111
- package/src/renderer/styles.css +6 -256
- package/src/scan.cjs +43 -0
package/src/heif.cjs
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* The turn a phone HEIC carries only in its EXIF.
|
|
5
|
+
*
|
|
6
|
+
* Most phones store a portrait photo as a landscape picture with a rotation
|
|
7
|
+
* box (irot) that ffmpeg applies by itself. Some (an iPhone 7, for one) write
|
|
8
|
+
* no box and only set the Orientation tag in the EXIF item, which ffmpeg
|
|
9
|
+
* ignores for HEIC, so the photo came out on its side. This reads that tag
|
|
10
|
+
* straight from the file's boxes. It answers 1 (no turn) whenever the file has
|
|
11
|
+
* a rotation or mirror box, because ffmpeg has already applied that one and
|
|
12
|
+
* turning again would put the picture on its side the other way.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const fs = require('node:fs');
|
|
16
|
+
|
|
17
|
+
const u16 = (b, o, le) => (le ? b[o] | (b[o + 1] << 8) : (b[o] << 8) | b[o + 1]);
|
|
18
|
+
const u32 = (b, o, le) => (le
|
|
19
|
+
? b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)
|
|
20
|
+
: (b[o] << 24) | (b[o + 1] << 16) | (b[o + 2] << 8) | b[o + 3]) >>> 0;
|
|
21
|
+
|
|
22
|
+
function* boxes(b, start, end) {
|
|
23
|
+
let p = start;
|
|
24
|
+
while (p + 8 <= end) {
|
|
25
|
+
let size = u32(b, p, false);
|
|
26
|
+
const type = b.toString('latin1', p + 4, p + 8);
|
|
27
|
+
let header = 8;
|
|
28
|
+
if (size === 1) {
|
|
29
|
+
if (p + 16 > end) return;
|
|
30
|
+
size = u32(b, p + 12, false);
|
|
31
|
+
header = 16;
|
|
32
|
+
} else if (size === 0) {
|
|
33
|
+
size = end - p;
|
|
34
|
+
}
|
|
35
|
+
if (size < header || p + size > end) return;
|
|
36
|
+
yield { type, body: p + header, end: p + size };
|
|
37
|
+
p += size;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function findBox(b, start, end, names) {
|
|
42
|
+
const [head, ...rest] = names;
|
|
43
|
+
for (const box of boxes(b, start, end)) {
|
|
44
|
+
if (box.type !== head) continue;
|
|
45
|
+
const body = head === 'meta' ? box.body + 4 : box.body; // meta is a FullBox
|
|
46
|
+
return rest.length ? findBox(b, body, box.end, rest) : { ...box, body };
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function exifItemId(b, iinf) {
|
|
52
|
+
const version = b[iinf.body];
|
|
53
|
+
let p = iinf.body + 4;
|
|
54
|
+
p += version === 0 ? 2 : 4;
|
|
55
|
+
for (const box of boxes(b, p, iinf.end)) {
|
|
56
|
+
if (box.type !== 'infe') continue;
|
|
57
|
+
const v = b[box.body];
|
|
58
|
+
if (v < 2) continue;
|
|
59
|
+
let q = box.body + 4;
|
|
60
|
+
let id;
|
|
61
|
+
if (v === 2) { id = u16(b, q, false); q += 2; } else { id = u32(b, q, false); q += 4; }
|
|
62
|
+
q += 2;
|
|
63
|
+
if (b.toString('latin1', q, q + 4) === 'Exif') return id;
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function itemRange(b, iloc, wanted) {
|
|
69
|
+
const version = b[iloc.body];
|
|
70
|
+
let p = iloc.body + 4;
|
|
71
|
+
const offsetSize = b[p] >> 4;
|
|
72
|
+
const lengthSize = b[p] & 0x0f;
|
|
73
|
+
const baseSize = b[p + 1] >> 4;
|
|
74
|
+
const indexSize = b[p + 1] & 0x0f;
|
|
75
|
+
p += 2;
|
|
76
|
+
let count;
|
|
77
|
+
if (version < 2) { count = u16(b, p, false); p += 2; } else { count = u32(b, p, false); p += 4; }
|
|
78
|
+
const sized = (at, size) => (size === 4 ? u32(b, at, false) : size === 8 ? u32(b, at + 4, false) : size === 2 ? u16(b, at, false) : 0);
|
|
79
|
+
|
|
80
|
+
for (let i = 0; i < count; i++) {
|
|
81
|
+
let id;
|
|
82
|
+
if (version < 2) { id = u16(b, p, false); p += 2; } else { id = u32(b, p, false); p += 4; }
|
|
83
|
+
if (version === 1 || version === 2) p += 2;
|
|
84
|
+
p += 2;
|
|
85
|
+
const base = sized(p, baseSize); p += baseSize;
|
|
86
|
+
const extents = u16(b, p, false); p += 2;
|
|
87
|
+
for (let e = 0; e < extents; e++) {
|
|
88
|
+
if ((version === 1 || version === 2) && indexSize) p += indexSize;
|
|
89
|
+
const off = sized(p, offsetSize); p += offsetSize;
|
|
90
|
+
const len = sized(p, lengthSize); p += lengthSize;
|
|
91
|
+
if (id === wanted) return { offset: base + off, length: len };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function tiffOrientation(t) {
|
|
98
|
+
if (t.length < 8) return 1;
|
|
99
|
+
const le = u16(t, 0, false) === 0x4949;
|
|
100
|
+
const ifd = u32(t, 4, le);
|
|
101
|
+
if (ifd + 2 > t.length) return 1;
|
|
102
|
+
const count = u16(t, ifd, le);
|
|
103
|
+
for (let i = 0; i < count; i++) {
|
|
104
|
+
const e = ifd + 2 + i * 12;
|
|
105
|
+
if (e + 12 > t.length) break;
|
|
106
|
+
if (u16(t, e, le) === 0x0112) {
|
|
107
|
+
const v = u16(t, e + 8, le);
|
|
108
|
+
return v >= 1 && v <= 8 ? v : 1;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return 1;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The EXIF orientation (1..8) a HEIC file leaves for the viewer to apply; 1 when none, or when a box already carries the turn. */
|
|
115
|
+
function heifOrientation(file) {
|
|
116
|
+
try {
|
|
117
|
+
const fd = fs.openSync(file, 'r');
|
|
118
|
+
let head;
|
|
119
|
+
try {
|
|
120
|
+
head = Buffer.alloc(12);
|
|
121
|
+
fs.readSync(fd, head, 0, 12, 0);
|
|
122
|
+
// Only an ISO base media file with a heif-family brand; anything else is none of our business.
|
|
123
|
+
if (head.toString('latin1', 4, 8) !== 'ftyp' || !/^(hei|hev|mif|msf)/.test(head.toString('latin1', 8, 12))) return 1;
|
|
124
|
+
} finally { fs.closeSync(fd); }
|
|
125
|
+
const b = fs.readFileSync(file);
|
|
126
|
+
const ipco = findBox(b, 0, b.length, ['meta', 'iprp', 'ipco']);
|
|
127
|
+
if (ipco) {
|
|
128
|
+
for (const box of boxes(b, ipco.body, ipco.end)) if (box.type === 'irot' || box.type === 'imir') return 1;
|
|
129
|
+
}
|
|
130
|
+
const iinf = findBox(b, 0, b.length, ['meta', 'iinf']);
|
|
131
|
+
const iloc = findBox(b, 0, b.length, ['meta', 'iloc']);
|
|
132
|
+
if (!iinf || !iloc) return 1;
|
|
133
|
+
const id = exifItemId(b, iinf);
|
|
134
|
+
if (id == null) return 1;
|
|
135
|
+
const loc = itemRange(b, iloc, id);
|
|
136
|
+
if (!loc || loc.offset + loc.length > b.length) return 1;
|
|
137
|
+
const payload = b.subarray(loc.offset, loc.offset + loc.length);
|
|
138
|
+
// The item starts with a 4-byte offset to the TIFF header (usually 6, past "Exif\0\0").
|
|
139
|
+
const at = 4 + (payload.length >= 4 ? u32(payload, 0, false) : 0);
|
|
140
|
+
const t = payload.subarray(at);
|
|
141
|
+
if (t.length >= 4 && ((t[0] === 0x49 && t[1] === 0x49) || (t[0] === 0x4d && t[1] === 0x4d))) return tiffOrientation(t);
|
|
142
|
+
return 1;
|
|
143
|
+
} catch {
|
|
144
|
+
return 1;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The ffmpeg filters that turn pixels upright for an EXIF orientation. */
|
|
149
|
+
function orientationFilters(o) {
|
|
150
|
+
return ({
|
|
151
|
+
2: ['hflip'], 3: ['hflip', 'vflip'], 4: ['vflip'],
|
|
152
|
+
5: ['transpose=0'], 6: ['transpose=1'], 7: ['transpose=3'], 8: ['transpose=2'],
|
|
153
|
+
})[o] || [];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
module.exports = { heifOrientation, orientationFilters };
|
package/src/kit/scan.cjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/* PonderDev kit 1.0.0, built from kit/src. Do not edit: run bin/kit-sync. */
|
|
2
|
+
'use strict';
|
|
3
|
+
/*
|
|
4
|
+
* The family's intake for several files or a folder (decided 2026-11-20:
|
|
5
|
+
* every tool takes a dropped folder, and several files). It runs on the main
|
|
6
|
+
* side, and in a tool's command line, so a folder means the same thing in the
|
|
7
|
+
* window and in a terminal, and in every tool.
|
|
8
|
+
*
|
|
9
|
+
* const { scan } = require('./kit/scan.cjs');
|
|
10
|
+
* const { files, folders, missing } = await scan(paths, {
|
|
11
|
+
* extensions: ['.mp4', '.mov'], // what a tool takes from inside a folder
|
|
12
|
+
* ours: /-fits( \d+)?$/i, // its own results, passed over in a folder
|
|
13
|
+
* anyNamed: true, // a file named on its own is taken whatever its name
|
|
14
|
+
* word: 'fitted', // its results folders ("Holiday (fitted)"), passed over
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* files is [{ path, name, bytes, from }], resolved, each once, in the order a file
|
|
18
|
+
* browser lists them (numbers counted, so "clip 2" comes before "clip 10").
|
|
19
|
+
* folders counts the folders named; missing lists named paths that are not
|
|
20
|
+
* there. Inside a folder, subfolders too, only names with one of the
|
|
21
|
+
* extensions are taken, and hidden files and folders, the AppleDouble files
|
|
22
|
+
* ("._name") a Mac leaves on other disks, and the tool's own results are
|
|
23
|
+
* passed over, so a folder done once and then again is not done on top of
|
|
24
|
+
* itself. A file named on its own is taken when anyNamed is set (the tool's
|
|
25
|
+
* probe then says whether it can read it), or else only with one of the
|
|
26
|
+
* extensions. from is the folder named that the file was found in, or null
|
|
27
|
+
* for a file named on its own. It never throws: a folder it cannot read is
|
|
28
|
+
* passed over.
|
|
29
|
+
*
|
|
30
|
+
* Where results go (decided 2026-11-20): a file named on its own gets its
|
|
31
|
+
* result beside it, as always. A folder's results go into a new folder beside
|
|
32
|
+
* it, named for what the tool did ("Holiday (fitted)", "Holiday (JPEG)"),
|
|
33
|
+
* with the folder's subfolders mirrored inside and the folder itself left
|
|
34
|
+
* untouched. A name that is taken gets " 2", then 3: nothing is written over.
|
|
35
|
+
* A tool may offer "Save next to the originals instead", which puts every
|
|
36
|
+
* result beside its own original.
|
|
37
|
+
*
|
|
38
|
+
* const into = resultsFolder(file.from, 'fitted'); // once per folder, per run
|
|
39
|
+
* const dir = resultDir(file, into); // where this file's result goes
|
|
40
|
+
*/
|
|
41
|
+
const fs = require('node:fs');
|
|
42
|
+
const path = require('node:path');
|
|
43
|
+
|
|
44
|
+
async function scan(paths, options = {}) {
|
|
45
|
+
const extensions = new Set((options.extensions || []).map((e) => e.toLowerCase()));
|
|
46
|
+
const ours = options.ours || null;
|
|
47
|
+
const anyNamed = options.anyNamed !== false;
|
|
48
|
+
const resultsName = options.word ? new RegExp(` \\(${options.word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\)( \\d+)?$`, 'i') : null;
|
|
49
|
+
const found = [];
|
|
50
|
+
const seen = new Set();
|
|
51
|
+
const missing = [];
|
|
52
|
+
let folders = 0;
|
|
53
|
+
|
|
54
|
+
async function walk(target, named, from) {
|
|
55
|
+
let stat;
|
|
56
|
+
try { stat = await fs.promises.stat(target); } catch { if (named) missing.push(target); return; }
|
|
57
|
+
|
|
58
|
+
if (stat.isDirectory()) {
|
|
59
|
+
if (named) folders++;
|
|
60
|
+
else if (resultsName && resultsName.test(path.basename(target))) return;
|
|
61
|
+
let entries;
|
|
62
|
+
try { entries = await fs.promises.readdir(target, { withFileTypes: true }); } catch { return; }
|
|
63
|
+
for (const entry of entries) {
|
|
64
|
+
if (entry.name.startsWith('.')) continue;
|
|
65
|
+
await walk(path.join(target, entry.name), false, named ? path.resolve(target) : from);
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!stat.isFile()) return;
|
|
71
|
+
const ext = path.extname(target).toLowerCase();
|
|
72
|
+
if (!(named && anyNamed) && !extensions.has(ext)) return;
|
|
73
|
+
if (!named && path.basename(target).startsWith('._')) return;
|
|
74
|
+
if (!named && ours && ours.test(path.parse(target).name)) return;
|
|
75
|
+
const key = path.resolve(target);
|
|
76
|
+
if (seen.has(key)) return;
|
|
77
|
+
seen.add(key);
|
|
78
|
+
found.push({ path: key, name: path.basename(target), bytes: stat.size, from: named ? null : from });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for (const p of paths) await walk(p, true, null);
|
|
82
|
+
found.sort((a, b) => a.path.localeCompare(b.path, undefined, { numeric: true }));
|
|
83
|
+
return { files: found, folders, missing };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// A new folder beside the one named, for its results: "Holiday (fitted)", or
|
|
87
|
+
// "Holiday (fitted) 2" when that is taken (by anything: a file, a folder, an
|
|
88
|
+
// earlier run). Nothing is made here; the tool makes it when a result is ready.
|
|
89
|
+
function resultsFolder(folder, word, exists = fs.existsSync) {
|
|
90
|
+
const base = `${path.resolve(folder)} (${word})`;
|
|
91
|
+
let candidate = base;
|
|
92
|
+
for (let n = 2; exists(candidate) && n < 1000; n++) candidate = `${base} ${n}`;
|
|
93
|
+
return candidate;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Where one file's result goes: beside it when it was named on its own or no
|
|
97
|
+
// results folder is given (Save next to the originals), or else at the same
|
|
98
|
+
// place inside the results folder as it sits inside its own folder.
|
|
99
|
+
function resultDir(file, into) {
|
|
100
|
+
if (!file.from || !into) return path.dirname(file.path);
|
|
101
|
+
return path.join(into, path.relative(file.from, path.dirname(file.path)));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = { scan, resultsFolder, resultDir };
|
|
105
|
+
module.exports.VERSION = '1.0.0';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* PonderDev kit 1.0.0, built from kit/src. Do not edit: run bin/kit-sync. */
|
|
2
|
+
'use strict';
|
|
3
|
+
/*
|
|
4
|
+
* kit, main side: every Just tool window's frame (decided 2026-11-20).
|
|
5
|
+
*
|
|
6
|
+
* On Windows and Linux the app draws its own title bar in its own dark
|
|
7
|
+
* background, with the system's minimise, maximise and close buttons drawn
|
|
8
|
+
* over it in matching colours (Electron's titleBarOverlay), so no tool shows
|
|
9
|
+
* a white frame around a dark app. On a Mac the traffic lights sit over the
|
|
10
|
+
* page as before (hiddenInset).
|
|
11
|
+
*
|
|
12
|
+
* const { frame, dress } = require('./kit/window.cjs');
|
|
13
|
+
* win = new BrowserWindow({ width: 720, height: 780, ...frame(), webPreferences: {...} });
|
|
14
|
+
* dress(win);
|
|
15
|
+
*
|
|
16
|
+
* frame() gives the window's options: the frame, and backgroundColor (the
|
|
17
|
+
* kit's --bg, so nothing flashes white while the page loads). dress(win) puts
|
|
18
|
+
* the strip the title bar is drawn on into every page the window loads, a
|
|
19
|
+
* page of the tool's own or one without the kit (the licences page): the
|
|
20
|
+
* window's title on the left, room for the system buttons on the right, and
|
|
21
|
+
* the whole strip drags the window. A page that already has a .titlebar
|
|
22
|
+
* keeps it, and gets the title written into it.
|
|
23
|
+
*
|
|
24
|
+
* On Linux frame() also gives the window the tool's own name as its app id
|
|
25
|
+
* ("Just Cut"). A Linux desktop names a window it has no menu entry for by its
|
|
26
|
+
* app id alone (GNOME's dock does), so an AppImage run straight from Downloads
|
|
27
|
+
* shows "Just Cut" there, not "com.ponderdev.justcut". A menu entry finds the
|
|
28
|
+
* window by StartupWMClass, which the tool's Linux build sets to the same name.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
// The kit's --bg and --text (kit/src/tokens.css; the kit's tests compare them).
|
|
32
|
+
const BG = '#12131a';
|
|
33
|
+
const TEXT = '#eceef5';
|
|
34
|
+
const MUTED = '#9aa0b4';
|
|
35
|
+
// The strip's height on Windows and Linux: the system buttons' own height.
|
|
36
|
+
const HEIGHT = 32;
|
|
37
|
+
|
|
38
|
+
// Electron reads the app id from the desktop name (minus ".desktop") each
|
|
39
|
+
// time it makes a window, so this has to run before the first window: frame()
|
|
40
|
+
// is called in the window's options. Only in Electron's main process.
|
|
41
|
+
function linuxName(app, platform = process.platform) {
|
|
42
|
+
if (platform !== 'linux' || !app || typeof app.setDesktopName !== 'function') return;
|
|
43
|
+
app.setDesktopName(`${app.getName()}.desktop`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function frame(platform = process.platform) {
|
|
47
|
+
if (platform === 'linux' && process.type === 'browser') linuxName(require('electron').app, platform);
|
|
48
|
+
if (platform === 'darwin') return { titleBarStyle: 'hiddenInset', backgroundColor: BG };
|
|
49
|
+
return {
|
|
50
|
+
titleBarStyle: 'hidden',
|
|
51
|
+
titleBarOverlay: { color: BG, symbolColor: TEXT, height: HEIGHT },
|
|
52
|
+
backgroundColor: BG,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// The strip, for any page. env(titlebar-area-*) is where the page may draw
|
|
57
|
+
// beside the system buttons; the fallbacks are for a window without them.
|
|
58
|
+
const CSS = `
|
|
59
|
+
.titlebar { height: ${HEIGHT}px; flex: none; display: flex; align-items: center; -webkit-app-region: drag;
|
|
60
|
+
background: ${BG}; position: relative; z-index: 10; }
|
|
61
|
+
.titlebar .k-titlebar-name { padding-left: calc(env(titlebar-area-x, 0px) + 14px); max-width: env(titlebar-area-width, 100%);
|
|
62
|
+
font: 12px/1 system-ui, -apple-system, "Segoe UI", Ubuntu, sans-serif; color: ${MUTED};
|
|
63
|
+
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; user-select: none; }
|
|
64
|
+
body.k-doc > .titlebar, body > .titlebar:first-child { margin: 0; }
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
// Runs in the page: a .titlebar first in the body (made if the page has none),
|
|
68
|
+
// with the window's title in it. DOM calls only, so a page's own
|
|
69
|
+
// Content-Security-Policy (no scripts, no inline styles) does not stop it.
|
|
70
|
+
const PUT = `(() => {
|
|
71
|
+
let bar = document.querySelector('.titlebar');
|
|
72
|
+
if (!bar) { bar = document.createElement('div'); bar.className = 'titlebar'; document.body.prepend(bar); }
|
|
73
|
+
if (!bar.querySelector('.k-titlebar-name')) {
|
|
74
|
+
const name = document.createElement('span');
|
|
75
|
+
name.className = 'k-titlebar-name';
|
|
76
|
+
bar.append(name);
|
|
77
|
+
}
|
|
78
|
+
bar.querySelector('.k-titlebar-name').textContent = document.title;
|
|
79
|
+
return true;
|
|
80
|
+
})()`;
|
|
81
|
+
|
|
82
|
+
function dress(win, platform = process.platform) {
|
|
83
|
+
if (platform === 'darwin') return;
|
|
84
|
+
const contents = win.webContents;
|
|
85
|
+
contents.on('dom-ready', () => {
|
|
86
|
+
contents.insertCSS(CSS).catch(() => {});
|
|
87
|
+
contents.executeJavaScript(PUT).catch(() => {});
|
|
88
|
+
});
|
|
89
|
+
// A page that changes its title (a file's name, say) keeps the strip true.
|
|
90
|
+
win.on('page-title-updated', () => { contents.executeJavaScript(PUT).catch(() => {}); });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = { frame, dress, linuxName, BG, TEXT, HEIGHT, CSS };
|
package/src/main.cjs
CHANGED
|
@@ -12,12 +12,22 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
const { app, BrowserWindow, screen, ipcMain, dialog, shell } = require('electron');
|
|
15
|
+
// The frame: a dark title bar of the app's own on Windows and Linux, the traffic lights on a Mac.
|
|
16
|
+
const { frame, dress } = require('./kit/window.cjs');
|
|
17
|
+
|
|
15
18
|
const path = require('node:path');
|
|
19
|
+
|
|
20
|
+
// The tool's own version, from its package.json. Not app.getVersion(): run by
|
|
21
|
+
// a test or a harness, Electron reports its own version instead.
|
|
22
|
+
const APP_VERSION = (() => {
|
|
23
|
+
try { return require(path.join(__dirname, '..', 'package.json')).version; } catch (e) { return app.getVersion(); }
|
|
24
|
+
})();
|
|
16
25
|
const fs = require('node:fs');
|
|
17
26
|
|
|
18
27
|
const engine = require('./engine.cjs');
|
|
19
28
|
const plan = require('./plan.js');
|
|
20
29
|
const binaries = require('./binaries.cjs');
|
|
30
|
+
const { scan, places } = require('./scan.cjs');
|
|
21
31
|
|
|
22
32
|
let win = null;
|
|
23
33
|
let ctl = null;
|
|
@@ -51,6 +61,11 @@ function binary(name) {
|
|
|
51
61
|
* own window. Without that the test would be checking a renderer that has
|
|
52
62
|
* nothing to talk to, and would miss a handler going missing here.
|
|
53
63
|
*/
|
|
64
|
+
// The folder each gathered file came in with (null for a file on its own),
|
|
65
|
+
// and where the current run is putting results (scan.cjs places).
|
|
66
|
+
const gathered = new Map();
|
|
67
|
+
let placing = null;
|
|
68
|
+
|
|
54
69
|
function registerHandlers() {
|
|
55
70
|
ipcMain.handle('targets', () => plan.TARGETS);
|
|
56
71
|
|
|
@@ -66,21 +81,46 @@ function registerHandlers() {
|
|
|
66
81
|
lastOpenDir = path.dirname(file);
|
|
67
82
|
return { ok: true, info };
|
|
68
83
|
} catch (err) {
|
|
84
|
+
// Read fine but refused on purpose: its own sentence already names the file.
|
|
85
|
+
if (err.code === 'animated-webp') return { ok: false, message: err.message };
|
|
69
86
|
return { ok: false, message: probeMessage(file, String(err.message || err)) };
|
|
70
87
|
}
|
|
71
88
|
});
|
|
72
89
|
|
|
73
|
-
|
|
90
|
+
// run: a number the window gives each press of Compress, so a folder's
|
|
91
|
+
// results folder is chosen once per run ("Holiday (fitted)", then
|
|
92
|
+
// "Holiday (fitted) 2" the next time). Which folder a file came in with is
|
|
93
|
+
// main's own record from gather, never the page's word.
|
|
94
|
+
ipcMain.handle('fit', async (_e, { file, targetBytes, beside, run }) => {
|
|
74
95
|
ctl = engine.control();
|
|
75
96
|
const onProgress = (p) => {
|
|
76
97
|
if (win && !win.isDestroyed()) win.webContents.send('progress', p);
|
|
77
98
|
};
|
|
99
|
+
let where = null;
|
|
78
100
|
try {
|
|
79
101
|
const info = await engine.probe(binary('ffprobe'), file);
|
|
80
|
-
|
|
102
|
+
if (!placing || placing.run !== run || placing.beside !== Boolean(beside)) placing = { run, beside: Boolean(beside), place: places({ beside }) };
|
|
103
|
+
where = placing.place({ path: path.resolve(file), from: gathered.get(path.resolve(file)) || null });
|
|
104
|
+
if (where.outDir) await fs.promises.mkdir(where.outDir, { recursive: true });
|
|
105
|
+
// Already small enough, in a folder's results folder: copied in under its
|
|
106
|
+
// -fits name, so the results folder holds the whole folder (decided 2026-11-20).
|
|
107
|
+
if (where.folder && info.bytes <= targetBytes) {
|
|
108
|
+
const to = engine.outputPath(path.join(where.outDir, path.basename(file)), path.extname(file), fs.existsSync, where.suffix);
|
|
109
|
+
await fs.promises.copyFile(file, to, fs.constants.COPYFILE_EXCL);
|
|
110
|
+
return { ok: true, copied: true, path: to, bytes: info.bytes, before: info.bytes, kind: info.kind, folder: where.folder };
|
|
111
|
+
}
|
|
112
|
+
const args = { ffmpeg: binary('ffmpeg'), file, info, targetBytes, onProgress, ctl, outDir: where.outDir, suffix: where.suffix };
|
|
81
113
|
const result = info.kind === 'image' ? await engine.fitImage(args) : await engine.fitVideo(args);
|
|
82
|
-
return { ok: true, ...result, before: info.bytes, kind: info.kind };
|
|
114
|
+
return { ok: true, ...result, before: info.bytes, kind: info.kind, folder: where.folder, note: info.kind === 'video' ? plan.lengthNote(result.plan) : '' };
|
|
83
115
|
} catch (err) {
|
|
116
|
+
// Stopped or failed: a results folder made for this file and left empty
|
|
117
|
+
// goes again, up to the results folder itself, so nothing empty is left.
|
|
118
|
+
if (where && where.folder) {
|
|
119
|
+
for (let dir = where.outDir; dir.startsWith(where.folder); dir = path.dirname(dir)) {
|
|
120
|
+
try { await fs.promises.rmdir(dir); } catch { break; }
|
|
121
|
+
if (dir === where.folder) break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
84
124
|
if (err && err.cancelled) return { ok: false, cancelled: true };
|
|
85
125
|
return { ok: false, message: String(err.message || err) };
|
|
86
126
|
} finally {
|
|
@@ -96,8 +136,15 @@ function registerHandlers() {
|
|
|
96
136
|
|
|
97
137
|
ipcMain.handle('reveal', (_e, file) => shell.showItemInFolder(file));
|
|
98
138
|
|
|
99
|
-
// Opens in the real browser, never in an app window.
|
|
139
|
+
// Opens in the real browser, never in an app window. The addresses are fixed
|
|
140
|
+
// here; the page never hands main a URL to open.
|
|
100
141
|
ipcMain.handle('open-site', () => shell.openExternal('https://ponderdev.com'));
|
|
142
|
+
ipcMain.handle('open-how-it-works', () => shell.openExternal('https://ponderdev.com/tools/just-fits/how-it-works'));
|
|
143
|
+
|
|
144
|
+
// The skill that ships with the app, for the start screen's Copy skill. Read
|
|
145
|
+
// beside the app's own files, so it is the same text inside the packaged app
|
|
146
|
+
// (electron-builder packs agents/) as from source.
|
|
147
|
+
ipcMain.handle('skill-text', () => fs.promises.readFile(path.join(__dirname, '..', 'agents', 'skills', 'justfits', 'SKILL.md'), 'utf8'));
|
|
101
148
|
|
|
102
149
|
// The GPL wants its notices readable by whoever has the app, not just by
|
|
103
150
|
// whoever finds the repo, so they open in a window of their own.
|
|
@@ -107,25 +154,51 @@ function registerHandlers() {
|
|
|
107
154
|
width: 720,
|
|
108
155
|
height: 680,
|
|
109
156
|
title: 'Licences and credits',
|
|
110
|
-
|
|
157
|
+
...frame(),
|
|
111
158
|
webPreferences: { contextIsolation: true, nodeIntegration: false },
|
|
112
159
|
});
|
|
160
|
+
dress(licenceWindow);
|
|
113
161
|
licenceWindow.setMenuBarVisibility(false);
|
|
114
162
|
licenceWindow.loadFile(path.join(__dirname, 'renderer', 'licenses.html'));
|
|
115
163
|
});
|
|
116
164
|
|
|
165
|
+
// Files and folders, several at once. Windows and Linux cannot offer files
|
|
166
|
+
// and folders in one window (asked for both, they show a folder picker), so
|
|
167
|
+
// there it picks files, and the start screen's Choose a folder (pick-folder)
|
|
168
|
+
// brings a folder in.
|
|
117
169
|
ipcMain.handle('pick-file', async () => {
|
|
118
170
|
const r = await dialog.showOpenDialog(win, {
|
|
119
171
|
defaultPath: lastOpenDir || app.getPath('documents'),
|
|
120
|
-
properties: ['openFile'],
|
|
172
|
+
properties: process.platform === 'darwin' ? ['openFile', 'openDirectory', 'multiSelections'] : ['openFile', 'multiSelections'],
|
|
121
173
|
filters: [{
|
|
122
174
|
name: 'Video and images',
|
|
123
175
|
extensions: ['mp4', 'mov', 'mkv', 'webm', 'avi', 'm4v', 'jpg', 'jpeg', 'png', 'heic', 'webp', 'gif'],
|
|
124
176
|
}],
|
|
125
177
|
});
|
|
126
|
-
if (r.canceled) return
|
|
178
|
+
if (r.canceled || !r.filePaths.length) return [];
|
|
127
179
|
lastOpenDir = path.dirname(r.filePaths[0]);
|
|
128
|
-
return r.filePaths
|
|
180
|
+
return r.filePaths;
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// Folders only, for Windows and Linux, where pick-file cannot offer them.
|
|
184
|
+
ipcMain.handle('pick-folder', async () => {
|
|
185
|
+
const r = await dialog.showOpenDialog(win, {
|
|
186
|
+
defaultPath: lastOpenDir || app.getPath('documents'),
|
|
187
|
+
properties: ['openDirectory', 'multiSelections'],
|
|
188
|
+
});
|
|
189
|
+
if (r.canceled || !r.filePaths.length) return [];
|
|
190
|
+
lastOpenDir = r.filePaths[0];
|
|
191
|
+
return r.filePaths;
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// What several paths, or a folder, hold (scan.cjs). single: one path that is
|
|
195
|
+
// not a folder, which the window takes the way it takes one file.
|
|
196
|
+
ipcMain.handle('gather', async (_e, paths) => {
|
|
197
|
+
const list = Array.isArray(paths) ? paths.filter((p) => typeof p === 'string' && p) : [];
|
|
198
|
+
const found = await scan(list);
|
|
199
|
+
for (const f of found.files) gathered.set(f.path, f.from);
|
|
200
|
+
if (found.files[0]) lastOpenDir = path.dirname(found.files[0].path);
|
|
201
|
+
return { ...found, single: list.length === 1 && found.folders === 0 };
|
|
129
202
|
});
|
|
130
203
|
}
|
|
131
204
|
|
|
@@ -161,14 +234,17 @@ function registerHandlers() {
|
|
|
161
234
|
height: 780,
|
|
162
235
|
minWidth: 560,
|
|
163
236
|
minHeight: 640,
|
|
164
|
-
|
|
165
|
-
titleBarStyle: process.platform === 'darwin' ? 'hiddenInset' : 'default',
|
|
237
|
+
...frame(),
|
|
166
238
|
webPreferences: {
|
|
167
239
|
preload: path.join(__dirname, 'preload.cjs'),
|
|
168
240
|
contextIsolation: true,
|
|
169
241
|
nodeIntegration: false,
|
|
242
|
+
// The version, from package.json, for the start screen and the footer.
|
|
243
|
+
// A sandboxed preload cannot read package.json, so it comes on argv.
|
|
244
|
+
additionalArguments: ['--app-version=' + APP_VERSION],
|
|
170
245
|
},
|
|
171
246
|
}));
|
|
247
|
+
dress(win);
|
|
172
248
|
win.setMenuBarVisibility(false);
|
|
173
249
|
win.loadFile(path.join(__dirname, 'renderer', 'index.html'));
|
|
174
250
|
}
|
package/src/plan.js
CHANGED
|
@@ -231,8 +231,19 @@ function isDeviceName(outPath) {
|
|
|
231
231
|
return /^(con|prn|aux|nul|conin\$|conout\$|com[1-9¹²³]|lpt[1-9¹²³])$/i.test(head);
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
+
// The sentence for a video too long for its target, the same words in the
|
|
235
|
+
// window and on the command line. Empty when the picture will be fine.
|
|
236
|
+
function lengthNote(p, size) {
|
|
237
|
+
if (!p) return '';
|
|
238
|
+
const forSize = size ? `for ${size}` : 'for that size';
|
|
239
|
+
if (p.hopeless) return `This is too long ${forSize}: expect a small, blurry picture, and it may still come out over. A bigger target or a shorter clip would look far better.`;
|
|
240
|
+
if (p.rough) return `This is long ${forSize}, so the picture will be softer than the original.`;
|
|
241
|
+
return '';
|
|
242
|
+
}
|
|
243
|
+
|
|
234
244
|
module.exports = {
|
|
235
245
|
MB,
|
|
246
|
+
lengthNote,
|
|
236
247
|
BASE64,
|
|
237
248
|
TARGETS,
|
|
238
249
|
HEADROOM,
|
package/src/preload.cjs
CHANGED
|
@@ -3,18 +3,31 @@
|
|
|
3
3
|
// The only surface the page gets. No node, no filesystem, no network.
|
|
4
4
|
const { contextBridge, ipcRenderer, webUtils } = require('electron');
|
|
5
5
|
|
|
6
|
+
// The tool's version, put on argv by main (a sandboxed preload cannot read
|
|
7
|
+
// package.json), for the start screen and the footer.
|
|
8
|
+
const version = (process.argv.find((a) => a.startsWith('--app-version=')) || '').slice('--app-version='.length);
|
|
9
|
+
|
|
6
10
|
contextBridge.exposeInMainWorld('fits', {
|
|
11
|
+
version,
|
|
7
12
|
// Fetched from main rather than required in here: preloads are sandboxed
|
|
8
13
|
// and can only require Electron's own modules. Keeping the sandbox is worth
|
|
9
14
|
// more than the convenience, and it means one copy of the list either way.
|
|
10
15
|
targets: () => ipcRenderer.invoke('targets'),
|
|
11
16
|
probe: (file) => ipcRenderer.invoke('probe', file),
|
|
12
|
-
fit: (file, targetBytes) => ipcRenderer.invoke('fit', { file, targetBytes }),
|
|
17
|
+
fit: (file, targetBytes, options = {}) => ipcRenderer.invoke('fit', { file, targetBytes, beside: Boolean(options.beside), run: Number(options.run) || 0 }),
|
|
13
18
|
cancel: () => ipcRenderer.invoke('cancel'),
|
|
14
19
|
reveal: (file) => ipcRenderer.invoke('reveal', file),
|
|
15
20
|
openSite: () => ipcRenderer.invoke('open-site'),
|
|
21
|
+
openHowItWorks: () => ipcRenderer.invoke('open-how-it-works'),
|
|
22
|
+
skillText: () => ipcRenderer.invoke('skill-text'),
|
|
16
23
|
openLicences: () => ipcRenderer.invoke('open-licences'),
|
|
24
|
+
// Several paths at once: files, or folders on a Mac.
|
|
17
25
|
pickFile: () => ipcRenderer.invoke('pick-file'),
|
|
26
|
+
// Folders alone, where the file picker cannot take them (Windows, Linux).
|
|
27
|
+
pickFolder: () => ipcRenderer.invoke('pick-folder'),
|
|
28
|
+
folderButton: process.platform !== 'darwin',
|
|
29
|
+
platform: process.platform,
|
|
30
|
+
gather: (paths) => ipcRenderer.invoke('gather', paths),
|
|
18
31
|
// File.path was removed in Electron 32. Dropped files have to be resolved
|
|
19
32
|
// through webUtils now, which only exists on this side of the bridge.
|
|
20
33
|
pathFor: (file) => webUtils.getPathForFile(file),
|