@trippnology/mixshell 0.0.0-stage → 0.7.1
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/LICENSE +21 -0
- package/README.md +320 -2
- package/assets/mixshell-screenshot.png +0 -0
- package/package.json +74 -5
- package/src/alsa-pulse.conf +20 -0
- package/src/alsa-setup.js +15 -0
- package/src/components/browser.js +193 -0
- package/src/components/config.js +168 -0
- package/src/components/durations.js +151 -0
- package/src/components/library.js +679 -0
- package/src/components/mediakeys.js +222 -0
- package/src/components/mpris.js +309 -0
- package/src/components/player.js +436 -0
- package/src/components/playlist.js +335 -0
- package/src/components/session.js +96 -0
- package/src/constants.js +164 -0
- package/src/engines/ffmpeg.js +610 -0
- package/src/engines/index.js +14 -0
- package/src/engines/webaudio.js +317 -0
- package/src/index.js +188 -0
- package/src/mediakeys/macos-nowplaying.js +227 -0
- package/src/ui/app.js +56 -0
- package/src/ui/artPane.js +314 -0
- package/src/ui/confirmPrompt.js +34 -0
- package/src/ui/constants.js +133 -0
- package/src/ui/controller.js +1654 -0
- package/src/ui/dom.js +9 -0
- package/src/ui/goToPrompt.js +105 -0
- package/src/ui/index.html +78 -0
- package/src/ui/libraryPanel.js +415 -0
- package/src/ui/listView.js +209 -0
- package/src/ui/onboarding.js +353 -0
- package/src/ui/playlistPanel.js +207 -0
- package/src/ui/settingsPanel.js +473 -0
- package/src/ui/shortcuts.js +30 -0
- package/src/ui/splash.js +302 -0
- package/src/ui/style.css +532 -0
- package/src/ui/subtabs.js +31 -0
- package/src/ui/visualizer/bars.js +74 -0
- package/src/ui/visualizer/circles.js +64 -0
- package/src/ui/visualizer/color.js +61 -0
- package/src/ui/visualizer/cube.js +105 -0
- package/src/ui/visualizer/grid.js +68 -0
- package/src/ui/visualizer/index.js +65 -0
- package/src/ui/visualizer/onset.js +130 -0
- package/src/ui/visualizer/spectrum.js +34 -0
- package/src/ui/visualizer/vu.js +71 -0
- package/src/ui/visualizer/waveform.js +58 -0
- package/src/utils/audioAnalyzer.js +362 -0
- package/src/utils/audioDevices.js +222 -0
- package/src/utils/cellart.js +75 -0
- package/src/utils/debug.js +7 -0
- package/src/utils/durationProbe.js +48 -0
- package/src/utils/folderArt.js +46 -0
- package/src/utils/format.js +20 -0
- package/src/utils/id3.js +84 -0
- package/src/utils/pathInput.js +76 -0
- package/src/utils/playlistIO.js +176 -0
- package/src/utils/terminalGraphics.js +47 -0
|
@@ -0,0 +1,1654 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { createDurationService } from '../components/durations.js';
|
|
3
|
+
import { saveSession } from '../components/session.js';
|
|
4
|
+
import { KEYS, REPEAT_LABELS, SCREENS } from '../constants.js';
|
|
5
|
+
import { friendlyDeviceName } from '../utils/audioDevices.js';
|
|
6
|
+
import { DBG } from '../utils/debug.js';
|
|
7
|
+
import { findFolderArt } from '../utils/folderArt.js';
|
|
8
|
+
import { formatLongTime, formatTime } from '../utils/format.js';
|
|
9
|
+
import { graphicsLabel } from '../utils/terminalGraphics.js';
|
|
10
|
+
import { createArtPane } from './artPane.js';
|
|
11
|
+
import { createConfirm } from './confirmPrompt.js';
|
|
12
|
+
import {
|
|
13
|
+
globalShortcuts,
|
|
14
|
+
SEARCH_SHORTCUTS,
|
|
15
|
+
SETTINGS_DEVICE_SHORTCUTS,
|
|
16
|
+
SETTINGS_DIR_SHORTCUTS,
|
|
17
|
+
THEMES,
|
|
18
|
+
tabShortcuts,
|
|
19
|
+
VIZ_BARS,
|
|
20
|
+
VIZ_MODES,
|
|
21
|
+
} from './constants.js';
|
|
22
|
+
import { makeEl } from './dom.js';
|
|
23
|
+
import { createGoToPrompt } from './goToPrompt.js';
|
|
24
|
+
import { createLibraryPanel } from './libraryPanel.js';
|
|
25
|
+
import { createListView } from './listView.js';
|
|
26
|
+
import { createOnboarding } from './onboarding.js';
|
|
27
|
+
import { createPlaylistPanel } from './playlistPanel.js';
|
|
28
|
+
import { createSettingsPanel } from './settingsPanel.js';
|
|
29
|
+
import { isPlain, packShortcuts } from './shortcuts.js';
|
|
30
|
+
import { createSplash } from './splash.js';
|
|
31
|
+
import { MODES, Visualizer } from './visualizer/index.js';
|
|
32
|
+
|
|
33
|
+
export function init(term, app, attachReady) {
|
|
34
|
+
const { document } = term;
|
|
35
|
+
const win = term.window;
|
|
36
|
+
const {
|
|
37
|
+
player,
|
|
38
|
+
playlist,
|
|
39
|
+
browser,
|
|
40
|
+
config,
|
|
41
|
+
library,
|
|
42
|
+
analyzer,
|
|
43
|
+
debug,
|
|
44
|
+
startDirectory,
|
|
45
|
+
startupNotices = [],
|
|
46
|
+
} = app;
|
|
47
|
+
|
|
48
|
+
const $ = (sel) => document.querySelector(sel);
|
|
49
|
+
|
|
50
|
+
// Debug tab is developer tooling: hidden without --debug, stripped
|
|
51
|
+
// before the els snapshot so every tab list derives from clean markup.
|
|
52
|
+
if (!debug) {
|
|
53
|
+
$('[data-tab="debug"]')?.remove();
|
|
54
|
+
$('#panel-debug')?.remove();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const els = {
|
|
58
|
+
tabbar: $('#tabs'),
|
|
59
|
+
tabs: [...document.querySelectorAll('.tab')],
|
|
60
|
+
panels: [...document.querySelectorAll('.panel')],
|
|
61
|
+
headerInfo: $('#headerInfo'),
|
|
62
|
+
artBox: $('#artBox'),
|
|
63
|
+
artCanvas: $('#artCanvas'),
|
|
64
|
+
vizCanvas: $('#vizCanvas'),
|
|
65
|
+
trackMeta: $('#trackMeta'),
|
|
66
|
+
noTrack: $('#noTrack'),
|
|
67
|
+
splash: $('#splashCanvas'),
|
|
68
|
+
browserPath: $('#browserPath'),
|
|
69
|
+
browserList: $('#browserList'),
|
|
70
|
+
playlistInfo: $('#playlistInfo'),
|
|
71
|
+
playlistList: $('#playlistList'),
|
|
72
|
+
playlistEmpty: $('#playlistEmpty'),
|
|
73
|
+
librarySubtabs: $('#librarySubtabs'),
|
|
74
|
+
libraryPath: $('#libraryPath'),
|
|
75
|
+
libraryList: $('#libraryList'),
|
|
76
|
+
libraryEmpty: $('#libraryEmpty'),
|
|
77
|
+
settingsSubtabs: $('#settingsSubtabs'),
|
|
78
|
+
settingsPath: $('#settingsPath'),
|
|
79
|
+
settingsList: $('#settingsList'),
|
|
80
|
+
systemInfo: $('#systemInfo'),
|
|
81
|
+
debugList: $('#debugList'),
|
|
82
|
+
shortcutsGlobal: $('#shortcutsGlobal'),
|
|
83
|
+
shortcutsLocal: $('#shortcutsLocal'),
|
|
84
|
+
seekRow: $('#seekRow'),
|
|
85
|
+
seekBar: $('#seekBar'),
|
|
86
|
+
seekClock: $('#seekClock'),
|
|
87
|
+
status: $('#status'),
|
|
88
|
+
toast: $('#toast'),
|
|
89
|
+
toastMsg: $('#toastMsg'),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const tabs = els.tabs.map((tab) => tab.dataset.tab);
|
|
93
|
+
|
|
94
|
+
// Chrome row counts (issues/066). The footer's count is computed from
|
|
95
|
+
// the packed lines — never measured, so it cannot change when the seek
|
|
96
|
+
// row hides. The header's is measured from the tab bar's content height.
|
|
97
|
+
// Both are handed to CSS as custom properties; every chrome size and
|
|
98
|
+
// every panel budget derives from them.
|
|
99
|
+
let footerRows = 4;
|
|
100
|
+
let headerRows = 1;
|
|
101
|
+
|
|
102
|
+
// Library › Search at root level captures plain typing, so its footer
|
|
103
|
+
// uses a dedicated row and drops the global row (issues/052).
|
|
104
|
+
function isSearchRoot() {
|
|
105
|
+
return state.activeTab === SCREENS.LIBRARY && libraryPanel.isSearchRoot();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Local shortcuts depend on the tab and its current view/drill state.
|
|
109
|
+
function localShortcutEntries() {
|
|
110
|
+
let local = isSearchRoot()
|
|
111
|
+
? SEARCH_SHORTCUTS
|
|
112
|
+
: state.activeTab === SCREENS.SETTINGS && settingsPanel.isDeviceList()
|
|
113
|
+
? SETTINGS_DEVICE_SHORTCUTS
|
|
114
|
+
: state.activeTab === SCREENS.SETTINGS && settingsPanel.isDrilled()
|
|
115
|
+
? SETTINGS_DIR_SHORTCUTS
|
|
116
|
+
: tabShortcuts(config.get('seekStep'))[state.activeTab];
|
|
117
|
+
// `t` only applies on the Most Played view (issues/032).
|
|
118
|
+
if (
|
|
119
|
+
state.activeTab === SCREENS.LIBRARY &&
|
|
120
|
+
libraryPanel.isMostPlayedView()
|
|
121
|
+
) {
|
|
122
|
+
local = [...local, ['t', 'Timeframe']];
|
|
123
|
+
}
|
|
124
|
+
return local ?? [];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// --- State ---------------------------------------------------------------
|
|
128
|
+
const state = {
|
|
129
|
+
activeTab: SCREENS.MAIN,
|
|
130
|
+
// Per-tab list cursors. The browser keeps its own (the Browser
|
|
131
|
+
// component owns selectedIndex and resets it on loadDirectory);
|
|
132
|
+
// every other list tab stores its cursor here.
|
|
133
|
+
cursors: {
|
|
134
|
+
[SCREENS.PLAYLIST]: 0,
|
|
135
|
+
[SCREENS.LIBRARY]: 0,
|
|
136
|
+
[SCREENS.SETTINGS]: 0,
|
|
137
|
+
[SCREENS.DEBUG]: 0,
|
|
138
|
+
},
|
|
139
|
+
currentSong: null,
|
|
140
|
+
currentTime: 0,
|
|
141
|
+
duration: 0,
|
|
142
|
+
// Starred track paths (issues/045) — refreshed from the library on
|
|
143
|
+
// every 'updated'; the browser/playlist icons read this set.
|
|
144
|
+
favPaths: new Set(),
|
|
145
|
+
debugMessages: [],
|
|
146
|
+
toastTimer: null,
|
|
147
|
+
// Onboarding advice waits for scanDone when a scan was started:
|
|
148
|
+
// scanProgress owns the single toast slot until then (issues/060).
|
|
149
|
+
adviceAfterScan: false,
|
|
150
|
+
vizInterval: null,
|
|
151
|
+
vizHandler: null,
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// Filter mode (issues/040): Ctrl+F on the browser/playlist lists opens
|
|
155
|
+
// a key-capture input (deviation from the issue's type-to-search —
|
|
156
|
+
// plain typing must keep firing the tab shortcuts). One mode at a
|
|
157
|
+
// time; the query lives only while the mode is open. `map` is the
|
|
158
|
+
// filtered index -> real index the render-time slice produces.
|
|
159
|
+
const filter = {
|
|
160
|
+
open: false,
|
|
161
|
+
query: '',
|
|
162
|
+
pos: 0,
|
|
163
|
+
baseCursor: 0,
|
|
164
|
+
map: [],
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// Go-to prompt for the browser tab; owns its keys while open, above
|
|
168
|
+
// digits/globals and the filter (issues/051).
|
|
169
|
+
const goToBrowser = createGoToPrompt();
|
|
170
|
+
|
|
171
|
+
const visualizer = new Visualizer();
|
|
172
|
+
visualizer.setMode(vizMode());
|
|
173
|
+
syncVizNeeds();
|
|
174
|
+
const artPane = createArtPane({
|
|
175
|
+
els,
|
|
176
|
+
win,
|
|
177
|
+
document,
|
|
178
|
+
config,
|
|
179
|
+
visualizer,
|
|
180
|
+
log,
|
|
181
|
+
});
|
|
182
|
+
// Idle letter field (issues/049): shown by renderMain while nothing is
|
|
183
|
+
// playing, hidden the moment a track loads or resumes.
|
|
184
|
+
const splash = createSplash({ els, win, document });
|
|
185
|
+
|
|
186
|
+
function log(message) {
|
|
187
|
+
state.debugMessages.push({ timestamp: new Date(), message });
|
|
188
|
+
if (state.debugMessages.length > 100) {
|
|
189
|
+
state.debugMessages.shift();
|
|
190
|
+
}
|
|
191
|
+
if (state.activeTab === SCREENS.DEBUG) {
|
|
192
|
+
render();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// --- Helpers ---------------------------------------------------------------
|
|
197
|
+
const el = makeEl(document);
|
|
198
|
+
|
|
199
|
+
function isListTab() {
|
|
200
|
+
return (
|
|
201
|
+
state.activeTab === SCREENS.BROWSER ||
|
|
202
|
+
state.activeTab === SCREENS.PLAYLIST ||
|
|
203
|
+
state.activeTab === SCREENS.LIBRARY ||
|
|
204
|
+
state.activeTab === SCREENS.SETTINGS
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// --- List cursors ----------------------------------------------------------
|
|
209
|
+
// The browser tab keeps the Browser component's own selectedIndex (it is
|
|
210
|
+
// reset by loadDirectory); every other list tab uses state.cursors.
|
|
211
|
+
// `tab` defaults to the active tab; pass one explicitly outside renders.
|
|
212
|
+
function cursor(tab = state.activeTab) {
|
|
213
|
+
return tab === SCREENS.BROWSER
|
|
214
|
+
? browser.selectedIndex
|
|
215
|
+
: (state.cursors[tab] ?? 0);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function setCursor(index, tab = state.activeTab) {
|
|
219
|
+
if (tab === SCREENS.BROWSER) {
|
|
220
|
+
browser.selectedIndex = index;
|
|
221
|
+
} else {
|
|
222
|
+
state.cursors[tab] = index;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function vizMode() {
|
|
227
|
+
const mode = config.get('visualizerMode');
|
|
228
|
+
return VIZ_MODES.includes(mode) ? mode : 'albumart';
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function applyTheme() {
|
|
232
|
+
const theme = config.get('theme') || 'default';
|
|
233
|
+
for (const name of THEMES) {
|
|
234
|
+
document.body.classList.toggle(`theme-${name}`, name === theme);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function listItems() {
|
|
239
|
+
switch (state.activeTab) {
|
|
240
|
+
case SCREENS.BROWSER: {
|
|
241
|
+
const items = browser.entries.map((entry) => ({
|
|
242
|
+
icon: entry.isDirectory
|
|
243
|
+
? '▸'
|
|
244
|
+
: entry.isPlaylist
|
|
245
|
+
? '≡'
|
|
246
|
+
: entry.isAudio
|
|
247
|
+
? state.favPaths.has(entry.path)
|
|
248
|
+
? '★'
|
|
249
|
+
: '♪'
|
|
250
|
+
: '·',
|
|
251
|
+
text: entry.isDirectory ? `${entry.name}/` : entry.name,
|
|
252
|
+
audio: entry.isAudio,
|
|
253
|
+
}));
|
|
254
|
+
// Filter matches file and directory names (issues/040).
|
|
255
|
+
return applyFilter(items, (item) => item.text);
|
|
256
|
+
}
|
|
257
|
+
case SCREENS.PLAYLIST: {
|
|
258
|
+
// Per-row durations (issues/046): one cache read per render,
|
|
259
|
+
// suffix after the track details — unknown tracks get none.
|
|
260
|
+
const durations = library.durationsByPaths(
|
|
261
|
+
playlist.tracks.map((track) => track.path),
|
|
262
|
+
);
|
|
263
|
+
const items = playlist.tracks.map((track, index) => {
|
|
264
|
+
const seconds = durations.get(track.path);
|
|
265
|
+
return {
|
|
266
|
+
icon:
|
|
267
|
+
index === playlist.currentIndex
|
|
268
|
+
? '►'
|
|
269
|
+
: state.favPaths.has(track.path)
|
|
270
|
+
? '★'
|
|
271
|
+
: '',
|
|
272
|
+
text: track.name,
|
|
273
|
+
// Own span, default text colour (listView suffix).
|
|
274
|
+
suffix: seconds > 0 ? ` · ${formatTime(seconds)}` : '',
|
|
275
|
+
current: index === playlist.currentIndex,
|
|
276
|
+
audio: true,
|
|
277
|
+
};
|
|
278
|
+
});
|
|
279
|
+
// Filter matches whatever the row renders plus the path, so
|
|
280
|
+
// title/artist in folder-structured names still hit
|
|
281
|
+
// (issues/040).
|
|
282
|
+
return applyFilter(
|
|
283
|
+
items,
|
|
284
|
+
(_item, i) => `${playlist.tracks[i].name} ${playlist.tracks[i].path}`,
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
case SCREENS.LIBRARY:
|
|
288
|
+
// Keep filter.map/length in step while typing (the panel's
|
|
289
|
+
// render applies the same filter to the visible rows).
|
|
290
|
+
return applyFilter(libraryPanel.rows(), (item) => item.text);
|
|
291
|
+
case SCREENS.SETTINGS:
|
|
292
|
+
return settingsPanel.rows();
|
|
293
|
+
case SCREENS.DEBUG:
|
|
294
|
+
return [...state.debugMessages].reverse().map((entry) => ({
|
|
295
|
+
icon: '',
|
|
296
|
+
text: `[${entry.timestamp.toLocaleTimeString()}] ${entry.message}`,
|
|
297
|
+
}));
|
|
298
|
+
default:
|
|
299
|
+
return [];
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function listLength() {
|
|
304
|
+
return listItems().length;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// --- Filter mode (issues/040, Library in issues/052) -----------------------
|
|
308
|
+
// Opened by Ctrl+F on Browser/Playlist/Library and cleared by switchTab,
|
|
309
|
+
// so filter.open alone means "filtering".
|
|
310
|
+
|
|
311
|
+
function openFilter() {
|
|
312
|
+
filter.open = true;
|
|
313
|
+
filter.query = '';
|
|
314
|
+
filter.pos = 0;
|
|
315
|
+
// The real cursor when the mode opened — closeFilter falls back to
|
|
316
|
+
// it when the filtered view ends with nothing selected.
|
|
317
|
+
filter.baseCursor = cursor();
|
|
318
|
+
filter.map = [];
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Exit filter mode: put the real cursor on the row the filtered view
|
|
322
|
+
// had selected ("clearing the filter restores the unfiltered list with
|
|
323
|
+
// the cursor on the selected row"), then clear the state.
|
|
324
|
+
function closeFilter() {
|
|
325
|
+
const real = filter.map[cursor()];
|
|
326
|
+
setCursor(real === undefined ? filter.baseCursor : real);
|
|
327
|
+
filter.open = false;
|
|
328
|
+
filter.query = '';
|
|
329
|
+
filter.pos = 0;
|
|
330
|
+
filter.map = [];
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Render-time slice over the full row arrays — the underlying
|
|
334
|
+
// Browser.entries / Playlist.tracks are never spliced. Recomputes the
|
|
335
|
+
// index map, so mid-filter actions and closeFilter can resolve back to
|
|
336
|
+
// the real rows. An empty query matches everything ("x".includes(""))
|
|
337
|
+
// and the map comes out an identity.
|
|
338
|
+
function applyFilter(items, haystack) {
|
|
339
|
+
if (!filter.open) {
|
|
340
|
+
filter.map = [];
|
|
341
|
+
return items;
|
|
342
|
+
}
|
|
343
|
+
const query = filter.query.toLowerCase();
|
|
344
|
+
filter.map = [];
|
|
345
|
+
const filtered = [];
|
|
346
|
+
for (let i = 0; i < items.length; i++) {
|
|
347
|
+
if (haystack(items[i], i).toLowerCase().includes(query)) {
|
|
348
|
+
filtered.push(items[i]);
|
|
349
|
+
filter.map.push(i);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return filtered;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Typing shrinks the filtered list — keep the selection on the same real
|
|
356
|
+
// row. If that row was filtered away, jump to the top result (issues/052).
|
|
357
|
+
function clampFilteredCursor() {
|
|
358
|
+
// filter.map still reflects the previous query (render runs after
|
|
359
|
+
// key handling) — resolve the selected row to its real index first.
|
|
360
|
+
const current = filter.map[cursor()];
|
|
361
|
+
const length = listLength(); // re-applies filter, rebuilding map
|
|
362
|
+
if (length === 0 || current === undefined) {
|
|
363
|
+
setCursor(0);
|
|
364
|
+
} else {
|
|
365
|
+
setCursor(Math.max(0, filter.map.indexOf(current)));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Title-line feedback: query + text cursor, save-prompt style.
|
|
370
|
+
function filterSuffix() {
|
|
371
|
+
if (!filter.open) return '';
|
|
372
|
+
return ` · Filter: "${filter.query.slice(0, filter.pos)}▌${filter.query.slice(filter.pos)}"`;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Mid-filter actions resolve the real row through the index map (the
|
|
376
|
+
// cursor indexes the filtered view while filtering). Plain Enter doesn't
|
|
377
|
+
// come through here — closeFilter() restores the real cursor first.
|
|
378
|
+
function realIndex() {
|
|
379
|
+
return filter.open ? (filter.map[cursor()] ?? -1) : cursor();
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function filteredBrowserEntry() {
|
|
383
|
+
return browser.entries[realIndex()] ?? null;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Key capture while filtering (issues/040): printable and editing keys
|
|
387
|
+
// build the query; Esc closes and clears it; plain Enter closes so the
|
|
388
|
+
// fall-through action runs on the restored real cursor, while the
|
|
389
|
+
// Alt/Shift/Ctrl+Enter queue variants keep the filter open and resolve
|
|
390
|
+
// through the index map. Arrows/PageUp/PageDown also fall through so
|
|
391
|
+
// the filtered list stays walkable while typing — the save-prompt
|
|
392
|
+
// precedent parks those, but a filter you can't navigate is useless.
|
|
393
|
+
// Runs in the save prompt's slot, before the digit/global handlers.
|
|
394
|
+
function filterKey(e) {
|
|
395
|
+
const keyName = e.key;
|
|
396
|
+
if (keyName === 'Escape') {
|
|
397
|
+
closeFilter();
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
if (keyName === 'Enter') {
|
|
401
|
+
if (!e.altKey && !e.shiftKey && !e.ctrlKey) closeFilter();
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
if (keyName === 'Backspace') {
|
|
405
|
+
if (filter.pos > 0) {
|
|
406
|
+
filter.query =
|
|
407
|
+
filter.query.slice(0, filter.pos - 1) +
|
|
408
|
+
filter.query.slice(filter.pos);
|
|
409
|
+
filter.pos--;
|
|
410
|
+
clampFilteredCursor();
|
|
411
|
+
}
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
if (keyName === 'Delete') {
|
|
415
|
+
if (filter.pos < filter.query.length) {
|
|
416
|
+
filter.query =
|
|
417
|
+
filter.query.slice(0, filter.pos) +
|
|
418
|
+
filter.query.slice(filter.pos + 1);
|
|
419
|
+
clampFilteredCursor();
|
|
420
|
+
}
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
if (keyName === 'ArrowLeft') {
|
|
424
|
+
filter.pos = Math.max(0, filter.pos - 1);
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
if (keyName === 'ArrowRight') {
|
|
428
|
+
filter.pos = Math.min(filter.query.length, filter.pos + 1);
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
if (keyName === 'Home') {
|
|
432
|
+
filter.pos = 0;
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
if (keyName === 'End') {
|
|
436
|
+
filter.pos = filter.query.length;
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
if (keyName.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
440
|
+
filter.query =
|
|
441
|
+
filter.query.slice(0, filter.pos) +
|
|
442
|
+
keyName +
|
|
443
|
+
filter.query.slice(filter.pos);
|
|
444
|
+
filter.pos++;
|
|
445
|
+
clampFilteredCursor();
|
|
446
|
+
return true;
|
|
447
|
+
}
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// --- Rendering ---------------------------------------------------------------
|
|
452
|
+
// Panels are static markup toggled via display (demo pattern); renderers
|
|
453
|
+
// only update their contents. Shortcut rows and header info are plain text
|
|
454
|
+
// and change only on tab switch / visualizer mode change.
|
|
455
|
+
|
|
456
|
+
// --- Windowed lists ---------------------------------------------------------
|
|
457
|
+
// Windowing and the scroll listeners live in listView.js; this switch
|
|
458
|
+
// resolves the active list container.
|
|
459
|
+
function activeListContainer() {
|
|
460
|
+
switch (state.activeTab) {
|
|
461
|
+
case SCREENS.BROWSER:
|
|
462
|
+
return els.browserList;
|
|
463
|
+
case SCREENS.PLAYLIST:
|
|
464
|
+
return els.playlistList;
|
|
465
|
+
case SCREENS.SETTINGS:
|
|
466
|
+
return els.settingsList;
|
|
467
|
+
case SCREENS.DEBUG:
|
|
468
|
+
return els.debugList;
|
|
469
|
+
case SCREENS.LIBRARY:
|
|
470
|
+
return els.libraryList;
|
|
471
|
+
default:
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const listView = createListView({
|
|
477
|
+
document,
|
|
478
|
+
getViewportBase: () => win.innerHeight - footerRows - headerRows - 2,
|
|
479
|
+
getCursor: () => cursor(),
|
|
480
|
+
setCursor,
|
|
481
|
+
onSelectionChange: () => render(),
|
|
482
|
+
onActivate: activateRow,
|
|
483
|
+
activeContainer: activeListContainer,
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
// --- Onboarding (issues/041) -------------------------------------------------
|
|
487
|
+
const LOAD_ADVICE = 'Visit the Library or Browser to load music';
|
|
488
|
+
// First-run setup as a real top-layer <dialog> (termdom showModal — the
|
|
489
|
+
// page behind is inert, so nothing else needs parking). Completion hands
|
|
490
|
+
// back the chosen directory + scan answer: the app browser follows the
|
|
491
|
+
// new media directory and the scan runs through the same toast/error
|
|
492
|
+
// path as the settings rescan.
|
|
493
|
+
// Shared with Settings › Media Directory (issues/055): a failed reload
|
|
494
|
+
// keeps the current listing and toasts the reason (issues/054).
|
|
495
|
+
function reloadBrowserDirectory(dir) {
|
|
496
|
+
if (!browser.loadDirectory(dir)) {
|
|
497
|
+
showMessage(browser.lastError.error);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
const onboarding = createOnboarding({
|
|
501
|
+
document,
|
|
502
|
+
el,
|
|
503
|
+
config,
|
|
504
|
+
log,
|
|
505
|
+
startDirectory,
|
|
506
|
+
onFinished: ({ dir, scan }) => {
|
|
507
|
+
state.adviceAfterScan = scan && playlist.tracks.length === 0;
|
|
508
|
+
if (!scan && playlist.tracks.length === 0) showMessage(LOAD_ADVICE);
|
|
509
|
+
if (dir) reloadBrowserDirectory(dir);
|
|
510
|
+
if (scan) {
|
|
511
|
+
showMessage('Scanning library…');
|
|
512
|
+
library.scan().catch((error) => {
|
|
513
|
+
state.adviceAfterScan = false;
|
|
514
|
+
handleError(error);
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
// The modal is closed now: queued startup notices can finally own
|
|
518
|
+
// the toast slot (issues/063). Goes last so a real failure wins
|
|
519
|
+
// over the follow-up advice/scan messages.
|
|
520
|
+
showStartupNotice();
|
|
521
|
+
},
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
// --- Library panel ----------------------------------------------------------
|
|
525
|
+
// View stack, drill navigation and search input live in libraryPanel.js.
|
|
526
|
+
const libraryPanel = createLibraryPanel({
|
|
527
|
+
library,
|
|
528
|
+
player,
|
|
529
|
+
playlist,
|
|
530
|
+
els,
|
|
531
|
+
el,
|
|
532
|
+
getCursor: () => cursor(),
|
|
533
|
+
setCursor,
|
|
534
|
+
// While filtering the cursor is an index into the filtered view;
|
|
535
|
+
// resolve it through the map so Library actions hit the real row.
|
|
536
|
+
selectedRow: () => libraryPanel.rows()[realIndex()],
|
|
537
|
+
// Ctrl+F filter, wired here because the filter state lives in the
|
|
538
|
+
// controller (issues/040). Library renders its list itself.
|
|
539
|
+
filterRows: (rows) => applyFilter(rows, (item) => item.text),
|
|
540
|
+
showMessage,
|
|
541
|
+
refreshShortcuts: () => renderShortcuts(),
|
|
542
|
+
renderList: listView.render,
|
|
543
|
+
pathSuffix: filterSuffix,
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
// One Y/N prompt for every host (issues/031, /053, /057): the newest
|
|
547
|
+
// question replaces the old one, and the controller routes its keys
|
|
548
|
+
// ahead of the per-tab prompts.
|
|
549
|
+
const confirm = createConfirm({ showMessage, hideMessage });
|
|
550
|
+
|
|
551
|
+
// --- Settings panel -----------------------------------------------------------
|
|
552
|
+
// Sub-screen grouping of config rows lives in settingsPanel.js
|
|
553
|
+
// (issues/024); rows + labels come from Config's settingsViews. The
|
|
554
|
+
// Library view drills into the scan roots (issues/031).
|
|
555
|
+
const settingsPanel = createSettingsPanel({
|
|
556
|
+
config,
|
|
557
|
+
library,
|
|
558
|
+
analyzer,
|
|
559
|
+
syncVizNeeds,
|
|
560
|
+
els,
|
|
561
|
+
el,
|
|
562
|
+
getCursor: () => cursor(),
|
|
563
|
+
setCursor,
|
|
564
|
+
showMessage,
|
|
565
|
+
hideMessage,
|
|
566
|
+
confirm,
|
|
567
|
+
refreshShortcuts: () => renderShortcuts(),
|
|
568
|
+
onScanError: handleError,
|
|
569
|
+
renderList: listView.render,
|
|
570
|
+
applyTheme,
|
|
571
|
+
runOnboarding: () => onboarding.start(),
|
|
572
|
+
onMediaDirectoryChanged: reloadBrowserDirectory,
|
|
573
|
+
onArtStyleChanged: () => {
|
|
574
|
+
if (vizMode() === 'albumart' && state.currentSong?.images?.length) {
|
|
575
|
+
artPane.renderArt(state.currentSong);
|
|
576
|
+
}
|
|
577
|
+
},
|
|
578
|
+
startVisualizer,
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
// --- Playlist panel -----------------------------------------------------------
|
|
582
|
+
// Save prompt + open flow (issues/028) live in playlistPanel.js; the
|
|
583
|
+
// controller renders the prompt line and routes keys through the panel.
|
|
584
|
+
const playlistPanel = createPlaylistPanel({
|
|
585
|
+
playlist,
|
|
586
|
+
library,
|
|
587
|
+
browser,
|
|
588
|
+
config,
|
|
589
|
+
showMessage,
|
|
590
|
+
hideMessage,
|
|
591
|
+
confirm,
|
|
592
|
+
switchTab,
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
// --- Playlist totals (issues/046) ----------------------------------------------
|
|
596
|
+
// Duration cache + totals for the playlist header. The service probes
|
|
597
|
+
// only the tracks currently queued (playlist 'updated'), never the
|
|
598
|
+
// library; playback fills its own duration in via the 'play' handler.
|
|
599
|
+
const durations = createDurationService({ library, playlist });
|
|
600
|
+
durations.refresh();
|
|
601
|
+
|
|
602
|
+
// Terminal cell aspect (0.2.1 measureCell — issues/018 resolution):
|
|
603
|
+
// measureCell never returns the engine's 8×16 guess — it resolves null
|
|
604
|
+
// until the attach settles and when there is no answer, so the 2.0
|
|
605
|
+
// default stands until a real answer lands; cellchange re-fires when
|
|
606
|
+
// the measurement changes (first answer, font zoom).
|
|
607
|
+
function setCellRatio(ratio, source, px) {
|
|
608
|
+
DBG(`cell ratio ${ratio.toFixed(3)} via ${source}`);
|
|
609
|
+
log(
|
|
610
|
+
`Cell ${px.width}×${px.height} px → ratio ${ratio.toFixed(3)} (${source})`,
|
|
611
|
+
);
|
|
612
|
+
artPane.setCellRatio(ratio, px);
|
|
613
|
+
visualizer.setCellRatio(ratio);
|
|
614
|
+
splash.setCellRatio(ratio);
|
|
615
|
+
if (state.activeTab === SCREENS.MAIN) renderMain();
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
term.addEventListener('cellchange', (event) => {
|
|
619
|
+
setCellRatio(event.height / event.width, 'cellchange', {
|
|
620
|
+
width: event.width,
|
|
621
|
+
height: event.height,
|
|
622
|
+
});
|
|
623
|
+
});
|
|
624
|
+
attachReady
|
|
625
|
+
.then(() => term.measureCell())
|
|
626
|
+
.then((cell) => {
|
|
627
|
+
if (cell) {
|
|
628
|
+
setCellRatio(cell.height / cell.width, 'measureCell', cell);
|
|
629
|
+
} else {
|
|
630
|
+
log('measureCell: no answer, ratio stays 2.000');
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
// Terminal resize: the art/viz box is budgeted from win.innerWidth/
|
|
635
|
+
// innerHeight, so re-render to refit it (drawViz refits itself via the
|
|
636
|
+
// window-key check; renderArt refits via applyArtSize). The body
|
|
637
|
+
// ResizeObserver below covers this — body is min-height: 100vh, so it
|
|
638
|
+
// resizes with the terminal.
|
|
639
|
+
function render() {
|
|
640
|
+
// Repack the footer and remeasure the header first: every budget
|
|
641
|
+
// below (and the art box's flex height) reads the counts set here.
|
|
642
|
+
renderShortcuts();
|
|
643
|
+
renderStatus();
|
|
644
|
+
// Leaving the Main tab stops the splash (its rAF loop and mouse
|
|
645
|
+
// listeners only run while the field is on screen).
|
|
646
|
+
if (state.activeTab !== SCREENS.MAIN) splash.hide();
|
|
647
|
+
switch (state.activeTab) {
|
|
648
|
+
case SCREENS.MAIN:
|
|
649
|
+
renderMain();
|
|
650
|
+
break;
|
|
651
|
+
case SCREENS.BROWSER:
|
|
652
|
+
renderBrowser();
|
|
653
|
+
break;
|
|
654
|
+
case SCREENS.PLAYLIST:
|
|
655
|
+
renderPlaylist();
|
|
656
|
+
break;
|
|
657
|
+
case SCREENS.LIBRARY:
|
|
658
|
+
libraryPanel.render();
|
|
659
|
+
break;
|
|
660
|
+
case SCREENS.SETTINGS:
|
|
661
|
+
settingsPanel.render();
|
|
662
|
+
break;
|
|
663
|
+
case SCREENS.DEBUG:
|
|
664
|
+
renderDebug();
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function updateHeaderInfo() {
|
|
670
|
+
const vol = player.muted ? 'muted' : `${Math.round(player.volume * 100)}%`;
|
|
671
|
+
els.headerInfo.textContent = `Viz: ${vizMode()} · Vol: ${vol}`;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// One line of shortcuts: keys and labels separated by middle dots.
|
|
675
|
+
function shortcutLine(line) {
|
|
676
|
+
const div = el('div', 'shortcut-line');
|
|
677
|
+
line.forEach(([key, label], i) => {
|
|
678
|
+
if (i > 0) div.appendChild(document.createTextNode(' · '));
|
|
679
|
+
div.appendChild(el('kbd', null, key));
|
|
680
|
+
div.appendChild(document.createTextNode(` ${label}`));
|
|
681
|
+
});
|
|
682
|
+
return div;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// Pack both rows, measure the header, publish the sizes, draw. Called on
|
|
686
|
+
// tab switch, view change, drill and resize.
|
|
687
|
+
function renderShortcuts() {
|
|
688
|
+
const cols = Math.max(0, win.innerWidth - 2);
|
|
689
|
+
const globalLines = isSearchRoot()
|
|
690
|
+
? []
|
|
691
|
+
: packShortcuts(globalShortcuts(tabs.length), cols);
|
|
692
|
+
const localLines = packShortcuts(localShortcutEntries(), cols);
|
|
693
|
+
footerRows = localLines.length + globalLines.length + 2;
|
|
694
|
+
headerRows = Math.max(
|
|
695
|
+
1,
|
|
696
|
+
Math.round(els.tabbar.getBoundingClientRect().height),
|
|
697
|
+
);
|
|
698
|
+
const root = document.documentElement.style;
|
|
699
|
+
root.setProperty('--footer-rows', `${footerRows}px`);
|
|
700
|
+
root.setProperty('--header-rows', `${headerRows}px`);
|
|
701
|
+
els.shortcutsGlobal.replaceChildren(...globalLines.map(shortcutLine));
|
|
702
|
+
els.shortcutsLocal.replaceChildren(...localLines.map(shortcutLine));
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function renderStatus() {
|
|
706
|
+
let line;
|
|
707
|
+
if (player.isPlaying) {
|
|
708
|
+
const icon = player.isPaused ? '‖' : '▶';
|
|
709
|
+
if (state.currentSong) {
|
|
710
|
+
line = `${icon} ${state.currentSong.artist} - ${state.currentSong.title}`;
|
|
711
|
+
} else {
|
|
712
|
+
line = `${icon} Unknown track`;
|
|
713
|
+
}
|
|
714
|
+
} else {
|
|
715
|
+
line = `■ No track selected`;
|
|
716
|
+
}
|
|
717
|
+
els.status.textContent = line;
|
|
718
|
+
renderSeekBar();
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// Seek bar row (issues/038): <progress> + clock, plain DOM updated per
|
|
722
|
+
// timeUpdate (1 Hz while playing, frozen while paused — timeUpdate does
|
|
723
|
+
// not fire then). Unknown duration → no fraction to fill: bar hidden,
|
|
724
|
+
// elapsed-only clock. The footer pins rows to the bottom, so hiding the
|
|
725
|
+
// row leaves the old three-row footer geometry.
|
|
726
|
+
function renderSeekBar() {
|
|
727
|
+
const playing = player.isPlaying;
|
|
728
|
+
els.seekRow.classList.toggle('hidden', !playing);
|
|
729
|
+
if (!playing) return;
|
|
730
|
+
const known = state.duration > 0;
|
|
731
|
+
els.seekBar.classList.toggle('hidden', !known);
|
|
732
|
+
if (known) {
|
|
733
|
+
els.seekClock.textContent = `${formatTime(state.currentTime)} / ${formatTime(state.duration)}`;
|
|
734
|
+
els.seekBar.max = state.duration;
|
|
735
|
+
els.seekBar.value = state.currentTime;
|
|
736
|
+
} else {
|
|
737
|
+
els.seekClock.textContent = formatTime(state.currentTime);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// Label/value rows. columns=2 renders the main panel's two-column
|
|
742
|
+
// block layout — each column is its own block, so column 2 starts at
|
|
743
|
+
// one fixed offset no matter how long the values in column 1 run.
|
|
744
|
+
function renderMeta(container, pairs, columns = 1) {
|
|
745
|
+
container.replaceChildren();
|
|
746
|
+
const two = columns === 2;
|
|
747
|
+
const wrap = two ? el('div', 'meta-cols') : container;
|
|
748
|
+
const half = Math.ceil(pairs.length / columns);
|
|
749
|
+
for (let c = 0; c < columns; c++) {
|
|
750
|
+
const column = two ? el('div', 'meta-col') : wrap;
|
|
751
|
+
for (const [label, value] of pairs.slice(c * half, (c + 1) * half)) {
|
|
752
|
+
const pair = el('div', two ? 'meta-pair' : 'meta-row');
|
|
753
|
+
pair.appendChild(el('span', 'meta-label', label));
|
|
754
|
+
pair.appendChild(el('span', 'meta-value', value ?? ''));
|
|
755
|
+
column.appendChild(pair);
|
|
756
|
+
}
|
|
757
|
+
if (two) wrap.appendChild(column);
|
|
758
|
+
}
|
|
759
|
+
if (two) container.appendChild(wrap);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
function renderMain() {
|
|
763
|
+
const mode = vizMode();
|
|
764
|
+
const playing = player.isPlaying && !player.isPaused;
|
|
765
|
+
|
|
766
|
+
// Idle splash (issues/049): the letter field stands in for the art
|
|
767
|
+
// box when nothing is loaded or playback stopped; a paused track
|
|
768
|
+
// keeps its art and meta.
|
|
769
|
+
const idle = !player.isPlaying && !player.isPaused;
|
|
770
|
+
els.noTrack.classList.toggle('hidden', !idle);
|
|
771
|
+
els.trackMeta.classList.toggle('hidden', idle || !state.currentSong);
|
|
772
|
+
|
|
773
|
+
if (idle) {
|
|
774
|
+
artPane.hide();
|
|
775
|
+
splash.show();
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
splash.hide();
|
|
779
|
+
|
|
780
|
+
if (state.currentSong) {
|
|
781
|
+
// Column-major: first half fills column 1, second half column 2.
|
|
782
|
+
renderMeta(
|
|
783
|
+
els.trackMeta,
|
|
784
|
+
[
|
|
785
|
+
['Artist', state.currentSong.artist],
|
|
786
|
+
['Album', state.currentSong.album],
|
|
787
|
+
['Genre', state.currentSong.genre],
|
|
788
|
+
['Title', state.currentSong.title],
|
|
789
|
+
['Track', state.currentSong.trackNumber],
|
|
790
|
+
['Year', state.currentSong.year],
|
|
791
|
+
],
|
|
792
|
+
2,
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
if (mode !== 'albumart' && playing) {
|
|
797
|
+
artPane.drawViz();
|
|
798
|
+
} else {
|
|
799
|
+
artPane.renderArt(state.currentSong);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
function renderBrowser() {
|
|
804
|
+
els.browserPath.textContent = `Dir: ${browser.currentDirectory}${filterSuffix()}${goToBrowser.suffix()}`;
|
|
805
|
+
listView.render(els.browserList, listItems());
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// Totals suffix (issues/046): ` · 12:43:11 total · 9:12 left · 3
|
|
809
|
+
// unknown`. Elapsed of the playing track comes out of `left`; with
|
|
810
|
+
// nothing playing the full pass remains. Unknown durations are
|
|
811
|
+
// counted, never guessed; an all-unknown queue skips the time parts.
|
|
812
|
+
function playlistTotalsSuffix() {
|
|
813
|
+
const { total, left, unknown } = durations.totals({
|
|
814
|
+
elapsed: player.isPlaying ? state.currentTime : 0,
|
|
815
|
+
});
|
|
816
|
+
const parts = [];
|
|
817
|
+
if (total > 0) {
|
|
818
|
+
parts.push(`${formatLongTime(total)} total`);
|
|
819
|
+
parts.push(`${formatLongTime(left)} left`);
|
|
820
|
+
}
|
|
821
|
+
if (unknown > 0) parts.push(`${unknown} unknown`);
|
|
822
|
+
return parts.length > 0 ? ` · ${parts.join(' · ')}` : '';
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
function renderPlaylistHeader() {
|
|
826
|
+
const prompt = playlistPanel.getPrompt();
|
|
827
|
+
if (prompt) {
|
|
828
|
+
const { text, pos, base } = prompt;
|
|
829
|
+
els.playlistInfo.textContent = `Save playlist (from ${base}; Enter: save · Esc: cancel): ${text.slice(0, pos)}▌${text.slice(pos)}`;
|
|
830
|
+
} else {
|
|
831
|
+
els.playlistInfo.textContent =
|
|
832
|
+
`Playlist (${playlist.tracks.length} tracks${playlistTotalsSuffix()}) ` +
|
|
833
|
+
`Shuffle: ${playlist.shuffle ? 'ON' : 'OFF'} ` +
|
|
834
|
+
`Repeat: ${REPEAT_LABELS[playlist.repeat] ?? 'OFF'}${filterSuffix()}`;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
function renderPlaylist() {
|
|
839
|
+
renderPlaylistHeader();
|
|
840
|
+
els.playlistEmpty.classList.toggle('hidden', playlist.tracks.length > 0);
|
|
841
|
+
listView.render(els.playlistList, listItems());
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
function renderDebug() {
|
|
845
|
+
renderMeta(els.systemInfo, [
|
|
846
|
+
['Node', process.version],
|
|
847
|
+
['Platform', process.platform],
|
|
848
|
+
['Graphics', graphicsLabel()],
|
|
849
|
+
[
|
|
850
|
+
'Memory',
|
|
851
|
+
`${Math.round(process.memoryUsage().heapUsed / 1024 / 1024)} MB`,
|
|
852
|
+
],
|
|
853
|
+
['Messages', String(state.debugMessages.length)],
|
|
854
|
+
]);
|
|
855
|
+
listView.render(els.debugList, listItems());
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// --- Tabs ---------------------------------------------------------------
|
|
859
|
+
function switchTab(name) {
|
|
860
|
+
if (state.activeTab === name) return;
|
|
861
|
+
// Leaving the playlist tab cancels an open save prompt (issues/028).
|
|
862
|
+
if (state.activeTab === SCREENS.PLAYLIST) {
|
|
863
|
+
playlistPanel.cancelPrompt();
|
|
864
|
+
}
|
|
865
|
+
// Leaving the tab clears the filter (issues/040) — closeFilter's
|
|
866
|
+
// cursor restore is overwritten by the per-tab cursor resets below.
|
|
867
|
+
if (filter.open) closeFilter();
|
|
868
|
+
state.activeTab = name;
|
|
869
|
+
els.tabs.forEach((tab) => {
|
|
870
|
+
tab.classList.toggle('active', tab.dataset.tab === name);
|
|
871
|
+
});
|
|
872
|
+
els.panels.forEach((panel) => {
|
|
873
|
+
panel.classList.toggle('active', panel.id === `panel-${name}`);
|
|
874
|
+
});
|
|
875
|
+
if (name === SCREENS.PLAYLIST) {
|
|
876
|
+
setCursor(playlist.currentIndex !== -1 ? playlist.currentIndex : 0, name);
|
|
877
|
+
} else if (name === SCREENS.SETTINGS) {
|
|
878
|
+
setCursor(0, name);
|
|
879
|
+
} else if (name === SCREENS.BROWSER) {
|
|
880
|
+
browser.selectedIndex = Math.max(
|
|
881
|
+
0,
|
|
882
|
+
Math.min(browser.selectedIndex, browser.entries.length - 1),
|
|
883
|
+
);
|
|
884
|
+
} else if (name === SCREENS.LIBRARY) {
|
|
885
|
+
const max = libraryPanel.rows().length - 1;
|
|
886
|
+
setCursor(Math.max(0, Math.min(cursor(name), max)), name);
|
|
887
|
+
}
|
|
888
|
+
if (name === SCREENS.MAIN) {
|
|
889
|
+
if (player.isPlaying && !player.isPaused) startVisualizer();
|
|
890
|
+
} else {
|
|
891
|
+
stopVisualizer();
|
|
892
|
+
}
|
|
893
|
+
log(`Switched to tab: ${name}`);
|
|
894
|
+
render();
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
els.tabs.forEach((tab) => {
|
|
898
|
+
tab.addEventListener('click', () => switchTab(tab.dataset.tab));
|
|
899
|
+
});
|
|
900
|
+
|
|
901
|
+
// Click-to-seek (issues/038): the fraction math is ours — the engine's
|
|
902
|
+
// <progress> carries no click-to-value behaviour. Compare clientX with
|
|
903
|
+
// the host's own rect: the hit target may retarget out of the shadow
|
|
904
|
+
// bar/track parts, so the host box is the only stable denominator.
|
|
905
|
+
// Player.seek clamps the seconds (paused seeks and no-track no-ops
|
|
906
|
+
// included).
|
|
907
|
+
els.seekBar.addEventListener('click', (e) => {
|
|
908
|
+
if (!player.isPlaying || !(state.duration > 0)) return;
|
|
909
|
+
const rect = els.seekBar.getBoundingClientRect();
|
|
910
|
+
if (!(rect.width > 0)) return;
|
|
911
|
+
player.seek(((e.clientX - rect.left) / rect.width) * state.duration);
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
// --- Visualizer ---------------------------------------------------------------
|
|
915
|
+
|
|
916
|
+
// Map config onto the viz stack: which modes need the spectrum, and the
|
|
917
|
+
// render-detail bar cap for the current quality level. Future modes opt
|
|
918
|
+
// into the spectrum via their registry entry; the analyzer stays
|
|
919
|
+
// mode-agnostic.
|
|
920
|
+
function syncVizNeeds() {
|
|
921
|
+
analyzer.setSpectrumEnabled(Boolean(MODES[vizMode()]?.needsSpectrum));
|
|
922
|
+
const quality = config.get('visualizerQuality');
|
|
923
|
+
visualizer.setMaxBars(VIZ_BARS[quality] || VIZ_BARS.medium);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
function startVisualizer() {
|
|
927
|
+
// Paused/idle starts are no-ops (settings FPS row, event handlers).
|
|
928
|
+
DBG(
|
|
929
|
+
`viz start: playing=${player.isPlaying} paused=${player.isPaused} tab=${state.activeTab} mode=${vizMode()}`,
|
|
930
|
+
);
|
|
931
|
+
if (!player.isPlaying || player.isPaused) return;
|
|
932
|
+
stopVisualizer();
|
|
933
|
+
if (vizMode() === 'albumart') return;
|
|
934
|
+
|
|
935
|
+
state.vizHandler = (data) => visualizer.update(data);
|
|
936
|
+
analyzer.on('analysis', state.vizHandler);
|
|
937
|
+
|
|
938
|
+
const fps = config.get('visualizerFPS') || 30;
|
|
939
|
+
state.vizInterval = setInterval(
|
|
940
|
+
() => {
|
|
941
|
+
if (
|
|
942
|
+
state.activeTab === SCREENS.MAIN &&
|
|
943
|
+
player.isPlaying &&
|
|
944
|
+
!player.isPaused
|
|
945
|
+
) {
|
|
946
|
+
if (vizMode() !== 'albumart') {
|
|
947
|
+
artPane.drawViz();
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
},
|
|
951
|
+
Math.round(1000 / fps),
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
function stopVisualizer() {
|
|
956
|
+
if (state.vizInterval) {
|
|
957
|
+
clearInterval(state.vizInterval);
|
|
958
|
+
state.vizInterval = null;
|
|
959
|
+
}
|
|
960
|
+
if (state.vizHandler) {
|
|
961
|
+
analyzer.off('analysis', state.vizHandler);
|
|
962
|
+
state.vizHandler = null;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function cycleVisualizerMode() {
|
|
967
|
+
const nextMode =
|
|
968
|
+
VIZ_MODES[(VIZ_MODES.indexOf(vizMode()) + 1) % VIZ_MODES.length];
|
|
969
|
+
config.set('visualizerMode', nextMode);
|
|
970
|
+
visualizer.setMode(nextMode);
|
|
971
|
+
syncVizNeeds();
|
|
972
|
+
log(`Visualizer mode: ${nextMode}`);
|
|
973
|
+
if (player.isPlaying && !player.isPaused) {
|
|
974
|
+
startVisualizer();
|
|
975
|
+
} else {
|
|
976
|
+
stopVisualizer();
|
|
977
|
+
}
|
|
978
|
+
updateHeaderInfo();
|
|
979
|
+
render();
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// --- Toast ---------------------------------------------------------------
|
|
983
|
+
// Sticky toasts stay up until hideMessage() — the settings Y/N prompts
|
|
984
|
+
// (issues/031) need more than the standard 5s read time.
|
|
985
|
+
function showMessage(message, { sticky = false } = {}) {
|
|
986
|
+
if (!els.toast || !els.toastMsg) return;
|
|
987
|
+
els.toastMsg.textContent = message;
|
|
988
|
+
els.toast.classList.add('show');
|
|
989
|
+
if (state.toastTimer) clearTimeout(state.toastTimer);
|
|
990
|
+
state.toastTimer = sticky
|
|
991
|
+
? null
|
|
992
|
+
: setTimeout(() => {
|
|
993
|
+
els.toast.classList.remove('show');
|
|
994
|
+
state.toastTimer = null;
|
|
995
|
+
}, 5000);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
function hideMessage() {
|
|
999
|
+
if (state.toastTimer) {
|
|
1000
|
+
clearTimeout(state.toastTimer);
|
|
1001
|
+
state.toastTimer = null;
|
|
1002
|
+
}
|
|
1003
|
+
els.toast?.classList.remove('show');
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// One toast slot: stagger notices by the toast's own lifetime so a
|
|
1007
|
+
// directory fallback can't bury a playlist failure (issues/063).
|
|
1008
|
+
function showStartupNotice() {
|
|
1009
|
+
if (!startupNotices.length) return;
|
|
1010
|
+
showMessage(startupNotices.shift());
|
|
1011
|
+
if (startupNotices.length) setTimeout(showStartupNotice, 5000);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
// --- Actions ---------------------------------------------------------------
|
|
1015
|
+
function quit() {
|
|
1016
|
+
stopVisualizer();
|
|
1017
|
+
artPane.hide();
|
|
1018
|
+
splash.hide();
|
|
1019
|
+
state.currentSong?.artBitmap?.close?.();
|
|
1020
|
+
// Snapshot the session before tearing the engine down — the stored
|
|
1021
|
+
// position is the engine's byte counter (issues/027).
|
|
1022
|
+
saveSession({ player, playlist });
|
|
1023
|
+
player.stop();
|
|
1024
|
+
config.save();
|
|
1025
|
+
// Kill pending duration probes before the DB closes (issues/046).
|
|
1026
|
+
durations.stop();
|
|
1027
|
+
library.close();
|
|
1028
|
+
win.close();
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// Enter in the browser (issues/025): directories open (drill-in),
|
|
1032
|
+
// audio files replace the queue with every audio file in the current
|
|
1033
|
+
// listing and play the selection. Playlist containers (issues/028)
|
|
1034
|
+
// load into the queue instead — open, not merge, never autoplay.
|
|
1035
|
+
// Folder trees queue via `a` (issues/039). While the filter (issues/040)
|
|
1036
|
+
// is open the selection resolves through the index map, so Enter acts
|
|
1037
|
+
// on the filtered row, not the unfiltered position.
|
|
1038
|
+
function openBrowserEntry() {
|
|
1039
|
+
const selected = filteredBrowserEntry();
|
|
1040
|
+
if (!selected) return;
|
|
1041
|
+
if (selected.isPlaylist) {
|
|
1042
|
+
playlistPanel.openFile(selected.path);
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
// Entry-based on purpose: browser.openSelected() reads
|
|
1046
|
+
// browser.selectedIndex, which during filtering holds a filtered
|
|
1047
|
+
// index. Drill-in and playback use the resolved entry directly.
|
|
1048
|
+
if (selected.isDirectory) {
|
|
1049
|
+
if (!browser.loadDirectory(selected.path)) {
|
|
1050
|
+
showMessage(browser.lastError.error);
|
|
1051
|
+
}
|
|
1052
|
+
return;
|
|
1053
|
+
}
|
|
1054
|
+
if (selected.isAudio) {
|
|
1055
|
+
const paths = browser.findAudioFiles().map((file) => file.path);
|
|
1056
|
+
playlist.replaceAll(paths);
|
|
1057
|
+
playlist.select(paths.indexOf(selected.path));
|
|
1058
|
+
// When playing, the playlist trackChanged listener already
|
|
1059
|
+
// restarts the player; playing again here would emit a second
|
|
1060
|
+
// 'play' event and count the track twice in the library log.
|
|
1061
|
+
if (!player.isPlaying) player.play();
|
|
1062
|
+
showMessage(`Now playing: ${selected.name}`);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
// Alt+Enter / Alt+J on audio files queue at the bottom / after the
|
|
1067
|
+
// playing track; non-audio rows do nothing. Shift+Enter / Ctrl+Enter /
|
|
1068
|
+
// Ctrl+J are alternates (termdom 0.2.6, issues/036); legacy terminals
|
|
1069
|
+
// send bare \r / \n which keeps meaning play / ctrl+j.
|
|
1070
|
+
function queueBrowserSelection(afterCurrent) {
|
|
1071
|
+
const entry = filteredBrowserEntry();
|
|
1072
|
+
if (!entry?.isAudio) return;
|
|
1073
|
+
if (playlist.tracks.some((track) => track.path === entry.path)) {
|
|
1074
|
+
showMessage('Already queued');
|
|
1075
|
+
return;
|
|
1076
|
+
}
|
|
1077
|
+
if (afterCurrent) {
|
|
1078
|
+
const playing = playlist.currentIndex !== -1;
|
|
1079
|
+
playlist.insertNext(entry.path);
|
|
1080
|
+
showMessage(
|
|
1081
|
+
playing ? `Playing next: ${entry.name}` : `Queued: ${entry.name}`,
|
|
1082
|
+
);
|
|
1083
|
+
} else {
|
|
1084
|
+
playlist.add(entry.path);
|
|
1085
|
+
showMessage(`Queued: ${entry.name}`);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
// `a` (issues/039): add every audio file under the browsed directory,
|
|
1090
|
+
// recursing into subdirectories.
|
|
1091
|
+
async function addAllAudio() {
|
|
1092
|
+
const audioFiles = await browser.collectAudioFiles();
|
|
1093
|
+
if (audioFiles.length === 0) {
|
|
1094
|
+
showMessage('No audio files');
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
playlist.addMultiple(audioFiles.map((file) => file.path));
|
|
1098
|
+
showMessage(`Added ${audioFiles.length} files to playlist`);
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
// Add the browsed directory to the library scan roots (issues/023), then
|
|
1102
|
+
// offer a scan (issues/053) — even when it was already configured.
|
|
1103
|
+
function addLibraryDir() {
|
|
1104
|
+
settingsPanel.addDirAndAskScan(browser.currentDirectory);
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
// Star the selected track (issues/045, `f`).
|
|
1108
|
+
function toggleBrowserFavourite() {
|
|
1109
|
+
const entry = filteredBrowserEntry();
|
|
1110
|
+
if (!entry?.isAudio) return;
|
|
1111
|
+
const starred = library.toggleFavourite(entry.path);
|
|
1112
|
+
showMessage(starred ? `★ ${entry.name}` : `Unstarred ${entry.name}`);
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
function togglePlaylistFavourite() {
|
|
1116
|
+
const index = realIndex();
|
|
1117
|
+
const track = playlist.tracks[index];
|
|
1118
|
+
if (!track) return;
|
|
1119
|
+
const starred = library.toggleFavourite(track.path);
|
|
1120
|
+
showMessage(starred ? `★ ${track.name}` : `Unstarred ${track.name}`);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
function playSelectedTrack() {
|
|
1124
|
+
const index = realIndex();
|
|
1125
|
+
if (index >= 0 && index < playlist.length) {
|
|
1126
|
+
playlist.select(index);
|
|
1127
|
+
player.play();
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
function deleteSelectedTrack() {
|
|
1132
|
+
const index = realIndex();
|
|
1133
|
+
if (index >= 0 && index < playlist.length) {
|
|
1134
|
+
playlist.remove(index);
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
function moveTrack(delta) {
|
|
1139
|
+
// Filter mode (issues/040): the swap runs on the real index, the
|
|
1140
|
+
// filtered cursor stays put — the filtered order never changes.
|
|
1141
|
+
const index = realIndex();
|
|
1142
|
+
if (playlist.move(index, delta) && !filter.open) {
|
|
1143
|
+
setCursor(index + delta);
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
// Double-click on a list row (issues/044): same action as Enter on the
|
|
1148
|
+
// tab, modifiers mirroring Alt+Enter / Ctrl+Enter. Settings/debug rows
|
|
1149
|
+
// stay click-only — an accidental double-click must not apply a
|
|
1150
|
+
// setting, and the issue only asks for the three playback tabs.
|
|
1151
|
+
function activateRow(e) {
|
|
1152
|
+
switch (state.activeTab) {
|
|
1153
|
+
case SCREENS.BROWSER:
|
|
1154
|
+
if (e.altKey || e.shiftKey) queueBrowserSelection(false);
|
|
1155
|
+
else if (e.ctrlKey) queueBrowserSelection(true);
|
|
1156
|
+
else openBrowserEntry();
|
|
1157
|
+
break;
|
|
1158
|
+
case SCREENS.PLAYLIST:
|
|
1159
|
+
// The save prompt owns Enter (issues/028) — double-click
|
|
1160
|
+
// must not play under it either.
|
|
1161
|
+
if (!playlistPanel.getPrompt()) playSelectedTrack();
|
|
1162
|
+
break;
|
|
1163
|
+
case SCREENS.LIBRARY:
|
|
1164
|
+
if (e.altKey || e.shiftKey) libraryPanel.queueSelection();
|
|
1165
|
+
else if (e.ctrlKey) libraryPanel.queueNext();
|
|
1166
|
+
else libraryPanel.openEntry();
|
|
1167
|
+
break;
|
|
1168
|
+
}
|
|
1169
|
+
render();
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// --- Keyboard ---------------------------------------------------------------
|
|
1173
|
+
document.addEventListener('keydown', (e) => {
|
|
1174
|
+
const key = e.key;
|
|
1175
|
+
const plain = isPlain(e);
|
|
1176
|
+
|
|
1177
|
+
// TermDOM 0.2.0 scrolls the active panel (or the document) on Space and
|
|
1178
|
+
// the up/down/page keys unless the keydown handler cancels them. We own
|
|
1179
|
+
// the screen: cancel every key we consume so lists don't scroll under
|
|
1180
|
+
// the app — without this the engine double-actions play/volume/page
|
|
1181
|
+
// presses with panel scrolls.
|
|
1182
|
+
// Ctrl/alt/meta combos are left alone so Ctrl-C/Ctrl-Z keep their
|
|
1183
|
+
// engine defaults (exit 130 / suspend).
|
|
1184
|
+
if (
|
|
1185
|
+
plain &&
|
|
1186
|
+
(key === KEYS.PLAY_PAUSE ||
|
|
1187
|
+
key === KEYS.VOLUME_UP ||
|
|
1188
|
+
key === KEYS.VOLUME_DOWN ||
|
|
1189
|
+
key === KEYS.PAGE_UP ||
|
|
1190
|
+
key === KEYS.PAGE_DOWN ||
|
|
1191
|
+
key === KEYS.HOME ||
|
|
1192
|
+
key === KEYS.END ||
|
|
1193
|
+
(e.target && e.target.tagName === 'BUTTON' && key === KEYS.SELECT))
|
|
1194
|
+
) {
|
|
1195
|
+
e.preventDefault();
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// Onboarding modal (issues/041): while the dialog is up the flow
|
|
1199
|
+
// owns every key — Esc skips, arrows/Enter drive the steps; nothing
|
|
1200
|
+
// falls through to the tab handlers. key() always consumes once
|
|
1201
|
+
// isOpen() passed.
|
|
1202
|
+
if (onboarding.isOpen()) {
|
|
1203
|
+
onboarding.key(e);
|
|
1204
|
+
render();
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
// Library search input mode: printable keys build the query instead of
|
|
1209
|
+
// firing shortcuts ([ and ] still switch views; Backspace edits, Esc
|
|
1210
|
+
// clears/exits). Everything else — Enter, arrows, page keys — falls
|
|
1211
|
+
// through to the normal handling below.
|
|
1212
|
+
if (state.activeTab === SCREENS.LIBRARY && libraryPanel.isSearchRoot()) {
|
|
1213
|
+
if (libraryPanel.searchKey(e)) {
|
|
1214
|
+
render();
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
// Filter mode entry (issues/040): Ctrl+F on the browser/playlist
|
|
1220
|
+
// lists. Gated off while the playlist save prompt owns the tab.
|
|
1221
|
+
// While a filter is already open Ctrl+F is swallowed — Esc is the
|
|
1222
|
+
// exit, and falling through would fire the tab's favourite toggle.
|
|
1223
|
+
if (
|
|
1224
|
+
e.ctrlKey &&
|
|
1225
|
+
!e.metaKey &&
|
|
1226
|
+
!e.altKey &&
|
|
1227
|
+
key === 'f' &&
|
|
1228
|
+
!goToBrowser.isOpen() &&
|
|
1229
|
+
(state.activeTab === SCREENS.BROWSER ||
|
|
1230
|
+
state.activeTab === SCREENS.LIBRARY ||
|
|
1231
|
+
(state.activeTab === SCREENS.PLAYLIST && !playlistPanel.getPrompt()))
|
|
1232
|
+
) {
|
|
1233
|
+
if (!filter.open) openFilter();
|
|
1234
|
+
render();
|
|
1235
|
+
return;
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
// Media Directory prompt owns keys ahead of digits/globals and the
|
|
1239
|
+
// settings Y/N prompt (invariant 5).
|
|
1240
|
+
if (state.activeTab === SCREENS.SETTINGS && settingsPanel.promptKey(e)) {
|
|
1241
|
+
render();
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
// Y/N prompt (Library Dirs removal, issues/031; scan question,
|
|
1246
|
+
// issues/053): answers are consumed here wherever the question was
|
|
1247
|
+
// asked, so `n` declines instead of skipping tracks.
|
|
1248
|
+
if (settingsPanel.confirmKey(e)) {
|
|
1249
|
+
render();
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
// Playlist save prompt (issues/028): consumes everything so the
|
|
1254
|
+
// typed path can contain shortcut keys (q, w, s, digits…). key()
|
|
1255
|
+
// is false when no prompt is open.
|
|
1256
|
+
if (state.activeTab === SCREENS.PLAYLIST && playlistPanel.key(e)) {
|
|
1257
|
+
render();
|
|
1258
|
+
return;
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
// Browser go-to prompt (issues/051): typed paths may contain
|
|
1262
|
+
// shortcut letters/digits without firing them.
|
|
1263
|
+
if (goToBrowser.key(e)) {
|
|
1264
|
+
render();
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
// Filter input mode (issues/040): owns printable/editing keys while
|
|
1269
|
+
// open; navigation keys and the modified Enter variants fall
|
|
1270
|
+
// through to the handlers below.
|
|
1271
|
+
if (filter.open && filterKey(e)) {
|
|
1272
|
+
render();
|
|
1273
|
+
return;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
// Issue 062: plain only; Ctrl+F and the Enter/J combos were handled above.
|
|
1277
|
+
if (!plain && key !== KEYS.SELECT && key !== 'j') return;
|
|
1278
|
+
|
|
1279
|
+
if (key >= '1' && key <= String(tabs.length)) {
|
|
1280
|
+
switchTab(tabs[Number(key) - 1]);
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
switch (key) {
|
|
1285
|
+
case KEYS.QUIT:
|
|
1286
|
+
quit();
|
|
1287
|
+
return;
|
|
1288
|
+
case KEYS.PLAY_PAUSE:
|
|
1289
|
+
player.togglePlayPause();
|
|
1290
|
+
render();
|
|
1291
|
+
return;
|
|
1292
|
+
case KEYS.STOP:
|
|
1293
|
+
player.stop();
|
|
1294
|
+
render();
|
|
1295
|
+
return;
|
|
1296
|
+
case KEYS.NEXT:
|
|
1297
|
+
player.next();
|
|
1298
|
+
render();
|
|
1299
|
+
return;
|
|
1300
|
+
case KEYS.PREVIOUS:
|
|
1301
|
+
player.previous();
|
|
1302
|
+
render();
|
|
1303
|
+
return;
|
|
1304
|
+
case KEYS.MUTE:
|
|
1305
|
+
player.toggleMuted();
|
|
1306
|
+
renderStatus();
|
|
1307
|
+
return;
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
if (isListTab() && (key === KEYS.PAGE_UP || key === KEYS.PAGE_DOWN)) {
|
|
1311
|
+
listView.selectBy(key === KEYS.PAGE_UP ? -10 : 10);
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
// Home/End (issues/043): jump the cursor to the first/last row;
|
|
1316
|
+
// input modes (save prompt, filter) intercept above for their text
|
|
1317
|
+
// cursor.
|
|
1318
|
+
if (isListTab() && (key === KEYS.HOME || key === KEYS.END)) {
|
|
1319
|
+
listView.selectAt(key === KEYS.HOME ? 0 : Infinity);
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
if (key === KEYS.VOLUME_UP || key === KEYS.VOLUME_DOWN) {
|
|
1324
|
+
const delta =
|
|
1325
|
+
(key === KEYS.VOLUME_UP ? 1 : -1) * (config.get('volumeStep') / 100);
|
|
1326
|
+
if (state.activeTab === SCREENS.MAIN) {
|
|
1327
|
+
player.setVolume(Math.min(1, Math.max(0, player.volume + delta)));
|
|
1328
|
+
render();
|
|
1329
|
+
} else if (isListTab()) {
|
|
1330
|
+
listView.selectBy(delta > 0 ? -1 : 1);
|
|
1331
|
+
}
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
switch (state.activeTab) {
|
|
1336
|
+
case SCREENS.MAIN:
|
|
1337
|
+
if (key === KEYS.NAVIGATE_LEFT || key === KEYS.NAVIGATE_RIGHT) {
|
|
1338
|
+
// Seek-only during playback; idle = no-op. Tab switching
|
|
1339
|
+
// stays on 1-6 and mouse.
|
|
1340
|
+
if (player.isPlaying) {
|
|
1341
|
+
const delta =
|
|
1342
|
+
(key === KEYS.NAVIGATE_LEFT ? -1 : 1) * config.get('seekStep');
|
|
1343
|
+
player.seek(player.position + delta);
|
|
1344
|
+
}
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
if (key === 'v') cycleVisualizerMode();
|
|
1348
|
+
break;
|
|
1349
|
+
|
|
1350
|
+
case SCREENS.BROWSER:
|
|
1351
|
+
if (key === KEYS.NAVIGATE_LEFT) browser.goBack();
|
|
1352
|
+
else if (key === KEYS.NAVIGATE_RIGHT) {
|
|
1353
|
+
const selected = browser.getSelected();
|
|
1354
|
+
if (selected?.isDirectory && !browser.loadDirectory(selected.path)) {
|
|
1355
|
+
showMessage(browser.lastError.error);
|
|
1356
|
+
}
|
|
1357
|
+
} else if (key === 'g') {
|
|
1358
|
+
goToBrowser.start({
|
|
1359
|
+
base: browser.currentDirectory,
|
|
1360
|
+
onConfirm: (dir) => {
|
|
1361
|
+
if (!browser.loadDirectory(dir)) {
|
|
1362
|
+
return browser.lastError.error;
|
|
1363
|
+
}
|
|
1364
|
+
},
|
|
1365
|
+
});
|
|
1366
|
+
} else if (key === KEYS.SELECT && (e.altKey || e.shiftKey))
|
|
1367
|
+
queueBrowserSelection(false);
|
|
1368
|
+
else if (key === KEYS.SELECT && e.ctrlKey) queueBrowserSelection(true);
|
|
1369
|
+
else if (key === KEYS.SELECT) openBrowserEntry();
|
|
1370
|
+
else if (key === 'j' && (e.altKey || e.ctrlKey))
|
|
1371
|
+
queueBrowserSelection(true);
|
|
1372
|
+
else if (key === 'a') addAllAudio();
|
|
1373
|
+
else if (key === 'L') addLibraryDir();
|
|
1374
|
+
else if (key === 'f') toggleBrowserFavourite();
|
|
1375
|
+
break;
|
|
1376
|
+
|
|
1377
|
+
case SCREENS.PLAYLIST:
|
|
1378
|
+
if (key === KEYS.SELECT) playSelectedTrack();
|
|
1379
|
+
else if (key === KEYS.DELETE) deleteSelectedTrack();
|
|
1380
|
+
else if (key === 'u') moveTrack(-1);
|
|
1381
|
+
else if (key === 'd') moveTrack(1);
|
|
1382
|
+
else if (key === 'c') playlist.clear();
|
|
1383
|
+
else if (key === 'w') playlistPanel.startSavePrompt();
|
|
1384
|
+
else if (key === 'o') playlistPanel.openBrowser();
|
|
1385
|
+
else if (key === 'r') playlist.toggleRepeat();
|
|
1386
|
+
else if (key === 'h') playlist.toggleShuffle();
|
|
1387
|
+
else if (key === 'f') togglePlaylistFavourite();
|
|
1388
|
+
break;
|
|
1389
|
+
|
|
1390
|
+
case SCREENS.LIBRARY:
|
|
1391
|
+
if (key === '[') libraryPanel.cycleView(-1);
|
|
1392
|
+
else if (key === ']') libraryPanel.cycleView(1);
|
|
1393
|
+
else if (key === KEYS.SELECT && (e.altKey || e.shiftKey))
|
|
1394
|
+
libraryPanel.queueSelection();
|
|
1395
|
+
else if (
|
|
1396
|
+
key === 'j' ? e.altKey || e.ctrlKey : key === KEYS.SELECT && e.ctrlKey
|
|
1397
|
+
)
|
|
1398
|
+
libraryPanel.queueNext();
|
|
1399
|
+
else if (key === KEYS.SELECT) libraryPanel.openEntry();
|
|
1400
|
+
else if (key === KEYS.NAVIGATE_LEFT) libraryPanel.ascend();
|
|
1401
|
+
else if (
|
|
1402
|
+
key === KEYS.NAVIGATE_RIGHT &&
|
|
1403
|
+
!e.ctrlKey &&
|
|
1404
|
+
!e.altKey &&
|
|
1405
|
+
!e.metaKey
|
|
1406
|
+
)
|
|
1407
|
+
libraryPanel.openGroup();
|
|
1408
|
+
else if (key === 'a') {
|
|
1409
|
+
showMessage('Adding tracks...');
|
|
1410
|
+
// Paint the toast before the synchronous query + queue blocks
|
|
1411
|
+
// the event loop on a large library.
|
|
1412
|
+
win.requestAnimationFrame(() =>
|
|
1413
|
+
setTimeout(() => libraryPanel.addAll(), 0),
|
|
1414
|
+
);
|
|
1415
|
+
} else if (key === 'f') libraryPanel.toggleFavourite();
|
|
1416
|
+
else if (key === 't') libraryPanel.cycleWindow();
|
|
1417
|
+
else if (key === 'S') libraryPanel.cycleSort();
|
|
1418
|
+
else if (key === 'D') libraryPanel.flipSortDir();
|
|
1419
|
+
break;
|
|
1420
|
+
|
|
1421
|
+
case SCREENS.SETTINGS:
|
|
1422
|
+
if (key === '[') settingsPanel.cycleView(-1);
|
|
1423
|
+
else if (key === ']') settingsPanel.cycleView(1);
|
|
1424
|
+
else if (key === 'Escape' && settingsPanel.isDeviceList())
|
|
1425
|
+
settingsPanel.ascend();
|
|
1426
|
+
else if (key === KEYS.SELECT && settingsPanel.isDeviceList())
|
|
1427
|
+
settingsPanel.chooseDevice();
|
|
1428
|
+
else if (key === KEYS.SELECT && settingsPanel.isDrilled())
|
|
1429
|
+
settingsPanel.openDirEntry();
|
|
1430
|
+
else if (key === KEYS.SELECT) settingsPanel.applySelected();
|
|
1431
|
+
else if (key === KEYS.NAVIGATE_LEFT && settingsPanel.isDrilled())
|
|
1432
|
+
settingsPanel.ascend();
|
|
1433
|
+
else if (key === KEYS.NAVIGATE_LEFT) {
|
|
1434
|
+
// Value rows step back; action rows keep ← as Back/no-op.
|
|
1435
|
+
if (!settingsPanel.applySelected(-1)) settingsPanel.ascend();
|
|
1436
|
+
} else if (key === KEYS.NAVIGATE_RIGHT && !settingsPanel.isDrilled())
|
|
1437
|
+
settingsPanel.applySelected(1);
|
|
1438
|
+
else if (key === KEYS.DELETE) settingsPanel.removeSelected();
|
|
1439
|
+
break;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
render();
|
|
1443
|
+
});
|
|
1444
|
+
|
|
1445
|
+
// --- Component events ---------------------------------------------------------------
|
|
1446
|
+
// Star set for the browser/playlist icon columns (issues/045).
|
|
1447
|
+
function refreshFavPaths() {
|
|
1448
|
+
state.favPaths = new Set(library.favouritePaths());
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
player.on('play', (track, detail) => {
|
|
1452
|
+
DBG(`play event: ${track.name}`);
|
|
1453
|
+
// Count the play Winamp-style: once per track start, skips included.
|
|
1454
|
+
// The resume-start of a restored session does not count (issues/027).
|
|
1455
|
+
if (track?.path && !detail?.resume) library.recordPlay(track.path);
|
|
1456
|
+
// Learn the played duration (issues/046): metadata read on play
|
|
1457
|
+
// carries it; fills the cache for files outside scanned roots.
|
|
1458
|
+
if (track?.path && player.duration > 0) {
|
|
1459
|
+
library.learnDurations([{ path: track.path, seconds: player.duration }]);
|
|
1460
|
+
}
|
|
1461
|
+
log(`Now playing: ${track.name}`);
|
|
1462
|
+
startVisualizer();
|
|
1463
|
+
render();
|
|
1464
|
+
});
|
|
1465
|
+
player.on('pause', () => {
|
|
1466
|
+
stopVisualizer();
|
|
1467
|
+
render();
|
|
1468
|
+
});
|
|
1469
|
+
player.on('resume', () => {
|
|
1470
|
+
startVisualizer();
|
|
1471
|
+
render();
|
|
1472
|
+
});
|
|
1473
|
+
player.on('stop', () => {
|
|
1474
|
+
stopVisualizer();
|
|
1475
|
+
analyzer.reset();
|
|
1476
|
+
render();
|
|
1477
|
+
});
|
|
1478
|
+
player.on('timeUpdate', (currentTime, duration) => {
|
|
1479
|
+
state.currentTime = currentTime;
|
|
1480
|
+
state.duration = duration;
|
|
1481
|
+
renderStatus();
|
|
1482
|
+
// Remaining time (issues/046) shifts with the elapsed part —
|
|
1483
|
+
// header text only, the list window does not move.
|
|
1484
|
+
if (state.activeTab === SCREENS.PLAYLIST) renderPlaylistHeader();
|
|
1485
|
+
});
|
|
1486
|
+
player.on('metadataUpdate', handleMetadataUpdate);
|
|
1487
|
+
player.on('muteChange', updateHeaderInfo);
|
|
1488
|
+
player.on('volumeChange', updateHeaderInfo);
|
|
1489
|
+
// Playback failures skip the track and keep the app alive (issues/026);
|
|
1490
|
+
// handleError stays wired only as the last-resort handler for
|
|
1491
|
+
// non-playback faults.
|
|
1492
|
+
player.on('trackError', (track, err) => {
|
|
1493
|
+
DBG(`trackError: ${track?.name}: ${err?.message}`);
|
|
1494
|
+
log(`Playback error (${track?.name}): ${err?.message}`);
|
|
1495
|
+
showMessage(`Skipped unreadable: ${track?.name}`);
|
|
1496
|
+
});
|
|
1497
|
+
player.on('playbackStalled', (count) => {
|
|
1498
|
+
DBG(`playbackStalled: ${count}`);
|
|
1499
|
+
log(`${count} unreadable tracks — stopped`);
|
|
1500
|
+
showMessage(`${count} unreadable tracks — stopped`);
|
|
1501
|
+
});
|
|
1502
|
+
// Unplayable selected device (issues/047): one toast per id, playback
|
|
1503
|
+
// falls back to the system default.
|
|
1504
|
+
player.on('deviceUnavailable', (id) => {
|
|
1505
|
+
log(`Audio device unavailable (${id}) — using system default`);
|
|
1506
|
+
showMessage(
|
|
1507
|
+
`Audio device ${friendlyDeviceName(id) ?? id} unavailable — using system default`,
|
|
1508
|
+
);
|
|
1509
|
+
});
|
|
1510
|
+
// speaker load canary (player): the ffmpeg engine cannot run on systems
|
|
1511
|
+
// whose system default the binding cannot open — play continues on
|
|
1512
|
+
// webaudio for the session.
|
|
1513
|
+
player.on('engineFallback', (from, reason) => {
|
|
1514
|
+
if (reason === 'speaker-unavailable') {
|
|
1515
|
+
const tools =
|
|
1516
|
+
process.platform === 'win32'
|
|
1517
|
+
? ' (Visual Studio Build Tools + Python)'
|
|
1518
|
+
: '';
|
|
1519
|
+
log(
|
|
1520
|
+
`Audio engine ${from} could not load speaker${tools} — using WebAudio`,
|
|
1521
|
+
);
|
|
1522
|
+
showMessage(
|
|
1523
|
+
`Default audio engine did not install${tools} — using WebAudio; see README → Troubleshooting`,
|
|
1524
|
+
);
|
|
1525
|
+
return;
|
|
1526
|
+
}
|
|
1527
|
+
log(`Audio engine ${from} unusable (system default) — using WebAudio`);
|
|
1528
|
+
showMessage('System audio default unusable — using WebAudio output');
|
|
1529
|
+
});
|
|
1530
|
+
player.on('error', handleError);
|
|
1531
|
+
// Engine/player debug chatter (spawn lines, format, sync-lead
|
|
1532
|
+
// estimates, viz pool stats) goes to the debug file only — the footer
|
|
1533
|
+
// is for user-visible status.
|
|
1534
|
+
player.on('debug', (msg) => DBG(msg));
|
|
1535
|
+
|
|
1536
|
+
library.on('scanProgress', (done, total) => {
|
|
1537
|
+
showMessage(`Scanning library: ${done}/${total}`);
|
|
1538
|
+
if (state.activeTab === SCREENS.LIBRARY) render();
|
|
1539
|
+
});
|
|
1540
|
+
library.on('scanDone', ({ indexed, failed = 0 }) => {
|
|
1541
|
+
const empty = !playlist.tracks.length;
|
|
1542
|
+
const advice = state.adviceAfterScan && empty ? ` — ${LOAD_ADVICE}` : '';
|
|
1543
|
+
state.adviceAfterScan = false;
|
|
1544
|
+
showMessage(
|
|
1545
|
+
`Library scan complete: ${indexed} tracks${failed ? ` (${failed} unreadable)` : ''}${advice}`,
|
|
1546
|
+
);
|
|
1547
|
+
// Scan changed list membership; drilled snapshots are stale. The
|
|
1548
|
+
// settings dir list shows index counts, so it refreshes too.
|
|
1549
|
+
if (state.activeTab === SCREENS.LIBRARY) {
|
|
1550
|
+
libraryPanel.reset();
|
|
1551
|
+
render();
|
|
1552
|
+
} else if (state.activeTab === SCREENS.SETTINGS) {
|
|
1553
|
+
render();
|
|
1554
|
+
}
|
|
1555
|
+
});
|
|
1556
|
+
// Any index change (play counted, star toggled — issues/045) refreshes
|
|
1557
|
+
// the star set and the active view; drilled snapshots re-slice since
|
|
1558
|
+
// track rows carry the favourite flag (Favourites membership shifts).
|
|
1559
|
+
refreshFavPaths();
|
|
1560
|
+
library.on('updated', () => {
|
|
1561
|
+
refreshFavPaths();
|
|
1562
|
+
if (state.activeTab === SCREENS.LIBRARY) libraryPanel.reslice();
|
|
1563
|
+
if (isListTab()) render();
|
|
1564
|
+
});
|
|
1565
|
+
|
|
1566
|
+
playlist.on('updated', () => {
|
|
1567
|
+
if (state.activeTab === SCREENS.PLAYLIST) {
|
|
1568
|
+
// Filter mode (issues/040) owns the cursor as a filtered index;
|
|
1569
|
+
// a real-index sync here would corrupt it.
|
|
1570
|
+
if (!filter.open) {
|
|
1571
|
+
setCursor(
|
|
1572
|
+
playlist.currentIndex !== -1 ? playlist.currentIndex : 0,
|
|
1573
|
+
SCREENS.PLAYLIST,
|
|
1574
|
+
);
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
render();
|
|
1578
|
+
});
|
|
1579
|
+
playlist.on('trackChanged', () => {
|
|
1580
|
+
if (state.activeTab === SCREENS.PLAYLIST) {
|
|
1581
|
+
if (!filter.open) {
|
|
1582
|
+
setCursor(playlist.currentIndex, SCREENS.PLAYLIST);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
render();
|
|
1586
|
+
});
|
|
1587
|
+
|
|
1588
|
+
browser.on('directoryLoaded', (directory) => {
|
|
1589
|
+
log(`Loaded directory: ${directory}`);
|
|
1590
|
+
render();
|
|
1591
|
+
});
|
|
1592
|
+
browser.on('fileSelected', (file) => {
|
|
1593
|
+
log(`Selected file: ${file.name}`);
|
|
1594
|
+
});
|
|
1595
|
+
|
|
1596
|
+
// --- Metadata ---------------------------------------------------------------
|
|
1597
|
+
function handleMetadataUpdate(metadata) {
|
|
1598
|
+
DBG(`metadataUpdate: ${metadata.title}`);
|
|
1599
|
+
state.currentSong = metadata;
|
|
1600
|
+
const { images, ...meta } = metadata;
|
|
1601
|
+
log(`Loaded metadata: ${JSON.stringify(meta)}`);
|
|
1602
|
+
render();
|
|
1603
|
+
lookupFolderArt(metadata);
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
// Folder-art fallback (display only): a track without an embedded
|
|
1607
|
+
// picture borrows Cover.jpg/Folder.jpg from its own directory. The
|
|
1608
|
+
// lookup is fire-and-forget — the metadata paint above never waits for
|
|
1609
|
+
// the disk — and a stale song is dropped, not repainted.
|
|
1610
|
+
async function lookupFolderArt(metadata) {
|
|
1611
|
+
if (metadata.images?.length) return;
|
|
1612
|
+
const track = playlist.current;
|
|
1613
|
+
if (!track?.path) return;
|
|
1614
|
+
const img = await findFolderArt(path.dirname(track.path));
|
|
1615
|
+
if (!img || state.currentSong !== metadata || playlist.current !== track) {
|
|
1616
|
+
return;
|
|
1617
|
+
}
|
|
1618
|
+
metadata.images = [img];
|
|
1619
|
+
DBG(`folder art: ${track.path}`);
|
|
1620
|
+
if (state.activeTab === SCREENS.MAIN) renderMain();
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
function handleError(error) {
|
|
1624
|
+
DBG(`error event: ${error?.message}`);
|
|
1625
|
+
stopVisualizer();
|
|
1626
|
+
term.dispose().then(() => {
|
|
1627
|
+
console.error(error);
|
|
1628
|
+
process.exit(1);
|
|
1629
|
+
});
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
// --- Resize ---------------------------------------------------------------
|
|
1633
|
+
// Sizing is CSS-driven: the <img> reflows via object-fit, the cellgrid
|
|
1634
|
+
// canvas re-derives cols/rows from its box and repaints through render().
|
|
1635
|
+
new win.ResizeObserver(() => {
|
|
1636
|
+
render();
|
|
1637
|
+
}).observe(document.body);
|
|
1638
|
+
|
|
1639
|
+
// --- Initial paint ---------------------------------------------------------------
|
|
1640
|
+
applyTheme();
|
|
1641
|
+
updateHeaderInfo();
|
|
1642
|
+
render();
|
|
1643
|
+
|
|
1644
|
+
// First run (issues/041): a freshly created config.json, or an explicit
|
|
1645
|
+
// `onboarded: false` re-arming the flow. Legacy configs (file exists,
|
|
1646
|
+
// flag absent) never nag.
|
|
1647
|
+
if (config.needsOnboarding()) {
|
|
1648
|
+
onboarding.start();
|
|
1649
|
+
} else {
|
|
1650
|
+
showStartupNotice();
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
return { cleanup: quit };
|
|
1654
|
+
}
|