Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Show Music folders alongside songs

Bitti962d36fb06dc
diff --git a/src/components/workspace/MusicView.test.tsx b/src/components/workspace/MusicView.test.tsx
index 0272281..a2205d7 100644
--- a/src/components/workspace/MusicView.test.tsx
+++ b/src/components/workspace/MusicView.test.tsx
@@ -48,17 +48,50 @@ it("creates songs in the browsed folder and moves them between folders by drag a
const dataTransfer = { effectAllowed: "", dropEffect: "", types: ["application/x-slopus-music-song"],
setData: (type: string, value: string) => data.set(type, value), getData: (type: string) => data.get(type) ?? "" };
fireEvent.dragStart(screen.getByRole("listitem"), { dataTransfer });
- const destination = within(screen.getByRole("navigation", { name: "Music folders" })).getByRole("button", { name: "Jazz" });
+ const destination = within(screen.getByRole("navigation", { name: "Music path" })).getByRole("button", { name: "Jazz" });
fireEvent.dragOver(destination, { dataTransfer });
expect(destination).toHaveAttribute("data-drop-target", "true");
fireEvent.drop(destination, { dataTransfer });
expect(latest.musicSongs![1].folderId).toBe(jazz.id);
expect(screen.getByText("This folder is empty")).toBeInTheDocument();
fireEvent.click(destination);
- expect(screen.getAllByRole("listitem")).toHaveLength(2);
- fireEvent.dragStart(screen.getAllByRole("listitem")[1], { dataTransfer });
- fireEvent.drop(within(screen.getByRole("navigation", { name: "Music folders" })).getByRole("button", { name: "Your music" }), { dataTransfer });
+ expect(screen.getAllByRole("listitem")).toHaveLength(3);
+ fireEvent.dragStart(screen.getAllByRole("button", { name: "Select New song" })[1].closest('[role="listitem"]')!, { dataTransfer });
+ const root = within(screen.getByRole("navigation", { name: "Music path" })).getByRole("button", { name: "Music" });
+ fireEvent.drop(root, { dataTransfer });
expect(latest.musicSongs![1].folderId).toBeNull();
+ fireEvent.click(root);
+ fireEvent.dragStart(screen.getByRole("button", { name: "Select New song" }).closest('[role="listitem"]')!, { dataTransfer });
+ const folderCard = screen.getByRole("button", { name: "Open Jazz" }).closest('[role="listitem"]')!;
+ fireEvent.dragOver(folderCard, { dataTransfer });
+ expect(folderCard).toHaveAttribute("data-drop-target", "true");
+ fireEvent.drop(folderCard, { dataTransfer });
+ expect(latest.musicSongs![1].folderId).toBe(jazz.id);
+});
+
+it.each(["Grid", "List"])("browses folders as %s items with a Music path header and only the current contents", (view) => {
+ const parent = newMusicFolder("Jazz", null), child = newMusicFolder("Piano", parent.id);
+ const rootSong = { ...newSong(), title: "Root song" }, nestedSong = { ...newSong("yue2", 32, child.id), title: "Piano song" };
+ render(<Harness initial={{ ...createConfig(), musicFolders: [parent, child], musicSongs: [rootSong, nestedSong] }} />);
+ fireEvent.click(screen.getByRole("button", { name: `${view} view` }));
+ expect(screen.getByRole("heading", { name: "Music", level: 2 })).toBeInTheDocument();
+ expect(screen.getByRole("list", { name: "Music contents" })).toHaveClass(`music-songs--${view.toLowerCase()}`);
+ const folder = screen.getByRole("button", { name: "Open Jazz" });
+ expect(folder).toHaveClass("music-song__select");
+ expect(folder.querySelector(".music-song__art svg")).toBeInTheDocument();
+ expect(screen.queryByRole("button", { name: "Open Piano" })).not.toBeInTheDocument();
+ fireEvent.click(folder);
+ expect(screen.getByRole("heading", { name: "Music / Jazz", level: 2 })).toBeInTheDocument();
+ expect(screen.queryByRole("button", { name: "Select Root song" })).not.toBeInTheDocument();
+ expect(screen.queryByText("This folder is empty")).not.toBeInTheDocument();
+ expect(screen.getAllByRole("listitem")).toHaveLength(1);
+ fireEvent.click(screen.getByRole("button", { name: "Open Piano" }));
+ expect(screen.getByRole("heading", { name: "Music / Jazz / Piano", level: 2 })).toBeInTheDocument();
+ expect(screen.getByRole("button", { name: "Select Piano song" })).toBeInTheDocument();
+ const path = within(screen.getByRole("navigation", { name: "Music path" }));
+ fireEvent.click(path.getByRole("button", { name: "Music" }));
+ expect(screen.getByRole("button", { name: "Select Root song" })).toBeInTheDocument();
+ expect(screen.queryByRole("navigation", { name: "Music folders" })).not.toBeInTheDocument();
});
it("creates songs and folders from the library and song context menus in the current folder", () => {
@@ -82,15 +115,16 @@ it("removes the context-clicked folder and moves its contents to the parent", ()
const parent = newMusicFolder("Jazz", null), folder = newMusicFolder("Piano", parent.id), child = newMusicFolder("Solo", folder.id);
const song = newSong("yue2", 32, folder.id), nested = newSong("yue2", 32, child.id);
render(<Harness initial={{ ...createConfig(), musicFolders: [parent, folder, child], musicSongs: [song, nested] }} />);
- const navigation = within(screen.getByRole("navigation", { name: "Music folders" }));
- fireEvent.click(navigation.getByRole("button", { name: "Jazz / Piano" }));
- fireEvent.keyDown(navigation.getByRole("button", { name: "Jazz / Piano" }), { key: "ContextMenu" });
+ const navigation = within(screen.getByRole("navigation", { name: "Music path" }));
+ fireEvent.click(screen.getByRole("button", { name: "Open Jazz" }));
+ fireEvent.click(screen.getByRole("button", { name: "Open Piano" }));
+ fireEvent.keyDown(navigation.getByRole("button", { name: "Piano" }), { key: "ContextMenu" });
fireEvent.click(screen.getByRole("menuitem", { name: "Remove folder" }));
expect(latest.musicFolders).toEqual([parent, { ...child, parentId: parent.id }]);
expect(latest.musicSongs).toEqual([{ ...song, folderId: parent.id }, nested]);
expect(navigation.getByRole("button", { name: "Jazz" })).toHaveAttribute("aria-current", "page");
- expect(navigation.getByRole("button", { name: "Jazz / Solo" })).toBeInTheDocument();
- fireEvent.contextMenu(navigation.getByRole("button", { name: "Your music" }));
+ expect(screen.getByRole("button", { name: "Open Solo" })).toBeInTheDocument();
+ fireEvent.contextMenu(navigation.getByRole("button", { name: "Music" }));
expect(screen.queryByRole("menuitem", { name: "Remove folder" })).not.toBeInTheDocument();
});
@@ -133,13 +167,14 @@ it("automatically plays the next generated song in the starting folder even afte
relativePath: `media/${song.assetId}.wav`, durationMs: 30000, mimeType: "audio/wav", createdAt: song.createdAt }] : []);
vi.mocked(readMediaFileUrl).mockImplementation(async (_, asset) => `blob:${asset.relativePath}`);
const { container } = render(<Harness initial={{ ...createConfig(), musicFolders: [jazz, other], musicSongs: songs, assets }} />);
- const folders = within(screen.getByRole("navigation", { name: "Music folders" }));
- fireEvent.click(folders.getByRole("button", { name: "Jazz" }));
+ const path = within(screen.getByRole("navigation", { name: "Music path" }));
+ fireEvent.click(screen.getByRole("button", { name: "Open Jazz" }));
fireEvent.click(screen.getByRole("button", { name: "Play First" }));
const audio = container.querySelector("audio")!;
await waitFor(() => expect(audio).toHaveAttribute("src", "blob:media/first.wav"));
fireEvent.canPlay(audio);
- fireEvent.click(folders.getByRole("button", { name: "Other" }));
+ fireEvent.click(path.getByRole("button", { name: "Music" }));
+ fireEvent.click(screen.getByRole("button", { name: "Open Other" }));
fireEvent.ended(audio);
await waitFor(() => expect(audio).toHaveAttribute("src", "blob:media/second.wav"));
fireEvent.canPlay(audio);
@@ -158,7 +193,7 @@ it("adds songs, edits lyrics and style, switches layout and queues the selected
fireEvent.change(screen.getByLabelText("Music lyrics"), { target: { value: "[Verse]\nCity lights" } });
expect(latest.musicSongs![0]).toMatchObject({ title: "Night drive", style: "Synth pop", settings: { lyrics: "[Verse]\nCity lights" } });
fireEvent.click(screen.getByRole("button", { name: "List view" }));
- expect(screen.getByRole("list", { name: "Songs" })).toHaveClass("music-songs--list");
+ expect(screen.getByRole("list", { name: "Music contents" })).toHaveClass("music-songs--list");
fireEvent.click(screen.getByRole("button", { name: "Generate" }));
expect(generate).toHaveBeenCalledWith(latest.musicSongs![0].id, expect.objectContaining({ modelType: "yue2" }));
});
diff --git a/src/components/workspace/MusicView.tsx b/src/components/workspace/MusicView.tsx
index 3a4976f..f15e382 100644
--- a/src/components/workspace/MusicView.tsx
+++ b/src/components/workspace/MusicView.tsx
@@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from "react";
import type { ProjectAsset, ProjectConfig } from "../../lib/project";
import { isTauri, readMediaFileUrl } from "../../lib/persistence";
import { loadGeneratorTemplateSettings, subscribeGeneratorTemplates, templateSupportsMusic, templateUsable, generatorPathFields, type GeneratorTemplate } from "../../lib/settings";
-import { musicFolderLabel, musicSettingsForModel, moveSong, newMusicFolder, newSong, removeMusicFolder, songReference, updateSong, type MusicSettings, type MusicSong } from "../../lib/music";
+import { musicSettingsForModel, moveSong, newMusicFolder, newSong, removeMusicFolder, songReference, updateSong, type MusicSettings, type MusicSong } from "../../lib/music";
import { isWorkActive, type WorkItem } from "../../lib/workQueue";
import { ComboBox, CommandBarButton, PropRow, PropSection, Slider, Splitter, useContextMenu, usePaneSize, type MenuEntry } from "../ui";
import { Add16, Audio16, Delete16, FolderAdd16, FolderOpen16, GoToEnd16, GoToStart16, GridView16, ListView16, Pause16, Play16, References16, Speaker14, Sparkle16, Stop14 } from "../ui/icons";
@@ -27,6 +27,16 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
const [folderId, setFolderId] = useState<string | null>(null);
const currentFolder = folders.find((folder) => folder.id === folderId);
const currentFolderId = currentFolder?.id ?? null;
+ const visibleFolders = folders.filter((folder) => (folder.parentId ?? null) === currentFolderId);
+ const breadcrumbs: { id: string | null; name: string }[] = [];
+ const visited = new Set<string>();
+ let ancestor = currentFolder;
+ while (ancestor && !visited.has(ancestor.id)) {
+ visited.add(ancestor.id); breadcrumbs.unshift({ id: ancestor.id, name: ancestor.name });
+ const parentId = ancestor.parentId;
+ ancestor = folders.find((folder) => folder.id === parentId);
+ }
+ breadcrumbs.unshift({ id: null, name: "Music" });
const visibleSongs = (config.musicSongs ?? []).filter((song) => (song.folderId ?? null) === currentFolderId);
const [creatingFolder, setCreatingFolder] = useState(false);
const [folderName, setFolderName] = useState("");
@@ -88,6 +98,17 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
onChange((current) => moveSong(current, data.songId, destination));
} catch { /* Ignore unrelated or malformed drag data. */ }
};
+ const browseFolder = (id: string | null) => {
+ setFolderId(id); setSelectedId(undefined); setNotice(null); setCreatingFolder(false); setFolderName("");
+ };
+ const folderDropProps = (id: string | null) => ({
+ "data-drop-target": dropFolder === (id ?? "") ? "true" : undefined,
+ onDragOver: (event: React.DragEvent<HTMLElement>) => {
+ if (event.dataTransfer.types.includes(SONG_DRAG_TYPE)) { event.preventDefault(); event.dataTransfer.dropEffect = "move"; setDropFolder(id ?? ""); }
+ },
+ onDragLeave: () => setDropFolder(undefined),
+ onDrop: (event: React.DragEvent<HTMLElement>) => dropSong(event, id),
+ });
const chooseGenerator = (id: string) => {
const next = templates.find((item) => item.id === id);
setNewGeneratorId(id);
@@ -192,16 +213,18 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
<div className="music-library" tabIndex={0} aria-label="Music library"
onContextMenu={(event) => { if (!(event.target as HTMLElement).closest("input, textarea")) folderContext(event, currentFolderId); }}
onKeyDown={(event) => { if (event.target === event.currentTarget && (event.key === "ContextMenu" || event.shiftKey && event.key === "F10")) folderContext(event, currentFolderId); }}>
- <div className="music-library__heading"><h2>{currentFolder?.name ?? "Your music"}</h2><span>{visibleSongs.length} songs</span></div>
- <nav className="music-folders" aria-label="Music folders">
- {[{ id: null, name: "Your music" }, ...folders.map((folder) => ({ id: folder.id, name: musicFolderLabel(folders, folder.id) }))].map((folder) =>
- <button key={folder.id ?? "root"} type="button" className="secondary-button" aria-current={currentFolderId === folder.id ? "page" : undefined}
- onContextMenu={(event) => folderContext(event, folder.id)}
- onKeyDown={(event) => { if (event.key === "ContextMenu" || event.shiftKey && event.key === "F10") folderContext(event, folder.id); }}
- data-drop-target={dropFolder === (folder.id ?? "") ? "true" : undefined} onClick={() => { setFolderId(folder.id); setSelectedId(undefined); setNotice(null); }}
- onDragOver={(event) => { if (event.dataTransfer.types.includes(SONG_DRAG_TYPE)) { event.preventDefault(); event.dataTransfer.dropEffect = "move"; setDropFolder(folder.id ?? ""); } }}
- onDragLeave={() => setDropFolder(undefined)} onDrop={(event) => dropSong(event, folder.id)} data-tooltip={folder.name}><FolderOpen16 /><span>{folder.name}</span></button>)}
- </nav>
+ <div className="music-library__heading">
+ <nav aria-label="Music path"><h2 className="music-path">
+ {breadcrumbs.map((folder, index) => <span key={folder.id ?? "root"}>
+ {index > 0 && <span className="music-path__separator"> / </span>}
+ <button type="button" aria-current={currentFolderId === folder.id ? "page" : undefined}
+ onClick={() => browseFolder(folder.id)} {...folderDropProps(folder.id)}
+ onContextMenu={(event) => folderContext(event, folder.id)}
+ onKeyDown={(event) => { if (event.key === "ContextMenu" || event.shiftKey && event.key === "F10") folderContext(event, folder.id); }}>{folder.name}</button>
+ </span>)}
+ </h2></nav>
+ <span>{visibleFolders.length > 0 && `${visibleFolders.length} folders · `}{visibleSongs.length} songs</span>
+ </div>
{creatingFolder && <form className="music-new-folder" onSubmit={createFolder}>
<input className="text-field" aria-label="Folder name" placeholder="Folder name" autoFocus maxLength={200} value={folderName} onChange={(event) => setFolderName(event.target.value)} />
<button type="submit" className="primary-button" disabled={!folderName.trim()}>Create folder</button>
@@ -209,12 +232,20 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
</form>}
{notice && <p role="status">{notice}</p>}
{error && <p role="alert">{error}</p>}
- {!visibleSongs.length ? <div className="music-empty"><Audio16 /><h3>{currentFolder ? "This folder is empty" : "Create your first song"}</h3><p>Add a song, describe its style and write lyrics, or leave lyrics empty for an instrumental.</p><button type="button" className="primary-button" onClick={add}><Add16 />New song</button></div> :
- <div className={`music-songs music-songs--${view}`} role="list" aria-label="Songs">
+ {!visibleSongs.length && !visibleFolders.length ? <div className="music-empty"><Audio16 /><h3>{currentFolder ? "This folder is empty" : "Create your first song"}</h3><p>Add a song, describe its style and write lyrics, or leave lyrics empty for an instrumental.</p><button type="button" className="primary-button" onClick={add}><Add16 />New song</button></div> :
+ <div className={`music-songs music-songs--${view}`} role="list" aria-label="Music contents">
+ {visibleFolders.map((folder) => <div className="music-song music-folder" role="listitem" key={`folder:${folder.id}`}
+ {...folderDropProps(folder.id)} onContextMenu={(event) => folderContext(event, folder.id)}>
+ <button type="button" className="music-song__select" aria-label={`Open ${folder.name}`} onClick={() => browseFolder(folder.id)}
+ onKeyDown={(event) => { if (event.key === "ContextMenu" || event.shiftKey && event.key === "F10") folderContext(event, folder.id); }}>
+ <span className="music-song__art" aria-hidden="true"><FolderOpen16 /></span>
+ <span className="music-song__info"><strong>{folder.name}</strong></span>
+ </button>
+ </div>)}
{visibleSongs.map((song, index) => {
const asset = config.assets.find((item) => item.id === song.assetId);
const work = workItems.find((item) => item.sceneId === song.id && isWorkActive(item));
- return <div className={`music-song${selected?.id === song.id ? " music-song--selected" : ""}`} role="listitem" key={song.id}
+ return <div className={`music-song${selected?.id === song.id ? " music-song--selected" : ""}`} role="listitem" key={`song:${song.id}`}
draggable onDragStart={(event) => { event.dataTransfer.effectAllowed = "move"; event.dataTransfer.setData(SONG_DRAG_TYPE, JSON.stringify({ projectId: config.id, songId: song.id })); }} onDragEnd={() => setDropFolder(undefined)}
onContextMenu={(event) => context(event, song)}>
<button type="button" className="music-song__select" aria-label={`Select ${song.title}`} aria-pressed={selected?.id === song.id}
diff --git a/src/styles/music.css b/src/styles/music.css
index 7732132..9cf2828 100644
--- a/src/styles/music.css
+++ b/src/styles/music.css
@@ -8,11 +8,15 @@
.music-library { grid-column: 1; grid-row: 1; min-width: 0; overflow: auto; padding: var(--space-4); }
.music-library__heading { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); margin-bottom: var(--space-4); }
.music-library__heading h2 { margin: 0; font-size: var(--text-lg); min-width: 0; overflow-wrap: anywhere; }
-.music-folders { display: flex; flex-wrap: wrap; gap: var(--space-1); max-height: 128px; overflow-y: auto; margin-bottom: var(--space-3); }
-.music-folders > button { min-width: 0; max-width: 100%; }
-.music-folders > button > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
-.music-folders [aria-current="page"] { color: var(--accent); border-color: var(--accent); }
-.music-folders [data-drop-target="true"] { outline: 2px solid var(--accent); outline-offset: -2px; background: var(--fill-subtle-hover); }
+.music-library__heading nav { min-width: 0; }
+.music-library__heading > span { flex-shrink: 0; }
+.music-path { display: flex; flex-wrap: wrap; align-items: center; }
+.music-path > span { min-width: 0; display: inline-flex; align-items: center; }
+.music-path button { border: 0; border-radius: var(--radius-sm); padding: 2px 4px; background: transparent; color: var(--text-secondary); font: inherit; overflow-wrap: anywhere; text-align: left; }
+.music-path button:hover { background: var(--fill-subtle-hover); color: var(--text-primary); }
+.music-path [aria-current="page"] { color: var(--text-primary); }
+.music-path__separator { padding-inline: 4px; color: var(--text-secondary); }
+.music-library [data-drop-target="true"] { outline: 2px solid var(--accent); outline-offset: -2px; background: var(--fill-subtle-hover); }
.music-new-folder { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-bottom: var(--space-3); }
.music-new-folder input { flex: 1; min-width: 120px; }
.music-library__heading > span, .music-song__info > span, .music-song__status, .music-player__now span { color: var(--text-secondary); font-size: var(--text-xs); }