diff --git a/ui/app.js b/ui/app.js index 687d28c..796b7fd 100644 --- a/ui/app.js +++ b/ui/app.js @@ -240,6 +240,7 @@ async function init() { bindUpdates(); bindPlaybackSettings(); bindSearch(); + bindTools(); bindStats(); await listen("atmosphere://job", (e) => onJob(e.payload)); await listen("atmosphere://probe", (e) => onProbe(e.payload)); diff --git a/ui/icons.js b/ui/icons.js index 9129678..1e2e376 100644 --- a/ui/icons.js +++ b/ui/icons.js @@ -1,2 +1,2 @@ /* atmosphere · icons.js · Tabler icon set (MIT, via Iconify) bundled as inline SVG markup for the UI. */ -window.ICONS = {"home":"","layout-sidebar-left-expand":"","layout-sidebar-left-collapse":"","picture-in-picture":"","arrows-maximize":"","pinned":"","pin":"","droplet":"","grip-vertical":"","adjustments-horizontal":"","alert-circle":"","arrow-down-circle":"","arrow-left":"","arrow-right":"","arrows-shuffle":"","arrows-sort":"","brand-bandcamp":"","brand-instagram":"","brand-soundcloud":"","brand-tiktok":"","brand-twitch":"","brand-vimeo":"","brand-x":"","brand-youtube":"","calendar":"","chart-bar":"","check":"","chevron-down":"","chevron-left":"","chevron-right":"","circle-check":"","clipboard":"","clock":"","cloud-download":"","copy":"","device-tv":"","disc-filled":"","disc":"","dots":"","download":"","external-link":"","file-music":"","flame":"","folder-open":"","folder-plus":"","folder":"","headphones":"","heart-filled":"","heart":"","info-circle":"","layout-grid":"","link":"","list-details":"","list":"","loader-2":"","maximize":"","microphone-2":"","microphone":"","minimize":"","music":"","pencil":"","photo-down":"","photo":"","player-pause-filled":"","player-play-filled":"","player-play":"","player-skip-back-filled":"","player-skip-forward-filled":"","playlist-add":"","playlist":"","plus":"","refresh":"","repeat-once":"","repeat":"","scissors":"","search":"","settings":"","share":"","sparkles":"","trash":"","user-circle":"","user":"","volume-off":"","volume":"","wave-sine":"","world":"","x":""}; +window.ICONS = {"moon":"","home":"","layout-sidebar-left-expand":"","layout-sidebar-left-collapse":"","picture-in-picture":"","arrows-maximize":"","pinned":"","pin":"","droplet":"","grip-vertical":"","adjustments-horizontal":"","alert-circle":"","arrow-down-circle":"","arrow-left":"","arrow-right":"","arrows-shuffle":"","arrows-sort":"","brand-bandcamp":"","brand-instagram":"","brand-soundcloud":"","brand-tiktok":"","brand-twitch":"","brand-vimeo":"","brand-x":"","brand-youtube":"","calendar":"","chart-bar":"","check":"","chevron-down":"","chevron-left":"","chevron-right":"","circle-check":"","clipboard":"","clock":"","cloud-download":"","copy":"","device-tv":"","disc-filled":"","disc":"","dots":"","download":"","external-link":"","file-music":"","flame":"","folder-open":"","folder-plus":"","folder":"","headphones":"","heart-filled":"","heart":"","info-circle":"","layout-grid":"","link":"","list-details":"","list":"","loader-2":"","maximize":"","microphone-2":"","microphone":"","minimize":"","music":"","pencil":"","photo-down":"","photo":"","player-pause-filled":"","player-play-filled":"","player-play":"","player-skip-back-filled":"","player-skip-forward-filled":"","playlist-add":"","playlist":"","plus":"","refresh":"","repeat-once":"","repeat":"","scissors":"","search":"","settings":"","share":"","sparkles":"","trash":"","user-circle":"","user":"","volume-off":"","volume":"","wave-sine":"","world":"","x":""}; diff --git a/ui/index.html b/ui/index.html index 8fa0988..333d072 100644 --- a/ui/index.html +++ b/ui/index.html @@ -341,6 +341,7 @@ + @@ -396,5 +397,6 @@ + diff --git a/ui/library.js b/ui/library.js index 22135a4..3676c6c 100644 --- a/ui/library.js +++ b/ui/library.js @@ -365,6 +365,7 @@ function trackMenu(e, t, ctx, i) { albumKey && { label: "Go to album", run: () => openLib("album", albumKey) }, { label: "Go to artist", run: () => openLib("artist", lnorm(t.albumArtist || primaryArtist(t.artist))) }, "-", + { label: "Song info…", run: () => songInfo(t) }, { label: "Edit info…", run: () => editInfo(t) }, { label: "Show lyrics", run: () => { if (Player.current?.id !== t.id) playList(ctx.list, i); openLyrics(); } }, { label: "Show in folder", run: () => invoke("reveal", { path: t.path }) }, diff --git a/ui/nowplaying.js b/ui/nowplaying.js index 5e4143e..5925531 100644 --- a/ui/nowplaying.js +++ b/ui/nowplaying.js @@ -253,7 +253,7 @@ function renderQueue() { cover(t.art, "lt-cover"), h("div", { class: "lt-main" }, h("span", { class: "lt-title" }, t.title), h("span", { class: "lt-sub" }, t.artist)), isNow ? h("span", { class: "flag" }, "Now") : h("button", { class: "icon-btn", "aria-label": `Remove ${t.title}`, onclick: () => Player.removeAt(i) }, icon("x"))); panel.replaceChildren( - h("div", { class: "drawer-head" }, h("h2", {}, "Queue"), h("div", { class: "row" }, next.length ? h("button", { class: "btn small ghost", onclick: () => Player.clearUpNext() }, "Clear") : null, h("button", { class: "icon-btn", "aria-label": "Close", onclick: closeDrawers }, icon("x")))), + h("div", { class: "drawer-head" }, h("h2", {}, "Queue"), h("div", { class: "row" }, Player.queue.length > 1 ? h("button", { class: "btn small ghost", title: "Save the queue as a playlist", onclick: saveQueueAsPlaylist }, icon("playlist-add"), "Save") : null, next.length ? h("button", { class: "btn small ghost", onclick: () => Player.clearUpNext() }, "Clear") : null, h("button", { class: "icon-btn", "aria-label": "Close", onclick: closeDrawers }, icon("x")))), h("div", { class: "drawer-body" }, now ? row(now, Player.index, true) : h("p", { class: "muted pad" }, "Nothing playing."), next.length ? h("h3", { class: "q-sub" }, `Up next · ${next.length}`) : null, diff --git a/ui/player.js b/ui/player.js index c04ae0c..1c17e73 100644 --- a/ui/player.js +++ b/ui/player.js @@ -37,6 +37,7 @@ const Player = { standbyIndex: -1, deckNodes: null, transitionTimer: 0, + stopAfterCurrent: false, crossfade: 0, leveling: false, ctx: null, @@ -290,7 +291,7 @@ const Player = { scheduleTransition() { const a = this.audio; - if (this.transitionTimer || a.paused || this.standbyIndex < 0 || !isFinite(a.duration)) return; + if (this.transitionTimer || this.stopAfterCurrent || a.paused || this.standbyIndex < 0 || !isFinite(a.duration)) return; const rate = a.playbackRate || 1; const remaining = (a.duration - a.currentTime) / rate; const fade = this.fadeFor(); @@ -596,6 +597,12 @@ Player.on = (type, fn) => Player.decks.forEach((el) => el.addEventListener(type, on("ended", () => { Player.cancelTransition(); Player.flushListen(); + if (Player.stopAfterCurrent) { + Player.stopAfterCurrent = false; + Player.emit("state"); + Player.emit("sleep"); + return; + } if (Player.standbyIndex >= 0 && Player.standbyIndex === Player.nextIndex()) Player.load(Player.standbyIndex); else Player.next(true); }); diff --git a/ui/styles.css b/ui/styles.css index 892d949..678b074 100644 --- a/ui/styles.css +++ b/ui/styles.css @@ -1129,3 +1129,15 @@ body.mini-mode > *:not(#mini):not(.toasts):not(.modal-wrap):not(script) { displa .about .source { margin-top: var(--s2); font-size: 12px; } .row.between { justify-content: space-between; align-items: center; gap: var(--s3); } .row.between p { margin: 0; flex: 1; } + +/* song info and sleep */ +.info-head { display: grid; grid-template-columns: 64px minmax(0, 1fr); gap: var(--s3); align-items: center; } +.info-art { width: 64px; height: 64px; border-radius: 8px; object-fit: cover; } +.info-title { font: 650 16px/1.3 var(--font-display); } +.stack.tight { gap: 2px; } +.info-grid { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 6px var(--s4); margin: 0; font-size: 13px; } +.info-grid dt { color: var(--text-3); } +.info-grid dd { margin: 0; color: var(--text); overflow-wrap: anywhere; } +.info-path { display: grid; gap: var(--s2); justify-items: start; } +.info-path code { font-size: 12px; color: var(--text-2); overflow-wrap: anywhere; padding: 6px 10px; border-radius: 8px; background: var(--surface-2, var(--surface)); border: 1px solid var(--hairline); width: 100%; box-sizing: border-box; } +#sleepBtn[aria-pressed="true"] { color: var(--accent); } diff --git a/ui/tools.js b/ui/tools.js new file mode 100644 index 0000000..5435978 --- /dev/null +++ b/ui/tools.js @@ -0,0 +1,177 @@ +// atmosphere · tools.js · Everyday player tools: song info, save the queue as a playlist, sleep timer and resuming long tracks +const Sleep = { until: 0, endOfSong: false, timer: 0, tick: 0, fading: false }; +const RESUME_KEY = "atmo.resume"; +const RESUME_MIN_MS = 20 * 60 * 1000; + +function fmtDate(ms) { + return ms ? new Intl.DateTimeFormat(undefined, { dateStyle: "medium", timeStyle: "short" }).format(new Date(ms)) : "Never"; +} + +function songInfo(t) { + const last = Lib.data?.lastPlayed?.[t.id]; + const rate = t.sampleRate ? `${(t.sampleRate / 1000).toFixed(t.sampleRate % 1000 ? 1 : 0)} kHz` : null; + const ch = t.channels === 1 ? "mono" : t.channels === 2 ? "stereo" : t.channels ? `${t.channels} channels` : null; + const rows = [ + ["Format", [String(t.format || "").toUpperCase(), t.bitrate ? `${t.bitrate} kbps` : null, rate, ch].filter(Boolean).join(" · ")], + ["Length", fmtDur(t.durationMs || 0)], + ["Size", t.size ? fmtBytes(t.size) : null], + ["Album", [t.album, t.trackNo ? `track ${t.trackNo}${t.trackTotal ? ` of ${t.trackTotal}` : ""}` : null, t.discNo > 1 ? `disc ${t.discNo}` : null].filter(Boolean).join(" · ") || null], + ["Year", t.year ? String(t.year) : null], + ["Genre", t.genre || null], + ["Plays", String(Lib.plays[t.id] || 0)], + ["Last played", last ? fmtDate(last * (last < 1e12 ? 1000 : 1)) : "Never"], + ["Added", t.added ? fmtDate(t.added * (t.added < 1e12 ? 1000 : 1)) : null], + ["Lyrics", t.hasLyrics ? "Saved with the song" : "Not saved yet"], + ["Loudness", typeof t.gainDb === "number" ? `${t.gainDb > 0 ? "+" : ""}${t.gainDb} dB to even out` : "Not measured yet"], + ["Playlist", t.playlist || null], + ].filter(([, v]) => v); + const body = h("div", { class: "stack" }, + h("div", { class: "info-head" }, cover(t.art, "info-art", t.title), h("div", { class: "stack tight" }, h("strong", { class: "info-title" }, t.title), h("span", { class: "muted" }, t.artist || "Unknown artist"))), + h("dl", { class: "info-grid" }, ...rows.flatMap(([k, v]) => [h("dt", {}, k), h("dd", {}, v)])), + h("div", { class: "info-path" }, h("code", {}, t.path), h("button", { class: "btn small", type: "button", onclick: () => invoke("reveal", { path: t.path }) }, icon("folder-open"), "Show in folder"))); + modal("Song info", body, [{ label: "Edit info…", value: "edit" }, { label: "Close", value: false, primary: true }]).then((v) => v === "edit" && editInfo(t)); +} + +async function saveQueueAsPlaylist() { + const tracks = Player.queue.filter(Boolean); + if (!tracks.length) return toast("The queue is empty.", ""); + const d = new Date(); + const input = h("input", { class: "text wide", value: `Queue ${d.toLocaleDateString()}`, "aria-label": "Playlist name", spellcheck: "false" }); + const ok = await modal("Save queue as a playlist", h("div", { class: "stack" }, input, h("p", { class: "muted small" }, `${plural(tracks.length, "song")}, in the order they're queued.`)), [{ label: "Cancel", value: false }, { label: "Save", value: true, primary: true }]); + const name = input.value.trim(); + if (!ok || !name) return; + try { + const created = await invoke("playlist_create", { name }); + await invoke("playlist_add", { name: created, ids: tracks.map((t) => t.id) }); + toast(`Saved “${created}”`, "ok"); + } catch (e) { + toast(errText(e), "err"); + } +} + +function sleepActive() { + return Sleep.endOfSong || Sleep.until > Date.now(); +} + +function sleepLabel() { + if (Sleep.endOfSong) return "Stops after this song"; + const left = Math.max(0, Sleep.until - Date.now()); + const m = Math.ceil(left / 60000); + return m >= 60 ? `Stops in ${Math.floor(m / 60)} h ${m % 60} min` : `Stops in ${m} min`; +} + +function renderSleep() { + const b = $("#sleepBtn"); + if (!b) return; + const on = sleepActive(); + b.setAttribute("aria-pressed", String(on)); + b.title = on ? `Sleep timer: ${sleepLabel().toLowerCase()}` : "Sleep timer"; +} + +function cancelSleep(quiet = false) { + clearTimeout(Sleep.timer); + clearInterval(Sleep.tick); + Sleep.until = 0; + Sleep.endOfSong = false; + Player.stopAfterCurrent = false; + renderSleep(); + if (!quiet) toast("Sleep timer off", ""); +} + +async function sleepNow() { + if (Sleep.fading) return; + Sleep.fading = true; + const start = Player.volume; + const steps = 40; + for (let i = 1; i <= steps; i++) { + await new Promise((r) => setTimeout(r, 200)); + Player.setVolume(start * (1 - i / steps)); + } + Player.pause(); + Player.setVolume(start); + Sleep.fading = false; + cancelSleep(true); + toast("Sleep timer stopped the music. Good night.", ""); +} + +function setSleep(minutes) { + cancelSleep(true); + if (minutes === "song") { + Sleep.endOfSong = true; + Player.stopAfterCurrent = true; + Player.cancelTransition(); + } else { + Sleep.until = Date.now() + minutes * 60000; + Sleep.timer = setTimeout(sleepNow, Math.max(0, minutes * 60000 - 8000)); + Sleep.tick = setInterval(renderSleep, 30000); + } + renderSleep(); + toast(sleepLabel(), "ok"); +} + +function sleepMenu(e) { + e.stopPropagation(); + const r = e.currentTarget.getBoundingClientRect(); + const opt = (label, v) => ({ label, run: () => setSleep(v) }); + menuAt(r.left, r.top - 8, [ + sleepActive() ? { label: `${sleepLabel()} · Turn off`, run: () => cancelSleep() } : null, + sleepActive() ? "-" : null, + opt("15 minutes", 15), opt("30 minutes", 30), opt("45 minutes", 45), opt("1 hour", 60), opt("2 hours", 120), + opt("End of this song", "song"), + ].filter(Boolean)); +} + +function resumeMap() { + try { + return JSON.parse(localStorage.getItem(RESUME_KEY) || "{}"); + } catch { + return {}; + } +} + +function saveResume() { + const t = Player.current; + const a = Player.audio; + if (!t || (t.durationMs || 0) < RESUME_MIN_MS || !isFinite(a.duration)) return; + const map = resumeMap(); + if (a.currentTime > 30 && a.currentTime < a.duration * 0.97) map[t.id] = Math.floor(a.currentTime); + else delete map[t.id]; + const keys = Object.keys(map); + if (keys.length > 60) delete map[keys[0]]; + try { + localStorage.setItem(RESUME_KEY, JSON.stringify(map)); + } catch {} +} + +function bindTools() { + $("#sleepBtn")?.addEventListener("click", sleepMenu); + let lastSave = 0; + window.addEventListener("player", (e) => { + const type = e.detail.type; + if (type === "time" && Date.now() - lastSave > 10000) { + lastSave = Date.now(); + saveResume(); + } + if (type === "sleep" && Sleep.endOfSong) { + cancelSleep(true); + toast("Sleep timer stopped the music. Good night.", ""); + } + if (type === "track") { + if (Sleep.endOfSong && !Player.stopAfterCurrent) Player.stopAfterCurrent = true; + const t = Player.current; + const at = t && (t.durationMs || 0) >= RESUME_MIN_MS ? resumeMap()[t.id] : 0; + if (at) { + const seekTo = Math.max(0, at - 5); + const apply = () => { + if (Player.current?.id === t.id && Player.audio.currentTime < 5) { + Player.seek(seekTo); + toast(`Picked up where you left off (${fmtDur(seekTo * 1000)})`, ""); + } + }; + Player.audio.readyState >= 1 ? apply() : Player.audio.addEventListener("loadedmetadata", apply, { once: true }); + } + } + }); + Player.on("pause", saveResume); + renderSleep(); +}