mirror of
https://github.com/pvrzz/atmosphere.git
synced 2026-11-29 05:22:26 +00:00
Open audio files with Atmosphere, and drop files onto the window
- Audio files can be opened with Atmosphere (file associations in the installer) - Opening a file while Atmosphere runs hands it to the open window instead of a second copy - Files outside your library play without being added; library files play as themselves - Dropping audio files on the window copies them into an Imported folder and plays them - macOS "Open with" works through the app's open-file event
This commit is contained in:
@@ -241,6 +241,7 @@ async function init() {
|
||||
bindPlaybackSettings();
|
||||
bindSearch();
|
||||
bindTools();
|
||||
bindOpenFiles();
|
||||
bindStats();
|
||||
await listen("atmosphere://job", (e) => onJob(e.payload));
|
||||
await listen("atmosphere://probe", (e) => onProbe(e.payload));
|
||||
@@ -406,9 +407,17 @@ function bindShell() {
|
||||
const t = e.target;
|
||||
if (t.nodeType !== 1 || (t.getAttribute("draggable") !== "true" && !t.closest?.('[draggable="true"]'))) e.preventDefault();
|
||||
});
|
||||
document.addEventListener("dragover", (e) => e.preventDefault());
|
||||
document.addEventListener("dragover", (e) => {
|
||||
e.preventDefault();
|
||||
if (e.dataTransfer.types.includes("Files")) showDropHint();
|
||||
});
|
||||
document.addEventListener("dragleave", (e) => {
|
||||
if (!e.relatedTarget) hideDropHint();
|
||||
});
|
||||
document.addEventListener("drop", (e) => {
|
||||
e.preventDefault();
|
||||
hideDropHint();
|
||||
if (e.dataTransfer.files?.length) return importDropped([...e.dataTransfer.files]);
|
||||
const text = (e.dataTransfer.getData("text/uri-list") || e.dataTransfer.getData("text/plain") || "").trim();
|
||||
if (/^https?:\/\//i.test(text)) {
|
||||
go("get");
|
||||
|
||||
@@ -1141,3 +1141,10 @@ body.mini-mode > *:not(#mini):not(.toasts):not(.modal-wrap):not(script) { displa
|
||||
.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); }
|
||||
|
||||
/* dropping files */
|
||||
.drop-hint { position: fixed; inset: 12px; z-index: 130; border: 2px dashed color-mix(in srgb, var(--accent) 70%, transparent); border-radius: 18px; background: color-mix(in srgb, var(--bg) 82%, transparent); display: grid; place-items: center; pointer-events: none; animation: fadeOnly 120ms var(--ease); }
|
||||
.drop-hint > div { display: grid; justify-items: center; gap: 6px; text-align: center; }
|
||||
.drop-hint .i svg { width: 40px; height: 40px; color: var(--accent); }
|
||||
.drop-hint strong { font: 650 18px/1.3 var(--font-display); }
|
||||
.drop-hint span { color: var(--text-2); font-size: 13px; }
|
||||
|
||||
+47
-1
@@ -1,4 +1,4 @@
|
||||
// atmosphere · tools.js · Everyday player tools: song info, save the queue as a playlist, sleep timer and resuming long tracks
|
||||
// atmosphere · tools.js · Everyday player tools: song info, save queue, sleep timer, resuming long tracks, and opening or dropping audio files
|
||||
const Sleep = { until: 0, endOfSong: false, timer: 0, tick: 0, fading: false };
|
||||
const RESUME_KEY = "atmo.resume";
|
||||
const RESUME_MIN_MS = 20 * 60 * 1000;
|
||||
@@ -175,3 +175,49 @@ function bindTools() {
|
||||
Player.on("pause", saveResume);
|
||||
renderSleep();
|
||||
}
|
||||
|
||||
const AUDIO_FILE = /\.(mp3|flac|wav|m4a|aac|ogg|oga|opus|aiff?|wma|alac)$/i;
|
||||
|
||||
function showDropHint() {
|
||||
if ($("#dropHint")) return;
|
||||
document.body.append(h("div", { class: "drop-hint", id: "dropHint" }, h("div", {}, icon("music"), h("strong", {}, "Drop to add to your library"), h("span", {}, "Songs are copied into an Imported folder and start playing."))));
|
||||
}
|
||||
|
||||
function hideDropHint() {
|
||||
$("#dropHint")?.remove();
|
||||
}
|
||||
|
||||
async function importDropped(files) {
|
||||
const audio = files.filter((f) => AUDIO_FILE.test(f.name));
|
||||
if (!audio.length) return toast("Those aren't audio files Atmosphere can play.", "err");
|
||||
toast(`Adding ${plural(audio.length, "song")}…`, "");
|
||||
const tracks = [];
|
||||
for (const f of audio) {
|
||||
try {
|
||||
const bytes = new Uint8Array(await f.arrayBuffer());
|
||||
tracks.push(await invoke("import_file", bytes, { headers: { "x-file-name": encodeURIComponent(f.name) } }));
|
||||
} catch (e) {
|
||||
toast(`${f.name}: ${errText(e)}`, "err");
|
||||
}
|
||||
}
|
||||
if (!tracks.length) return;
|
||||
await loadLibrary();
|
||||
const fresh = tracks.map((t) => Lib.byId.get(t.id) || t);
|
||||
Player.setQueue(fresh, 0);
|
||||
toast(`Added ${plural(fresh.length, "song")} to your library`, "ok");
|
||||
}
|
||||
|
||||
async function openFiles(paths) {
|
||||
if (!paths?.length) return;
|
||||
const tracks = await invoke("open_files", { paths }).catch(() => []);
|
||||
if (!tracks.length) return toast("Atmosphere couldn't open that file.", "err");
|
||||
Player.setQueue(tracks.map((t) => Lib.byId.get(t.id) || t), 0);
|
||||
toast(tracks.length === 1 ? `Playing “${tracks[0].title}”` : `Playing ${plural(tracks.length, "song")}`, "");
|
||||
}
|
||||
|
||||
async function bindOpenFiles() {
|
||||
listen("atmosphere://open-files", (e) => openFiles(e.payload));
|
||||
const waitLib = () => (Lib.restored ? Promise.resolve() : new Promise((r) => setTimeout(() => waitLib().then(r), 200)));
|
||||
await waitLib();
|
||||
openFiles(await invoke("startup_files").catch(() => []));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user