mirror of
https://github.com/pvrzz/atmosphere.git
synced 2026-11-29 05:22:26 +00:00
Quick search across the library (Ctrl+K) with link and online search shortcuts
This commit is contained in:
@@ -173,8 +173,10 @@ cargo tauri build
|
||||
| `L` | Lyrics |
|
||||
| `Q` | Queue |
|
||||
| `F` | TV mode |
|
||||
| `Ctrl K` | Search your library, or paste a link |
|
||||
| `Ctrl L` | Get: the link and online search bar |
|
||||
| `Ctrl B` | Collapse or expand the sidebar |
|
||||
| `P` | Mini player |
|
||||
| `Ctrl K` | Link or search bar |
|
||||
| `Ctrl F` | Search the current view |
|
||||
| `Delete` | Delete the selected songs (with confirmation) |
|
||||
| `Ctrl A` | Select all in the current list |
|
||||
|
||||
@@ -239,6 +239,7 @@ async function init() {
|
||||
bindConnect();
|
||||
bindUpdates();
|
||||
bindPlaybackSettings();
|
||||
bindSearch();
|
||||
bindStats();
|
||||
await listen("atmosphere://job", (e) => onJob(e.payload));
|
||||
await listen("atmosphere://probe", (e) => onProbe(e.payload));
|
||||
@@ -435,7 +436,10 @@ function go(view) {
|
||||
|
||||
function onKey(e) {
|
||||
const mod = e.ctrlKey || e.metaKey;
|
||||
if (mod && (e.key === "k" || e.key === "l")) {
|
||||
if (mod && e.key.toLowerCase() === "k") {
|
||||
e.preventDefault();
|
||||
openSearch();
|
||||
} else if (mod && e.key.toLowerCase() === "l") {
|
||||
e.preventDefault();
|
||||
go("get");
|
||||
$("#commandInput").focus();
|
||||
|
||||
+3
-1
@@ -28,7 +28,8 @@
|
||||
</div>
|
||||
<nav class="nav" aria-label="Main">
|
||||
<button class="nav-item" data-lib="home"><span class="i" data-icon="home"></span><span>Home</span></button>
|
||||
<button class="nav-item" data-nav="get" aria-current="page"><span class="i" data-icon="cloud-download"></span><span>Get</span><kbd>Ctrl K</kbd></button>
|
||||
<button class="nav-item" id="navSearch" type="button"><span class="i" data-icon="search"></span><span>Search</span><kbd>Ctrl K</kbd></button>
|
||||
<button class="nav-item" data-nav="get"><span class="i" data-icon="cloud-download"></span><span>Get</span><kbd>Ctrl L</kbd></button>
|
||||
<button class="nav-item" data-nav="downloads" id="navDownloads">
|
||||
<span class="nav-ring" aria-hidden="true"><svg viewBox="0 0 20 20"><circle class="track" cx="10" cy="10" r="8.5"/><circle class="fill" id="navRing" cx="10" cy="10" r="8.5"/></svg><span class="i" data-icon="download"></span></span>
|
||||
<span>Downloads</span><span class="count" id="navCount" hidden></span>
|
||||
@@ -393,5 +394,6 @@
|
||||
<script src="miniplayer.js"></script>
|
||||
<script src="connect.js"></script>
|
||||
<script src="updates.js"></script>
|
||||
<script src="search.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
// atmosphere · search.js · Quick search palette (Ctrl+K) across songs, albums, artists and playlists, with link and online search shortcuts
|
||||
const QS = { open: false, items: [], sel: 0, timer: 0 };
|
||||
|
||||
function qsScore(hay, q, words) {
|
||||
if (!hay) return 0;
|
||||
if (hay === q) return 100;
|
||||
if (hay.startsWith(q)) return 80;
|
||||
if (words.every((w) => hay.includes(w))) return hay.split(/[\s\-–(]+/).some((p) => p.startsWith(words[0])) ? 60 : 40;
|
||||
return 0;
|
||||
}
|
||||
|
||||
function qsResults(raw) {
|
||||
const q = lnorm(raw);
|
||||
if (!q) return [];
|
||||
const words = q.split(/\s+/).filter(Boolean);
|
||||
const out = [];
|
||||
if (/^(https?:\/\/|www\.)\S+$/i.test(raw.trim())) out.push({ kind: "link", title: "Get this link", sub: raw.trim(), icon: "link", run: () => qsOnline(raw.trim(), null) });
|
||||
const songs = [];
|
||||
for (const t of Lib.songs) {
|
||||
const s = Math.max(qsScore(lnorm(t.title), q, words), qsScore(lnorm(`${t.title} ${t.artist}`), q, words) - 5, qsScore(lnorm(`${t.artist} ${t.title}`), q, words) - 8);
|
||||
if (s) songs.push([s + Math.min(15, (Lib.plays[t.id] || 0) / 2), t]);
|
||||
}
|
||||
songs.sort((a, b) => b[0] - a[0]);
|
||||
const albums = [...Lib.albums.values()].map((a) => [Math.max(qsScore(lnorm(a.title), q, words), qsScore(lnorm(`${a.title} ${a.artist}`), q, words) - 5), a]).filter((x) => x[0]).sort((a, b) => b[0] - a[0]);
|
||||
const artists = [...Lib.artists.values()].map((a) => [qsScore(a.key, q, words) + Math.min(10, a.tracks.length / 5), a]).filter((x) => x[0] > 10).sort((a, b) => b[0] - a[0]);
|
||||
const lists = (Lib.data?.playlists || []).map((p) => [qsScore(lnorm(p.name), q, words), p]).filter((x) => x[0]).sort((a, b) => b[0] - a[0]);
|
||||
const top = [songs[0], albums[0], artists[0], lists[0]].filter(Boolean).sort((a, b) => b[0] - a[0])[0];
|
||||
const add = (group, list, n, make) => list.slice(0, n).forEach(([, x], i) => out.push({ group: i === 0 ? group : null, ...make(x) }));
|
||||
const songItem = (t) => ({ kind: "song", title: t.title, sub: [t.artist, t.album].filter(Boolean).join(" · "), art: t.art, run: () => playList([t]), alt: () => Player.playNext([t]) });
|
||||
const albumItem = (a) => ({ kind: "album", title: a.title, sub: `${albumKind(a)} · ${a.artist}`, art: a.art, run: () => openLib("album", a.key), alt: () => playList(a.tracks) });
|
||||
const artistItem = (a) => ({ kind: "artist", title: a.name, sub: `Artist · ${plural(a.tracks.length, "song")}`, art: a.meta?.image || a.albums[0]?.art, round: true, run: () => openLib("artist", a.key), alt: () => playList(a.tracks, 0, true) });
|
||||
const listItem = (p) => ({ kind: "playlist", title: p.name, sub: `Playlist · ${plural(p.order.length, "song")}`, icon: p.linked ? "folder" : "playlist", run: () => openLib("playlist", p.name), alt: () => playList(p.order.map((id) => Lib.byId.get(id)).filter(Boolean)) });
|
||||
if (top && top[0] >= 60) {
|
||||
const x = top[1];
|
||||
out.push({ group: "Top result", ...(songs[0] === top ? songItem(x) : albums[0] === top ? albumItem(x) : artists[0] === top ? artistItem(x) : listItem(x)), top: true });
|
||||
}
|
||||
add("Songs", songs.filter((s) => !(top && s === top)), 6, songItem);
|
||||
add("Albums", albums.filter((s) => !(top && s === top)), 4, albumItem);
|
||||
add("Artists", artists.filter((s) => !(top && s === top)), 4, artistItem);
|
||||
add("Playlists", lists.filter((s) => !(top && s === top)), 4, listItem);
|
||||
if (!out.some((x) => x.kind === "link")) {
|
||||
out.push({ group: "Find online", kind: "online", title: `Search YouTube for “${raw.trim()}”`, icon: "brand-youtube", run: () => qsOnline(raw.trim(), "youtube") });
|
||||
out.push({ kind: "online", title: `Search SoundCloud for “${raw.trim()}”`, icon: "brand-soundcloud", run: () => qsOnline(raw.trim(), "soundcloud") });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function qsOnline(text, scope) {
|
||||
closeSearch();
|
||||
if (scope) {
|
||||
state.scope = scope;
|
||||
$$("#searchScope button").forEach((x) => x.setAttribute("aria-checked", String(x.dataset.scope === scope)));
|
||||
}
|
||||
go("get");
|
||||
$("#commandInput").value = text;
|
||||
$("#commandInput").dispatchEvent(new Event("input"));
|
||||
fetchText(text);
|
||||
}
|
||||
|
||||
function qsRender() {
|
||||
const list = $("#qsList");
|
||||
if (!list) return;
|
||||
const raw = $("#qsInput").value;
|
||||
if (!raw.trim()) {
|
||||
list.replaceChildren(h("p", { class: "qs-hint" }, "Search your songs, albums, artists and playlists. Paste a link to get it."));
|
||||
QS.items = [];
|
||||
return;
|
||||
}
|
||||
QS.items = qsResults(raw);
|
||||
QS.sel = Math.min(QS.sel, Math.max(0, QS.items.length - 1));
|
||||
if (!QS.items.length) {
|
||||
list.replaceChildren(h("p", { class: "qs-hint" }, "Nothing matches."));
|
||||
return;
|
||||
}
|
||||
list.replaceChildren(
|
||||
...QS.items.flatMap((it, i) => [
|
||||
it.group ? h("div", { class: "qs-group" }, it.group) : null,
|
||||
h("div", { class: `qs-item ${i === QS.sel ? "sel" : ""} ${it.top ? "top" : ""}`, role: "option", "aria-selected": String(i === QS.sel), "data-i": i, onmousemove: () => qsSelect(i), onclick: (e) => qsRun(i, e.shiftKey) },
|
||||
it.art ? cover(it.art, `qs-art ${it.round ? "round" : ""}`) : h("span", { class: "qs-art qs-ico" }, icon(it.icon || "music")),
|
||||
h("div", { class: "qs-text" }, h("span", { class: "qs-title" }, it.title), it.sub ? h("span", { class: "qs-sub" }, it.sub) : null),
|
||||
it.alt ? h("span", { class: "qs-keys" }, h("kbd", {}, "↵"), it.kind === "song" ? " play · " : " open · ", h("kbd", {}, "⇧↵"), it.kind === "song" ? " play next" : " play") : null),
|
||||
]).filter(Boolean)
|
||||
);
|
||||
}
|
||||
|
||||
function qsSelect(i) {
|
||||
if (i === QS.sel) return;
|
||||
QS.sel = i;
|
||||
$$(".qs-item").forEach((el) => {
|
||||
const on = Number(el.dataset.i) === i;
|
||||
el.classList.toggle("sel", on);
|
||||
el.setAttribute("aria-selected", String(on));
|
||||
});
|
||||
}
|
||||
|
||||
function qsRun(i, alt) {
|
||||
const it = QS.items[i];
|
||||
if (!it) return;
|
||||
closeSearch();
|
||||
(alt && it.alt ? it.alt : it.run)();
|
||||
}
|
||||
|
||||
function openSearch(prefill = "") {
|
||||
if (QS.open) return $("#qsInput").select();
|
||||
QS.open = true;
|
||||
QS.sel = 0;
|
||||
const input = h("input", { class: "qs-input", id: "qsInput", type: "search", placeholder: "Search your music, or paste a link", spellcheck: "false", autocomplete: "off", "aria-label": "Search", value: prefill });
|
||||
const wrap = h("div", { class: "qs-wrap", id: "qsWrap", onmousedown: (e) => { if (e.target === wrap) closeSearch(); } },
|
||||
h("div", { class: "qs", role: "dialog", "aria-modal": "true", "aria-label": "Search" },
|
||||
h("div", { class: "qs-bar" }, icon("search"), input, h("kbd", {}, "Esc")),
|
||||
h("div", { class: "qs-list", id: "qsList", role: "listbox" })));
|
||||
input.addEventListener("input", () => {
|
||||
clearTimeout(QS.timer);
|
||||
QS.sel = 0;
|
||||
QS.timer = setTimeout(qsRender, Lib.songs.length > 5000 ? 60 : 0);
|
||||
});
|
||||
input.addEventListener("keydown", (e) => {
|
||||
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
||||
e.preventDefault();
|
||||
const n = QS.items.length;
|
||||
if (!n) return;
|
||||
qsSelect((QS.sel + (e.key === "ArrowDown" ? 1 : n - 1)) % n);
|
||||
$(".qs-item.sel")?.scrollIntoView({ block: "nearest" });
|
||||
} else if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
qsRun(QS.sel, e.shiftKey);
|
||||
} else if (e.key === "Escape") {
|
||||
e.preventDefault();
|
||||
closeSearch();
|
||||
}
|
||||
});
|
||||
document.body.append(wrap);
|
||||
input.focus();
|
||||
qsRender();
|
||||
}
|
||||
|
||||
function closeSearch() {
|
||||
QS.open = false;
|
||||
$("#qsWrap")?.remove();
|
||||
}
|
||||
|
||||
function bindSearch() {
|
||||
$("#navSearch")?.addEventListener("click", () => openSearch());
|
||||
}
|
||||
@@ -1084,3 +1084,27 @@ body.mini-mode > *:not(#mini):not(.toasts):not(.modal-wrap):not(script) { displa
|
||||
.tile-mosaic img, .tile-mosaic .cover { width: 100%; height: 100%; object-fit: cover; border-radius: 0; }
|
||||
.tile-img.ph { display: grid; place-items: center; background: var(--surface); color: var(--text-3); }
|
||||
.tile-img.ph .i svg { width: 36px; height: 36px; }
|
||||
|
||||
/* quick search */
|
||||
.qs-wrap { position: fixed; inset: 0; z-index: 110; background: rgba(5, 6, 10, 0.45); display: grid; justify-items: center; align-items: start; padding-top: 12vh; animation: fadeOnly 120ms var(--ease); }
|
||||
.qs { width: min(640px, calc(100vw - 32px)); max-height: 70vh; display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.6); overflow: hidden; }
|
||||
.qs-bar { display: flex; align-items: center; gap: var(--s3); padding: 12px 16px; border-bottom: 1px solid var(--hairline); color: var(--text-3); }
|
||||
.qs-bar .i svg { width: 18px; height: 18px; }
|
||||
.qs-input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text); font: 500 16px/1.4 var(--font); }
|
||||
.qs-input::-webkit-search-cancel-button { display: none; }
|
||||
.qs-list { overflow-y: auto; padding: 6px; }
|
||||
.qs-hint { margin: 0; padding: 18px 12px; color: var(--text-3); font-size: 13px; text-align: center; }
|
||||
.qs-group { padding: 10px 10px 4px; font-size: 11.5px; font-weight: 650; color: var(--text-3); letter-spacing: 0.02em; }
|
||||
.qs-item { display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; gap: var(--s3); align-items: center; padding: 6px 10px; border-radius: 10px; cursor: default; }
|
||||
.qs-item.sel { background: var(--hover); }
|
||||
.qs-item.top { padding: 10px; }
|
||||
.qs-item.top .qs-art { width: 52px; height: 52px; }
|
||||
.qs-item.top { grid-template-columns: 52px minmax(0, 1fr) auto; }
|
||||
.qs-art { width: 40px; height: 40px; border-radius: 6px; object-fit: cover; }
|
||||
.qs-art.round { border-radius: 50%; }
|
||||
.qs-ico { display: grid; place-items: center; background: var(--surface-2, var(--hover)); color: var(--text-2); }
|
||||
.qs-text { display: grid; min-width: 0; }
|
||||
.qs-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.qs-sub { font-size: 12px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.qs-keys { font-size: 11px; color: var(--text-3); opacity: 0; white-space: nowrap; }
|
||||
.qs-item.sel .qs-keys { opacity: 1; }
|
||||
|
||||
Reference in New Issue
Block a user