Compare commits
32
Commits
main
..
609b80336b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
609b80336b | ||
|
|
a933fc3ca8 | ||
|
|
d6506c2f22 | ||
|
|
f0929e2b6d | ||
|
|
327e40026a | ||
|
|
398457093a | ||
|
|
47a6569fe0 | ||
|
|
73d5d554c7 | ||
|
|
b4e5469b51 | ||
|
|
42679cb02a | ||
|
|
42bdab988d | ||
|
|
e9455c2ef2 | ||
|
|
73fd191a28 | ||
|
|
f79439ced3 | ||
|
|
bfc8be4058 | ||
|
|
25248f5088 | ||
|
|
b6c72f3175 | ||
|
|
6d04143606 | ||
|
|
3f0fcbb626 | ||
|
|
9b9a61f2a0 | ||
|
|
ea47f1d1c8 | ||
|
|
6bbc660019 | ||
|
|
51bed98b0b | ||
|
|
fc82d1e1be | ||
|
|
1871bb713c | ||
|
|
e65aac496c | ||
|
|
3db3eaef67 | ||
|
|
82733268c5 | ||
|
|
c6206b87bd | ||
|
|
822012b17b | ||
|
|
10b69837c3 | ||
|
|
a07b9bae76 |
@@ -54,27 +54,6 @@ jobs:
|
||||
with:
|
||||
targets: ${{ matrix.target != '' && matrix.target || '' }}
|
||||
|
||||
# A [patch.crates-io] whose version doesn't satisfy the dependency requirement is silently
|
||||
# IGNORED by cargo: it warns "patch ... was not used in the crate graph" and builds against the
|
||||
# real crates.io crate instead. That's how a stale egui fork got us a `cannot find type
|
||||
# CursorImage` error four minutes into the build, in a file that had nothing to do with the
|
||||
# cause. Fail here instead, with the actual reason.
|
||||
- name: Verify egui fork patch applies
|
||||
shell: bash
|
||||
working-directory: ./lightningbeam-ui
|
||||
run: |
|
||||
unused=$(cargo metadata --format-version 1 2>&1 >/dev/null | grep "was not used in the crate graph" || true)
|
||||
if [ -n "$unused" ]; then
|
||||
echo "::error::The egui fork's [patch.crates-io] entries are being ignored — cargo is building against stock crates.io egui, not the fork."
|
||||
echo "$unused"
|
||||
echo ""
|
||||
echo "The fork's version must SATISFY the requirement in lightningbeam-ui/Cargo.toml."
|
||||
echo "Fork workspace version: $(grep -m1 '^version' ../egui-fork/Cargo.toml)"
|
||||
echo "Required by us: $(grep -m1 '^eframe' Cargo.toml)"
|
||||
exit 1
|
||||
fi
|
||||
echo "egui fork patch applies cleanly."
|
||||
|
||||
- name: Rust cache
|
||||
uses: swatinem/rust-cache@v2
|
||||
with:
|
||||
|
||||
+1
-4
@@ -38,7 +38,4 @@ packaging/AppDir/
|
||||
packaging/squashfs-root/
|
||||
|
||||
# Wrapper script (generated, not needed with static FFmpeg)
|
||||
lightningbeam-ui/lightningbeam-editor/debian/
|
||||
|
||||
# Personal working notes (not version-controlled)
|
||||
TODO.md
|
||||
lightningbeam-ui/lightningbeam-editor/debian/
|
||||
@@ -1,53 +1,3 @@
|
||||
# 1.0.10-alpha:
|
||||
Changes:
|
||||
- Cycle recording: highlight a range on the timeline ruler (the cycle strip, shown when looping is armed) and the transport loops it. Recording round the loop turns each pass into a separate take you can choose between afterwards, and recording again over the same region adds more takes rather than stacking a second clip on top. Starting playback from outside the region jumps to its start
|
||||
- Takes: a "Take N/M" badge on the clip picks which take plays. Splitting a clip lets each half play a different take, so you can comp a part together from the best bits of several passes. Right-click a clip to delete the take that's playing or to delete all the unused ones; double-click a take in the list to rename it
|
||||
- MIDI cycle recording can either merge every pass into one clip (the default — earlier passes play back as you record, so you can layer a hi-hat over a kick) or keep each pass as a separate take, like audio does. Set it in Preferences → Audio
|
||||
- Painting: every colour-using tool (brush, paint bucket, eyedropper) now shares one Foreground/Background swatch row, and the eyedropper has an explicit toggle for which of the two it fills
|
||||
- Painting: the effect brushes (dodge/burn, sponge, blur, smudge, clone stamp, healing brush, pattern stamp) were stuck on a single fixed brush shape. They now each have their own size, strength, hardness and spacing, and can use any brush from the library
|
||||
- Tablet: stylus barrel buttons now work, with actions you can bind in Preferences (Pan, Eyedropper, or Eraser). Middle-mouse panning added too
|
||||
- Timeline: raster and text layers get an opacity slider; each layer row now shows only the controls that mean anything for it (volume for audio, opacity for raster/text, both for vector and video), and layers gain a visibility (eye) toggle
|
||||
- The stage gains undo/redo buttons, sized for touch and tablet use
|
||||
|
||||
Bugfixes:
|
||||
- Splitting an audio clip made it play back at the wrong length (a clip split at 1 second cut off after half a second at 120 BPM)
|
||||
- Trimmed MIDI clips were the wrong length on the timeline at any tempo other than 60 BPM
|
||||
- The paint bucket always filled with the background colour, whatever the brush was set to
|
||||
- Where an eyedropper sample landed (foreground or background) depended on which colour picker you had opened last
|
||||
- Stylus barrel buttons were silently ignored on Wayland
|
||||
- The opacity slider on a raster layer actually changed its volume, which means nothing on a raster layer
|
||||
- Tool cursors lagged behind the pointer while drawing; they're now drawn by the system rather than into the canvas
|
||||
- Colour picker popups wouldn't close when you dragged on the stage, so drawing left one hanging open, and clicking inside a picker's own hue slider closed it
|
||||
- The toolbar could not be scrolled when the pane was too short to show every tool
|
||||
|
||||
# 1.0.9-alpha:
|
||||
Bugfixes:
|
||||
- Fix audio recording placement: a second recording landed at the wrong spot (and clicking/dragging clips was similarly off) at any tempo other than 60 BPM — recordings now start exactly at the playhead at any tempo
|
||||
- While recording a second audio clip, the live bar showed as a zero-length clip until you stopped; it now grows as you record
|
||||
- MIDI clips were drawn with the wrong length (their end grew too fast) at tempos other than 120 BPM
|
||||
- Recording a MIDI clip didn't mark the project as having unsaved changes, so closing or starting a new file didn't prompt to save
|
||||
- Audio and MIDI recording can now be undone (and redone)
|
||||
|
||||
# 1.0.8-alpha:
|
||||
Changes:
|
||||
- Mobile/touch UI (experimental, testing only — not built or packaged for mobile yet; enabled on desktop with the LB_MOBILE_UI environment variable): work-in-progress phone-friendly interface with a vertical sliding-window pane stack you drag to reveal panes, a new-file intent picker, a selection inspector sheet, a keyboard-primary music surface, a Focus/Patch node editor, long-press context menus, a command palette, and landscape/orientation support
|
||||
- Text layers: add and edit text with a chosen font; non-bundled fonts are embedded in the project so it renders on machines that lack them
|
||||
- Animated GIF export (parallel palette encoding)
|
||||
- Audio tag metadata (title, artist, album, genre, year, track, comment) is written into exports — ID3v2 for MP3, iTunes/MP4 atoms for AAC, Vorbis comments for FLAC, RIFF INFO for WAV — with sensible defaults (year, artist/album remembered between exports)
|
||||
- Lossy WebP image export, so the quality control now actually applies
|
||||
- SVG export now includes text layers, as real font-independent glyph outlines
|
||||
- Crash recovery: the editor autosaves your work to a recovery file in the background and offers to restore it after an unclean shutdown
|
||||
- Prompt to save unsaved changes before starting a new file, opening another, or quitting
|
||||
- Faster saves on painting projects: unchanged raster frames are no longer re-encoded every save
|
||||
|
||||
Bugfixes:
|
||||
- FLAC export previously wrote a WAV file with a .flac extension; it now encodes real (compressed) FLAC
|
||||
- ProRes 422 export always failed; it now encodes 10-bit 4:2:2 correctly
|
||||
- Fix VP8 video export with audio (muxed into WebM instead of an incompatible container)
|
||||
- The WebP "Quality" slider had no effect
|
||||
- Starting a new file now fully resets the audio engine, so instruments and voices from the previous project no longer linger
|
||||
- Fix oscillator/synth phase drift over long playback
|
||||
|
||||
# 1.0.7-alpha:
|
||||
Changes:
|
||||
- HDR video support: PQ/HLG/BT.2020 video is now read correctly (decoded to scene-linear), with a per-document output mode (clip vs highlight rolloff) and 10-bit HDR export (HEVC Main10, PQ or HLG)
|
||||
|
||||
@@ -58,21 +58,13 @@ Lightningbeam is developed on the `main` branch. The project has been rewritten
|
||||
- ✅ Audio engine with node graph processing
|
||||
- ✅ GPU waveform rendering with mipmaps
|
||||
- ✅ Video decoding integration
|
||||
- ✅ Export system (video, image, audio, animated GIF, SVG)
|
||||
- ✅ Node editor UI
|
||||
- 🚧 Piano roll editor (in progress)
|
||||
- 🚧 Export system (in progress)
|
||||
- 🚧 Node editor UI (planned)
|
||||
- 🚧 Piano roll editor (planned)
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Download a prebuilt release
|
||||
|
||||
If you don't want to build from source, prebuilt binaries for each release are on the
|
||||
[GitHub releases page](https://github.com/skykooler/lightningbeam/releases). Download the build for
|
||||
your platform and run it directly.
|
||||
|
||||
### Building from source
|
||||
|
||||
#### Prerequisites
|
||||
### Prerequisites
|
||||
|
||||
- Rust (stable toolchain via [rustup](https://rustup.rs/))
|
||||
- System dependencies:
|
||||
@@ -82,7 +74,7 @@ your platform and run it directly.
|
||||
|
||||
See [docs/BUILDING.md](docs/BUILDING.md) for detailed setup instructions.
|
||||
|
||||
#### Building and Running
|
||||
### Building and Running
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
|
||||
@@ -0,0 +1,859 @@
|
||||
# Streaming Media To/From Disk — Plan
|
||||
|
||||
**Goal:** Lightningbeam must handle audio and video files (and raster animation, and
|
||||
image assets) of *arbitrary length/size*. Anywhere we touch media we should stream from
|
||||
and to disk when the data is too large to fit comfortably in memory, rather than loading
|
||||
the entire file regardless of size.
|
||||
|
||||
**Scope of this document:** audio, video, raster frames, image-asset paging, **and the
|
||||
`.beam` container format** — these turned out to be one problem, not two. Streaming on load
|
||||
is impossible while the container forces a full decode, so the container decision (below)
|
||||
is now part of this plan.
|
||||
|
||||
## Deferred bugs (do at the end)
|
||||
- [x] **Timeline thumbnail scroll (FIXED):** the strip tiled from the *clamped* visible-left of the
|
||||
clip, so when a clip was scrolled partly off the left it showed the clip's start content at the
|
||||
viewport edge. Now tiled from the clip's **true (unclamped) origin** over its full width, drawing
|
||||
only the tiles intersecting the visible rect (`draw_video_thumbnail_strip` in timeline.rs). Both
|
||||
render sites (collapsed-group + expanded-track) share the helper. *(Compiles; needs in-app check.)*
|
||||
- [x] **Clip thumbnails stop updating (FIXED):** the GPU texture cache was keyed by the *requested*
|
||||
content time, so once a tile cached the first (often far-off) thumbnail it never refreshed as
|
||||
closer ones loaded. `VideoManager::get_thumbnail_at` now also returns the **actual** thumbnail
|
||||
timestamp, and the cache keys on that — so a tile picks up a new texture when a closer thumbnail
|
||||
finishes generating. Existing `retain`-by-visible-clip cleanup keeps it bounded. *(Needs in-app check.)*
|
||||
|
||||
## Raster-keyframe-UI bugs — **[DONE]** (built the raster keyframe timeline UI, 2026-06-20)
|
||||
Both resolved by the raster-keyframe-timeline-UI work: timeline now draws a diamond per
|
||||
`RasterKeyframe` (mirrors vector), `K`/New Keyframe inserts a blank cel via `AddRasterKeyframeAction`
|
||||
(canvas refreshes), paint tools edit the active keyframe instead of lazily creating, diamonds are
|
||||
click-to-seek (pointing-hand cursor), playback prefetches frames, and onion skinning (raster+vector,
|
||||
tinted, Info-Panel settings) is in. (a) canvas-refresh-on-new-keyframe and (b) keyframes-on-timeline
|
||||
are both fixed.
|
||||
|
||||
## Noted enhancements (later, after the phases)
|
||||
- [x] **Surround → stereo downmix (DONE).** Done uniformly in `render_from_file` (`pool.rs`) so it
|
||||
covers every storage type (PCM/InMemory, compressed via symphonia, video-audio via ffmpeg — all
|
||||
flow through this mixer with the source kept multichannel in the read-ahead buffer). New
|
||||
`stereo_downmix_matrix(src_channels)` gives `[L][src]`/`[R][src]` coefficients for the conventional
|
||||
interleave order (FL FR FC LFE BL BR SL SR…) for 3/4/5/5.1/6.1/7.1: full level for the matching
|
||||
front, `1/√2` for centre + each surround, LFE dropped; each row normalized so |coef| sum ≤ 1 to
|
||||
prevent clipping (matches ffmpeg's default). Applied in both the direct-copy and sinc-resample
|
||||
paths (only when `dst==2 && src>2`; unknown layouts fall back to front L/R). Compiles clean.
|
||||
*(Needs in-app check: a 5.1 file now has centre/dialog present and isn't thin; not distorted/clipping.)*
|
||||
Native multichannel support remains a separate, larger project.
|
||||
- **Export speed (audited 2026-06-21):** a 1:14 1080p MP4 took ~9:06 (~7.4x realtime, ~135 ms/frame).
|
||||
Audit **refuted** the per-frame-seek theory — export decodes the source *sequentially*
|
||||
(`video.rs` `need_seek` is false once advancing forward), and readback is already async +
|
||||
triple-buffered. Real hotspots:
|
||||
- **[DONE] #1 — per-frame renderer rebuild.** The export pump built a fresh `vello::Renderer`
|
||||
(full wgpu pipeline init) + empty `ImageCache` *every egui repaint* (`main.rs` ~6218). Now built
|
||||
once per export and reused; also fixed lazy-image export (the throwaway cache had no container
|
||||
path). **Expected the dominant win.**
|
||||
- **[DONE] #2a — encode swscale rebuilt per frame.** `CpuYuvConverter::convert` now caches the
|
||||
RGBA→YUV420p `scaling::Context` + frames in `new()` instead of per call.
|
||||
- **[TODO] #2b — decode swscale + stride-repack** per frame in `video.rs:294-320` (shared with
|
||||
scrubbing; cache the YUV→RGBA scaler on the decoder). Small win, modest risk.
|
||||
- **Result of #1+#2a (measured):** ~7.4x → **~1.74x realtime** (130.7 s for 4488 frames @ 60 fps;
|
||||
34 fps). Per-stage avg: Render(CPU build) 15 ms, **Readback(GPU latency) 42 ms**, Extract 1.3 ms,
|
||||
Convert 5.7 ms.
|
||||
- **Now GPU-bound.** Per ~87 ms poll cycle the CPU does ~66 ms (3× build 45 + convert 17 + extract 4)
|
||||
but the GPU does ~87 ms (3 × ~29 ms composite) → GPU saturated at ~29 ms/frame; "Readback 42 ms" is
|
||||
queue latency, not transfer (8 MB is sub-ms).
|
||||
- **[SKIP] #3 GPU YUV / #5 pacing** — both only trim the CPU side, which is already *under* the GPU.
|
||||
Won't move a GPU-bound throughput.
|
||||
- **[TODO, big] Reduce the GPU composite (~29 ms/frame).** The per-layer HDR pipeline (Vello render →
|
||||
linear → composite, ×layers) is the wall, shared with live rendering. Options: batch composite
|
||||
passes; a fast-path skipping HDR compositing for simple single-layer/no-blend docs; cache unchanged
|
||||
layers' scenes (CPU-side, only helps if it later becomes CPU-bound). Render-architecture project.
|
||||
- Non-issues: per-frame seek, blocking readback, audio. (`video.rs:237` container-reopen-on-seek is
|
||||
a latent cost but doesn't fire on forward export.)
|
||||
- **AAC export NaN guard (done):** `convert_chunk_to_planar_f32` now sanitizes non-finite samples
|
||||
(NaN/Inf → 0, finite clamped to [-1,1]) like the integer paths, with a one-time warning — a stray
|
||||
non-finite render sample no longer fails the whole export. Upstream NaN source (effect/automation/
|
||||
decode) still worth chasing if it recurs.
|
||||
- [x] **Persist video thumbnails (DONE).** Mirrors waveform persistence: each clip's thumbnails are
|
||||
PNG-encoded + packed into one opaque `LBTN` blob (editor owns the format; `encode/decode_thumbnail_blob`
|
||||
in main.rs), stored as a `MediaKind::Thumbnail` row keyed by `thumbnail_media_id(clip_id)` (clip id XOR
|
||||
a fixed sentinel). Save: a cheap Arc-clone snapshot (`VideoManager::snapshot_all_thumbnails`) rides the
|
||||
`FileCommand::Save`, PNG-encoded off the UI thread in the worker, written by `save_beam` (kept in place
|
||||
on re-save). Load: `load_beam_sqlite` reads the packs into `LoadedProject.thumbnail_blobs`; the editor
|
||||
decodes + `insert_thumbnail`s them on a background thread and **gates regeneration** (`register_loaded_videos`
|
||||
skips clips with persisted thumbnails). Bonus: thumbnails show even if the source video file is missing.
|
||||
**Partial sets are persisted and resumed** (not thrown away): the `LBTN` blob (v2) carries a `complete`
|
||||
flag (`VideoManager.thumbnails_complete`, marked when the keyframe pass finishes). On load, complete
|
||||
packs are restored + skip regeneration; *partial* packs are restored AND generation is resumed —
|
||||
`generate_keyframe_thumbnails` takes a `should_skip` predicate (`has_thumbnail_near`) so it only decodes
|
||||
the keyframes not already covered. `insert_thumbnail` is now sorted + idempotent (fixes a latent
|
||||
unsorted-`binary_search` bug and makes concurrent restore + resume race-safe). So a save 50 min into a
|
||||
2 h video keeps that work and continues from there on reload.
|
||||
Container tests still green; all crates compile. *(Needs in-app check: reload = instant thumbnails for
|
||||
complete clips; a mid-generation save resumes from where it left off on reload.)*
|
||||
**Size assessment (done):** thumbnails are 128px wide, height by aspect (72px at 16:9 →
|
||||
128×72×4 ≈ **36 KB raw** each; 4:3 ≈ 49 KB), generated **one per ~5 s** (capped `interval_secs`,
|
||||
at keyframes — so ~12/min). Raw: ~0.5 MB per 1:14 clip, ~26 MB/hour, ~52 MB/2 h. Compressed for
|
||||
on-disk: JPEG ~3–6 KB/thumb → **~6 MB/2 h**; PNG ~8–15 KB → ~14 MB/2 h. So persistence is cheap
|
||||
(≤ the waveform's ~36 MB/2 h), especially as JPEG. Plan: encode each clip's thumbnails (JPEG) +
|
||||
their timestamps into one blob, a new `MediaKind::Thumbnail` row keyed by the clip/media id (mirror
|
||||
the waveform persistence: write on save, restore via `insert_thumbnail` on load, regenerate if
|
||||
absent). The 5 s interval already bounds count; no extra budget needed.
|
||||
- **Progressive waveform on first import:** generation streams the whole file before the
|
||||
waveform appears (several seconds for large files). Since `build_waveform_pyramid` already
|
||||
streams, emit partial floors as it advances (e.g. flush every N seconds of decoded audio via
|
||||
the existing `waveform_result` channel + chunked GPU upload) so the overview fills in across
|
||||
the clip left-to-right instead of appearing all at once. Persistence saves only the final
|
||||
complete pyramid.
|
||||
|
||||
## Guiding principle
|
||||
Three subsystems already have the right streaming primitive; most of the work is wiring,
|
||||
bounding caches, and adding a residency window. The recurring pattern:
|
||||
|
||||
> Keep tiny metadata always-resident, fault the heavy payload in on demand keyed by a
|
||||
> stable ID, and evict everything outside a window around the playhead.
|
||||
|
||||
---
|
||||
|
||||
## Audit summary (where we stand today)
|
||||
|
||||
### Correctly streaming / bounded
|
||||
- Video frame decode/seek/playback (`lightningbeam-core/src/video.rs:191` `get_frame` —
|
||||
keyframe-index seek + decode-until-target, one frame resident).
|
||||
- WAV/AIFF import via mmap (`daw-backend/src/audio/engine.rs:2328`).
|
||||
- Webcam capture encodes directly to disk (`lightningbeam-core/src/webcam.rs`).
|
||||
- `WaveformCache` (100MB cap), decoder `LruCache` (20 frames), export render loop (≤3
|
||||
frames in flight).
|
||||
- The compressed-audio disk reader `daw-backend/src/audio/disk_reader.rs`
|
||||
(`CompressedReader` + 3s `ReadAheadBuffer`) — **correct but never activated** (Phase 1a).
|
||||
|
||||
### Fully-loaded, unbounded by file length (the problems)
|
||||
| Site | Issue |
|
||||
|---|---|
|
||||
| `daw-backend/src/io/audio_file.rs:344` `decode_progressive` | Decodes whole compressed file into a `Vec<f32>`; de-facto playback source. |
|
||||
| `daw-backend/src/audio/pool.rs:1071` `load_file_into_pool` | Every audio file in a saved project fully decoded to `InMemory` on open. |
|
||||
| `lightningbeam-core/src/video.rs:711` `extract_audio_from_video` | Whole video audio track into one `Vec<f32>`. |
|
||||
| `lightningbeam-core/src/video.rs:412` `VideoManager.frame_cache` | Unbounded `HashMap` of full-res RGBA frames; grows while scrubbing. |
|
||||
| `export/mod.rs:388-400` | Mux step buffers all compressed packets into `Vec`s; O(duration). |
|
||||
| `lightningbeam-core/src/raster_layer.rs:115` `RasterKeyframe.raw_pixels` | ~8MB/frame at 1080p; all keyframes decoded from PNG at load (`file_io.rs:611-640`), never evicted. |
|
||||
| `lightningbeam-editor/src/gpu_brush.rs:1051` `raster_layer_cache` | Unbounded GPU texture `HashMap`. |
|
||||
| `lightningbeam-core/src/renderer.rs:25` `ImageCache` | Unbounded decoded image cache (asset textures). |
|
||||
| `Document.image_assets` (`document.rs:206`) | Every image asset's compressed bytes resident for document life. |
|
||||
|
||||
---
|
||||
|
||||
## Container format decision: `.beam` → SQLite *(DECIDED)*
|
||||
|
||||
The `.beam` container moves from a **ZIP archive** to a **SQLite database file** (same
|
||||
`.beam` extension). This is the foundation the rest of the plan builds on.
|
||||
|
||||
### Why
|
||||
ZIP can stream `Stored` entries in place (via `data_start()`), but it has **no in-place
|
||||
mutation** — every save and every raster frame write-back rewrites the whole archive — and
|
||||
embedded PCM is rarely mmap-aligned. The current load path is even worse: it reads each
|
||||
ZIP audio entry fully, decodes FLAC → re-encodes WAV → base64 → base64-decodes → temp file
|
||||
→ full Symphonia decode → resident `Vec<f32>` (`file_io.rs:513-604`, `pool.rs:1071`).
|
||||
|
||||
SQLite dissolves the single-file-vs-performance tension:
|
||||
- **Single file** — beginner-friendly, behaves like a file on every OS (no package-folder
|
||||
confusion; we have no bundle magic on Linux/Windows).
|
||||
- **Streaming reads** — `sqlite3_blob_open` / `blob_read(offset, len)` gives seekable,
|
||||
chunked reads through the pager (mmap mode for the DB). For chunked streaming the
|
||||
pager-copy is negligible vs. decode cost, so the lack of zero-copy mmap doesn't matter.
|
||||
- **Cheap, crash-safe mutation** — raster frame write-back is a transactional `UPDATE`;
|
||||
save is a metadata write + dirty-blob updates. **ACID** means a force-quit / power loss /
|
||||
crash mid-save can't corrupt the project (ZIP and package-dirs both have to hand-roll
|
||||
atomicity).
|
||||
- **Inspectable / scriptable** — `sqlite3` CLI; `beam_inspector.py` can read it directly.
|
||||
|
||||
**Net effect: there is no scratch directory anywhere in this plan.** Media stream via blob
|
||||
reads (or external paths); raster frames live in blob rows and write back transactionally.
|
||||
|
||||
### Large-media policy: packed OR referenced
|
||||
Two storage modes per media item, both supported:
|
||||
- **Packed** — bytes live in the DB. To stay under SQLite's ~2GB per-blob ceiling (and to
|
||||
make reads naturally chunked), large media is split into **multiple blob-chunk rows**
|
||||
(e.g. 64 MB/chunk); streaming reads address `(chunk_index, offset)`.
|
||||
- **Referenced** — the DB stores only a path; bytes stay on disk (useful for shared media
|
||||
on a network drive, or media too large/volatile to pack).
|
||||
|
||||
**Default-mode preference for files over the per-blob limit (~2GB):**
|
||||
- A user preference `large_media_default: Pack | Reference` controls what happens to
|
||||
imports above the threshold.
|
||||
- The **first time** the user imports a media file over the limit, **prompt** them
|
||||
(Pack vs Reference), apply it, and **persist the choice** as the preference for future
|
||||
large imports (changeable later in settings).
|
||||
- Files under the limit are packed by default (chunked only if needed).
|
||||
|
||||
### Schema sketch
|
||||
```
|
||||
media(
|
||||
id BLOB PRIMARY KEY, -- stable Uuid
|
||||
kind INTEGER, -- audio | video | raster | image-asset
|
||||
codec TEXT, -- "flac","mp3","png",... (original, lossless-preserving)
|
||||
storage INTEGER, -- 0 = packed, 1 = referenced
|
||||
ext_path TEXT, -- set when storage = referenced
|
||||
total_len INTEGER, -- bytes (packed) for chunk math
|
||||
channels INTEGER, sample_rate INTEGER, width INTEGER, height INTEGER -- kind-specific meta
|
||||
)
|
||||
media_chunk(
|
||||
media_id BLOB, chunk_index INTEGER, bytes BLOB,
|
||||
PRIMARY KEY (media_id, chunk_index)
|
||||
)
|
||||
project_json(id INTEGER PRIMARY KEY CHECK (id = 0), data TEXT) -- existing project.json, verbatim
|
||||
meta(key TEXT PRIMARY KEY, value TEXT) -- version, created, modified
|
||||
```
|
||||
`project.json` stays the same serialized `BeamProject` for now — only its container and the
|
||||
media storage change. A migration reads a legacy ZIP `.beam` and writes the SQLite form on
|
||||
first open/save.
|
||||
|
||||
### Streaming reads from packed media
|
||||
A `BlobReader` implementing `Read + Seek` over `media_chunk` rows feeds the existing
|
||||
streaming consumers unchanged: `CompressedReader` (audio) decodes from it instead of a
|
||||
`File`; the video decoder seeks within it; raster `UPDATE`s a chunk. Referenced media uses a
|
||||
plain `File` exactly as `do_import_audio` already does for originals today.
|
||||
|
||||
---
|
||||
|
||||
## Phase 1 — Audio: activate what already exists *(highest impact, lowest effort)*
|
||||
|
||||
### 1a. Turn on the compressed-audio disk reader
|
||||
The `CompressedReader` + 3-second `ReadAheadBuffer` in `disk_reader.rs` is complete but
|
||||
never invoked (`DiskReaderCommand::ActivateFile` / `DiskReader::create_buffer` are never
|
||||
called; `AudioClip::read_ahead` at `clip.rs:63` is hard-wired to `None`).
|
||||
- On compressed import (`engine.rs:2381`) and during playback setup, activate the file and
|
||||
assign `AudioClip::read_ahead`.
|
||||
- Change `decode_progressive` (`io/audio_file.rs:344`) to produce only the downsampled
|
||||
waveform overview (min/max peaks) the UI needs, then drop decoded PCM. Playback comes
|
||||
from the ring buffer, not RAM.
|
||||
- Verify `render_from_file` (`pool.rs:449`) reads from `read_ahead` when `data()` is empty.
|
||||
|
||||
**Risk:** the real-time thread must never block on disk. The ring buffer prefetches ~2s
|
||||
ahead; underruns degrade to silence (live) or block-wait (export), which `disk_reader.rs`
|
||||
already distinguishes.
|
||||
|
||||
### 1b. Stream on project load *(depends on the SQLite container)*
|
||||
Three coupled changes (none works alone):
|
||||
1. Replace `load_file_into_pool`'s full decode (`pool.rs:1071`) with the same branching as
|
||||
`do_import_audio`: PCM → mmap (referenced) or in-memory for tiny packed PCM; compressed
|
||||
(incl. FLAC) → `from_compressed` placeholder backed by a `BlobReader` (packed) or `File`
|
||||
(referenced). The claxon FLAC→WAV→base64 round-trip in `file_io.rs:533-591` is deleted.
|
||||
2. **Bulk read-ahead activation:** loaded clips are deserialized directly
|
||||
(`audio_backend.project`), bypassing `AddAudioClip`, so the Phase 1a wiring never fires
|
||||
for them. After the engine installs the project, walk all audio clips and
|
||||
`create_buffer` + `ActivateFile` + set `read_ahead` for every clip referencing a
|
||||
`Compressed` pool entry. (`CompressedReader::open` needs a variant that takes a
|
||||
`BlobReader` instead of a path for packed media.)
|
||||
3. Pool entries carry storage mode (packed-chunks vs referenced path) from the `media`
|
||||
table instead of base64 `embedded_data`.
|
||||
|
||||
### 1c. Video's embedded audio track — stream from the video via ffmpeg
|
||||
|
||||
**Interim stopgap (shipped):** `extract_audio_from_video_to_wav` streams the decoded audio to
|
||||
a temp WAV, imported via `import_audio_sync` (mmap). Fixes the RAM OOM but writes the whole
|
||||
uncompressed track to `/tmp` (fills small temp partitions) and the temp path doesn't survive
|
||||
save/reload. **Superseded by the design below.**
|
||||
|
||||
**Proper design — stream the video's audio track on demand, never materialized.**
|
||||
|
||||
*Enabler:* `daw-backend` already depends on `ffmpeg-next` (used for MP3/AAC encoding), so the
|
||||
ffmpeg audio decoder lives beside `CompressedReader` in `daw-backend/src/audio/`. No
|
||||
cross-crate work (`core → daw-backend` is one-way). `CompressedReader` already has the needed
|
||||
interface.
|
||||
|
||||
1. **`VideoAudioReader` (ffmpeg)** — mirrors `CompressedReader`:
|
||||
`open(path)`, `decode_next(&mut Vec<f32>) -> frames` (resample → interleaved f32 at native
|
||||
rate; reuse the old extraction resampler), `seek(target_frame) -> actual`,
|
||||
`sample_rate`/`channels`/`total_frames`.
|
||||
2. **Source dispatch:** `enum StreamSource { Compressed(CompressedReader), Video(VideoAudioReader) }`
|
||||
(or a small `trait AudioFrameSource`) held by the reader thread; ring buffer / prefetch /
|
||||
export-blocking unchanged. `DiskReaderCommand::ActivateFile` gains a `kind: SourceKind`.
|
||||
3. **Pool model:** `AudioStorage::VideoAudio { video_path, decoded_for_waveform, decoded_frames,
|
||||
total_frames }` (near-copy of `Compressed`); `data()` empty, playback via `read_ahead`. Pool
|
||||
entry `path` = the video file.
|
||||
4. **Engine API:** `EngineController::add_video_audio_sync(video_path) -> usize` — ffmpeg-probe
|
||||
the audio track (rate/channels/frames/duration, no decode), build the pool entry, return index.
|
||||
5. **Clip activation:** extend the Phase 1a `AddAudioClip` wiring — if entry is `VideoAudio`,
|
||||
make the buffer + `ActivateFile{kind:VideoAudio, path:video_path}` + set `clip.read_ahead`.
|
||||
One ffmpeg context + 3 s buffer per active clip instance.
|
||||
6. **Import flow:** `import_video` calls `add_video_audio_sync(video_path)` →
|
||||
`AudioClip::new_sampled`. **Remove** `extract_audio_from_video_to_wav`, the temp-WAV
|
||||
handling, and the now-dead `add_audio_file_sync`. No WAV / `/tmp` / RAM.
|
||||
7. **Save/load:** the `VideoAudio` entry serializes as a path reference to the video (no media
|
||||
bytes — the video is already referenced by its `VideoClip`); reconstruct on load by
|
||||
re-probing. Fixes the stopgap's reload fragility (nothing to persist).
|
||||
8. **Waveform overview:** background ffmpeg pass emitting **downsampled peaks only** (bounded
|
||||
memory) into the existing waveform path — shared with the Phase 1a `decode_progressive`
|
||||
cleanup.
|
||||
|
||||
**Sample accuracy (required — video audio must stay frame-synced with other clips):**
|
||||
Coarse ffmpeg seeks are NOT sufficient. `VideoAudioReader::seek(target_frame)` must:
|
||||
- coarse-seek to a point ≤ target, then **decode-and-discard** to land exactly on
|
||||
`target_frame`, tracking the absolute sample position from decoded-frame PTS (discard whole
|
||||
frames before target; for the frame straddling target, drop its leading samples). After
|
||||
`seek`, `decode_next` yields samples starting at exactly `target_frame`.
|
||||
- This makes frame N of the video-audio pool entry correspond to the exact timeline position,
|
||||
so it mixes sample-aligned with mmap/InMemory clips. Continuous decode advances frame-exact.
|
||||
- *Consistency note:* `CompressedReader` should get the same decode-discard alignment (its
|
||||
current coarse-seek-then-write-at-target can misalign by up to a GOP after a seek). Fold in
|
||||
while here, or at least flag.
|
||||
|
||||
*Model decision (confirmed):* the video's audio stays a **separate, editable `AudioClip`** on
|
||||
an audio track, backed by the `VideoAudio` pool entry — users can move/trim/mute/detach it.
|
||||
|
||||
*Build order:* `VideoAudioReader` + `StreamSource` → pool `VideoAudio` variant →
|
||||
`add_video_audio_sync` + activation → swap `import_video` (remove WAV path) → sample-accurate
|
||||
seek (both readers) → waveform-peaks pass.
|
||||
|
||||
---
|
||||
|
||||
## Phase 2 — Video: bound the caches *(small, isolated)*
|
||||
|
||||
### 2a. Bound `VideoManager.frame_cache`
|
||||
`video.rs:412` — convert the unbounded `HashMap<(Uuid,i64), Arc<VideoFrame>>` to an LRU
|
||||
mirroring the decoder-level cache (`video.rs:34`). Frame-count or byte budget.
|
||||
|
||||
### 2b. Stream the export mux
|
||||
`export/mod.rs:388-400` — interleave-write packets to the output as produced (compare PTS,
|
||||
write the earlier stream) instead of collecting all then writing. O(duration) → O(1).
|
||||
|
||||
---
|
||||
|
||||
## Phase 3 — Raster: disk-backed keyframe paging *(the heavy one)* **[locked design]**
|
||||
|
||||
Today `load_beam_sqlite` (`file_io.rs:564`) eagerly `decode_png`s **every** raster keyframe's
|
||||
`Raster` media row into `RasterKeyframe.raw_pixels` (`raster_layer.rs:115`, `w·h·4` ≈ 8 MB @
|
||||
1080p, `#[serde(skip)]`), never evicts, has an unbounded GPU texture cache, and holds full-frame
|
||||
undo snapshots. `raw_pixels` is the working rep (edits write it, save reads it, render reads it),
|
||||
`has_pixels()` = `!raw_pixels.is_empty()`, `keyframe_at` is a `partition_point` binary search, and
|
||||
the container is opened only at load/save (no live handle).
|
||||
|
||||
**Design (confirmed with user):** keep `raw_pixels` as the working rep; make residency explicit
|
||||
via a `RasterStore` + an editor-run fault-in/evict pass *before* the immutable render. Async
|
||||
fault-in (no scrub hitch), with a **low-res image proxy** shown until the full frame lands.
|
||||
Decisions: small window (±~2 keyframes); **dirty (edited-unsaved) frames stay fully resident**
|
||||
(spill-to-scratch deferred); fault-in is **async**; proxy is a **per-keyframe low-res RGBA image**
|
||||
(PNG/WebP, correct alpha), NOT a video (VP9-alpha was rejected as finicky for negligible disk win).
|
||||
|
||||
### Drive-by (Arc pixels): DROPPED
|
||||
Investigated and rejected: `raw_pixels` has ~64 access sites, and most `.clone()`s genuinely need
|
||||
an owned `Vec<u8>` (undo buffers, export, GPU readback) so `Arc<Vec<u8>>` would force `(*p).clone()`
|
||||
and still copy. The only beneficiary, the per-frame `renderer.rs:550` Vello clone, is on the
|
||||
**legacy/dead** path — the live HDR canvas renders raster as `RenderedLayerType::Raster` → GPU
|
||||
upload in `stage.rs` which passes a `&[u8]` slice and uploads only on cache-miss (no per-frame
|
||||
clone). Not worth 64 edits. Start at 3a.
|
||||
|
||||
### 3a. Lazy async fault-in + image proxy
|
||||
- **[DONE 3a-1]** Lazy load: full-decode removed; `raw_pixels` empty on load, `needs_fault_in`
|
||||
armed recursively; canvas records misses → App pages in via `RasterStore.load_pixels`.
|
||||
- **[DONE 3a-2]** Async: page-in runs on a background thread (deduped via `raster_loads_inflight`);
|
||||
results applied at top of `update()`. No UI block on cold scrub.
|
||||
- **[DONE 3a-3]** Image proxy: `MediaKind::RasterProxy` (≤192px PNG, derived id), written
|
||||
beside each resident full PNG on save + eager-decoded on load into `RasterKeyframe::proxy`.
|
||||
Separate `proxy_layer_cache` (own LRU, budget 64); the raster render blits the proxy mapped to
|
||||
the keyframe's FULL logical dims (upscales via sampler) when the full texture isn't resident.
|
||||
*(Proxies exist only after a save+reload; eager decode → lazy/paged is a refinement for huge
|
||||
paint projects.)*
|
||||
|
||||
- **`RasterStore`** (core): current `.beam` path + a read-only connection; `load_pixels(kf_id,w,h)`
|
||||
reads the `Raster` row and `decode_png`s it. Set/cleared by the editor on load + save-as.
|
||||
- **Save:** alongside the full PNG, write a low-res RGBA proxy per resident keyframe
|
||||
(`MediaKind::RasterProxy`, ≤~480px long edge, keyed by `kf.id`).
|
||||
- **Load:** stop eager full-decode; decode **proxies** eagerly (cheap → instant scrub everywhere);
|
||||
leave full `raw_pixels` empty.
|
||||
- **Fault-in pass** (editor, `&mut document` + store, each frame before render): for each raster
|
||||
layer ensure the active keyframe ±N is requested; load full PNGs on a **background thread pool**;
|
||||
on arrival, set `raw_pixels` + `texture_dirty`. Render uses full `raw_pixels` if resident, else the
|
||||
upscaled proxy. Reused by the exporter (already frame-by-frame).
|
||||
|
||||
### 3b. Residency window + eviction **[DONE]**
|
||||
- Added `#[serde(skip)] dirty: bool` (edited-since-persist; distinct from `texture_dirty`). Set on
|
||||
stroke/fill/paint-bucket/floating-lift commits + undo/redo; cleared on save (which re-arms the LRU).
|
||||
- Implemented as a fault-in-recency **LRU** (`RASTER_RESIDENT_MAX = 12`), not a strict ±N window:
|
||||
evict the oldest **clean** frame (drop `raw_pixels`, re-arm `needs_fault_in`); the shown frame is
|
||||
always most-recent so it's protected; **dirty frames never evicted**. Save preserves evicted frames'
|
||||
rows via `media_exists` (no data loss) and walks all layers to match load.
|
||||
*(Refinement deferred: count budget → byte budget for 4K resolution-robustness.)*
|
||||
|
||||
### 3c. Bound the GPU cache **[DONE for raster_layer_cache]**
|
||||
`raster_layer_cache` (`gpu_brush.rs`, `HashMap<Uuid,CanvasPair>`, Rgba16Float ping-pong
|
||||
≈ `w·h·16`/entry, was **unbounded**) → recency LRU (`RASTER_LAYER_CACHE_MAX = 12`) in
|
||||
`ensure_layer_texture`: bump-to-most-recent + evict oldest; shown frames protected. F3 overlay
|
||||
now shows tracked VRAM (raster cache MB + count). *(Refinements: count→byte budget; raise/headroom
|
||||
if >12 raster layers are visible at once. Export `raster_cache` lives one export — fine. Vello
|
||||
`ImageCache` is image *assets* → Phase 4.)*
|
||||
|
||||
### 3d. Undo memory **[DONE]**
|
||||
`RasterStrokeAction`/`RasterFillAction` stored `buffer_before`+`buffer_after` full frames.
|
||||
Now store a `RasterDiff` (`actions/raster_diff.rs`) — changed bbox before/after only, computed in
|
||||
`new()`, full buffers dropped. Undo/redo apply onto the keyframe's resident pixels; the editor
|
||||
faults the target frame in first (`Action::raster_resident_hint` + `peek_undo/redo_raster_hint`),
|
||||
correct because a clean evicted frame's container bytes == its logical state. Non-resident base ⇒
|
||||
skip (no corruption). Unit-tested round-trip. *(Refinement: compress full-canvas-fill diffs, whose
|
||||
bbox is the whole frame.)*
|
||||
|
||||
### 3e. Prefetch frames **[DONE for playback]**
|
||||
Implemented for playback: each update during playback, page in the next `PREFETCH_AHEAD=4`
|
||||
upcoming keyframes per raster layer (reusing the async worker + `raster_loads_inflight` dedup), so
|
||||
full frames are resident before the playhead arrives — fixes "proxy on every frame"/flicker during
|
||||
playback. *(Caveat: with many simultaneous raster layers the 12-frame resident budget may evict a
|
||||
prefetched frame before it's shown — raise budget or scale prefetch if that surfaces. Scrub-direction
|
||||
prefetch still TODO.)*
|
||||
|
||||
Original note: *(future, after 3d — pure latency win, no correctness need)*
|
||||
Fault-in is reactive (page in only on a render miss), so a never-visited frame still shows the
|
||||
proxy for a beat before the full lands. **Prefetch the full pixels for frames about to be shown**:
|
||||
on scrub/playback, dispatch background page-ins for the active keyframe ±N in the direction of
|
||||
playhead motion (and during playback, the next K keyframes), reusing the 3a-2 async worker +
|
||||
`raster_loads_inflight` dedup. Keep prefetched frames in the 3b LRU so they're still bounded; cap
|
||||
concurrent prefetch loads so scrubbing fast doesn't thrash the disk. Optional: also prewarm the GPU
|
||||
texture (3c cache) for the immediate next frame. Net effect: cold scrubbing/playback shows full-res
|
||||
frames with no proxy flicker. Proxy stays as the instant fallback when prefetch can't keep up.
|
||||
|
||||
### Build order & tests
|
||||
1. Arc drive-by — COW make_mut test. 2. 3a fault-in + store + proxy — load→empty-until-faulted,
|
||||
PNG round-trip, proxy-then-swap. 3. 3b window/evict/dirty — residency ≤ window while scrubbing,
|
||||
dirty never evicted. 4. 3c GPU bound. 5. 3d undo diffs reproduce pre-stroke buffer exactly.
|
||||
|
||||
---
|
||||
|
||||
## Phase 3.5 — Image textures in vector scenes **[DONE 2026-06-21]** *(prereq for Phase 4; fixed DCEL-broken image import)*
|
||||
|
||||
**Done:** 3.5a — import/drop places an image as a borderless image-filled rectangle
|
||||
(`AddShapeAction::image_rect`), centered (direct import) or at the drop point (library drag);
|
||||
renderer now maps the image brush onto the fill's bounding box (was anchored at world origin →
|
||||
only a corner showed); `SetImageFillAction` + an **Image** fill-type tab (None|Solid|Gradient|Image)
|
||||
with an asset picker in the Info Panel. 3.5b — image bytes persist as `MediaKind::ImageAsset` rows in
|
||||
the `.beam` (kept-in-place; `ImageAsset.data` is `skip_serializing` + container-backed; old base64
|
||||
projects migrate on re-save); eager-read on load. *(ImageCache still unbounded — Phase 4 adds the
|
||||
usage-based LRU/lazy paging.)*
|
||||
|
||||
### (original plan below)
|
||||
## Phase 3.5 — Image textures in vector scenes *(prereq for testing Phase 4; fixes DCEL-broken image import)*
|
||||
|
||||
**Why:** Phase 4 pages *image assets*, but there's currently no way to get an image asset into a
|
||||
vector scene — so nothing to page. This also repairs image import, half-broken since the DCEL switch.
|
||||
|
||||
**Current state (audited 2026-06-21):**
|
||||
- *Works:* `import_image` (`main.rs`) decodes dims + creates an `ImageAsset` (raw bytes embedded in
|
||||
`Document::image_assets`, serialized as **base64 in project JSON**). The renderer's image-fill paths
|
||||
are **complete** — GPU/Vello (`renderer.rs:~1160`, `ImageBrush` via `ImageCache.get_or_decode`) and
|
||||
CPU/tiny-skia (`renderer.rs:~1486`). `Fill::image_fill` (`vector_graph/mod.rs:110`) and
|
||||
`Face::image_fill` (`dcel2/mod.rs:117`) fields exist and render when set.
|
||||
- *Broken/missing (the workflow):*
|
||||
1. **Drop image → canvas is stubbed:** `stage.rs:~11782` and `main.rs:~4924` both just print
|
||||
"Image drag to stage not yet supported with DCEL backend". Nothing is added to the scene.
|
||||
2. **No way to assign an image fill:** no `SetImageFillAction` (only `SetFillPaintAction` for
|
||||
color/gradient); no Info-Panel picker. `Fill`/`Face.image_fill` are never populated.
|
||||
3. **DCEL faces never get `image_fill`** (`dcel2/import.rs:275` always `None`; topology copies from
|
||||
parent which is also `None`).
|
||||
4. **Not in the container:** `MediaKind::ImageAsset` exists but is **dead** — image bytes live only
|
||||
as base64 in project JSON. Not chunked, not pageable (so Phase 4 can't page them).
|
||||
|
||||
**Tasks:**
|
||||
- **3.5a — Place + assign.** Replace the two drop stubs: dropping an image onto a vector layer creates
|
||||
a rectangle face sized to the image at the drop point with `image_fill = asset_id`. Add
|
||||
`SetImageFillAction` (set/clear an image fill on the selected face/shape; mirrors `SetFillPaintAction`)
|
||||
+ an Info-Panel image-asset picker for the selected shape's fill. Populate `Face.image_fill` in DCEL
|
||||
(and keep it through topology ops — already copied from parent).
|
||||
- **3.5b — Persist in the container.** Write image assets as `MediaKind::ImageAsset` rows in the `.beam`
|
||||
SQLite (like raster/audio: write on save kept-in-place on re-save; read on load), keyed by asset id;
|
||||
drop the base64-in-JSON embedding (or keep a tiny ref). This is the storage Phase 4 pages from.
|
||||
- **3.5c — Lazy decode hook.** Image bytes load from the container into `ImageCache` on first render
|
||||
(decode → `ImageBrush`/`Pixmap`). Leave `ImageCache` **unbounded for now**; Phase 4 adds the
|
||||
usage-based LRU/eviction (this phase just makes there *be* real, container-backed image assets to page).
|
||||
- **Tests:** import→drop→render round-trip; save/reload preserves the image fill + reads bytes from the
|
||||
container (not JSON); CPU and GPU render paths both show the image.
|
||||
|
||||
---
|
||||
|
||||
## Phase 4 — Asset paging by usage + LRU *(vector's real cost is assets, not geometry)*
|
||||
|
||||
Vector geometry is compact flat POD (tens of KB/frame, no cached tessellation/DCEL) — leave
|
||||
it resident. The heavy, evictable thing is the **image assets** referenced by fills.
|
||||
|
||||
**Data model.**
|
||||
- `ImageAsset` (`clip.rs:250`): `path: PathBuf` + `data: Option<Vec<u8>>` (whole compressed
|
||||
file bytes) + dims. Imported fully into `data` at `main.rs:3936`.
|
||||
- All assets resident in `Document.image_assets: HashMap<Uuid, ImageAsset>` (`document.rs:206`).
|
||||
- Decoded form in `ImageCache` (`renderer.rs:25`): `HashMap<Uuid, Arc<ImageBrush>>` + CPU
|
||||
`Pixmap` map, keyed by asset id, **unbounded**.
|
||||
- A `Fill` references an asset by `image_fill: Option<Uuid>` (`vector_graph/mod.rs:110`).
|
||||
Same UUID may appear in many fills/keyframes/layers and recursively through clip instances.
|
||||
**No asset→frame or frame→asset index exists today.**
|
||||
|
||||
**Two evictable tiers:** Tier 1 = compressed bytes (`ImageAsset.data`, droppable, reload
|
||||
from blob row or external `path`); Tier 2 = decoded pixels (`ImageCache` + GPU textures —
|
||||
the heavy one).
|
||||
|
||||
**Progress (2026-06-21):**
|
||||
- **[DONE] Tier 2 — bound the decoded `ImageCache`.** 256 MB **usage-LRU**: every
|
||||
`get_or_decode`/`_cpu` bumps the asset's recency; inserts past budget evict the least-recently-used
|
||||
(a miss re-decodes from `asset.data`). Achieves usage-based eviction via render-access recency
|
||||
(simpler than the frame→asset enumeration below; that enumeration is only needed for *prefetch*).
|
||||
- **[DONE] Tier 1 — lazy compressed bytes.** `ImageCache` holds the container path (threaded
|
||||
App.current_file_path → SharedPaneState → VelloRenderContext) and pages bytes on a decode miss via
|
||||
`read_packed_media_readonly`; `load_beam_sqlite` no longer eager-reads → instant load, compressed
|
||||
bytes don't accumulate. `asset.data` is still used when resident (fresh import / old base64 project).
|
||||
*(Refinement: persistent read connection vs open-per-miss.)*
|
||||
- **[DONE] Prefetch.** `assets_needed_at(document, time)` enumerates image ids in the visible vector
|
||||
layers' active keyframes; during playback the stage decodes the ~0.5s-ahead set into the cache.
|
||||
*(Refinements: nested clip-instance recursion; background-thread decode.)*
|
||||
|
||||
**Phase 4 = DONE** (image asset paging by usage + LRU).
|
||||
|
||||
### 4a. Frame→asset enumeration (incl. nested clips — see note below)
|
||||
A function `assets_needed_at(time) -> HashSet<Uuid>`: walk each visible vector layer's active
|
||||
`ShapeKeyframe`, collect `fill.image_fill` across its `VectorGraph.fills`, **recursing into
|
||||
clip instances** with the outer→inner local-time mapping. This is "needed now". Scanning
|
||||
upcoming keyframes (and upcoming nested-clip keyframes) gives "needed soon" for prefetch.
|
||||
|
||||
### 4b. Usage bookkeeping (the multi-frame problem)
|
||||
Maintain a reverse index `asset_id → usage count` (fills referencing it across the whole
|
||||
document), updated incrementally as edits add/remove `image_fill`s (hook the fill-mutation
|
||||
paths in `vector_graph` and the relevant actions).
|
||||
- count 0 → dead, fully evictable / GC candidate.
|
||||
- count > 0 → keep metadata; residency of `data`/decoded pixels driven by **proximity to
|
||||
playhead**, not by count (a high-count asset far from the playhead is still evicted).
|
||||
|
||||
Residency decision: `resident = needed-now ∪ needed-soon`; beyond that, an **LRU with a byte
|
||||
budget** for referenced-but-distant assets (covers scrubbing back without a reload).
|
||||
Eviction never touches an asset in needed-now.
|
||||
|
||||
### 4c. Bound the decoded tier
|
||||
Convert `ImageCache`'s two maps to LRU/byte-budgeted (`renderer.rs:25`) and bound the GPU
|
||||
image-texture cache the same way, keyed to the residency window.
|
||||
|
||||
### Nested-clip prefetch (important)
|
||||
A clip instance placed on an outer frame has its **own internal timeline of keyframes**,
|
||||
each of which can reference its own image assets. Prefetch must therefore:
|
||||
- Recurse through clip instances when computing both needed-now and needed-soon.
|
||||
- Map outer playhead time → each nested clip's local time, and look ahead along the
|
||||
**nested** timeline (not just the outer one) so assets used by an upcoming *inner*
|
||||
keyframe are loaded before the nested clip reaches it.
|
||||
- Deduplicate across the whole recursion (an asset shared by outer and inner frames counts
|
||||
once); the usage index handles refcounting.
|
||||
|
||||
---
|
||||
|
||||
## Cross-cutting: a shared residency abstraction
|
||||
|
||||
A generic **`PagedStore<Id, Payload>`** with three consumers — always-resident metadata,
|
||||
disk backing, residency = window/needed-set around playhead + LRU byte budget:
|
||||
|
||||
| Consumer | Metadata kept | Paged payload | Backing | "Needed now" key |
|
||||
|---|---|---|---|---|
|
||||
| Raster keyframes (Ph 3) | id, dims, time | `raw_pixels` + GPU texture | SQLite blob row (`UPDATE` on write-back) | active keyframe per layer |
|
||||
| Image assets (Ph 4) | id, dims, storage | `data` bytes + decoded pixels/texture | SQLite blob row or external path | fills' `image_fill` set at time (recursive) |
|
||||
| Video frames (Ph 2a) | — | RGBA frame | source via ffmpeg seek | requested timestamps |
|
||||
|
||||
Audio stays separate (real-time ring buffer, different constraints). The frame→asset
|
||||
enumeration + usage index is unique to Phase 4.
|
||||
|
||||
---
|
||||
|
||||
## Sequencing
|
||||
1. **Phase 1a** — done; independent of the container, works with the current ZIP loader.
|
||||
2. **Phase 2** — small, isolated, independently shippable; container-independent.
|
||||
3. **Phase 0 (container)** — `.beam` ZIP → SQLite + `BlobReader` + large-media policy +
|
||||
legacy-ZIP migration. Prerequisite for 1b/1c/3/4.
|
||||
4. **Phase 1b** — streaming pool loader + bulk read-ahead activation (on the SQLite store).
|
||||
5. **Phase 1c** — depends on 1b's pool path.
|
||||
6. **Phase 3** — the substantial build; implement `PagedStore` over blob rows.
|
||||
7. **Phase 4** — thin layer on the same abstraction + the frame→asset/usage index.
|
||||
|
||||
Phase 1a and Phase 2 can ship now; everything else waits on Phase 0 (the container).
|
||||
|
||||
---
|
||||
|
||||
## Status
|
||||
- [~] Phase 1a — activate compressed-audio disk reader ← **in progress**
|
||||
- [x] Wire `ActivateFile` + assign `clip.read_ahead` on `AddAudioClip` for compressed
|
||||
pool files (`engine.rs:909`). Per-clip reader keyed by `clip_id`; matches the
|
||||
existing `DeactivateFile` convention in `RemoveAudioClip`. Compiles clean.
|
||||
- [ ] Stop `decode_progressive` (`io/audio_file.rs:344`) from accumulating/streaming the
|
||||
full PCM; emit only the downsampled waveform overview. (Crosses into the UI
|
||||
waveform pipeline — `AudioDecodeProgress` consumer — so handled as its own step.)
|
||||
- [ ] Runtime verification: confirm a compressed clip actually plays from the ring
|
||||
buffer (was effectively silent before, since `read_ahead` was always `None`).
|
||||
- [~] **Phase 0 — container migration `.beam` ZIP → SQLite** ← **in progress**
|
||||
- [x] SQLite schema (`media`, `media_chunk`, `project_json`, `meta`) + `rusqlite` dep
|
||||
(bundled) — `lightningbeam-core/src/beam_archive.rs`
|
||||
- [x] `BlobReader` (`Read + Seek` over `media_chunk`, owns its own read-only connection,
|
||||
opens a blob handle per read with rowids resolved once) — for `CompressedReader` /
|
||||
video decoder in 1b. 5 integration tests pass (`tests/beam_archive.rs`): json
|
||||
round-trip, packed full read, streaming reads + seeks across chunk boundaries,
|
||||
referenced-path, overwrite-replaces-chunks.
|
||||
- [x] Packed (chunked) + referenced media write/read API; `is_sqlite()` format detection;
|
||||
`MediaKind`/`MediaStorage`/`MediaMeta`/`MediaInfo`.
|
||||
- [x] `BeamArchive::transaction()` / `BeamTxn` — in-place transactional save (only
|
||||
changed rows written; unchanged large media never rewritten); orphan cleanup via
|
||||
`retain_media`. 7 archive tests pass (added txn-grouping + rollback). Per user: save
|
||||
must NOT copy+rename for existing SQLite files.
|
||||
- [x] Wire `save_beam` to `BeamArchive` — in-place txn for existing SQLite, temp+rename
|
||||
only for new/migrated files. Audio → packed (or referenced ≥2GB) `media` rows;
|
||||
raster → PNG `media` rows keyed by keyframe id. FLAC→WAV→base64 save round-trip
|
||||
deleted (now packs original bytes with their codec).
|
||||
- [x] Wire `load_beam` — format dispatch: SQLite (`load_beam_sqlite`) vs legacy ZIP
|
||||
(`load_beam_zip_legacy`, kept verbatim). SQLite load reconstitutes packed audio into
|
||||
`embedded_data` so the existing pool loader is unchanged (streaming = Phase 1b).
|
||||
- [x] Legacy ZIP `.beam` → SQLite migration: `is_sqlite()` routes load; saving a
|
||||
ZIP-loaded project writes SQLite (migrates on save). Editor compiles end-to-end.
|
||||
- [x] Large-media policy: packed (chunked) vs referenced — `LargeMediaMode {Ask,Pack,
|
||||
Reference}`; save honors it for files ≥`LARGE_MEDIA_THRESHOLD`. Packing streams from
|
||||
disk via `put_media_packed_from_path` (chunk-by-chunk, never loads the whole file).
|
||||
`Ask` behaves as `Reference` at save time.
|
||||
- [x] `large_media_default` user preference: persisted in `AppConfig`, editable in
|
||||
Preferences → Advanced (incl. resetting to `Ask` to re-trigger the prompt).
|
||||
- [x] First-import-over-threshold prompt: `note_possible_large_media` (hooked into
|
||||
import_audio/video/image) queues a one-time modal; choice persists to config.
|
||||
Threshold shown in the modal is derived from the constant.
|
||||
- [ ] Runtime verification: save a real project, reopen it, confirm audio + raster survive
|
||||
round-trip; confirm an old ZIP `.beam` still opens and migrates on save.
|
||||
- [ ] (Optimization, later) FLAC-compress packed PCM/WAV audio; raster disk-dirty flag to
|
||||
skip unchanged frames on in-place save (Phase 3).
|
||||
|
||||
> Note: the crate's internal `#[cfg(test)]` modules (`clip.rs`, `effect_layer.rs`) have
|
||||
> pre-existing compile breakage (old `Beats`/`TempoMap` API) unrelated to this work; it
|
||||
> blocks `cargo test --lib`, so `beam_archive` tests live in `tests/` (integration) which
|
||||
> build the lib in normal mode. Worth fixing separately.
|
||||
- [x] Phase 1b — stream on project load (PACKED audio path complete & user-verified: streams on load,
|
||||
waveform generates + persists, sample-accurate seeking). Referenced-path streaming + MP3 seek index
|
||||
+ proper video-audio reload remain as noted follow-ups.
|
||||
- **Decision (user):** cross-crate packed streaming via an **inversion-of-control factory** —
|
||||
daw-backend defines the interface, core implements it over `BlobReader`. Keeps the audio
|
||||
engine container-agnostic. (Alternatives rejected: daw-backend owning rusqlite = layering
|
||||
violation; referenced-only-first = leaves packed <2GB in RAM.)
|
||||
- **Current load reality (why this is needed):** *nothing* streams on load today — every entry
|
||||
is fully decoded to a PCM `Vec<f32>`. Packed audio is base64-reconstituted into `embedded_data`
|
||||
(`load_beam_sqlite`) → written to a temp file → `load_file_into_pool` full-decodes; referenced
|
||||
audio also full-decodes via `load_file_into_pool`; and the Phase 1a/1c disk-reader activation
|
||||
never fires for loaded clips (they bypass `AddAudioClip`).
|
||||
- [x] **B1/B2 foundation (DONE, headless-tested):** in `disk_reader.rs` — `trait MediaByteSource:
|
||||
Read+Seek+Send+Sync { byte_len }` + `trait AudioBlobSourceFactory: Send+Sync { open(media_id)
|
||||
-> Box<dyn MediaByteSource> }`; `SymphoniaByteSource` adapter (impl `MediaSource`,
|
||||
is_seekable/byte_len); `CompressedReader::open_source(src, ext)` sharing probe via a
|
||||
refactored `from_mss`; `enum StreamOpen { Path, Source{src,ext} }`; `StreamSource::open` and
|
||||
`DiskReaderCommand::ActivateFile` now take `StreamOpen` (engine site wraps `Path`); re-exported
|
||||
`AudioBlobSourceFactory`/`MediaByteSource` at `daw_backend::audio`. Test
|
||||
`tests/compressed_source_stream.rs` decodes an in-memory WAV through a `Cursor`-backed
|
||||
`MediaByteSource` (proves probe+decode+seek over a byte stream). daw-backend compiles clean.
|
||||
- [x] **B3 (engine, DONE):** `Engine.blob_source_factory: Option<Arc<dyn AudioBlobSourceFactory>>` +
|
||||
`EngineController::set_blob_source_factory` (via `Query::SetBlobSourceFactory`, ordered before
|
||||
`SetProject` on the same queue). `AudioFile.packed_media_id: Option<String>` (Some ⇒ open via
|
||||
factory using `original_format` as the ext hint; None ⇒ `StreamOpen::Path`). Activation factored
|
||||
into `Engine::activate_streaming_for(reader_id, pool_index)`, used by `AddAudioClip` and bulk.
|
||||
- [x] **C (core factory, DONE):** `file_io::blob_source_factory(beam_path)` → `BeamBlobFactory`
|
||||
implementing `AudioBlobSourceFactory` over `BeamArchive::open_blob_reader`. `BlobReader` holds a
|
||||
`!Sync` rusqlite `Connection`, so it's wrapped in `SyncBlobReader` (a `Mutex` used via `get_mut`
|
||||
on the hot path — no runtime locking) to satisfy Symphonia's `MediaSource: Send + Sync`. Installed
|
||||
by the editor between `load_audio_pool` and `set_project`.
|
||||
- [x] **D (load-path, DONE — packed audio):** `load_beam_sqlite` now streams packed audio whose codec
|
||||
is recognized (`is_streamable_audio_codec`) — leaves `embedded_data` empty so the pool builds a
|
||||
Compressed placeholder with `packed_media_id`; no base64, no temp file, no decode. `serialize`
|
||||
round-trips packed entries by media id (so in-place re-save keeps the row). Non-audio codecs
|
||||
(video-container audio tracks) keep the legacy reconstitution path → **no regression**.
|
||||
- [x] **E (bulk activation, DONE):** `SetProject` calls `Engine::activate_all_streaming_clips` —
|
||||
walks every loaded audio clip and `activate_streaming_for` (create_buffer + `ActivateFile` + set
|
||||
`read_ahead`), the loaded-clip equivalent of the Phase 1a wiring.
|
||||
- [x] **Waveform-on-load for streamed audio (DONE):** streaming broke the old waveform path (it came
|
||||
from the full in-RAM decode, which no longer happens). Added
|
||||
`disk_reader::build_waveform_pyramid_from_source(Box<dyn MediaByteSource>, ext, B)` (load-time
|
||||
counterpart of the path-based builder). On load, the editor background-generates a pyramid for any
|
||||
streamed entry lacking a persisted one (opens the packed blob via a local factory), sending the
|
||||
floor through the same `waveform_result` channel `update()` drains; the next save persists it.
|
||||
Verified in-app: packed MP3 **streams + plays** (`Activated reader=0, kind=CompressedAudio`); the
|
||||
overview now fills in shortly after load.
|
||||
- **Headless tests pass** (compressed_source_stream, video_audio_stream, waveform_pyramid); all three
|
||||
crates compile clean. **Needs in-app verification:** the waveform appears after load (background gen),
|
||||
then instantly on subsequent loads once saved; RAM stays flat on a big project.
|
||||
- [x] **Seek alignment fix (DONE):** streamed compressed audio was ~1.2s off *after seeking*
|
||||
(fine from the start). `CompressedReader::seek` used `SeekMode::Coarse`, which for MP3
|
||||
byte-estimates the position and seeds the timestamp from that estimate — wrong for VBR / files
|
||||
whose header padding the estimate ignores, so `actual_ts` (and thus the buffer's frame labels)
|
||||
landed ~1.2s early. Switched to `SeekMode::Accurate`: Symphonia counts frame *headers* (no
|
||||
decode) from a true anchor (current pos, or rewind-to-0 for backward seeks) → exact `actual_ts`;
|
||||
the existing sub-frame `pending_discard` finishes the job. FLAC/OGG seek cheaply (seek tables);
|
||||
a long MP3 backward seek walks headers from 0 (I/O, not decode). Tests still green.
|
||||
- [ ] **Deferred (follow-up):** per-file **seek index** for elementary streams (MP3) — a one-time
|
||||
header scan (ts↔byte map) to make far seeks O(1) instead of an Accurate header-walk from the
|
||||
anchor. Matters for multi-hour MP3s; song-length files are fine as-is.
|
||||
- [x] **Proper video-audio reload (DONE):** a video's audio is now stored as a **path reference** to
|
||||
the video (never packed/embedded as audio media) and **re-probed via FFmpeg** on load into a
|
||||
streaming `VideoAudio` entry — `AudioPoolEntry.is_video_audio` flag drives both `serialize`
|
||||
(reference, not pack), `save_beam` (`reference_it |= is_video_audio`), and `load_from_serialized`
|
||||
(`VideoAudioReader::open` → `from_video_audio`). Fixes 5.1 audio losing its channels on reload
|
||||
(the old Symphonia reconstitution collapsed it); also no more decode-whole-video-to-RAM / temp
|
||||
files on load. Old saves (video mis-packed as audio) self-heal on the next save.
|
||||
- [ ] **Deferred (follow-up):** stream *referenced* (external-path) **audio** on load too — replace
|
||||
`load_file_into_pool`'s full decode with the `do_import_audio` branching (PCM → mmap, compressed
|
||||
→ `from_compressed` placeholder). Higher risk (touches the working referenced path); packed
|
||||
covers the common <2GB case first.
|
||||
- [x] **DONE: packed video streaming.** Small videos pack into the `.beam`
|
||||
(a `MediaKind::Video` blob at the clip id, `VideoClip.media_id` referencing it) and stream **both
|
||||
frames and audio** from the DB blob via FFmpeg. The `AVIOContext`-over-`Read+Seek` shim lives in
|
||||
the new `ffmpeg-blob-io` crate (`BlobInput`, version-pinned `=8.0.0`/`=8.0.1`), isolating the
|
||||
unsafe + ABI coupling. Frames: `video.rs` `VideoSource{Path,Packed}` opens a fresh `BlobReader`
|
||||
per decoder/seek/scan. Audio: `VideoAudioReader::open_source` over the same blob (the
|
||||
`disk_reader.rs` `StreamSource` blocker is removed); save points the linked video-audio pool
|
||||
entry's `media_id` at the video row so it streams from the same blob. Tests: ffmpeg-blob-io AVIO
|
||||
unit tests (WAV via Cursor + seek + open/drop loop), core `packed_video_stream` (blob→AVIO→Input),
|
||||
`beam_archive` packed-video round-trip, daw-backend `open_source` (compiles; can't link in the
|
||||
container — user runtime-verifies actual A/V playback).
|
||||
- [~] Phase 1c — video embedded-audio track ← **stopgap shipped; proper design next**
|
||||
- [x] Stopgap: `extract_audio_from_video_to_wav` streams to a temp WAV → `import_audio_sync`
|
||||
(mmap). Fixed the ~2.8GB-`Vec<f32>` OOM. But writes the whole WAV to `/tmp` (fills
|
||||
small temp partitions) and the temp path doesn't survive reload.
|
||||
- [~] **Proper design** (see "Phase 1c" body): stream the video's audio on demand via a new
|
||||
ffmpeg `VideoAudioReader` in the disk reader — no extraction, no `/tmp`, no RAM; path
|
||||
reference survives save/load.
|
||||
- [x] **Step 1 (DONE):** `VideoAudioReader` (ffmpeg) + `StreamSource` enum + `SourceKind`
|
||||
in `disk_reader.rs`. Sample-accurate seek (coarse seek + decode-discard to exact
|
||||
frame via PTS). 2 integration tests pass (`daw-backend/tests/video_audio_stream.rs`):
|
||||
in-order decode + sample-exact seek at several targets. (Found: mono frames have an
|
||||
empty channel layout → must `set_channel_layout` before resampling, else swr returns
|
||||
AVERROR_INPUT_CHANGED.) Lib compiles clean; `StreamSource` `#[allow(dead_code)]`
|
||||
until wired. `VideoAudioReader` made `pub` for the integration test.
|
||||
- [x] **Step 2 (DONE):** `AudioStorage::VideoAudio { decoded_for_waveform, decoded_frames,
|
||||
total_frames }` + `AudioFile::from_video_audio` (path = the video file). `data()`
|
||||
empty / `read_samples()` 0 (streamed). `Query::AddVideoAudioSync` +
|
||||
`do_add_video_audio` (probes via `VideoAudioReader::open`, no decode) +
|
||||
`EngineController::add_video_audio_sync`. `GetPoolAudioSamples` surfaces VideoAudio's
|
||||
waveform overview too. daw-backend compiles clean; probe `total_frames` test passes.
|
||||
- [x] **Step 3 (DONE):** reader thread now holds `StreamSource` (opens via
|
||||
`StreamSource::open(path, kind)`, dispatches `sample_rate()/channels()/seek/decode_next`);
|
||||
`ActivateFile` carries `kind: SourceKind`; `#[allow(dead_code)]` removed. `AddAudioClip`
|
||||
activation maps `Compressed`→`CompressedAudio`, `VideoAudio`→`VideoAudio`, creates the
|
||||
read-ahead buffer + `ActivateFile{kind}` + sets `clip.read_ahead`. Compressed path is
|
||||
behaviorally identical (StreamSource::Compressed wraps the same CompressedReader).
|
||||
daw-backend + editor compile clean; VideoAudioReader tests still pass.
|
||||
⚠️ Not runtime-verified — needs in-app check that compressed audio still plays (no
|
||||
regression) and that an activated VideoAudio clip produces sound.
|
||||
- [x] **Step 4 (DONE):** `import_video` now calls `add_video_audio_sync(video_path)` →
|
||||
pool index, fetches channels/sample_rate via `get_pool_file_info`, makes the
|
||||
`AudioClip` with the video's duration. **No WAV / /tmp / RAM.** Removed the stopgap
|
||||
(`extract_audio_from_video_to_wav` + WAV helpers + `ExtractedAudioInfo`), dead
|
||||
`add_audio_file_sync` (+ `Query::AddAudioFileSync` / `QueryResponse::AudioFileAddedSync`
|
||||
/ handler), and the now-unreachable `AudioExtractionResult::NoAudio`. Kept
|
||||
`import_audio_sync` (still used by normal audio import). daw-backend + editor clean.
|
||||
**→ Feature is live end-to-end; ready for in-app testing.**
|
||||
- [x] **Step 5 (DONE):** `CompressedReader` now seeks sample-accurately too — coarse
|
||||
symphonia seek + decode-discard (`pending_discard` set from `seeked.actual_ts` in
|
||||
`seek`, applied in `decode_next`, which continues rather than reporting EOF when a
|
||||
whole packet is discarded). So compressed clips no longer drift vs video audio after
|
||||
a seek. Test `compressed_reader_seek_is_sample_accurate` passes (the WAV coarse seek
|
||||
lands pre-target, exercising the discard). `CompressedReader` made `pub` for the test.
|
||||
- [~] Step 6: **bounded waveform overview** — replaces today's full-resolution
|
||||
`raw_audio_cache`/GPU waveform (which doesn't scale: it stores every sample at mip 0,
|
||||
so a long file is multi-GB on GPU + RAM — the same memory issue, and the Phase 1a
|
||||
`decode_progressive` leftover). Design below. Slices: (1a) streaming pyramid builder
|
||||
+ (1b) persistence + (1c) min/max GPU upload, then (2) LRU tile cache + re-decode floor.
|
||||
- [x] **Slice 1a (DONE):** `daw-backend/src/audio/waveform_pyramid.rs` —
|
||||
`WaveformPyramidBuilder` streams interleaved samples, accumulates the floor, and
|
||||
reduces `BRANCH(4):1` at `finish` into a root-first pyramid (convention B:
|
||||
`levels[0]`=root envelope, `levels.last()`=floor, `.root()`/`.floor()` accessors).
|
||||
Ragged last buckets reduce over available children (no value padding). Bounded
|
||||
(~22 MB/2 h @ B=256). 7 integration tests pass (`tests/waveform_pyramid.rs`):
|
||||
bucket min/max, partial flush, multi-level envelope == global min/max, root-first
|
||||
ordering, stereo channels, size bound, chunk-agnostic.
|
||||
- [~] **Slice 1b (data layer DONE; orchestration folded into 1c):**
|
||||
- [x] Generation bridge `disk_reader::build_waveform_pyramid(path, kind, B)` — streams
|
||||
a decode (`StreamSource` over symphonia/ffmpeg) into the builder; bounded
|
||||
memory (one chunk + the pyramid). Test: envelope matches the signal through
|
||||
both backends.
|
||||
- [x] Serialization `WaveformPyramid::to_bytes`/`from_bytes` (LBWF blob; f32 texels —
|
||||
f16 a later size optimization). Round-trip test + rejects truncated/garbage.
|
||||
- [x] `MediaKind::Waveform` in the SQLite container (keyed by the audio item's id).
|
||||
- [ ] Orchestration (with 1c).
|
||||
- [~] **Slice 1c (in-memory floor overview DONE; persistence next):**
|
||||
- [x] `waveform_gpu`: `PendingUpload.minmax` flag + `pack_texel` helper; `upload_audio`
|
||||
threads `minmax` (frame_stride 4, packs `(Lmin,Lmax,Rmin,Rmax)` directly).
|
||||
The texture is already Rgba16Float and the GPU mipgen builds zoom-out levels, so
|
||||
only the texel-packing differs. Render the floor at **effective rate `sr/B`** (so
|
||||
time→texel maps B samples/texel) and `total_frames = floor_texel_count`.
|
||||
- [x] `AppConfig.waveform_floor_samples_per_texel` (default 256, user-configurable).
|
||||
- [x] App: `waveform_minmax_pools: HashMap<usize, u32>` (pool → `B`, carries the floor rate
|
||||
with full float precision) + a `(pool, packed_floor, sr, channels, B)` results channel;
|
||||
drained in `update()` → `raw_audio_cache.insert(floor)` + flag pool + `waveform_gpu_dirty`.
|
||||
- [x] Generation: on video-audio import Success, the same bg thread streams
|
||||
`disk_reader::build_waveform_pyramid(path, VideoAudio, B)` once and sends the packed
|
||||
`floor()`. (Video-audio has no in-RAM samples, so this is what makes its waveform appear.)
|
||||
- [x] Threaded `waveform_minmax_pools` through the pane-context (`panes/mod.rs` +
|
||||
main.rs construction) → `render_layers` → **both** render sites (collapsed-group
|
||||
~timeline.rs:3048 AND expanded-track ~3613): compute `total_frames = len/4`,
|
||||
`eff_sr = sr/B`, set `minmax`. Compiles clean (editor `cargo check` = 0 errors).
|
||||
- [x] Shader fix: `waveform.wgsl` now reads the **nearest integer LOD via `textureLoad`**
|
||||
instead of sampling a fractional mip. Trilinear blends two levels whose row-major
|
||||
linearizations differ → horizontal shift that flips each 0.5 of `mip_f` (= each 2x
|
||||
zoom step), the "every other zoom level is offset" artifact. **User-confirmed fixed:**
|
||||
features hold position at every zoom and line up with playback.
|
||||
See memory `waveform-shader-fractional-mip-offset`.
|
||||
- [x] **Persistence (done):** the full pyramid is serialized (`to_bytes`) on generation and
|
||||
kept in `App.waveform_pyramid_blobs`. `save_beam` writes it as a `MediaKind::Waveform`
|
||||
row keyed by a **deterministic id derived from the pool index** (`file_io::waveform_media_id`,
|
||||
"LBWF" sentinel in the high 32 bits) — independent of how the audio bytes are stored, so
|
||||
it works for packed/referenced/video-audio alike, and an in-place re-save reuses the row.
|
||||
Carried in/out via a transient `#[serde(skip)] AudioPoolEntry.waveform_blob` and a
|
||||
`waveform_blobs` field on `FileCommand::Save`. `load_beam_sqlite` reads the row back;
|
||||
the editor restores `raw_audio_cache`/`waveform_minmax_pools`/`waveform_pyramid_blobs`
|
||||
+ flags `waveform_gpu_dirty` after the backend loads the pool (using each entry's
|
||||
`sample_rate` for `eff_sr`, the stored `B` for the rate). No re-decode on load.
|
||||
`register_loaded_videos` only loads frames (not audio), so there is no redundant
|
||||
regeneration to suppress. Compiles clean across all three crates.
|
||||
|
||||
### Waveform LOD pyramid design (step 6)
|
||||
A min/max LOD pyramid (tree of zoom-level textures): fully zoomed out → envelope; fully zoomed
|
||||
in → per-sample; seamless between.
|
||||
|
||||
- **One streaming decode pass** builds the whole pyramid down to a configurable **floor**
|
||||
`B` samples/texel (default 256), via a hierarchical reduction (each sample updates a running
|
||||
per-level min/max accumulator; a filled bucket emits a texel and folds into its parent —
|
||||
`branch` 4:1). Bounded memory: holds only the pyramid (~`N/B·4/3` texels ≈ **~14 MB / 2 h
|
||||
stereo @ B=256**), never the full samples. Full-res (B=1 ≈ 2.7 GB) is the only level NOT
|
||||
stored.
|
||||
- **Persist the pyramid** in the `.beam` SQLite container (a `waveform` media kind; session
|
||||
temp before first save). `B` is stored with it (preference is just the default for new gen).
|
||||
Persistence is load-bearing: it makes mid-zoom a cheap **disk read**, not a re-decode.
|
||||
- **Runtime = LRU tile cache** (GPU textures) loaded from the persisted pyramid on demand.
|
||||
Eviction is **ancestor-closed**: only evict an LRU node with no resident children ("a node is
|
||||
cleared only after its children") — so rendering can always walk up to a resident ancestor;
|
||||
detail sharpens in, never blanks. Root is tiny/hot → effectively pinned for free.
|
||||
- **Re-decode only below the floor** (texel < `B` samples): by then the visible window spans a
|
||||
tiny time range, so decoding it (via the sample-accurate seekable readers from steps 1–5 —
|
||||
the payoff) for true per-sample detail is cheap. This removes the large-span re-decode gap:
|
||||
above the floor it's a disk read; below it the span is already small.
|
||||
- **Why a deep floor (not a coarse cutoff):** a coarse-only pinned set would force the first
|
||||
on-demand level to re-reduce a huge time span per tile. Persisting deep makes every level a
|
||||
disk read; `B` is a size-vs-crossover knob (smaller B = bigger pyramid, cheaper re-decode).
|
||||
- `waveform_gpu` needs a **min/max texel upload** (`Lmin,Lmax,Rmin,Rmax` per texel) instead of
|
||||
min=max-per-sample; the existing compute mipgen still builds the mip chain *within* a tile.
|
||||
|
||||
**Decisions (locked):** branch 4:1; floor `B≈256` samples/texel, **user-configurable**
|
||||
(`AppConfig.waveform_floor_samples_per_texel`, stored per-pyramid); 8192-wide tiles; LRU ~4
|
||||
viewports of fine tiles; persist pyramid in `.beam`.
|
||||
- [x] Video decoder concurrency (movie-length lag/freeze): keyframe-index scan now runs
|
||||
holding no VideoManager/decoder lock (brief locks only bracket it) → no more multi-second
|
||||
UI freeze on import/load; thumbnail generation uses a **dedicated** decoder and samples
|
||||
at keyframes (≈1 frame each vs whole-GOP) → no playback contention. Removed dead
|
||||
`VideoManager::build_keyframe_index`, `build_and_set_keyframe_index`, `downsample_rgba*`.
|
||||
- [x] Phase 2a — bound video frame cache. `VideoManager.frame_cache` (was an unbounded
|
||||
`HashMap<(Uuid,i64), Arc<VideoFrame>>` that grew per distinct frame during playback) is now an
|
||||
`LruCache` evicted by a **byte budget** (`FRAME_CACHE_BYTE_BUDGET` = 256 MB) rather than a frame
|
||||
count — robust across resolutions (a 4K frame is ~33 MB vs ~2 MB at 800×600). Byte total tracked
|
||||
on insert/evict/remove; `unload_video` pops per-clip keys (LruCache has no `retain`). Decoder-level
|
||||
cache was already LRU. Editor compiles clean. *(Not yet runtime-verified.)*
|
||||
- [x] Phase 2b — stream export mux. `export/mod.rs::mux_video_and_audio` no longer collects every
|
||||
packet into two `Vec`s before interleaving; it stream-merges the two inputs by PTS with one pending
|
||||
packet per stream (O(1) memory vs O(duration)). Same tie-break (`v_us <= a_us`) and drain-on-EOF
|
||||
behavior; output is byte-identical. Editor compiles clean. *(Not yet runtime-verified — needs an
|
||||
in-app export to confirm A/V sync.)*
|
||||
- [x] Phase 3a — lazy + async raster fault-in (`RasterStore` + background thread + image proxy)
|
||||
- [x] Phase 3b — raster residency LRU + eviction (dirty-flag data-loss safety)
|
||||
- [x] Phase 3c — bound raster GPU texture cache (recency LRU + F3 VRAM readout)
|
||||
- [x] Phase 3d — raster undo dirty-rect diffs (+ fault-in-before-undo)
|
||||
- [x] Phase 3.5 — image textures in vector scenes (fixed DCEL-broken image import; image-fill tab + picker; container-persisted)
|
||||
- [x] Phase 4 — image asset paging: Tier 2 decoded-cache byte-LRU, Tier 1 lazy container bytes, playback prefetch
|
||||
- [x] Phase 5 — fixed the broken `#[cfg(test)]` unit tests; **`cargo test --lib` green again**
|
||||
(daw-backend 17 passed, lightningbeam-core 264 passed). Wrapped stale raw-`f64` time literals
|
||||
in `Beats(...)` / passed `&TempoMap` to changed signatures (automation.rs, clip.rs,
|
||||
effect_layer.rs); fixed stale test setup (register a vector clip so `get_clip_duration` resolves)
|
||||
and a stale default expectation (shape `fill_color` defaults `None`). Surfaced + fixed one **real
|
||||
undo bug**: `DeleteFolderAction(MoveToParent)` reparented child subfolders but never restored them
|
||||
on rollback (orphaned them) — now tracked and restored. Production code otherwise untouched.
|
||||
@@ -0,0 +1,305 @@
|
||||
# Lightningbeam TODO
|
||||
|
||||
> ⚠️ **Stale entries:** Lightningbeam was rewritten from JavaScript to Rust. Any entry below
|
||||
> that cites `src/*.js` / `main.js` / `animation.js` predates that migration — the *issue* may
|
||||
> or may not still exist in the Rust codebase, but the file/line references are obsolete.
|
||||
> **Re-verify against the current Rust code before acting** (this covers the "Animation System
|
||||
> Refactoring" section and the JS-referencing "Known Issues" entries — node editor, default
|
||||
> interpolation, etc.). Items with no `.js` references are current.
|
||||
|
||||
## Animation System Refactoring *(STALE — JS-era migration notes; superseded by the Rust DCEL/keyframe system)*
|
||||
|
||||
### Completed
|
||||
- ✅ Implement AnimationData curve-based system (Keyframe, AnimationCurve, AnimationData classes)
|
||||
- ✅ Add GraphicsObject.currentTime property
|
||||
- ✅ Migrate shape rendering to use AnimationData curves (exists, zOrder)
|
||||
- ✅ Binary search optimization for keyframe lookups
|
||||
|
||||
### In Progress
|
||||
- Migrating from Frame-based to AnimationData curve-based system throughout codebase
|
||||
|
||||
### Pending Features
|
||||
|
||||
#### Animation Curve Enhancements
|
||||
- [ ] Implement extrapolation modes (separate for start vs end):
|
||||
- "hold" (default) - hold value at first/last keyframe
|
||||
- "extend" - linearly extend the curve beyond keyframes
|
||||
- "repeat" - repeat the animation
|
||||
- "decay" - exponential decay to a target value
|
||||
- [ ] Add position, scale, rotation animation curves for shapes
|
||||
- [ ] Add shape morphing/tweening between keyframes
|
||||
|
||||
#### Keyframing Behavior
|
||||
- [ ] Add user preference for keyframing behavior when editing objects:
|
||||
- Auto-keyframe (current default): create/update keyframe at current time
|
||||
- Edit previous (Flash-style): update most recent keyframe before current time
|
||||
- Ephemeral (Blender-style): changes don't persist without manual keyframe
|
||||
- Optional: Add modifier key (e.g. Shift) to toggle between modes
|
||||
|
||||
#### Shape Ordering
|
||||
- [ ] Add "Bring Forward" menu option (swap zOrder with shape in front)
|
||||
- [ ] Add "Send Backward" menu option (swap zOrder with shape behind)
|
||||
- [ ] Add "Bring to Front" menu option (set zOrder to max + 1)
|
||||
- [ ] Add "Send to Back" menu option (set zOrder to min - 1)
|
||||
|
||||
#### Code Cleanup
|
||||
- [ ] Remove all remaining references to Frame-based system
|
||||
- [ ] Remove legacy Frame class once migration is complete
|
||||
- [ ] Clean up GraphicsObject.shapes[] array (shapes should only live in Layers)
|
||||
|
||||
## Known Issues / Platform Limitations
|
||||
|
||||
### Animation: Tweens are broken (Rust codebase) — LOW PRIORITY
|
||||
- **Issue**: Animation tweening between keyframes (shape/vector interpolation, and the
|
||||
`tween_after` behavior on keyframes) does not work correctly in the current Rust app.
|
||||
Needs investigation + fix. Not urgent — revisit later.
|
||||
- (Older JS-codebase animation entries below reference `src/*.js` and are stale.)
|
||||
|
||||
### Audio: Oscillator Timbre Drift (Phase Accumulation Error)
|
||||
- **Issue**: Oscillators exhibit timbre changes over time due to floating-point phase accumulation errors
|
||||
- **Affected Files**:
|
||||
- `daw-backend/src/effects/synth.rs:117-120` (SimpleSynth)
|
||||
- `daw-backend/src/audio/node_graph/nodes/oscillator.rs:167-170` (OscillatorNode)
|
||||
- **Root Cause**: Current phase wrapping uses conditional subtraction (`if phase >= 1.0 { phase -= 1.0 }`), which accumulates f32 rounding errors over time, especially for long-playing notes
|
||||
- **Current Code**:
|
||||
```rust
|
||||
self.phase += frequency / sample_rate;
|
||||
if self.phase >= 1.0 {
|
||||
self.phase -= 1.0;
|
||||
}
|
||||
```
|
||||
- **Recommended Fix**: Replace with `.fract()` for numerically stable wraparound:
|
||||
```rust
|
||||
self.phase += frequency / sample_rate;
|
||||
self.phase = self.phase.fract();
|
||||
```
|
||||
- **Impact**: Medium - affects audio quality for sustained notes, becomes noticeable after several seconds
|
||||
- **Priority**: Medium - should be addressed before production use
|
||||
|
||||
### UI: Node Connections Render Behind VoiceAllocator Child Nodes
|
||||
- **Issue**: Connection lines (SVG paths) inside expanded VoiceAllocator nodes render behind child nodes due to z-index stacking
|
||||
- **Affected File**: `src/styles.css:1128`
|
||||
- **Root Cause**: Child nodes have `z-index: 10` while connection SVG paths have default/lower z-index
|
||||
- **Current Code**:
|
||||
```css
|
||||
.drawflow .drawflow-node.child-node {
|
||||
opacity: 0.9;
|
||||
border: 1px solid #5a5aaa !important;
|
||||
box-shadow: 0 2px 8px rgba(90, 90, 170, 0.3);
|
||||
z-index: 10;
|
||||
}
|
||||
```
|
||||
- **Recommended Fix**: Either:
|
||||
1. Remove `z-index: 10` from `.child-node` (simplest), or
|
||||
2. Add higher z-index to connection SVG paths, or
|
||||
3. Use CSS `isolation: isolate` on the VoiceAllocator contents area to create a new stacking context
|
||||
- **Impact**: Low - visual issue only, connections still function but appear to go "behind" nodes
|
||||
- **Priority**: Low - cosmetic issue that doesn't affect functionality
|
||||
|
||||
### UI: VoiceAllocator Child Nodes Don't Move with Parent
|
||||
- **Issue**: When a VoiceAllocator node is moved, its child nodes remain in their original positions instead of moving with the parent
|
||||
- **Affected File**: `src/main.js:6202-6207`
|
||||
- **Root Cause**: The `nodeMoved` event handler only handles the case where a child node is moved (resizes parent), but doesn't handle when the VoiceAllocator itself is moved
|
||||
- **Current Code**:
|
||||
```javascript
|
||||
editor.on("nodeMoved", (nodeId) => {
|
||||
const node = editor.getNodeFromId(nodeId);
|
||||
if (node && node.data.parentNodeId) {
|
||||
resizeVoiceAllocatorToFit(node.data.parentNodeId);
|
||||
}
|
||||
});
|
||||
```
|
||||
- **Recommended Fix**: Add logic to detect when a VoiceAllocator is moved and update all child node positions:
|
||||
```javascript
|
||||
editor.on("nodeMoved", (nodeId) => {
|
||||
const node = editor.getNodeFromId(nodeId);
|
||||
|
||||
// Case 1: A child node was moved - resize parent
|
||||
if (node && node.data.parentNodeId) {
|
||||
resizeVoiceAllocatorToFit(node.data.parentNodeId);
|
||||
}
|
||||
|
||||
// Case 2: A VoiceAllocator was moved - move all children
|
||||
if (node && node.data.nodeType === 'VoiceAllocator') {
|
||||
// Calculate delta from previous position (need to track)
|
||||
// Update all child node positions by the delta
|
||||
// Call editor.updateConnectionNodes() for parent and all children
|
||||
}
|
||||
});
|
||||
```
|
||||
- **Impact**: High - child nodes become disconnected from parent visually
|
||||
- **Priority**: High - breaks expected behavior of grouped nodes
|
||||
|
||||
### UI: VoiceAllocator Expansion Doesn't Update Connection Positions
|
||||
- **Issue**: When expanding/collapsing a VoiceAllocator, connection endpoints don't update to match the new port positions
|
||||
- **Affected File**: `src/main.js:6496-6555` (handleNodeDoubleClick function)
|
||||
- **Root Cause**: The expand/collapse logic shows/hides child nodes and resizes the container, but never calls `editor.updateConnectionNodes()` to refresh connection positions
|
||||
- **Current Code**: In `handleNodeDoubleClick()`, after expanding or collapsing:
|
||||
```javascript
|
||||
// Expand
|
||||
expandedNodes.add(nodeId);
|
||||
nodeElement.classList.add('expanded');
|
||||
nodeElement.style.width = '600px';
|
||||
nodeElement.style.height = '400px';
|
||||
// ... shows child nodes ...
|
||||
// Missing: editor.updateConnectionNodes(`node-${nodeId}`)
|
||||
```
|
||||
- **Recommended Fix**: Call `editor.updateConnectionNodes()` after resizing:
|
||||
```javascript
|
||||
// After expanding
|
||||
expandedNodes.add(nodeId);
|
||||
nodeElement.classList.add('expanded');
|
||||
// ... resize and show children ...
|
||||
|
||||
// Update connection positions for VoiceAllocator and all children
|
||||
editor.updateConnectionNodes(`node-${nodeId}`);
|
||||
for (const [childId, parentId] of nodeParents.entries()) {
|
||||
if (parentId === nodeId) {
|
||||
editor.updateConnectionNodes(`node-${childId}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
- **Impact**: Medium - connections appear in wrong positions until manually moved
|
||||
- **Priority**: Medium - visual issue that affects usability
|
||||
|
||||
### UI: Node Editor Allows Editing Without MIDI Layer Selected
|
||||
- **Issue**: The node editor pane allows adding/editing instrument nodes even when no MIDI layer is selected, and always uses hardcoded `trackId: 0`
|
||||
- **Affected File**: `src/main.js:6045-6920` (nodeEditor function)
|
||||
- **Root Cause**: The node editor never checks if `context.activeObject.activeLayer` exists or is a MIDI track, and all backend commands use hardcoded `trackId: 0`
|
||||
- **Current Code**: All graph commands hardcode track 0:
|
||||
```javascript
|
||||
const commandArgs = parentNodeId
|
||||
? {
|
||||
trackId: 0, // HARDCODED!
|
||||
voiceAllocatorId: editor.getNodeFromId(parentNodeId).data.backendId,
|
||||
nodeType: nodeType,
|
||||
x: x,
|
||||
y: y
|
||||
}
|
||||
: {
|
||||
trackId: 0, // HARDCODED!
|
||||
nodeType: nodeType,
|
||||
x: x,
|
||||
y: y
|
||||
};
|
||||
```
|
||||
- **Recommended Fix**:
|
||||
1. Check if activeLayer is a MIDI track before allowing edits:
|
||||
```javascript
|
||||
function getSelectedMidiTrack() {
|
||||
const activeLayer = context.activeObject?.activeLayer;
|
||||
if (!activeLayer || activeLayer.type !== 'midi') {
|
||||
return null;
|
||||
}
|
||||
return activeLayer;
|
||||
}
|
||||
```
|
||||
2. Show placeholder when no MIDI track selected:
|
||||
```javascript
|
||||
function nodeEditor() {
|
||||
const container = document.createElement("div");
|
||||
const midiTrack = getSelectedMidiTrack();
|
||||
|
||||
if (!midiTrack) {
|
||||
container.innerHTML = '<div class="placeholder">Select a MIDI layer to edit instruments</div>';
|
||||
return container;
|
||||
}
|
||||
// ... rest of node editor code ...
|
||||
}
|
||||
```
|
||||
3. Use actual track ID instead of hardcoded 0:
|
||||
```javascript
|
||||
const trackId = midiTrack.audioTrackId || 0;
|
||||
const commandArgs = { trackId, nodeType, x, y };
|
||||
```
|
||||
4. Add listener to refresh node editor when layer selection changes
|
||||
- **Impact**: High - allows editing wrong track's instrument graph, data corruption risk
|
||||
- **Priority**: High - can cause confusion and data loss
|
||||
|
||||
### Animation: Wrong Default Interpolation for Shape and Object Keyframes
|
||||
- **Issue**: Shape index and object transform keyframes default to "linear" interpolation but should default to "hold" (step function), and there's no UI to change interpolation after creation
|
||||
- **Affected Files**:
|
||||
- `src/models/animation.js:124` (Keyframe constructor defaults to "linear")
|
||||
- `src/main.js:2161` (shapeIndex keyframes default to "linear")
|
||||
- `src/main.js:2198` (object position/rotation/scale keyframes default to "linear")
|
||||
- `src/main.js:5910` (Timeline menu - missing tween options)
|
||||
- **Root Cause**:
|
||||
1. The Keyframe constructor defaults interpolation to "linear"
|
||||
2. Shape index keyframes preserve existing interpolation or default to "linear"
|
||||
3. Object transform keyframes explicitly use "linear"
|
||||
4. No menu options exist to change interpolation mode after keyframe creation
|
||||
- **Current Code**:
|
||||
- Keyframe constructor (animation.js:124):
|
||||
```javascript
|
||||
constructor(time, value, interpolation = "linear", uuid = undefined) {
|
||||
```
|
||||
- Shape index keyframes (main.js:2161):
|
||||
```javascript
|
||||
const interpolationType = existingShapeIndexKf ? existingShapeIndexKf.interpolation : 'linear';
|
||||
const shapeIndexKeyframe = new Keyframe(currentTime, newShapeIndex, interpolationType);
|
||||
```
|
||||
- Object keyframes (main.js:2198):
|
||||
```javascript
|
||||
const newKeyframe = new Keyframe(
|
||||
currentTime,
|
||||
currentValue,
|
||||
'linear' // Default to linear interpolation
|
||||
);
|
||||
```
|
||||
- **Expected Behavior**:
|
||||
- Shape index keyframes should default to "hold" (shapes shouldn't morph between versions)
|
||||
- Object transforms should default to "hold" (objects shouldn't move/rotate/scale between keyframes unless explicitly tweened)
|
||||
- Timeline menu should have options to convert between interpolation modes
|
||||
- **Recommended Fix**:
|
||||
1. Change shapeIndex default to "hold" (main.js:2161):
|
||||
```javascript
|
||||
const interpolationType = existingShapeIndexKf ? existingShapeIndexKf.interpolation : 'hold';
|
||||
```
|
||||
2. Change object keyframe default to "hold" (main.js:2198):
|
||||
```javascript
|
||||
const newKeyframe = new Keyframe(currentTime, currentValue, 'hold');
|
||||
```
|
||||
3. Add Timeline menu options (main.js:5910, in timelineSubmenu):
|
||||
```javascript
|
||||
{
|
||||
text: "Add Shape Tween",
|
||||
enabled: /* check if shape is selected and has keyframes */,
|
||||
action: () => {
|
||||
// Find shapeIndex curve for selected shape
|
||||
// Change interpolation between keyframes to "linear"
|
||||
}
|
||||
},
|
||||
{
|
||||
text: "Add Motion Tween",
|
||||
enabled: /* check if object is selected and has transform keyframes */,
|
||||
action: () => {
|
||||
// Find position/rotation/scale curves for selected object
|
||||
// Change interpolation between keyframes to "linear" or "bezier"
|
||||
}
|
||||
}
|
||||
```
|
||||
- **Note**: exists and zOrder keyframes already correctly use "hold" (main.js:2139, 2150)
|
||||
- **Impact**: High - causes unwanted interpolation, shapes morph unexpectedly, objects move when they shouldn't
|
||||
- **Priority**: High - fundamental animation behavior is incorrect
|
||||
|
||||
### Tauri Pinch-Zoom on Linux
|
||||
- **Issue**: Two-finger pinch gestures zoom the entire Tauri window instead of individual canvases
|
||||
- **Status**: Known Tauri limitation on Linux/GTK with no cross-platform solution
|
||||
- **Tracking**: https://github.com/tauri-apps/tauri/discussions/3843
|
||||
- **Workaround attempts**: Tried `zoomHotkeysEnabled: false`, `touch-action: none`, viewport meta tags - none worked
|
||||
- **Resolution**: Monitor Tauri releases for official fix
|
||||
|
||||
## Notes
|
||||
|
||||
### Architecture
|
||||
- **GraphicsObject** contains Layers and has `currentTime` (continuous time)
|
||||
- **Layer** contains `shapes[]` array and `animationData` (AnimationData instance)
|
||||
- **AnimationData** contains curves dictionary, each curve identified by parameter name
|
||||
- Shape curves: `shape.{uuid}.exists`, `shape.{uuid}.zOrder`
|
||||
- Future: `shape.{uuid}.x`, `shape.{uuid}.y`, `shape.{uuid}.rotation`, etc.
|
||||
- **Shapes render based on curves**: Layer.draw checks exists > 0, sorts by zOrder, draws in order
|
||||
|
||||
### Interpolation Types
|
||||
- `linear` - Linear interpolation between keyframes
|
||||
- `bezier` - Cubic Bezier with easing control points
|
||||
- `step`/`hold` - Step function (jumps to next value)
|
||||
Generated
-10
@@ -586,7 +586,6 @@ dependencies = [
|
||||
"dasp_rms",
|
||||
"dasp_sample",
|
||||
"dasp_signal",
|
||||
"ffmpeg-blob-io",
|
||||
"ffmpeg-next",
|
||||
"hound",
|
||||
"memmap2",
|
||||
@@ -662,15 +661,6 @@ version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af9673d8203fcb076b19dfd17e38b3d4ae9f44959416ea532ce72415a6020365"
|
||||
|
||||
[[package]]
|
||||
name = "ffmpeg-blob-io"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ffmpeg-next",
|
||||
"ffmpeg-sys-next",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ffmpeg-next"
|
||||
version = "8.0.0"
|
||||
|
||||
+92
-579
File diff suppressed because it is too large
Load Diff
+35
-247
@@ -50,10 +50,6 @@ pub struct ExportSettings {
|
||||
pub end_time: Seconds,
|
||||
/// Tempo map for beat-position scheduling
|
||||
pub tempo_map: TempoMap,
|
||||
/// Tag metadata as (ffmpeg-key, value) pairs (e.g. ("title", "…"), ("artist", "…")). Written to
|
||||
/// the container's native tags: ID3v2 (MP3), MP4 atoms (M4A), Vorbis comments (FLAC), RIFF INFO
|
||||
/// (WAV). Empty = no tags.
|
||||
pub metadata: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl Default for ExportSettings {
|
||||
@@ -67,26 +63,10 @@ impl Default for ExportSettings {
|
||||
start_time: Seconds::ZERO,
|
||||
end_time: Seconds(60.0),
|
||||
tempo_map: TempoMap::constant(120.0),
|
||||
metadata: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set tag metadata on an ffmpeg output context (before `write_header`). FFmpeg maps the standard
|
||||
/// keys to each container's native tags.
|
||||
fn apply_metadata(output: &mut ffmpeg_next::format::context::Output, metadata: &[(String, String)]) {
|
||||
if metadata.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut dict = ffmpeg_next::Dictionary::new();
|
||||
for (k, v) in metadata {
|
||||
if !v.is_empty() {
|
||||
dict.set(k, v);
|
||||
}
|
||||
}
|
||||
output.set_metadata(dict);
|
||||
}
|
||||
|
||||
/// Export the project to an audio file
|
||||
///
|
||||
/// This performs offline rendering, processing the entire timeline
|
||||
@@ -128,21 +108,17 @@ pub fn export_audio<P: AsRef<Path>>(
|
||||
// Route to appropriate export implementation based on format.
|
||||
// Ensure export mode is disabled even if an error occurs.
|
||||
let result = match settings.format {
|
||||
ExportFormat::Wav => {
|
||||
ExportFormat::Wav | ExportFormat::Flac => {
|
||||
let samples = render_to_memory(project, pool, settings, event_tx.as_mut().map(|tx| &mut **tx))?;
|
||||
// Signal that rendering is done and we're now writing the file
|
||||
if let Some(ref mut tx) = event_tx {
|
||||
let _ = tx.push(AudioEvent::ExportFinalizing);
|
||||
}
|
||||
write_wav(&samples, settings, &output_path)
|
||||
// hound writes no metadata; append a RIFF INFO chunk for tags.
|
||||
.and_then(|_| append_wav_info_chunk(output_path.as_ref(), &settings.metadata))
|
||||
}
|
||||
ExportFormat::Flac => {
|
||||
let samples = render_to_memory(project, pool, settings, event_tx.as_mut().map(|tx| &mut **tx))?;
|
||||
if let Some(ref mut tx) = event_tx {
|
||||
let _ = tx.push(AudioEvent::ExportFinalizing);
|
||||
match settings.format {
|
||||
ExportFormat::Wav => write_wav(&samples, settings, &output_path),
|
||||
ExportFormat::Flac => write_flac(&samples, settings, &output_path),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
export_flac(&samples, settings, &output_path)
|
||||
}
|
||||
ExportFormat::Mp3 => {
|
||||
export_mp3(project, pool, settings, output_path, event_tx)
|
||||
@@ -211,7 +187,6 @@ pub fn render_to_memory(
|
||||
settings.sample_rate,
|
||||
settings.channels,
|
||||
false,
|
||||
None, // export never runs with a recording in flight
|
||||
);
|
||||
|
||||
// Calculate how many samples we actually need from this chunk
|
||||
@@ -298,174 +273,48 @@ fn write_wav<P: AsRef<Path>>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Export real FLAC via ffmpeg from already-rendered interleaved f32 samples (Vorbis-comment
|
||||
/// metadata). Replaces the former `write_flac`, which wrote WAV bytes to a `.flac` file. 16-bit
|
||||
/// uses S16; 24-bit uses S32 (ffmpeg's flac encoder emits `bits_per_raw_sample = 24` for S32,
|
||||
/// taking the top 24 bits).
|
||||
fn export_flac<P: AsRef<Path>>(
|
||||
/// Write FLAC file using hound (FLAC is essentially lossless WAV)
|
||||
fn write_flac<P: AsRef<Path>>(
|
||||
samples: &[f32],
|
||||
settings: &ExportSettings,
|
||||
output_path: P,
|
||||
) -> Result<(), String> {
|
||||
use ffmpeg_next as ffmpeg;
|
||||
|
||||
ffmpeg::init().map_err(|e| format!("Failed to initialize FFmpeg: {}", e))?;
|
||||
|
||||
let codec = ffmpeg::encoder::find(ffmpeg::codec::Id::FLAC)
|
||||
.ok_or("FLAC encoder not found in this ffmpeg build")?;
|
||||
let mut output = ffmpeg::format::output(&output_path)
|
||||
.map_err(|e| format!("Failed to create output file: {}", e))?;
|
||||
|
||||
let channel_layout = match settings.channels {
|
||||
1 => ffmpeg::channel_layout::ChannelLayout::MONO,
|
||||
2 => ffmpeg::channel_layout::ChannelLayout::STEREO,
|
||||
_ => return Err(format!("Unsupported channel count: {}", settings.channels)),
|
||||
// For now, we'll use hound to write a WAV-like FLAC file
|
||||
// In the future, we could use a dedicated FLAC encoder
|
||||
let spec = hound::WavSpec {
|
||||
channels: settings.channels as u16,
|
||||
sample_rate: settings.sample_rate,
|
||||
bits_per_sample: settings.bit_depth,
|
||||
sample_format: hound::SampleFormat::Int,
|
||||
};
|
||||
|
||||
// FLAC accepts packed S16 or S32; S32 → 24-bit output.
|
||||
let use_24 = settings.bit_depth >= 24;
|
||||
let sample_fmt = if use_24 {
|
||||
ffmpeg::format::Sample::I32(ffmpeg::format::sample::Type::Packed)
|
||||
} else {
|
||||
ffmpeg::format::Sample::I16(ffmpeg::format::sample::Type::Packed)
|
||||
};
|
||||
let mut writer = hound::WavWriter::create(output_path, spec)
|
||||
.map_err(|e| format!("Failed to create FLAC file: {}", e))?;
|
||||
|
||||
let mut encoder = ffmpeg::codec::Context::new_with_codec(codec)
|
||||
.encoder()
|
||||
.audio()
|
||||
.map_err(|e| format!("Failed to create FLAC encoder: {}", e))?;
|
||||
encoder.set_rate(settings.sample_rate as i32);
|
||||
encoder.set_channel_layout(channel_layout);
|
||||
encoder.set_format(sample_fmt);
|
||||
encoder.set_time_base(ffmpeg::Rational(1, settings.sample_rate as i32));
|
||||
let mut encoder = encoder.open_as(codec)
|
||||
.map_err(|e| format!("Failed to open FLAC encoder: {}", e))?;
|
||||
|
||||
{
|
||||
let mut stream = output.add_stream(codec)
|
||||
.map_err(|e| format!("Failed to add stream: {}", e))?;
|
||||
stream.set_parameters(&encoder);
|
||||
}
|
||||
apply_metadata(&mut output, &settings.metadata);
|
||||
output.write_header()
|
||||
.map_err(|e| format!("Failed to write FLAC header: {}", e))?;
|
||||
|
||||
let channels = settings.channels as usize;
|
||||
let num_frames = samples.len() / channels;
|
||||
let frame_size = if encoder.frame_size() > 0 { encoder.frame_size() as usize } else { 4096 };
|
||||
|
||||
let mut done = 0usize;
|
||||
while done < num_frames {
|
||||
let n = (num_frames - done).min(frame_size);
|
||||
let mut frame = ffmpeg::frame::Audio::new(sample_fmt, n, channel_layout);
|
||||
frame.set_rate(settings.sample_rate);
|
||||
frame.set_pts(Some(done as i64)); // samples; the FLAC muxer requires PTS
|
||||
|
||||
let buf = frame.data_mut(0); // packed interleaved → plane 0
|
||||
let base = done * channels;
|
||||
if use_24 {
|
||||
for i in 0..n * channels {
|
||||
let s = samples[base + i].clamp(-1.0, 1.0);
|
||||
let v = (s as f64 * 2_147_483_647.0) as i32; // full-scale S32; encoder takes top 24
|
||||
buf[i * 4..i * 4 + 4].copy_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
} else {
|
||||
for i in 0..n * channels {
|
||||
let s = samples[base + i].clamp(-1.0, 1.0);
|
||||
let v = (s * 32767.0) as i16;
|
||||
buf[i * 2..i * 2 + 2].copy_from_slice(&v.to_le_bytes());
|
||||
// Write samples (same as WAV for now)
|
||||
match settings.bit_depth {
|
||||
16 => {
|
||||
for &sample in samples {
|
||||
let clamped = sample.max(-1.0).min(1.0);
|
||||
let pcm_value = (clamped * 32767.0) as i16;
|
||||
writer.write_sample(pcm_value)
|
||||
.map_err(|e| format!("Failed to write sample: {}", e))?;
|
||||
}
|
||||
}
|
||||
|
||||
encoder.send_frame(&frame).map_err(|e| format!("Failed to send FLAC frame: {}", e))?;
|
||||
flac_write_packets(&mut encoder, &mut output)?;
|
||||
done += n;
|
||||
}
|
||||
encoder.send_eof().map_err(|e| format!("Failed to flush FLAC encoder: {}", e))?;
|
||||
flac_write_packets(&mut encoder, &mut output)?;
|
||||
output.write_trailer().map_err(|e| format!("Failed to finalize FLAC: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Drain encoded FLAC packets and write them (non-interleaved). Skips the trailing empty flush
|
||||
/// packet, which the FLAC muxer otherwise rejects as "Invalid data". Rescales packet ts from the
|
||||
/// encoder time base to the stream's.
|
||||
fn flac_write_packets(
|
||||
encoder: &mut ffmpeg_next::encoder::Audio,
|
||||
output: &mut ffmpeg_next::format::context::Output,
|
||||
) -> Result<(), String> {
|
||||
let mut pkt = ffmpeg_next::Packet::empty();
|
||||
let enc_tb = encoder.time_base();
|
||||
let stream_tb = output.stream(0).map(|s| s.time_base()).unwrap_or(enc_tb);
|
||||
while encoder.receive_packet(&mut pkt).is_ok() {
|
||||
if pkt.size() == 0 {
|
||||
continue;
|
||||
24 => {
|
||||
for &sample in samples {
|
||||
let clamped = sample.max(-1.0).min(1.0);
|
||||
let pcm_value = (clamped * 8388607.0) as i32;
|
||||
writer.write_sample(pcm_value)
|
||||
.map_err(|e| format!("Failed to write sample: {}", e))?;
|
||||
}
|
||||
}
|
||||
pkt.set_stream(0);
|
||||
pkt.rescale_ts(enc_tb, stream_tb);
|
||||
pkt.write(output).map_err(|e| format!("Failed to write FLAC packet: {}", e))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Append a RIFF `LIST`/`INFO` metadata chunk to a finished WAV file (hound writes no tags), then
|
||||
/// fix up the top-level RIFF size. Maps ffmpeg-style keys to RIFF INFO sub-chunk IDs. Trailing INFO
|
||||
/// chunks are ignored by players that don't read them.
|
||||
fn append_wav_info_chunk(path: &Path, metadata: &[(String, String)]) -> Result<(), String> {
|
||||
use std::io::{Seek, SeekFrom, Write};
|
||||
|
||||
let riff_id = |key: &str| -> Option<&'static [u8; 4]> {
|
||||
match key {
|
||||
"title" => Some(b"INAM"),
|
||||
"artist" => Some(b"IART"),
|
||||
"album" => Some(b"IPRD"),
|
||||
"genre" => Some(b"IGNR"),
|
||||
"comment" => Some(b"ICMT"),
|
||||
"date" => Some(b"ICRD"),
|
||||
"track" => Some(b"ITRK"),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
|
||||
let mut info: Vec<u8> = Vec::new();
|
||||
info.extend_from_slice(b"INFO");
|
||||
for (key, val) in metadata {
|
||||
if val.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some(id) = riff_id(key) else { continue };
|
||||
let mut bytes = val.as_bytes().to_vec();
|
||||
bytes.push(0); // NUL-terminate
|
||||
if bytes.len() % 2 == 1 {
|
||||
bytes.push(0); // pad to even
|
||||
}
|
||||
info.extend_from_slice(id);
|
||||
info.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
|
||||
info.extend_from_slice(&bytes);
|
||||
}
|
||||
if info.len() <= 4 {
|
||||
return Ok(()); // nothing but the "INFO" tag
|
||||
_ => return Err(format!("Unsupported bit depth: {}", settings.bit_depth)),
|
||||
}
|
||||
|
||||
let mut list: Vec<u8> = Vec::with_capacity(info.len() + 8);
|
||||
list.extend_from_slice(b"LIST");
|
||||
list.extend_from_slice(&(info.len() as u32).to_le_bytes());
|
||||
list.extend_from_slice(&info);
|
||||
writer.finalize()
|
||||
.map_err(|e| format!("Failed to finalize FLAC file: {}", e))?;
|
||||
|
||||
let mut f = std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(path)
|
||||
.map_err(|e| format!("Failed to open WAV for tagging: {}", e))?;
|
||||
let end = f.seek(SeekFrom::End(0)).map_err(|e| e.to_string())?;
|
||||
if end % 2 == 1 {
|
||||
f.write_all(&[0]).map_err(|e| e.to_string())?;
|
||||
}
|
||||
f.write_all(&list).map_err(|e| format!("Failed to write WAV tags: {}", e))?;
|
||||
let new_len = f.seek(SeekFrom::End(0)).map_err(|e| e.to_string())?;
|
||||
f.seek(SeekFrom::Start(4)).map_err(|e| e.to_string())?;
|
||||
f.write_all(&((new_len - 8) as u32).to_le_bytes())
|
||||
.map_err(|e| format!("Failed to update RIFF size: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -513,7 +362,6 @@ fn export_mp3<P: AsRef<Path>>(
|
||||
stream.set_parameters(&encoder);
|
||||
}
|
||||
|
||||
apply_metadata(&mut output, &settings.metadata);
|
||||
output.write_header()
|
||||
.map_err(|e| format!("Failed to write header: {}", e))?;
|
||||
|
||||
@@ -558,7 +406,6 @@ fn export_mp3<P: AsRef<Path>>(
|
||||
settings.sample_rate,
|
||||
settings.channels,
|
||||
false,
|
||||
None, // export never runs with a recording in flight
|
||||
);
|
||||
|
||||
// Calculate how many samples we need from this chunk
|
||||
@@ -684,7 +531,6 @@ fn export_aac<P: AsRef<Path>>(
|
||||
stream.set_parameters(&encoder);
|
||||
}
|
||||
|
||||
apply_metadata(&mut output, &settings.metadata);
|
||||
output.write_header()
|
||||
.map_err(|e| format!("Failed to write header: {}", e))?;
|
||||
|
||||
@@ -729,7 +575,6 @@ fn export_aac<P: AsRef<Path>>(
|
||||
settings.sample_rate,
|
||||
settings.channels,
|
||||
false,
|
||||
None, // export never runs with a recording in flight
|
||||
);
|
||||
|
||||
// Calculate how many samples we need from this chunk
|
||||
@@ -993,61 +838,4 @@ mod tests {
|
||||
assert_eq!(ExportFormat::Wav.extension(), "wav");
|
||||
assert_eq!(ExportFormat::Flac.extension(), "flac");
|
||||
}
|
||||
|
||||
fn tagged_settings(format: ExportFormat) -> ExportSettings {
|
||||
ExportSettings {
|
||||
format,
|
||||
sample_rate: 48000,
|
||||
channels: 2,
|
||||
bit_depth: 24,
|
||||
mp3_bitrate: 192,
|
||||
start_time: Seconds::ZERO,
|
||||
end_time: Seconds(0.2), // tiny render
|
||||
tempo_map: TempoMap::constant(120.0),
|
||||
metadata: vec![
|
||||
("title".to_string(), "Test Title".to_string()),
|
||||
("artist".to_string(), "Test Artist".to_string()),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
/// FLAC export must be a real FLAC container (not WAV bytes) carrying Vorbis-comment tags.
|
||||
#[test]
|
||||
fn flac_export_is_real_flac_with_tags() {
|
||||
let settings = tagged_settings(ExportFormat::Flac);
|
||||
let mut project = Project::new(48000);
|
||||
let pool = AudioPool::new();
|
||||
let path = std::env::temp_dir().join("lb_be_flac_test.flac");
|
||||
|
||||
export_audio(&mut project, &pool, &settings, &path, None).expect("FLAC export failed");
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
|
||||
assert_eq!(&bytes[0..4], b"fLaC", "not real FLAC (got {:?})", &bytes[0..4]);
|
||||
let s = String::from_utf8_lossy(&bytes);
|
||||
assert!(s.contains("Test Title"), "title tag missing from FLAC");
|
||||
assert!(s.contains("Test Artist"), "artist tag missing from FLAC");
|
||||
std::fs::remove_file(&path).ok();
|
||||
}
|
||||
|
||||
/// WAV export keeps a valid RIFF container and gains a LIST/INFO tag chunk with a fixed-up size.
|
||||
#[test]
|
||||
fn wav_export_has_info_chunk() {
|
||||
let settings = tagged_settings(ExportFormat::Wav);
|
||||
let mut project = Project::new(48000);
|
||||
let pool = AudioPool::new();
|
||||
let path = std::env::temp_dir().join("lb_be_wav_test.wav");
|
||||
|
||||
export_audio(&mut project, &pool, &settings, &path, None).expect("WAV export failed");
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
|
||||
assert_eq!(&bytes[0..4], b"RIFF");
|
||||
assert_eq!(&bytes[8..12], b"WAVE");
|
||||
let riff_size = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) as usize;
|
||||
assert_eq!(riff_size, bytes.len() - 8, "RIFF size not fixed up after tagging");
|
||||
let s = String::from_utf8_lossy(&bytes);
|
||||
assert!(s.contains("LIST") && s.contains("INFO") && s.contains("INAM"),
|
||||
"no RIFF INFO chunk");
|
||||
assert!(s.contains("Test Title"), "title not in WAV INFO chunk");
|
||||
std::fs::remove_file(&path).ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,11 +164,11 @@ impl AudioNode for OscillatorNode {
|
||||
output[frame * 2] = sample; // Left
|
||||
output[frame * 2 + 1] = sample; // Right
|
||||
|
||||
// Update phase once per frame. `rem_euclid(1.0)` gives a numerically stable
|
||||
// wraparound into [0, 1): repeated conditional subtraction accumulates f32
|
||||
// rounding error over long-held notes (timbre drift), and FM can drive
|
||||
// `freq_mod` negative, which a one-sided `if >= 1.0` wouldn't wrap at all.
|
||||
self.phase = (self.phase + freq_mod / sample_rate_f32).rem_euclid(1.0);
|
||||
// Update phase once per frame
|
||||
self.phase += freq_mod / sample_rate_f32;
|
||||
if self.phase >= 1.0 {
|
||||
self.phase -= 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -383,7 +383,6 @@ impl Project {
|
||||
sample_rate: u32,
|
||||
channels: u32,
|
||||
live_only: bool,
|
||||
recording_midi: Option<(TrackId, MidiClipId)>,
|
||||
) {
|
||||
output.fill(0.0);
|
||||
|
||||
@@ -392,7 +391,6 @@ impl Project {
|
||||
// Create initial render context
|
||||
let ctx = RenderContext {
|
||||
live_only,
|
||||
recording_midi,
|
||||
..RenderContext::new(playhead_seconds, tempo_map, sample_rate, channels, output.len())
|
||||
};
|
||||
|
||||
|
||||
@@ -5,61 +5,6 @@ use crate::time::{Beats, Seconds};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Cycle-recording bookkeeping attached to a recording that started with a cycle region armed.
|
||||
///
|
||||
/// Takes are sliced **geometrically** at stop, in exact `loop_len_frames` multiples — not at the
|
||||
/// instant the wrap was detected. The playhead advances before the capture block in `process()`, so
|
||||
/// the wrap instant isn't sample-exact against the buffer that was just captured, but the geometry
|
||||
/// is. `wrap_count` therefore only decides *whether* this is a multi-take recording, never where the
|
||||
/// cuts land.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct CycleRecordInfo {
|
||||
/// Where the cycle region starts, in beats. Takes are laid down here, not at the punch-in point.
|
||||
pub loop_start: Beats,
|
||||
/// The cycle region's length in beats — what the take folder records as `recorded_loop_beats`.
|
||||
pub loop_len_beats: Beats,
|
||||
/// One cycle pass, in frames. The take size.
|
||||
pub loop_len_frames: usize,
|
||||
/// Frames between the region start and where capture actually began. Non-zero only for a
|
||||
/// punch-in (record while already rolling); take 1 gets this much silence prepended so it still
|
||||
/// spans the whole region.
|
||||
pub lead_pad_frames: usize,
|
||||
/// How many times the transport wrapped during this recording. Zero normally means the user
|
||||
/// stopped before completing a pass, which stays an ordinary single recording — unless
|
||||
/// `force_takes` says otherwise.
|
||||
pub wrap_count: usize,
|
||||
/// Cut takes even if the transport never wrapped.
|
||||
///
|
||||
/// Set when the region already holds takes: a further recording there is another take, however
|
||||
/// short, and it gets padded out to the region like any partial pass. Without this a run that
|
||||
/// stopped before the loop came round would land as a separate overlapping clip instead of
|
||||
/// joining the take list.
|
||||
///
|
||||
/// The editor decides this at record start, because whether takes already exist is document
|
||||
/// state the engine can't see.
|
||||
pub force_takes: bool,
|
||||
}
|
||||
|
||||
/// Min/max waveform peaks for a finished buffer of interleaved samples.
|
||||
///
|
||||
/// The live recording path builds its peaks incrementally as samples arrive; cycle takes don't
|
||||
/// exist until the recording is sliced at stop, so they get theirs in one pass here.
|
||||
pub fn compute_peaks(samples: &[f32], channels: u32, frames_per_peak: usize) -> Vec<WaveformPeak> {
|
||||
let samples_per_peak = (frames_per_peak * channels.max(1) as usize).max(1);
|
||||
samples
|
||||
.chunks(samples_per_peak)
|
||||
.map(|chunk| {
|
||||
let mut min = 0.0f32;
|
||||
let mut max = 0.0f32;
|
||||
for s in chunk {
|
||||
min = min.min(*s);
|
||||
max = max.max(*s);
|
||||
}
|
||||
WaveformPeak { min, max }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// State of an active recording session
|
||||
pub struct RecordingState {
|
||||
/// Track being recorded to
|
||||
@@ -90,8 +35,6 @@ pub struct RecordingState {
|
||||
pub frames_per_peak: usize,
|
||||
/// All recorded audio data accumulated in memory (written to disk at finalization)
|
||||
pub audio_data: Vec<f32>,
|
||||
/// Cycle-recording bookkeeping, when a cycle region was armed at record start.
|
||||
pub cycle: Option<CycleRecordInfo>,
|
||||
}
|
||||
|
||||
impl RecordingState {
|
||||
@@ -126,69 +69,9 @@ impl RecordingState {
|
||||
waveform_buffer: Vec::new(),
|
||||
frames_per_peak,
|
||||
audio_data: Vec::new(),
|
||||
cycle: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Slice the recording into cycle takes: one per pass, each spanning the FULL cycle region.
|
||||
///
|
||||
/// Partial passes are padded with silence — the head of take 1 for a punch-in, the tail of the
|
||||
/// last take when the user stops mid-pass — so every take is the same length and aligned to the
|
||||
/// region. That uniformity is what makes comping-via-split work: take 1 on the left half and
|
||||
/// take 3 on the right always line up.
|
||||
///
|
||||
/// Returns `None` if this wasn't a cycle recording or the transport never wrapped (an ordinary
|
||||
/// single recording, which keeps the existing path untouched).
|
||||
pub fn slice_takes(&self) -> Option<Vec<Vec<f32>>> {
|
||||
let cycle = self.cycle?;
|
||||
if (cycle.wrap_count == 0 && !cycle.force_takes) || cycle.loop_len_frames == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let ch = self.channels.max(1) as usize;
|
||||
let take_len = cycle.loop_len_frames * ch;
|
||||
let lead = cycle.lead_pad_frames * ch;
|
||||
|
||||
// The recording as positioned *within the region*: silence for the gap between the region
|
||||
// start and the punch-in, then the captured audio. Slicing this at whole-take boundaries is
|
||||
// the whole trick — take 1 comes out short-by-`lead` at the front, already padded.
|
||||
let virtual_len = lead + self.audio_data.len();
|
||||
let take_count = virtual_len.div_ceil(take_len);
|
||||
|
||||
let mut takes: Vec<Vec<f32>> = Vec::with_capacity(take_count);
|
||||
for i in 0..take_count {
|
||||
let mut take = vec![0.0f32; take_len];
|
||||
let take_begin = i * take_len;
|
||||
for slot in 0..take_len {
|
||||
// Position in the virtual (lead-padded) buffer.
|
||||
let v = take_begin + slot;
|
||||
if v < lead {
|
||||
continue; // still in the punch-in silence
|
||||
}
|
||||
match self.audio_data.get(v - lead) {
|
||||
Some(s) => take[slot] = *s,
|
||||
None => break, // past the end of capture; the rest stays silent
|
||||
}
|
||||
}
|
||||
takes.push(take);
|
||||
}
|
||||
|
||||
// A final take holding only a sliver of real audio is a stop artifact (the user hit stop a
|
||||
// moment after the wrap), not a performance. Drop it — but only if it's actually a PARTIAL
|
||||
// pass, and never the only take. A pass that filled the region is a real take no matter how
|
||||
// short the region is.
|
||||
const MIN_TAKE_SECONDS: f64 = 0.05;
|
||||
if takes.len() > 1 {
|
||||
let last_real_samples = virtual_len - (takes.len() - 1) * take_len;
|
||||
let last_real_seconds = (last_real_samples / ch) as f64 / self.sample_rate as f64;
|
||||
if last_real_samples < take_len && last_real_seconds < MIN_TAKE_SECONDS {
|
||||
takes.pop();
|
||||
}
|
||||
}
|
||||
|
||||
Some(takes)
|
||||
}
|
||||
|
||||
/// Add samples to the accumulation buffer
|
||||
/// Returns true if a flush occurred
|
||||
pub fn add_samples(&mut self, samples: &[f32]) -> Result<bool, std::io::Error> {
|
||||
@@ -306,26 +189,6 @@ pub struct MidiRecordingState {
|
||||
active_notes: HashMap<u8, ActiveMidiNote>,
|
||||
/// Completed notes: (time_offset, note, velocity, duration) — all times in beats
|
||||
pub completed_notes: Vec<(Beats, u8, u8, Beats)>,
|
||||
/// The cycle region's length in beats, if one was armed at record start.
|
||||
///
|
||||
/// A cycle MIDI recording is anchored at the region start (`start_time == loop_start`), which is
|
||||
/// what makes MERGE fall out for free: the transport always wraps back into the region, so every
|
||||
/// note's offset already lands inside `[0, loop_len)` and successive passes overdub onto each
|
||||
/// other with no folding needed.
|
||||
pub cycle_loop_len: Option<Beats>,
|
||||
/// Whether the transport actually came round. Distinct from `cycle_loop_len`, which only says a
|
||||
/// region was armed: the clip only pins to the full region once a pass has completed, so until
|
||||
/// then the bar still grows with the playhead.
|
||||
pub wrapped: bool,
|
||||
/// Cut takes even if the transport never wrapped — see [`CycleRecordInfo::force_takes`].
|
||||
pub force_takes: bool,
|
||||
/// Which cycle pass is currently being recorded (0-based). Bumped at each wrap.
|
||||
current_pass: usize,
|
||||
/// The pass each completed note belongs to, parallel to `completed_notes`.
|
||||
///
|
||||
/// Only meaningful in "separate takes" mode, where each pass becomes its own MIDI clip. Merge
|
||||
/// mode ignores it — all passes fold into one clip, which is the whole point.
|
||||
note_pass: Vec<usize>,
|
||||
}
|
||||
|
||||
impl MidiRecordingState {
|
||||
@@ -336,28 +199,9 @@ impl MidiRecordingState {
|
||||
start_time,
|
||||
active_notes: HashMap::new(),
|
||||
completed_notes: Vec::new(),
|
||||
cycle_loop_len: None,
|
||||
wrapped: false,
|
||||
force_takes: false,
|
||||
current_pass: 0,
|
||||
note_pass: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Record a finished note, tagging it with the pass it was played in.
|
||||
///
|
||||
/// Every completion goes through here so `completed_notes` and `note_pass` can't drift apart.
|
||||
fn push_completed(&mut self, note: &ActiveMidiNote, end_time: Beats) {
|
||||
let note_start = note.start_time.max(self.start_time);
|
||||
self.completed_notes.push((
|
||||
note_start - self.start_time,
|
||||
note.note,
|
||||
note.velocity,
|
||||
end_time - note_start,
|
||||
));
|
||||
self.note_pass.push(self.current_pass);
|
||||
}
|
||||
|
||||
pub fn note_on(&mut self, note: u8, velocity: u8, absolute_time: Beats) {
|
||||
self.active_notes.insert(note, ActiveMidiNote { note, velocity, start_time: absolute_time });
|
||||
}
|
||||
@@ -367,38 +211,16 @@ impl MidiRecordingState {
|
||||
if absolute_time <= self.start_time {
|
||||
return;
|
||||
}
|
||||
self.push_completed(&active_note, absolute_time);
|
||||
let note_start = active_note.start_time.max(self.start_time);
|
||||
self.completed_notes.push((
|
||||
note_start - self.start_time,
|
||||
active_note.note,
|
||||
active_note.velocity,
|
||||
absolute_time - note_start,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// Completed notes grouped by cycle pass — one bucket per pass, in recording order.
|
||||
///
|
||||
/// Used by "separate takes" mode, where each pass becomes its own MIDI clip.
|
||||
///
|
||||
/// An *interior* pass in which nothing was played still yields an empty take, so take N in the
|
||||
/// folder is always pass N on the transport — otherwise the numbering would silently shift and
|
||||
/// "take 3" would stop meaning "the third time round". A *trailing* empty pass is dropped
|
||||
/// though: that's what you get by hitting stop shortly after a wrap, and it's a stop artifact
|
||||
/// rather than a take you played. (Same reasoning as the audio path's short-final-take rule.)
|
||||
pub fn notes_by_pass(&self, passes: usize) -> Vec<Vec<(Beats, u8, u8, Beats)>> {
|
||||
let mut buckets = vec![Vec::new(); passes.max(1)];
|
||||
for (note, &pass) in self.completed_notes.iter().zip(self.note_pass.iter()) {
|
||||
if let Some(bucket) = buckets.get_mut(pass) {
|
||||
bucket.push(*note);
|
||||
}
|
||||
}
|
||||
// Never drop the only take.
|
||||
while buckets.len() > 1 && buckets.last().is_some_and(|b| b.is_empty()) {
|
||||
buckets.pop();
|
||||
}
|
||||
buckets
|
||||
}
|
||||
|
||||
/// How many cycle passes this recording covered (1 if the transport never wrapped).
|
||||
pub fn pass_count(&self) -> usize {
|
||||
self.current_pass + 1
|
||||
}
|
||||
|
||||
pub fn get_notes(&self) -> &[(Beats, u8, u8, Beats)] {
|
||||
&self.completed_notes
|
||||
}
|
||||
@@ -407,28 +229,18 @@ impl MidiRecordingState {
|
||||
self.completed_notes.len()
|
||||
}
|
||||
|
||||
/// The still-held notes, given a provisional duration running to `current_time`.
|
||||
///
|
||||
/// These belong to whatever pass is in progress, so a per-pass view can append them as-is.
|
||||
pub fn active_notes_with_provisional_end(&self, current_time: Beats) -> Vec<(Beats, u8, u8, Beats)> {
|
||||
self.active_notes
|
||||
.values()
|
||||
.map(|active| {
|
||||
let note_start = active.start_time.max(self.start_time);
|
||||
(
|
||||
note_start - self.start_time,
|
||||
active.note,
|
||||
active.velocity,
|
||||
(current_time - note_start).max(Beats::ZERO),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get all completed notes plus currently-held notes with a provisional duration.
|
||||
pub fn get_notes_with_active(&self, current_time: Beats) -> Vec<(Beats, u8, u8, Beats)> {
|
||||
let mut notes = self.completed_notes.clone();
|
||||
notes.extend(self.active_notes_with_provisional_end(current_time));
|
||||
for active in self.active_notes.values() {
|
||||
let note_start = active.start_time.max(self.start_time);
|
||||
notes.push((
|
||||
note_start - self.start_time,
|
||||
active.note,
|
||||
active.velocity,
|
||||
(current_time - note_start).max(Beats::ZERO),
|
||||
));
|
||||
}
|
||||
notes
|
||||
}
|
||||
|
||||
@@ -440,259 +252,13 @@ impl MidiRecordingState {
|
||||
let active_notes: Vec<_> = self.active_notes.drain().collect();
|
||||
|
||||
for (_note_num, active_note) in active_notes {
|
||||
self.push_completed(&active_note, end_time);
|
||||
let note_start = active_note.start_time.max(self.start_time);
|
||||
self.completed_notes.push((
|
||||
note_start - self.start_time,
|
||||
active_note.note,
|
||||
active_note.velocity,
|
||||
end_time - note_start,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a transport cycle wrap during MIDI recording.
|
||||
///
|
||||
/// Note times are stored as offsets from `start_time`, and the playhead jumps *backwards* at a
|
||||
/// wrap — so a note still held across the boundary would otherwise get a nonsensical (negative)
|
||||
/// duration, or never be closed at all. Write its note-off at `region_end` (exactly as
|
||||
/// `close_active_notes` does when recording stops), then re-open it at `region_start` so a key
|
||||
/// the player is still physically holding keeps being captured in the next pass. Mirrors the
|
||||
/// way `handle_start_midi_recording` re-injects already-held notes at the recording start.
|
||||
pub fn wrap_at_cycle(&mut self, region_end: Beats, region_start: Beats) {
|
||||
// Snapshot the held notes (close_active_notes drains them and loses the velocities).
|
||||
let held: Vec<(u8, u8)> = self
|
||||
.active_notes
|
||||
.values()
|
||||
.map(|n| (n.note, n.velocity))
|
||||
.collect();
|
||||
|
||||
// Close first, so a note held across the boundary has its tail attributed to the pass that's
|
||||
// ending; then advance, so the re-opened half belongs to the pass that's beginning.
|
||||
self.close_active_notes(region_end);
|
||||
self.current_pass += 1;
|
||||
|
||||
for (note, velocity) in held {
|
||||
self.note_on(note, velocity, region_start);
|
||||
}
|
||||
|
||||
// A pass has completed, so from here the clip spans the whole region rather than however long
|
||||
// the user happens to hold the record button.
|
||||
self.cycle_loop_len = Some(region_end - region_start);
|
||||
self.wrapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod cycle_tests {
|
||||
use super::*;
|
||||
|
||||
/// A recording state holding `audio_data`, armed for cycle recording. Mono, 100 Hz, so a frame
|
||||
/// is a sample and 5 frames is 50 ms (exactly the min-take threshold).
|
||||
fn rec_forced(audio: Vec<f32>, loop_len_frames: usize, lead_pad_frames: usize, wraps: usize) -> RecordingState {
|
||||
let mut r = rec(audio, loop_len_frames, lead_pad_frames, wraps);
|
||||
if let Some(c) = r.cycle.as_mut() {
|
||||
c.force_takes = true;
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
fn rec(audio: Vec<f32>, loop_len_frames: usize, lead_pad_frames: usize, wraps: usize) -> RecordingState {
|
||||
let mut r = RecordingState::new(
|
||||
0,
|
||||
0,
|
||||
PathBuf::from("/dev/null"),
|
||||
WavWriter::create(&PathBuf::from("/dev/null"), 100, 1).expect("wav writer"),
|
||||
100,
|
||||
1,
|
||||
Beats(0.0),
|
||||
1.0,
|
||||
);
|
||||
r.audio_data = audio;
|
||||
r.cycle = Some(CycleRecordInfo {
|
||||
loop_start: Beats(0.0),
|
||||
loop_len_beats: Beats(4.0),
|
||||
loop_len_frames,
|
||||
lead_pad_frames,
|
||||
wrap_count: wraps,
|
||||
force_takes: false,
|
||||
});
|
||||
r
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_wrap_is_not_a_cycle_recording() {
|
||||
// Stopping before the transport ever wraps stays an ordinary single recording — the whole
|
||||
// point of triggering on the wrap rather than on the cycle region merely existing.
|
||||
let r = rec(vec![1.0; 10], 4, 0, 0);
|
||||
assert!(r.slice_takes().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn force_takes_makes_a_partial_pass_a_take() {
|
||||
// Recording over a region that already holds takes: this run is another take however short
|
||||
// it ran, so it's cut and padded like any partial pass rather than landing as a separate
|
||||
// overlapping clip. Two real frames of an 8-frame region -> one take, silence for the rest.
|
||||
let takes = rec_forced(vec![1.0, 2.0], 8, 0, 0)
|
||||
.slice_takes()
|
||||
.expect("forced takes");
|
||||
assert_eq!(takes.len(), 1);
|
||||
assert_eq!(takes[0], vec![1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn takes_are_cut_at_exact_loop_multiples() {
|
||||
// 12 frames of audio, 4-frame loop, started at the region start => 3 clean takes.
|
||||
let audio: Vec<f32> = (1..=12).map(|i| i as f32).collect();
|
||||
let takes = rec(audio, 4, 0, 2).slice_takes().expect("cycle takes");
|
||||
assert_eq!(takes.len(), 3);
|
||||
assert_eq!(takes[0], vec![1.0, 2.0, 3.0, 4.0]);
|
||||
assert_eq!(takes[1], vec![5.0, 6.0, 7.0, 8.0]);
|
||||
assert_eq!(takes[2], vec![9.0, 10.0, 11.0, 12.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn punch_in_pads_the_head_of_take_one() {
|
||||
// Punched in 2 frames into the region: take 1 gets 2 frames of silence at the FRONT so it
|
||||
// still spans the whole region and lines up with every other take.
|
||||
let audio: Vec<f32> = (1..=10).map(|i| i as f32).collect();
|
||||
let takes = rec(audio, 4, 2, 2).slice_takes().expect("cycle takes");
|
||||
assert_eq!(takes.len(), 3);
|
||||
assert_eq!(takes[0], vec![0.0, 0.0, 1.0, 2.0]);
|
||||
assert_eq!(takes[1], vec![3.0, 4.0, 5.0, 6.0]);
|
||||
assert_eq!(takes[2], vec![7.0, 8.0, 9.0, 10.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stopping_mid_pass_pads_the_tail_of_the_last_take() {
|
||||
// 13 frames, 8-frame loop => the second take holds 5 real frames (50 ms at 100 Hz, right at
|
||||
// the keep threshold) and 3 of silence.
|
||||
let audio: Vec<f32> = (1..=13).map(|i| i as f32).collect();
|
||||
let takes = rec(audio, 8, 0, 1).slice_takes().expect("cycle takes");
|
||||
assert_eq!(takes.len(), 2);
|
||||
assert_eq!(takes[1], vec![9.0, 10.0, 11.0, 12.0, 13.0, 0.0, 0.0, 0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_take_is_the_same_length() {
|
||||
// Uniform length is the invariant comping-via-split depends on.
|
||||
let audio: Vec<f32> = (1..=23).map(|i| i as f32).collect();
|
||||
let takes = rec(audio, 8, 3, 3).slice_takes().expect("cycle takes");
|
||||
assert!(takes.iter().all(|t| t.len() == 8), "takes must be uniform");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_sliver_of_a_final_take_is_dropped() {
|
||||
// Stopped 1 frame (10 ms at 100 Hz) after the wrap — below the 50 ms floor, so that stub of
|
||||
// a take is a stop artifact and goes.
|
||||
let audio: Vec<f32> = (1..=9).map(|i| i as f32).collect();
|
||||
let takes = rec(audio, 8, 0, 1).slice_takes().expect("cycle takes");
|
||||
assert_eq!(takes.len(), 1, "a 10ms tail take should be dropped");
|
||||
assert_eq!(takes[0].len(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_full_final_take_is_never_dropped() {
|
||||
// Regression: the sliver rule must only fire on a PARTIAL pass. A pass that filled the
|
||||
// region is a real take however short the region is — an earlier version compared a full
|
||||
// take's duration to the floor and silently ate it.
|
||||
let audio: Vec<f32> = (1..=8).map(|i| i as f32).collect();
|
||||
let takes = rec(audio, 4, 0, 1).slice_takes().expect("cycle takes");
|
||||
assert_eq!(takes.len(), 2, "both passes filled the region");
|
||||
assert_eq!(takes[1], vec![5.0, 6.0, 7.0, 8.0]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod midi_cycle_tests {
|
||||
use super::*;
|
||||
|
||||
/// A MIDI recording anchored at the region start (beat 0), region 4 beats long.
|
||||
fn rec() -> MidiRecordingState {
|
||||
MidiRecordingState::new(0, 0, Beats(0.0))
|
||||
}
|
||||
|
||||
/// One pass of the transport around a 4-beat region.
|
||||
fn wrap(r: &mut MidiRecordingState) {
|
||||
r.wrap_at_cycle(Beats(4.0), Beats(0.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notes_are_bucketed_by_the_pass_they_were_played_in() {
|
||||
let mut r = rec();
|
||||
r.note_on(60, 100, Beats(1.0));
|
||||
r.note_off(60, Beats(2.0)); // pass 0
|
||||
wrap(&mut r);
|
||||
r.note_on(62, 100, Beats(1.0));
|
||||
r.note_off(62, Beats(2.0)); // pass 1
|
||||
wrap(&mut r);
|
||||
r.note_on(64, 100, Beats(1.0));
|
||||
r.note_off(64, Beats(2.0)); // pass 2
|
||||
|
||||
assert_eq!(r.pass_count(), 3);
|
||||
let by_pass = r.notes_by_pass(r.pass_count());
|
||||
let pitches: Vec<Vec<u8>> = by_pass
|
||||
.iter()
|
||||
.map(|p| p.iter().map(|n| n.1).collect())
|
||||
.collect();
|
||||
assert_eq!(pitches, vec![vec![60], vec![62], vec![64]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_note_held_across_a_wrap_splits_between_the_two_passes() {
|
||||
// The key is still down at the boundary: the sounding half belongs to the pass that's
|
||||
// ending, and the re-opened half to the pass that's beginning. Getting the pass bump on the
|
||||
// wrong side of close_active_notes would file the whole note under one pass.
|
||||
let mut r = rec();
|
||||
r.note_on(60, 100, Beats(3.0));
|
||||
wrap(&mut r); // still held
|
||||
r.note_off(60, Beats(1.0)); // released 1 beat into the next pass
|
||||
|
||||
assert_eq!(r.pass_count(), 2);
|
||||
let by_pass = r.notes_by_pass(r.pass_count());
|
||||
assert_eq!(by_pass[0].len(), 1, "the held half lands in the pass that ended");
|
||||
assert_eq!(by_pass[1].len(), 1, "the re-opened half lands in the next pass");
|
||||
// Pass 0's half runs from beat 3 to the region end at 4.
|
||||
assert_eq!(by_pass[0][0].0, Beats(3.0));
|
||||
assert_eq!(by_pass[0][0].3, Beats(1.0));
|
||||
// Pass 1's half starts at the region start and runs to the release.
|
||||
assert_eq!(by_pass[1][0].0, Beats(0.0));
|
||||
assert_eq!(by_pass[1][0].3, Beats(1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_silent_interior_pass_still_yields_an_empty_take() {
|
||||
// Take N in the folder must be pass N on the transport, even if nothing was played — else
|
||||
// the take numbering silently shifts under the user.
|
||||
let mut r = rec();
|
||||
r.note_on(60, 100, Beats(1.0));
|
||||
r.note_off(60, Beats(2.0)); // pass 0
|
||||
wrap(&mut r);
|
||||
wrap(&mut r); // pass 1: played nothing
|
||||
r.note_on(64, 100, Beats(1.0));
|
||||
r.note_off(64, Beats(2.0)); // pass 2
|
||||
|
||||
let by_pass = r.notes_by_pass(r.pass_count());
|
||||
assert_eq!(by_pass.len(), 3);
|
||||
assert_eq!(by_pass[1].len(), 0, "the silent pass is still take 2");
|
||||
assert_eq!(by_pass[2][0].1, 64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_trailing_empty_pass_is_dropped() {
|
||||
// Hitting stop shortly after a wrap leaves a pass you never played into. That's a stop
|
||||
// artifact, not a take — unlike a silent pass in the middle, which was a deliberate rest.
|
||||
let mut r = rec();
|
||||
r.note_on(60, 100, Beats(1.0));
|
||||
r.note_off(60, Beats(2.0)); // pass 0
|
||||
wrap(&mut r);
|
||||
r.note_on(62, 100, Beats(1.0));
|
||||
r.note_off(62, Beats(2.0)); // pass 1
|
||||
wrap(&mut r); // pass 2 begins... and the user hits stop
|
||||
|
||||
assert_eq!(r.pass_count(), 3);
|
||||
let by_pass = r.notes_by_pass(r.pass_count());
|
||||
assert_eq!(by_pass.len(), 2, "the empty trailing pass is not a take");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_recording_still_yields_one_take() {
|
||||
let mut r = rec();
|
||||
wrap(&mut r);
|
||||
wrap(&mut r);
|
||||
assert_eq!(r.notes_by_pass(r.pass_count()).len(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::automation::{AutomationLane, AutomationLaneId, ParameterId};
|
||||
use super::clip::{AudioClipInstance, AudioClipInstanceId};
|
||||
use super::midi::{MidiClipId, MidiClipInstance, MidiClipInstanceId, MidiEvent};
|
||||
use super::midi::{MidiClipInstance, MidiClipInstanceId, MidiEvent};
|
||||
use super::midi_pool::MidiClipPool;
|
||||
use super::node_graph::AudioGraph;
|
||||
use super::node_graph::nodes::{AudioInputNode, AudioOutputNode};
|
||||
@@ -43,13 +43,6 @@ pub struct RenderContext<'a> {
|
||||
/// Used after pause/stop to route note-off tails through the normal group hierarchy
|
||||
/// without re-triggering notes from clips at the paused position.
|
||||
pub live_only: bool,
|
||||
/// The MIDI recording in progress, if any: (track being recorded to, clip being recorded into).
|
||||
///
|
||||
/// On that track, every OTHER clip is silenced for the duration of the recording. You're playing
|
||||
/// a part into this region — hearing what's already there (a previous take, say) fighting with
|
||||
/// what you're playing now is just noise. The clip being recorded into is exempt, because in
|
||||
/// merge mode that's exactly what you DO want to hear: the overdub you've been building up.
|
||||
pub recording_midi: Option<(TrackId, MidiClipId)>,
|
||||
}
|
||||
|
||||
impl<'a> RenderContext<'a> {
|
||||
@@ -68,7 +61,6 @@ impl<'a> RenderContext<'a> {
|
||||
buffer_size,
|
||||
time_stretch: 1.0,
|
||||
live_only: false,
|
||||
recording_midi: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -872,21 +864,9 @@ impl MidiTrack {
|
||||
let playhead_beats = ctx.playhead_beats();
|
||||
let buffer_end_beats = ctx.buffer_end_beats();
|
||||
|
||||
// While recording into this track, every clip EXCEPT the one being recorded into is
|
||||
// silenced. Otherwise a take folder already sitting in the cycle region would play its
|
||||
// active take underneath you on every pass, fighting the part you're trying to record.
|
||||
// The recording clip itself is exempt: in merge mode that's the overdub monitoring.
|
||||
let muted_clip = match ctx.recording_midi {
|
||||
Some((track_id, clip_id)) if track_id == self.id => Some(clip_id),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// Collect MIDI events from all clip instances that overlap with current beat range
|
||||
let mut currently_active = HashSet::new();
|
||||
for instance in &self.clip_instances {
|
||||
if muted_clip.is_some_and(|recording| instance.clip_id != recording) {
|
||||
continue;
|
||||
}
|
||||
if instance.overlaps_range(playhead_beats, buffer_end_beats) {
|
||||
currently_active.insert(instance.id);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
pub mod types;
|
||||
|
||||
pub use types::{AudioEvent, Command, MidiClipData, OscilloscopeData, Query, QueryResponse, TrimRange};
|
||||
pub use types::{AudioEvent, Command, MidiClipData, OscilloscopeData, Query, QueryResponse};
|
||||
|
||||
@@ -8,21 +8,6 @@ use crate::audio::node_graph::nodes::LoopMode;
|
||||
use crate::io::WaveformPeak;
|
||||
use crate::time::{Beats, Seconds};
|
||||
|
||||
/// A clip's internal (content) boundaries, tagged with the domain they're measured in.
|
||||
///
|
||||
/// A clip's content time is SECONDS for sampled audio but BEATS for MIDI — the same polymorphism
|
||||
/// `ClipInstance::trim_start`/`trim_end` carry. Passing these as bare `f64`s meant the caller and
|
||||
/// the engine could disagree about the unit with nothing to catch it: an audio trim of "1.0" was
|
||||
/// once stored as `Beats(1.0)` for the clip's external duration, so a 1-second split played back as
|
||||
/// half a second at 120 BPM. Tagging the domain makes that a type error instead of a bug report.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum TrimRange {
|
||||
/// Sampled-audio content time.
|
||||
Seconds { start: Seconds, end: Seconds },
|
||||
/// MIDI content time.
|
||||
Beats { start: Beats, end: Beats },
|
||||
}
|
||||
|
||||
/// Commands sent from UI/control thread to audio thread
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Command {
|
||||
@@ -34,7 +19,7 @@ pub enum Command {
|
||||
/// Pause playback (maintains position)
|
||||
Pause,
|
||||
/// Seek to a specific position in seconds
|
||||
Seek(Seconds),
|
||||
Seek(f64),
|
||||
|
||||
// Track management commands
|
||||
/// Set track volume (0.0 = silence, 1.0 = unity gain)
|
||||
@@ -46,12 +31,13 @@ pub enum Command {
|
||||
|
||||
// Clip management commands
|
||||
/// Move a clip to a new timeline position (track_id, clip_id, new_external_start)
|
||||
MoveClip(TrackId, ClipId, Beats),
|
||||
/// Trim a clip's internal boundaries — which portion of the source content is used.
|
||||
TrimClip(TrackId, ClipId, TrimRange),
|
||||
MoveClip(TrackId, ClipId, f64),
|
||||
/// Trim a clip's internal boundaries (track_id, clip_id, new_internal_start, new_internal_end)
|
||||
/// This changes which portion of the source content is used
|
||||
TrimClip(TrackId, ClipId, f64, f64),
|
||||
/// Extend/shrink a clip's external duration (track_id, clip_id, new_external_duration)
|
||||
/// If duration > internal duration, the clip will loop
|
||||
ExtendClip(TrackId, ClipId, Beats),
|
||||
ExtendClip(TrackId, ClipId, f64),
|
||||
|
||||
// Metatrack management commands
|
||||
/// Create a new metatrack with a name and optional parent group
|
||||
@@ -67,15 +53,15 @@ pub enum Command {
|
||||
SetTimeStretch(TrackId, f32),
|
||||
/// Set metatrack time offset in seconds (track_id, offset)
|
||||
/// Positive = shift content later, negative = shift earlier
|
||||
SetOffset(TrackId, Seconds),
|
||||
SetOffset(TrackId, f64),
|
||||
/// Set metatrack pitch shift in semitones (track_id, semitones) - for future use
|
||||
SetPitchShift(TrackId, f32),
|
||||
/// Set metatrack trim start in seconds (track_id, trim_start)
|
||||
/// Children won't hear content before this point
|
||||
SetTrimStart(TrackId, Seconds),
|
||||
SetTrimStart(TrackId, f64),
|
||||
/// Set metatrack trim end in seconds (track_id, trim_end)
|
||||
/// None means no end trim
|
||||
SetTrimEnd(TrackId, Option<Seconds>),
|
||||
SetTrimEnd(TrackId, Option<f64>),
|
||||
|
||||
// Audio track commands
|
||||
/// Create a new audio track with a name and optional parent group
|
||||
@@ -85,8 +71,7 @@ pub enum Command {
|
||||
AddAudioFile(String, Vec<f32>, u32, u32),
|
||||
/// Add a clip to an audio track (track_id, clip_id, pool_index, start_time, duration, offset)
|
||||
/// The clip_id is pre-assigned by the caller (via EngineController::next_audio_clip_id())
|
||||
/// (track, clip_id, pool_index, start_time [beats], duration [beats], offset [seconds])
|
||||
AddAudioClip(TrackId, AudioClipInstanceId, usize, Beats, Beats, Seconds),
|
||||
AddAudioClip(TrackId, AudioClipInstanceId, usize, f64, f64, f64),
|
||||
|
||||
// MIDI commands
|
||||
/// Create a new MIDI track with a name and optional parent group
|
||||
@@ -94,14 +79,14 @@ pub enum Command {
|
||||
/// Add a MIDI clip to the pool without placing it on a track
|
||||
AddMidiClipToPool(MidiClip),
|
||||
/// Create a new MIDI clip on a track (track_id, start_time, duration)
|
||||
CreateMidiClip(TrackId, Beats, Beats),
|
||||
CreateMidiClip(TrackId, f64, f64),
|
||||
/// Add a MIDI note to a clip (track_id, clip_id, time_offset, note, velocity, duration)
|
||||
AddMidiNote(TrackId, MidiClipId, Beats, u8, u8, Beats),
|
||||
AddMidiNote(TrackId, MidiClipId, f64, u8, u8, f64),
|
||||
/// Add a pre-loaded MIDI clip to a track (track_id, clip, start_time)
|
||||
AddLoadedMidiClip(TrackId, MidiClip, Beats),
|
||||
AddLoadedMidiClip(TrackId, MidiClip, f64),
|
||||
/// Update MIDI clip notes (track_id, clip_id, notes: Vec<(start_time, note, velocity, duration)>)
|
||||
/// NOTE: May need to switch to individual note operations if this becomes slow on clips with many notes
|
||||
UpdateMidiClipNotes(TrackId, MidiClipId, Vec<(Beats, u8, u8, Beats)>),
|
||||
UpdateMidiClipNotes(TrackId, MidiClipId, Vec<(f64, u8, u8, f64)>),
|
||||
/// Replace all events in a MIDI clip (track_id, clip_id, events). Used for CC/pitch bend editing.
|
||||
UpdateMidiClipEvents(TrackId, MidiClipId, Vec<MidiEvent>),
|
||||
/// Remove a MIDI clip instance from a track (track_id, instance_id) - for undo/redo support
|
||||
@@ -117,9 +102,9 @@ pub enum Command {
|
||||
/// Create a new automation lane on a track (track_id, parameter_id)
|
||||
CreateAutomationLane(TrackId, ParameterId),
|
||||
/// Add an automation point to a lane (track_id, lane_id, time, value, curve)
|
||||
AddAutomationPoint(TrackId, AutomationLaneId, Beats, f32, CurveType),
|
||||
AddAutomationPoint(TrackId, AutomationLaneId, f64, f32, CurveType),
|
||||
/// Remove an automation point at a specific time (track_id, lane_id, time, tolerance)
|
||||
RemoveAutomationPoint(TrackId, AutomationLaneId, Beats, Beats),
|
||||
RemoveAutomationPoint(TrackId, AutomationLaneId, f64, f64),
|
||||
/// Clear all automation points from a lane (track_id, lane_id)
|
||||
ClearAutomationLane(TrackId, AutomationLaneId),
|
||||
/// Remove an automation lane (track_id, lane_id)
|
||||
@@ -127,24 +112,9 @@ pub enum Command {
|
||||
/// Enable/disable an automation lane (track_id, lane_id, enabled)
|
||||
SetAutomationLaneEnabled(TrackId, AutomationLaneId, bool),
|
||||
|
||||
// Transport cycle (loop) region
|
||||
/// Set the cycle region the transport loops over, in beats (None clears it).
|
||||
/// Authored in beats so it survives tempo changes.
|
||||
SetLoopRegion(Option<(Beats, Beats)>),
|
||||
/// Enable/disable wrapping at the cycle region's end.
|
||||
SetLoopEnabled(bool),
|
||||
/// How a cycle MIDI recording treats its passes.
|
||||
///
|
||||
/// `false` (default) = MERGE: every pass overdubs into one clip. `true` = SEPARATE TAKES: each
|
||||
/// pass becomes its own MIDI clip, and the editor folds them into a take folder — the same shape
|
||||
/// audio always gets.
|
||||
SetCycleMidiSeparateTakes(bool),
|
||||
|
||||
// Recording commands
|
||||
/// Start recording on a track (track_id, start_time)
|
||||
/// (track, start_time, force_takes — cut takes even if the transport never wraps, because the
|
||||
/// region already holds takes and this is another one)
|
||||
StartRecording(TrackId, Beats, bool),
|
||||
StartRecording(TrackId, Beats),
|
||||
/// Stop the current recording
|
||||
StopRecording,
|
||||
/// Pause the current recording
|
||||
@@ -154,8 +124,7 @@ pub enum Command {
|
||||
|
||||
// MIDI Recording commands
|
||||
/// Start MIDI recording on a track (track_id, clip_id, start_time)
|
||||
/// (track, clip, start_time, force_takes — see [`Command::StartRecording`])
|
||||
StartMidiRecording(TrackId, MidiClipId, Beats, bool),
|
||||
StartMidiRecording(TrackId, MidiClipId, Beats),
|
||||
/// Stop the current MIDI recording
|
||||
StopMidiRecording,
|
||||
|
||||
@@ -267,9 +236,9 @@ pub enum Command {
|
||||
|
||||
// Automation Input Node commands
|
||||
/// Add or update a keyframe on an AutomationInput node (track_id, node_id, time, value, interpolation, ease_out, ease_in)
|
||||
AutomationAddKeyframe(TrackId, u32, Beats, f32, String, (f32, f32), (f32, f32)),
|
||||
AutomationAddKeyframe(TrackId, u32, f64, f32, String, (f32, f32), (f32, f32)),
|
||||
/// Remove a keyframe from an AutomationInput node (track_id, node_id, time)
|
||||
AutomationRemoveKeyframe(TrackId, u32, Beats),
|
||||
AutomationRemoveKeyframe(TrackId, u32, f64),
|
||||
/// Set the display name of an AutomationInput node (track_id, node_id, name)
|
||||
AutomationSetName(TrackId, u32, String),
|
||||
|
||||
@@ -301,7 +270,7 @@ pub enum Command {
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum AudioEvent {
|
||||
/// Current playback position in seconds
|
||||
PlaybackPosition(Seconds),
|
||||
PlaybackPosition(f64),
|
||||
/// Playback has stopped (reached end of audio)
|
||||
PlaybackStopped,
|
||||
/// Audio buffer underrun detected
|
||||
@@ -322,35 +291,6 @@ pub enum AudioEvent {
|
||||
RecordingProgress(ClipId, Seconds),
|
||||
/// Recording stopped (clip_id, pool_index, waveform)
|
||||
RecordingStopped(ClipId, usize, Vec<WaveformPeak>),
|
||||
/// A MIDI recording that wrapped the cycle region at least once, in SEPARATE TAKES mode.
|
||||
///
|
||||
/// One MIDI clip per pass, in recording order. (Merge mode emits the ordinary
|
||||
/// `MidiRecordingStopped` instead — all passes are already folded into the one clip.)
|
||||
MidiCycleRecordingStopped {
|
||||
track_id: TrackId,
|
||||
/// One pool MIDI clip per pass. The first is the clip the recording started on.
|
||||
clip_ids: Vec<MidiClipId>,
|
||||
/// Where the takes sit on the timeline — the cycle region's start.
|
||||
loop_start: Beats,
|
||||
/// The region's length in beats: every take spans exactly this.
|
||||
loop_len_beats: Beats,
|
||||
},
|
||||
/// A recording that wrapped the cycle region at least once, and so became multi-take.
|
||||
///
|
||||
/// Each take spans the full region and they're all the same length (partial passes are padded
|
||||
/// with silence), so the editor can promote the recording clip straight to a take folder.
|
||||
CycleRecordingStopped {
|
||||
clip_id: ClipId,
|
||||
/// One entry per pass: (audio pool index, waveform peaks), in recording order.
|
||||
takes: Vec<(usize, Vec<WaveformPeak>)>,
|
||||
/// Where the takes sit on the timeline — the cycle region's start, not the punch-in point.
|
||||
loop_start: Beats,
|
||||
/// The region's length in beats (what the take folder stores as `recorded_loop_beats`).
|
||||
loop_len_beats: Beats,
|
||||
/// The same length in seconds — the take folder's content duration, which is seconds-domain
|
||||
/// for audio.
|
||||
loop_len_seconds: Seconds,
|
||||
},
|
||||
/// Recording error (error_message)
|
||||
RecordingError(String),
|
||||
/// MIDI recording stopped (track_id, clip_id, note_count)
|
||||
@@ -401,7 +341,7 @@ pub enum AudioEvent {
|
||||
WaveformChunksReady {
|
||||
pool_index: usize,
|
||||
detail_level: u8,
|
||||
chunks: Vec<(u32, (Seconds, Seconds), Vec<WaveformPeak>)>,
|
||||
chunks: Vec<(u32, (f64, f64), Vec<WaveformPeak>)>,
|
||||
},
|
||||
|
||||
/// An audio file has been imported and is ready for playback.
|
||||
@@ -412,7 +352,7 @@ pub enum AudioEvent {
|
||||
path: String,
|
||||
channels: u32,
|
||||
sample_rate: u32,
|
||||
duration: Seconds,
|
||||
duration: f64,
|
||||
format: crate::io::audio_file::AudioFormat,
|
||||
},
|
||||
|
||||
@@ -486,7 +426,7 @@ pub enum Query {
|
||||
/// Export audio to file (settings, output_path)
|
||||
ExportAudio(crate::audio::ExportSettings, std::path::PathBuf),
|
||||
/// Add a MIDI clip to a track synchronously (track_id, clip, start_time) - returns instance ID
|
||||
AddMidiClipSync(TrackId, crate::audio::midi::MidiClip, Beats),
|
||||
AddMidiClipSync(TrackId, crate::audio::midi::MidiClip, f64),
|
||||
/// Add a MIDI clip instance to a track synchronously (track_id, instance) - returns instance ID
|
||||
/// The clip must already exist in the MidiClipPool
|
||||
AddMidiClipInstanceSync(TrackId, crate::audio::midi::MidiClipInstance),
|
||||
@@ -531,21 +471,16 @@ pub struct OscilloscopeData {
|
||||
}
|
||||
|
||||
/// MIDI clip data for serialization
|
||||
///
|
||||
/// `Beats`/`Seconds` are `#[serde(transparent)]`, so naming the domain here costs nothing on disk —
|
||||
/// the `.beam` still holds a plain number.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MidiClipData {
|
||||
/// MIDI content length is musical, so beats.
|
||||
pub duration: Beats,
|
||||
pub duration: f64,
|
||||
pub events: Vec<crate::audio::midi::MidiEvent>,
|
||||
}
|
||||
|
||||
/// Automation keyframe data for serialization
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct AutomationKeyframeData {
|
||||
/// Automation x-axes are all beats.
|
||||
pub time: Beats,
|
||||
pub time: f64,
|
||||
pub value: f32,
|
||||
pub interpolation: String,
|
||||
pub ease_out: (f32, f32),
|
||||
@@ -582,7 +517,7 @@ pub enum QueryResponse {
|
||||
/// Pool waveform data
|
||||
PoolWaveform(Result<Vec<crate::io::WaveformPeak>, String>),
|
||||
/// Pool file info (duration, sample_rate, channels)
|
||||
PoolFileInfo(Result<(Seconds, u32, u32), String>),
|
||||
PoolFileInfo(Result<(f64, u32, u32), String>),
|
||||
/// Audio exported
|
||||
AudioExported(Result<(), String>),
|
||||
/// MIDI clip instance added (returns instance ID)
|
||||
|
||||
@@ -113,10 +113,11 @@ impl SynthVoice {
|
||||
// Simple sine wave
|
||||
let sample = (self.phase * 2.0 * PI).sin() * (self.velocity as f32 / 127.0) * 0.3;
|
||||
|
||||
// Update phase. Use `.fract()` for a numerically stable wraparound: repeated
|
||||
// conditional subtraction accumulates f32 rounding error over long-held notes,
|
||||
// which drifts the timbre.
|
||||
self.phase = (self.phase + self.frequency / sample_rate).fract();
|
||||
// Update phase
|
||||
self.phase += self.frequency / sample_rate;
|
||||
if self.phase >= 1.0 {
|
||||
self.phase -= 1.0;
|
||||
}
|
||||
|
||||
self.age += 1;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ pub use audio::{
|
||||
TrackNode,
|
||||
};
|
||||
pub use audio::node_graph::{GraphPreset, AudioGraph, PresetMetadata, SerializedConnection, SerializedNode};
|
||||
pub use time::{Beats, ContentTime, Seconds};
|
||||
pub use time::{Beats, Seconds};
|
||||
pub use tempo_map::{TempoEntry, TempoInterpolation, TempoMap, beats_to_seconds_stack, seconds_to_beats_stack};
|
||||
pub use command::{AudioEvent, Command, OscilloscopeData};
|
||||
pub use command::types::AutomationKeyframeData;
|
||||
|
||||
@@ -16,50 +16,6 @@ pub struct Beats(pub f64);
|
||||
#[serde(transparent)]
|
||||
pub struct Seconds(pub f64);
|
||||
|
||||
/// A time *inside a clip's own content*, in whatever unit that clip measures content in.
|
||||
///
|
||||
/// Clip content time is domain-polymorphic: SECONDS for sampled audio, video and vector, but BEATS
|
||||
/// for MIDI (musical, so it survives tempo changes). `ClipInstance::trim_start`/`trim_end` are
|
||||
/// content times, and storing them as bare `f64`s is what let a seconds delta get added to a MIDI
|
||||
/// clip's beats trim — splitting a MIDI clip at beat 4 landed at beat 2 at 120 BPM.
|
||||
///
|
||||
/// This type is deliberately a **dead end**: it has no `.to_seconds()`, no `.to_beats()`, and no
|
||||
/// arithmetic with `Seconds` or `Beats`. Content times can be compared and combined with each other
|
||||
/// (that's domain-safe — both operands are in the same clip's domain), but the only way to get a
|
||||
/// real timeline duration out is to resolve it against the clip that knows the domain, via
|
||||
/// `AudioClip::resolve_content_time` / `Document::resolve_content_time`. So a passthrough costs
|
||||
/// nothing, and mixing domains won't compile.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct ContentTime(pub f64);
|
||||
|
||||
impl ContentTime {
|
||||
pub const ZERO: Self = Self(0.0);
|
||||
|
||||
pub fn max(self, other: Self) -> Self { Self(self.0.max(other.0)) }
|
||||
pub fn min(self, other: Self) -> Self { Self(self.0.min(other.0)) }
|
||||
|
||||
/// The raw magnitude, with the domain discarded.
|
||||
///
|
||||
/// Only for code that is *already* working in this clip's content domain (trim arithmetic,
|
||||
/// serialization, drawing a waveform whose x-axis is the clip's own content). If you are about
|
||||
/// to combine this with a timeline position, resolve it against the clip instead.
|
||||
pub fn raw(self) -> f64 { self.0 }
|
||||
}
|
||||
|
||||
impl Add for ContentTime {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: Self) -> Self { Self(self.0 + rhs.0) }
|
||||
}
|
||||
impl Sub for ContentTime {
|
||||
type Output = Self;
|
||||
fn sub(self, rhs: Self) -> Self { Self(self.0 - rhs.0) }
|
||||
}
|
||||
impl Rem for ContentTime {
|
||||
type Output = Self;
|
||||
fn rem(self, rhs: Self) -> Self { Self(self.0 % rhs.0) }
|
||||
}
|
||||
|
||||
impl Beats {
|
||||
pub const ZERO: Self = Self(0.0);
|
||||
|
||||
|
||||
@@ -556,7 +556,7 @@ pub fn run_tui(
|
||||
while let Ok(event) = rx.pop() {
|
||||
match event {
|
||||
AudioEvent::PlaybackPosition(pos) => {
|
||||
app.update_playback_position(pos.seconds_to_f64());
|
||||
app.update_playback_position(pos);
|
||||
}
|
||||
AudioEvent::PlaybackStopped => {
|
||||
app.set_playing(false);
|
||||
@@ -790,7 +790,7 @@ fn execute_command(
|
||||
return Err("Usage: seek <seconds>".to_string());
|
||||
}
|
||||
let pos: f64 = parts[1].parse().map_err(|_| "Invalid position")?;
|
||||
controller.seek(crate::Seconds(pos));
|
||||
controller.seek(pos);
|
||||
app.set_status(format!("Seeked to {:.2}s", pos));
|
||||
}
|
||||
"track" => {
|
||||
@@ -830,7 +830,7 @@ fn execute_command(
|
||||
app.next_clip_id += 1;
|
||||
app.add_clip(track_id, clip_id, start_time, duration, format!("Clip {}", clip_id), Vec::new());
|
||||
|
||||
controller.create_midi_clip(track_id, crate::Beats(start_time), crate::Beats(duration));
|
||||
controller.create_midi_clip(track_id, start_time, duration);
|
||||
app.set_status(format!("Created MIDI clip on track {} at {:.2}s for {:.2}s", track_id, start_time, duration));
|
||||
}
|
||||
"loadmidi" => {
|
||||
@@ -882,7 +882,7 @@ fn execute_command(
|
||||
app.next_clip_id += 1;
|
||||
|
||||
// Send to audio engine with the start_time (clip content is separate from timeline position)
|
||||
controller.add_loaded_midi_clip(track_id, midi_clip, crate::Beats(start_time));
|
||||
controller.add_loaded_midi_clip(track_id, midi_clip, start_time);
|
||||
|
||||
app.set_status(format!("Loaded {} ({} events, {:.2}s) to track {} at {:.2}s",
|
||||
file_path, event_count, duration, track_id, start_time));
|
||||
|
||||
@@ -15,8 +15,3 @@
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Local, machine-specific cargo config (e.g. PKG_CONFIG_PATH for a local ffmpeg).
|
||||
# CI/packaging build ffmpeg from source (see .github/workflows/build.yml), so this
|
||||
# must never leak into a commit.
|
||||
.cargo/config.toml
|
||||
|
||||
Generated
+144
-411
File diff suppressed because it is too large
Load Diff
@@ -24,11 +24,6 @@ vello = { git = "https://github.com/linebender/vello", branch = "main" }
|
||||
wgpu = { version = "27", features = ["vulkan", "metal", "gles"] }
|
||||
kurbo = { version = "0.12", features = ["serde"] }
|
||||
peniko = "0.5"
|
||||
# Text layout/shaping for text layers. Pinned to git main to match the peniko 0.5
|
||||
# / skrifa 0.37 that vello's git-main resolves to — a released parley still pins
|
||||
# peniko 0.4, which would split `peniko::Font` into two incompatible types at the
|
||||
# vello `draw_glyphs` boundary.
|
||||
parley = { git = "https://github.com/linebender/parley", branch = "main" }
|
||||
|
||||
# Windowing
|
||||
winit = "0.30"
|
||||
@@ -83,15 +78,6 @@ opt-level = 2
|
||||
[profile.dev.package.cpal]
|
||||
opt-level = 2
|
||||
|
||||
# GIF export: NeuQuant palette quantization is tight numeric loops that are punishingly slow
|
||||
# unoptimized (10–30× in debug). Optimize the encoder crates even in dev builds.
|
||||
[profile.dev.package.gif]
|
||||
opt-level = 3
|
||||
[profile.dev.package.color_quant]
|
||||
opt-level = 3
|
||||
[profile.dev.package.weezl]
|
||||
opt-level = 3
|
||||
|
||||
# Use local egui fork with ibus/Wayland text input fix
|
||||
[patch.crates-io]
|
||||
egui = { path = "../../egui-fork/crates/egui" }
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
# GPU-resident video decode + dynamic decode resolution
|
||||
|
||||
## Context
|
||||
|
||||
Profiling the zero-copy H.264 export (single Group[Video, Audio] clip, `LB_RENDER_PROFILE=1`)
|
||||
broke the per-frame CPU "render" bucket down as:
|
||||
|
||||
| Cost (ms/frame) | 1080p | 4K | What it is |
|
||||
|-------------------------|-------|-------|-----------------------------------------------------|
|
||||
| decode | 3.1 | 19.0 | software ffmpeg decode (`video.rs::get_frame`) |
|
||||
| background re-render | 3.6 | 7.5 | static background pushed through Vello *every frame* |
|
||||
| video upload + blit | 4.1 | 4.2 | per-frame transient texture alloc + `write_texture` |
|
||||
| srgb | 0.4 | 0.4 | linear→sRGB pass |
|
||||
|
||||
The video correctly takes the GPU Video-instance path (not Vello-baked) — `LB_LAYER_DEBUG=1`
|
||||
shows `Video (1 instance)`. So the cost is **the video frame itself**: software decode, then an
|
||||
8 MB `write_texture` upload of the decoded RGBA every frame. At 4K, software decode (19 ms)
|
||||
dominates everything.
|
||||
|
||||
### Two correctness problems found alongside the perf issue
|
||||
|
||||
1. **Decode resolution is frozen to document size at import.** `load_video(clip, src, doc_w, doc_h)`
|
||||
(`main.rs:4302`) sizes the decoder's swscale output to the document, capped to never upscale
|
||||
(`video.rs:149`). Export *reuses that decoder*, so exporting **above** document resolution yields
|
||||
video that was decoded to ≤document res and then GPU-**up**scaled — real source detail thrown away.
|
||||
2. **It can't follow the consumer or a document resize.** Preview wants small/fast frames; export
|
||||
wants full res; changing the document size should re-target the decode. None of that works with a
|
||||
size frozen at import.
|
||||
|
||||
## Goal
|
||||
|
||||
Decouple **decode resolution** from import/document size: the renderer requests a frame *at a target
|
||||
resolution*, and the decode path produces it. Hardware-decode H.264 (and later HEVC/AV1) into a GPU
|
||||
surface and keep it GPU-resident through composite into the encoder — no CPU frame copy in either
|
||||
direction. Software decode stays a **first-class** path (codecs/platforms without HW support), decoding
|
||||
at the requested target res. This fixes the 4K decode wall, the 8 MB upload, *and* the resolution bugs.
|
||||
|
||||
## Design principles
|
||||
|
||||
- **Decode native, scale to the consumer's target.**
|
||||
- *Hardware path:* decode into a native VAAPI surface → import as a wgpu texture (reuse the
|
||||
`gpu-video-encoder` `dmabuf.rs` / `vk_device.rs` plumbing, read direction) → the GPU blit that
|
||||
already composites the Video instance scales native→target for free. Handles any target res and
|
||||
document resizes inherently; the cached frame is a native GPU texture.
|
||||
- *Software path:* decode native → `swscale` to the requested target (the reusable scaler is keyed
|
||||
on input format/size **and** output size — rebuilt when the target changes). Preview requests
|
||||
preview res (cheap); export requests export res (full quality).
|
||||
- **`VideoManager::get_frame` takes a target `(w, h)`** instead of relying on a frozen output size.
|
||||
The frame cache is keyed to handle multiple live targets (preview + export) — either cache native
|
||||
frames and scale on demand, or key by `(clip, ts, target)`; decide in Stage 2 by measuring cache
|
||||
hit/scale tradeoff.
|
||||
- **Software is not optional.** Hardware decode is an acceleration of the same `get_frame` contract,
|
||||
selected per source when the codec/driver supports it; everything falls back to software cleanly.
|
||||
|
||||
## Approach (staged; each stage compiles + is independently useful)
|
||||
|
||||
### Stage 0 — independent quick wins (not blocked on decode)
|
||||
- **Cache the static background** (`composite_document_to_hdr`): render once, reuse via a persistent
|
||||
HDR texture (copy-in each frame) instead of a full Vello render + 2 passes/submits every frame.
|
||||
Recovers ~3.6 ms (1080p) / ~7.5 ms (4K) per frame on *every* export. (In flight.)
|
||||
|
||||
### Stage 1 — software: decode at the requested target res (testable; fixes the quality bug now)
|
||||
- Change `VideoManager::get_frame(clip, ts)` → `get_frame(clip, ts, target_w, target_h)`; thread the
|
||||
target from the renderer (preview = current doc/preview res, export = export res). Cap at native.
|
||||
- Rework `VideoDecoder` so output size is per-request, not frozen at construction; cache the swscale
|
||||
context per output size (already cached per stream — extend the key). Adjust the frame cache key.
|
||||
- Result: software exports are full-quality at any export res, and document resizes re-target decode.
|
||||
No hardware needed; this is the correctness fix for the codecs HW can't handle anyway.
|
||||
|
||||
### Stage 2 — hardware decode primitive (headless-testable here, like the 8 encode tests)
|
||||
- In `gpu-video-encoder` (rename → `gpu-video-codec`): `h264_vaapi`-style **decode** → VAAPI surface →
|
||||
export DMA-BUF → import as a wgpu texture. Hardware test: decode a known file, verify dims/contents.
|
||||
|
||||
### Stage 3 — wire hardware decode into `get_frame` (blind; user-verifies)
|
||||
- When the source codec/driver is HW-decodable, `get_frame` returns a **GPU texture** (native res)
|
||||
instead of `Arc<Vec<u8>>`; the compositor uses it directly (no `write_texture`), GPU-scaling to the
|
||||
target. For the zero-copy export the frame never leaves the GPU: **decode → composite → encode** on
|
||||
one device. Software path is the fallback for everything else.
|
||||
|
||||
## Critical files
|
||||
- `lightningbeam-core/src/video.rs` — `VideoDecoder` (per-request output size, scaler cache),
|
||||
`VideoManager::get_frame` (target param, cache key).
|
||||
- `lightningbeam-core/src/renderer.rs` — pass the render target res into the video-instance build.
|
||||
- `lightningbeam-editor/src/export/video_exporter.rs` — background cache (Stage 0); consume a GPU
|
||||
texture instead of uploading RGBA (Stage 3).
|
||||
- `gpu-video-encoder/` (→ `gpu-video-codec`) — `dmabuf.rs`/`vk_device.rs` reused for the decode import.
|
||||
|
||||
## Risks
|
||||
- **Codec coverage** — only some codecs are HW-decodable per GPU/driver; software must stay correct
|
||||
and well-tested. Selection must probe support per source, not assume.
|
||||
- **Cache memory** — native-res GPU textures (esp. 4K) are large; the frame cache budget needs revisiting.
|
||||
- **Colorspace/format** — VAAPI decode surfaces are NV12/tiled; the existing import handles NV12, but
|
||||
10-bit/HDR sources (P010) need format handling.
|
||||
- **Preview vs export sharing** — two live targets (preview res + export res) from the same source; the
|
||||
cache/scaler design must serve both without thrashing.
|
||||
|
||||
## Verification
|
||||
- Stage 0/1: visual — export above document res is now full-quality (not upscaled); profile shows
|
||||
background ≈ 0 and (Stage 1) software export correct at the chosen res.
|
||||
- Stage 2: headless hardware test in `gpu-video-codec` (decode → wgpu texture, ffprobe/byte checks).
|
||||
- Stage 3 (user): 1080p + 4K H.264 export — decode/upload buckets collapse; software fallback for a
|
||||
non-HW codec (e.g. ProRes) still produces correct full-res output.
|
||||
@@ -18,12 +18,6 @@ bytemuck = { version = "1.14", features = ["derive"] }
|
||||
kurbo = { workspace = true }
|
||||
vello = { workspace = true }
|
||||
|
||||
# Text layout/shaping (text layers)
|
||||
parley = { workspace = true }
|
||||
# Glyph outline extraction for lossless text→SVG export. Pinned to the version parley resolves
|
||||
# (0.43) so glyph IDs / normalized coords from parley layouts match this outliner.
|
||||
skrifa = "0.43"
|
||||
|
||||
# Image decoding for image fills
|
||||
image = { workspace = true }
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,14 +0,0 @@
|
||||
Bundled fonts for Lightningbeam text layers
|
||||
===========================================
|
||||
|
||||
These are the Liberation fonts, used as the built-in serif / sans-serif / monospaced
|
||||
families so text renders deterministically without depending on system fonts:
|
||||
|
||||
LiberationSans-Regular.ttf
|
||||
LiberationSerif-Regular.ttf
|
||||
LiberationMono-Regular.ttf
|
||||
|
||||
License: SIL Open Font License, Version 1.1.
|
||||
Copyright (c) Red Hat, Inc., with Reserved Font Name "Liberation".
|
||||
Full license text: https://openfontlicense.org/ (also distributed with the Liberation
|
||||
fonts package as its OFL.txt / LICENSE).
|
||||
@@ -47,113 +47,6 @@ pub struct BackendContext<'a> {
|
||||
// Future: pub video_controller: Option<&'a mut VideoController>,
|
||||
}
|
||||
|
||||
impl BackendContext<'_> {
|
||||
/// Hand a clip instance to the audio engine and record it in the instance→backend map.
|
||||
///
|
||||
/// Take folders are resolved through the instance's `active_take`, so the backend gets whichever
|
||||
/// take is selected. Returns the backend track and instance IDs, or `None` when there's nothing
|
||||
/// to sync yet (a recording in progress, or an empty take folder).
|
||||
///
|
||||
/// Lives here rather than in any one action because more than one action needs it: adding an
|
||||
/// instance, and switching a take folder's active take (which is a remove + re-add, there being
|
||||
/// no in-place pool-swap command). Keeping one implementation keeps the trim/duration
|
||||
/// conversions — the easy thing to get subtly wrong, since `trim_*` is SECONDS while
|
||||
/// `timeline_*` is BEATS — from drifting between copies.
|
||||
pub fn add_clip_instance(
|
||||
&mut self,
|
||||
document: &Document,
|
||||
layer_id: &Uuid,
|
||||
instance: &crate::clip::ClipInstance,
|
||||
) -> Result<Option<(daw_backend::TrackId, BackendClipInstanceId)>, String> {
|
||||
use crate::clip::ResolvedContent;
|
||||
|
||||
let clip = document
|
||||
.get_audio_clip(&instance.clip_id)
|
||||
.ok_or_else(|| format!("Audio clip {} not found", instance.clip_id))?;
|
||||
|
||||
let track_id = *self
|
||||
.layer_to_track_map
|
||||
.get(layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not mapped to backend track", layer_id))?;
|
||||
|
||||
let resolved = instance.resolve(clip);
|
||||
let content = clip.content_duration();
|
||||
let internal_start = instance.trim_start;
|
||||
let internal_end = instance
|
||||
.trim_end
|
||||
.unwrap_or(daw_backend::ContentTime(content.native()));
|
||||
let start_time = instance.timeline_start;
|
||||
|
||||
// How long the clip occupies the timeline, in BEATS. `effective_duration_beats` resolves the
|
||||
// content window in the clip's own domain — beats content carries over directly, wall-clock
|
||||
// content converts at the clip's position — so neither kind can be read as the other here.
|
||||
let effective_duration = instance.effective_duration_beats(content, document.tempo_map());
|
||||
|
||||
let controller = self
|
||||
.audio_controller
|
||||
.as_mut()
|
||||
.ok_or_else(|| "Audio controller not available".to_string())?;
|
||||
|
||||
let backend_id = match resolved {
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
// MIDI content time IS beats, so the trims carry straight over.
|
||||
let midi_instance = daw_backend::MidiClipInstance::new(
|
||||
0, // assigned by the backend
|
||||
midi_clip_id,
|
||||
daw_backend::Beats(internal_start.raw()),
|
||||
daw_backend::Beats(internal_end.raw()),
|
||||
start_time,
|
||||
effective_duration,
|
||||
);
|
||||
|
||||
match controller
|
||||
.send_query(Query::AddMidiClipInstanceSync(track_id, midi_instance))?
|
||||
{
|
||||
QueryResponse::MidiClipInstanceAdded(Ok(id)) => BackendClipInstanceId::Midi(id),
|
||||
QueryResponse::MidiClipInstanceAdded(Err(e)) => return Err(e),
|
||||
_ => return Err("Unexpected query response".to_string()),
|
||||
}
|
||||
}
|
||||
ResolvedContent::Audio { audio_pool_index } => {
|
||||
// Sampled-audio content time is SECONDS; the backend's start/duration are BEATS.
|
||||
let id = controller.add_audio_clip(
|
||||
track_id,
|
||||
audio_pool_index,
|
||||
start_time,
|
||||
effective_duration,
|
||||
daw_backend::Seconds(internal_start.raw()),
|
||||
);
|
||||
BackendClipInstanceId::Audio(id)
|
||||
}
|
||||
// Nothing to sync until it has content.
|
||||
ResolvedContent::Recording => return Ok(None),
|
||||
};
|
||||
|
||||
self.clip_instance_to_backend_map
|
||||
.insert(instance.id, backend_id);
|
||||
|
||||
Ok(Some((track_id, backend_id)))
|
||||
}
|
||||
|
||||
/// Remove a clip instance's backend clip and drop it from the instance→backend map.
|
||||
pub fn remove_clip_instance(
|
||||
&mut self,
|
||||
track_id: daw_backend::TrackId,
|
||||
backend_id: BackendClipInstanceId,
|
||||
instance_id: Uuid,
|
||||
) {
|
||||
if let Some(controller) = self.audio_controller.as_mut() {
|
||||
match backend_id {
|
||||
BackendClipInstanceId::Midi(id) => controller.remove_midi_clip(track_id, id),
|
||||
BackendClipInstanceId::Audio(id) => controller.remove_audio_clip(track_id, id),
|
||||
}
|
||||
}
|
||||
self.clip_instance_to_backend_map.remove(&instance_id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Action trait for undo/redo operations
|
||||
///
|
||||
/// Each action must be able to execute (apply changes) and rollback (undo changes).
|
||||
@@ -343,24 +236,6 @@ impl ActionExecutor {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Register an action whose effect has **already been applied** to the document (and backend)
|
||||
/// outside the executor — e.g. a recording, which streams its content into the document live
|
||||
/// over time and can't be applied by a single synchronous `execute()`.
|
||||
///
|
||||
/// Unlike `execute`, this does NOT run `execute()`/`execute_backend()` (the effect is already
|
||||
/// present). It clears the redo stack, bumps the epoch (so the document reads as modified), and
|
||||
/// pushes the action so it becomes undoable: undo runs `rollback`/`rollback_backend` to remove
|
||||
/// the content, redo runs `execute`/`execute_backend` to bring it back. The action must be
|
||||
/// constructed already in its post-execute state (see e.g. `AddClipInstanceAction::already_applied`).
|
||||
pub fn push_applied(&mut self, action: Box<dyn Action>) {
|
||||
self.redo_stack.clear();
|
||||
self.epoch = self.epoch.wrapping_add(1);
|
||||
self.undo_stack.push(action);
|
||||
if self.undo_stack.len() > self.max_undo_depth {
|
||||
self.undo_stack.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Undo the last action
|
||||
///
|
||||
/// Returns Ok(true) if an action was undone, Ok(false) if undo stack is empty,
|
||||
@@ -372,7 +247,6 @@ impl ActionExecutor {
|
||||
Ok(()) => {
|
||||
// Move to redo stack
|
||||
self.redo_stack.push(action);
|
||||
self.epoch = self.epoch.wrapping_add(1);
|
||||
Ok(true)
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -397,7 +271,6 @@ impl ActionExecutor {
|
||||
Ok(()) => {
|
||||
// Move back to undo stack
|
||||
self.undo_stack.push(action);
|
||||
self.epoch = self.epoch.wrapping_add(1);
|
||||
Ok(true)
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -571,7 +444,6 @@ impl ActionExecutor {
|
||||
|
||||
// Move to redo stack
|
||||
self.redo_stack.push(action);
|
||||
self.epoch = self.epoch.wrapping_add(1);
|
||||
|
||||
Ok(true)
|
||||
} else {
|
||||
@@ -609,7 +481,6 @@ impl ActionExecutor {
|
||||
|
||||
// Move back to undo stack
|
||||
self.undo_stack.push(action);
|
||||
self.epoch = self.epoch.wrapping_add(1);
|
||||
|
||||
Ok(true)
|
||||
} else {
|
||||
|
||||
@@ -47,31 +47,6 @@ impl AddClipInstanceAction {
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct the action in its **already-applied** state: the clip instance is already in the
|
||||
/// document and its backend clip already exists (e.g. a finished recording). Pair with
|
||||
/// `ActionExecutor::push_applied` so the recording becomes undoable without re-adding anything.
|
||||
/// Undo will `rollback`/`rollback_backend` (removing the clip from doc + backend via the seeded
|
||||
/// ids); redo re-adds it via the normal `execute`/`execute_backend` path.
|
||||
pub fn already_applied(
|
||||
layer_id: Uuid,
|
||||
clip_instance: ClipInstance,
|
||||
backend_track_id: daw_backend::TrackId,
|
||||
backend_id: crate::action::BackendClipInstanceId,
|
||||
) -> Self {
|
||||
let (backend_midi_instance_id, backend_audio_instance_id) = match backend_id {
|
||||
crate::action::BackendClipInstanceId::Midi(id) => (Some(id), None),
|
||||
crate::action::BackendClipInstanceId::Audio(id) => (None, Some(id)),
|
||||
};
|
||||
Self {
|
||||
layer_id,
|
||||
clip_instance,
|
||||
executed: true, // already present in the document
|
||||
backend_track_id: Some(backend_track_id),
|
||||
backend_midi_instance_id,
|
||||
backend_audio_instance_id,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the ID of the clip instance that will be/was added
|
||||
pub fn clip_instance_id(&self) -> Uuid {
|
||||
self.clip_instance.id
|
||||
@@ -85,15 +60,13 @@ impl AddClipInstanceAction {
|
||||
|
||||
impl Action for AddClipInstanceAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
// Calculate the clip's effective duration in BEATS for overlap testing.
|
||||
// `get_clip_duration` is the content length in seconds; the placement span
|
||||
// must be beats (the timeline is beats-domain), so convert via the clip's
|
||||
// typed helper rather than treating the seconds span as beats.
|
||||
// The clip's content duration in ITS OWN domain, so the trims resolve correctly for MIDI.
|
||||
let clip_content = document.clip_trim_duration(&self.clip_instance.clip_id)
|
||||
// Calculate the clip's effective duration
|
||||
let clip_duration = document.get_clip_duration(&self.clip_instance.clip_id)
|
||||
.ok_or_else(|| format!("Clip {} not found", self.clip_instance.clip_id))?;
|
||||
let effective_duration = self.clip_instance
|
||||
.effective_duration_beats(clip_content, document.tempo_map());
|
||||
|
||||
let trim_start = self.clip_instance.trim_start;
|
||||
let trim_end = self.clip_instance.trim_end.unwrap_or(clip_duration);
|
||||
let effective_duration = trim_end - trim_start;
|
||||
|
||||
// Auto-adjust position for audio/video layers to avoid overlaps
|
||||
let adjusted_start = document.find_nearest_valid_position(
|
||||
@@ -131,7 +104,7 @@ impl Action for AddClipInstanceAction {
|
||||
AnyLayer::Group(_) => {
|
||||
return Err("Cannot add clip instances directly to group layers".to_string());
|
||||
}
|
||||
AnyLayer::Raster(_) | AnyLayer::Text(_) => {
|
||||
AnyLayer::Raster(_) => {
|
||||
return Err("Cannot add clip instances directly to group layers".to_string());
|
||||
}
|
||||
}
|
||||
@@ -172,8 +145,8 @@ impl Action for AddClipInstanceAction {
|
||||
AnyLayer::Group(_) => {
|
||||
// Group layers don't have clip instances, nothing to rollback
|
||||
}
|
||||
AnyLayer::Raster(_) | AnyLayer::Text(_) => {
|
||||
// Raster/text layers don't have clip instances, nothing to rollback
|
||||
AnyLayer::Raster(_) => {
|
||||
// Raster layers don't have clip instances, nothing to rollback
|
||||
}
|
||||
}
|
||||
self.executed = false;
|
||||
@@ -197,23 +170,112 @@ impl Action for AddClipInstanceAction {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Add via the shared BackendContext helper — the same one SetActiveTakeAction uses, so
|
||||
// the trim/duration conversions (and take-folder resolution) live in exactly one place.
|
||||
if let Some((track_id, backend_id)) =
|
||||
backend.add_clip_instance(document, &self.layer_id, &self.clip_instance)?
|
||||
{
|
||||
self.backend_track_id = Some(track_id);
|
||||
match backend_id {
|
||||
crate::action::BackendClipInstanceId::Midi(id) => {
|
||||
self.backend_midi_instance_id = Some(id)
|
||||
}
|
||||
crate::action::BackendClipInstanceId::Audio(id) => {
|
||||
self.backend_audio_instance_id = Some(id)
|
||||
// Look up the clip from the document
|
||||
let clip = document
|
||||
.get_audio_clip(&self.clip_instance.clip_id)
|
||||
.ok_or_else(|| "Audio clip not found".to_string())?;
|
||||
|
||||
// Look up backend track ID from layer mapping
|
||||
let backend_track_id = backend
|
||||
.layer_to_track_map
|
||||
.get(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not mapped to backend track", self.layer_id))?;
|
||||
|
||||
// Get audio controller
|
||||
let controller = backend
|
||||
.audio_controller
|
||||
.as_mut()
|
||||
.ok_or_else(|| "Audio controller not available".to_string())?;
|
||||
|
||||
// Handle different clip types
|
||||
use crate::clip::AudioClipType;
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
// Create a MIDI clip instance referencing the existing clip in the backend pool
|
||||
// No need to add to pool again - it was added during MIDI import
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
// Calculate internal start/end from trim parameters
|
||||
let internal_start = self.clip_instance.trim_start;
|
||||
let internal_end = self.clip_instance.trim_end.unwrap_or(clip.duration);
|
||||
let external_start = self.clip_instance.timeline_start;
|
||||
|
||||
// Calculate external duration (for looping if timeline_duration is set)
|
||||
let external_duration = self.clip_instance.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
|
||||
// Create MidiClipInstance
|
||||
let instance = daw_backend::MidiClipInstance::new(
|
||||
0, // Instance ID will be assigned by backend
|
||||
*midi_clip_id,
|
||||
daw_backend::Beats(internal_start),
|
||||
daw_backend::Beats(internal_end),
|
||||
daw_backend::Beats(external_start),
|
||||
daw_backend::Beats(external_duration),
|
||||
);
|
||||
|
||||
// Send query to add instance and get instance ID
|
||||
let query = Query::AddMidiClipInstanceSync(*backend_track_id, instance);
|
||||
|
||||
match controller.send_query(query)? {
|
||||
QueryResponse::MidiClipInstanceAdded(Ok(instance_id)) => {
|
||||
self.backend_track_id = Some(*backend_track_id);
|
||||
self.backend_midi_instance_id = Some(instance_id);
|
||||
|
||||
// Add to global clip instance mapping
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
self.clip_instance.id,
|
||||
crate::action::BackendClipInstanceId::Midi(instance_id)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
QueryResponse::MidiClipInstanceAdded(Err(e)) => Err(e),
|
||||
_ => Err("Unexpected query response".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
// `trim_*` / `clip.duration` are in SECONDS (audio content time),
|
||||
// while `timeline_*` and the backend's `duration` are in BEATS.
|
||||
let internal_start = self.clip_instance.trim_start;
|
||||
let internal_end = self.clip_instance.trim_end.unwrap_or(clip.duration);
|
||||
let start_time = self.clip_instance.timeline_start;
|
||||
// `effective_duration` is in BEATS. When `timeline_duration` is set
|
||||
// it already is; otherwise the clip occupies its natural content
|
||||
// length, so convert that seconds-span to beats at the clip's start
|
||||
// (NOT `internal_end - internal_start`, which is seconds — that was
|
||||
// the seconds-as-beats bug that made clips stop early off 60 BPM).
|
||||
let effective_duration = self.clip_instance.timeline_duration.unwrap_or_else(|| {
|
||||
let tempo_map = document.tempo_map();
|
||||
let content_secs = internal_end - internal_start;
|
||||
tempo_map.inverse_transform(tempo_map.transform(start_time) + content_secs)
|
||||
- start_time
|
||||
});
|
||||
|
||||
Ok(())
|
||||
let instance_id = controller.add_audio_clip(
|
||||
*backend_track_id,
|
||||
*audio_pool_index,
|
||||
start_time,
|
||||
effective_duration,
|
||||
internal_start,
|
||||
);
|
||||
|
||||
self.backend_track_id = Some(*backend_track_id);
|
||||
self.backend_audio_instance_id = Some(instance_id);
|
||||
|
||||
// Add to global clip instance mapping
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
self.clip_instance.id,
|
||||
crate::action::BackendClipInstanceId::Audio(instance_id)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips are not synced to backend until finalized
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rollback_backend(&mut self, backend: &mut BackendContext, _document: &Document) -> Result<(), String> {
|
||||
|
||||
@@ -159,7 +159,7 @@ mod tests {
|
||||
fn test_add_effect() {
|
||||
let (mut document, layer_id, def) = create_test_setup();
|
||||
|
||||
let instance = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance = def.create_instance(0.0, 10.0);
|
||||
let instance_id = instance.id;
|
||||
|
||||
let mut action = AddEffectAction::new(layer_id, instance);
|
||||
@@ -181,7 +181,7 @@ mod tests {
|
||||
fn test_add_effect_rollback() {
|
||||
let (mut document, layer_id, def) = create_test_setup();
|
||||
|
||||
let instance = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance = def.create_instance(0.0, 10.0);
|
||||
|
||||
let mut action = AddEffectAction::new(layer_id, instance);
|
||||
action.execute(&mut document).unwrap();
|
||||
@@ -201,19 +201,19 @@ mod tests {
|
||||
let (mut document, layer_id, def) = create_test_setup();
|
||||
|
||||
// Add first effect
|
||||
let instance1 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance1 = def.create_instance(0.0, 10.0);
|
||||
let id1 = instance1.id;
|
||||
let mut action1 = AddEffectAction::new(layer_id, instance1);
|
||||
action1.execute(&mut document).unwrap();
|
||||
|
||||
// Add second effect
|
||||
let instance2 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance2 = def.create_instance(0.0, 10.0);
|
||||
let id2 = instance2.id;
|
||||
let mut action2 = AddEffectAction::new(layer_id, instance2);
|
||||
action2.execute(&mut document).unwrap();
|
||||
|
||||
// Insert third effect at index 1 (between first and second)
|
||||
let instance3 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance3 = def.create_instance(0.0, 10.0);
|
||||
let id3 = instance3.id;
|
||||
let mut action3 = AddEffectAction::at_index(layer_id, instance3, 1);
|
||||
action3.execute(&mut document).unwrap();
|
||||
|
||||
@@ -138,7 +138,6 @@ impl Action for AddLayerAction {
|
||||
AnyLayer::Effect(_) => "Add effect layer",
|
||||
AnyLayer::Group(_) => "Add group layer",
|
||||
AnyLayer::Raster(_) => "Add raster layer",
|
||||
AnyLayer::Text(_) => "Add text layer",
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
|
||||
@@ -1,141 +0,0 @@
|
||||
//! Append freshly-recorded takes to an existing take folder.
|
||||
//!
|
||||
//! Cycle-recording over a region that already holds a take folder should *add* to that folder, not
|
||||
//! drop a second clip on top of it. Otherwise the takes from your second attempt are stranded in a
|
||||
//! separate, overlapping clip and you can't audition them against the first.
|
||||
//!
|
||||
//! The recorded content already exists in the backend pools by the time this runs (the engine put it
|
||||
//! there at stop), so this action only touches the document — plus the one backend clip the instance
|
||||
//! plays, which has to be repointed at the newly-active take.
|
||||
|
||||
use crate::action::{Action, BackendClipInstanceId, BackendContext};
|
||||
use crate::clip::AudioTake;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Action that appends takes to an instance's take list and selects the last of them.
|
||||
pub struct AppendTakesAction {
|
||||
layer_id: Uuid,
|
||||
/// The instance whose take list is being extended (and whose active take changes).
|
||||
instance_id: Uuid,
|
||||
/// The takes to add, in recording order.
|
||||
new_takes: Vec<AudioTake>,
|
||||
|
||||
// Stored during execute for rollback.
|
||||
old_take_count: usize,
|
||||
old_active_take: Option<usize>,
|
||||
executed: bool,
|
||||
}
|
||||
|
||||
impl AppendTakesAction {
|
||||
pub fn new(layer_id: Uuid, instance_id: Uuid, new_takes: Vec<AudioTake>) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
instance_id,
|
||||
new_takes,
|
||||
old_take_count: 0,
|
||||
old_active_take: None,
|
||||
executed: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Swap the instance's backend clip to whatever take the document now says is active.
|
||||
///
|
||||
/// Same remove + re-add as `SetActiveTakeAction` — there's no in-place pool-swap command.
|
||||
fn resync(&self, backend: &mut BackendContext, document: &Document) -> Result<(), String> {
|
||||
let instance = document
|
||||
.get_layer(&self.layer_id)
|
||||
.and_then(|l| match l {
|
||||
AnyLayer::Audio(al) => al.clip_instances.iter().find(|ci| ci.id == self.instance_id),
|
||||
_ => None,
|
||||
})
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("Clip instance {} not found", self.instance_id))?;
|
||||
|
||||
let existing: Option<BackendClipInstanceId> = backend
|
||||
.clip_instance_to_backend_map
|
||||
.get(&self.instance_id)
|
||||
.copied();
|
||||
let track_id = backend.layer_to_track_map.get(&self.layer_id).copied();
|
||||
if let (Some(backend_id), Some(track_id)) = (existing, track_id) {
|
||||
backend.remove_clip_instance(track_id, backend_id, self.instance_id);
|
||||
}
|
||||
|
||||
backend.add_clip_instance(document, &self.layer_id, &instance)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for AppendTakesAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
let AnyLayer::Audio(audio_layer) = layer else {
|
||||
return Err("Takes only exist on audio layers".to_string());
|
||||
};
|
||||
let instance = audio_layer
|
||||
.clip_instances
|
||||
.iter_mut()
|
||||
.find(|ci| ci.id == self.instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not found", self.instance_id))?;
|
||||
|
||||
// Only record the pre-state on the first execute; a redo must not overwrite it with the
|
||||
// post-state left behind by the previous run.
|
||||
if !self.executed {
|
||||
self.old_take_count = instance.takes.len();
|
||||
self.old_active_take = instance.active_take;
|
||||
}
|
||||
|
||||
// Number the new takes on from what's already there. Existing names are left alone — the
|
||||
// user may well have renamed them, and renumbering would clobber that.
|
||||
let base = instance.takes.len();
|
||||
for (i, take) in self.new_takes.iter().enumerate() {
|
||||
let mut take = take.clone();
|
||||
if take.name.is_empty() {
|
||||
take.name = format!("Take {}", base + i + 1);
|
||||
}
|
||||
instance.takes.push(take);
|
||||
}
|
||||
|
||||
// Land on the take just recorded, GarageBand-style.
|
||||
instance.active_take = Some(instance.takes.len() - 1);
|
||||
self.executed = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
if let Some(AnyLayer::Audio(audio_layer)) = document.get_layer_mut(&self.layer_id) {
|
||||
if let Some(instance) = audio_layer
|
||||
.clip_instances
|
||||
.iter_mut()
|
||||
.find(|ci| ci.id == self.instance_id)
|
||||
{
|
||||
instance.takes.truncate(self.old_take_count);
|
||||
instance.active_take = self.old_active_take;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
format!("Record {} take(s)", self.new_takes.len())
|
||||
}
|
||||
|
||||
fn execute_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
self.resync(backend, document)
|
||||
}
|
||||
|
||||
fn rollback_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
self.resync(backend, document)
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
//! Create-text-clip action
|
||||
//!
|
||||
//! Used by the text tool when the active layer is a **vector** layer: it creates a
|
||||
//! VectorClip containing a single text layer, registers it, and places a clip
|
||||
//! instance in the parent vector layer. The editor then enters that clip so the
|
||||
//! text is directly editable. All of this is one undoable step so undo never
|
||||
//! leaves an orphan clip.
|
||||
|
||||
use crate::action::Action;
|
||||
use crate::clip::{ClipInstance, VectorClip};
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::text_layer::TextLayer;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Minimum clip duration (seconds) when the document has no duration set yet.
|
||||
const FALLBACK_DURATION: f64 = 10.0;
|
||||
|
||||
pub struct CreateTextClipAction {
|
||||
/// The vector layer to place the clip instance in.
|
||||
parent_layer_id: Uuid,
|
||||
/// The text layer to embed (its id is stable across redo).
|
||||
text_layer: TextLayer,
|
||||
/// Instance position in the parent layer's space.
|
||||
position: (f64, f64),
|
||||
|
||||
// Assigned on first execute; reused on redo so ids are stable.
|
||||
clip_id: Option<Uuid>,
|
||||
instance_id: Option<Uuid>,
|
||||
executed: bool,
|
||||
}
|
||||
|
||||
impl CreateTextClipAction {
|
||||
pub fn new(parent_layer_id: Uuid, text_layer: TextLayer, position: (f64, f64)) -> Self {
|
||||
Self {
|
||||
parent_layer_id,
|
||||
text_layer,
|
||||
position,
|
||||
clip_id: None,
|
||||
instance_id: None,
|
||||
executed: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Preset the clip + instance ids (so the caller knows them up-front for
|
||||
/// entering the clip immediately after execute). Ids stay stable across redo.
|
||||
pub fn with_ids(mut self, clip_id: Uuid, instance_id: Uuid) -> Self {
|
||||
self.clip_id = Some(clip_id);
|
||||
self.instance_id = Some(instance_id);
|
||||
self
|
||||
}
|
||||
|
||||
/// The vector clip id (after execute).
|
||||
pub fn clip_id(&self) -> Option<Uuid> {
|
||||
self.clip_id
|
||||
}
|
||||
|
||||
/// The clip instance id placed in the parent layer (after execute).
|
||||
pub fn instance_id(&self) -> Option<Uuid> {
|
||||
self.instance_id
|
||||
}
|
||||
|
||||
/// The embedded text layer's id.
|
||||
pub fn text_layer_id(&self) -> Uuid {
|
||||
self.text_layer.layer.id
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for CreateTextClipAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let clip_id = self.clip_id.unwrap_or_else(Uuid::new_v4);
|
||||
self.clip_id = Some(clip_id);
|
||||
let instance_id = self.instance_id.unwrap_or_else(Uuid::new_v4);
|
||||
self.instance_id = Some(instance_id);
|
||||
|
||||
let duration = if document.duration > 0.0 { document.duration } else { FALLBACK_DURATION };
|
||||
|
||||
// Build the clip with the text layer as its single root layer.
|
||||
let mut clip = VectorClip::with_id(
|
||||
clip_id,
|
||||
"Text",
|
||||
self.text_layer.box_width.max(1.0),
|
||||
self.text_layer.box_height.max(1.0),
|
||||
duration,
|
||||
);
|
||||
// A movie clip (not keyframe-gated) so the text persists across the timeline.
|
||||
clip.is_group = false;
|
||||
clip.layers.add_root(AnyLayer::Text(self.text_layer.clone()));
|
||||
// Registers the clip and its layers in layer_to_clip_map for O(1) lookup.
|
||||
document.add_vector_clip(clip);
|
||||
|
||||
// Place an instance of the clip in the parent vector layer.
|
||||
let layer = document
|
||||
.get_layer_mut(&self.parent_layer_id)
|
||||
.ok_or_else(|| format!("Parent layer {} not found", self.parent_layer_id))?;
|
||||
let AnyLayer::Vector(vector_layer) = layer else {
|
||||
return Err("Text clip can only be created inside a vector layer".to_string());
|
||||
};
|
||||
let mut instance = ClipInstance::with_id(instance_id, clip_id);
|
||||
instance.transform.x = self.position.0;
|
||||
instance.transform.y = self.position.1;
|
||||
vector_layer.clip_instances.push(instance);
|
||||
|
||||
self.executed = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
if !self.executed {
|
||||
return Ok(());
|
||||
}
|
||||
// Remove the clip instance from the parent layer.
|
||||
if let (Some(instance_id), Some(AnyLayer::Vector(vector_layer))) =
|
||||
(self.instance_id, document.get_layer_mut(&self.parent_layer_id))
|
||||
{
|
||||
vector_layer.clip_instances.retain(|ci| ci.id != instance_id);
|
||||
}
|
||||
// Remove the clip and its layer_to_clip_map registrations.
|
||||
if let Some(clip_id) = self.clip_id {
|
||||
if let Some(clip) = document.vector_clips.remove(&clip_id) {
|
||||
for node in &clip.layers.roots {
|
||||
document.layer_to_clip_map.remove(&node.data.id());
|
||||
}
|
||||
}
|
||||
}
|
||||
self.executed = false;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
"Add text".to_string()
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,8 @@ use crate::layer::AnyLayer;
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Per-instance loop change: (instance_id, old_timeline_duration, new_timeline_duration, old_loop_before, new_loop_before).
|
||||
/// All durations/offsets are in beats.
|
||||
pub type LoopEntry = (Uuid, Option<daw_backend::Beats>, Option<daw_backend::Beats>, Option<daw_backend::Beats>, Option<daw_backend::Beats>);
|
||||
/// Per-instance loop change: (instance_id, old_timeline_duration, new_timeline_duration, old_loop_before, new_loop_before)
|
||||
pub type LoopEntry = (Uuid, Option<f64>, Option<f64>, Option<f64>, Option<f64>);
|
||||
|
||||
/// Action that changes the loop duration of clip instances
|
||||
pub struct LoopClipInstancesAction {
|
||||
@@ -38,7 +37,6 @@ impl Action for LoopClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
AnyLayer::Group(_) => continue,
|
||||
AnyLayer::Raster(_) => continue,
|
||||
AnyLayer::Text(_) => continue,
|
||||
};
|
||||
|
||||
for (instance_id, _old_dur, new_dur, _old_lb, new_lb) in loops {
|
||||
@@ -63,7 +61,6 @@ impl Action for LoopClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
AnyLayer::Group(_) => continue,
|
||||
AnyLayer::Raster(_) => continue,
|
||||
AnyLayer::Text(_) => continue,
|
||||
};
|
||||
|
||||
for (instance_id, old_dur, _new_dur, old_lb, _new_lb) in loops {
|
||||
@@ -91,7 +88,7 @@ impl Action for LoopClipInstancesAction {
|
||||
|
||||
impl LoopClipInstancesAction {
|
||||
fn sync_backend(&self, backend: &mut crate::action::BackendContext, document: &Document, rollback: bool) -> Result<(), String> {
|
||||
use crate::clip::ResolvedContent;
|
||||
use crate::clip::AudioClipType;
|
||||
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
Some(c) => c,
|
||||
@@ -128,22 +125,19 @@ impl LoopClipInstancesAction {
|
||||
(new_dur, new_lb)
|
||||
};
|
||||
|
||||
// Natural content length as a beats span (the fallback when no explicit
|
||||
// timeline_duration is set). Resolved in the clip's own domain, so MIDI's beats
|
||||
// content carries over directly rather than being read as seconds.
|
||||
let content_window_beats = instance.effective_duration_beats(
|
||||
clip.content_duration(),
|
||||
document.tempo_map(),
|
||||
);
|
||||
let right_duration = target_duration.unwrap_or(content_window_beats);
|
||||
let left_duration = target_loop_before.unwrap_or(daw_backend::Beats::ZERO);
|
||||
let content_window = {
|
||||
let trim_end = instance.trim_end.unwrap_or(clip.duration);
|
||||
(trim_end - instance.trim_start).max(0.0)
|
||||
};
|
||||
let right_duration = target_duration.unwrap_or(content_window);
|
||||
let left_duration = target_loop_before.unwrap_or(0.0);
|
||||
let external_duration = left_duration + right_duration;
|
||||
let external_start = instance.timeline_start - left_duration;
|
||||
|
||||
let get_backend_clip_id = |inst_id: &Uuid| -> Result<u32, String> {
|
||||
match &instance.resolve(clip) {
|
||||
ResolvedContent::Midi { midi_clip_id } => Ok(*midi_clip_id),
|
||||
ResolvedContent::Audio { .. } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => Ok(*midi_clip_id),
|
||||
AudioClipType::Sampled { .. } => {
|
||||
let backend_id = backend.clip_instance_to_backend_map.get(inst_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not mapped to backend", inst_id))?;
|
||||
match backend_id {
|
||||
@@ -151,7 +145,7 @@ impl LoopClipInstancesAction {
|
||||
_ => Err("Expected audio instance ID for sampled clip".to_string()),
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => Err("Cannot sync recording clip".to_string()),
|
||||
AudioClipType::Recording => Err("Cannot sync recording clip".to_string()),
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,419 +0,0 @@
|
||||
//! Take management: delete and rename the takes on a clip instance.
|
||||
//!
|
||||
//! Takes live on the INSTANCE, so both of these are naturally scoped to the one the user clicked —
|
||||
//! deleting a take from one half of a comped split leaves the other half's list alone.
|
||||
|
||||
use crate::action::{Action, BackendClipInstanceId, BackendContext};
|
||||
use crate::clip::{AudioTake, ClipInstance};
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// The instance a take action targets, looked up mutably.
|
||||
fn instance_mut<'a>(
|
||||
document: &'a mut Document,
|
||||
layer_id: &Uuid,
|
||||
instance_id: &Uuid,
|
||||
) -> Result<&'a mut ClipInstance, String> {
|
||||
let layer = document
|
||||
.get_layer_mut(layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
|
||||
let AnyLayer::Audio(audio_layer) = layer else {
|
||||
return Err("Takes only exist on audio layers".to_string());
|
||||
};
|
||||
audio_layer
|
||||
.clip_instances
|
||||
.iter_mut()
|
||||
.find(|ci| ci.id == *instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not found", instance_id))
|
||||
}
|
||||
|
||||
/// Swap an instance's backend clip to whatever take the document now says is active.
|
||||
///
|
||||
/// The same remove + re-add as `SetActiveTakeAction` — there's no in-place pool-swap command.
|
||||
fn resync(
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
layer_id: &Uuid,
|
||||
instance_id: &Uuid,
|
||||
) -> Result<(), String> {
|
||||
let instance = document
|
||||
.get_layer(layer_id)
|
||||
.and_then(|l| match l {
|
||||
AnyLayer::Audio(al) => al.clip_instances.iter().find(|ci| ci.id == *instance_id),
|
||||
_ => None,
|
||||
})
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("Clip instance {} not found", instance_id))?;
|
||||
|
||||
let existing: Option<BackendClipInstanceId> = backend
|
||||
.clip_instance_to_backend_map
|
||||
.get(instance_id)
|
||||
.copied();
|
||||
let track_id = backend.layer_to_track_map.get(layer_id).copied();
|
||||
if let (Some(backend_id), Some(track_id)) = (existing, track_id) {
|
||||
backend.remove_clip_instance(track_id, backend_id, *instance_id);
|
||||
}
|
||||
|
||||
backend.add_clip_instance(document, layer_id, &instance)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove a take from an instance's take list.
|
||||
///
|
||||
/// The take's recorded audio/MIDI stays in the backend pool — undo has to be able to put it back,
|
||||
/// and other instances (the other half of a split, say) may still be playing it.
|
||||
pub struct DeleteTakeAction {
|
||||
layer_id: Uuid,
|
||||
instance_id: Uuid,
|
||||
take_index: usize,
|
||||
|
||||
// Stored during execute for rollback.
|
||||
removed: Option<AudioTake>,
|
||||
old_active_take: Option<usize>,
|
||||
}
|
||||
|
||||
impl DeleteTakeAction {
|
||||
pub fn new(layer_id: Uuid, instance_id: Uuid, take_index: usize) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
instance_id,
|
||||
take_index,
|
||||
removed: None,
|
||||
old_active_take: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for DeleteTakeAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let instance = instance_mut(document, &self.layer_id, &self.instance_id)?;
|
||||
|
||||
if self.take_index >= instance.takes.len() {
|
||||
return Err(format!("Take {} does not exist", self.take_index + 1));
|
||||
}
|
||||
// An instance with no takes at all would fall back to the clip's own content, which for a
|
||||
// cycle recording is a take we may just have deleted. Refuse rather than strand it.
|
||||
if instance.takes.len() == 1 {
|
||||
return Err("Can't delete the only take".to_string());
|
||||
}
|
||||
|
||||
self.old_active_take = instance.active_take;
|
||||
self.removed = Some(instance.takes.remove(self.take_index));
|
||||
|
||||
// Everything above the removed take shifts down one, so the selection has to move with it.
|
||||
// Deleting the *active* take lands on the one that took its place (or the new last take, if
|
||||
// it was at the end) — that keeps the clip sounding rather than silently picking take 1.
|
||||
let active = instance.active_take.unwrap_or(0);
|
||||
instance.active_take = Some(if active > self.take_index {
|
||||
active - 1
|
||||
} else if active == self.take_index {
|
||||
self.take_index.min(instance.takes.len() - 1)
|
||||
} else {
|
||||
active
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let Some(take) = self.removed.take() else {
|
||||
return Ok(());
|
||||
};
|
||||
let instance = instance_mut(document, &self.layer_id, &self.instance_id)?;
|
||||
let at = self.take_index.min(instance.takes.len());
|
||||
instance.takes.insert(at, take);
|
||||
instance.active_take = self.old_active_take;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
format!("Delete take {}", self.take_index + 1)
|
||||
}
|
||||
|
||||
fn execute_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
resync(backend, document, &self.layer_id, &self.instance_id)
|
||||
}
|
||||
|
||||
fn rollback_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
resync(backend, document, &self.layer_id, &self.instance_id)
|
||||
}
|
||||
}
|
||||
|
||||
/// Throw away every take except the one that's playing.
|
||||
///
|
||||
/// The tidy-up once you've picked your keeper. Scoped to this instance, so on a comped split it
|
||||
/// prunes the half you clicked and leaves the other half's alternatives intact.
|
||||
pub struct DeleteUnusedTakesAction {
|
||||
layer_id: Uuid,
|
||||
instance_id: Uuid,
|
||||
|
||||
// Stored during execute for rollback.
|
||||
old_takes: Vec<AudioTake>,
|
||||
old_active_take: Option<usize>,
|
||||
}
|
||||
|
||||
impl DeleteUnusedTakesAction {
|
||||
pub fn new(layer_id: Uuid, instance_id: Uuid) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
instance_id,
|
||||
old_takes: Vec::new(),
|
||||
old_active_take: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for DeleteUnusedTakesAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let instance = instance_mut(document, &self.layer_id, &self.instance_id)?;
|
||||
if instance.takes.len() < 2 {
|
||||
return Err("Nothing to delete".to_string());
|
||||
}
|
||||
|
||||
let keep = instance.active_take_index();
|
||||
self.old_takes = instance.takes.clone();
|
||||
self.old_active_take = instance.active_take;
|
||||
|
||||
let kept = instance.takes.remove(keep);
|
||||
instance.takes.clear();
|
||||
instance.takes.push(kept);
|
||||
instance.active_take = Some(0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let instance = instance_mut(document, &self.layer_id, &self.instance_id)?;
|
||||
instance.takes = std::mem::take(&mut self.old_takes);
|
||||
instance.active_take = self.old_active_take;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
"Delete unused takes".to_string()
|
||||
}
|
||||
|
||||
// The take that plays doesn't change, so the backend clip is already correct.
|
||||
}
|
||||
|
||||
/// Rename a take. Document-only — which take *plays* doesn't change, so the backend is untouched.
|
||||
pub struct RenameTakeAction {
|
||||
layer_id: Uuid,
|
||||
instance_id: Uuid,
|
||||
take_index: usize,
|
||||
new_name: String,
|
||||
old_name: String,
|
||||
}
|
||||
|
||||
impl RenameTakeAction {
|
||||
pub fn new(layer_id: Uuid, instance_id: Uuid, take_index: usize, new_name: String) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
instance_id,
|
||||
take_index,
|
||||
new_name,
|
||||
old_name: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for RenameTakeAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let instance = instance_mut(document, &self.layer_id, &self.instance_id)?;
|
||||
let take = instance
|
||||
.takes
|
||||
.get_mut(self.take_index)
|
||||
.ok_or_else(|| format!("Take {} does not exist", self.take_index + 1))?;
|
||||
self.old_name = std::mem::replace(&mut take.name, self.new_name.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let instance = instance_mut(document, &self.layer_id, &self.instance_id)?;
|
||||
if let Some(take) = instance.takes.get_mut(self.take_index) {
|
||||
take.name = self.old_name.clone();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
format!("Rename take to \"{}\"", self.new_name)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::clip::TakeContent;
|
||||
use crate::layer::AudioLayer;
|
||||
|
||||
/// A document with one audio layer holding one instance with 4 takes (pools 10..13).
|
||||
fn doc_with_takes() -> (Document, Uuid, Uuid) {
|
||||
let mut document = Document::new("Test");
|
||||
let clip = crate::clip::AudioClip::new_sampled("Cycle rec", 10, 2.0);
|
||||
let clip_id = document.add_audio_clip(clip);
|
||||
|
||||
let mut instance = ClipInstance::new(clip_id);
|
||||
instance.takes = (10..14)
|
||||
.map(|pool| AudioTake {
|
||||
name: format!("Take {}", pool - 9),
|
||||
content: TakeContent::Audio { audio_pool_index: pool },
|
||||
})
|
||||
.collect();
|
||||
let instance_id = instance.id;
|
||||
|
||||
let mut layer = AudioLayer::new("Layer");
|
||||
layer.clip_instances.push(instance);
|
||||
let layer_id = document.root.add_child(AnyLayer::Audio(layer));
|
||||
(document, layer_id, instance_id)
|
||||
}
|
||||
|
||||
fn takes_of(document: &Document, layer_id: &Uuid, instance_id: &Uuid) -> ClipInstance {
|
||||
let AnyLayer::Audio(al) = document.get_layer(layer_id).unwrap() else { panic!() };
|
||||
al.clip_instances.iter().find(|ci| ci.id == *instance_id).unwrap().clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_a_take_below_the_active_one_shifts_the_selection_down() {
|
||||
// Everything above the removed take shifts down one, so a selection above it has to move
|
||||
// with it — otherwise the instance silently starts playing a different take.
|
||||
let (mut document, layer_id, instance_id) = doc_with_takes();
|
||||
{
|
||||
let AnyLayer::Audio(al) = document.get_layer_mut(&layer_id).unwrap() else { panic!() };
|
||||
al.clip_instances[0].active_take = Some(3); // playing pool 13
|
||||
}
|
||||
|
||||
DeleteTakeAction::new(layer_id, instance_id, 1)
|
||||
.execute(&mut document)
|
||||
.expect("delete");
|
||||
|
||||
let inst = takes_of(&document, &layer_id, &instance_id);
|
||||
assert_eq!(inst.takes.len(), 3);
|
||||
assert_eq!(inst.active_take, Some(2), "index shifted down with the take");
|
||||
assert_eq!(
|
||||
inst.takes[inst.active_take_index()].content,
|
||||
TakeContent::Audio { audio_pool_index: 13 },
|
||||
"still playing the same take it was",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_the_active_take_lands_on_its_replacement() {
|
||||
// Deleting what you're listening to should hand you the take that took its place, not
|
||||
// silently jump you back to take 1.
|
||||
let (mut document, layer_id, instance_id) = doc_with_takes();
|
||||
{
|
||||
let AnyLayer::Audio(al) = document.get_layer_mut(&layer_id).unwrap() else { panic!() };
|
||||
al.clip_instances[0].active_take = Some(1); // playing pool 11
|
||||
}
|
||||
|
||||
DeleteTakeAction::new(layer_id, instance_id, 1)
|
||||
.execute(&mut document)
|
||||
.expect("delete");
|
||||
|
||||
let inst = takes_of(&document, &layer_id, &instance_id);
|
||||
assert_eq!(inst.active_take, Some(1));
|
||||
assert_eq!(
|
||||
inst.takes[1].content,
|
||||
TakeContent::Audio { audio_pool_index: 12 },
|
||||
"the take that slid into the deleted one's place",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_the_last_take_in_the_list_steps_back() {
|
||||
let (mut document, layer_id, instance_id) = doc_with_takes();
|
||||
{
|
||||
let AnyLayer::Audio(al) = document.get_layer_mut(&layer_id).unwrap() else { panic!() };
|
||||
al.clip_instances[0].active_take = Some(3);
|
||||
}
|
||||
|
||||
DeleteTakeAction::new(layer_id, instance_id, 3)
|
||||
.execute(&mut document)
|
||||
.expect("delete");
|
||||
|
||||
let inst = takes_of(&document, &layer_id, &instance_id);
|
||||
assert_eq!(inst.active_take, Some(2), "there is no take 4 to land on");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_only_take_cannot_be_deleted() {
|
||||
let (mut document, layer_id, instance_id) = doc_with_takes();
|
||||
{
|
||||
let AnyLayer::Audio(al) = document.get_layer_mut(&layer_id).unwrap() else { panic!() };
|
||||
al.clip_instances[0].takes.truncate(1);
|
||||
}
|
||||
assert!(DeleteTakeAction::new(layer_id, instance_id, 0)
|
||||
.execute(&mut document)
|
||||
.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undoing_a_delete_puts_the_take_back_where_it_was() {
|
||||
let (mut document, layer_id, instance_id) = doc_with_takes();
|
||||
{
|
||||
let AnyLayer::Audio(al) = document.get_layer_mut(&layer_id).unwrap() else { panic!() };
|
||||
al.clip_instances[0].active_take = Some(2);
|
||||
}
|
||||
|
||||
let mut action = DeleteTakeAction::new(layer_id, instance_id, 1);
|
||||
action.execute(&mut document).expect("delete");
|
||||
action.rollback(&mut document).expect("undo");
|
||||
|
||||
let inst = takes_of(&document, &layer_id, &instance_id);
|
||||
assert_eq!(inst.takes.len(), 4);
|
||||
assert_eq!(
|
||||
inst.takes[1].content,
|
||||
TakeContent::Audio { audio_pool_index: 11 },
|
||||
"restored at its original index",
|
||||
);
|
||||
assert_eq!(inst.active_take, Some(2), "and the selection with it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_unused_takes_keeps_the_one_thats_playing() {
|
||||
let (mut document, layer_id, instance_id) = doc_with_takes();
|
||||
{
|
||||
let AnyLayer::Audio(al) = document.get_layer_mut(&layer_id).unwrap() else { panic!() };
|
||||
al.clip_instances[0].active_take = Some(2); // pool 12 — the keeper
|
||||
}
|
||||
|
||||
let mut action = DeleteUnusedTakesAction::new(layer_id, instance_id);
|
||||
action.execute(&mut document).expect("prune");
|
||||
|
||||
let inst = takes_of(&document, &layer_id, &instance_id);
|
||||
assert_eq!(inst.takes.len(), 1);
|
||||
assert_eq!(inst.active_take, Some(0));
|
||||
assert_eq!(
|
||||
inst.takes[0].content,
|
||||
TakeContent::Audio { audio_pool_index: 12 },
|
||||
"the take that was playing survives, and nothing else",
|
||||
);
|
||||
|
||||
action.rollback(&mut document).expect("undo");
|
||||
let inst = takes_of(&document, &layer_id, &instance_id);
|
||||
assert_eq!(inst.takes.len(), 4);
|
||||
assert_eq!(inst.active_take, Some(2), "back to what was playing before");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renaming_a_take_round_trips() {
|
||||
let (mut document, layer_id, instance_id) = doc_with_takes();
|
||||
let mut action =
|
||||
RenameTakeAction::new(layer_id, instance_id, 2, "The good one".to_string());
|
||||
|
||||
action.execute(&mut document).expect("rename");
|
||||
assert_eq!(takes_of(&document, &layer_id, &instance_id).takes[2].name, "The good one");
|
||||
|
||||
action.rollback(&mut document).expect("undo");
|
||||
assert_eq!(takes_of(&document, &layer_id, &instance_id).takes[2].name, "Take 3");
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,8 @@ pub mod add_layer;
|
||||
pub mod add_shape;
|
||||
pub mod modify_shape_path;
|
||||
pub mod move_clip_instances;
|
||||
pub mod reorder_clip_instances;
|
||||
pub mod paint_bucket;
|
||||
pub mod remove_effect;
|
||||
pub mod set_cycle_region;
|
||||
pub mod append_takes;
|
||||
pub mod manage_takes;
|
||||
pub mod set_active_take;
|
||||
pub mod set_document_properties;
|
||||
pub mod set_instance_properties;
|
||||
pub mod set_layer_properties;
|
||||
@@ -49,21 +44,13 @@ pub mod resize_raster_layer;
|
||||
pub mod move_layer;
|
||||
pub mod set_fill_paint;
|
||||
pub mod set_image_fill;
|
||||
pub mod create_text_clip;
|
||||
pub mod set_text_content;
|
||||
pub mod resize_text_box;
|
||||
|
||||
pub use add_clip_instance::AddClipInstanceAction;
|
||||
pub use set_cycle_region::SetCycleRegionAction;
|
||||
pub use append_takes::AppendTakesAction;
|
||||
pub use manage_takes::{DeleteTakeAction, DeleteUnusedTakesAction, RenameTakeAction};
|
||||
pub use set_active_take::SetActiveTakeAction;
|
||||
pub use add_effect::AddEffectAction;
|
||||
pub use add_layer::AddLayerAction;
|
||||
pub use add_shape::AddShapeAction;
|
||||
pub use modify_shape_path::ModifyGraphAction;
|
||||
pub use move_clip_instances::MoveClipInstancesAction;
|
||||
pub use reorder_clip_instances::ReorderClipInstancesAction;
|
||||
pub use paint_bucket::PaintBucketAction;
|
||||
pub use remove_effect::RemoveEffectAction;
|
||||
pub use set_document_properties::SetDocumentPropertiesAction;
|
||||
@@ -97,6 +84,3 @@ pub use set_fill_paint::SetFillPaintAction;
|
||||
pub use set_image_fill::SetImageFillAction;
|
||||
pub use change_bpm::ChangeBpmAction;
|
||||
pub use change_fps::ChangeFpsAction;
|
||||
pub use create_text_clip::CreateTextClipAction;
|
||||
pub use set_text_content::SetTextContentAction;
|
||||
pub use resize_text_box::ResizeTextBoxAction;
|
||||
|
||||
@@ -6,15 +6,13 @@ use crate::action::Action;
|
||||
use crate::clip::ClipInstance;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use daw_backend::Beats;
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Action that moves clip instances to new timeline positions
|
||||
pub struct MoveClipInstancesAction {
|
||||
/// Map of layer IDs to vectors of (clip_instance_id, old_timeline_start, new_timeline_start).
|
||||
/// Timeline positions are in beats.
|
||||
layer_moves: HashMap<Uuid, Vec<(Uuid, Beats, Beats)>>,
|
||||
/// Map of layer IDs to vectors of (clip_instance_id, old_timeline_start, new_timeline_start)
|
||||
layer_moves: HashMap<Uuid, Vec<(Uuid, f64, f64)>>,
|
||||
}
|
||||
|
||||
impl MoveClipInstancesAction {
|
||||
@@ -22,8 +20,8 @@ impl MoveClipInstancesAction {
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `layer_moves` - Map of layer IDs to vectors of (clip_instance_id, old_timeline_start, new_timeline_start) in beats
|
||||
pub fn new(layer_moves: HashMap<Uuid, Vec<(Uuid, Beats, Beats)>>) -> Self {
|
||||
/// * `layer_moves` - Map of layer IDs to vectors of (clip_instance_id, old_timeline_start, new_timeline_start)
|
||||
pub fn new(layer_moves: HashMap<Uuid, Vec<(Uuid, f64, f64)>>) -> Self {
|
||||
Self { layer_moves }
|
||||
}
|
||||
}
|
||||
@@ -44,7 +42,7 @@ impl Action for MoveClipInstancesAction {
|
||||
|
||||
// Check if this instance is in a group
|
||||
if let Some(group) = document.find_group_for_instance(instance_id) {
|
||||
let offset = *new_start - *old_start;
|
||||
let offset = new_start - old_start;
|
||||
|
||||
// Add all group members to the move list
|
||||
for (member_layer_id, member_instance_id) in group.get_members() {
|
||||
@@ -60,7 +58,6 @@ impl Action for MoveClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
if let Some(instance) = clip_instances.iter().find(|ci| ci.id == *member_instance_id) {
|
||||
@@ -79,7 +76,7 @@ impl Action for MoveClipInstancesAction {
|
||||
}
|
||||
|
||||
// Auto-adjust moves to avoid overlaps
|
||||
let mut adjusted_moves: HashMap<Uuid, Vec<(Uuid, Beats, Beats)>> = HashMap::new();
|
||||
let mut adjusted_moves: HashMap<Uuid, Vec<(Uuid, f64, f64)>> = HashMap::new();
|
||||
|
||||
for (layer_id, moves) in &expanded_moves {
|
||||
let layer = document.get_layer(layer_id)
|
||||
@@ -100,13 +97,12 @@ impl Action for MoveClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
let group: Vec<(Uuid, Beats, Beats)> = moves.iter().filter_map(|(id, old_start, _)| {
|
||||
let group: Vec<(Uuid, f64, f64)> = moves.iter().filter_map(|(id, old_start, _)| {
|
||||
let inst = clip_instances.iter().find(|ci| &ci.id == id)?;
|
||||
let dur = document.clip_trim_duration(&inst.clip_id)?;
|
||||
let eff = inst.effective_duration_beats(dur, document.tempo_map());
|
||||
let dur = document.get_clip_duration(&inst.clip_id)?;
|
||||
let eff = inst.trim_end.unwrap_or(dur) - inst.trim_start;
|
||||
Some((*id, *old_start, eff))
|
||||
}).collect();
|
||||
|
||||
@@ -114,7 +110,7 @@ impl Action for MoveClipInstancesAction {
|
||||
let clamped = document.clamp_group_move_offset(layer_id, &group, desired_offset);
|
||||
|
||||
for (instance_id, old_start, _) in moves {
|
||||
adjusted_layer_moves.push((*instance_id, *old_start, (*old_start + clamped).max(Beats::ZERO)));
|
||||
adjusted_layer_moves.push((*instance_id, *old_start, (*old_start + clamped).max(0.0)));
|
||||
}
|
||||
|
||||
adjusted_moves.insert(*layer_id, adjusted_layer_moves);
|
||||
@@ -136,7 +132,6 @@ impl Action for MoveClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
AnyLayer::Group(_) => continue,
|
||||
AnyLayer::Raster(_) => continue,
|
||||
AnyLayer::Text(_) => continue,
|
||||
};
|
||||
|
||||
// Update timeline_start for each clip instance
|
||||
@@ -164,7 +159,6 @@ impl Action for MoveClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
AnyLayer::Group(_) => continue,
|
||||
AnyLayer::Raster(_) => continue,
|
||||
AnyLayer::Text(_) => continue,
|
||||
};
|
||||
|
||||
// Restore original timeline_start for each clip instance
|
||||
@@ -190,7 +184,7 @@ impl Action for MoveClipInstancesAction {
|
||||
|
||||
fn execute_backend(&mut self, backend: &mut crate::action::BackendContext, document: &Document) -> Result<(), String> {
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::clip::ResolvedContent;
|
||||
use crate::clip::AudioClipType;
|
||||
|
||||
// Get audio controller
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
@@ -210,10 +204,9 @@ impl Action for MoveClipInstancesAction {
|
||||
if let Some(instance) = vl.clip_instances.iter().find(|ci| ci.id == *instance_id) {
|
||||
// Check if this clip has a metatrack
|
||||
if let Some(&metatrack_id) = backend.layer_to_track_map.get(&instance.clip_id) {
|
||||
controller.set_offset(metatrack_id, document.tempo_map().beats_to_seconds(*new_start));
|
||||
// A vector clip's content is wall-clock, so its content times ARE seconds.
|
||||
controller.set_trim_start(metatrack_id, daw_backend::Seconds(instance.trim_start.raw()));
|
||||
controller.set_trim_end(metatrack_id, instance.trim_end.map(|t| daw_backend::Seconds(t.raw())));
|
||||
controller.set_offset(metatrack_id, *new_start);
|
||||
controller.set_trim_start(metatrack_id, instance.trim_start);
|
||||
controller.set_trim_end(metatrack_id, instance.trim_end);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,12 +240,12 @@ impl Action for MoveClipInstancesAction {
|
||||
.ok_or_else(|| format!("Audio clip {} not found", instance.clip_id))?;
|
||||
|
||||
// Handle move based on clip type
|
||||
match &instance.resolve(clip) {
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
// For MIDI: move_clip expects the pool clip ID
|
||||
controller.move_clip(*track_id, *midi_clip_id, *new_start);
|
||||
}
|
||||
ResolvedContent::Audio { .. } => {
|
||||
AudioClipType::Sampled { .. } => {
|
||||
// For sampled audio: move_clip expects the instance ID
|
||||
let backend_instance_id = backend.clip_instance_to_backend_map.get(instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not mapped to backend", instance_id))?;
|
||||
@@ -264,7 +257,7 @@ impl Action for MoveClipInstancesAction {
|
||||
_ => return Err("Expected audio instance ID for sampled clip".to_string()),
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips cannot be moved - skip
|
||||
}
|
||||
}
|
||||
@@ -276,7 +269,7 @@ impl Action for MoveClipInstancesAction {
|
||||
|
||||
fn rollback_backend(&mut self, backend: &mut crate::action::BackendContext, document: &Document) -> Result<(), String> {
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::clip::ResolvedContent;
|
||||
use crate::clip::AudioClipType;
|
||||
|
||||
// Get audio controller
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
@@ -295,10 +288,9 @@ impl Action for MoveClipInstancesAction {
|
||||
for (instance_id, old_start, _new_start) in moves {
|
||||
if let Some(instance) = vl.clip_instances.iter().find(|ci| ci.id == *instance_id) {
|
||||
if let Some(&metatrack_id) = backend.layer_to_track_map.get(&instance.clip_id) {
|
||||
controller.set_offset(metatrack_id, document.tempo_map().beats_to_seconds(*old_start));
|
||||
// A vector clip's content is wall-clock, so its content times ARE seconds.
|
||||
controller.set_trim_start(metatrack_id, daw_backend::Seconds(instance.trim_start.raw()));
|
||||
controller.set_trim_end(metatrack_id, instance.trim_end.map(|t| daw_backend::Seconds(t.raw())));
|
||||
controller.set_offset(metatrack_id, *old_start);
|
||||
controller.set_trim_start(metatrack_id, instance.trim_start);
|
||||
controller.set_trim_end(metatrack_id, instance.trim_end);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -332,12 +324,12 @@ impl Action for MoveClipInstancesAction {
|
||||
.ok_or_else(|| format!("Audio clip {} not found", instance.clip_id))?;
|
||||
|
||||
// Handle move based on clip type (restore old position)
|
||||
match &instance.resolve(clip) {
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
// For MIDI: move_clip expects the pool clip ID
|
||||
controller.move_clip(*track_id, *midi_clip_id, *old_start);
|
||||
}
|
||||
ResolvedContent::Audio { .. } => {
|
||||
AudioClipType::Sampled { .. } => {
|
||||
// For sampled audio: move_clip expects the instance ID
|
||||
let backend_instance_id = backend.clip_instance_to_backend_map.get(instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not mapped to backend", instance_id))?;
|
||||
@@ -349,7 +341,7 @@ impl Action for MoveClipInstancesAction {
|
||||
_ => return Err("Expected audio instance ID for sampled clip".to_string()),
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips cannot be moved - skip
|
||||
}
|
||||
}
|
||||
@@ -377,15 +369,15 @@ mod tests {
|
||||
let mut vector_layer = VectorLayer::new("Layer 1");
|
||||
|
||||
let mut clip_instance = ClipInstance::new(clip_id);
|
||||
clip_instance.timeline_start = Beats(1.0); // Start at beat 1
|
||||
clip_instance.timeline_start = 1.0; // Start at 1 second
|
||||
let instance_id = clip_instance.id;
|
||||
vector_layer.clip_instances.push(clip_instance);
|
||||
|
||||
let layer_id = document.root.add_child(AnyLayer::Vector(vector_layer));
|
||||
|
||||
// Create move action: move from beat 1 to beat 5
|
||||
// Create move action: move from 1.0 to 5.0 seconds
|
||||
let mut layer_moves = HashMap::new();
|
||||
layer_moves.insert(layer_id, vec![(instance_id, Beats(1.0), Beats(5.0))]);
|
||||
layer_moves.insert(layer_id, vec![(instance_id, 1.0, 5.0)]);
|
||||
|
||||
let mut action = MoveClipInstancesAction::new(layer_moves);
|
||||
|
||||
@@ -399,7 +391,7 @@ mod tests {
|
||||
.iter()
|
||||
.find(|ci| ci.id == instance_id)
|
||||
.unwrap();
|
||||
assert_eq!(instance.timeline_start, Beats(5.0));
|
||||
assert_eq!(instance.timeline_start, 5.0);
|
||||
}
|
||||
|
||||
// Rollback
|
||||
@@ -412,7 +404,7 @@ mod tests {
|
||||
.iter()
|
||||
.find(|ci| ci.id == instance_id)
|
||||
.unwrap();
|
||||
assert_eq!(instance.timeline_start, Beats(1.0));
|
||||
assert_eq!(instance.timeline_start, 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,6 @@ impl Action for RemoveClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
AnyLayer::Group(_) => continue,
|
||||
AnyLayer::Raster(_) => continue,
|
||||
AnyLayer::Text(_) => continue,
|
||||
};
|
||||
|
||||
// Find and remove the instance, saving it for rollback
|
||||
@@ -73,7 +72,6 @@ impl Action for RemoveClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
AnyLayer::Group(_) => continue,
|
||||
AnyLayer::Raster(_) => continue,
|
||||
AnyLayer::Text(_) => continue,
|
||||
};
|
||||
|
||||
clip_instances.push(instance);
|
||||
@@ -138,23 +136,85 @@ impl Action for RemoveClipInstancesAction {
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
if backend.audio_controller.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
use crate::clip::AudioClipType;
|
||||
|
||||
// Re-add the clips that were removed. `BackendContext::add_clip_instance` is the same
|
||||
// helper the add and split actions use, so the trim/duration conversions (and take-folder
|
||||
// resolution) stay in exactly one place instead of being copied into every action that has
|
||||
// to put a clip back.
|
||||
let saved = std::mem::take(&mut self.saved);
|
||||
for (layer_id, instance) in &saved {
|
||||
if !matches!(document.get_layer(layer_id), Some(AnyLayer::Audio(_))) {
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
Some(c) => c,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
// Re-add clips that were removed from backend
|
||||
for (layer_id, instance) in &self.saved {
|
||||
let layer = match document.get_layer(layer_id) {
|
||||
Some(l) => l,
|
||||
None => continue,
|
||||
};
|
||||
if !matches!(layer, AnyLayer::Audio(_)) {
|
||||
continue;
|
||||
}
|
||||
// A missing track/clip just means there's nothing to restore on the backend.
|
||||
let _ = backend.add_clip_instance(document, layer_id, instance);
|
||||
|
||||
let track_id = match backend.layer_to_track_map.get(layer_id) {
|
||||
Some(id) => *id,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let clip = match document.get_audio_clip(&instance.clip_id) {
|
||||
Some(c) => c,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
let internal_start = instance.trim_start;
|
||||
let internal_end = instance.trim_end.unwrap_or(clip.duration);
|
||||
let external_start = instance.timeline_start;
|
||||
let external_duration = instance
|
||||
.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
|
||||
let midi_instance = daw_backend::MidiClipInstance::new(
|
||||
0,
|
||||
*midi_clip_id,
|
||||
daw_backend::Beats(internal_start),
|
||||
daw_backend::Beats(internal_end),
|
||||
daw_backend::Beats(external_start),
|
||||
daw_backend::Beats(external_duration),
|
||||
);
|
||||
|
||||
let query = Query::AddMidiClipInstanceSync(track_id, midi_instance);
|
||||
if let Ok(QueryResponse::MidiClipInstanceAdded(Ok(new_id))) =
|
||||
controller.send_query(query)
|
||||
{
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
instance.id,
|
||||
BackendClipInstanceId::Midi(new_id),
|
||||
);
|
||||
}
|
||||
}
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
let internal_start = instance.trim_start;
|
||||
let internal_end = instance.trim_end.unwrap_or(clip.duration);
|
||||
let effective_duration = instance.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
let start_time = instance.timeline_start;
|
||||
|
||||
let new_id = controller.add_audio_clip(
|
||||
track_id,
|
||||
*audio_pool_index,
|
||||
start_time,
|
||||
effective_duration,
|
||||
internal_start,
|
||||
);
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
instance.id,
|
||||
BackendClipInstanceId::Audio(new_id),
|
||||
);
|
||||
}
|
||||
AudioClipType::Recording => {}
|
||||
}
|
||||
}
|
||||
self.saved = saved;
|
||||
|
||||
// Clear saved backend IDs
|
||||
self.saved_backend_ids.clear();
|
||||
@@ -176,11 +236,11 @@ mod tests {
|
||||
let mut vector_layer = VectorLayer::new("Layer 1");
|
||||
|
||||
let mut ci1 = ClipInstance::new(clip_id);
|
||||
ci1.timeline_start = daw_backend::Beats::ZERO;
|
||||
ci1.timeline_start = 0.0;
|
||||
let id1 = ci1.id;
|
||||
|
||||
let mut ci2 = ClipInstance::new(clip_id);
|
||||
ci2.timeline_start = daw_backend::Beats(5.0);
|
||||
ci2.timeline_start = 5.0;
|
||||
let id2 = ci2.id;
|
||||
|
||||
vector_layer.clip_instances.push(ci1);
|
||||
|
||||
@@ -134,7 +134,7 @@ mod tests {
|
||||
let (mut document, layer_id, def) = create_test_setup();
|
||||
|
||||
// Add an effect first
|
||||
let instance = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance = def.create_instance(0.0, 10.0);
|
||||
let instance_id = instance.id;
|
||||
|
||||
if let Some(AnyLayer::Effect(el)) = document.get_layer_mut(&layer_id) {
|
||||
@@ -161,7 +161,7 @@ mod tests {
|
||||
let (mut document, layer_id, def) = create_test_setup();
|
||||
|
||||
// Add an effect first
|
||||
let instance = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance = def.create_instance(0.0, 10.0);
|
||||
let instance_id = instance.id;
|
||||
|
||||
if let Some(AnyLayer::Effect(el)) = document.get_layer_mut(&layer_id) {
|
||||
@@ -185,11 +185,11 @@ mod tests {
|
||||
let (mut document, layer_id, def) = create_test_setup();
|
||||
|
||||
// Add three effects
|
||||
let instance1 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance1 = def.create_instance(0.0, 10.0);
|
||||
let id1 = instance1.id;
|
||||
let instance2 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance2 = def.create_instance(0.0, 10.0);
|
||||
let id2 = instance2.id;
|
||||
let instance3 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let instance3 = def.create_instance(0.0, 10.0);
|
||||
let id3 = instance3.id;
|
||||
|
||||
if let Some(AnyLayer::Effect(el)) = document.get_layer_mut(&layer_id) {
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
//! Reorder clip instances within a layer's stacking order (Send to Back / Bring to Front).
|
||||
//!
|
||||
//! A layer's `clip_instances` Vec order *is* the stacking order — the last element renders on top
|
||||
//! (hit-testing walks it in reverse). Geometry renders underneath and is unaffected. This action
|
||||
//! moves the selected instances to the front (end) or back (start) of that Vec.
|
||||
|
||||
use crate::action::Action;
|
||||
use crate::clip::ClipInstance;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct ReorderClipInstancesAction {
|
||||
layer_id: Uuid,
|
||||
instance_ids: Vec<Uuid>,
|
||||
/// `true` = bring to front (top), `false` = send to back (bottom).
|
||||
to_front: bool,
|
||||
/// Full instance order captured on `execute`, for `rollback`.
|
||||
old_order: Option<Vec<Uuid>>,
|
||||
}
|
||||
|
||||
impl ReorderClipInstancesAction {
|
||||
pub fn new(layer_id: Uuid, instance_ids: Vec<Uuid>, to_front: bool) -> Self {
|
||||
Self { layer_id, instance_ids, to_front, old_order: None }
|
||||
}
|
||||
}
|
||||
|
||||
/// The clip-instance stack for a layer, if it has one (Group/Raster/Text don't).
|
||||
fn clip_instances_mut<'a>(document: &'a mut Document, layer_id: &Uuid) -> Option<&'a mut Vec<ClipInstance>> {
|
||||
match document.get_layer_mut(layer_id)? {
|
||||
AnyLayer::Vector(l) => Some(&mut l.clip_instances),
|
||||
AnyLayer::Audio(l) => Some(&mut l.clip_instances),
|
||||
AnyLayer::Video(l) => Some(&mut l.clip_instances),
|
||||
AnyLayer::Effect(l) => Some(&mut l.clip_instances),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for ReorderClipInstancesAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let instances = clip_instances_mut(document, &self.layer_id)
|
||||
.ok_or_else(|| "Layer has no clip-instance stack".to_string())?;
|
||||
self.old_order = Some(instances.iter().map(|c| c.id).collect());
|
||||
|
||||
let mut selected = Vec::new();
|
||||
let mut rest = Vec::new();
|
||||
for ci in instances.drain(..) {
|
||||
if self.instance_ids.contains(&ci.id) {
|
||||
selected.push(ci);
|
||||
} else {
|
||||
rest.push(ci);
|
||||
}
|
||||
}
|
||||
if self.to_front {
|
||||
rest.extend(selected); // selected last → rendered on top
|
||||
*instances = rest;
|
||||
} else {
|
||||
selected.extend(rest); // selected first → rendered at the bottom (of the stack)
|
||||
*instances = selected;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let Some(order) = self.old_order.clone() else {
|
||||
return Ok(());
|
||||
};
|
||||
let instances = clip_instances_mut(document, &self.layer_id)
|
||||
.ok_or_else(|| "Layer has no clip-instance stack".to_string())?;
|
||||
let rank = |id: &Uuid| order.iter().position(|o| o == id).unwrap_or(usize::MAX);
|
||||
instances.sort_by(|a, b| rank(&a.id).cmp(&rank(&b.id)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
if self.to_front { "Bring to Front".to_string() } else { "Send to Back".to_string() }
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
//! Resize-text-box action
|
||||
//!
|
||||
//! Updates a text layer's box origin and dimensions (which changes the wrap
|
||||
//! width, so parley re-wraps the text). One undoable step.
|
||||
|
||||
use crate::action::Action;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use kurbo::Point;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct ResizeTextBoxAction {
|
||||
layer_id: Uuid,
|
||||
new_origin: Point,
|
||||
new_width: f64,
|
||||
new_height: f64,
|
||||
old: Option<(Point, f64, f64)>,
|
||||
}
|
||||
|
||||
impl ResizeTextBoxAction {
|
||||
pub fn new(layer_id: Uuid, new_origin: Point, new_width: f64, new_height: f64) -> Self {
|
||||
Self { layer_id, new_origin, new_width, new_height, old: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for ResizeTextBoxAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
let AnyLayer::Text(text_layer) = layer else {
|
||||
return Err("ResizeTextBoxAction target is not a text layer".to_string());
|
||||
};
|
||||
if self.old.is_none() {
|
||||
self.old = Some((text_layer.box_origin, text_layer.box_width, text_layer.box_height));
|
||||
}
|
||||
text_layer.box_origin = self.new_origin;
|
||||
text_layer.box_width = self.new_width.max(1.0);
|
||||
text_layer.box_height = self.new_height.max(1.0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let Some((origin, w, h)) = self.old else { return Ok(()) };
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
if let AnyLayer::Text(text_layer) = layer {
|
||||
text_layer.box_origin = origin;
|
||||
text_layer.box_width = w;
|
||||
text_layer.box_height = h;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
"Resize text box".to_string()
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
//! Choose which take of a take-folder clip an instance plays.
|
||||
//!
|
||||
//! The take list lives on the *clip* but the selection lives on the *instance*, so two instances of
|
||||
//! the same folder can play different takes. Splitting a take-folder instance clones it, which is
|
||||
//! what makes comping work: set take 1 on the left half and take 3 on the right, and you've comped.
|
||||
//!
|
||||
//! There's no in-place pool-swap command in the backend, so switching a take means removing the
|
||||
//! instance's backend clip and re-adding it against the new take's audio/MIDI resource. Both halves
|
||||
//! of that go through `BackendContext`, which is also what `AddClipInstanceAction` uses.
|
||||
|
||||
use crate::action::{Action, BackendClipInstanceId, BackendContext};
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Action that points a clip instance at a different take of its take folder.
|
||||
#[derive(Clone)]
|
||||
pub struct SetActiveTakeAction {
|
||||
layer_id: Uuid,
|
||||
instance_id: Uuid,
|
||||
new_take: Option<usize>,
|
||||
old_take: Option<usize>,
|
||||
/// The backend track/clip the instance was on before we swapped, so rollback can undo it.
|
||||
backend_track_id: Option<daw_backend::TrackId>,
|
||||
}
|
||||
|
||||
impl SetActiveTakeAction {
|
||||
pub fn new(layer_id: Uuid, instance_id: Uuid, new_take: usize, old_take: Option<usize>) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
instance_id,
|
||||
new_take: Some(new_take),
|
||||
old_take,
|
||||
backend_track_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Point the instance at `take`, mutating the document. Shared by execute and rollback.
|
||||
fn apply(&self, document: &mut Document, take: Option<usize>) -> Result<(), String> {
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
let AnyLayer::Audio(audio_layer) = layer else {
|
||||
return Err("Take folders only exist on audio layers".to_string());
|
||||
};
|
||||
let instance = audio_layer
|
||||
.clip_instances
|
||||
.iter_mut()
|
||||
.find(|ci| ci.id == self.instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not found", self.instance_id))?;
|
||||
instance.active_take = take;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Swap the instance's backend clip to whatever take the document now says is active.
|
||||
///
|
||||
/// Called after the document has already been mutated, so re-adding just re-resolves the
|
||||
/// instance — `BackendContext::add_clip_instance` reads `active_take` itself.
|
||||
fn resync(&mut self, backend: &mut BackendContext, document: &Document) -> Result<(), String> {
|
||||
let instance = document
|
||||
.get_layer(&self.layer_id)
|
||||
.and_then(|l| match l {
|
||||
AnyLayer::Audio(al) => al.clip_instances.iter().find(|ci| ci.id == self.instance_id),
|
||||
_ => None,
|
||||
})
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("Clip instance {} not found", self.instance_id))?;
|
||||
|
||||
// Drop the old backend clip first. Its track comes from the map we're about to overwrite,
|
||||
// so read it before add_clip_instance replaces the entry.
|
||||
let existing: Option<BackendClipInstanceId> = backend
|
||||
.clip_instance_to_backend_map
|
||||
.get(&self.instance_id)
|
||||
.copied();
|
||||
let track_id = backend.layer_to_track_map.get(&self.layer_id).copied();
|
||||
if let (Some(backend_id), Some(track_id)) = (existing, track_id) {
|
||||
backend.remove_clip_instance(track_id, backend_id, self.instance_id);
|
||||
}
|
||||
|
||||
let added = backend.add_clip_instance(document, &self.layer_id, &instance)?;
|
||||
self.backend_track_id = added.map(|(track_id, _)| track_id);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for SetActiveTakeAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
self.apply(document, self.new_take)
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
self.apply(document, self.old_take)
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
match self.new_take {
|
||||
Some(i) => format!("Select take {}", i + 1),
|
||||
None => "Select take".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
self.resync(backend, document)
|
||||
}
|
||||
|
||||
fn rollback_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
self.resync(backend, document)
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
//! Set the transport cycle (loop) region.
|
||||
//!
|
||||
//! The cycle region is document state (it's saved in the `.beam`), so changing it goes through the
|
||||
//! action system like any other edit: it's undoable and it marks the document modified.
|
||||
//!
|
||||
//! The region is stored in **beats** so it stays put musically across tempo changes. Callers commit
|
||||
//! one action per gesture (e.g. on drag release, or a toggle click) rather than one per frame —
|
||||
//! the timeline previews the drag from its own local state, exactly like a clip drag does.
|
||||
|
||||
use crate::action::{Action, BackendContext};
|
||||
use crate::document::Document;
|
||||
use daw_backend::Beats;
|
||||
|
||||
/// Action that sets the cycle region and/or whether the transport loops over it.
|
||||
#[derive(Clone)]
|
||||
pub struct SetCycleRegionAction {
|
||||
old_region: Option<(Beats, Beats)>,
|
||||
old_enabled: bool,
|
||||
new_region: Option<(Beats, Beats)>,
|
||||
new_enabled: bool,
|
||||
}
|
||||
|
||||
impl SetCycleRegionAction {
|
||||
/// Build from the document's current state and the desired new region/enabled flag.
|
||||
pub fn new(
|
||||
document: &Document,
|
||||
new_region: Option<(Beats, Beats)>,
|
||||
new_enabled: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
old_region: document.cycle_region,
|
||||
old_enabled: document.cycle_enabled,
|
||||
new_region,
|
||||
new_enabled,
|
||||
}
|
||||
}
|
||||
|
||||
/// Toggle looping on/off, leaving the region itself alone.
|
||||
pub fn toggle_enabled(document: &Document) -> Self {
|
||||
Self::new(document, document.cycle_region, !document.cycle_enabled)
|
||||
}
|
||||
|
||||
/// True if this action would not actually change anything (lets callers skip a no-op undo entry).
|
||||
pub fn is_noop(&self) -> bool {
|
||||
self.old_region == self.new_region && self.old_enabled == self.new_enabled
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for SetCycleRegionAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
document.cycle_region = self.new_region;
|
||||
document.cycle_enabled = self.new_enabled;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
document.cycle_region = self.old_region;
|
||||
document.cycle_enabled = self.old_enabled;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
"Set cycle region".to_string()
|
||||
}
|
||||
|
||||
fn execute_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
_document: &Document,
|
||||
) -> Result<(), String> {
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
Some(c) => c,
|
||||
None => return Ok(()),
|
||||
};
|
||||
controller.set_loop_region(self.new_region);
|
||||
controller.set_loop_enabled(self.new_enabled);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
_document: &Document,
|
||||
) -> Result<(), String> {
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
Some(c) => c,
|
||||
None => return Ok(()),
|
||||
};
|
||||
controller.set_loop_region(self.old_region);
|
||||
controller.set_loop_enabled(self.old_enabled);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -81,11 +81,9 @@ impl Action for SetKeyframeAction {
|
||||
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&self.layer_id) {
|
||||
for clip_id in &self.clip_instance_ids {
|
||||
if let Some(ci) = vl.clip_instances.iter().find(|c| c.id == *clip_id) {
|
||||
// `start` is a keyframe time in seconds; group_visibility_start returns seconds,
|
||||
// so the fallback must convert the clip's beats start to seconds too.
|
||||
let start = vl
|
||||
.group_visibility_start(clip_id, self.time)
|
||||
.unwrap_or_else(|| document.tempo_map().beats_to_seconds(ci.timeline_start).seconds_to_f64());
|
||||
.unwrap_or(ci.timeline_start);
|
||||
clip_info.insert(*clip_id, (ci.transform.clone(), ci.opacity, start));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
//! Set-text-content action
|
||||
//!
|
||||
//! Replaces a text layer's [`TextContent`] (text string, font size, color, family,
|
||||
//! alignment) as one undoable step. Used both by in-place editing (text changes)
|
||||
//! and the info panel (style changes).
|
||||
|
||||
use crate::action::Action;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::text_layer::TextContent;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct SetTextContentAction {
|
||||
layer_id: Uuid,
|
||||
new: TextContent,
|
||||
old: Option<TextContent>,
|
||||
}
|
||||
|
||||
impl SetTextContentAction {
|
||||
pub fn new(layer_id: Uuid, new: TextContent) -> Self {
|
||||
Self { layer_id, new, old: None }
|
||||
}
|
||||
|
||||
/// Construct with an explicit `old` value. Used by in-place editing, which mutates
|
||||
/// the document live for preview and then records one undoable step capturing the
|
||||
/// content as it was *before* editing began.
|
||||
pub fn with_old(layer_id: Uuid, old: TextContent, new: TextContent) -> Self {
|
||||
Self { layer_id, new, old: Some(old) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for SetTextContentAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
let AnyLayer::Text(text_layer) = layer else {
|
||||
return Err("SetTextContentAction target is not a text layer".to_string());
|
||||
};
|
||||
if self.old.is_none() {
|
||||
self.old = Some(text_layer.content.clone());
|
||||
}
|
||||
text_layer.content = self.new.clone();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let Some(old) = self.old.clone() else { return Ok(()) };
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
if let AnyLayer::Text(text_layer) = layer {
|
||||
text_layer.content = old;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
"Edit text".to_string()
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ use crate::action::{Action, BackendContext};
|
||||
use crate::clip::ClipInstance;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use daw_backend::ContentTime;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Action that splits a clip instance at a specific timeline position
|
||||
@@ -18,17 +17,17 @@ pub struct SplitClipInstanceAction {
|
||||
/// The clip instance to split
|
||||
instance_id: Uuid,
|
||||
|
||||
/// Timeline position where to split (in beats)
|
||||
split_time: daw_backend::Beats,
|
||||
/// Timeline time where to split (in seconds)
|
||||
split_time: f64,
|
||||
|
||||
/// Whether the action has been executed (for rollback)
|
||||
executed: bool,
|
||||
|
||||
// Stored during execute for rollback
|
||||
/// Original trim_end value of the left (original) instance
|
||||
original_trim_end: Option<ContentTime>,
|
||||
/// Original timeline_duration value of the left (original) instance (beats)
|
||||
original_timeline_duration: Option<daw_backend::Beats>,
|
||||
original_trim_end: Option<f64>,
|
||||
/// Original timeline_duration value of the left (original) instance
|
||||
original_timeline_duration: Option<f64>,
|
||||
/// ID of the new (right) instance created by the split
|
||||
new_instance_id: Option<Uuid>,
|
||||
|
||||
@@ -48,8 +47,8 @@ impl SplitClipInstanceAction {
|
||||
///
|
||||
/// * `layer_id` - The ID of the layer containing the clip instance
|
||||
/// * `instance_id` - The ID of the clip instance to split
|
||||
/// * `split_time` - The timeline position (in beats) where to split
|
||||
pub fn new(layer_id: Uuid, instance_id: Uuid, split_time: daw_backend::Beats) -> Self {
|
||||
/// * `split_time` - The timeline time (in seconds) where to split
|
||||
pub fn new(layer_id: Uuid, instance_id: Uuid, split_time: f64) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
instance_id,
|
||||
@@ -73,9 +72,9 @@ impl SplitClipInstanceAction {
|
||||
///
|
||||
/// * `layer_id` - The ID of the layer containing the clip instance
|
||||
/// * `instance_id` - The ID of the clip instance to split
|
||||
/// * `split_time` - The timeline position (in beats) where to split
|
||||
/// * `split_time` - The timeline time (in seconds) where to split
|
||||
/// * `new_instance_id` - The UUID to use for the new (right) clip instance
|
||||
pub fn with_new_instance_id(layer_id: Uuid, instance_id: Uuid, split_time: daw_backend::Beats, new_instance_id: Uuid) -> Self {
|
||||
pub fn with_new_instance_id(layer_id: Uuid, instance_id: Uuid, split_time: f64, new_instance_id: Uuid) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
instance_id,
|
||||
@@ -115,7 +114,6 @@ impl Action for SplitClipInstanceAction {
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => return Err("Cannot split clip instances on group layers".to_string()),
|
||||
AnyLayer::Raster(_) => return Err("Cannot split clip instances on group layers".to_string()),
|
||||
AnyLayer::Text(_) => return Err("Cannot split clip instances on group layers".to_string()),
|
||||
};
|
||||
|
||||
let instance = clip_instances
|
||||
@@ -123,21 +121,19 @@ impl Action for SplitClipInstanceAction {
|
||||
.find(|ci| ci.id == self.instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not found", self.instance_id))?;
|
||||
|
||||
// The clip's content duration in its OWN domain — seconds for audio/video/vector, beats for
|
||||
// MIDI. All the content math below is trim-domain, so it has to be done in whichever domain
|
||||
// this clip uses; a seconds duration would silently be added to a MIDI clip's beats trim.
|
||||
let trim_duration = document
|
||||
.clip_trim_duration(&instance.clip_id)
|
||||
// Get the clip's duration
|
||||
let clip_duration = document
|
||||
.get_clip_duration(&instance.clip_id)
|
||||
.ok_or_else(|| format!("Clip {} not found", instance.clip_id))?;
|
||||
|
||||
// Calculate the effective duration and timeline end (both in beats)
|
||||
let effective_duration = instance.effective_duration(trim_duration, document.tempo_map());
|
||||
let effective_duration = instance.effective_duration(clip_duration, document.tempo_map());
|
||||
let timeline_end = instance.timeline_start + effective_duration;
|
||||
|
||||
// Validate: split_time must be strictly within the clip's timeline span
|
||||
let epsilon = daw_backend::Beats(0.001); // ~1ms tolerance
|
||||
if self.split_time <= instance.timeline_start + epsilon
|
||||
|| self.split_time >= timeline_end - epsilon
|
||||
const EPSILON: f64 = 0.001; // 1ms tolerance
|
||||
if self.split_time <= instance.timeline_start + EPSILON
|
||||
|| self.split_time >= timeline_end - EPSILON
|
||||
{
|
||||
return Err(format!(
|
||||
"Split time {} must be within clip bounds ({} to {})",
|
||||
@@ -149,30 +145,21 @@ impl Action for SplitClipInstanceAction {
|
||||
self.original_trim_end = instance.trim_end;
|
||||
self.original_timeline_duration = instance.timeline_duration;
|
||||
|
||||
// Check if this is a looping clip
|
||||
let is_looping = instance.timeline_duration.is_some();
|
||||
let content_duration = ContentTime(instance.content_window(trim_duration).native());
|
||||
let content_duration = instance.trim_end.unwrap_or(clip_duration) - instance.trim_start;
|
||||
|
||||
// Timeline split point (beats).
|
||||
// Calculate the split point
|
||||
let time_into_clip = self.split_time - instance.timeline_start;
|
||||
let left_duration = time_into_clip;
|
||||
let right_duration = effective_duration - left_duration;
|
||||
|
||||
// How far the split lands into the clip's *content*, expressed in the content domain: beats
|
||||
// content takes the beats delta directly, wall-clock content takes the seconds delta.
|
||||
let tempo_map = document.tempo_map();
|
||||
let time_into_content = ContentTime(match trim_duration {
|
||||
crate::clip::ClipDuration::Beats(_) => time_into_clip.beats_to_f64(),
|
||||
crate::clip::ClipDuration::Seconds(_) => (tempo_map.beats_to_seconds(self.split_time)
|
||||
- tempo_map.beats_to_seconds(instance.timeline_start))
|
||||
.seconds_to_f64(),
|
||||
});
|
||||
|
||||
// Calculate the content split point (content domain).
|
||||
let content_split_time = if is_looping && content_duration > ContentTime::ZERO {
|
||||
// Calculate content split time
|
||||
let content_split_time = if is_looping {
|
||||
// For looping clips, wrap around content
|
||||
instance.trim_start + (time_into_content % content_duration)
|
||||
instance.trim_start + (time_into_clip % content_duration)
|
||||
} else {
|
||||
instance.trim_start + time_into_content
|
||||
instance.trim_start + time_into_clip
|
||||
};
|
||||
|
||||
// Clone the instance for the right side
|
||||
@@ -246,7 +233,7 @@ impl Action for SplitClipInstanceAction {
|
||||
AnyLayer::Group(_) => {
|
||||
return Err("Cannot split clip instances on group layers".to_string());
|
||||
}
|
||||
AnyLayer::Raster(_) | AnyLayer::Text(_) => {
|
||||
AnyLayer::Raster(_) => {
|
||||
return Err("Cannot split clip instances on group layers".to_string());
|
||||
}
|
||||
}
|
||||
@@ -307,8 +294,8 @@ impl Action for SplitClipInstanceAction {
|
||||
AnyLayer::Group(_) => {
|
||||
// Group layers don't have clip instances, nothing to rollback
|
||||
}
|
||||
AnyLayer::Raster(_) | AnyLayer::Text(_) => {
|
||||
// Raster/text layers don't have clip instances, nothing to rollback
|
||||
AnyLayer::Raster(_) => {
|
||||
// Raster layers don't have clip instances, nothing to rollback
|
||||
}
|
||||
}
|
||||
|
||||
@@ -363,63 +350,112 @@ impl Action for SplitClipInstanceAction {
|
||||
.get_audio_clip(&new_instance.clip_id)
|
||||
.ok_or_else(|| "Audio clip not found".to_string())?;
|
||||
|
||||
use crate::clip::ResolvedContent;
|
||||
if matches!(original_instance.resolve(clip), ResolvedContent::Recording) {
|
||||
return Err("Cannot split a clip that is currently recording".to_string());
|
||||
}
|
||||
|
||||
// A split is: shorten the left half's backend clip, then add the right half as a new one.
|
||||
//
|
||||
// 1. Trim the left (original) instance. `trim_range` tags the bounds with the clip's own
|
||||
// content domain, so a MIDI clip's beats trims can't be sent as seconds.
|
||||
let left_trim = clip.trim_range(
|
||||
original_instance.trim_start,
|
||||
original_instance
|
||||
.trim_end
|
||||
.unwrap_or(ContentTime(clip.content_duration().native())),
|
||||
);
|
||||
let new_instance = new_instance.clone();
|
||||
|
||||
let backend_track_id = *backend
|
||||
// Look up backend track ID from layer mapping
|
||||
let backend_track_id = backend
|
||||
.layer_to_track_map
|
||||
.get(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not mapped to backend track", self.layer_id))?;
|
||||
let left_backend_id = backend
|
||||
.clip_instance_to_backend_map
|
||||
.get(&self.instance_id)
|
||||
.copied();
|
||||
|
||||
{
|
||||
let controller = backend
|
||||
.audio_controller
|
||||
.as_mut()
|
||||
.ok_or_else(|| "Audio controller not available".to_string())?;
|
||||
match left_backend_id {
|
||||
Some(crate::action::BackendClipInstanceId::Midi(id))
|
||||
| Some(crate::action::BackendClipInstanceId::Audio(id)) => {
|
||||
controller.trim_clip(backend_track_id, id, left_trim);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
// Get audio controller
|
||||
let controller = backend
|
||||
.audio_controller
|
||||
.as_mut()
|
||||
.ok_or_else(|| "Audio controller not available".to_string())?;
|
||||
|
||||
// 2. Add the right (new) instance via the shared helper — same one AddClipInstanceAction
|
||||
// uses, so the trim/duration conversions live in exactly one place.
|
||||
if let Some((track_id, backend_id)) =
|
||||
backend.add_clip_instance(document, &self.layer_id, &new_instance)?
|
||||
{
|
||||
self.backend_track_id = Some(track_id);
|
||||
match backend_id {
|
||||
crate::action::BackendClipInstanceId::Midi(id) => {
|
||||
self.backend_midi_instance_id = Some(id)
|
||||
// Handle different clip types
|
||||
use crate::clip::AudioClipType;
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
// 1. Trim the original (left) instance
|
||||
let orig_internal_start = original_instance.trim_start;
|
||||
let orig_internal_end = original_instance.trim_end.unwrap_or(clip.duration);
|
||||
|
||||
// Look up the original backend instance ID
|
||||
if let Some(crate::action::BackendClipInstanceId::Midi(orig_backend_id)) =
|
||||
backend.clip_instance_to_backend_map.get(&self.instance_id)
|
||||
{
|
||||
controller.trim_clip(*backend_track_id, *orig_backend_id, orig_internal_start, orig_internal_end);
|
||||
}
|
||||
crate::action::BackendClipInstanceId::Audio(id) => {
|
||||
self.backend_audio_instance_id = Some(id)
|
||||
|
||||
// 2. Add the new (right) instance
|
||||
let internal_start = new_instance.trim_start;
|
||||
let internal_end = new_instance.trim_end.unwrap_or(clip.duration);
|
||||
let external_start = new_instance.timeline_start;
|
||||
let external_duration = new_instance
|
||||
.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
|
||||
let instance = daw_backend::MidiClipInstance::new(
|
||||
0,
|
||||
*midi_clip_id,
|
||||
daw_backend::Beats(internal_start),
|
||||
daw_backend::Beats(internal_end),
|
||||
daw_backend::Beats(external_start),
|
||||
daw_backend::Beats(external_duration),
|
||||
);
|
||||
|
||||
let query = Query::AddMidiClipInstanceSync(*backend_track_id, instance);
|
||||
|
||||
match controller.send_query(query)? {
|
||||
QueryResponse::MidiClipInstanceAdded(Ok(instance_id)) => {
|
||||
self.backend_track_id = Some(*backend_track_id);
|
||||
self.backend_midi_instance_id = Some(instance_id);
|
||||
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
new_instance_id,
|
||||
crate::action::BackendClipInstanceId::Midi(instance_id),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
QueryResponse::MidiClipInstanceAdded(Err(e)) => Err(e),
|
||||
_ => Err("Unexpected query response".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
// 1. Trim the original (left) instance
|
||||
let orig_internal_start = original_instance.trim_start;
|
||||
let orig_internal_end = original_instance.trim_end.unwrap_or(clip.duration);
|
||||
|
||||
Ok(())
|
||||
// Look up the original backend instance ID
|
||||
if let Some(crate::action::BackendClipInstanceId::Audio(orig_backend_id)) =
|
||||
backend.clip_instance_to_backend_map.get(&self.instance_id)
|
||||
{
|
||||
controller.trim_clip(*backend_track_id, *orig_backend_id, orig_internal_start, orig_internal_end);
|
||||
}
|
||||
|
||||
// 2. Add the new (right) instance
|
||||
let internal_start = new_instance.trim_start;
|
||||
let internal_end = new_instance.trim_end.unwrap_or(clip.duration);
|
||||
let effective_duration = new_instance.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
let start_time = new_instance.timeline_start;
|
||||
|
||||
let instance_id = controller.add_audio_clip(
|
||||
*backend_track_id,
|
||||
*audio_pool_index,
|
||||
start_time,
|
||||
effective_duration,
|
||||
internal_start,
|
||||
);
|
||||
|
||||
self.backend_track_id = Some(*backend_track_id);
|
||||
self.backend_audio_instance_id = Some(instance_id);
|
||||
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
new_instance_id,
|
||||
crate::action::BackendClipInstanceId::Audio(instance_id),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips cannot be split
|
||||
Err("Cannot split a clip that is currently recording".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rollback_backend(
|
||||
@@ -449,28 +485,26 @@ impl Action for SplitClipInstanceAction {
|
||||
if let Some(instance) = al.clip_instances.iter().find(|ci| ci.id == self.instance_id) {
|
||||
if let Some(clip) = document.get_audio_clip(&instance.clip_id) {
|
||||
let orig_internal_start = instance.trim_start;
|
||||
let orig_internal_end = self
|
||||
.original_trim_end
|
||||
.unwrap_or(ContentTime(clip.content_duration().native()));
|
||||
let orig_internal_end = self.original_trim_end.unwrap_or(clip.duration);
|
||||
|
||||
// Restore based on clip type
|
||||
use crate::clip::ResolvedContent;
|
||||
match &instance.resolve(clip) {
|
||||
ResolvedContent::Midi { .. } => {
|
||||
use crate::clip::AudioClipType;
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { .. } => {
|
||||
if let Some(crate::action::BackendClipInstanceId::Midi(orig_backend_id)) =
|
||||
backend.clip_instance_to_backend_map.get(&self.instance_id)
|
||||
{
|
||||
controller.trim_clip(track_id, *orig_backend_id, clip.trim_range(orig_internal_start, orig_internal_end));
|
||||
controller.trim_clip(track_id, *orig_backend_id, orig_internal_start, orig_internal_end);
|
||||
}
|
||||
}
|
||||
ResolvedContent::Audio { .. } => {
|
||||
AudioClipType::Sampled { .. } => {
|
||||
if let Some(crate::action::BackendClipInstanceId::Audio(orig_backend_id)) =
|
||||
backend.clip_instance_to_backend_map.get(&self.instance_id)
|
||||
{
|
||||
controller.trim_clip(track_id, *orig_backend_id, clip.trim_range(orig_internal_start, orig_internal_end));
|
||||
controller.trim_clip(track_id, *orig_backend_id, orig_internal_start, orig_internal_end);
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips - nothing to rollback
|
||||
}
|
||||
}
|
||||
@@ -505,16 +539,16 @@ mod tests {
|
||||
|
||||
// Create a clip instance at timeline 0, with trim 0-10 (10 seconds)
|
||||
let mut clip_instance = ClipInstance::new(clip_id);
|
||||
clip_instance.timeline_start = daw_backend::Beats::ZERO;
|
||||
clip_instance.trim_start = ContentTime::ZERO;
|
||||
clip_instance.trim_end = Some(ContentTime(10.0));
|
||||
clip_instance.timeline_start = 0.0;
|
||||
clip_instance.trim_start = 0.0;
|
||||
clip_instance.trim_end = Some(10.0);
|
||||
let instance_id = clip_instance.id;
|
||||
vector_layer.clip_instances.push(clip_instance);
|
||||
|
||||
let layer_id = document.root.add_child(AnyLayer::Vector(vector_layer));
|
||||
|
||||
// Split at timeline 5.0
|
||||
let mut action = SplitClipInstanceAction::new(layer_id, instance_id, daw_backend::Beats(5.0));
|
||||
let mut action = SplitClipInstanceAction::new(layer_id, instance_id, 5.0);
|
||||
|
||||
// Execute - this will fail because we don't have a real clip in the document
|
||||
// In a real test, we'd need to add a VectorClip first
|
||||
@@ -524,45 +558,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_split_action_description() {
|
||||
let action = SplitClipInstanceAction::new(Uuid::new_v4(), Uuid::new_v4(), daw_backend::Beats(5.0));
|
||||
let action = SplitClipInstanceAction::new(Uuid::new_v4(), Uuid::new_v4(), 5.0);
|
||||
assert_eq!(action.description(), "Split clip instance");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splitting_a_midi_clip_stays_in_the_beats_domain() {
|
||||
// Regression: `trim_start`/`trim_end` are domain-polymorphic — SECONDS for audio/video/
|
||||
// vector, but BEATS for MIDI (the backend takes MIDI trims as `Beats`). Split used to map
|
||||
// the split point into the clip's content in seconds unconditionally, so on a MIDI clip it
|
||||
// added a seconds delta to a beats offset. At anything but 60 BPM the right half started at
|
||||
// the wrong place in the content.
|
||||
//
|
||||
// At 120 BPM, beat 4 is 2 SECONDS in. The right half must trim to beat 4, not "4 seconds"
|
||||
// (= beat 8) and not 2 (the seconds value).
|
||||
let mut document = Document::new("Test");
|
||||
document.set_bpm(120.0);
|
||||
|
||||
// 8-beat MIDI clip at the timeline origin.
|
||||
let clip = crate::clip::AudioClip::new_midi("Midi", 1, daw_backend::Beats(8.0));
|
||||
let clip_id = document.add_audio_clip(clip);
|
||||
|
||||
let mut audio_layer = crate::layer::AudioLayer::new("Layer 1");
|
||||
let mut instance = ClipInstance::new(clip_id);
|
||||
instance.timeline_start = daw_backend::Beats::ZERO;
|
||||
instance.trim_start = ContentTime::ZERO;
|
||||
instance.trim_end = Some(ContentTime(8.0)); // beats
|
||||
let instance_id = instance.id;
|
||||
audio_layer.clip_instances.push(instance);
|
||||
let layer_id = document.root.add_child(AnyLayer::Audio(audio_layer));
|
||||
|
||||
let mut action = SplitClipInstanceAction::new(layer_id, instance_id, daw_backend::Beats(4.0));
|
||||
action.execute(&mut document).expect("split");
|
||||
let new_id = action.new_instance_id().expect("right instance");
|
||||
|
||||
let AnyLayer::Audio(al) = document.get_layer(&layer_id).unwrap() else { panic!() };
|
||||
let right = al.clip_instances.iter().find(|ci| ci.id == new_id).unwrap();
|
||||
let left = al.clip_instances.iter().find(|ci| ci.id == instance_id).unwrap();
|
||||
|
||||
assert_eq!(right.trim_start, ContentTime(4.0), "right half must start 4 BEATS into the content");
|
||||
assert_eq!(left.trim_end, Some(ContentTime(4.0)), "left half must end 4 BEATS into the content");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +101,6 @@ impl Action for TransformClipInstancesAction {
|
||||
AnyLayer::Effect(_) => {}
|
||||
AnyLayer::Group(_) => {}
|
||||
AnyLayer::Raster(_) => {}
|
||||
AnyLayer::Text(_) => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -141,7 +140,6 @@ impl Action for TransformClipInstancesAction {
|
||||
AnyLayer::Effect(_) => {}
|
||||
AnyLayer::Group(_) => {}
|
||||
AnyLayer::Raster(_) => {}
|
||||
AnyLayer::Text(_) => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ use crate::action::Action;
|
||||
use crate::clip::ClipInstance;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use daw_backend::{Beats, ContentTime, Seconds};
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -32,57 +31,15 @@ pub struct TrimClipInstancesAction {
|
||||
pub struct TrimData {
|
||||
/// For TrimLeft: trim_start value
|
||||
/// For TrimRight: trim_end value (Option because it can be None)
|
||||
///
|
||||
/// A content time — measured in the clip's own domain (seconds for audio/video/vector, beats
|
||||
/// for MIDI), so it must be resolved against the clip before meeting a timeline position.
|
||||
pub trim_value: Option<ContentTime>,
|
||||
/// For TrimLeft: timeline_start value (where the clip appears on timeline, beats)
|
||||
pub trim_value: Option<f64>,
|
||||
/// For TrimLeft: timeline_start value (where the clip appears on timeline)
|
||||
/// For TrimRight: unused (None)
|
||||
pub timeline_start: Option<Beats>,
|
||||
}
|
||||
|
||||
/// A wall-clock gap on the timeline, expressed in a clip's content domain.
|
||||
///
|
||||
/// Trim validation clamps how far a clip may be dragged against the empty space next to it, and that
|
||||
/// space is measured on the timeline (seconds) while the trim lives in the clip's content domain. For
|
||||
/// wall-clock content they're the same number; for MIDI (beats content) the gap has to be converted
|
||||
/// at the clip's position, or a seconds gap silently clamps a beats trim.
|
||||
fn gap_to_content(
|
||||
gap: Seconds,
|
||||
clip_content: crate::clip::ClipDuration,
|
||||
timeline_start: Beats,
|
||||
tempo_map: &crate::tempo_map::TempoMap,
|
||||
) -> ContentTime {
|
||||
match clip_content {
|
||||
crate::clip::ClipDuration::Seconds(_) => ContentTime(gap.seconds_to_f64()),
|
||||
crate::clip::ClipDuration::Beats(_) => {
|
||||
let beats = tempo_map
|
||||
.seconds_to_beats(tempo_map.beats_to_seconds(timeline_start) + gap)
|
||||
- timeline_start;
|
||||
ContentTime(beats.beats_to_f64())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The inverse: a content-domain span as wall-clock seconds at the clip's position.
|
||||
fn content_to_secs(
|
||||
span: ContentTime,
|
||||
clip_content: crate::clip::ClipDuration,
|
||||
timeline_start: Beats,
|
||||
tempo_map: &crate::tempo_map::TempoMap,
|
||||
) -> Seconds {
|
||||
match clip_content {
|
||||
crate::clip::ClipDuration::Seconds(_) => Seconds(span.raw()),
|
||||
crate::clip::ClipDuration::Beats(_) => {
|
||||
tempo_map.beats_to_seconds(timeline_start + Beats(span.raw()))
|
||||
- tempo_map.beats_to_seconds(timeline_start)
|
||||
}
|
||||
}
|
||||
pub timeline_start: Option<f64>,
|
||||
}
|
||||
|
||||
impl TrimData {
|
||||
/// Create TrimData for left trim
|
||||
pub fn left(trim_start: ContentTime, timeline_start: Beats) -> Self {
|
||||
pub fn left(trim_start: f64, timeline_start: f64) -> Self {
|
||||
Self {
|
||||
trim_value: Some(trim_start),
|
||||
timeline_start: Some(timeline_start),
|
||||
@@ -90,7 +47,7 @@ impl TrimData {
|
||||
}
|
||||
|
||||
/// Create TrimData for right trim
|
||||
pub fn right(trim_end: Option<ContentTime>) -> Self {
|
||||
pub fn right(trim_end: Option<f64>) -> Self {
|
||||
Self {
|
||||
trim_value: trim_end,
|
||||
timeline_start: None,
|
||||
@@ -144,7 +101,6 @@ impl Action for TrimClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
if let Some(instance) = clip_instances.iter().find(|ci| ci.id == *member_instance_id) {
|
||||
@@ -182,7 +138,6 @@ impl Action for TrimClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
if let Some(instance) = clip_instances.iter().find(|ci| ci.id == *member_instance_id) {
|
||||
@@ -227,15 +182,13 @@ impl Action for TrimClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
let instance = clip_instances.iter()
|
||||
.find(|ci| &ci.id == instance_id)
|
||||
.ok_or_else(|| format!("Instance {} not found", instance_id))?;
|
||||
|
||||
// The clip's content duration in ITS OWN domain, so trims resolve correctly for MIDI.
|
||||
let clip_content = document.clip_trim_duration(&instance.clip_id)
|
||||
let clip_duration = document.get_clip_duration(&instance.clip_id)
|
||||
.ok_or_else(|| format!("Clip {} not found", instance.clip_id))?;
|
||||
|
||||
let mut clamped_new = new.clone();
|
||||
@@ -247,74 +200,51 @@ impl Action for TrimClipInstancesAction {
|
||||
{
|
||||
// If extending to the left (new_trim < old_trim)
|
||||
if should_validate && new_trim < old_trim {
|
||||
let tempo_map = document.tempo_map();
|
||||
|
||||
// Max leftward extension: the gap's wall-clock span, converted into
|
||||
// the clip's content domain so it can clamp a content-domain trim.
|
||||
let max_extend = gap_to_content(
|
||||
document.find_max_trim_extend_left(
|
||||
layer_id,
|
||||
instance_id,
|
||||
instance.timeline_start,
|
||||
),
|
||||
clip_content,
|
||||
// Find the maximum we can extend left
|
||||
let max_extend = document.find_max_trim_extend_left(
|
||||
layer_id,
|
||||
instance_id,
|
||||
instance.timeline_start,
|
||||
tempo_map,
|
||||
);
|
||||
|
||||
// Calculate how much we want to extend
|
||||
let desired_extend = old_trim - new_trim;
|
||||
|
||||
// Clamp to max allowed
|
||||
let actual_extend = desired_extend.min(max_extend);
|
||||
let clamped_trim_start = old_trim - actual_extend;
|
||||
|
||||
// Move the timeline left by the same span, as wall-clock seconds.
|
||||
let shift = content_to_secs(
|
||||
actual_extend,
|
||||
clip_content,
|
||||
instance.timeline_start,
|
||||
tempo_map,
|
||||
);
|
||||
let clamped_timeline_start = tempo_map
|
||||
.seconds_to_beats(tempo_map.beats_to_seconds(old_timeline) - shift)
|
||||
.max(Beats::ZERO);
|
||||
let clamped_timeline_start = (old_timeline - actual_extend).max(0.0);
|
||||
|
||||
clamped_new = TrimData::left(clamped_trim_start, clamped_timeline_start);
|
||||
}
|
||||
}
|
||||
}
|
||||
TrimType::TrimRight => {
|
||||
let content_end = ContentTime(clip_content.native());
|
||||
let old_trim_end = old.trim_value.unwrap_or(content_end);
|
||||
let new_trim_end = new.trim_value.unwrap_or(content_end);
|
||||
let old_trim_end = old.trim_value.unwrap_or(clip_duration);
|
||||
let new_trim_end = new.trim_value.unwrap_or(clip_duration);
|
||||
|
||||
// If extending to the right (new_trim_end > old_trim_end)
|
||||
if should_validate && new_trim_end > old_trim_end {
|
||||
let tempo_map = document.tempo_map();
|
||||
// Calculate current effective duration
|
||||
let current_effective_duration = old_trim_end - instance.trim_start;
|
||||
|
||||
// How long the clip currently occupies the timeline, in beats. Resolved
|
||||
// in the clip's own domain, so a MIDI clip's beats content isn't run
|
||||
// through the seconds→beats conversion a second time.
|
||||
let current_effective_duration = instance
|
||||
.effective_duration_beats(clip_content, tempo_map);
|
||||
|
||||
// Max rightward extension: the gap's wall-clock span, in content domain.
|
||||
let max_extend = gap_to_content(
|
||||
document.find_max_trim_extend_right(
|
||||
layer_id,
|
||||
instance_id,
|
||||
instance.timeline_start,
|
||||
current_effective_duration,
|
||||
),
|
||||
clip_content,
|
||||
// Find the maximum we can extend right
|
||||
let max_extend = document.find_max_trim_extend_right(
|
||||
layer_id,
|
||||
instance_id,
|
||||
instance.timeline_start,
|
||||
tempo_map,
|
||||
current_effective_duration,
|
||||
);
|
||||
|
||||
// Calculate how much we want to extend
|
||||
let desired_extend = new_trim_end - old_trim_end;
|
||||
|
||||
// Clamp to max allowed
|
||||
let actual_extend = desired_extend.min(max_extend);
|
||||
let clamped_trim_end = old_trim_end + actual_extend;
|
||||
|
||||
// Don't exceed the clip's content.
|
||||
let final_trim_end = clamped_trim_end.min(content_end);
|
||||
// Don't exceed clip duration
|
||||
let final_trim_end = clamped_trim_end.min(clip_duration);
|
||||
|
||||
clamped_new = TrimData::right(Some(final_trim_end));
|
||||
}
|
||||
@@ -345,7 +275,6 @@ impl Action for TrimClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
AnyLayer::Group(_) => continue,
|
||||
AnyLayer::Raster(_) => continue,
|
||||
AnyLayer::Text(_) => continue,
|
||||
};
|
||||
|
||||
// Apply trims
|
||||
@@ -386,7 +315,6 @@ impl Action for TrimClipInstancesAction {
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
AnyLayer::Group(_) => continue,
|
||||
AnyLayer::Raster(_) => continue,
|
||||
AnyLayer::Text(_) => continue,
|
||||
};
|
||||
|
||||
// Restore original trim values
|
||||
@@ -423,7 +351,7 @@ impl Action for TrimClipInstancesAction {
|
||||
|
||||
fn execute_backend(&mut self, backend: &mut crate::action::BackendContext, document: &Document) -> Result<(), String> {
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::clip::ResolvedContent;
|
||||
use crate::clip::AudioClipType;
|
||||
|
||||
// Get audio controller
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
@@ -443,10 +371,9 @@ impl Action for TrimClipInstancesAction {
|
||||
if let Some(instance) = vl.clip_instances.iter().find(|ci| ci.id == *instance_id) {
|
||||
if let Some(&metatrack_id) = backend.layer_to_track_map.get(&instance.clip_id) {
|
||||
// Instance already has new values after execute()
|
||||
controller.set_offset(metatrack_id, document.tempo_map().beats_to_seconds(instance.timeline_start));
|
||||
// A vector clip's content is wall-clock, so its content times ARE seconds.
|
||||
controller.set_trim_start(metatrack_id, daw_backend::Seconds(instance.trim_start.raw()));
|
||||
controller.set_trim_end(metatrack_id, instance.trim_end.map(|t| daw_backend::Seconds(t.raw())));
|
||||
controller.set_offset(metatrack_id, instance.timeline_start);
|
||||
controller.set_trim_start(metatrack_id, instance.trim_start);
|
||||
controller.set_trim_end(metatrack_id, instance.trim_end);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -482,29 +409,27 @@ impl Action for TrimClipInstancesAction {
|
||||
// Calculate new internal_start and internal_end for backend
|
||||
// Note: instance already has the new trim values after execute()
|
||||
let internal_start = instance.trim_start;
|
||||
let internal_end = instance
|
||||
.trim_end
|
||||
.unwrap_or(ContentTime(clip.content_duration().native()));
|
||||
let internal_end = instance.trim_end.unwrap_or(clip.duration);
|
||||
|
||||
// Handle trim based on clip type
|
||||
match &instance.resolve(clip) {
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
// For MIDI: trim_clip expects the pool clip ID
|
||||
controller.trim_clip(*track_id, *midi_clip_id, clip.trim_range(internal_start, internal_end));
|
||||
controller.trim_clip(*track_id, *midi_clip_id, internal_start, internal_end);
|
||||
}
|
||||
ResolvedContent::Audio { .. } => {
|
||||
AudioClipType::Sampled { .. } => {
|
||||
// For sampled audio: trim_clip expects the instance ID
|
||||
let backend_instance_id = backend.clip_instance_to_backend_map.get(instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not mapped to backend", instance_id))?;
|
||||
|
||||
match backend_instance_id {
|
||||
crate::action::BackendClipInstanceId::Audio(audio_id) => {
|
||||
controller.trim_clip(*track_id, *audio_id, clip.trim_range(internal_start, internal_end));
|
||||
controller.trim_clip(*track_id, *audio_id, internal_start, internal_end);
|
||||
}
|
||||
_ => return Err("Expected audio instance ID for sampled clip".to_string()),
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips cannot be trimmed - skip
|
||||
}
|
||||
}
|
||||
@@ -516,7 +441,7 @@ impl Action for TrimClipInstancesAction {
|
||||
|
||||
fn rollback_backend(&mut self, backend: &mut crate::action::BackendContext, document: &Document) -> Result<(), String> {
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::clip::ResolvedContent;
|
||||
use crate::clip::AudioClipType;
|
||||
|
||||
// Get audio controller
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
@@ -536,10 +461,9 @@ impl Action for TrimClipInstancesAction {
|
||||
if let Some(instance) = vl.clip_instances.iter().find(|ci| ci.id == *instance_id) {
|
||||
if let Some(&metatrack_id) = backend.layer_to_track_map.get(&instance.clip_id) {
|
||||
// Instance already has old values after rollback()
|
||||
controller.set_offset(metatrack_id, document.tempo_map().beats_to_seconds(instance.timeline_start));
|
||||
// A vector clip's content is wall-clock, so its content times ARE seconds.
|
||||
controller.set_trim_start(metatrack_id, daw_backend::Seconds(instance.trim_start.raw()));
|
||||
controller.set_trim_end(metatrack_id, instance.trim_end.map(|t| daw_backend::Seconds(t.raw())));
|
||||
controller.set_offset(metatrack_id, instance.timeline_start);
|
||||
controller.set_trim_start(metatrack_id, instance.trim_start);
|
||||
controller.set_trim_end(metatrack_id, instance.trim_end);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -573,35 +497,34 @@ impl Action for TrimClipInstancesAction {
|
||||
.ok_or_else(|| format!("Audio clip {} not found", instance.clip_id))?;
|
||||
|
||||
// Calculate old internal_start and internal_end for backend
|
||||
let content_end = ContentTime(clip.content_duration().native());
|
||||
let internal_start = match trim_type {
|
||||
TrimType::TrimLeft => old.trim_value.unwrap_or(ContentTime::ZERO),
|
||||
TrimType::TrimLeft => old.trim_value.unwrap_or(0.0),
|
||||
TrimType::TrimRight => instance.trim_start, // trim_start wasn't changed
|
||||
};
|
||||
let internal_end = match trim_type {
|
||||
TrimType::TrimLeft => instance.trim_end.unwrap_or(content_end), // trim_end wasn't changed
|
||||
TrimType::TrimRight => old.trim_value.unwrap_or(content_end),
|
||||
TrimType::TrimLeft => instance.trim_end.unwrap_or(clip.duration), // trim_end wasn't changed
|
||||
TrimType::TrimRight => old.trim_value.unwrap_or(clip.duration),
|
||||
};
|
||||
|
||||
// Handle trim based on clip type
|
||||
match &instance.resolve(clip) {
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
// For MIDI: trim_clip expects the pool clip ID
|
||||
controller.trim_clip(*track_id, *midi_clip_id, clip.trim_range(internal_start, internal_end));
|
||||
controller.trim_clip(*track_id, *midi_clip_id, internal_start, internal_end);
|
||||
}
|
||||
ResolvedContent::Audio { .. } => {
|
||||
AudioClipType::Sampled { .. } => {
|
||||
// For sampled audio: trim_clip expects the instance ID
|
||||
let backend_instance_id = backend.clip_instance_to_backend_map.get(instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not mapped to backend", instance_id))?;
|
||||
|
||||
match backend_instance_id {
|
||||
crate::action::BackendClipInstanceId::Audio(audio_id) => {
|
||||
controller.trim_clip(*track_id, *audio_id, clip.trim_range(internal_start, internal_end));
|
||||
controller.trim_clip(*track_id, *audio_id, internal_start, internal_end);
|
||||
}
|
||||
_ => return Err("Expected audio instance ID for sampled clip".to_string()),
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips cannot be trimmed - skip
|
||||
}
|
||||
}
|
||||
@@ -630,8 +553,8 @@ mod tests {
|
||||
let mut vector_layer = VectorLayer::new("Layer 1");
|
||||
|
||||
let mut clip_instance = ClipInstance::new(clip_id);
|
||||
clip_instance.timeline_start = Beats::ZERO;
|
||||
clip_instance.trim_start = ContentTime::ZERO;
|
||||
clip_instance.timeline_start = 0.0;
|
||||
clip_instance.trim_start = 0.0;
|
||||
let instance_id = clip_instance.id;
|
||||
vector_layer.clip_instances.push(clip_instance);
|
||||
|
||||
@@ -644,8 +567,8 @@ mod tests {
|
||||
vec![(
|
||||
instance_id,
|
||||
TrimType::TrimLeft,
|
||||
TrimData::left(ContentTime::ZERO, Beats::ZERO),
|
||||
TrimData::left(ContentTime(2.0), Beats(2.0)),
|
||||
TrimData::left(0.0, 0.0),
|
||||
TrimData::left(2.0, 2.0),
|
||||
)],
|
||||
);
|
||||
|
||||
@@ -661,8 +584,8 @@ mod tests {
|
||||
.iter()
|
||||
.find(|ci| ci.id == instance_id)
|
||||
.unwrap();
|
||||
assert_eq!(instance.trim_start, ContentTime(2.0));
|
||||
assert_eq!(instance.timeline_start, Beats(2.0));
|
||||
assert_eq!(instance.trim_start, 2.0);
|
||||
assert_eq!(instance.timeline_start, 2.0);
|
||||
}
|
||||
|
||||
// Rollback
|
||||
@@ -675,8 +598,8 @@ mod tests {
|
||||
.iter()
|
||||
.find(|ci| ci.id == instance_id)
|
||||
.unwrap();
|
||||
assert_eq!(instance.trim_start, ContentTime::ZERO);
|
||||
assert_eq!(instance.timeline_start, Beats::ZERO);
|
||||
assert_eq!(instance.trim_start, 0.0);
|
||||
assert_eq!(instance.timeline_start, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -706,7 +629,7 @@ mod tests {
|
||||
instance_id,
|
||||
TrimType::TrimRight,
|
||||
TrimData::right(None),
|
||||
TrimData::right(Some(ContentTime(8.0))),
|
||||
TrimData::right(Some(8.0)),
|
||||
)],
|
||||
);
|
||||
|
||||
@@ -722,7 +645,7 @@ mod tests {
|
||||
.iter()
|
||||
.find(|ci| ci.id == instance_id)
|
||||
.unwrap();
|
||||
assert_eq!(instance.trim_end, Some(ContentTime(8.0)));
|
||||
assert_eq!(instance.trim_end, Some(8.0));
|
||||
}
|
||||
|
||||
// Rollback
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
use crate::action::Action;
|
||||
use crate::document::Document;
|
||||
use daw_backend::Beats;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Convert editor-side beats-domain note tuples to the typed backend form.
|
||||
fn notes_to_beats(notes: &[(f64, u8, u8, f64)]) -> Vec<(Beats, u8, u8, Beats)> {
|
||||
notes.iter().map(|&(t, n, v, d)| (Beats(t), n, v, Beats(d))).collect()
|
||||
}
|
||||
|
||||
/// Action to update MIDI notes in a clip (supports undo/redo)
|
||||
///
|
||||
/// Stores the before and after note states. MIDI note data lives in the backend,
|
||||
@@ -55,8 +49,7 @@ impl Action for UpdateMidiNotesAction {
|
||||
.get(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not mapped to backend track", self.layer_id))?;
|
||||
|
||||
// Note times/durations are beats (MIDI content domain); assert that at the typed boundary.
|
||||
controller.update_midi_clip_notes(*track_id, self.midi_clip_id, notes_to_beats(&self.new_notes));
|
||||
controller.update_midi_clip_notes(*track_id, self.midi_clip_id, self.new_notes.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -75,7 +68,7 @@ impl Action for UpdateMidiNotesAction {
|
||||
.get(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not mapped to backend track", self.layer_id))?;
|
||||
|
||||
controller.update_midi_clip_notes(*track_id, self.midi_clip_id, notes_to_beats(&self.old_notes));
|
||||
controller.update_midi_clip_notes(*track_id, self.midi_clip_id, self.old_notes.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -62,10 +62,6 @@ pub enum MediaKind {
|
||||
/// the keyframe id). Decoded eagerly on load and shown while the full-res pixels
|
||||
/// page in, so cold scrubs don't flash blank. See `raster_proxy_media_id`.
|
||||
RasterProxy = 6,
|
||||
/// An embedded font file (TTF/OTF) used by a text layer (keyed by a content hash
|
||||
/// of the bytes so identical fonts dedupe). Registered into the font collection
|
||||
/// on load so documents render faithfully on machines lacking the font.
|
||||
Font = 7,
|
||||
}
|
||||
|
||||
impl MediaKind {
|
||||
@@ -78,7 +74,6 @@ impl MediaKind {
|
||||
4 => Some(Self::Waveform),
|
||||
5 => Some(Self::Thumbnail),
|
||||
6 => Some(Self::RasterProxy),
|
||||
7 => Some(Self::Font),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::layer_tree::LayerTree;
|
||||
use crate::object::Transform;
|
||||
use daw_backend::{Beats, ContentTime, Seconds};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
@@ -111,7 +110,7 @@ impl VectorClip {
|
||||
pub fn content_duration_with(&self, framerate: f64, tempo_map: &crate::tempo_map::TempoMap, clip_duration_fn: impl Fn(&Uuid) -> Option<f64>) -> f64 {
|
||||
let frame_duration = 1.0 / framerate;
|
||||
// Work in beats, convert to seconds at the end.
|
||||
let mut last_beats: Option<Beats> = None;
|
||||
let mut last_beats: Option<f64> = None;
|
||||
let mut last_secs: Option<f64> = None;
|
||||
|
||||
for layer_node in self.layers.iter() {
|
||||
@@ -123,26 +122,21 @@ impl VectorClip {
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
for ci in clip_instances {
|
||||
// Compute end position of this clip instance in beats
|
||||
let end_beats: Beats = if let Some(td_beats) = ci.timeline_duration {
|
||||
let end_beats = if let Some(td_beats) = ci.timeline_duration {
|
||||
ci.timeline_start + td_beats
|
||||
} else if let Some(te) = ci.trim_end {
|
||||
// `clip_duration_fn` hands back seconds, so this whole path treats nested
|
||||
// content as wall-clock. That's right for the vector/video/audio clips a vector
|
||||
// clip actually nests; a nested MIDI clip (beats content) would need resolving
|
||||
// against its clip, which this callback can't do. Pre-existing limitation.
|
||||
let secs = (te - ci.trim_start).raw().max(0.0);
|
||||
tempo_map.seconds_to_beats(tempo_map.beats_to_seconds(ci.timeline_start) + Seconds(secs))
|
||||
let secs = (te - ci.trim_start).max(0.0);
|
||||
ci.timeline_start + tempo_map.inverse_transform(tempo_map.transform(ci.timeline_start) + secs) - ci.timeline_start
|
||||
} else if let Some(clip_dur_secs) = clip_duration_fn(&ci.clip_id) {
|
||||
let secs = (clip_dur_secs - ci.trim_start.raw()).max(0.0);
|
||||
tempo_map.seconds_to_beats(tempo_map.beats_to_seconds(ci.timeline_start) + Seconds(secs))
|
||||
let secs = (clip_dur_secs - ci.trim_start).max(0.0);
|
||||
ci.timeline_start + tempo_map.inverse_transform(tempo_map.transform(ci.timeline_start) + secs) - ci.timeline_start
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
last_beats = Some(last_beats.map_or(end_beats, |t: Beats| t.max(end_beats)));
|
||||
last_beats = Some(last_beats.map_or(end_beats, |t: f64| t.max(end_beats)));
|
||||
}
|
||||
|
||||
// Vector layer keyframes are in seconds
|
||||
@@ -153,7 +147,7 @@ impl VectorClip {
|
||||
}
|
||||
}
|
||||
|
||||
let from_clips = last_beats.map(|b| tempo_map.beats_to_seconds(b).seconds_to_f64());
|
||||
let from_clips = last_beats.map(|b| tempo_map.transform(b));
|
||||
let combined = match (from_clips, last_secs) {
|
||||
(Some(a), Some(b)) => Some(a.max(b)),
|
||||
(Some(a), None) => Some(a),
|
||||
@@ -204,10 +198,8 @@ impl VectorClip {
|
||||
for clip_instance in &vector_layer.clip_instances {
|
||||
// Convert parent clip time (seconds) to nested clip local time (seconds).
|
||||
// timeline_start is in beats; convert to seconds using document BPM.
|
||||
let start_secs = document.tempo_map().beats_to_seconds(clip_instance.timeline_start).seconds_to_f64();
|
||||
// Nested clips here are vector clips, whose content is wall-clock seconds.
|
||||
let nested_clip_time =
|
||||
((clip_time - start_secs) * clip_instance.playback_speed) + clip_instance.trim_start.raw();
|
||||
let start_secs = document.tempo_map().transform(clip_instance.timeline_start);
|
||||
let nested_clip_time = ((clip_time - start_secs) * clip_instance.playback_speed) + clip_instance.trim_start;
|
||||
|
||||
// Look up the nested clip definition
|
||||
let nested_bounds = if let Some(nested_clip) = document.get_vector_clip(&clip_instance.clip_id) {
|
||||
@@ -231,17 +223,6 @@ impl VectorClip {
|
||||
Some(existing) => existing.union(transformed_bounds),
|
||||
});
|
||||
}
|
||||
} else if let AnyLayer::Text(text_layer) = &layer_node.data {
|
||||
// Text layers contribute their box bounds (so a text-only clip is
|
||||
// selectable/draggable, not a degenerate point).
|
||||
let r = Rect::from_origin_size(
|
||||
text_layer.box_origin,
|
||||
(text_layer.box_width, text_layer.box_height),
|
||||
);
|
||||
combined_bounds = Some(match combined_bounds {
|
||||
None => r,
|
||||
Some(existing) => existing.union(r),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -474,67 +455,6 @@ pub enum AudioClipType {
|
||||
Recording,
|
||||
}
|
||||
|
||||
/// One take of a cycle recording — see [`ClipInstance::takes`].
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct AudioTake {
|
||||
/// Display name, e.g. "Take 1". User-editable.
|
||||
pub name: String,
|
||||
/// The recorded content this take points at.
|
||||
pub content: TakeContent,
|
||||
}
|
||||
|
||||
/// What a take actually holds. An instance's takes are all the same kind — one cycle-record session
|
||||
/// captures either audio or MIDI, never a mix.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum TakeContent {
|
||||
/// Sampled audio: index into the audio pool.
|
||||
Audio { audio_pool_index: usize },
|
||||
/// MIDI: backend MIDI clip ID.
|
||||
Midi { midi_clip_id: u32 },
|
||||
}
|
||||
|
||||
/// A clip's content duration, tagged by its native unit.
|
||||
///
|
||||
/// Sampled/recording audio and video measure content in wall-clock **seconds**; MIDI measures
|
||||
/// it in **beats** (tempo-independent musical length). Carrying the domain in the type means a
|
||||
/// duration can't be silently read in the wrong unit.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum ClipDuration {
|
||||
Seconds(Seconds),
|
||||
Beats(Beats),
|
||||
}
|
||||
|
||||
impl ClipDuration {
|
||||
/// Wall-clock seconds. Beats are converted as a length from beat 0 (exact under constant
|
||||
/// tempo; a reasonable approximation otherwise — durations are position-independent here).
|
||||
pub fn to_seconds(self, tempo_map: &daw_backend::TempoMap) -> Seconds {
|
||||
match self {
|
||||
ClipDuration::Seconds(s) => s,
|
||||
ClipDuration::Beats(b) => tempo_map.beats_to_seconds(b),
|
||||
}
|
||||
}
|
||||
|
||||
/// The raw magnitude in the clip's native unit. Use only in code that already works in that
|
||||
/// domain (e.g. trim math, whose values share the clip's native domain).
|
||||
pub fn native(self) -> f64 {
|
||||
match self {
|
||||
ClipDuration::Seconds(s) => s.seconds_to_f64(),
|
||||
ClipDuration::Beats(b) => b.beats_to_f64(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Tag a [`ContentTime`] with *this* duration's domain.
|
||||
///
|
||||
/// Handy when you already hold a clip's content duration (so you know the domain) and need to
|
||||
/// resolve one of its trim bounds, without going back to the clip.
|
||||
pub fn same_domain(self, t: ContentTime) -> ClipDuration {
|
||||
match self {
|
||||
ClipDuration::Seconds(_) => ClipDuration::Seconds(Seconds(t.raw())),
|
||||
ClipDuration::Beats(_) => ClipDuration::Beats(Beats(t.raw())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Audio clip
|
||||
///
|
||||
/// This is compatible with daw-backend's audio system:
|
||||
@@ -548,13 +468,9 @@ pub struct AudioClip {
|
||||
/// Clip name
|
||||
pub name: String,
|
||||
|
||||
/// Raw content duration in the clip's **native domain** — SECONDS for sampled/recording
|
||||
/// audio, BEATS for MIDI (musical length, tempo-independent). Private on purpose: the domain
|
||||
/// depends on `clip_type`, so all access goes through [`AudioClip::content_duration`] /
|
||||
/// [`AudioClip::set_content_duration`], which keep it type-safe. Stored as a bare `f64`
|
||||
/// because the `.beam` format serializes it as a plain number (serde derives over private
|
||||
/// fields fine); a domain-tagged newtype would change the on-disk shape.
|
||||
duration: f64,
|
||||
/// Duration in seconds
|
||||
/// For sampled audio, this can be set to trim the audio shorter than the source file
|
||||
pub duration: f64,
|
||||
|
||||
/// Audio clip type (sampled or MIDI)
|
||||
pub clip_type: AudioClipType,
|
||||
@@ -565,34 +481,6 @@ pub struct AudioClip {
|
||||
}
|
||||
|
||||
impl AudioClip {
|
||||
/// The clip's content duration, tagged with its native domain (seconds for sampled/recording,
|
||||
/// beats for MIDI). This is the only sanctioned way to read the raw `duration` field.
|
||||
pub fn content_duration(&self) -> ClipDuration {
|
||||
if self.is_midi_domain() {
|
||||
ClipDuration::Beats(Beats(self.duration))
|
||||
} else {
|
||||
ClipDuration::Seconds(Seconds(self.duration))
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the content duration. Debug-asserts the value's domain matches the clip type so a
|
||||
/// beats duration can't be stored on a seconds clip (or vice-versa).
|
||||
pub fn set_content_duration(&mut self, duration: ClipDuration) {
|
||||
debug_assert!(
|
||||
matches!(
|
||||
(self.is_midi_domain(), duration),
|
||||
(true, ClipDuration::Beats(_)) | (false, ClipDuration::Seconds(_))
|
||||
),
|
||||
"clip duration domain must match clip type",
|
||||
);
|
||||
self.duration = duration.native();
|
||||
}
|
||||
|
||||
/// Whether this clip's `duration` is measured in beats (MIDI) rather than seconds.
|
||||
fn is_midi_domain(&self) -> bool {
|
||||
matches!(self.clip_type, AudioClipType::Midi { .. })
|
||||
}
|
||||
|
||||
/// Create a new sampled audio clip
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -676,73 +564,6 @@ impl AudioClip {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The clip's own content, ignoring takes.
|
||||
///
|
||||
/// Callers that are handing content to the backend want [`ClipInstance::resolve`] instead — an
|
||||
/// instance with takes overrides the clip's content with whichever take is active.
|
||||
pub fn resolve(&self) -> ResolvedContent {
|
||||
match &self.clip_type {
|
||||
AudioClipType::Sampled { audio_pool_index } => ResolvedContent::Audio {
|
||||
audio_pool_index: *audio_pool_index,
|
||||
},
|
||||
AudioClipType::Midi { midi_clip_id } => ResolvedContent::Midi {
|
||||
midi_clip_id: *midi_clip_id,
|
||||
},
|
||||
AudioClipType::Recording => ResolvedContent::Recording,
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a content time against this clip's domain.
|
||||
///
|
||||
/// This is the ONLY sanctioned way to turn a [`ContentTime`] into a real duration — the type has
|
||||
/// no `.to_seconds()` of its own precisely so that the clip, which is the one thing that knows
|
||||
/// whether its content is measured in seconds or beats, has to be consulted.
|
||||
pub fn resolve_content_time(&self, t: ContentTime) -> ClipDuration {
|
||||
if self.is_midi_domain() {
|
||||
ClipDuration::Beats(Beats(t.raw()))
|
||||
} else {
|
||||
ClipDuration::Seconds(Seconds(t.raw()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Tag a pair of trim bounds with this clip's content domain, ready for the backend.
|
||||
///
|
||||
/// Building the [`TrimRange`] from the clip means a caller can't reach for the wrong variant:
|
||||
/// the clip is the one thing that knows the domain.
|
||||
pub fn trim_range(&self, start: ContentTime, end: ContentTime) -> daw_backend::command::TrimRange {
|
||||
if self.is_midi_domain() {
|
||||
daw_backend::command::TrimRange::Beats {
|
||||
start: Beats(start.raw()),
|
||||
end: Beats(end.raw()),
|
||||
}
|
||||
} else {
|
||||
daw_backend::command::TrimRange::Seconds {
|
||||
start: Seconds(start.raw()),
|
||||
end: Seconds(end.raw()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this clip's own content is the given audio pool index.
|
||||
pub fn owns_audio_pool_index(&self, pool_index: usize) -> bool {
|
||||
self.audio_pool_index() == Some(pool_index)
|
||||
}
|
||||
|
||||
/// Whether this clip's own content is the given backend MIDI clip ID.
|
||||
pub fn owns_midi_clip_id(&self, id: u32) -> bool {
|
||||
self.midi_clip_id() == Some(id)
|
||||
}
|
||||
}
|
||||
|
||||
/// What a clip instance actually plays, once takes are resolved to the active one.
|
||||
/// Produced by [`ClipInstance::resolve`].
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum ResolvedContent {
|
||||
Audio { audio_pool_index: usize },
|
||||
Midi { midi_clip_id: u32 },
|
||||
/// A recording in progress (or an empty take folder) — no backend content yet.
|
||||
Recording,
|
||||
}
|
||||
|
||||
/// Unified clip enum for polymorphic handling
|
||||
@@ -814,24 +635,23 @@ pub struct ClipInstance {
|
||||
|
||||
/// When this instance starts on the timeline, in **beats**.
|
||||
/// Default: 0.0
|
||||
pub timeline_start: Beats,
|
||||
pub timeline_start: f64,
|
||||
|
||||
/// How long this instance appears on the timeline, in **beats**.
|
||||
/// If set and longer than the trimmed content, the content will loop.
|
||||
/// Default: None (use trimmed clip duration, no looping)
|
||||
pub timeline_duration: Option<Beats>,
|
||||
pub timeline_duration: Option<f64>,
|
||||
|
||||
/// Trim start: offset into the clip's internal content.
|
||||
///
|
||||
/// A [`ContentTime`] — measured in the CLIP's content domain, which is seconds for sampled
|
||||
/// audio/video/vector but BEATS for MIDI. Resolve it against the clip
|
||||
/// ([`Document::resolve_content_time`]) before combining it with anything on the timeline.
|
||||
/// Trim start: offset into the clip's internal content, in **seconds**.
|
||||
/// - For audio: byte-offset into the audio file
|
||||
/// - For video: seek position in the video file
|
||||
/// - For vector: time offset into the animation
|
||||
/// Default: 0.0
|
||||
pub trim_start: ContentTime,
|
||||
pub trim_start: f64,
|
||||
|
||||
/// Trim end: offset into the clip's internal content. See [`Self::trim_start`].
|
||||
/// Trim end: offset into the clip's internal content, in **seconds**.
|
||||
/// Default: None (use full clip duration)
|
||||
pub trim_end: Option<ContentTime>,
|
||||
pub trim_end: Option<f64>,
|
||||
|
||||
/// Playback speed multiplier
|
||||
/// 1.0 = normal speed, 0.5 = half speed, 2.0 = double speed
|
||||
@@ -847,36 +667,7 @@ pub struct ClipInstance {
|
||||
/// When set, loop iterations are drawn/played before the content start.
|
||||
/// Default: None (no pre-loop)
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub loop_before: Option<Beats>,
|
||||
|
||||
/// Alternate takes from cycle recording. Empty = an ordinary instance with no takes.
|
||||
///
|
||||
/// The takes live on the INSTANCE, not the clip, so managing them is per-instance: deleting or
|
||||
/// renaming a take on one instance leaves every other instance alone. Splitting clones the list
|
||||
/// along with the rest of the instance, so the two halves get independent take lists — and,
|
||||
/// since they can each select a different take, comping still falls out for free.
|
||||
///
|
||||
/// Every take spans the full cycle region (partial passes are padded with silence at capture
|
||||
/// time), so they're all the same length as the clip's own content. That uniformity is what lets
|
||||
/// a take switch leave the instance's geometry untouched.
|
||||
///
|
||||
/// When non-empty, these OVERRIDE the clip's own content — see [`Self::resolve`].
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub takes: Vec<AudioTake>,
|
||||
|
||||
/// Which of [`Self::takes`] plays. `None` (or a stale index) means take 0.
|
||||
/// Default: None
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub active_take: Option<usize>,
|
||||
|
||||
/// The cycle region's length in beats when these takes were recorded.
|
||||
///
|
||||
/// Audio takes are cut geometrically (by sample count), so they're only meaningful against the
|
||||
/// tempo they were recorded at. Keeping the recorded length lets a future time-stretch/conform
|
||||
/// feature reconcile them if the tempo moves underneath, and lets a new recording tell whether
|
||||
/// it belongs in this take list or a fresh one.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub recorded_loop_beats: Option<Beats>,
|
||||
pub loop_before: Option<f64>,
|
||||
}
|
||||
|
||||
/// High 64-bit sentinel used to identify UUIDs that encode a backend audio clip instance ID.
|
||||
@@ -928,16 +719,13 @@ impl ClipInstance {
|
||||
transform: Transform::default(),
|
||||
opacity: 1.0,
|
||||
name: None,
|
||||
timeline_start: Beats::ZERO,
|
||||
timeline_start: 0.0,
|
||||
timeline_duration: None,
|
||||
trim_start: ContentTime::ZERO,
|
||||
trim_start: 0.0,
|
||||
trim_end: None,
|
||||
playback_speed: 1.0,
|
||||
gain: 1.0,
|
||||
loop_before: None,
|
||||
takes: Vec::new(),
|
||||
active_take: None,
|
||||
recorded_loop_beats: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -949,16 +737,13 @@ impl ClipInstance {
|
||||
transform: Transform::default(),
|
||||
opacity: 1.0,
|
||||
name: None,
|
||||
timeline_start: Beats::ZERO,
|
||||
timeline_start: 0.0,
|
||||
timeline_duration: None,
|
||||
trim_start: ContentTime::ZERO,
|
||||
trim_start: 0.0,
|
||||
trim_end: None,
|
||||
playback_speed: 1.0,
|
||||
gain: 1.0,
|
||||
loop_before: None,
|
||||
takes: Vec::new(),
|
||||
active_take: None,
|
||||
recorded_loop_beats: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -987,14 +772,14 @@ impl ClipInstance {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set timeline position (beats).
|
||||
pub fn with_timeline_start(mut self, timeline_start: Beats) -> Self {
|
||||
/// Set timeline position
|
||||
pub fn with_timeline_start(mut self, timeline_start: f64) -> Self {
|
||||
self.timeline_start = timeline_start;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set trimming (start and end time within the clip's internal content)
|
||||
pub fn with_trimming(mut self, trim_start: ContentTime, trim_end: Option<ContentTime>) -> Self {
|
||||
pub fn with_trimming(mut self, trim_start: f64, trim_end: Option<f64>) -> Self {
|
||||
self.trim_start = trim_start;
|
||||
self.trim_end = trim_end;
|
||||
self
|
||||
@@ -1013,149 +798,80 @@ impl ClipInstance {
|
||||
}
|
||||
|
||||
/// Set explicit timeline duration (in beats) by directly setting `timeline_duration`.
|
||||
pub fn with_timeline_duration(mut self, duration_beats: Beats) -> Self {
|
||||
pub fn with_timeline_duration(mut self, duration_beats: f64) -> Self {
|
||||
self.timeline_duration = Some(duration_beats);
|
||||
self
|
||||
}
|
||||
|
||||
/// The take this instance plays, if it has any.
|
||||
///
|
||||
/// A `None`/stale `active_take` falls back to take 0 — an index can go stale (an undo that
|
||||
/// shrank the list, an old `.beam`), and silently playing the first take beats playing nothing.
|
||||
pub fn active_take(&self) -> Option<&AudioTake> {
|
||||
self.takes
|
||||
.get(self.active_take.unwrap_or(0))
|
||||
.or_else(|| self.takes.first())
|
||||
}
|
||||
|
||||
/// The index [`Self::active_take`] actually resolves to, clamped into range.
|
||||
pub fn active_take_index(&self) -> usize {
|
||||
let i = self.active_take.unwrap_or(0);
|
||||
if i < self.takes.len() { i } else { 0 }
|
||||
}
|
||||
|
||||
/// What this instance plays: its active take if it has takes, otherwise the clip's own content.
|
||||
///
|
||||
/// This is the sanctioned way to ask "what content do I hand the backend for this instance?".
|
||||
/// Takes are never a distinct *case* at the call site — an instance with takes is just an audio
|
||||
/// or MIDI instance whose identity depends on which take is live.
|
||||
pub fn resolve(&self, clip: &AudioClip) -> ResolvedContent {
|
||||
match self.active_take().map(|t| &t.content) {
|
||||
Some(TakeContent::Audio { audio_pool_index }) => ResolvedContent::Audio {
|
||||
audio_pool_index: *audio_pool_index,
|
||||
},
|
||||
Some(TakeContent::Midi { midi_clip_id }) => ResolvedContent::Midi {
|
||||
midi_clip_id: *midi_clip_id,
|
||||
},
|
||||
None => clip.resolve(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The audio pool index this instance plays. See [`Self::resolve`].
|
||||
pub fn resolved_audio_pool_index(&self, clip: &AudioClip) -> Option<usize> {
|
||||
match self.resolve(clip) {
|
||||
ResolvedContent::Audio { audio_pool_index } => Some(audio_pool_index),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The backend MIDI clip ID this instance plays. See [`Self::resolve`].
|
||||
pub fn resolved_midi_clip_id(&self, clip: &AudioClip) -> Option<u32> {
|
||||
match self.resolve(clip) {
|
||||
ResolvedContent::Midi { midi_clip_id } => Some(midi_clip_id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Content window (`trim_end - trim_start`) in the clip's own content domain.
|
||||
/// Content window size in seconds: `trim_end - trim_start`.
|
||||
/// Used for internal looping calculations.
|
||||
pub fn content_window(&self, clip_content: ClipDuration) -> ClipDuration {
|
||||
let end = self.trim_end.map_or(clip_content.native(), |t| t.raw());
|
||||
let window = (end - self.trim_start.raw()).max(0.0);
|
||||
match clip_content {
|
||||
ClipDuration::Beats(_) => ClipDuration::Beats(Beats(window)),
|
||||
ClipDuration::Seconds(_) => ClipDuration::Seconds(Seconds(window)),
|
||||
}
|
||||
pub fn content_window_secs(&self, clip_duration_secs: f64) -> f64 {
|
||||
let end = self.trim_end.unwrap_or(clip_duration_secs);
|
||||
(end - self.trim_start).max(0.0)
|
||||
}
|
||||
|
||||
/// How long this instance appears on the timeline, in **beats**.
|
||||
///
|
||||
/// If `timeline_duration` is set, returns that (enabling content looping). Otherwise the clip
|
||||
/// occupies its content window — converted to beats *in the clip's own domain*:
|
||||
///
|
||||
/// - MIDI content is already beats and is tempo-invariant, so it carries over directly.
|
||||
/// - Wall-clock content (audio/video/vector) is a seconds span, so it converts at the clip's
|
||||
/// position on the timeline.
|
||||
///
|
||||
/// Taking a `ClipDuration` rather than a bare `Seconds` is what keeps those apart: this used to
|
||||
/// take seconds and subtract `trim_start` from it, which for a TRIMMED MIDI clip subtracted a
|
||||
/// beats offset from a seconds duration and got the clip's length wrong.
|
||||
pub fn effective_duration_beats(&self, clip_content: ClipDuration, tempo_map: &crate::tempo_map::TempoMap) -> Beats {
|
||||
/// If `timeline_duration` is set, returns that (enabling content looping).
|
||||
/// Otherwise converts the content window from seconds to beats using the tempo map.
|
||||
pub fn effective_duration_beats(&self, clip_duration_secs: f64, tempo_map: &crate::tempo_map::TempoMap) -> f64 {
|
||||
if let Some(td) = self.timeline_duration {
|
||||
return td;
|
||||
}
|
||||
match self.content_window(clip_content) {
|
||||
ClipDuration::Beats(b) => b,
|
||||
ClipDuration::Seconds(s) => {
|
||||
let start_secs = tempo_map.beats_to_seconds(self.timeline_start);
|
||||
tempo_map.seconds_to_beats(start_secs + s) - self.timeline_start
|
||||
}
|
||||
}
|
||||
let window_secs = self.content_window_secs(clip_duration_secs);
|
||||
let start_secs = tempo_map.transform(self.timeline_start);
|
||||
tempo_map.inverse_transform(start_secs + window_secs) - self.timeline_start
|
||||
}
|
||||
|
||||
/// Left edge of the clip's visual extent on the timeline, in **beats**.
|
||||
pub fn effective_start(&self) -> Beats {
|
||||
self.timeline_start - self.loop_before.unwrap_or(Beats::ZERO)
|
||||
pub fn effective_start(&self) -> f64 {
|
||||
self.timeline_start - self.loop_before.unwrap_or(0.0)
|
||||
}
|
||||
|
||||
/// Total visual duration (loop_before + effective_duration), in **beats**.
|
||||
pub fn total_duration(&self, clip_content: ClipDuration, tempo_map: &crate::tempo_map::TempoMap) -> Beats {
|
||||
self.loop_before.unwrap_or(Beats::ZERO) + self.effective_duration_beats(clip_content, tempo_map)
|
||||
pub fn total_duration(&self, clip_duration_secs: f64, tempo_map: &crate::tempo_map::TempoMap) -> f64 {
|
||||
self.loop_before.unwrap_or(0.0) + self.effective_duration_beats(clip_duration_secs, tempo_map)
|
||||
}
|
||||
|
||||
/// Map a playback time (in **seconds**) to clip-local content time (in **seconds**).
|
||||
///
|
||||
/// The trim bounds are resolved through `clip_content`'s domain first, so a MIDI clip's beats
|
||||
/// trims are converted rather than read as seconds. Callers are the wall-clock consumers (video
|
||||
/// seek, vector/raster rendering), which want seconds regardless of how the clip stores content.
|
||||
///
|
||||
/// Returns `None` if the clip instance is not active at `time`.
|
||||
pub fn remap_time_secs(&self, time: Seconds, clip_content: ClipDuration, tempo_map: &crate::tempo_map::TempoMap) -> Option<Seconds> {
|
||||
let start_secs = tempo_map.beats_to_seconds(self.timeline_start);
|
||||
let dur_beats = self.effective_duration_beats(clip_content, tempo_map);
|
||||
let end_secs = tempo_map.beats_to_seconds(self.timeline_start + dur_beats);
|
||||
/// Returns `None` if the clip instance is not active at `time_secs`.
|
||||
pub fn remap_time_secs(&self, time_secs: f64, clip_duration_secs: f64, tempo_map: &crate::tempo_map::TempoMap) -> Option<f64> {
|
||||
let start_secs = tempo_map.transform(self.timeline_start);
|
||||
let dur_beats = self.effective_duration_beats(clip_duration_secs, tempo_map);
|
||||
let end_secs = tempo_map.transform(self.timeline_start + dur_beats);
|
||||
|
||||
if time < start_secs || time >= end_secs {
|
||||
if time_secs < start_secs || time_secs >= end_secs {
|
||||
return None;
|
||||
}
|
||||
|
||||
let trim_start_secs = clip_content.same_domain(self.trim_start).to_seconds(tempo_map);
|
||||
let content_time = (time - start_secs) * self.playback_speed;
|
||||
let content_window = self.content_window(clip_content).to_seconds(tempo_map);
|
||||
let relative_secs = time_secs - start_secs;
|
||||
let content_time = relative_secs * self.playback_speed;
|
||||
let content_window = self.content_window_secs(clip_duration_secs);
|
||||
|
||||
if content_window == Seconds::ZERO {
|
||||
return Some(trim_start_secs);
|
||||
if content_window == 0.0 {
|
||||
return Some(self.trim_start);
|
||||
}
|
||||
|
||||
let looped = if content_time > content_window {
|
||||
let looped_time = if content_time > content_window {
|
||||
content_time % content_window
|
||||
} else {
|
||||
content_time
|
||||
};
|
||||
|
||||
Some(trim_start_secs + looped)
|
||||
Some(self.trim_start + looped_time)
|
||||
}
|
||||
|
||||
/// Alias for `remap_time_secs`.
|
||||
#[inline]
|
||||
pub fn remap_time(&self, time: Seconds, clip_content: ClipDuration, tempo_map: &crate::tempo_map::TempoMap) -> Option<Seconds> {
|
||||
self.remap_time_secs(time, clip_content, tempo_map)
|
||||
pub fn remap_time(&self, time_secs: f64, clip_duration_secs: f64, tempo_map: &crate::tempo_map::TempoMap) -> Option<f64> {
|
||||
self.remap_time_secs(time_secs, clip_duration_secs, tempo_map)
|
||||
}
|
||||
|
||||
/// Alias for `effective_duration_beats`.
|
||||
#[inline]
|
||||
pub fn effective_duration(&self, clip_content: ClipDuration, tempo_map: &crate::tempo_map::TempoMap) -> Beats {
|
||||
self.effective_duration_beats(clip_content, tempo_map)
|
||||
pub fn effective_duration(&self, clip_duration_secs: f64, tempo_map: &crate::tempo_map::TempoMap) -> f64 {
|
||||
self.effective_duration_beats(clip_duration_secs, tempo_map)
|
||||
}
|
||||
|
||||
/// Convert to affine transform
|
||||
@@ -1231,8 +947,8 @@ mod tests {
|
||||
|
||||
assert_eq!(instance.clip_id, clip_id);
|
||||
assert_eq!(instance.opacity, 1.0);
|
||||
assert_eq!(instance.timeline_start, Beats::ZERO);
|
||||
assert_eq!(instance.trim_start, ContentTime::ZERO);
|
||||
assert_eq!(instance.timeline_start, 0.0);
|
||||
assert_eq!(instance.trim_start, 0.0);
|
||||
assert_eq!(instance.trim_end, None);
|
||||
assert_eq!(instance.playback_speed, 1.0);
|
||||
assert_eq!(instance.gain, 1.0);
|
||||
@@ -1242,52 +958,27 @@ mod tests {
|
||||
fn test_clip_instance_trimming() {
|
||||
let clip_id = Uuid::new_v4();
|
||||
let instance = ClipInstance::new(clip_id)
|
||||
.with_trimming(ContentTime(2.0), Some(ContentTime(8.0)));
|
||||
.with_trimming(2.0, Some(8.0));
|
||||
|
||||
assert_eq!(instance.trim_start, ContentTime(2.0));
|
||||
assert_eq!(instance.trim_end, Some(ContentTime(8.0)));
|
||||
assert_eq!(instance.trim_start, 2.0);
|
||||
assert_eq!(instance.trim_end, Some(8.0));
|
||||
// At 60 BPM the tempo map is identity (1 beat == 1 second), so the
|
||||
// beats-domain effective duration equals the seconds content window.
|
||||
let tempo_map = crate::tempo_map::TempoMap::constant(60.0);
|
||||
let content = ClipDuration::Seconds(Seconds(10.0));
|
||||
assert_eq!(instance.effective_duration(content, &tempo_map), Beats(6.0));
|
||||
assert_eq!(instance.effective_duration(10.0, &tempo_map), 6.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clip_instance_no_end_trim() {
|
||||
let clip_id = Uuid::new_v4();
|
||||
let instance = ClipInstance::new(clip_id)
|
||||
.with_trimming(ContentTime(2.0), None);
|
||||
.with_trimming(2.0, None);
|
||||
|
||||
assert_eq!(instance.trim_start, ContentTime(2.0));
|
||||
assert_eq!(instance.trim_start, 2.0);
|
||||
assert_eq!(instance.trim_end, None);
|
||||
// At 60 BPM the tempo map is identity (1 beat == 1 second).
|
||||
let tempo_map = crate::tempo_map::TempoMap::constant(60.0);
|
||||
let content = ClipDuration::Seconds(Seconds(10.0));
|
||||
assert_eq!(instance.effective_duration(content, &tempo_map), Beats(8.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trimmed_midi_clip_keeps_its_beats_length_across_tempo() {
|
||||
// Regression: `effective_duration_beats` used to take a SECONDS clip duration and subtract
|
||||
// `trim_start` from it. For a TRIMMED MIDI clip that subtracted a beats offset from a
|
||||
// seconds duration, so the clip's timeline length came out wrong at any tempo but 60 BPM.
|
||||
//
|
||||
// MIDI content is beats and tempo-invariant: a clip trimmed to beats 2..6 is 4 beats long
|
||||
// whatever the tempo says.
|
||||
let clip_id = Uuid::new_v4();
|
||||
let instance = ClipInstance::new(clip_id)
|
||||
.with_trimming(ContentTime(2.0), Some(ContentTime(6.0)));
|
||||
let content = ClipDuration::Beats(Beats(8.0));
|
||||
|
||||
for bpm in [60.0, 120.0, 90.0] {
|
||||
let tempo_map = crate::tempo_map::TempoMap::constant(bpm);
|
||||
assert_eq!(
|
||||
instance.effective_duration(content, &tempo_map),
|
||||
Beats(4.0),
|
||||
"a MIDI clip trimmed to beats 2..6 is 4 beats long at {bpm} BPM",
|
||||
);
|
||||
}
|
||||
assert_eq!(instance.effective_duration(10.0, &tempo_map), 8.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1307,109 +998,4 @@ mod tests {
|
||||
assert_eq!(instance.playback_speed, 2.0);
|
||||
assert_eq!(instance.gain, 0.8);
|
||||
}
|
||||
|
||||
/// A clip whose own content is pool 10, plus an instance carrying takes over the given pools.
|
||||
fn with_takes(pool_indices: &[usize]) -> (AudioClip, ClipInstance) {
|
||||
let clip = AudioClip::new_sampled("Cycle rec", pool_indices[0], 2.0);
|
||||
let mut instance = ClipInstance::new(clip.id);
|
||||
instance.takes = pool_indices
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &audio_pool_index)| AudioTake {
|
||||
name: format!("Take {}", i + 1),
|
||||
content: TakeContent::Audio { audio_pool_index },
|
||||
})
|
||||
.collect();
|
||||
instance.recorded_loop_beats = Some(Beats(8.0));
|
||||
(clip, instance)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn active_take_selects_the_pool_file() {
|
||||
let (clip, mut instance) = with_takes(&[10, 11, 12]);
|
||||
instance.active_take = Some(0);
|
||||
assert_eq!(instance.resolved_audio_pool_index(&clip), Some(10));
|
||||
instance.active_take = Some(2);
|
||||
assert_eq!(instance.resolved_audio_pool_index(&clip), Some(12));
|
||||
// None means take 0.
|
||||
instance.active_take = None;
|
||||
assert_eq!(instance.resolved_audio_pool_index(&clip), Some(10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_range_take_falls_back_to_the_first() {
|
||||
// An index can go stale (an undo that shrank the list, an old .beam). Playing the first take
|
||||
// beats playing nothing.
|
||||
let (clip, mut instance) = with_takes(&[10, 11]);
|
||||
instance.active_take = Some(99);
|
||||
assert_eq!(instance.resolved_audio_pool_index(&clip), Some(10));
|
||||
assert_eq!(instance.active_take_index(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_instance_without_takes_plays_the_clips_own_content() {
|
||||
let clip = AudioClip::new_sampled("Plain", 42, 2.0);
|
||||
let instance = ClipInstance::new(clip.id);
|
||||
assert!(instance.takes.is_empty());
|
||||
assert_eq!(instance.resolved_audio_pool_index(&clip), Some(42));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn midi_takes_resolve_to_midi_content() {
|
||||
let clip = AudioClip::new_midi("Cycle rec", 7, Beats(4.0));
|
||||
let mut instance = ClipInstance::new(clip.id);
|
||||
instance.takes = vec![AudioTake {
|
||||
name: "Take 1".into(),
|
||||
content: TakeContent::Midi { midi_clip_id: 7 },
|
||||
}];
|
||||
assert_eq!(clip.content_duration(), ClipDuration::Beats(Beats(4.0)));
|
||||
assert_eq!(instance.resolved_midi_clip_id(&clip), Some(7));
|
||||
assert_eq!(instance.resolved_audio_pool_index(&clip), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splitting_gives_each_half_an_independent_take_list() {
|
||||
// Takes live on the INSTANCE, so a split (which clones the instance) hands each half its own
|
||||
// list. Two consequences, both wanted: the halves can select different takes (comping), and
|
||||
// deleting a take from one leaves the other alone.
|
||||
let (clip, left_src) = with_takes(&[10, 11, 12]);
|
||||
let mut left = left_src.clone();
|
||||
let mut right = left_src.clone();
|
||||
right.id = Uuid::new_v4();
|
||||
|
||||
left.active_take = Some(0);
|
||||
right.active_take = Some(2);
|
||||
assert_eq!(left.resolved_audio_pool_index(&clip), Some(10));
|
||||
assert_eq!(right.resolved_audio_pool_index(&clip), Some(12));
|
||||
|
||||
// Delete take 2 (pool 11) from the left half only.
|
||||
left.takes.remove(1);
|
||||
assert_eq!(left.takes.len(), 2);
|
||||
assert_eq!(right.takes.len(), 3, "the other half keeps its own takes");
|
||||
assert_eq!(
|
||||
right.resolved_audio_pool_index(&clip),
|
||||
Some(12),
|
||||
"and its selection still points where it did",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clip_instance_without_active_take_deserializes() {
|
||||
// Back-compat: .beam files written before take folders have no `active_take` field.
|
||||
let json = r#"{
|
||||
"id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"clip_id": "550e8400-e29b-41d4-a716-446655440001",
|
||||
"transform": {"x": 0.0, "y": 0.0, "rotation": 0.0, "scale_x": 1.0, "scale_y": 1.0, "skew_x": 0.0, "skew_y": 0.0},
|
||||
"opacity": 1.0,
|
||||
"name": null,
|
||||
"timeline_start": 0.0,
|
||||
"timeline_duration": null,
|
||||
"trim_start": 0.0,
|
||||
"trim_end": null,
|
||||
"playback_speed": 1.0,
|
||||
"gain": 1.0
|
||||
}"#;
|
||||
let instance: ClipInstance = serde_json::from_str(json).expect("old instances must load");
|
||||
assert_eq!(instance.active_take, None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,6 @@ impl ClipboardLayerType {
|
||||
AnyLayer::Effect(_) => ClipboardLayerType::Effect,
|
||||
AnyLayer::Group(_) => ClipboardLayerType::Vector,
|
||||
AnyLayer::Raster(_) => ClipboardLayerType::Vector,
|
||||
AnyLayer::Text(_) => ClipboardLayerType::Vector,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,13 +449,6 @@ fn regen_any_layer(layer: &AnyLayer, id_map: &mut HashMap<Uuid, Uuid>) -> AnyLay
|
||||
nl.layer.id = new_layer_id;
|
||||
AnyLayer::Raster(nl)
|
||||
}
|
||||
AnyLayer::Text(tl) => {
|
||||
let new_layer_id = Uuid::new_v4();
|
||||
id_map.insert(tl.layer.id, new_layer_id);
|
||||
let mut nl = tl.clone();
|
||||
nl.layer.id = new_layer_id;
|
||||
AnyLayer::Text(nl)
|
||||
}
|
||||
AnyLayer::Group(gl) => {
|
||||
let new_layer_id = Uuid::new_v4();
|
||||
id_map.insert(gl.layer.id, new_layer_id);
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
use crate::asset_folder::AssetFolderTree;
|
||||
use crate::clip::{AudioClip, ClipInstance, ImageAsset, VideoClip, VectorClip};
|
||||
use daw_backend::{Beats, Seconds};
|
||||
use crate::effect::EffectDefinition;
|
||||
use crate::layer::{AnyLayer, GroupLayer};
|
||||
use crate::script::ScriptDefinition;
|
||||
@@ -201,17 +200,6 @@ pub struct Document {
|
||||
#[serde(default)]
|
||||
pub time_signature: TimeSignature,
|
||||
|
||||
/// Transport cycle (loop) region, as `(start, end)` in **beats**.
|
||||
///
|
||||
/// Authored in beats so it stays put musically when the tempo changes. `None` = no region set.
|
||||
/// Saved with the project; `#[serde(default)]` keeps older `.beam` files loading.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub cycle_region: Option<(Beats, Beats)>,
|
||||
|
||||
/// Whether the transport loops over `cycle_region`.
|
||||
#[serde(default)]
|
||||
pub cycle_enabled: bool,
|
||||
|
||||
/// Master track (master bus + tempo automation lane).
|
||||
/// Stored separately from the root layer tree; shown in timeline when
|
||||
/// `show_master_track` is enabled in the editor state.
|
||||
@@ -308,8 +296,6 @@ impl Default for Document {
|
||||
height: 1080.0,
|
||||
framerate: 60.0,
|
||||
time_signature: TimeSignature::default(),
|
||||
cycle_region: None,
|
||||
cycle_enabled: false,
|
||||
master_layer: {
|
||||
let mut ml = GroupLayer::new_master(120.0);
|
||||
ml.layer.id = uuid::Uuid::new_v4();
|
||||
@@ -452,27 +438,20 @@ impl Document {
|
||||
/// Returns the end time of the last clip instance across all layers,
|
||||
/// or the document's duration if no clips are found.
|
||||
pub fn calculate_timeline_endpoint(&self) -> f64 {
|
||||
let tempo_map = self.tempo_map();
|
||||
// Accumulated in **beats** (as f64, to keep the recursive helper's `Fn(_, f64) -> f64`
|
||||
// signature); converted to seconds once at the return.
|
||||
let mut max_end_time: f64 = 0.0;
|
||||
|
||||
// End position of a clip instance, in **beats**. Its trimmed content window is in seconds
|
||||
// (scaled by playback speed), so convert that seconds span to beats via the tempo map — the
|
||||
// old code added a seconds duration straight onto the beats start.
|
||||
// Helper function to calculate the end time of a clip instance
|
||||
let calculate_instance_end = |instance: &ClipInstance, clip_duration: f64| -> f64 {
|
||||
let end_beats: Beats = if let Some(timeline_duration) = instance.timeline_duration {
|
||||
instance.timeline_start + timeline_duration
|
||||
let effective_duration = if let Some(timeline_duration) = instance.timeline_duration {
|
||||
// Explicit timeline duration set (may include looping)
|
||||
timeline_duration
|
||||
} else {
|
||||
// `clip_duration` arrives as seconds (the recursive helper's signature), so this
|
||||
// path is the wall-clock one; MIDI content would need resolving against its clip.
|
||||
let trim_end = instance.trim_end.map_or(clip_duration, |t| t.raw());
|
||||
let trimmed_secs =
|
||||
((trim_end - instance.trim_start.raw()) / instance.playback_speed).max(0.0);
|
||||
let start_secs = tempo_map.beats_to_seconds(instance.timeline_start);
|
||||
tempo_map.seconds_to_beats(start_secs + Seconds(trimmed_secs))
|
||||
// Calculate from trim points
|
||||
let trim_end = instance.trim_end.unwrap_or(clip_duration);
|
||||
let trimmed_duration = trim_end - instance.trim_start;
|
||||
trimmed_duration / instance.playback_speed // Adjust for playback speed
|
||||
};
|
||||
end_beats.beats_to_f64()
|
||||
instance.timeline_start + effective_duration
|
||||
};
|
||||
|
||||
// Iterate through all layers to find the maximum end time
|
||||
@@ -488,10 +467,8 @@ impl Document {
|
||||
}
|
||||
crate::layer::AnyLayer::Audio(audio_layer) => {
|
||||
for instance in &audio_layer.clip_instances {
|
||||
// get_clip_duration yields seconds (converting MIDI's beats duration),
|
||||
// which is what the closure expects.
|
||||
if let Some(clip_duration) = self.get_clip_duration(&instance.clip_id) {
|
||||
let end_time = calculate_instance_end(instance, clip_duration.seconds_to_f64());
|
||||
if let Some(clip) = self.audio_clips.get(&instance.clip_id) {
|
||||
let end_time = calculate_instance_end(instance, clip.duration);
|
||||
max_end_time = max_end_time.max(end_time);
|
||||
}
|
||||
}
|
||||
@@ -507,13 +484,13 @@ impl Document {
|
||||
crate::layer::AnyLayer::Effect(effect_layer) => {
|
||||
for instance in &effect_layer.clip_instances {
|
||||
if let Some(clip_duration) = self.get_clip_duration(&instance.clip_id) {
|
||||
let end_time = calculate_instance_end(instance, clip_duration.seconds_to_f64());
|
||||
let end_time = calculate_instance_end(instance, clip_duration);
|
||||
max_end_time = max_end_time.max(end_time);
|
||||
}
|
||||
}
|
||||
}
|
||||
crate::layer::AnyLayer::Raster(_) | crate::layer::AnyLayer::Text(_) => {
|
||||
// Raster and text layers don't have clip instances
|
||||
crate::layer::AnyLayer::Raster(_) => {
|
||||
// Raster layers don't have clip instances
|
||||
}
|
||||
crate::layer::AnyLayer::Group(group) => {
|
||||
// Recurse into group children to find their clip instance endpoints
|
||||
@@ -534,8 +511,8 @@ impl Document {
|
||||
}
|
||||
crate::layer::AnyLayer::Audio(al) => {
|
||||
for inst in &al.clip_instances {
|
||||
if let Some(clip_duration) = doc.get_clip_duration(&inst.clip_id) {
|
||||
*max_end = max_end.max(calc_end(inst, clip_duration.seconds_to_f64()));
|
||||
if let Some(clip) = doc.audio_clips.get(&inst.clip_id) {
|
||||
*max_end = max_end.max(calc_end(inst, clip.duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -549,12 +526,12 @@ impl Document {
|
||||
crate::layer::AnyLayer::Effect(el) => {
|
||||
for inst in &el.clip_instances {
|
||||
if let Some(dur) = doc.get_clip_duration(&inst.clip_id) {
|
||||
*max_end = max_end.max(calc_end(inst, dur.seconds_to_f64()));
|
||||
*max_end = max_end.max(calc_end(inst, dur));
|
||||
}
|
||||
}
|
||||
}
|
||||
crate::layer::AnyLayer::Raster(_) | crate::layer::AnyLayer::Text(_) => {
|
||||
// Raster and text layers don't have clip instances
|
||||
crate::layer::AnyLayer::Raster(_) => {
|
||||
// Raster layers don't have clip instances
|
||||
}
|
||||
crate::layer::AnyLayer::Group(g) => {
|
||||
process_group_children(&g.children, doc, max_end, calc_end);
|
||||
@@ -567,10 +544,9 @@ impl Document {
|
||||
}
|
||||
}
|
||||
|
||||
// Return the max end (converting the beats accumulator to seconds), or the document
|
||||
// duration (already seconds) if no clips were found.
|
||||
// Return the maximum end time, or document duration if no clips found
|
||||
if max_end_time > 0.0 {
|
||||
tempo_map.beats_to_seconds(Beats(max_end_time)).seconds_to_f64()
|
||||
max_end_time
|
||||
} else {
|
||||
self.duration
|
||||
}
|
||||
@@ -783,18 +759,16 @@ impl Document {
|
||||
}
|
||||
|
||||
/// Find the document audio clip (UUID + ref) that owns the given backend pool index.
|
||||
/// A take folder owns one pool file per take, so any of them maps back to the folder.
|
||||
pub fn audio_clip_by_pool_index(&self, pool_index: usize) -> Option<(Uuid, &AudioClip)> {
|
||||
self.audio_clips.iter()
|
||||
.find(|(_, c)| c.owns_audio_pool_index(pool_index))
|
||||
.find(|(_, c)| c.audio_pool_index() == Some(pool_index))
|
||||
.map(|(&id, c)| (id, c))
|
||||
}
|
||||
|
||||
/// Find the document audio clip (UUID + ref) that owns the given backend MIDI clip ID.
|
||||
/// As above, a take folder owns one MIDI clip per take.
|
||||
pub fn audio_clip_by_midi_clip_id(&self, midi_clip_id: u32) -> Option<(Uuid, &AudioClip)> {
|
||||
self.audio_clips.iter()
|
||||
.find(|(_, c)| c.owns_midi_clip_id(midi_clip_id))
|
||||
.find(|(_, c)| c.midi_clip_id() == Some(midi_clip_id))
|
||||
.map(|(&id, c)| (id, c))
|
||||
}
|
||||
|
||||
@@ -916,94 +890,19 @@ impl Document {
|
||||
/// Searches through all clip libraries to find the clip and return its duration.
|
||||
/// For effect definitions, returns `EFFECT_DURATION` (f64::MAX) since effects
|
||||
/// have infinite internal duration.
|
||||
/// A clip's content duration **in the domain its `trim_start`/`trim_end` are measured in**.
|
||||
///
|
||||
/// Content time is domain-polymorphic exactly like `AudioClip::duration`: SECONDS for sampled
|
||||
/// audio, video and vector, but BEATS for MIDI. Anything doing arithmetic against a trim value —
|
||||
/// mapping a timeline position into the clip's content, say — has to work in that same domain,
|
||||
/// and [`Self::get_clip_duration`] can't tell it which: that one always converts to seconds.
|
||||
///
|
||||
/// Returns `None` for unknown clips.
|
||||
pub fn clip_trim_duration(&self, clip_id: &Uuid) -> Option<crate::clip::ClipDuration> {
|
||||
if let Some(clip) = self.audio_clips.get(clip_id) {
|
||||
return Some(clip.content_duration());
|
||||
}
|
||||
// Everything else measures its content in wall-clock seconds.
|
||||
self.get_clip_duration(clip_id).map(crate::clip::ClipDuration::Seconds)
|
||||
}
|
||||
|
||||
/// Find an existing take folder on `layer_id` that a new cycle recording should be appended to.
|
||||
///
|
||||
/// Cycle-recording over a region that already holds a take folder should ADD to that folder
|
||||
/// rather than drop a second clip on top of it — otherwise the new takes are stranded in a
|
||||
/// separate, overlapping clip and can't be auditioned against the ones already there.
|
||||
///
|
||||
/// "The same region" means the instance starts at `loop_start` and its folder was recorded
|
||||
/// against the same loop length. Matching the length too means resizing the cycle region starts
|
||||
/// a fresh folder rather than appending takes of a different length to an existing one, which
|
||||
/// would break the uniform-take invariant comping depends on.
|
||||
///
|
||||
/// `exclude` is the in-progress recording's own instance, which is on the layer but isn't a
|
||||
/// candidate. Returns the instance id.
|
||||
pub fn take_folder_at(
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
loop_start: Beats,
|
||||
loop_len: Beats,
|
||||
exclude: &Uuid,
|
||||
) -> Option<Uuid> {
|
||||
let Some(AnyLayer::Audio(audio_layer)) = self.get_layer(layer_id) else {
|
||||
return None;
|
||||
};
|
||||
const EPS: f64 = 1e-6;
|
||||
audio_layer.clip_instances.iter().find_map(|ci| {
|
||||
if ci.id == *exclude
|
||||
|| ci.takes.is_empty()
|
||||
|| (ci.timeline_start - loop_start).beats_to_f64().abs() > EPS
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let recorded = ci.recorded_loop_beats?;
|
||||
((recorded - loop_len).beats_to_f64().abs() < EPS).then_some(ci.id)
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve a [`ContentTime`] (a trim bound) against the clip it belongs to.
|
||||
///
|
||||
/// The clip is the only thing that knows whether its content is measured in seconds or beats, so
|
||||
/// this is the sanctioned exit from `ContentTime`. Works for every clip kind, not just audio.
|
||||
/// Returns `None` for unknown clips.
|
||||
pub fn resolve_content_time(
|
||||
&self,
|
||||
clip_id: &Uuid,
|
||||
t: daw_backend::ContentTime,
|
||||
) -> Option<crate::clip::ClipDuration> {
|
||||
if let Some(clip) = self.audio_clips.get(clip_id) {
|
||||
return Some(clip.resolve_content_time(t));
|
||||
}
|
||||
if self.vector_clips.contains_key(clip_id)
|
||||
|| self.video_clips.contains_key(clip_id)
|
||||
|| self.effect_definitions.contains_key(clip_id)
|
||||
{
|
||||
// Wall-clock content.
|
||||
return Some(crate::clip::ClipDuration::Seconds(Seconds(t.raw())));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_clip_duration(&self, clip_id: &Uuid) -> Option<Seconds> {
|
||||
pub fn get_clip_duration(&self, clip_id: &Uuid) -> Option<f64> {
|
||||
if let Some(clip) = self.vector_clips.get(clip_id) {
|
||||
if clip.is_group {
|
||||
Some(Seconds(clip.duration))
|
||||
Some(clip.duration)
|
||||
} else {
|
||||
let tempo_map = self.tempo_map();
|
||||
Some(Seconds(clip.content_duration_with(self.framerate, tempo_map, |id| {
|
||||
Some(clip.content_duration_with(self.framerate, tempo_map, |id| {
|
||||
// Resolve nested clip durations (audio, video, other vector clips)
|
||||
if let Some(vc) = self.vector_clips.get(id) {
|
||||
// Avoid deep recursion — use stored duration for nested vector clips
|
||||
Some(vc.content_duration(self.framerate, tempo_map))
|
||||
} else if let Some(ac) = self.audio_clips.get(id) {
|
||||
Some(ac.content_duration().to_seconds(tempo_map).seconds_to_f64())
|
||||
Some(ac.duration)
|
||||
} else if let Some(vc) = self.video_clips.get(id) {
|
||||
Some(vc.duration)
|
||||
} else if self.effect_definitions.contains_key(id) {
|
||||
@@ -1011,25 +910,23 @@ impl Document {
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})))
|
||||
}))
|
||||
}
|
||||
} else if let Some(clip) = self.video_clips.get(clip_id) {
|
||||
Some(Seconds(clip.duration))
|
||||
Some(clip.duration)
|
||||
} else if let Some(clip) = self.audio_clips.get(clip_id) {
|
||||
// Interpret the clip's native-domain duration as wall-clock seconds (MIDI stores
|
||||
// beats, sampled stores seconds — content_duration keeps that straight).
|
||||
Some(clip.content_duration().to_seconds(self.tempo_map()))
|
||||
Some(clip.duration)
|
||||
} else if self.effect_definitions.contains_key(clip_id) {
|
||||
// Effects have infinite internal duration - their timeline length
|
||||
// is controlled by ClipInstance.trim_end
|
||||
Some(Seconds(crate::effect::EFFECT_DURATION))
|
||||
Some(crate::effect::EFFECT_DURATION)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the end position of a clip instance on the timeline, in **beats**.
|
||||
pub fn get_clip_instance_end_time(&self, layer_id: &Uuid, instance_id: &Uuid) -> Option<Beats> {
|
||||
/// Calculate the end time of a clip instance on the timeline
|
||||
pub fn get_clip_instance_end_time(&self, layer_id: &Uuid, instance_id: &Uuid) -> Option<f64> {
|
||||
let layer = self.get_layer(layer_id)?;
|
||||
|
||||
// Find the clip instance
|
||||
@@ -1040,13 +937,16 @@ impl Document {
|
||||
AnyLayer::Effect(effect) => &effect.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
let instance = instances.iter().find(|inst| &inst.id == instance_id)?;
|
||||
// The clip's content duration in ITS OWN domain, so the trims resolve correctly for MIDI.
|
||||
let clip_content = self.clip_trim_duration(&instance.clip_id)?;
|
||||
Some(instance.timeline_start + instance.effective_duration_beats(clip_content, self.tempo_map()))
|
||||
let clip_duration = self.get_clip_duration(&instance.clip_id)?;
|
||||
|
||||
let trim_start = instance.trim_start;
|
||||
let trim_end = instance.trim_end.unwrap_or(clip_duration);
|
||||
let effective_duration = trim_end - trim_start;
|
||||
|
||||
Some(instance.timeline_start + effective_duration)
|
||||
}
|
||||
|
||||
/// Check if a time range overlaps with any existing clip on the layer
|
||||
@@ -1057,8 +957,8 @@ impl Document {
|
||||
pub fn check_overlap_on_layer(
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
start_time: Beats,
|
||||
end_time: Beats,
|
||||
start_time: f64,
|
||||
end_time: f64,
|
||||
exclude_instance_ids: &[Uuid],
|
||||
) -> (bool, Option<Uuid>) {
|
||||
let Some(layer) = self.get_layer(layer_id) else {
|
||||
@@ -1077,7 +977,6 @@ impl Document {
|
||||
AnyLayer::Effect(effect) => &effect.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
for instance in instances {
|
||||
@@ -1086,14 +985,13 @@ impl Document {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate instance extent (accounting for loop_before). Content duration in the clip's
|
||||
// own domain, so the trims resolve correctly for MIDI.
|
||||
let Some(clip_content) = self.clip_trim_duration(&instance.clip_id) else {
|
||||
// Calculate instance extent (accounting for loop_before)
|
||||
let Some(clip_duration) = self.get_clip_duration(&instance.clip_id) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let instance_start = instance.effective_start();
|
||||
let instance_end = instance.timeline_start + instance.effective_duration(clip_content, self.tempo_map());
|
||||
let instance_end = instance.timeline_start + instance.effective_duration(clip_duration, self.tempo_map());
|
||||
|
||||
// Check overlap: start_a < end_b AND start_b < end_a
|
||||
if start_time < instance_end && instance_start < end_time {
|
||||
@@ -1112,14 +1010,14 @@ impl Document {
|
||||
pub fn find_nearest_valid_position(
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
desired_start: Beats,
|
||||
clip_duration: Beats,
|
||||
desired_start: f64,
|
||||
clip_duration: f64,
|
||||
exclude_instance_ids: &[Uuid],
|
||||
) -> Option<Beats> {
|
||||
) -> Option<f64> {
|
||||
let layer = self.get_layer(layer_id)?;
|
||||
|
||||
// Clamp to timeline start (can't go before 0)
|
||||
let desired_start = desired_start.max(Beats::ZERO);
|
||||
let desired_start = desired_start.max(0.0);
|
||||
|
||||
// Vector layers don't need overlap adjustment, but still respect timeline start
|
||||
if matches!(layer, AnyLayer::Vector(_) | AnyLayer::Group(_)) {
|
||||
@@ -1141,16 +1039,15 @@ impl Document {
|
||||
AnyLayer::Vector(_) => return Some(desired_start), // Shouldn't reach here
|
||||
AnyLayer::Group(_) => return Some(desired_start), // Groups don't have own clips
|
||||
AnyLayer::Raster(_) => return Some(desired_start), // Raster layers don't have own clips
|
||||
AnyLayer::Text(_) => return Some(desired_start), // Text layers don't have own clips
|
||||
};
|
||||
|
||||
let mut occupied_ranges: Vec<(Beats, Beats, Uuid)> = Vec::new();
|
||||
let mut occupied_ranges: Vec<(f64, f64, Uuid)> = Vec::new();
|
||||
for instance in instances {
|
||||
if exclude_instance_ids.contains(&instance.id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(clip_dur) = self.clip_trim_duration(&instance.clip_id) {
|
||||
if let Some(clip_dur) = self.get_clip_duration(&instance.clip_id) {
|
||||
let inst_start = instance.effective_start();
|
||||
let inst_end = instance.timeline_start + instance.effective_duration(clip_dur, self.tempo_map());
|
||||
occupied_ranges.push((inst_start, inst_end, instance.id));
|
||||
@@ -1163,7 +1060,7 @@ impl Document {
|
||||
// Find the clip we're overlapping with and try both sides, pick nearest
|
||||
for (occupied_start, occupied_end, _) in &occupied_ranges {
|
||||
if desired_start < *occupied_end && *occupied_start < desired_end {
|
||||
let mut candidates: Vec<Beats> = Vec::new();
|
||||
let mut candidates: Vec<f64> = Vec::new();
|
||||
|
||||
// Try snapping to the right (after this clip)
|
||||
let snap_right = *occupied_end;
|
||||
@@ -1179,8 +1076,8 @@ impl Document {
|
||||
}
|
||||
|
||||
// Try snapping to the left (before this clip)
|
||||
let snap_left = *occupied_start - clip_duration;
|
||||
if snap_left >= Beats::ZERO {
|
||||
let snap_left = occupied_start - clip_duration;
|
||||
if snap_left >= 0.0 {
|
||||
let (overlaps_left, _) = self.check_overlap_on_layer(
|
||||
layer_id,
|
||||
snap_left,
|
||||
@@ -1195,8 +1092,8 @@ impl Document {
|
||||
// Pick the candidate closest to desired_start
|
||||
if !candidates.is_empty() {
|
||||
candidates.sort_by(|a, b| {
|
||||
let dist_a = (*a - desired_start).abs();
|
||||
let dist_b = (*b - desired_start).abs();
|
||||
let dist_a = (a - desired_start).abs();
|
||||
let dist_b = (b - desired_start).abs();
|
||||
dist_a.partial_cmp(&dist_b).unwrap_or(std::cmp::Ordering::Equal)
|
||||
});
|
||||
return Some(candidates[0]);
|
||||
@@ -1206,7 +1103,7 @@ impl Document {
|
||||
|
||||
// If no gap found, try placing at timeline start
|
||||
if occupied_ranges.is_empty() || occupied_ranges[0].0 >= clip_duration {
|
||||
return Some(Beats::ZERO);
|
||||
return Some(0.0);
|
||||
}
|
||||
|
||||
// No valid position found
|
||||
@@ -1218,9 +1115,9 @@ impl Document {
|
||||
pub fn clamp_group_move_offset(
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
group: &[(Uuid, Beats, Beats)], // (instance_id, timeline_start, effective_duration) in beats
|
||||
desired_offset: Beats,
|
||||
) -> Beats {
|
||||
group: &[(Uuid, f64, f64)], // (instance_id, timeline_start, effective_duration)
|
||||
desired_offset: f64,
|
||||
) -> f64 {
|
||||
let Some(layer) = self.get_layer(layer_id) else {
|
||||
return desired_offset;
|
||||
};
|
||||
@@ -1237,16 +1134,15 @@ impl Document {
|
||||
AnyLayer::Vector(v) => &v.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
// Collect non-group clip ranges (beats)
|
||||
let mut non_group: Vec<(Beats, Beats)> = Vec::new();
|
||||
// Collect non-group clip ranges
|
||||
let mut non_group: Vec<(f64, f64)> = Vec::new();
|
||||
for inst in instances {
|
||||
if group_ids.contains(&inst.id) {
|
||||
continue;
|
||||
}
|
||||
if let Some(dur) = self.clip_trim_duration(&inst.clip_id) {
|
||||
if let Some(dur) = self.get_clip_duration(&inst.clip_id) {
|
||||
let start = inst.effective_start();
|
||||
let end = inst.timeline_start + inst.effective_duration(dur, self.tempo_map());
|
||||
non_group.push((start, end));
|
||||
@@ -1263,12 +1159,12 @@ impl Document {
|
||||
|
||||
// Check against non-group clips
|
||||
for &(ns, ne) in &non_group {
|
||||
if clamped < Beats::ZERO {
|
||||
if clamped < 0.0 {
|
||||
// Moving left: if non-group clip end is between our destination and current start
|
||||
if ne <= start && ne > start + clamped {
|
||||
clamped = clamped.max(ne - start);
|
||||
}
|
||||
} else if clamped > Beats::ZERO {
|
||||
} else if clamped > 0.0 {
|
||||
// Moving right: if non-group clip start is between our current end and destination
|
||||
if ns >= end && ns < end + clamped {
|
||||
clamped = clamped.min(ns - end);
|
||||
@@ -1284,25 +1180,23 @@ impl Document {
|
||||
///
|
||||
/// Returns the distance to the nearest clip to the left, or the distance to
|
||||
/// timeline start (0.0) if no clips exist to the left.
|
||||
/// Returns the max leftward trim extension as a content-seconds span (the wall-clock
|
||||
/// length of the timeline gap to the previous clip); the trim domain is seconds.
|
||||
pub fn find_max_trim_extend_left(
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
instance_id: &Uuid,
|
||||
current_timeline_start: Beats,
|
||||
) -> Seconds {
|
||||
current_timeline_start: f64,
|
||||
) -> f64 {
|
||||
let Some(layer) = self.get_layer(layer_id) else {
|
||||
return self.tempo_map().beats_to_seconds(current_timeline_start); // No limit if layer not found
|
||||
return current_timeline_start; // No limit if layer not found
|
||||
};
|
||||
|
||||
// Only check audio, video, and effect layers
|
||||
if matches!(layer, AnyLayer::Vector(_) | AnyLayer::Group(_)) {
|
||||
return self.tempo_map().beats_to_seconds(current_timeline_start); // No limit for vector/group layers
|
||||
return current_timeline_start; // No limit for vector/group layers
|
||||
};
|
||||
|
||||
// Find the nearest clip to the left
|
||||
let mut nearest_end = Beats::ZERO; // Can extend to timeline start by default
|
||||
let mut nearest_end = 0.0; // Can extend to timeline start by default
|
||||
|
||||
let instances: &[ClipInstance] = match layer {
|
||||
AnyLayer::Audio(audio) => &audio.clip_instances,
|
||||
@@ -1311,7 +1205,6 @@ impl Document {
|
||||
AnyLayer::Vector(vector) => &vector.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
for other in instances {
|
||||
@@ -1320,7 +1213,7 @@ impl Document {
|
||||
}
|
||||
|
||||
// Calculate other clip's extent (accounting for loop_before)
|
||||
if let Some(clip_duration) = self.clip_trim_duration(&other.clip_id) {
|
||||
if let Some(clip_duration) = self.get_clip_duration(&other.clip_id) {
|
||||
let other_end = other.timeline_start + other.effective_duration(clip_duration, self.tempo_map());
|
||||
|
||||
// If this clip is to the left and closer than current nearest
|
||||
@@ -1330,27 +1223,27 @@ impl Document {
|
||||
}
|
||||
}
|
||||
|
||||
self.tempo_map().beats_to_seconds(current_timeline_start) - self.tempo_map().beats_to_seconds(nearest_end)
|
||||
current_timeline_start - nearest_end
|
||||
}
|
||||
|
||||
/// Find the maximum amount we can extend a clip to the right without overlapping.
|
||||
/// Find the maximum amount we can extend a clip to the right without overlapping
|
||||
///
|
||||
/// Returns the content-seconds span of the timeline gap to the nearest clip on the
|
||||
/// right, or Seconds(f64::MAX) if none. `current_effective_duration` is beats (timeline).
|
||||
/// Returns the distance to the nearest clip to the right, or f64::MAX if no
|
||||
/// clips exist to the right.
|
||||
pub fn find_max_trim_extend_right(
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
instance_id: &Uuid,
|
||||
current_timeline_start: Beats,
|
||||
current_effective_duration: Beats,
|
||||
) -> Seconds {
|
||||
current_timeline_start: f64,
|
||||
current_effective_duration: f64,
|
||||
) -> f64 {
|
||||
let Some(layer) = self.get_layer(layer_id) else {
|
||||
return Seconds(f64::MAX); // No limit if layer not found
|
||||
return f64::MAX; // No limit if layer not found
|
||||
};
|
||||
|
||||
// Only check audio, video, and effect layers
|
||||
if matches!(layer, AnyLayer::Vector(_) | AnyLayer::Group(_)) {
|
||||
return Seconds(f64::MAX); // No limit for vector/group layers
|
||||
return f64::MAX; // No limit for vector/group layers
|
||||
}
|
||||
|
||||
let instances: &[ClipInstance] = match layer {
|
||||
@@ -1360,10 +1253,9 @@ impl Document {
|
||||
AnyLayer::Vector(vector) => &vector.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
let mut nearest_start = Beats(f64::MAX);
|
||||
let mut nearest_start = f64::MAX;
|
||||
let current_end = current_timeline_start + current_effective_duration;
|
||||
|
||||
for other in instances {
|
||||
@@ -1378,11 +1270,10 @@ impl Document {
|
||||
}
|
||||
}
|
||||
|
||||
if nearest_start == Beats(f64::MAX) {
|
||||
Seconds(f64::MAX) // No clip to the right, can extend freely
|
||||
if nearest_start == f64::MAX {
|
||||
f64::MAX // No clip to the right, can extend freely
|
||||
} else {
|
||||
// Gap between our end and next clip's start, as content seconds.
|
||||
(self.tempo_map().beats_to_seconds(nearest_start) - self.tempo_map().beats_to_seconds(current_end)).max(Seconds::ZERO)
|
||||
(nearest_start - current_end).max(0.0) // Gap between our end and next clip's start
|
||||
}
|
||||
}
|
||||
/// Find the maximum amount we can extend loop_before to the left without overlapping.
|
||||
@@ -1392,8 +1283,8 @@ impl Document {
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
instance_id: &Uuid,
|
||||
current_effective_start: Beats,
|
||||
) -> Beats {
|
||||
current_effective_start: f64,
|
||||
) -> f64 {
|
||||
let Some(layer) = self.get_layer(layer_id) else {
|
||||
return current_effective_start;
|
||||
};
|
||||
@@ -1409,17 +1300,16 @@ impl Document {
|
||||
AnyLayer::Vector(vector) => &vector.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
|
||||
let mut nearest_end = Beats::ZERO;
|
||||
let mut nearest_end = 0.0;
|
||||
|
||||
for other in instances {
|
||||
if &other.id == instance_id {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(clip_duration) = self.clip_trim_duration(&other.clip_id) {
|
||||
if let Some(clip_duration) = self.get_clip_duration(&other.clip_id) {
|
||||
let other_end = other.timeline_start + other.effective_duration(clip_duration, self.tempo_map());
|
||||
|
||||
if other_end <= current_effective_start && other_end > nearest_end {
|
||||
|
||||
@@ -351,7 +351,7 @@ impl EffectDefinition {
|
||||
///
|
||||
/// * `timeline_start` - When the effect starts on the timeline (seconds)
|
||||
/// * `duration` - How long the effect appears on the timeline (seconds)
|
||||
pub fn create_instance(&self, timeline_start: daw_backend::Beats, duration: daw_backend::Beats) -> ClipInstance {
|
||||
pub fn create_instance(&self, timeline_start: f64, duration: f64) -> ClipInstance {
|
||||
ClipInstance::new(self.id)
|
||||
.with_timeline_start(timeline_start)
|
||||
.with_timeline_duration(duration)
|
||||
|
||||
@@ -148,16 +148,11 @@ impl EffectLayer {
|
||||
/// `timeline_start` (beats) to seconds for comparison.
|
||||
pub fn active_clip_instances_at(&self, time_secs: f64, tempo_map: &crate::tempo_map::TempoMap) -> Vec<&ClipInstance> {
|
||||
use crate::effect::EFFECT_DURATION;
|
||||
let time_beats = tempo_map.seconds_to_beats(daw_backend::Seconds(time_secs));
|
||||
let time_beats = tempo_map.inverse_transform(time_secs);
|
||||
self.clip_instances
|
||||
.iter()
|
||||
.filter(|e| {
|
||||
// Effects have an "infinite" wall-clock content length.
|
||||
let end = e.timeline_start
|
||||
+ e.effective_duration(
|
||||
crate::clip::ClipDuration::Seconds(daw_backend::Seconds(EFFECT_DURATION)),
|
||||
tempo_map,
|
||||
);
|
||||
let end = e.timeline_start + e.effective_duration(EFFECT_DURATION, tempo_map);
|
||||
time_beats >= e.timeline_start && time_beats < end
|
||||
})
|
||||
.collect()
|
||||
@@ -223,7 +218,7 @@ mod tests {
|
||||
fn test_add_effect() {
|
||||
let mut layer = EffectLayer::new("Effects");
|
||||
let def = create_test_effect_def();
|
||||
let effect = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let effect = def.create_instance(0.0, 10.0);
|
||||
let effect_id = effect.id;
|
||||
|
||||
let id = layer.add_clip_instance(effect);
|
||||
@@ -238,11 +233,11 @@ mod tests {
|
||||
let def = create_test_effect_def();
|
||||
|
||||
// Effect 1: active from 0 to 5
|
||||
let effect1 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(5.0));
|
||||
let effect1 = def.create_instance(0.0, 5.0);
|
||||
layer.add_clip_instance(effect1);
|
||||
|
||||
// Effect 2: active from 3 to 10
|
||||
let effect2 = def.create_instance(daw_backend::Beats(3.0), daw_backend::Beats(7.0)); // 3.0 + 7.0 = 10.0 end
|
||||
let effect2 = def.create_instance(3.0, 7.0); // 3.0 + 7.0 = 10.0 end
|
||||
layer.add_clip_instance(effect2);
|
||||
|
||||
// At 60 BPM the tempo map is identity (1 beat == 1 second), so the
|
||||
@@ -264,11 +259,11 @@ mod tests {
|
||||
let mut layer = EffectLayer::new("Effects");
|
||||
let def = create_test_effect_def();
|
||||
|
||||
let effect1 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let effect1 = def.create_instance(0.0, 10.0);
|
||||
let id1 = effect1.id;
|
||||
layer.add_clip_instance(effect1);
|
||||
|
||||
let effect2 = def.create_instance(daw_backend::Beats(0.0), daw_backend::Beats(10.0));
|
||||
let effect2 = def.create_instance(0.0, 10.0);
|
||||
let id2 = effect2.id;
|
||||
layer.add_clip_instance(effect2);
|
||||
|
||||
|
||||
@@ -51,54 +51,6 @@ impl AudioFormat {
|
||||
}
|
||||
}
|
||||
|
||||
/// Optional tag metadata written into the exported audio file. Empty fields are omitted. FFmpeg
|
||||
/// maps these standard keys to each container's native tags: ID3v2 (MP3), iTunes/MP4 atoms (M4A),
|
||||
/// Vorbis comments (FLAC), and RIFF INFO (WAV).
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct AudioMetadata {
|
||||
pub title: String,
|
||||
pub artist: String,
|
||||
pub album: String,
|
||||
pub genre: String,
|
||||
pub comment: String,
|
||||
/// Year or full date (written to the `date` tag).
|
||||
pub year: String,
|
||||
/// Track number (written to the `track` tag).
|
||||
pub track: String,
|
||||
}
|
||||
|
||||
impl AudioMetadata {
|
||||
/// True when no field is set (so no metadata need be written).
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.title.is_empty()
|
||||
&& self.artist.is_empty()
|
||||
&& self.album.is_empty()
|
||||
&& self.genre.is_empty()
|
||||
&& self.comment.is_empty()
|
||||
&& self.year.is_empty()
|
||||
&& self.track.is_empty()
|
||||
}
|
||||
|
||||
/// The set (ffmpeg-key, value) pairs for non-empty fields, in a stable order.
|
||||
pub fn pairs(&self) -> Vec<(&'static str, &str)> {
|
||||
let mut v = Vec::new();
|
||||
for (key, val) in [
|
||||
("title", &self.title),
|
||||
("artist", &self.artist),
|
||||
("album", &self.album),
|
||||
("genre", &self.genre),
|
||||
("comment", &self.comment),
|
||||
("date", &self.year),
|
||||
("track", &self.track),
|
||||
] {
|
||||
if !val.is_empty() {
|
||||
v.push((key, val.as_str()));
|
||||
}
|
||||
}
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
/// Audio export settings
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AudioExportSettings {
|
||||
@@ -127,10 +79,6 @@ pub struct AudioExportSettings {
|
||||
|
||||
/// Project BPM (for beat-position scheduling during export)
|
||||
pub bpm: f64,
|
||||
|
||||
/// Tag metadata (title/artist/…) written into the file. Empty = none.
|
||||
#[serde(default)]
|
||||
pub metadata: AudioMetadata,
|
||||
}
|
||||
|
||||
impl Default for AudioExportSettings {
|
||||
@@ -144,7 +92,6 @@ impl Default for AudioExportSettings {
|
||||
start_time: 0.0,
|
||||
end_time: 60.0,
|
||||
bpm: 120.0,
|
||||
metadata: AudioMetadata::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -596,82 +543,6 @@ impl ImageExportSettings {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Animated GIF export ──────────────────────────────────────────────────────
|
||||
|
||||
/// Settings for exporting an animated GIF (multi-frame, palette-quantized, no audio).
|
||||
///
|
||||
/// GIF stores a per-frame delay in centiseconds (1/100 s), so effective frame rate is quantized to
|
||||
/// whole centiseconds — [`Self::frame_delay_ms`] rounds accordingly and the dialog offers sensible
|
||||
/// GIF rates. Each frame is quantized to a 256-color palette by the encoder.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct GifExportSettings {
|
||||
/// Output width in pixels (None = use document width).
|
||||
pub width: Option<u32>,
|
||||
/// Output height in pixels (None = use document height).
|
||||
pub height: Option<u32>,
|
||||
/// Frame rate (fps). Snapped to whole-centisecond delays at encode time.
|
||||
pub framerate: f64,
|
||||
/// Loop the animation forever (GIF `NETSCAPE2.0` infinite loop). False = play once.
|
||||
pub loop_forever: bool,
|
||||
/// Preserve full alpha as GIF 1-bit transparency (pixels below the alpha threshold become the
|
||||
/// transparent index). When false, frames are composited onto an opaque background first.
|
||||
pub transparency: bool,
|
||||
/// How the document is fit into the output frame when aspect ratios differ (default Letterbox).
|
||||
#[serde(default)]
|
||||
pub fit: ExportFitMode,
|
||||
/// Start time in seconds.
|
||||
pub start_time: f64,
|
||||
/// End time in seconds.
|
||||
pub end_time: f64,
|
||||
}
|
||||
|
||||
impl Default for GifExportSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
width: None,
|
||||
height: None,
|
||||
framerate: 15.0,
|
||||
loop_forever: true,
|
||||
transparency: false,
|
||||
fit: ExportFitMode::Letterbox,
|
||||
start_time: 0.0,
|
||||
end_time: 5.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GifExportSettings {
|
||||
pub fn validate(&self) -> Result<(), String> {
|
||||
if let Some(w) = self.width { if w == 0 { return Err("Width must be > 0".into()); } }
|
||||
if let Some(h) = self.height { if h == 0 { return Err("Height must be > 0".into()); } }
|
||||
if self.framerate <= 0.0 {
|
||||
return Err("Framerate must be greater than 0".into());
|
||||
}
|
||||
if self.start_time < 0.0 {
|
||||
return Err("Start time cannot be negative".into());
|
||||
}
|
||||
if self.end_time <= self.start_time {
|
||||
return Err("End time must be greater than start time".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Duration in seconds.
|
||||
pub fn duration(&self) -> f64 { self.end_time - self.start_time }
|
||||
|
||||
/// Total number of frames to render.
|
||||
pub fn total_frames(&self) -> usize {
|
||||
(self.duration() * self.framerate).ceil().max(1.0) as usize
|
||||
}
|
||||
|
||||
/// Per-frame delay in milliseconds, from the framerate (GIF stores this at centisecond
|
||||
/// resolution, so the effective rate is snapped to the nearest 10 ms, min 10 ms).
|
||||
pub fn frame_delay_ms(&self) -> u32 {
|
||||
let ms = 1000.0 / self.framerate;
|
||||
((ms / 10.0).round().max(1.0) * 10.0) as u32
|
||||
}
|
||||
}
|
||||
|
||||
/// Progress updates during export
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ExportProgress {
|
||||
@@ -857,33 +728,6 @@ mod tests {
|
||||
assert_eq!(settings.total_frames(), 300);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gif_frame_delay_and_frames() {
|
||||
// Frame rates that map to clean centisecond delays.
|
||||
let mk = |fps: f64| GifExportSettings { framerate: fps, ..Default::default() };
|
||||
assert_eq!(mk(10.0).frame_delay_ms(), 100); // 100 ms
|
||||
assert_eq!(mk(20.0).frame_delay_ms(), 50); // 50 ms
|
||||
assert_eq!(mk(50.0).frame_delay_ms(), 20); // 20 ms
|
||||
// 15 fps = 66.6 ms rounds to 70 ms (7 cs); 25 fps = 40 ms.
|
||||
assert_eq!(mk(15.0).frame_delay_ms(), 70);
|
||||
assert_eq!(mk(25.0).frame_delay_ms(), 40);
|
||||
// Very high fps clamps to the 10 ms minimum (1 cs).
|
||||
assert_eq!(mk(1000.0).frame_delay_ms(), 10);
|
||||
|
||||
let settings = GifExportSettings { framerate: 20.0, start_time: 0.0, end_time: 3.0, ..Default::default() };
|
||||
assert_eq!(settings.total_frames(), 60);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gif_validate() {
|
||||
let mut s = GifExportSettings::default();
|
||||
assert!(s.validate().is_ok());
|
||||
s.framerate = 0.0;
|
||||
assert!(s.validate().is_err());
|
||||
s = GifExportSettings { start_time: 5.0, end_time: 2.0, ..Default::default() };
|
||||
assert!(s.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_export_progress_percentage() {
|
||||
let progress = ExportProgress::FrameRendered { frame: 50, total: 100 };
|
||||
|
||||
@@ -242,21 +242,6 @@ fn thumbnail_media_id(clip_id: Uuid) -> Uuid {
|
||||
Uuid::from_u128(clip_id.as_u128() ^ SENTINEL)
|
||||
}
|
||||
|
||||
/// Content-hash id for an embedded font row, so identical font files dedupe to one
|
||||
/// row regardless of which / how many text layers use them. A 128-bit id is built
|
||||
/// from two salted 64-bit hashes of the bytes.
|
||||
fn font_media_id(bytes: &[u8]) -> Uuid {
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut h1 = std::collections::hash_map::DefaultHasher::new();
|
||||
bytes.hash(&mut h1);
|
||||
let mut h2 = std::collections::hash_map::DefaultHasher::new();
|
||||
0xF0E1_D2C3_B4A5_9687u64.hash(&mut h2);
|
||||
bytes.hash(&mut h2);
|
||||
let hi = h1.finish() as u128;
|
||||
let lo = h2.finish() as u128;
|
||||
Uuid::from_u128((hi << 64) | lo)
|
||||
}
|
||||
|
||||
/// Derived id for a raster keyframe's low-res proxy row (distinct from the keyframe's
|
||||
/// own full-res `Raster` row, which is keyed by the raw keyframe id).
|
||||
fn raster_proxy_media_id(kf_id: Uuid) -> Uuid {
|
||||
@@ -399,29 +384,16 @@ pub fn save_beam(
|
||||
}
|
||||
|
||||
// --- raster keyframes -> media rows (PNG), keyed by keyframe id ---
|
||||
// Incremental: only (re)encode a keyframe whose pixels changed since the last save.
|
||||
// `kf.dirty` means "current pixels are not yet in the container" (set on any edit,
|
||||
// cleared on a successful save — see main.rs); a clean frame already stored is kept
|
||||
// in place, skipping the PNG re-encode of every resident frame on every save.
|
||||
// (Phase 0 writes all resident frames each save; a disk-dirty flag to skip
|
||||
// unchanged frames in place is deferred to Phase 3.)
|
||||
// Walk ALL layers (incl. nested in groups/clips) so nested raster keyframes
|
||||
// are persisted too, and so `live_media` covers them — matching the load path,
|
||||
// which arms `needs_fault_in` recursively. Top-level-only projects are unaffected.
|
||||
let mut raster_count = 0usize;
|
||||
let mut raster_skipped = 0usize;
|
||||
for layer in document.all_layers() {
|
||||
if let crate::layer::AnyLayer::Raster(rl) = layer {
|
||||
for kf in &rl.keyframes {
|
||||
if !kf.raw_pixels.is_empty() {
|
||||
// Clean + already stored → keep the existing full + proxy rows untouched.
|
||||
if !kf.dirty && txn.media_exists(kf.id)? {
|
||||
live_media.insert(kf.id);
|
||||
let proxy_id = raster_proxy_media_id(kf.id);
|
||||
if txn.media_exists(proxy_id)? {
|
||||
live_media.insert(proxy_id);
|
||||
}
|
||||
raster_skipped += 1;
|
||||
continue;
|
||||
}
|
||||
let img =
|
||||
crate::brush_engine::image_from_raw(kf.raw_pixels.clone(), kf.width, kf.height);
|
||||
match crate::brush_engine::encode_png(&img) {
|
||||
@@ -563,32 +535,6 @@ pub fn save_beam(
|
||||
}
|
||||
let _ = image_count;
|
||||
|
||||
// --- embedded fonts -> media rows (Font), keyed by content hash so identical
|
||||
// fonts dedupe. Embed every non-bundled family used by a text layer so the
|
||||
// document renders faithfully on machines lacking the font. The bundled
|
||||
// three ship with the app and are never embedded. ---
|
||||
{
|
||||
use crate::layer::AnyLayer;
|
||||
let mut families: HashSet<String> = HashSet::new();
|
||||
for layer in document.all_layers() {
|
||||
if let AnyLayer::Text(tl) = layer {
|
||||
let fam = &tl.content.font_family;
|
||||
if !fam.is_empty() && !crate::fonts::is_bundled(fam) {
|
||||
families.insert(fam.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
for fam in families {
|
||||
if let Some(bytes) = crate::fonts::family_font_bytes(&fam) {
|
||||
let id = font_media_id(&bytes);
|
||||
if !txn.media_exists(id)? {
|
||||
txn.put_media_packed(id, MediaKind::Font, &fam, &bytes, MediaMeta::default())?;
|
||||
}
|
||||
live_media.insert(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- orphan cleanup: drop media for removed clips/keyframes ---
|
||||
let removed = txn.retain_media(&live_media)?;
|
||||
|
||||
@@ -621,10 +567,9 @@ pub fn save_beam(
|
||||
}
|
||||
|
||||
eprintln!(
|
||||
"📊 [SAVE_BEAM] ✅ Saved {} audio + {} raster media ({} unchanged frames skipped), {} orphans removed, in {:.2}ms",
|
||||
"📊 [SAVE_BEAM] ✅ Saved {} audio + {} raster media, {} orphans removed, in {:.2}ms",
|
||||
audio_pool_entries.len(),
|
||||
raster_count,
|
||||
raster_skipped,
|
||||
removed,
|
||||
fn_start.elapsed().as_secs_f64() * 1000.0
|
||||
);
|
||||
@@ -653,17 +598,6 @@ fn load_beam_sqlite(path: &Path) -> Result<LoadedProject, String> {
|
||||
eprintln!("📊 [LOAD_BEAM] Starting load_beam() (SQLite container)...");
|
||||
|
||||
let archive = BeamArchive::open(path)?;
|
||||
|
||||
// Register document-embedded fonts into the runtime font collection so text
|
||||
// layers referencing them render faithfully even if the host lacks the font.
|
||||
if let Ok(font_ids) = archive.media_ids_of_kind(MediaKind::Font) {
|
||||
for id in font_ids {
|
||||
if let Ok(bytes) = archive.read_media_full(id) {
|
||||
crate::fonts::register_embedded(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let json = archive.get_project_json()?;
|
||||
let beam_project: BeamProject = serde_json::from_str(&json)
|
||||
.map_err(|e| format!("Failed to deserialize project.json: {}", e))?;
|
||||
|
||||
@@ -1,296 +0,0 @@
|
||||
//! Font registry + text layout for text layers.
|
||||
//!
|
||||
//! Wraps a thread-local parley [`FontContext`]/[`LayoutContext`]. Three fonts are
|
||||
//! bundled (serif, sans-serif, monospaced) so text renders deterministically even
|
||||
//! offline; system fonts are also enumerated (parley's fontique source) and
|
||||
//! document-embedded fonts can be registered at load time (see `file_io`).
|
||||
//!
|
||||
//! The same `linebender_resource_handle` crate (0.1.1) backs both vello's
|
||||
//! `peniko::FontData` and parley's `FontData`, so a glyph run's `run.font()` can be
|
||||
//! handed straight to `vello::Scene::draw_glyphs` with no conversion.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
|
||||
use parley::{
|
||||
Alignment, AlignmentOptions, FontContext, FontFamily, FontFamilyName, Layout, LayoutContext,
|
||||
PositionedLayoutItem, StyleProperty,
|
||||
};
|
||||
use vello::peniko::Blob;
|
||||
|
||||
use crate::text_layer::{TextAlign, TextContent};
|
||||
|
||||
// ── Bundled fonts (SIL OFL; vendored under assets/fonts) ─────────────────────
|
||||
static BUNDLED_SANS: &[u8] = include_bytes!("../assets/fonts/LiberationSans-Regular.ttf");
|
||||
static BUNDLED_SERIF: &[u8] = include_bytes!("../assets/fonts/LiberationSerif-Regular.ttf");
|
||||
static BUNDLED_MONO: &[u8] = include_bytes!("../assets/fonts/LiberationMono-Regular.ttf");
|
||||
|
||||
/// Parley requires a brush type, but glyph color is applied by vello at draw time,
|
||||
/// so we use a zero-sized placeholder.
|
||||
#[derive(Clone, Copy, PartialEq, Default, Debug)]
|
||||
pub struct NoBrush;
|
||||
|
||||
struct FontStore {
|
||||
fcx: FontContext,
|
||||
lcx: LayoutContext<NoBrush>,
|
||||
/// Registered family names of the three bundled fonts (sans, serif, mono).
|
||||
bundled: Vec<String>,
|
||||
/// The default family (bundled sans-serif), used when `font_family` is empty.
|
||||
default_family: String,
|
||||
}
|
||||
|
||||
impl FontStore {
|
||||
fn new() -> Self {
|
||||
let mut fcx = FontContext::new();
|
||||
let mut bundled = Vec::new();
|
||||
for bytes in [BUNDLED_SANS, BUNDLED_SERIF, BUNDLED_MONO] {
|
||||
let blob = Blob::new(Arc::new(bytes) as Arc<dyn AsRef<[u8]> + Send + Sync>);
|
||||
for (family_id, _) in fcx.collection.register_fonts(blob, None) {
|
||||
if let Some(name) = fcx.collection.family_name(family_id) {
|
||||
let name = name.to_string();
|
||||
if !bundled.contains(&name) {
|
||||
bundled.push(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let default_family = bundled.first().cloned().unwrap_or_else(|| "sans-serif".to_string());
|
||||
Self { fcx, lcx: LayoutContext::new(), bundled, default_family }
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static STORE: RefCell<FontStore> = RefCell::new(FontStore::new());
|
||||
}
|
||||
|
||||
/// The default family name (bundled sans-serif), used when a text layer's
|
||||
/// `font_family` is empty.
|
||||
pub fn default_family() -> String {
|
||||
STORE.with(|s| s.borrow().default_family.clone())
|
||||
}
|
||||
|
||||
/// Whether `family` is one of the three bundled families (which therefore must
|
||||
/// never be embedded into a `.beam`).
|
||||
pub fn is_bundled(family: &str) -> bool {
|
||||
STORE.with(|s| s.borrow().bundled.iter().any(|f| f == family))
|
||||
}
|
||||
|
||||
/// True if a shorter word-prefix of `name` is itself a family in `set` — i.e. `name`
|
||||
/// is a variant of a base family (e.g. "Noto Sans Arabic" when "Noto Sans" exists).
|
||||
/// Used to collapse the many script/region-specific fonts (mostly Noto) into their
|
||||
/// base family in the picker; parley's fallback still resolves the right script font
|
||||
/// automatically when rendering non-Latin text.
|
||||
fn is_variant_of_listed_base(name: &str, set: &std::collections::HashSet<&str>) -> bool {
|
||||
let words: Vec<&str> = name.split(' ').collect();
|
||||
for k in 1..words.len() {
|
||||
if set.contains(words[..k].join(" ").as_str()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Available family names for the info-panel picker: the three bundled families first,
|
||||
/// then a consolidated, alphabetical list of system families — script/region variants
|
||||
/// whose base family is present are dropped (monospace variants are kept).
|
||||
pub fn families() -> Vec<String> {
|
||||
STORE.with(|s| {
|
||||
let s = &mut *s.borrow_mut();
|
||||
|
||||
let mut system: Vec<String> =
|
||||
s.fcx.collection.family_names().map(|n| n.to_string()).collect();
|
||||
system.sort();
|
||||
system.dedup();
|
||||
let set: std::collections::HashSet<&str> = system.iter().map(|x| x.as_str()).collect();
|
||||
|
||||
let mut out = s.bundled.clone();
|
||||
for name in &system {
|
||||
if out.iter().any(|b| b == name) {
|
||||
continue;
|
||||
}
|
||||
// Keep base families and monospace variants; drop script/region variants
|
||||
// whose base family is already in the list.
|
||||
if name.contains("Mono") || !is_variant_of_listed_base(name, &set) {
|
||||
out.push(name.clone());
|
||||
}
|
||||
}
|
||||
out
|
||||
})
|
||||
}
|
||||
|
||||
/// Register a document-embedded font (raw TTF/OTF bytes) into the runtime font
|
||||
/// collection so text layers referencing its family resolve to it. Returns the
|
||||
/// registered family names.
|
||||
pub fn register_embedded(bytes: Vec<u8>) -> Vec<String> {
|
||||
STORE.with(|s| {
|
||||
let s = &mut *s.borrow_mut();
|
||||
let blob = Blob::new(Arc::new(bytes) as Arc<dyn AsRef<[u8]> + Send + Sync>);
|
||||
let mut names = Vec::new();
|
||||
for (family_id, _) in s.fcx.collection.register_fonts(blob, None) {
|
||||
if let Some(name) = s.fcx.collection.family_name(family_id) {
|
||||
names.push(name.to_string());
|
||||
}
|
||||
}
|
||||
names
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `family` currently resolves to a registered family (bundled, system,
|
||||
/// or embedded). Used to flag "missing font" on load.
|
||||
pub fn family_available(family: &str) -> bool {
|
||||
if family.is_empty() {
|
||||
return true;
|
||||
}
|
||||
STORE.with(|s| {
|
||||
let s = &mut *s.borrow_mut();
|
||||
s.fcx.collection.family_id(family).is_some()
|
||||
})
|
||||
}
|
||||
|
||||
/// The raw bytes of the font file that `family` resolves to (for embedding into a
|
||||
/// `.beam`). Returns `None` if the family resolves to no glyphs. Lays out a single
|
||||
/// glyph and reads the resolved run's font blob (the same `linebender_resource_handle`
|
||||
/// blob vello uses).
|
||||
pub fn family_font_bytes(family: &str) -> Option<Vec<u8>> {
|
||||
let content = TextContent {
|
||||
text: "A".to_string(),
|
||||
font_family: family.to_string(),
|
||||
..TextContent::default()
|
||||
};
|
||||
with_layout(&content, 10_000.0, |layout| {
|
||||
for line in layout.lines() {
|
||||
for item in line.items() {
|
||||
if let PositionedLayoutItem::GlyphRun(run) = item {
|
||||
return Some(run.run().font().data.data().to_vec());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
})
|
||||
}
|
||||
|
||||
// ── Background font preloading ───────────────────────────────────────────────
|
||||
//
|
||||
// Loading a family's bytes (`family_font_bytes`) is the expensive part (font file IO +
|
||||
// shaping). Doing it for the whole picker on the UI thread causes hitches, so a
|
||||
// background thread (started at app launch) loads every picker family's bytes ahead of
|
||||
// time into a shared map. The UI then only has to *register* them with its renderer
|
||||
// (cheap), and synchronously loads the few stragglers only if needed before they're ready.
|
||||
|
||||
struct PreloadState {
|
||||
loaded: HashMap<String, Vec<u8>>,
|
||||
started: bool,
|
||||
done: bool,
|
||||
}
|
||||
|
||||
fn preload() -> &'static Mutex<PreloadState> {
|
||||
static P: OnceLock<Mutex<PreloadState>> = OnceLock::new();
|
||||
P.get_or_init(|| Mutex::new(PreloadState {
|
||||
loaded: HashMap::new(),
|
||||
started: false,
|
||||
done: false,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Start (once) a background thread that loads every picker font's bytes. Idempotent;
|
||||
/// safe to call every frame or from app startup.
|
||||
pub fn start_preload() {
|
||||
{
|
||||
let mut p = preload().lock().unwrap();
|
||||
if p.started {
|
||||
return;
|
||||
}
|
||||
p.started = true;
|
||||
}
|
||||
let _ = std::thread::Builder::new()
|
||||
.name("font-preload".into())
|
||||
.spawn(|| {
|
||||
// This thread gets its own thread-local FontContext (enumerates system fonts
|
||||
// off the UI thread). Bytes are plain `Vec<u8>` (Send) handed back via the map.
|
||||
for fam in families() {
|
||||
if let Some(bytes) = family_font_bytes(&fam) {
|
||||
preload().lock().unwrap().loaded.insert(fam, bytes);
|
||||
}
|
||||
}
|
||||
preload().lock().unwrap().done = true;
|
||||
});
|
||||
}
|
||||
|
||||
/// Remove and return a preloaded font's bytes if the background thread has them ready.
|
||||
pub fn take_preloaded(family: &str) -> Option<Vec<u8>> {
|
||||
preload().lock().unwrap().loaded.remove(family)
|
||||
}
|
||||
|
||||
/// Whether the background preloader has finished loading every family.
|
||||
pub fn preload_done() -> bool {
|
||||
preload().lock().unwrap().done
|
||||
}
|
||||
|
||||
/// Caret rectangle (x0, y0, x1, y1, in layout space relative to the box origin) for
|
||||
/// `byte_index` into `content.text`, wrapped to `max_width`.
|
||||
pub fn caret_geometry(content: &TextContent, max_width: f32, byte_index: usize) -> Option<(f64, f64, f64, f64)> {
|
||||
let caret_w = (content.font_size as f32 * 0.06).max(1.0);
|
||||
with_layout(content, max_width, |layout| {
|
||||
let cur = parley::Cursor::from_byte_index(layout, byte_index, parley::Affinity::Downstream);
|
||||
let bb = cur.geometry(layout, caret_w);
|
||||
Some((bb.x0, bb.y0, bb.x1, bb.y1))
|
||||
})
|
||||
}
|
||||
|
||||
/// Selection highlight rectangles (each x0, y0, x1, y1 in layout space) for the byte
|
||||
/// range `[start, end)` into `content.text`, wrapped to `max_width`.
|
||||
pub fn selection_geometry(content: &TextContent, max_width: f32, start: usize, end: usize) -> Vec<(f64, f64, f64, f64)> {
|
||||
if start == end {
|
||||
return Vec::new();
|
||||
}
|
||||
with_layout(content, max_width, |layout| {
|
||||
let anchor = parley::Cursor::from_byte_index(layout, start, parley::Affinity::Downstream);
|
||||
let focus = parley::Cursor::from_byte_index(layout, end, parley::Affinity::Downstream);
|
||||
let sel = parley::Selection::new(anchor, focus);
|
||||
sel.geometry(layout)
|
||||
.into_iter()
|
||||
.map(|(bb, _)| (bb.x0, bb.y0, bb.x1, bb.y1))
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
fn alignment_of(a: TextAlign) -> Alignment {
|
||||
match a {
|
||||
TextAlign::Left => Alignment::Left,
|
||||
TextAlign::Center => Alignment::Center,
|
||||
TextAlign::Right => Alignment::Right,
|
||||
TextAlign::Justify => Alignment::Justify,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a parley layout for `content` wrapped to `max_width` (document units),
|
||||
/// then invoke `f` with it. The layout is rebuilt on each call (v1: no cache).
|
||||
pub fn with_layout<R>(
|
||||
content: &TextContent,
|
||||
max_width: f32,
|
||||
f: impl FnOnce(&Layout<NoBrush>) -> R,
|
||||
) -> R {
|
||||
STORE.with(|s| {
|
||||
let s = &mut *s.borrow_mut();
|
||||
let default_family = s.default_family.clone();
|
||||
let FontStore { fcx, lcx, .. } = s;
|
||||
|
||||
let mut builder = lcx.ranged_builder(fcx, &content.text, 1.0, true);
|
||||
builder.push_default(StyleProperty::FontSize(content.font_size as f32));
|
||||
|
||||
let family_name = if content.font_family.is_empty() {
|
||||
default_family
|
||||
} else {
|
||||
content.font_family.clone()
|
||||
};
|
||||
let family = FontFamily::Single(FontFamilyName::Named(Cow::Owned(family_name)));
|
||||
builder.push_default(StyleProperty::FontFamily(family));
|
||||
|
||||
let mut layout = builder.build(&content.text);
|
||||
layout.break_all_lines(Some(max_width));
|
||||
layout.align(alignment_of(content.align), AlignmentOptions::default());
|
||||
f(&layout)
|
||||
})
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
//! Provides functions for testing if points or rectangles intersect with
|
||||
//! vector graph elements and clip instances, taking into account transform hierarchies.
|
||||
|
||||
use crate::clip::{ClipDuration, ClipInstance};
|
||||
use crate::clip::ClipInstance;
|
||||
use crate::vector_graph::{VertexId, EdgeId, FillId};
|
||||
use crate::layer::VectorLayer;
|
||||
use crate::shape::Shape;
|
||||
@@ -260,20 +260,16 @@ pub fn hit_test_clip_instances(
|
||||
for clip_instance in clip_instances.iter().rev() {
|
||||
// Check time bounds: skip clip instances not active at this time
|
||||
// timeline_start/instance_end are in beats; convert timeline_time (seconds) to beats.
|
||||
// Hit-testing runs on vector/raster content, which is wall-clock seconds.
|
||||
let clip_duration = ClipDuration::Seconds(
|
||||
document.get_clip_duration(&clip_instance.clip_id).unwrap_or(daw_backend::Seconds::ZERO),
|
||||
);
|
||||
let clip_duration = document.get_clip_duration(&clip_instance.clip_id).unwrap_or(0.0);
|
||||
let instance_end = clip_instance.timeline_start + clip_instance.effective_duration(clip_duration, tempo_map);
|
||||
let timeline_beats = tempo_map.seconds_to_beats(daw_backend::Seconds(timeline_time));
|
||||
let timeline_beats = tempo_map.inverse_transform(timeline_time);
|
||||
if timeline_beats < clip_instance.timeline_start || timeline_beats >= instance_end {
|
||||
continue;
|
||||
}
|
||||
|
||||
// clip_time is in seconds; offset from clip start (in seconds) + trim_start (seconds)
|
||||
let start_secs = tempo_map.beats_to_seconds(clip_instance.timeline_start).seconds_to_f64();
|
||||
let clip_time =
|
||||
((timeline_time - start_secs) * clip_instance.playback_speed) + clip_instance.trim_start.raw();
|
||||
let start_secs = tempo_map.transform(clip_instance.timeline_start);
|
||||
let clip_time = ((timeline_time - start_secs) * clip_instance.playback_speed) + clip_instance.trim_start;
|
||||
|
||||
let content_bounds = if let Some(vector_clip) = document.get_vector_clip(&clip_instance.clip_id) {
|
||||
vector_clip.calculate_content_bounds(document, clip_time)
|
||||
@@ -308,19 +304,15 @@ pub fn hit_test_clip_instances_in_rect(
|
||||
for clip_instance in clip_instances {
|
||||
// Check time bounds: skip clip instances not active at this time
|
||||
// timeline_start/instance_end are in beats; convert timeline_time (seconds) to beats.
|
||||
// Hit-testing runs on vector/raster content, which is wall-clock seconds.
|
||||
let clip_duration = ClipDuration::Seconds(
|
||||
document.get_clip_duration(&clip_instance.clip_id).unwrap_or(daw_backend::Seconds::ZERO),
|
||||
);
|
||||
let clip_duration = document.get_clip_duration(&clip_instance.clip_id).unwrap_or(0.0);
|
||||
let instance_end = clip_instance.timeline_start + clip_instance.effective_duration(clip_duration, tempo_map);
|
||||
let timeline_beats = tempo_map.seconds_to_beats(daw_backend::Seconds(timeline_time));
|
||||
let timeline_beats = tempo_map.inverse_transform(timeline_time);
|
||||
if timeline_beats < clip_instance.timeline_start || timeline_beats >= instance_end {
|
||||
continue;
|
||||
}
|
||||
|
||||
let start_secs = tempo_map.beats_to_seconds(clip_instance.timeline_start).seconds_to_f64();
|
||||
let clip_time =
|
||||
((timeline_time - start_secs) * clip_instance.playback_speed) + clip_instance.trim_start.raw();
|
||||
let start_secs = tempo_map.transform(clip_instance.timeline_start);
|
||||
let clip_time = ((timeline_time - start_secs) * clip_instance.playback_speed) + clip_instance.trim_start;
|
||||
|
||||
let content_bounds = if let Some(vector_clip) = document.get_vector_clip(&clip_instance.clip_id) {
|
||||
vector_clip.calculate_content_bounds(document, clip_time)
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::vector_graph::VectorGraph;
|
||||
use crate::effect_layer::EffectLayer;
|
||||
use crate::object::ShapeInstance;
|
||||
use crate::raster_layer::RasterLayer;
|
||||
use crate::text_layer::TextLayer;
|
||||
use crate::shape::Shape;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
@@ -32,8 +31,6 @@ pub enum LayerType {
|
||||
Group,
|
||||
/// Raster pixel-buffer painting layer
|
||||
Raster,
|
||||
/// Text layer (a single editable text box)
|
||||
Text,
|
||||
}
|
||||
|
||||
/// Common trait for all layer types
|
||||
@@ -869,7 +866,6 @@ impl GroupLayer {
|
||||
AnyLayer::Effect(l) => &l.clip_instances,
|
||||
AnyLayer::Group(_) => &[], // no nested groups
|
||||
AnyLayer::Raster(_) => &[], // raster layers have no clip instances
|
||||
AnyLayer::Text(_) => &[], // raster layers have no clip instances
|
||||
};
|
||||
for ci in instances {
|
||||
result.push((child_id, ci));
|
||||
@@ -888,7 +884,6 @@ pub enum AnyLayer {
|
||||
Effect(EffectLayer),
|
||||
Group(GroupLayer),
|
||||
Raster(RasterLayer),
|
||||
Text(TextLayer),
|
||||
}
|
||||
|
||||
impl LayerTrait for AnyLayer {
|
||||
@@ -900,7 +895,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.id(),
|
||||
AnyLayer::Group(l) => l.id(),
|
||||
AnyLayer::Raster(l) => l.id(),
|
||||
AnyLayer::Text(l) => l.id(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -912,7 +906,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.name(),
|
||||
AnyLayer::Group(l) => l.name(),
|
||||
AnyLayer::Raster(l) => l.name(),
|
||||
AnyLayer::Text(l) => l.name(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -924,7 +917,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.set_name(name),
|
||||
AnyLayer::Group(l) => l.set_name(name),
|
||||
AnyLayer::Raster(l) => l.set_name(name),
|
||||
AnyLayer::Text(l) => l.set_name(name),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -936,7 +928,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.has_custom_name(),
|
||||
AnyLayer::Group(l) => l.has_custom_name(),
|
||||
AnyLayer::Raster(l) => l.has_custom_name(),
|
||||
AnyLayer::Text(l) => l.has_custom_name(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -948,7 +939,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.set_has_custom_name(custom),
|
||||
AnyLayer::Group(l) => l.set_has_custom_name(custom),
|
||||
AnyLayer::Raster(l) => l.set_has_custom_name(custom),
|
||||
AnyLayer::Text(l) => l.set_has_custom_name(custom),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -960,7 +950,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.visible(),
|
||||
AnyLayer::Group(l) => l.visible(),
|
||||
AnyLayer::Raster(l) => l.visible(),
|
||||
AnyLayer::Text(l) => l.visible(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -972,7 +961,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.set_visible(visible),
|
||||
AnyLayer::Group(l) => l.set_visible(visible),
|
||||
AnyLayer::Raster(l) => l.set_visible(visible),
|
||||
AnyLayer::Text(l) => l.set_visible(visible),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -984,7 +972,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.opacity(),
|
||||
AnyLayer::Group(l) => l.opacity(),
|
||||
AnyLayer::Raster(l) => l.opacity(),
|
||||
AnyLayer::Text(l) => l.opacity(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -996,7 +983,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.set_opacity(opacity),
|
||||
AnyLayer::Group(l) => l.set_opacity(opacity),
|
||||
AnyLayer::Raster(l) => l.set_opacity(opacity),
|
||||
AnyLayer::Text(l) => l.set_opacity(opacity),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1008,7 +994,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.volume(),
|
||||
AnyLayer::Group(l) => l.volume(),
|
||||
AnyLayer::Raster(l) => l.volume(),
|
||||
AnyLayer::Text(l) => l.volume(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1020,7 +1005,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.set_volume(volume),
|
||||
AnyLayer::Group(l) => l.set_volume(volume),
|
||||
AnyLayer::Raster(l) => l.set_volume(volume),
|
||||
AnyLayer::Text(l) => l.set_volume(volume),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1032,7 +1016,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.muted(),
|
||||
AnyLayer::Group(l) => l.muted(),
|
||||
AnyLayer::Raster(l) => l.muted(),
|
||||
AnyLayer::Text(l) => l.muted(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1044,7 +1027,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.set_muted(muted),
|
||||
AnyLayer::Group(l) => l.set_muted(muted),
|
||||
AnyLayer::Raster(l) => l.set_muted(muted),
|
||||
AnyLayer::Text(l) => l.set_muted(muted),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1056,7 +1038,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.soloed(),
|
||||
AnyLayer::Group(l) => l.soloed(),
|
||||
AnyLayer::Raster(l) => l.soloed(),
|
||||
AnyLayer::Text(l) => l.soloed(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1068,7 +1049,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.set_soloed(soloed),
|
||||
AnyLayer::Group(l) => l.set_soloed(soloed),
|
||||
AnyLayer::Raster(l) => l.set_soloed(soloed),
|
||||
AnyLayer::Text(l) => l.set_soloed(soloed),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1080,7 +1060,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.locked(),
|
||||
AnyLayer::Group(l) => l.locked(),
|
||||
AnyLayer::Raster(l) => l.locked(),
|
||||
AnyLayer::Text(l) => l.locked(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1092,7 +1071,6 @@ impl LayerTrait for AnyLayer {
|
||||
AnyLayer::Effect(l) => l.set_locked(locked),
|
||||
AnyLayer::Group(l) => l.set_locked(locked),
|
||||
AnyLayer::Raster(l) => l.set_locked(locked),
|
||||
AnyLayer::Text(l) => l.set_locked(locked),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1107,7 +1085,6 @@ impl AnyLayer {
|
||||
AnyLayer::Effect(l) => &l.layer,
|
||||
AnyLayer::Group(l) => &l.layer,
|
||||
AnyLayer::Raster(l) => &l.layer,
|
||||
AnyLayer::Text(l) => &l.layer,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1120,7 +1097,6 @@ impl AnyLayer {
|
||||
AnyLayer::Effect(l) => &mut l.layer,
|
||||
AnyLayer::Group(l) => &mut l.layer,
|
||||
AnyLayer::Raster(l) => &mut l.layer,
|
||||
AnyLayer::Text(l) => &mut l.layer,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,8 +54,6 @@ pub mod svg_export;
|
||||
pub mod snap;
|
||||
pub mod webcam;
|
||||
pub mod raster_layer;
|
||||
pub mod text_layer;
|
||||
pub mod fonts;
|
||||
pub mod raster_store;
|
||||
pub mod brush_settings;
|
||||
pub mod brush_engine;
|
||||
|
||||
@@ -9,9 +9,8 @@
|
||||
//! The compositing mode enables proper per-layer opacity, blend modes, and effects.
|
||||
|
||||
use crate::animation::TransformProperty;
|
||||
use crate::clip::{ClipDuration, ClipInstance, ImageAsset};
|
||||
use crate::clip::{ClipInstance, ImageAsset};
|
||||
use crate::document::Document;
|
||||
use daw_backend::Seconds;
|
||||
use crate::gpu::BlendMode;
|
||||
use crate::layer::{AnyLayer, LayerTrait, VectorLayer};
|
||||
use kurbo::Affine;
|
||||
@@ -568,8 +567,7 @@ pub fn render_layer_isolated(
|
||||
let tempo_map = document.tempo_map();
|
||||
for clip_instance in &video_layer.clip_instances {
|
||||
let Some(video_clip) = document.video_clips.get(&clip_instance.clip_id) else { continue };
|
||||
let Some(clip_time) = clip_instance.remap_time(Seconds(time), ClipDuration::Seconds(Seconds(video_clip.duration)), tempo_map) else { continue };
|
||||
let clip_time = clip_time.seconds_to_f64();
|
||||
let Some(clip_time) = clip_instance.remap_time(time, video_clip.duration, tempo_map) else { continue };
|
||||
let Some(frame) = video_mgr.get_frame(&clip_instance.clip_id, clip_time, target_w, target_h) else { continue };
|
||||
|
||||
// Evaluate animated transform properties.
|
||||
@@ -700,11 +698,6 @@ pub fn render_layer_isolated(
|
||||
};
|
||||
}
|
||||
}
|
||||
AnyLayer::Text(text_layer) => {
|
||||
// Text composites as vector geometry (glyphs in the Vello scene).
|
||||
rendered.has_content =
|
||||
render_text_layer_to_scene(text_layer, time, &mut rendered.scene, base_transform);
|
||||
}
|
||||
}
|
||||
|
||||
rendered
|
||||
@@ -764,57 +757,6 @@ fn render_raster_layer_to_scene(
|
||||
scene.fill(Fill::NonZero, base_transform, &brush, None, &canvas_rect);
|
||||
}
|
||||
|
||||
/// Render a text layer's glyphs into a Vello scene.
|
||||
///
|
||||
/// Text is laid out with parley (wrapped to the box width) and drawn via
|
||||
/// `Scene::draw_glyphs`. The box origin offsets the whole layout; `base_transform`
|
||||
/// carries the layer/clip-instance transform and camera. Returns whether anything
|
||||
/// was drawn.
|
||||
fn render_text_layer_to_scene(
|
||||
layer: &crate::text_layer::TextLayer,
|
||||
time: f64,
|
||||
scene: &mut Scene,
|
||||
base_transform: Affine,
|
||||
) -> bool {
|
||||
let content = layer.content_at(time);
|
||||
if content.text.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let color = vello::peniko::Color::new(content.color);
|
||||
let origin = Affine::translate((layer.box_origin.x, layer.box_origin.y));
|
||||
let mut drew = false;
|
||||
crate::fonts::with_layout(content, layer.box_width as f32, |layout| {
|
||||
for line in layout.lines() {
|
||||
for item in line.items() {
|
||||
let parley::PositionedLayoutItem::GlyphRun(glyph_run) = item else { continue };
|
||||
let run = glyph_run.run();
|
||||
let font = run.font();
|
||||
let font_size = run.font_size();
|
||||
let synthesis = run.synthesis();
|
||||
let glyph_xform = synthesis
|
||||
.skew()
|
||||
.map(|angle| Affine::skew((angle as f64).to_radians().tan(), 0.0));
|
||||
drew = true;
|
||||
scene
|
||||
.draw_glyphs(font)
|
||||
.font_size(font_size)
|
||||
.brush(color)
|
||||
.transform(base_transform * origin)
|
||||
.glyph_transform(glyph_xform)
|
||||
.draw(
|
||||
Fill::NonZero,
|
||||
glyph_run.positioned_glyphs().map(|g| vello::Glyph {
|
||||
id: g.id as u32,
|
||||
x: g.x,
|
||||
y: g.y,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
drew
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Legacy Single-Scene Rendering (kept for backwards compatibility)
|
||||
// ============================================================================
|
||||
@@ -942,11 +884,6 @@ fn render_layer(
|
||||
if let Some(ex) = extract.as_deref_mut() { ex.drew_other = true; }
|
||||
render_raster_layer_to_scene(raster_layer, time, scene, base_transform);
|
||||
}
|
||||
AnyLayer::Text(text_layer) => {
|
||||
// Text is non-video content — force the Vello fallback if extracting.
|
||||
if let Some(ex) = extract.as_deref_mut() { ex.drew_other = true; }
|
||||
render_text_layer_to_scene(text_layer, time, scene, base_transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -977,7 +914,7 @@ pub fn render_single_clip_instance(
|
||||
.filter(|vc| vc.is_group)
|
||||
.map(|_| {
|
||||
let frame_duration = 1.0 / document.framerate;
|
||||
vector_layer.group_visibility_end(&clip_instance.id, document.tempo_map().beats_to_seconds(clip_instance.timeline_start).seconds_to_f64(), frame_duration)
|
||||
vector_layer.group_visibility_end(&clip_instance.id, clip_instance.timeline_start, frame_duration)
|
||||
});
|
||||
|
||||
render_clip_instance(
|
||||
@@ -1012,21 +949,18 @@ fn render_clip_instance(
|
||||
let clip_time = if vector_clip.is_group {
|
||||
// Groups are static — visible from timeline_start to the next keyframe boundary.
|
||||
// timeline_start is in beats; group_end_time is in seconds (render time).
|
||||
let start_secs = tempo_map.beats_to_seconds(clip_instance.timeline_start).seconds_to_f64();
|
||||
let start_secs = tempo_map.transform(clip_instance.timeline_start);
|
||||
let end = group_end_time.unwrap_or(start_secs);
|
||||
if time < start_secs || time >= end {
|
||||
return;
|
||||
}
|
||||
0.0
|
||||
} else {
|
||||
// A vector clip's content is wall-clock seconds.
|
||||
let clip_dur = ClipDuration::Seconds(
|
||||
document.get_clip_duration(&vector_clip.id).unwrap_or(Seconds(vector_clip.duration)),
|
||||
);
|
||||
let Some(t) = clip_instance.remap_time(Seconds(time), clip_dur, tempo_map) else {
|
||||
let clip_dur = document.get_clip_duration(&vector_clip.id).unwrap_or(vector_clip.duration);
|
||||
let Some(t) = clip_instance.remap_time(time, clip_dur, tempo_map) else {
|
||||
return; // Clip instance not active at this time
|
||||
};
|
||||
t.seconds_to_f64()
|
||||
t
|
||||
};
|
||||
|
||||
// Evaluate animated transform properties
|
||||
@@ -1177,10 +1111,9 @@ fn render_video_layer(
|
||||
|
||||
// Remap timeline time to clip's internal time
|
||||
let tempo_map = document.tempo_map();
|
||||
let Some(clip_time) = clip_instance.remap_time(Seconds(time), ClipDuration::Seconds(Seconds(video_clip.duration)), tempo_map) else {
|
||||
let Some(clip_time) = clip_instance.remap_time(time, video_clip.duration, tempo_map) else {
|
||||
continue; // Clip instance not active at this time
|
||||
};
|
||||
let clip_time = clip_time.seconds_to_f64();
|
||||
|
||||
// Get video frame from VideoManager at the output (export/preview) resolution.
|
||||
let (target_w, target_h) = video_decode_target(document, base_transform);
|
||||
@@ -1574,7 +1507,7 @@ fn render_vector_layer(
|
||||
.filter(|vc| vc.is_group)
|
||||
.map(|_| {
|
||||
let frame_duration = 1.0 / document.framerate;
|
||||
layer.group_visibility_end(&clip_instance.id, document.tempo_map().beats_to_seconds(clip_instance.timeline_start).seconds_to_f64(), frame_duration)
|
||||
layer.group_visibility_end(&clip_instance.id, clip_instance.timeline_start, frame_duration)
|
||||
});
|
||||
render_clip_instance(document, time, clip_instance, layer_opacity, scene, base_transform, &layer.layer.animation_data, image_cache, video_manager, group_end_time, extract.as_deref_mut());
|
||||
}
|
||||
@@ -1883,7 +1816,7 @@ fn render_vector_layer_cpu(
|
||||
.filter(|vc| vc.is_group)
|
||||
.map(|_| {
|
||||
let frame_duration = 1.0 / document.framerate;
|
||||
layer.group_visibility_end(&clip_instance.id, document.tempo_map().beats_to_seconds(clip_instance.timeline_start).seconds_to_f64(), frame_duration)
|
||||
layer.group_visibility_end(&clip_instance.id, clip_instance.timeline_start, frame_duration)
|
||||
});
|
||||
render_clip_instance_cpu(
|
||||
document, time, clip_instance, layer_opacity, pixmap, base_transform,
|
||||
@@ -1908,17 +1841,14 @@ fn render_clip_instance_cpu(
|
||||
|
||||
let tempo_map = document.tempo_map();
|
||||
let clip_time = if vector_clip.is_group {
|
||||
let start_secs = tempo_map.beats_to_seconds(clip_instance.timeline_start).seconds_to_f64();
|
||||
let start_secs = tempo_map.transform(clip_instance.timeline_start);
|
||||
let end = group_end_time.unwrap_or(start_secs);
|
||||
if time < start_secs || time >= end { return; }
|
||||
0.0
|
||||
} else {
|
||||
// A vector clip's content is wall-clock seconds.
|
||||
let clip_dur = ClipDuration::Seconds(
|
||||
document.get_clip_duration(&vector_clip.id).unwrap_or(Seconds(vector_clip.duration)),
|
||||
);
|
||||
let Some(t) = clip_instance.remap_time(Seconds(time), clip_dur, tempo_map) else { return };
|
||||
t.seconds_to_f64()
|
||||
let clip_dur = document.get_clip_duration(&vector_clip.id).unwrap_or(vector_clip.duration);
|
||||
let Some(t) = clip_instance.remap_time(time, clip_dur, tempo_map) else { return };
|
||||
t
|
||||
};
|
||||
|
||||
let transform = &clip_instance.transform;
|
||||
@@ -1970,9 +1900,7 @@ fn render_vector_content_cpu(
|
||||
render_vector_content_cpu(document, time, child, pixmap, base_transform, parent_opacity, image_cache);
|
||||
}
|
||||
}
|
||||
// Text is not rendered in the tiny-skia CPU fallback (GPU path only) for v1.
|
||||
AnyLayer::Audio(_) | AnyLayer::Video(_) | AnyLayer::Effect(_) | AnyLayer::Raster(_)
|
||||
| AnyLayer::Text(_) => {}
|
||||
AnyLayer::Audio(_) | AnyLayer::Video(_) | AnyLayer::Effect(_) | AnyLayer::Raster(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -232,9 +232,8 @@ use crate::vector_graph::{FillId, VectorGraph};
|
||||
use kurbo::{BezPath, PathEl, Rect, Shape};
|
||||
|
||||
/// Serialize the document's **vector** content to a standalone SVG string, at document time `time`.
|
||||
/// Vector layers, groups of them, and text layers (as real glyph outlines) — raster/video/audio/
|
||||
/// effect layers are skipped (a later pass can rasterize them to `<image>`). Animation is a single
|
||||
/// static frame at `time`.
|
||||
/// Vector layers (and groups of them) only — raster/video/audio/effect layers are skipped (a later
|
||||
/// pass can rasterize them to `<image>`). Animation is a single static frame at `time`.
|
||||
pub fn document_to_svg(document: &Document, time: f64) -> String {
|
||||
let (w, h) = (document.width, document.height);
|
||||
let mut defs = String::new();
|
||||
@@ -301,136 +300,11 @@ fn layer_to_svg(layer: &AnyLayer, time: f64, parent_opacity: f64, body: &mut Str
|
||||
body.push_str("</g>");
|
||||
}
|
||||
}
|
||||
AnyLayer::Text(tl) => text_layer_to_svg(tl, time, parent_opacity, body),
|
||||
// Raster/Video/Audio/Effect have no lossless vector representation — skipped this pass.
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// A skrifa outline pen that appends transformed glyph contours to an SVG path `d` string.
|
||||
///
|
||||
/// skrifa emits outline points in y-up pixel space (origin at the glyph baseline); this maps each
|
||||
/// point into document space: `x = gx + px + skew·py`, `y = gy − py` (Y flips, `skew` applies any
|
||||
/// synthetic-italic slant), where `(gx, gy)` is the glyph's document-space pen position.
|
||||
struct SvgOutlinePen<'a> {
|
||||
gx: f64,
|
||||
gy: f64,
|
||||
skew: f64,
|
||||
d: &'a mut String,
|
||||
}
|
||||
|
||||
impl<'a> SvgOutlinePen<'a> {
|
||||
fn map(&self, px: f32, py: f32) -> (f64, f64) {
|
||||
let (px, py) = (px as f64, py as f64);
|
||||
(self.gx + px + self.skew * py, self.gy - py)
|
||||
}
|
||||
}
|
||||
|
||||
impl skrifa::outline::OutlinePen for SvgOutlinePen<'_> {
|
||||
fn move_to(&mut self, x: f32, y: f32) {
|
||||
let (x, y) = self.map(x, y);
|
||||
self.d.push_str(&format!("M{x:.2} {y:.2}"));
|
||||
}
|
||||
fn line_to(&mut self, x: f32, y: f32) {
|
||||
let (x, y) = self.map(x, y);
|
||||
self.d.push_str(&format!("L{x:.2} {y:.2}"));
|
||||
}
|
||||
fn quad_to(&mut self, cx: f32, cy: f32, x: f32, y: f32) {
|
||||
let (cx, cy) = self.map(cx, cy);
|
||||
let (x, y) = self.map(x, y);
|
||||
self.d.push_str(&format!("Q{cx:.2} {cy:.2} {x:.2} {y:.2}"));
|
||||
}
|
||||
fn curve_to(&mut self, c0x: f32, c0y: f32, c1x: f32, c1y: f32, x: f32, y: f32) {
|
||||
let (c0x, c0y) = self.map(c0x, c0y);
|
||||
let (c1x, c1y) = self.map(c1x, c1y);
|
||||
let (x, y) = self.map(x, y);
|
||||
self.d.push_str(&format!("C{c0x:.2} {c0y:.2} {c1x:.2} {c1y:.2} {x:.2} {y:.2}"));
|
||||
}
|
||||
fn close(&mut self) {
|
||||
self.d.push('Z');
|
||||
}
|
||||
}
|
||||
|
||||
/// Append a text layer's glyphs to `body` as a single filled `<path>` of real glyph outlines
|
||||
/// (lossless — no font dependency in the SVG). Lays the text out with the same parley path the
|
||||
/// renderer uses, then extracts each glyph's outline with skrifa. Variable-font axis positions and
|
||||
/// synthetic-italic skew are honored; synthetic bold is not (rare).
|
||||
fn text_layer_to_svg(
|
||||
tl: &crate::text_layer::TextLayer,
|
||||
time: f64,
|
||||
parent_opacity: f64,
|
||||
body: &mut String,
|
||||
) {
|
||||
use skrifa::MetadataProvider;
|
||||
|
||||
if !tl.layer.visible {
|
||||
return;
|
||||
}
|
||||
let content = tl.content_at(time);
|
||||
if content.text.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let (ox, oy) = (tl.box_origin.x, tl.box_origin.y);
|
||||
let mut d = String::new();
|
||||
|
||||
crate::fonts::with_layout(content, tl.box_width as f32, |layout| {
|
||||
for line in layout.lines() {
|
||||
for item in line.items() {
|
||||
let parley::PositionedLayoutItem::GlyphRun(glyph_run) = item else { continue };
|
||||
let run = glyph_run.run();
|
||||
let font = run.font();
|
||||
let font_size = run.font_size();
|
||||
let skew = run
|
||||
.synthesis()
|
||||
.skew()
|
||||
.map(|angle| (angle as f64).to_radians().tan())
|
||||
.unwrap_or(0.0);
|
||||
|
||||
let Ok(font_ref) = skrifa::FontRef::from_index(font.data.data(), font.index) else {
|
||||
continue;
|
||||
};
|
||||
let outlines = font_ref.outline_glyphs();
|
||||
|
||||
// Variable-font axis position for this run (empty for static fonts).
|
||||
let coords: Vec<skrifa::instance::NormalizedCoord> = run
|
||||
.normalized_coords()
|
||||
.iter()
|
||||
.map(|&c| skrifa::instance::NormalizedCoord::from_bits(c))
|
||||
.collect();
|
||||
let location = skrifa::instance::LocationRef::new(&coords);
|
||||
let size = skrifa::instance::Size::new(font_size);
|
||||
|
||||
for g in glyph_run.positioned_glyphs() {
|
||||
let Some(glyph) = outlines.get(skrifa::GlyphId::new(g.id as u32)) else {
|
||||
continue;
|
||||
};
|
||||
let mut pen = SvgOutlinePen {
|
||||
gx: ox + g.x as f64,
|
||||
gy: oy + g.y as f64,
|
||||
skew,
|
||||
d: &mut d,
|
||||
};
|
||||
let settings = skrifa::outline::DrawSettings::unhinted(size, location);
|
||||
let _ = glyph.draw(settings, &mut pen);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if d.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let [r, g, b, a] = content.color;
|
||||
let to_u8 = |c: f32| (c.clamp(0.0, 1.0) * 255.0).round() as u8;
|
||||
let fill_opacity = (a as f64 * parent_opacity * tl.layer.opacity).clamp(0.0, 1.0);
|
||||
body.push_str(&format!(
|
||||
r#"<path fill="rgb({},{},{})" fill-opacity="{:.4}" fill-rule="nonzero" d="{}"/>"#,
|
||||
to_u8(r), to_u8(g), to_u8(b), fill_opacity, d
|
||||
));
|
||||
}
|
||||
|
||||
/// Emit a vector graph's fills (`<path fill>`) and stroked edges (`<path stroke>`) into `body`,
|
||||
/// accumulating any gradients into `defs`. Geometry is in document space (no per-layer transform).
|
||||
fn vector_graph_to_svg(graph: &VectorGraph, body: &mut String, defs: &mut String, grad_n: &mut usize) {
|
||||
@@ -614,58 +488,4 @@ mod export_tests {
|
||||
// 1 fill path + 3 stroked edges = 4 <path> elements.
|
||||
assert_eq!(body.matches("<path").count(), 4, "{body}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outline_pen_maps_yflip_and_skew() {
|
||||
use skrifa::outline::OutlinePen;
|
||||
let mut d = String::new();
|
||||
{
|
||||
let mut pen = SvgOutlinePen { gx: 10.0, gy: 100.0, skew: 0.0, d: &mut d };
|
||||
pen.move_to(0.0, 0.0); // baseline origin → (10, 100)
|
||||
pen.line_to(5.0, 20.0); // 20 up → y = 100 − 20 = 80
|
||||
pen.close();
|
||||
}
|
||||
assert!(d.contains("M10.00 100.00"), "d={d}");
|
||||
assert!(d.contains("L15.00 80.00"), "d={d}");
|
||||
assert!(d.ends_with('Z'));
|
||||
|
||||
// Synthetic-italic skew shifts x right in proportion to height.
|
||||
let mut d2 = String::new();
|
||||
{
|
||||
let mut pen = SvgOutlinePen { gx: 0.0, gy: 0.0, skew: 0.5, d: &mut d2 };
|
||||
pen.move_to(0.0, 10.0); // x = 0 + 0.5·10 = 5, y = −10
|
||||
}
|
||||
assert!(d2.contains("M5.00 -10.00"), "d={d2}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_layer_emits_real_glyph_outlines() {
|
||||
use crate::text_layer::TextLayer;
|
||||
|
||||
let mut tl = TextLayer::new("t", Point::new(20.0, 60.0));
|
||||
tl.content.text = "Hi".to_string();
|
||||
tl.content.font_size = 48.0;
|
||||
tl.content.color = [1.0, 0.0, 0.0, 1.0];
|
||||
|
||||
let mut body = String::new();
|
||||
text_layer_to_svg(&tl, 0.0, 1.0, &mut body);
|
||||
|
||||
// Bundled fonts guarantee glyphs → a filled path with actual outline segments.
|
||||
assert!(body.contains("<path"), "no path emitted: {body}");
|
||||
assert!(body.contains(r#"fill="rgb(255,0,0)""#), "wrong fill: {body}");
|
||||
assert!(
|
||||
body.contains('C') || body.contains('Q') || body.contains('L'),
|
||||
"path has no outline segments: {body}"
|
||||
);
|
||||
assert!(body.len() > 80, "suspiciously short path: {body}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_text_layer_emits_nothing() {
|
||||
use crate::text_layer::TextLayer;
|
||||
let tl = TextLayer::new("t", Point::new(0.0, 0.0)); // no text set
|
||||
let mut body = String::new();
|
||||
text_layer_to_svg(&tl, 0.0, 1.0, &mut body);
|
||||
assert!(body.is_empty(), "empty text should emit nothing: {body}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,145 +0,0 @@
|
||||
//! Text layer for Lightningbeam
|
||||
//!
|
||||
//! A text layer holds a single editable text field inside a resizable box,
|
||||
//! with editable size, color, and font. Text is rendered as vector glyphs
|
||||
//! (via parley + Vello) so it composites through the same path as vector art.
|
||||
//!
|
||||
//! The text/style fields are grouped in [`TextContent`] so they can move to a
|
||||
//! per-keyframe model later without touching call sites; v1 stores a single
|
||||
//! static instance and reads it through [`TextLayer::content_at`].
|
||||
|
||||
use crate::layer::{Layer, LayerTrait, LayerType};
|
||||
use kurbo::Point;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::hash::{Hash, Hasher};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Horizontal alignment of text within the box.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum TextAlign {
|
||||
Left,
|
||||
Center,
|
||||
Right,
|
||||
Justify,
|
||||
}
|
||||
|
||||
impl Default for TextAlign {
|
||||
fn default() -> Self {
|
||||
TextAlign::Left
|
||||
}
|
||||
}
|
||||
|
||||
/// The text content + styling for a text layer.
|
||||
///
|
||||
/// Grouped as its own struct so a future keyframed model can store
|
||||
/// `Vec<TextKeyframe>` of these without changing the layer's public shape.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct TextContent {
|
||||
/// The text string.
|
||||
pub text: String,
|
||||
/// Font size in pixels (document space).
|
||||
pub font_size: f64,
|
||||
/// Fill color, linear RGBA in 0..=1.
|
||||
pub color: [f32; 4],
|
||||
/// Logical font family name. Empty string = the bundled default font.
|
||||
pub font_family: String,
|
||||
/// Horizontal alignment within the box.
|
||||
pub align: TextAlign,
|
||||
}
|
||||
|
||||
impl Default for TextContent {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
text: String::new(),
|
||||
font_size: 48.0,
|
||||
color: [1.0, 1.0, 1.0, 1.0],
|
||||
font_family: String::new(),
|
||||
align: TextAlign::Left,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TextContent {
|
||||
/// A stable hash of everything that affects shaped layout (everything except
|
||||
/// `color`, which only affects the brush, not glyph positions). Used to key
|
||||
/// the renderer's external parley-layout cache, including the wrap width.
|
||||
pub fn layout_hash(&self, box_width: f64) -> u64 {
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
self.text.hash(&mut h);
|
||||
self.font_size.to_bits().hash(&mut h);
|
||||
self.font_family.hash(&mut h);
|
||||
self.align.hash(&mut h);
|
||||
box_width.to_bits().hash(&mut h);
|
||||
h.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for TextAlign {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
(*self as u8).hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
/// A text layer: a single resizable text box.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct TextLayer {
|
||||
/// Base layer properties (id, name, opacity, visibility, animation data, …).
|
||||
pub layer: Layer,
|
||||
/// Top-left of the text box, in layer/clip-local space.
|
||||
pub box_origin: Point,
|
||||
/// Box width in document units (drives text wrapping).
|
||||
pub box_width: f64,
|
||||
/// Box height in document units.
|
||||
pub box_height: f64,
|
||||
/// The text content + styling (single static instance for v1).
|
||||
pub content: TextContent,
|
||||
}
|
||||
|
||||
impl TextLayer {
|
||||
/// Default text-box dimensions for a freshly created layer.
|
||||
pub const DEFAULT_WIDTH: f64 = 300.0;
|
||||
pub const DEFAULT_HEIGHT: f64 = 100.0;
|
||||
|
||||
/// Create a new, empty text layer with the given name, positioned at `origin`.
|
||||
pub fn new(name: impl Into<String>, origin: Point) -> Self {
|
||||
Self {
|
||||
layer: Layer::new(LayerType::Text, name),
|
||||
box_origin: origin,
|
||||
box_width: Self::DEFAULT_WIDTH,
|
||||
box_height: Self::DEFAULT_HEIGHT,
|
||||
content: TextContent::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the active text content at `time`. v1 is static, so `time` is unused;
|
||||
/// a future keyframed model will select the active keyframe here.
|
||||
pub fn content_at(&self, _time: f64) -> &TextContent {
|
||||
&self.content
|
||||
}
|
||||
|
||||
/// Mutable access to the active text content at `time`.
|
||||
pub fn content_at_mut(&mut self, _time: f64) -> &mut TextContent {
|
||||
&mut self.content
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate all LayerTrait methods to self.layer (mirrors RasterLayer).
|
||||
impl LayerTrait for TextLayer {
|
||||
fn id(&self) -> Uuid { self.layer.id }
|
||||
fn name(&self) -> &str { &self.layer.name }
|
||||
fn set_name(&mut self, name: String) { self.layer.name = name; }
|
||||
fn has_custom_name(&self) -> bool { self.layer.has_custom_name }
|
||||
fn set_has_custom_name(&mut self, custom: bool) { self.layer.has_custom_name = custom; }
|
||||
fn visible(&self) -> bool { self.layer.visible }
|
||||
fn set_visible(&mut self, visible: bool) { self.layer.visible = visible; }
|
||||
fn opacity(&self) -> f64 { self.layer.opacity }
|
||||
fn set_opacity(&mut self, opacity: f64) { self.layer.opacity = opacity; }
|
||||
fn volume(&self) -> f64 { self.layer.volume }
|
||||
fn set_volume(&mut self, volume: f64) { self.layer.volume = volume; }
|
||||
fn muted(&self) -> bool { self.layer.muted }
|
||||
fn set_muted(&mut self, muted: bool) { self.layer.muted = muted; }
|
||||
fn soloed(&self) -> bool { self.layer.soloed }
|
||||
fn set_soloed(&mut self, soloed: bool) { self.layer.soloed = soloed; }
|
||||
fn locked(&self) -> bool { self.layer.locked }
|
||||
fn set_locked(&mut self, locked: bool) { self.layer.locked = locked; }
|
||||
}
|
||||
@@ -381,9 +381,7 @@ impl Tool {
|
||||
use crate::layer::LayerType;
|
||||
match layer_type {
|
||||
None | Some(LayerType::Vector) => Tool::all(),
|
||||
// The Text tool is available on audio/video/raster too: clicking the
|
||||
// stage with it there creates a new top-level text layer.
|
||||
Some(LayerType::Audio) | Some(LayerType::Video) => &[Tool::Select, Tool::Split, Tool::Text],
|
||||
Some(LayerType::Audio) | Some(LayerType::Video) => &[Tool::Select, Tool::Split],
|
||||
Some(LayerType::Raster) => &[
|
||||
// Brush tools
|
||||
Tool::Draw, Tool::Pencil, Tool::Pen, Tool::Airbrush,
|
||||
@@ -399,10 +397,9 @@ impl Tool {
|
||||
// Transform
|
||||
Tool::Transform, Tool::Warp, Tool::Liquify,
|
||||
// Utility
|
||||
Tool::Eyedropper, Tool::Text,
|
||||
Tool::Eyedropper,
|
||||
],
|
||||
Some(LayerType::Text) => &[Tool::Select, Tool::Text, Tool::Transform],
|
||||
_ => &[Tool::Select, Tool::Text],
|
||||
_ => &[Tool::Select],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -351,29 +351,6 @@ impl VectorGraph {
|
||||
}
|
||||
}
|
||||
|
||||
/// Free any vertex no longer referenced by a non-deleted edge (e.g. after deleting a shape's
|
||||
/// edges), so stale vertices don't linger as snap targets.
|
||||
pub fn gc_isolated_vertices(&mut self) {
|
||||
let mut referenced = vec![false; self.vertices.len()];
|
||||
for e in &self.edges {
|
||||
if e.deleted {
|
||||
continue;
|
||||
}
|
||||
for v in e.vertices.iter() {
|
||||
if !v.is_none() {
|
||||
referenced[v.idx()] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
let to_free: Vec<VertexId> = (0..self.vertices.len())
|
||||
.filter(|&i| !self.vertices[i].deleted && !referenced[i])
|
||||
.map(|i| VertexId(i as u32))
|
||||
.collect();
|
||||
for vid in to_free {
|
||||
self.free_vertex(vid);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Fill / hit-test queries
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "lightningbeam-editor"
|
||||
version = "1.0.10-alpha"
|
||||
version = "1.0.7-alpha"
|
||||
edition = "2021"
|
||||
description = "Multimedia editor for audio, video and 2D animation"
|
||||
license = "GPL-3.0-or-later"
|
||||
@@ -41,10 +41,6 @@ serde_json = { workspace = true }
|
||||
|
||||
# Image loading
|
||||
image = { workspace = true }
|
||||
# Animated GIF encoding — used directly (not just via `image`) so per-frame NeuQuant palette
|
||||
# quantization can be parallelized across a worker pool. Pinned to the version `image` already
|
||||
# resolves (0.13), so this adds no new transitive graph.
|
||||
gif = "0.13"
|
||||
resvg = { workspace = true }
|
||||
tiny-skia = "0.11"
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2026 Lucide Icons and Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
---
|
||||
|
||||
The following Lucide icons are derived from the Feather project:
|
||||
|
||||
airplay, alert-circle, alert-octagon, alert-triangle, aperture, arrow-down-circle, arrow-down-left, arrow-down-right, arrow-down, arrow-left-circle, arrow-left, arrow-right-circle, arrow-right, arrow-up-circle, arrow-up-left, arrow-up-right, arrow-up, at-sign, calendar, cast, check, chevron-down, chevron-left, chevron-right, chevron-up, chevrons-down, chevrons-left, chevrons-right, chevrons-up, circle, clipboard, clock, code, columns, command, compass, corner-down-left, corner-down-right, corner-left-down, corner-left-up, corner-right-down, corner-right-up, corner-up-left, corner-up-right, crosshair, database, divide-circle, divide-square, dollar-sign, download, external-link, feather, frown, hash, headphones, help-circle, info, italic, key, layout, life-buoy, link-2, link, loader, lock, log-in, log-out, maximize, meh, minimize, minimize-2, minus-circle, minus-square, minus, monitor, moon, more-horizontal, more-vertical, move, music, navigation-2, navigation, octagon, pause-circle, percent, plus-circle, plus-square, plus, power, radio, rss, search, server, share, shopping-bag, sidebar, smartphone, smile, square, table-2, tablet, target, terminal, trash-2, trash, triangle, tv, type, upload, x-circle, x-octagon, x-square, x, zoom-in, zoom-out
|
||||
|
||||
The MIT License (MIT) (for the icons listed above)
|
||||
|
||||
Copyright (c) 2013-present Cole Bemis
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Binary file not shown.
@@ -99,14 +99,6 @@
|
||||
--font-size-small: 11px;
|
||||
--font-size-default: 13px;
|
||||
--pane-border-width: 1px;
|
||||
|
||||
/* Modal backdrop (translucent) + intent/category accents (shared by light & dark) */
|
||||
--scrim: #10141ab0;
|
||||
--accent-coral: #e8826b;
|
||||
--accent-cyan: #54c3e8;
|
||||
--accent-amber: #f4a340;
|
||||
--accent-pink: #c75b8a;
|
||||
--accent-violet: #8a6ec0;
|
||||
}
|
||||
|
||||
/* ============================================
|
||||
|
||||
@@ -35,18 +35,6 @@ pub struct AppConfig {
|
||||
#[serde(default = "defaults::audio_buffer_size")]
|
||||
pub audio_buffer_size: u32,
|
||||
|
||||
/// How a cycle MIDI recording treats its passes.
|
||||
///
|
||||
/// `false` (default) = **merge**: every pass overdubs into one clip, and earlier passes play back
|
||||
/// as you record so you can layer against them. `true` = **separate takes**: each pass becomes
|
||||
/// its own take in a take folder, exactly as audio always does, and earlier passes stay silent
|
||||
/// (they're alternatives, not layers).
|
||||
///
|
||||
/// Only applies when the transport actually wraps; a recording that stops inside the first pass
|
||||
/// is an ordinary single recording either way.
|
||||
#[serde(default = "defaults::cycle_midi_separate_takes")]
|
||||
pub cycle_midi_separate_takes: bool,
|
||||
|
||||
/// Reopen last session on startup
|
||||
#[serde(default = "defaults::reopen_last_session")]
|
||||
pub reopen_last_session: bool,
|
||||
@@ -82,55 +70,6 @@ pub struct AppConfig {
|
||||
/// sooner; larger = smaller pyramid, wider re-decode span. Default 256.
|
||||
#[serde(default = "defaults::waveform_floor_samples_per_texel")]
|
||||
pub waveform_floor_samples_per_texel: u32,
|
||||
|
||||
/// Last-used audio-export "Artist" tag, remembered so it prefills next time.
|
||||
#[serde(default)]
|
||||
pub last_audio_artist: String,
|
||||
|
||||
/// Last-used audio-export "Album" tag, remembered so it prefills next time.
|
||||
#[serde(default)]
|
||||
pub last_audio_album: String,
|
||||
|
||||
/// What the stylus's lower barrel button does while held.
|
||||
#[serde(default = "defaults::tablet_button_lower")]
|
||||
pub tablet_button_lower: TabletButtonAction,
|
||||
|
||||
/// What the stylus's upper barrel button does while held.
|
||||
#[serde(default = "defaults::tablet_button_upper")]
|
||||
pub tablet_button_upper: TabletButtonAction,
|
||||
}
|
||||
|
||||
/// What a stylus barrel button does while held down.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum TabletButtonAction {
|
||||
/// Do nothing.
|
||||
None,
|
||||
/// Drag to pan the stage.
|
||||
#[default]
|
||||
Pan,
|
||||
/// Temporarily switch to the eyedropper.
|
||||
Eyedropper,
|
||||
/// Temporarily switch to the eraser.
|
||||
Erase,
|
||||
}
|
||||
|
||||
impl TabletButtonAction {
|
||||
pub const ALL: [TabletButtonAction; 4] = [
|
||||
TabletButtonAction::None,
|
||||
TabletButtonAction::Pan,
|
||||
TabletButtonAction::Eyedropper,
|
||||
TabletButtonAction::Erase,
|
||||
];
|
||||
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
TabletButtonAction::None => "None",
|
||||
TabletButtonAction::Pan => "Pan",
|
||||
TabletButtonAction::Eyedropper => "Eyedropper",
|
||||
TabletButtonAction::Erase => "Eraser",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AppConfig {
|
||||
@@ -143,7 +82,6 @@ impl Default for AppConfig {
|
||||
file_height: defaults::file_height(),
|
||||
scroll_speed: defaults::scroll_speed(),
|
||||
audio_buffer_size: defaults::audio_buffer_size(),
|
||||
cycle_midi_separate_takes: defaults::cycle_midi_separate_takes(),
|
||||
reopen_last_session: defaults::reopen_last_session(),
|
||||
restore_layout_from_file: defaults::restore_layout_from_file(),
|
||||
debug: defaults::debug(),
|
||||
@@ -152,10 +90,6 @@ impl Default for AppConfig {
|
||||
keybindings: KeybindingConfig::default(),
|
||||
large_media_default: LargeMediaMode::default(),
|
||||
waveform_floor_samples_per_texel: defaults::waveform_floor_samples_per_texel(),
|
||||
last_audio_artist: String::new(),
|
||||
last_audio_album: String::new(),
|
||||
tablet_button_lower: defaults::tablet_button_lower(),
|
||||
tablet_button_upper: defaults::tablet_button_upper(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -346,16 +280,12 @@ impl AppConfig {
|
||||
|
||||
/// Default values for preferences (matches JS implementation)
|
||||
mod defaults {
|
||||
use super::TabletButtonAction;
|
||||
pub fn tablet_button_lower() -> TabletButtonAction { TabletButtonAction::Pan }
|
||||
pub fn tablet_button_upper() -> TabletButtonAction { TabletButtonAction::Eyedropper }
|
||||
pub fn bpm() -> u32 { 120 }
|
||||
pub fn framerate() -> u32 { 24 }
|
||||
pub fn file_width() -> u32 { 800 }
|
||||
pub fn file_height() -> u32 { 600 }
|
||||
pub fn scroll_speed() -> f64 { 1.0 }
|
||||
pub fn audio_buffer_size() -> u32 { 256 }
|
||||
pub fn cycle_midi_separate_takes() -> bool { false }
|
||||
pub fn reopen_last_session() -> bool { false }
|
||||
pub fn restore_layout_from_file() -> bool { true }
|
||||
pub fn debug() -> bool { false }
|
||||
|
||||
@@ -1,380 +1,205 @@
|
||||
//! Custom cursor system
|
||||
//!
|
||||
//! Tool cursors are drawn from the bundled Lucide icon font. A tool falls into one of three
|
||||
//! kinds, because "draw the tool's icon at the pointer" only works for icons that actually have
|
||||
//! a point:
|
||||
//!
|
||||
//! * [`CursorKind::System`] — the OS cursor already says it better than we can (the arrow for
|
||||
//! Select, the move cross for Transform). We use the real OS cursor.
|
||||
//! * [`CursorKind::Precise`] — the glyph has an unambiguous tip (pencil, brush, pipette), so it
|
||||
//! *is* the cursor: the tip lands exactly on the click point.
|
||||
//! * [`CursorKind::Badge`] — the glyph has no focus point (stamp, bandage, spray can). Using it
|
||||
//! alone would leave you guessing where you're clicking, so we pair it with a crosshair: the
|
||||
//! crosshair marks the click point and the glyph hangs off its bottom-right.
|
||||
//!
|
||||
//! Rather than *painting* the cursor into the egui scene, we rasterize it and hand it to the
|
||||
//! windowing system as a real OS cursor (`egui::CursorImage` → winit `CustomCursor`). A painted
|
||||
//! cursor is composited with our frame and therefore always trails the pointer by a frame or
|
||||
//! more; an OS cursor is composited by the window system and doesn't lag at all.
|
||||
//!
|
||||
//! The glyphs are rasterized out of egui's own font atlas using the same placement maths as
|
||||
//! `epaint`'s text tessellator, so a glyph lands exactly where `painter.text` would have put it.
|
||||
//! Provides SVG-based custom cursors beyond egui's built-in system cursors.
|
||||
//! When a custom cursor is active, the system cursor is hidden and the SVG
|
||||
//! cursor image is drawn at the pointer position.
|
||||
|
||||
use eframe::egui;
|
||||
use egui::TextureHandle;
|
||||
use lightningbeam_core::tool::Tool;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::mobile::icons;
|
||||
|
||||
/// What kind of cursor a tool gets.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum CursorKind {
|
||||
/// Use a native OS cursor; build nothing ourselves.
|
||||
System(egui::CursorIcon),
|
||||
/// The glyph is the cursor. `hotspot` is the click point in Lucide's 24×24 icon grid.
|
||||
Precise {
|
||||
glyph: &'static str,
|
||||
hotspot: egui::Vec2,
|
||||
},
|
||||
/// A crosshair marks the click point; the glyph sits to its bottom-right.
|
||||
Badge { glyph: &'static str },
|
||||
}
|
||||
|
||||
/// Hotspot for a glyph whose tip is at the bottom-left (pencil, brush, pen, pipette).
|
||||
const TIP_BOTTOM_LEFT: egui::Vec2 = egui::vec2(3.0, 21.0);
|
||||
/// Hotspot for a glyph centred on the click point.
|
||||
const TIP_CENTER: egui::Vec2 = egui::vec2(12.0, 12.0);
|
||||
|
||||
/// Which cursor a stage tool uses.
|
||||
/// Custom cursor identifiers
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum CustomCursor {
|
||||
Tool(Tool),
|
||||
/// Timeline: dragging the end of the loop region.
|
||||
// Stage tool cursors
|
||||
Select,
|
||||
Draw,
|
||||
Transform,
|
||||
Rectangle,
|
||||
Ellipse,
|
||||
PaintBucket,
|
||||
Eyedropper,
|
||||
Line,
|
||||
Polygon,
|
||||
BezierEdit,
|
||||
Text,
|
||||
// Timeline cursors
|
||||
LoopExtend,
|
||||
}
|
||||
|
||||
impl CustomCursor {
|
||||
/// Convert a Tool enum to the corresponding custom cursor
|
||||
pub fn from_tool(tool: Tool) -> Self {
|
||||
CustomCursor::Tool(tool)
|
||||
match tool {
|
||||
Tool::Select => CustomCursor::Select,
|
||||
Tool::Draw => CustomCursor::Draw,
|
||||
Tool::Transform => CustomCursor::Transform,
|
||||
Tool::Rectangle => CustomCursor::Rectangle,
|
||||
Tool::Ellipse => CustomCursor::Ellipse,
|
||||
Tool::PaintBucket => CustomCursor::PaintBucket,
|
||||
Tool::Eyedropper => CustomCursor::Eyedropper,
|
||||
Tool::Line => CustomCursor::Line,
|
||||
Tool::Polygon => CustomCursor::Polygon,
|
||||
Tool::BezierEdit => CustomCursor::BezierEdit,
|
||||
Tool::Text => CustomCursor::Text,
|
||||
Tool::RegionSelect => CustomCursor::Select,
|
||||
Tool::Split => CustomCursor::Select,
|
||||
Tool::Erase => CustomCursor::Draw,
|
||||
Tool::Smudge => CustomCursor::Draw,
|
||||
Tool::SelectLasso => CustomCursor::Select,
|
||||
// Raster brush tools — use draw cursor until implemented
|
||||
Tool::Pencil
|
||||
| Tool::Pen
|
||||
| Tool::Airbrush
|
||||
| Tool::CloneStamp
|
||||
| Tool::HealingBrush
|
||||
| Tool::PatternStamp
|
||||
| Tool::DodgeBurn
|
||||
| Tool::Sponge
|
||||
| Tool::BlurSharpen => CustomCursor::Draw,
|
||||
// Selection tools — use select cursor until implemented
|
||||
Tool::SelectEllipse
|
||||
| Tool::MagicWand
|
||||
| Tool::QuickSelect => CustomCursor::Select,
|
||||
// Other tools — use select cursor until implemented
|
||||
Tool::Gradient
|
||||
| Tool::CustomShape
|
||||
| Tool::Warp
|
||||
| Tool::Liquify => CustomCursor::Select,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> CursorKind {
|
||||
use egui::CursorIcon as Sys;
|
||||
/// Hotspot offset — the "click point" relative to the image top-left
|
||||
pub fn hotspot(&self) -> egui::Vec2 {
|
||||
match self {
|
||||
// Select cursor: pointer tip at top-left
|
||||
CustomCursor::Select => egui::vec2(3.0, 1.0),
|
||||
// Drawing tools: tip at bottom-left
|
||||
CustomCursor::Draw => egui::vec2(1.0, 23.0),
|
||||
// Transform: center
|
||||
CustomCursor::Transform => egui::vec2(12.0, 12.0),
|
||||
// Shape tools: crosshair at center
|
||||
CustomCursor::Rectangle
|
||||
| CustomCursor::Ellipse
|
||||
| CustomCursor::Line
|
||||
| CustomCursor::Polygon => egui::vec2(12.0, 12.0),
|
||||
// Paint bucket: tip at bottom-left
|
||||
CustomCursor::PaintBucket => egui::vec2(2.0, 21.0),
|
||||
// Eyedropper: tip at bottom
|
||||
CustomCursor::Eyedropper => egui::vec2(4.0, 22.0),
|
||||
// Bezier edit: tip at top-left
|
||||
CustomCursor::BezierEdit => egui::vec2(3.0, 1.0),
|
||||
// Text: I-beam center
|
||||
CustomCursor::Text => egui::vec2(12.0, 12.0),
|
||||
// Loop extend: center of circular arrow
|
||||
CustomCursor::LoopExtend => egui::vec2(12.0, 12.0),
|
||||
}
|
||||
}
|
||||
|
||||
let tool = match self {
|
||||
CustomCursor::LoopExtend => {
|
||||
return CursorKind::Precise {
|
||||
glyph: icons::REPEAT,
|
||||
hotspot: TIP_CENTER,
|
||||
}
|
||||
}
|
||||
CustomCursor::Tool(t) => *t,
|
||||
};
|
||||
|
||||
let precise = |glyph, hotspot| CursorKind::Precise { glyph, hotspot };
|
||||
let badge = |glyph| CursorKind::Badge { glyph };
|
||||
|
||||
match tool {
|
||||
// The OS knows these better than we do.
|
||||
Tool::Select => CursorKind::System(Sys::Default),
|
||||
Tool::Transform => CursorKind::System(Sys::Move),
|
||||
|
||||
// Glyphs with a real tip: the icon is the cursor.
|
||||
Tool::Draw => precise(icons::BRUSH, TIP_BOTTOM_LEFT),
|
||||
Tool::Pencil => precise(icons::PENCIL, TIP_BOTTOM_LEFT),
|
||||
Tool::Pen => precise(icons::PEN_TOOL, TIP_BOTTOM_LEFT),
|
||||
Tool::Eyedropper => precise(icons::PIPETTE, TIP_BOTTOM_LEFT),
|
||||
Tool::PaintBucket => precise(icons::PAINT_BUCKET, TIP_BOTTOM_LEFT),
|
||||
Tool::Text => precise(icons::TEXT_CURSOR, TIP_CENTER),
|
||||
|
||||
// Shape tools: crosshair for precision, glyph to say which shape.
|
||||
Tool::Rectangle => badge(icons::SQUARE),
|
||||
Tool::Ellipse => badge(icons::CIRCLE),
|
||||
Tool::Line => badge(icons::MINUS),
|
||||
Tool::Polygon => badge(icons::HEXAGON),
|
||||
Tool::CustomShape => badge(icons::SHAPES),
|
||||
|
||||
// Selection tools.
|
||||
Tool::RegionSelect => badge(icons::SQUARE_DASHED),
|
||||
Tool::SelectEllipse => badge(icons::CIRCLE_DASHED),
|
||||
Tool::SelectLasso => badge(icons::LASSO_SELECT),
|
||||
Tool::MagicWand => badge(icons::WAND_SPARKLES),
|
||||
Tool::QuickSelect => badge(icons::BRUSH),
|
||||
Tool::Split => badge(icons::SCISSORS),
|
||||
|
||||
// Raster tools whose icons have no focus point. These also draw a brush-size ring on
|
||||
// the stage, which is the real precision cue.
|
||||
Tool::Erase => badge(icons::ERASER),
|
||||
Tool::Airbrush => badge(icons::SPRAY_CAN),
|
||||
Tool::Smudge => badge(icons::POINTER),
|
||||
Tool::CloneStamp => badge(icons::STAMP),
|
||||
Tool::PatternStamp => badge(icons::SHAPES),
|
||||
Tool::HealingBrush => badge(icons::BANDAGE),
|
||||
Tool::DodgeBurn => badge(icons::SUN_MOON),
|
||||
Tool::Sponge => badge(icons::DROPLETS),
|
||||
Tool::BlurSharpen => badge(icons::CONTRAST),
|
||||
Tool::Gradient => badge(icons::BLEND),
|
||||
Tool::Warp => badge(icons::SCALING),
|
||||
Tool::Liquify => badge(icons::DROPLET),
|
||||
|
||||
// Vertex editing: crosshair to place the point, glyph to say we're in bezier mode.
|
||||
Tool::BezierEdit => badge(icons::SPLINE),
|
||||
/// Get the embedded SVG data for this cursor
|
||||
fn svg_data(&self) -> &'static [u8] {
|
||||
match self {
|
||||
CustomCursor::Select => include_bytes!("../../../src/assets/select.svg"),
|
||||
CustomCursor::Draw => include_bytes!("../../../src/assets/draw.svg"),
|
||||
CustomCursor::Transform => include_bytes!("../../../src/assets/transform.svg"),
|
||||
CustomCursor::Rectangle => include_bytes!("../../../src/assets/rectangle.svg"),
|
||||
CustomCursor::Ellipse => include_bytes!("../../../src/assets/ellipse.svg"),
|
||||
CustomCursor::PaintBucket => include_bytes!("../../../src/assets/paint_bucket.svg"),
|
||||
CustomCursor::Eyedropper => include_bytes!("../../../src/assets/eyedropper.svg"),
|
||||
CustomCursor::Line => include_bytes!("../../../src/assets/line.svg"),
|
||||
CustomCursor::Polygon => include_bytes!("../../../src/assets/polygon.svg"),
|
||||
CustomCursor::BezierEdit => include_bytes!("../../../src/assets/bezier_edit.svg"),
|
||||
CustomCursor::Text => include_bytes!("../../../src/assets/text.svg"),
|
||||
CustomCursor::LoopExtend => include_bytes!("../../../src/assets/arrow-counterclockwise.svg"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Geometry (logical points; scaled by pixels_per_point when rasterized)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Rendered size of a cursor glyph.
|
||||
const GLYPH_SIZE: f32 = 18.0;
|
||||
/// Lucide icons are authored on a 24×24 grid; hotspots are given in those units.
|
||||
const ICON_GRID: f32 = 24.0;
|
||||
/// Where a badge glyph's top-left sits relative to the crosshair centre.
|
||||
const BADGE_OFFSET: egui::Vec2 = egui::vec2(6.0, 6.0);
|
||||
/// Half-length of a crosshair arm.
|
||||
const CROSSHAIR_ARM: f32 = 7.0;
|
||||
/// Gap between the crosshair centre and the start of each arm, so the click point stays visible.
|
||||
const CROSSHAIR_GAP: f32 = 2.0;
|
||||
/// Margin around the artwork, leaving room for the outline.
|
||||
const MARGIN: f32 = 1.5;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Rasterization
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Cache of rasterized cursor images, keyed by cursor and DPI scale.
|
||||
#[derive(Default)]
|
||||
/// Cache of rasterized cursor textures (black fill + white outline version)
|
||||
pub struct CursorCache {
|
||||
images: HashMap<(CustomCursor, u32), Option<egui::CursorImage>>,
|
||||
next_id: u64,
|
||||
/// Black cursor for the main image
|
||||
textures: HashMap<CustomCursor, TextureHandle>,
|
||||
/// White cursor for the outline
|
||||
outline_textures: HashMap<CustomCursor, TextureHandle>,
|
||||
}
|
||||
|
||||
impl CursorCache {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
Self {
|
||||
textures: HashMap::new(),
|
||||
outline_textures: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_or_build(
|
||||
&mut self,
|
||||
ctx: &egui::Context,
|
||||
cursor: CustomCursor,
|
||||
ppp: f32,
|
||||
) -> Option<egui::CursorImage> {
|
||||
let key = (cursor, ppp.to_bits());
|
||||
if let Some(cached) = self.images.get(&key) {
|
||||
return cached.clone();
|
||||
}
|
||||
/// Get or lazily load the black (fill) cursor texture
|
||||
pub fn get_or_load(&mut self, cursor: CustomCursor, ctx: &egui::Context) -> &TextureHandle {
|
||||
self.textures.entry(cursor).or_insert_with(|| {
|
||||
let svg_data = cursor.svg_data();
|
||||
let svg_string = String::from_utf8_lossy(svg_data);
|
||||
let svg_with_color = svg_string.replace("currentColor", "#000000");
|
||||
rasterize_cursor_svg(svg_with_color.as_bytes(), &format!("cursor_{:?}", cursor), CURSOR_SIZE, ctx)
|
||||
.expect("Failed to rasterize cursor SVG")
|
||||
})
|
||||
}
|
||||
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
|
||||
let built = build_cursor_image(ctx, cursor.kind(), ppp, id);
|
||||
self.images.insert(key, built.clone());
|
||||
built
|
||||
/// Get or lazily load the white (outline) cursor texture
|
||||
pub fn get_or_load_outline(&mut self, cursor: CustomCursor, ctx: &egui::Context) -> &TextureHandle {
|
||||
self.outline_textures.entry(cursor).or_insert_with(|| {
|
||||
let svg_data = cursor.svg_data();
|
||||
let svg_string = String::from_utf8_lossy(svg_data);
|
||||
// Replace all colors with white for the outline
|
||||
let svg_white = svg_string
|
||||
.replace("currentColor", "#ffffff")
|
||||
.replace("#000000", "#ffffff")
|
||||
.replace("#000", "#ffffff");
|
||||
rasterize_cursor_svg(svg_white.as_bytes(), &format!("cursor_{:?}_outline", cursor), CURSOR_SIZE, ctx)
|
||||
.expect("Failed to rasterize cursor SVG outline")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// An alpha coverage mask being composed, in physical pixels.
|
||||
struct Mask {
|
||||
w: usize,
|
||||
h: usize,
|
||||
a: Vec<f32>,
|
||||
}
|
||||
const CURSOR_SIZE: u32 = 24;
|
||||
const OUTLINE_OFFSET: f32 = 1.0;
|
||||
|
||||
impl Mask {
|
||||
fn new(w: usize, h: usize) -> Self {
|
||||
Self { w, h, a: vec![0.0; w * h] }
|
||||
}
|
||||
|
||||
fn add(&mut self, x: usize, y: usize, coverage: f32) {
|
||||
if x < self.w && y < self.h {
|
||||
let p = &mut self.a[y * self.w + x];
|
||||
*p = (*p + coverage).min(1.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn get(&self, x: isize, y: isize) -> f32 {
|
||||
if x < 0 || y < 0 || x as usize >= self.w || y as usize >= self.h {
|
||||
0.0
|
||||
} else {
|
||||
self.a[y as usize * self.w + x as usize]
|
||||
}
|
||||
}
|
||||
|
||||
/// Fill an axis-aligned rect (physical px, may be fractional) with full coverage.
|
||||
fn fill_rect(&mut self, rect: egui::Rect) {
|
||||
let x0 = rect.min.x.floor().max(0.0) as usize;
|
||||
let y0 = rect.min.y.floor().max(0.0) as usize;
|
||||
let x1 = (rect.max.x.ceil() as usize).min(self.w);
|
||||
let y1 = (rect.max.y.ceil() as usize).min(self.h);
|
||||
for y in y0..y1 {
|
||||
for x in x0..x1 {
|
||||
self.add(x, y, 1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Rasterize a cursor into a premultiplied-RGBA image: black artwork with a white outline, so it
|
||||
/// reads against both light and dark backgrounds.
|
||||
fn build_cursor_image(
|
||||
/// Rasterize an SVG into an egui texture (same approach as main.rs rasterize_svg)
|
||||
fn rasterize_cursor_svg(
|
||||
svg_data: &[u8],
|
||||
name: &str,
|
||||
render_size: u32,
|
||||
ctx: &egui::Context,
|
||||
kind: CursorKind,
|
||||
ppp: f32,
|
||||
id: u64,
|
||||
) -> Option<egui::CursorImage> {
|
||||
let (glyph, glyph_origin, hotspot, crosshair) = match kind {
|
||||
CursorKind::System(_) => return None,
|
||||
CursorKind::Precise { glyph, hotspot } => {
|
||||
let scale = GLYPH_SIZE / ICON_GRID;
|
||||
(
|
||||
glyph,
|
||||
egui::vec2(MARGIN, MARGIN),
|
||||
egui::pos2(MARGIN, MARGIN) + hotspot * scale,
|
||||
false,
|
||||
)
|
||||
}
|
||||
CursorKind::Badge { glyph } => {
|
||||
// The crosshair centre is the click point; place it a margin + arm in from the corner.
|
||||
let centre = egui::pos2(MARGIN + CROSSHAIR_ARM, MARGIN + CROSSHAIR_ARM);
|
||||
(glyph, centre.to_vec2() + BADGE_OFFSET, centre, true)
|
||||
}
|
||||
};
|
||||
|
||||
// Lay the glyph out exactly as `painter.text(.., Align2::LEFT_TOP, ..)` would, so we inherit
|
||||
// epaint's placement rather than re-deriving it from font metrics.
|
||||
let galley = ctx.fonts_mut(|f| {
|
||||
f.layout_no_wrap(
|
||||
glyph.to_owned(),
|
||||
icons::font(GLYPH_SIZE),
|
||||
egui::Color32::WHITE,
|
||||
)
|
||||
});
|
||||
|
||||
// Canvas must cover the glyph, and the crosshair too when there is one.
|
||||
let mut content = egui::Rect::from_min_size(glyph_origin.to_pos2(), galley.size());
|
||||
if crosshair {
|
||||
content = content.union(egui::Rect::from_center_size(
|
||||
hotspot,
|
||||
egui::Vec2::splat(2.0 * CROSSHAIR_ARM),
|
||||
));
|
||||
}
|
||||
let canvas = content.expand(MARGIN);
|
||||
|
||||
let w = (canvas.width() * ppp).ceil() as usize;
|
||||
let h = (canvas.height() * ppp).ceil() as usize;
|
||||
if w == 0 || h == 0 || w > 2048 || h > 2048 {
|
||||
return None; // winit caps cursors at 2048px.
|
||||
}
|
||||
|
||||
let mut mask = Mask::new(w, h);
|
||||
// Everything is positioned relative to the canvas origin.
|
||||
let to_px = |p: egui::Pos2| ((p - canvas.min.to_vec2()).to_vec2() * ppp).to_pos2();
|
||||
|
||||
// --- Crosshair ---
|
||||
if crosshair {
|
||||
let c = to_px(hotspot);
|
||||
let thickness = ppp.max(1.0); // one physical pixel, at least
|
||||
let arm = CROSSHAIR_ARM * ppp;
|
||||
let gap = CROSSHAIR_GAP * ppp;
|
||||
// Four arms, leaving a gap at the centre so the exact click point stays visible.
|
||||
for (dx, dy) in [(-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0)] {
|
||||
let from = egui::pos2(c.x + dx * gap, c.y + dy * gap);
|
||||
let to = egui::pos2(c.x + dx * arm, c.y + dy * arm);
|
||||
let rect = egui::Rect::from_two_pos(from, to).expand2(if dx == 0.0 {
|
||||
egui::vec2(thickness / 2.0, 0.0)
|
||||
} else {
|
||||
egui::vec2(0.0, thickness / 2.0)
|
||||
});
|
||||
mask.fill_rect(rect);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Glyph, copied out of egui's font atlas ---
|
||||
let atlas = ctx.fonts_mut(|f| f.image());
|
||||
let atlas_w = atlas.size[0];
|
||||
for row in &galley.rows {
|
||||
for g in &row.glyphs {
|
||||
let uv = g.uv_rect;
|
||||
if uv.is_nothing() {
|
||||
continue;
|
||||
}
|
||||
// Same maths as epaint's text tessellator: the glyph's top-left in points is
|
||||
// `glyph.pos + uv_rect.offset`, relative to the galley's top-left.
|
||||
let left_top = to_px((glyph_origin + (g.pos + uv.offset).to_vec2()).to_pos2());
|
||||
let src_w = (uv.max[0] - uv.min[0]) as usize;
|
||||
let src_h = (uv.max[1] - uv.min[1]) as usize;
|
||||
|
||||
for sy in 0..src_h {
|
||||
for sx in 0..src_w {
|
||||
let src_i = (uv.min[1] as usize + sy) * atlas_w + (uv.min[0] as usize + sx);
|
||||
let Some(px) = atlas.pixels.get(src_i) else { continue };
|
||||
let coverage = px.a() as f32 / 255.0;
|
||||
if coverage <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let dx = (left_top.x.round() as isize) + sx as isize;
|
||||
let dy = (left_top.y.round() as isize) + sy as isize;
|
||||
if dx >= 0 && dy >= 0 {
|
||||
mask.add(dx as usize, dy as usize, coverage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Compose: black fill over a white outline (dilate the mask by one pixel) ---
|
||||
let mut rgba = vec![0u8; w * h * 4];
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let fill = mask.get(x as isize, y as isize);
|
||||
|
||||
let mut outline: f32 = 0.0;
|
||||
for oy in -1..=1_isize {
|
||||
for ox in -1..=1_isize {
|
||||
outline = outline.max(mask.get(x as isize + ox, y as isize + oy));
|
||||
}
|
||||
}
|
||||
|
||||
// White outline underneath, black fill on top; premultiplied.
|
||||
let white = outline * (1.0 - fill);
|
||||
let alpha = fill + outline * (1.0 - fill);
|
||||
let c = (white * 255.0).round().clamp(0.0, 255.0) as u8;
|
||||
|
||||
let i = (y * w + x) * 4;
|
||||
rgba[i] = c;
|
||||
rgba[i + 1] = c;
|
||||
rgba[i + 2] = c;
|
||||
rgba[i + 3] = (alpha * 255.0).round().clamp(0.0, 255.0) as u8;
|
||||
}
|
||||
}
|
||||
|
||||
let hot = to_px(hotspot);
|
||||
Some(egui::CursorImage {
|
||||
id,
|
||||
rgba: Arc::new(rgba),
|
||||
size: (w as u16, h as u16),
|
||||
hotspot: (
|
||||
hot.x.round().clamp(0.0, w as f32 - 1.0) as u16,
|
||||
hot.y.round().clamp(0.0, h as f32 - 1.0) as u16,
|
||||
),
|
||||
})
|
||||
) -> Option<TextureHandle> {
|
||||
let tree = resvg::usvg::Tree::from_data(svg_data, &resvg::usvg::Options::default()).ok()?;
|
||||
let pixmap_size = tree.size().to_int_size();
|
||||
let scale_x = render_size as f32 / pixmap_size.width() as f32;
|
||||
let scale_y = render_size as f32 / pixmap_size.height() as f32;
|
||||
let mut pixmap = resvg::tiny_skia::Pixmap::new(render_size, render_size)?;
|
||||
resvg::render(
|
||||
&tree,
|
||||
resvg::tiny_skia::Transform::from_scale(scale_x, scale_y),
|
||||
&mut pixmap.as_mut(),
|
||||
);
|
||||
let rgba_data = pixmap.data().to_vec();
|
||||
let color_image = egui::ColorImage::from_rgba_unmultiplied(
|
||||
[render_size as usize, render_size as usize],
|
||||
&rgba_data,
|
||||
);
|
||||
Some(ctx.load_texture(name, color_image, egui::TextureOptions::LINEAR))
|
||||
}
|
||||
|
||||
// --- Per-frame cursor slot using egui context data ---
|
||||
|
||||
/// Key for storing the active custom cursor in egui's per-frame data
|
||||
#[derive(Clone, Copy)]
|
||||
struct ActiveCustomCursor(CustomCursor);
|
||||
|
||||
/// Set the custom cursor for this frame. Call from any pane during rendering.
|
||||
/// This hides the system cursor and draws the SVG cursor at pointer position.
|
||||
pub fn set(ctx: &egui::Context, cursor: CustomCursor) {
|
||||
ctx.data_mut(|d| d.insert_temp(egui::Id::new("active_custom_cursor"), ActiveCustomCursor(cursor)));
|
||||
}
|
||||
|
||||
/// Hand the active cursor to the windowing system. Call at the end of the main update loop.
|
||||
/// Render the custom cursor overlay. Call at the end of the main update loop.
|
||||
pub fn render_overlay(ctx: &egui::Context, cache: &mut CursorCache) {
|
||||
// Take and remove the cursor so it doesn't persist to the next frame
|
||||
let id = egui::Id::new("active_custom_cursor");
|
||||
@@ -384,24 +209,43 @@ pub fn render_overlay(ctx: &egui::Context, cache: &mut CursorCache) {
|
||||
val
|
||||
});
|
||||
|
||||
let Some(ActiveCustomCursor(cursor)) = cursor else { return };
|
||||
if let Some(ActiveCustomCursor(cursor)) = cursor {
|
||||
// If a system cursor was explicitly set (resize handles, text inputs, etc.),
|
||||
// let it take priority over the custom cursor
|
||||
let system_cursor = ctx.output(|o| o.cursor_icon);
|
||||
if system_cursor != egui::CursorIcon::Default {
|
||||
return;
|
||||
}
|
||||
|
||||
// If a widget explicitly asked for a system cursor (resize handles, text inputs, ...), let it
|
||||
// win — it knows something about the hover target that we don't.
|
||||
if ctx.output(|o| o.cursor_icon) != egui::CursorIcon::Default {
|
||||
return;
|
||||
}
|
||||
// Hide the system cursor
|
||||
ctx.set_cursor_icon(egui::CursorIcon::None);
|
||||
|
||||
match cursor.kind() {
|
||||
CursorKind::System(icon) => ctx.set_cursor_icon(icon),
|
||||
_ => {
|
||||
let ppp = ctx.pixels_per_point();
|
||||
if let Some(image) = cache.get_or_build(ctx, cursor, ppp) {
|
||||
ctx.set_cursor_image(Some(image));
|
||||
} else {
|
||||
// Rasterization failed — better a crosshair than an invisible cursor.
|
||||
ctx.set_cursor_icon(egui::CursorIcon::Crosshair);
|
||||
if let Some(pos) = ctx.input(|i| i.pointer.latest_pos()) {
|
||||
let hotspot = cursor.hotspot();
|
||||
let size = egui::vec2(CURSOR_SIZE as f32, CURSOR_SIZE as f32);
|
||||
let uv = egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0));
|
||||
let painter = ctx.debug_painter();
|
||||
|
||||
// Draw white outline: render white version offset in 8 directions
|
||||
let outline_tex = cache.get_or_load_outline(cursor, ctx);
|
||||
let outline_id = outline_tex.id();
|
||||
for &(dx, dy) in &[
|
||||
(-OUTLINE_OFFSET, 0.0), (OUTLINE_OFFSET, 0.0),
|
||||
(0.0, -OUTLINE_OFFSET), (0.0, OUTLINE_OFFSET),
|
||||
(-OUTLINE_OFFSET, -OUTLINE_OFFSET), (OUTLINE_OFFSET, -OUTLINE_OFFSET),
|
||||
(-OUTLINE_OFFSET, OUTLINE_OFFSET), (OUTLINE_OFFSET, OUTLINE_OFFSET),
|
||||
] {
|
||||
let offset_rect = egui::Rect::from_min_size(
|
||||
pos - hotspot + egui::vec2(dx, dy),
|
||||
size,
|
||||
);
|
||||
painter.image(outline_id, offset_rect, uv, egui::Color32::WHITE);
|
||||
}
|
||||
|
||||
// Draw black fill on top
|
||||
let fill_tex = cache.get_or_load(cursor, ctx);
|
||||
let cursor_rect = egui::Rect::from_min_size(pos - hotspot, size);
|
||||
painter.image(fill_tex.id(), cursor_rect, uv, egui::Color32::WHITE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,475 @@
|
||||
#![allow(dead_code)]
|
||||
//! Audio export functionality
|
||||
//!
|
||||
//! Exports audio from the timeline to various formats:
|
||||
//! - WAV and FLAC: Use existing DAW backend export
|
||||
//! - MP3 and AAC: Use FFmpeg encoding with rendered samples
|
||||
|
||||
use lightningbeam_core::export::{AudioExportSettings, AudioFormat};
|
||||
use daw_backend::audio::{
|
||||
export::{ExportFormat, ExportSettings as DawExportSettings, render_to_memory},
|
||||
midi_pool::MidiClipPool,
|
||||
pool::AudioPool,
|
||||
project::Project,
|
||||
};
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
/// Export audio to a file
|
||||
///
|
||||
/// This function routes to the appropriate export method based on the format:
|
||||
/// - WAV/FLAC: Use DAW backend export
|
||||
/// - MP3/AAC: Use FFmpeg encoding (TODO)
|
||||
pub fn export_audio<P: AsRef<Path>>(
|
||||
project: &mut Project,
|
||||
pool: &AudioPool,
|
||||
midi_pool: &MidiClipPool,
|
||||
settings: &AudioExportSettings,
|
||||
output_path: P,
|
||||
cancel_flag: &Arc<AtomicBool>,
|
||||
) -> Result<(), String> {
|
||||
// Validate settings
|
||||
settings.validate()?;
|
||||
|
||||
// Check for cancellation before starting
|
||||
if cancel_flag.load(Ordering::Relaxed) {
|
||||
return Err("Export cancelled by user".to_string());
|
||||
}
|
||||
|
||||
match settings.format {
|
||||
AudioFormat::Wav | AudioFormat::Flac => {
|
||||
export_audio_daw_backend(project, pool, midi_pool, settings, output_path)
|
||||
}
|
||||
AudioFormat::Mp3 => {
|
||||
export_audio_ffmpeg_mp3(project, pool, midi_pool, settings, output_path, cancel_flag)
|
||||
}
|
||||
AudioFormat::Aac => {
|
||||
export_audio_ffmpeg_aac(project, pool, midi_pool, settings, output_path, cancel_flag)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Export audio using the DAW backend (WAV/FLAC)
|
||||
fn export_audio_daw_backend<P: AsRef<Path>>(
|
||||
project: &mut Project,
|
||||
pool: &AudioPool,
|
||||
_midi_pool: &MidiClipPool,
|
||||
settings: &AudioExportSettings,
|
||||
output_path: P,
|
||||
) -> Result<(), String> {
|
||||
// Convert our export settings to DAW backend format
|
||||
let daw_settings = DawExportSettings {
|
||||
format: match settings.format {
|
||||
AudioFormat::Wav => ExportFormat::Wav,
|
||||
AudioFormat::Flac => ExportFormat::Flac,
|
||||
_ => unreachable!(), // This function only handles WAV/FLAC
|
||||
},
|
||||
sample_rate: settings.sample_rate,
|
||||
channels: settings.channels,
|
||||
bit_depth: settings.bit_depth,
|
||||
mp3_bitrate: 320, // Not used for WAV/FLAC
|
||||
start_time: daw_backend::Seconds(settings.start_time),
|
||||
end_time: daw_backend::Seconds(settings.end_time),
|
||||
tempo_map: daw_backend::TempoMap::constant(settings.bpm),
|
||||
};
|
||||
|
||||
// Use the existing DAW backend export function
|
||||
// No progress reporting for this direct export path
|
||||
daw_backend::audio::export::export_audio(
|
||||
project,
|
||||
pool,
|
||||
&daw_settings,
|
||||
output_path,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
/// Export audio as MP3 using FFmpeg
|
||||
fn export_audio_ffmpeg_mp3<P: AsRef<Path>>(
|
||||
project: &mut Project,
|
||||
pool: &AudioPool,
|
||||
_midi_pool: &MidiClipPool,
|
||||
settings: &AudioExportSettings,
|
||||
output_path: P,
|
||||
cancel_flag: &Arc<AtomicBool>,
|
||||
) -> Result<(), String> {
|
||||
use ffmpeg_next as ffmpeg;
|
||||
|
||||
// Initialize FFmpeg
|
||||
ffmpeg::init().map_err(|e| format!("Failed to initialize FFmpeg: {}", e))?;
|
||||
|
||||
// Convert settings to DAW backend format
|
||||
let daw_settings = DawExportSettings {
|
||||
format: ExportFormat::Wav, // Unused, but required
|
||||
sample_rate: settings.sample_rate,
|
||||
channels: settings.channels,
|
||||
bit_depth: 16, // Unused
|
||||
mp3_bitrate: settings.bitrate_kbps,
|
||||
start_time: daw_backend::Seconds(settings.start_time),
|
||||
end_time: daw_backend::Seconds(settings.end_time),
|
||||
tempo_map: daw_backend::TempoMap::constant(settings.bpm),
|
||||
};
|
||||
|
||||
// Step 1: Render audio to memory
|
||||
let pcm_samples = render_to_memory(
|
||||
project,
|
||||
pool,
|
||||
&daw_settings,
|
||||
None, // No progress events for now
|
||||
)?;
|
||||
|
||||
// Check for cancellation
|
||||
if cancel_flag.load(Ordering::Relaxed) {
|
||||
return Err("Export cancelled".to_string());
|
||||
}
|
||||
|
||||
// Step 2: Set up FFmpeg encoder
|
||||
let encoder_codec = ffmpeg::encoder::find(ffmpeg::codec::Id::MP3)
|
||||
.ok_or("MP3 encoder (libmp3lame) not found")?;
|
||||
|
||||
// Create output file
|
||||
let mut output = ffmpeg::format::output(&output_path)
|
||||
.map_err(|e| format!("Failed to create output file: {}", e))?;
|
||||
|
||||
// Create encoder
|
||||
let mut encoder = ffmpeg::codec::Context::new_with_codec(encoder_codec)
|
||||
.encoder()
|
||||
.audio()
|
||||
.map_err(|e| format!("Failed to create encoder: {}", e))?;
|
||||
|
||||
// Configure encoder
|
||||
let channel_layout = match settings.channels {
|
||||
1 => ffmpeg::channel_layout::ChannelLayout::MONO,
|
||||
2 => ffmpeg::channel_layout::ChannelLayout::STEREO,
|
||||
_ => return Err(format!("Unsupported channel count: {}", settings.channels)),
|
||||
};
|
||||
|
||||
encoder.set_rate(settings.sample_rate as i32);
|
||||
encoder.set_channel_layout(channel_layout);
|
||||
encoder.set_format(ffmpeg::format::Sample::I16(ffmpeg::format::sample::Type::Planar));
|
||||
encoder.set_bit_rate((settings.bitrate_kbps * 1000) as usize);
|
||||
encoder.set_time_base(ffmpeg::Rational(1, settings.sample_rate as i32));
|
||||
|
||||
// Open encoder
|
||||
let mut encoder = encoder.open_as(encoder_codec)
|
||||
.map_err(|e| format!("Failed to open MP3 encoder: {}", e))?;
|
||||
|
||||
// Add stream and set parameters
|
||||
{
|
||||
let mut stream = output.add_stream(encoder_codec)
|
||||
.map_err(|e| format!("Failed to add stream: {}", e))?;
|
||||
stream.set_parameters(&encoder);
|
||||
} // Drop stream here to release the borrow
|
||||
|
||||
// Write header
|
||||
output.write_header()
|
||||
.map_err(|e| format!("Failed to write header: {}", e))?;
|
||||
|
||||
// Step 3: Encode frames and write to output
|
||||
// Convert interleaved f32 samples to planar i16 format
|
||||
let num_frames = pcm_samples.len() / settings.channels as usize;
|
||||
let planar_samples = convert_to_planar_i16(&pcm_samples, settings.channels);
|
||||
|
||||
// Get encoder frame size
|
||||
let frame_size = encoder.frame_size();
|
||||
let samples_per_frame = if frame_size > 0 {
|
||||
frame_size as usize
|
||||
} else {
|
||||
1152 // Default MP3 frame size
|
||||
};
|
||||
|
||||
// Encode in chunks
|
||||
let mut samples_encoded = 0;
|
||||
while samples_encoded < num_frames {
|
||||
if cancel_flag.load(Ordering::Relaxed) {
|
||||
return Err("Export cancelled".to_string());
|
||||
}
|
||||
|
||||
let samples_remaining = num_frames - samples_encoded;
|
||||
let chunk_size = samples_remaining.min(samples_per_frame);
|
||||
|
||||
// Create audio frame
|
||||
let mut frame = ffmpeg::frame::Audio::new(
|
||||
ffmpeg::format::Sample::I16(ffmpeg::format::sample::Type::Planar),
|
||||
chunk_size,
|
||||
channel_layout,
|
||||
);
|
||||
frame.set_rate(settings.sample_rate);
|
||||
|
||||
// Copy planar samples to frame
|
||||
// Use plane_mut::<i16> instead of data_mut — data_mut(ch) is buggy for planar audio:
|
||||
// FFmpeg only sets linesize[0], so data_mut returns 0-length slices for ch > 0.
|
||||
// plane_mut uses self.samples() for the length, which is correct for all planes.
|
||||
for ch in 0..settings.channels as usize {
|
||||
let plane = frame.plane_mut::<i16>(ch);
|
||||
let offset = samples_encoded;
|
||||
plane.copy_from_slice(&planar_samples[ch][offset..offset + chunk_size]);
|
||||
}
|
||||
|
||||
// Send frame to encoder
|
||||
encoder.send_frame(&frame)
|
||||
.map_err(|e| format!("Failed to send frame: {}", e))?;
|
||||
|
||||
// Receive and write packets
|
||||
receive_and_write_packets(&mut encoder, &mut output)?;
|
||||
|
||||
samples_encoded += chunk_size;
|
||||
}
|
||||
|
||||
// Flush encoder
|
||||
encoder.send_eof()
|
||||
.map_err(|e| format!("Failed to send EOF: {}", e))?;
|
||||
receive_and_write_packets(&mut encoder, &mut output)?;
|
||||
|
||||
// Write trailer
|
||||
output.write_trailer()
|
||||
.map_err(|e| format!("Failed to write trailer: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Convert interleaved f32 samples to planar i16 format
|
||||
fn convert_to_planar_i16(interleaved: &[f32], channels: u32) -> Vec<Vec<i16>> {
|
||||
let num_frames = interleaved.len() / channels as usize;
|
||||
let mut planar = vec![vec![0i16; num_frames]; channels as usize];
|
||||
|
||||
for (i, chunk) in interleaved.chunks(channels as usize).enumerate() {
|
||||
for (ch, &sample) in chunk.iter().enumerate() {
|
||||
// Clamp and convert f32 (-1.0 to 1.0) to i16
|
||||
let clamped = sample.max(-1.0).min(1.0);
|
||||
planar[ch][i] = (clamped * 32767.0) as i16;
|
||||
}
|
||||
}
|
||||
|
||||
planar
|
||||
}
|
||||
|
||||
/// Convert interleaved f32 samples to planar f32 format
|
||||
fn convert_to_planar_f32(interleaved: &[f32], channels: u32) -> Vec<Vec<f32>> {
|
||||
let num_frames = interleaved.len() / channels as usize;
|
||||
let mut planar = vec![vec![0.0f32; num_frames]; channels as usize];
|
||||
|
||||
for (i, chunk) in interleaved.chunks(channels as usize).enumerate() {
|
||||
for (ch, &sample) in chunk.iter().enumerate() {
|
||||
planar[ch][i] = sample;
|
||||
}
|
||||
}
|
||||
|
||||
planar
|
||||
}
|
||||
|
||||
/// Receive encoded packets and write to output
|
||||
fn receive_and_write_packets(
|
||||
encoder: &mut ffmpeg_next::encoder::Audio,
|
||||
output: &mut ffmpeg_next::format::context::Output,
|
||||
) -> Result<(), String> {
|
||||
let mut encoded = ffmpeg_next::Packet::empty();
|
||||
|
||||
while encoder.receive_packet(&mut encoded).is_ok() {
|
||||
encoded.set_stream(0);
|
||||
encoded.write_interleaved(output)
|
||||
.map_err(|e| format!("Failed to write packet: {}", e))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Export audio as AAC using FFmpeg
|
||||
fn export_audio_ffmpeg_aac<P: AsRef<Path>>(
|
||||
project: &mut Project,
|
||||
pool: &AudioPool,
|
||||
_midi_pool: &MidiClipPool,
|
||||
settings: &AudioExportSettings,
|
||||
output_path: P,
|
||||
cancel_flag: &Arc<AtomicBool>,
|
||||
) -> Result<(), String> {
|
||||
use ffmpeg_next as ffmpeg;
|
||||
|
||||
// Initialize FFmpeg
|
||||
ffmpeg::init().map_err(|e| format!("Failed to initialize FFmpeg: {}", e))?;
|
||||
|
||||
// Convert settings to DAW backend format
|
||||
let daw_settings = DawExportSettings {
|
||||
format: ExportFormat::Wav, // Unused, but required
|
||||
sample_rate: settings.sample_rate,
|
||||
channels: settings.channels,
|
||||
bit_depth: 16, // Unused
|
||||
mp3_bitrate: settings.bitrate_kbps,
|
||||
start_time: daw_backend::Seconds(settings.start_time),
|
||||
end_time: daw_backend::Seconds(settings.end_time),
|
||||
tempo_map: daw_backend::TempoMap::constant(settings.bpm),
|
||||
};
|
||||
|
||||
// Step 1: Render audio to memory
|
||||
let pcm_samples = render_to_memory(
|
||||
project,
|
||||
pool,
|
||||
&daw_settings,
|
||||
None, // No progress events for now
|
||||
)?;
|
||||
|
||||
// Check for cancellation
|
||||
if cancel_flag.load(Ordering::Relaxed) {
|
||||
return Err("Export cancelled".to_string());
|
||||
}
|
||||
|
||||
// Step 2: Set up FFmpeg encoder
|
||||
let encoder_codec = ffmpeg::encoder::find(ffmpeg::codec::Id::AAC)
|
||||
.ok_or("AAC encoder not found")?;
|
||||
|
||||
// Create output file
|
||||
let mut output = ffmpeg::format::output(&output_path)
|
||||
.map_err(|e| format!("Failed to create output file: {}", e))?;
|
||||
|
||||
// Create encoder
|
||||
let mut encoder = ffmpeg::codec::Context::new_with_codec(encoder_codec)
|
||||
.encoder()
|
||||
.audio()
|
||||
.map_err(|e| format!("Failed to create encoder: {}", e))?;
|
||||
|
||||
// Configure encoder
|
||||
let channel_layout = match settings.channels {
|
||||
1 => ffmpeg::channel_layout::ChannelLayout::MONO,
|
||||
2 => ffmpeg::channel_layout::ChannelLayout::STEREO,
|
||||
_ => return Err(format!("Unsupported channel count: {}", settings.channels)),
|
||||
};
|
||||
|
||||
encoder.set_rate(settings.sample_rate as i32);
|
||||
encoder.set_channel_layout(channel_layout);
|
||||
// AAC encoder supports FLTP (F32 Planar) format
|
||||
encoder.set_format(ffmpeg::format::Sample::F32(ffmpeg::format::sample::Type::Planar));
|
||||
encoder.set_bit_rate((settings.bitrate_kbps * 1000) as usize);
|
||||
encoder.set_time_base(ffmpeg::Rational(1, settings.sample_rate as i32));
|
||||
|
||||
// Open encoder
|
||||
let mut encoder = encoder.open_as(encoder_codec)
|
||||
.map_err(|e| format!("Failed to open AAC encoder: {}", e))?;
|
||||
|
||||
// Add stream and set parameters
|
||||
{
|
||||
let mut stream = output.add_stream(encoder_codec)
|
||||
.map_err(|e| format!("Failed to add stream: {}", e))?;
|
||||
stream.set_parameters(&encoder);
|
||||
} // Drop stream here to release the borrow
|
||||
|
||||
// Write header
|
||||
output.write_header()
|
||||
.map_err(|e| format!("Failed to write header: {}", e))?;
|
||||
|
||||
// Step 3: Encode frames and write to output
|
||||
// Convert interleaved f32 samples to planar f32 format (no conversion needed, just rearrange)
|
||||
let num_frames = pcm_samples.len() / settings.channels as usize;
|
||||
let planar_samples = convert_to_planar_f32(&pcm_samples, settings.channels);
|
||||
|
||||
// Get encoder frame size
|
||||
let frame_size = encoder.frame_size();
|
||||
let samples_per_frame = if frame_size > 0 {
|
||||
frame_size as usize
|
||||
} else {
|
||||
1024 // Default AAC frame size
|
||||
};
|
||||
|
||||
// Encode in chunks
|
||||
let mut samples_encoded = 0;
|
||||
while samples_encoded < num_frames {
|
||||
if cancel_flag.load(Ordering::Relaxed) {
|
||||
return Err("Export cancelled".to_string());
|
||||
}
|
||||
|
||||
let samples_remaining = num_frames - samples_encoded;
|
||||
let chunk_size = samples_remaining.min(samples_per_frame);
|
||||
|
||||
// Create audio frame
|
||||
let mut frame = ffmpeg::frame::Audio::new(
|
||||
ffmpeg::format::Sample::F32(ffmpeg::format::sample::Type::Planar),
|
||||
chunk_size,
|
||||
channel_layout,
|
||||
);
|
||||
frame.set_rate(settings.sample_rate);
|
||||
|
||||
// Copy planar samples to frame
|
||||
unsafe {
|
||||
for ch in 0..settings.channels as usize {
|
||||
let plane = frame.data_mut(ch);
|
||||
let offset = samples_encoded;
|
||||
let src = &planar_samples[ch][offset..offset + chunk_size];
|
||||
|
||||
std::ptr::copy_nonoverlapping(
|
||||
src.as_ptr() as *const u8,
|
||||
plane.as_mut_ptr(),
|
||||
chunk_size * std::mem::size_of::<f32>(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Send frame to encoder
|
||||
encoder.send_frame(&frame)
|
||||
.map_err(|e| format!("Failed to send frame: {}", e))?;
|
||||
|
||||
// Receive and write packets
|
||||
receive_and_write_packets(&mut encoder, &mut output)?;
|
||||
|
||||
samples_encoded += chunk_size;
|
||||
}
|
||||
|
||||
// Flush encoder
|
||||
encoder.send_eof()
|
||||
.map_err(|e| format!("Failed to send EOF: {}", e))?;
|
||||
receive_and_write_packets(&mut encoder, &mut output)?;
|
||||
|
||||
// Write trailer
|
||||
output.write_trailer()
|
||||
.map_err(|e| format!("Failed to write trailer: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_export_audio_validation() {
|
||||
let mut settings = AudioExportSettings::default();
|
||||
settings.sample_rate = 0; // Invalid
|
||||
|
||||
let project = Project::new(44100);
|
||||
let pool = AudioPool::new();
|
||||
let midi_pool = MidiClipPool::new();
|
||||
let cancel_flag = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let result = export_audio(
|
||||
&mut project.clone(),
|
||||
&pool,
|
||||
&midi_pool,
|
||||
&settings,
|
||||
"/tmp/test.wav",
|
||||
&cancel_flag,
|
||||
);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("Sample rate"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_export_audio_cancellation() {
|
||||
let settings = AudioExportSettings::default();
|
||||
let mut project = Project::new(44100);
|
||||
let pool = AudioPool::new();
|
||||
let midi_pool = MidiClipPool::new();
|
||||
let cancel_flag = Arc::new(AtomicBool::new(true)); // Pre-cancelled
|
||||
|
||||
let result = export_audio(
|
||||
&mut project,
|
||||
&pool,
|
||||
&midi_pool,
|
||||
&settings,
|
||||
"/tmp/test.wav",
|
||||
&cancel_flag,
|
||||
);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("cancelled"));
|
||||
}
|
||||
}
|
||||
@@ -101,94 +101,6 @@ impl CpuYuvConverter {
|
||||
}
|
||||
}
|
||||
|
||||
/// CPU RGBA→YUV422P10LE converter (10-bit, 4:2:2) via swscale, for ProRes 422 export.
|
||||
///
|
||||
/// ProRes (`prores_ks`) requires a 10-bit 4:2:2 input; the SDR pipeline otherwise produces 8-bit
|
||||
/// 4:2:0. Source is still 8-bit RGBA (bit-depth is promoted, not conjured), which is normal for
|
||||
/// SDR ProRes. BT.709 with the requested range, matching the encoder's color tags.
|
||||
pub struct CpuYuv422P10Converter {
|
||||
width: u32,
|
||||
height: u32,
|
||||
scaler: ffmpeg::software::scaling::Context,
|
||||
rgba_frame: ffmpeg::frame::Video,
|
||||
yuv_frame: ffmpeg::frame::Video,
|
||||
}
|
||||
|
||||
impl CpuYuv422P10Converter {
|
||||
pub fn new(width: u32, height: u32, full_range: bool) -> Result<Self, String> {
|
||||
let mut scaler = ffmpeg::software::scaling::Context::get(
|
||||
ffmpeg::format::Pixel::RGBA, width, height,
|
||||
ffmpeg::format::Pixel::YUV422P10LE, width, height,
|
||||
ffmpeg::software::scaling::Flags::BILINEAR,
|
||||
)
|
||||
.map_err(|e| format!("Failed to create YUV422P10 swscale context: {}", e))?;
|
||||
|
||||
// BT.709, requested output range (matches setup_video_encoder's SDR tags). No safe
|
||||
// ffmpeg-next wrapper for sws_setColorspaceDetails, so this is the raw call (as in
|
||||
// CpuYuvConverter::new above).
|
||||
unsafe {
|
||||
let coeffs = ffmpeg::ffi::sws_getCoefficients(ffmpeg::ffi::SWS_CS_ITU709 as i32);
|
||||
let dst_range = if full_range { 1 } else { 0 };
|
||||
let one = 1 << 16;
|
||||
ffmpeg::ffi::sws_setColorspaceDetails(
|
||||
scaler.as_mut_ptr(),
|
||||
coeffs, 1,
|
||||
coeffs, dst_range,
|
||||
0, one, one,
|
||||
);
|
||||
}
|
||||
|
||||
let rgba_frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::RGBA, width, height);
|
||||
let yuv_frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::YUV422P10LE, width, height);
|
||||
Ok(Self { width, height, scaler, rgba_frame, yuv_frame })
|
||||
}
|
||||
|
||||
/// Convert packed RGBA (width*height*4) to tight YUV422P10LE planes (little-endian, 2 bytes per
|
||||
/// sample): Y is width×height, U and V are (width/2)×height. Planes are returned tight (stride
|
||||
/// padding stripped) to match what `encode_frame` expects.
|
||||
pub fn convert(&mut self, rgba_data: &[u8]) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>), String> {
|
||||
let expected = (self.width * self.height * 4) as usize;
|
||||
assert_eq!(rgba_data.len(), expected,
|
||||
"RGBA data size mismatch: expected {} bytes, got {}", expected, rgba_data.len());
|
||||
|
||||
// Copy RGBA into the source frame honoring its stride (may be padded).
|
||||
let row_bytes = (self.width * 4) as usize;
|
||||
let src_stride = self.rgba_frame.stride(0);
|
||||
{
|
||||
let dst = self.rgba_frame.data_mut(0);
|
||||
for row in 0..self.height as usize {
|
||||
let s = row * row_bytes;
|
||||
let d = row * src_stride;
|
||||
dst[d..d + row_bytes].copy_from_slice(&rgba_data[s..s + row_bytes]);
|
||||
}
|
||||
}
|
||||
|
||||
self.scaler
|
||||
.run(&self.rgba_frame, &mut self.yuv_frame)
|
||||
.map_err(|e| format!("YUV422P10 swscale conversion failed: {}", e))?;
|
||||
|
||||
// Extract each plane tight (2 bytes/sample). Y: width samples/row × height rows.
|
||||
// Chroma (4:2:2): width/2 samples/row × height rows.
|
||||
let extract = |frame: &ffmpeg::frame::Video, idx: usize, samples_w: usize, rows: usize| {
|
||||
let bytes_per_row = samples_w * 2;
|
||||
let stride = frame.stride(idx);
|
||||
let data = frame.data(idx);
|
||||
let mut out = Vec::with_capacity(bytes_per_row * rows);
|
||||
for row in 0..rows {
|
||||
let start = row * stride;
|
||||
out.extend_from_slice(&data[start..start + bytes_per_row]);
|
||||
}
|
||||
out
|
||||
};
|
||||
let (w, h) = (self.width as usize, self.height as usize);
|
||||
let y_plane = extract(&self.yuv_frame, 0, w, h);
|
||||
let u_plane = extract(&self.yuv_frame, 1, w / 2, h);
|
||||
let v_plane = extract(&self.yuv_frame, 2, w / 2, h);
|
||||
|
||||
Ok((y_plane, u_plane, v_plane))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -219,20 +131,6 @@ mod tests {
|
||||
assert_eq!(v.len(), (1920 / 2) * (1080 / 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_yuv422p10_output_sizes() {
|
||||
// Use a width that forces swscale linesize padding (not a multiple of 32/64) to exercise
|
||||
// the stride-stripping extraction.
|
||||
let (w, h) = (1000u32, 720u32);
|
||||
let mut c = CpuYuv422P10Converter::new(w, h, false).unwrap();
|
||||
let rgba = vec![0u8; (w * h * 4) as usize];
|
||||
let (y, u, v) = c.convert(&rgba).unwrap();
|
||||
// 10-bit → 2 bytes/sample. Y full res; U/V half width, full height (4:2:2).
|
||||
assert_eq!(y.len(), (w * h * 2) as usize);
|
||||
assert_eq!(u.len(), ((w / 2) * h * 2) as usize);
|
||||
assert_eq!(v.len(), ((w / 2) * h * 2) as usize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "RGBA data size mismatch")]
|
||||
fn test_wrong_input_size_panics() {
|
||||
|
||||
@@ -5,42 +5,11 @@
|
||||
use eframe::egui;
|
||||
use lightningbeam_core::export::{
|
||||
AudioExportSettings, AudioFormat,
|
||||
GifExportSettings,
|
||||
ImageExportSettings, ImageFormat,
|
||||
VideoExportSettings, VideoCodec, VideoQuality, ColorRange,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// The OS username (`$USER` / `%USERNAME%`), or empty if unavailable. Used as a default Artist tag.
|
||||
fn os_username() -> String {
|
||||
std::env::var("USER")
|
||||
.or_else(|_| std::env::var("USERNAME"))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Current civil year (UTC) computed from the system clock — avoids pulling in a date crate.
|
||||
fn current_year() -> i64 {
|
||||
let secs = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0) as i64;
|
||||
year_from_unix_secs(secs)
|
||||
}
|
||||
|
||||
/// Civil year (UTC) for a Unix timestamp, via Howard Hinnant's `civil_from_days` algorithm.
|
||||
fn year_from_unix_secs(secs: i64) -> i64 {
|
||||
let days = secs.div_euclid(86_400);
|
||||
let z = days + 719_468;
|
||||
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
|
||||
let doe = z - era * 146_097; // [0, 146096]
|
||||
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; // [0, 399]
|
||||
let y = yoe + era * 400;
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
|
||||
let mp = (5 * doy + 2) / 153; // [0, 11]
|
||||
let m = if mp < 10 { mp + 3 } else { mp - 9 }; // [1, 12]
|
||||
y + if m <= 2 { 1 } else { 0 }
|
||||
}
|
||||
|
||||
/// Hint about document content, used to pick a smart default export type.
|
||||
pub struct DocumentHint {
|
||||
pub has_video: bool,
|
||||
@@ -56,8 +25,6 @@ pub enum ExportType {
|
||||
Audio,
|
||||
Image,
|
||||
Video,
|
||||
/// Animated GIF (multi-frame, palette-quantized, no audio).
|
||||
Gif,
|
||||
/// Vector-only SVG of the current frame (lossless; raster/video layers skipped).
|
||||
Svg,
|
||||
}
|
||||
@@ -69,8 +36,6 @@ pub enum ExportResult {
|
||||
Image(ImageExportSettings, PathBuf),
|
||||
VideoOnly(VideoExportSettings, PathBuf),
|
||||
VideoWithAudio(VideoExportSettings, AudioExportSettings, PathBuf),
|
||||
/// Animated GIF export.
|
||||
Gif(GifExportSettings, PathBuf),
|
||||
/// SVG of vector layers at the given document time.
|
||||
Svg(f64, PathBuf),
|
||||
}
|
||||
@@ -92,9 +57,6 @@ pub struct ExportDialog {
|
||||
/// Video export settings
|
||||
pub video_settings: VideoExportSettings,
|
||||
|
||||
/// Animated GIF export settings
|
||||
pub gif_settings: GifExportSettings,
|
||||
|
||||
/// Include audio with video?
|
||||
pub include_audio: bool,
|
||||
|
||||
@@ -142,7 +104,6 @@ impl Default for ExportDialog {
|
||||
audio_settings: AudioExportSettings::standard_mp3(),
|
||||
image_settings: ImageExportSettings::default(),
|
||||
video_settings: VideoExportSettings::default(),
|
||||
gif_settings: GifExportSettings::default(),
|
||||
include_audio: true,
|
||||
output_path: None,
|
||||
error_message: None,
|
||||
@@ -159,18 +120,10 @@ impl Default for ExportDialog {
|
||||
|
||||
impl ExportDialog {
|
||||
/// Open the dialog with default settings, using `hint` to pick a smart default tab.
|
||||
pub fn open(
|
||||
&mut self,
|
||||
timeline_duration: f64,
|
||||
project_name: &str,
|
||||
hint: &DocumentHint,
|
||||
last_artist: &str,
|
||||
last_album: &str,
|
||||
) {
|
||||
pub fn open(&mut self, timeline_duration: f64, project_name: &str, hint: &DocumentHint) {
|
||||
self.open = true;
|
||||
self.audio_settings.end_time = timeline_duration;
|
||||
self.video_settings.end_time = timeline_duration;
|
||||
self.gif_settings.end_time = timeline_duration;
|
||||
self.image_settings.time = hint.current_time;
|
||||
// Propagate document dimensions as defaults (None means "use doc size").
|
||||
self.image_settings.width = None;
|
||||
@@ -190,24 +143,6 @@ impl ExportDialog {
|
||||
else if only_raster { ExportType::Image }
|
||||
else { self.export_type } // keep current as fallback
|
||||
};
|
||||
// Sensible tag defaults, only filled when empty so a user's edits are never clobbered:
|
||||
// • Title → project name (on a project switch)
|
||||
// • Year → current year
|
||||
// • Artist → last-used artist, else the OS username
|
||||
// • Album → last-used album
|
||||
let meta = &mut self.audio_settings.metadata;
|
||||
if meta.title.is_empty() && !same_project {
|
||||
meta.title = project_name.to_owned();
|
||||
}
|
||||
if meta.year.is_empty() {
|
||||
meta.year = current_year().to_string();
|
||||
}
|
||||
if meta.artist.is_empty() {
|
||||
meta.artist = if !last_artist.is_empty() { last_artist.to_owned() } else { os_username() };
|
||||
}
|
||||
if meta.album.is_empty() && !last_album.is_empty() {
|
||||
meta.album = last_album.to_owned();
|
||||
}
|
||||
self.current_project = project_name.to_owned();
|
||||
|
||||
// Restore the last exported path if available; otherwise default to project name.
|
||||
@@ -225,7 +160,6 @@ impl ExportDialog {
|
||||
ExportType::Audio => self.audio_settings.format.extension(),
|
||||
ExportType::Image => self.image_settings.format.extension(),
|
||||
ExportType::Video => self.video_settings.codec.container_format(),
|
||||
ExportType::Gif => "gif",
|
||||
ExportType::Svg => "svg",
|
||||
}
|
||||
}
|
||||
@@ -269,13 +203,12 @@ impl ExportDialog {
|
||||
ExportType::Audio => "Export Audio",
|
||||
ExportType::Image => "Export Image",
|
||||
ExportType::Video => "Export Video",
|
||||
ExportType::Gif => "Export GIF",
|
||||
ExportType::Svg => "Export SVG",
|
||||
};
|
||||
|
||||
let modal_response = egui::Modal::new(egui::Id::new("export_dialog_modal"))
|
||||
.show(ctx, |ui| {
|
||||
ui.set_width(crate::mobile::dialog_width(ctx, 500.0));
|
||||
ui.set_width(500.0);
|
||||
|
||||
ui.heading(window_title);
|
||||
ui.add_space(8.0);
|
||||
@@ -292,7 +225,6 @@ impl ExportDialog {
|
||||
(ExportType::Audio, "Audio"),
|
||||
(ExportType::Image, "Image"),
|
||||
(ExportType::Video, "Video"),
|
||||
(ExportType::Gif, "GIF"),
|
||||
(ExportType::Svg, "SVG"),
|
||||
] {
|
||||
if ui.selectable_value(&mut self.export_type, variant, label).clicked() {
|
||||
@@ -310,7 +242,6 @@ impl ExportDialog {
|
||||
ExportType::Audio => self.render_audio_basic(ui),
|
||||
ExportType::Image => self.render_image_settings(ui),
|
||||
ExportType::Video => self.render_video_basic(ui),
|
||||
ExportType::Gif => self.render_gif_basic(ui),
|
||||
ExportType::Svg => self.render_svg_settings(ui),
|
||||
}
|
||||
|
||||
@@ -330,7 +261,6 @@ impl ExportDialog {
|
||||
ExportType::Audio => self.render_audio_advanced(ui),
|
||||
ExportType::Image => self.render_image_advanced(ui),
|
||||
ExportType::Video => self.render_video_advanced(ui),
|
||||
ExportType::Gif => self.render_gif_advanced(ui),
|
||||
ExportType::Svg => {} // SVG has no advanced settings
|
||||
}
|
||||
}
|
||||
@@ -530,50 +460,10 @@ impl ExportDialog {
|
||||
|
||||
ui.add_space(8.0);
|
||||
|
||||
// Tag metadata (ID3 / MP4 / Vorbis / RIFF-INFO depending on format).
|
||||
self.render_audio_metadata(ui);
|
||||
|
||||
ui.add_space(8.0);
|
||||
|
||||
// Time range
|
||||
self.render_time_range(ui);
|
||||
}
|
||||
|
||||
/// Render the audio tag-metadata fields (title/artist/album/…). Written into the file on export.
|
||||
fn render_audio_metadata(&mut self, ui: &mut egui::Ui) {
|
||||
let m = &mut self.audio_settings.metadata;
|
||||
ui.label(egui::RichText::new("Tags").strong());
|
||||
// Placeholder styling: italic + a clearly faded color so an empty field's hint never reads
|
||||
// as a real value (the theme's default weak color is too close to the text color). An
|
||||
// explicit color overrides egui's weak-color fallback for hint text.
|
||||
let hint_color = {
|
||||
let t = ui.visuals().text_color();
|
||||
egui::Color32::from_rgba_unmultiplied(t.r(), t.g(), t.b(), 100)
|
||||
};
|
||||
let year_hint = format!("e.g. {}", current_year());
|
||||
egui::Grid::new("audio_metadata_grid")
|
||||
.num_columns(2)
|
||||
.spacing([8.0, 4.0])
|
||||
.show(ui, |ui| {
|
||||
let row = |ui: &mut egui::Ui, label: &str, val: &mut String, hint: &str| {
|
||||
ui.label(label);
|
||||
ui.add(
|
||||
egui::TextEdit::singleline(val)
|
||||
.hint_text(egui::RichText::new(hint).italics().color(hint_color))
|
||||
.desired_width(260.0),
|
||||
);
|
||||
ui.end_row();
|
||||
};
|
||||
row(ui, "Title", &mut m.title, "e.g. My Song");
|
||||
row(ui, "Artist", &mut m.artist, "e.g. Jane Doe");
|
||||
row(ui, "Album", &mut m.album, "e.g. Greatest Hits");
|
||||
row(ui, "Genre", &mut m.genre, "e.g. Electronic");
|
||||
row(ui, "Year", &mut m.year, &year_hint);
|
||||
row(ui, "Track", &mut m.track, "e.g. 1 or 1/12");
|
||||
row(ui, "Comment", &mut m.comment, "Optional notes…");
|
||||
});
|
||||
}
|
||||
|
||||
/// Video presets: (name, codec, quality, width, height, fps)
|
||||
const VIDEO_PRESETS: &'static [(&'static str, VideoCodec, VideoQuality, u32, u32, f64)] = &[
|
||||
("1080p H.264 (Standard)", VideoCodec::H264, VideoQuality::High, 1920, 1080, 30.0),
|
||||
@@ -724,65 +614,12 @@ impl ExportDialog {
|
||||
self.render_time_range(ui);
|
||||
}
|
||||
|
||||
/// GIF frame-rate presets (fps). GIF delays are centisecond-quantized, so these map to clean
|
||||
/// per-frame delays (10/15/20/25/50 fps → 100/70/50/40/20 ms after rounding).
|
||||
const GIF_FPS: &'static [f64] = &[10.0, 15.0, 20.0, 25.0, 50.0];
|
||||
|
||||
/// Render basic GIF settings (frame rate + loop).
|
||||
fn render_gif_basic(&mut self, ui: &mut egui::Ui) {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Frame rate:");
|
||||
egui::ComboBox::from_id_salt("gif_fps")
|
||||
.selected_text(format!("{} fps", self.gif_settings.framerate as u32))
|
||||
.show_ui(ui, |ui| {
|
||||
for &fps in Self::GIF_FPS {
|
||||
ui.selectable_value(&mut self.gif_settings.framerate, fps, format!("{} fps", fps as u32));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ui.checkbox(&mut self.gif_settings.loop_forever, "Loop forever");
|
||||
|
||||
ui.add_space(8.0);
|
||||
self.render_time_range(ui);
|
||||
}
|
||||
|
||||
/// Render advanced GIF settings (resolution, fit, transparency).
|
||||
fn render_gif_advanced(&mut self, ui: &mut egui::Ui) {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Size:");
|
||||
let mut w = self.gif_settings.width.unwrap_or(0);
|
||||
let mut h = self.gif_settings.height.unwrap_or(0);
|
||||
let changed_w = ui.add(egui::DragValue::new(&mut w).range(0..=u32::MAX).prefix("W ")).changed();
|
||||
let changed_h = ui.add(egui::DragValue::new(&mut h).range(0..=u32::MAX).prefix("H ")).changed();
|
||||
if changed_w { self.gif_settings.width = if w == 0 { None } else { Some(w) }; }
|
||||
if changed_h { self.gif_settings.height = if h == 0 { None } else { Some(h) }; }
|
||||
ui.weak("(0 = document size)");
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
use lightningbeam_core::export::ExportFitMode;
|
||||
ui.label("Fit:");
|
||||
egui::ComboBox::from_id_salt("gif_fit_mode")
|
||||
.selected_text(self.gif_settings.fit.name())
|
||||
.show_ui(ui, |ui| {
|
||||
ui.selectable_value(&mut self.gif_settings.fit, ExportFitMode::Letterbox, ExportFitMode::Letterbox.name());
|
||||
ui.selectable_value(&mut self.gif_settings.fit, ExportFitMode::Crop, ExportFitMode::Crop.name());
|
||||
ui.selectable_value(&mut self.gif_settings.fit, ExportFitMode::Stretch, ExportFitMode::Stretch.name());
|
||||
});
|
||||
});
|
||||
|
||||
ui.checkbox(&mut self.gif_settings.transparency, "Preserve transparency (1-bit)");
|
||||
ui.label(egui::RichText::new("GIF supports only on/off transparency; semi-transparent pixels are keyed out.").weak().small());
|
||||
}
|
||||
|
||||
/// Render time range UI (common to both audio and video)
|
||||
fn render_time_range(&mut self, ui: &mut egui::Ui) {
|
||||
let (start_time, end_time) = match self.export_type {
|
||||
ExportType::Audio => (&mut self.audio_settings.start_time, &mut self.audio_settings.end_time),
|
||||
ExportType::Image | ExportType::Svg => return, // single time field, not a range
|
||||
ExportType::Video => (&mut self.video_settings.start_time, &mut self.video_settings.end_time),
|
||||
ExportType::Gif => (&mut self.gif_settings.start_time, &mut self.gif_settings.end_time),
|
||||
};
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
@@ -856,13 +693,6 @@ impl ExportDialog {
|
||||
Some(ExportResult::Image(self.image_settings.clone(), output_path))
|
||||
}
|
||||
ExportType::Svg => Some(ExportResult::Svg(self.image_settings.time, output_path)),
|
||||
ExportType::Gif => {
|
||||
if let Err(err) = self.gif_settings.validate() {
|
||||
self.error_message = Some(err);
|
||||
return None;
|
||||
}
|
||||
Some(ExportResult::Gif(self.gif_settings.clone(), output_path))
|
||||
}
|
||||
ExportType::Audio => {
|
||||
// Validate audio settings
|
||||
if let Err(err) = self.audio_settings.validate() {
|
||||
@@ -971,7 +801,7 @@ impl ExportProgressDialog {
|
||||
|
||||
egui::Modal::new(egui::Id::new("export_progress_modal"))
|
||||
.show(ctx, |ui| {
|
||||
ui.set_width(crate::mobile::dialog_width(ctx, 400.0));
|
||||
ui.set_width(400.0);
|
||||
|
||||
ui.heading("Exporting...");
|
||||
ui.add_space(8.0);
|
||||
@@ -1031,30 +861,3 @@ impl ExportProgressDialog {
|
||||
should_cancel
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn year_from_unix_secs_known_values() {
|
||||
assert_eq!(year_from_unix_secs(0), 1970); // Unix epoch
|
||||
assert_eq!(year_from_unix_secs(946_684_800), 2000); // 2000-01-01
|
||||
assert_eq!(year_from_unix_secs(1_735_689_600), 2025); // 2025-01-01
|
||||
assert_eq!(year_from_unix_secs(1_767_225_599), 2025); // 2025-12-31 23:59:59
|
||||
assert_eq!(year_from_unix_secs(1_767_225_600), 2026); // 2026-01-01
|
||||
|
||||
// Post-2038: these timestamps exceed i32::MAX (2_147_483_647) — and the last exceeds
|
||||
// u32::MAX — so a 32-bit time_t would wrap here. i64 math handles them correctly.
|
||||
assert_eq!(year_from_unix_secs(2_148_595_200), 2038); // 2038-02-01 (> i32::MAX)
|
||||
assert_eq!(year_from_unix_secs(2_223_331_200), 2040); // 2040-06-15
|
||||
assert_eq!(year_from_unix_secs(4_102_444_800), 2100); // 2100-01-01 (not a leap year)
|
||||
assert_eq!(year_from_unix_secs(9_214_646_400), 2262); // 2262-01-01 (> u32::MAX)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_year_is_plausible() {
|
||||
let y = current_year();
|
||||
assert!((2020..3000).contains(&y), "implausible year: {y}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,196 +0,0 @@
|
||||
//! Animated GIF encoding.
|
||||
//!
|
||||
//! Palette-quantizes a stream of RGBA8 frames and writes them to a `.gif`. The expensive part —
|
||||
//! per-frame NeuQuant 256-color quantization — is embarrassingly parallel (each frame gets its own
|
||||
//! local palette), so it's fanned out across a worker pool. A single writer thread collects the
|
||||
//! quantized frames, reorders them, and LZW-encodes them to the file in sequence.
|
||||
//!
|
||||
//! Pipeline (all off the UI thread):
|
||||
//! ```text
|
||||
//! UI render thread ──RGBA──▶ coordinator ──round-robin──▶ N quantizer workers
|
||||
//! │ (idx, gif::Frame)
|
||||
//! ▼
|
||||
//! writer thread ──▶ .gif
|
||||
//! ```
|
||||
//! Rendering + readback happen on the UI thread (see `render_next_gif_frame`); this module owns
|
||||
//! everything after a raw RGBA frame arrives.
|
||||
|
||||
use lightningbeam_core::export::ExportProgress;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Message from the UI (render) thread to the GIF encoder coordinator.
|
||||
pub enum GifFrameMessage {
|
||||
/// One RGBA8 frame (top-left origin, tightly packed `width*height*4` bytes).
|
||||
Frame { frame_num: usize, pixels: Vec<u8> },
|
||||
/// All frames have been sent.
|
||||
Done,
|
||||
}
|
||||
|
||||
/// gif crate quantization speed (1 = slowest/best, 30 = fastest/worst). 10 balances palette quality
|
||||
/// against per-frame cost; the parallelism below is what actually recovers the wall-clock.
|
||||
const QUANT_SPEED: i32 = 10;
|
||||
|
||||
/// Run the GIF encoder pipeline. Receives RGBA8 frames from `frame_rx`, quantizes them in parallel,
|
||||
/// and writes the ordered result to `output_path`, reporting progress. `transparency == false`
|
||||
/// composites each frame onto opaque black first (GIF's 1-bit transparency would otherwise key out
|
||||
/// semi-transparent pixels).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn run_gif_encoder(
|
||||
frame_rx: Receiver<GifFrameMessage>,
|
||||
output_path: PathBuf,
|
||||
width: u32,
|
||||
height: u32,
|
||||
total_frames: usize,
|
||||
delay_ms: u32,
|
||||
loop_forever: bool,
|
||||
transparency: bool,
|
||||
progress_tx: Sender<ExportProgress>,
|
||||
cancel_flag: Arc<AtomicBool>,
|
||||
) {
|
||||
let _ = progress_tx.send(ExportProgress::Started { total_frames });
|
||||
|
||||
let delay_cs = ((delay_ms / 10).max(1)) as u16;
|
||||
let expected_len = (width as usize) * (height as usize) * 4;
|
||||
|
||||
// One quantizer worker per spare core (leave one for the UI render thread), capped so we don't
|
||||
// spawn absurdly many for short exports.
|
||||
let n_workers = std::thread::available_parallelism()
|
||||
.map(|n| n.get().saturating_sub(1))
|
||||
.unwrap_or(1)
|
||||
.clamp(1, 8);
|
||||
|
||||
// Per-worker input channels (coordinator dispatches round-robin) + one shared result channel.
|
||||
let mut worker_txs: Vec<Sender<(usize, Vec<u8>)>> = Vec::with_capacity(n_workers);
|
||||
let (result_tx, result_rx) = channel::<(usize, gif::Frame<'static>)>();
|
||||
let mut worker_handles = Vec::with_capacity(n_workers);
|
||||
|
||||
for _ in 0..n_workers {
|
||||
let (wtx, wrx) = channel::<(usize, Vec<u8>)>();
|
||||
worker_txs.push(wtx);
|
||||
let result_tx = result_tx.clone();
|
||||
let cancel = Arc::clone(&cancel_flag);
|
||||
worker_handles.push(std::thread::spawn(move || {
|
||||
while let Ok((idx, mut pixels)) = wrx.recv() {
|
||||
if cancel.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
// NeuQuant local-palette quantization (the expensive step). `from_rgba_speed` uses
|
||||
// the RGBA buffer as scratch, so it's fine that we own `pixels` here.
|
||||
let mut frame =
|
||||
gif::Frame::from_rgba_speed(width as u16, height as u16, &mut pixels, QUANT_SPEED);
|
||||
frame.delay = delay_cs;
|
||||
if result_tx.send((idx, frame)).is_err() {
|
||||
break; // writer gone
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
drop(result_tx); // only the workers hold senders now; writer's rx ends when they all finish
|
||||
|
||||
// Writer thread: order frames by index and LZW-encode them sequentially.
|
||||
let writer_progress = progress_tx.clone();
|
||||
let writer_cancel = Arc::clone(&cancel_flag);
|
||||
let writer_output = output_path.clone();
|
||||
let writer = std::thread::spawn(move || -> Result<(), String> {
|
||||
let file = std::fs::File::create(&writer_output)
|
||||
.map_err(|e| format!("Failed to create GIF file: {e}"))?;
|
||||
let mut buf = std::io::BufWriter::new(file);
|
||||
let mut encoder = gif::Encoder::new(&mut buf, width as u16, height as u16, &[])
|
||||
.map_err(|e| format!("GIF encoder init failed: {e}"))?;
|
||||
if loop_forever {
|
||||
encoder
|
||||
.set_repeat(gif::Repeat::Infinite)
|
||||
.map_err(|e| format!("GIF set_repeat failed: {e}"))?;
|
||||
}
|
||||
|
||||
// Frames may arrive out of order; hold stragglers until their turn.
|
||||
let mut pending: HashMap<usize, gif::Frame<'static>> = HashMap::new();
|
||||
let mut next = 0usize;
|
||||
let mut written = 0usize;
|
||||
|
||||
while let Ok((idx, frame)) = result_rx.recv() {
|
||||
if writer_cancel.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
pending.insert(idx, frame);
|
||||
while let Some(f) = pending.remove(&next) {
|
||||
encoder
|
||||
.write_frame(&f)
|
||||
.map_err(|e| format!("GIF write_frame failed: {e}"))?;
|
||||
next += 1;
|
||||
written += 1;
|
||||
let _ = writer_progress.send(ExportProgress::FrameRendered {
|
||||
frame: written,
|
||||
total: total_frames,
|
||||
});
|
||||
}
|
||||
}
|
||||
// Encoder/BufWriter flush on drop.
|
||||
Ok(())
|
||||
});
|
||||
|
||||
// Coordinator: pull RGBA frames from the UI thread and dispatch round-robin to the workers.
|
||||
let mut dispatched = 0usize;
|
||||
let mut fatal: Option<String> = None;
|
||||
loop {
|
||||
match frame_rx.recv() {
|
||||
Ok(GifFrameMessage::Frame { frame_num, mut pixels }) => {
|
||||
if cancel_flag.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
if pixels.len() != expected_len {
|
||||
fatal = Some("GIF frame size mismatch".into());
|
||||
break;
|
||||
}
|
||||
if !transparency {
|
||||
// Premultiply onto opaque black, then force alpha opaque.
|
||||
for px in pixels.chunks_exact_mut(4) {
|
||||
let a = px[3] as u32;
|
||||
px[0] = (px[0] as u32 * a / 255) as u8;
|
||||
px[1] = (px[1] as u32 * a / 255) as u8;
|
||||
px[2] = (px[2] as u32 * a / 255) as u8;
|
||||
px[3] = 255;
|
||||
}
|
||||
}
|
||||
let w = dispatched % n_workers;
|
||||
if worker_txs[w].send((frame_num, pixels)).is_err() {
|
||||
fatal = Some("GIF quantizer worker died".into());
|
||||
break;
|
||||
}
|
||||
dispatched += 1;
|
||||
}
|
||||
Ok(GifFrameMessage::Done) => break,
|
||||
Err(_) => break, // UI thread dropped the sender
|
||||
}
|
||||
}
|
||||
|
||||
let _ = progress_tx.send(ExportProgress::Finalizing);
|
||||
|
||||
// Close worker inputs → workers finish → their result senders drop → writer's loop ends.
|
||||
drop(worker_txs);
|
||||
for h in worker_handles {
|
||||
let _ = h.join();
|
||||
}
|
||||
let writer_result = writer.join().unwrap_or_else(|_| Err("GIF writer thread panicked".into()));
|
||||
|
||||
if cancel_flag.load(Ordering::Relaxed) {
|
||||
std::fs::remove_file(&output_path).ok();
|
||||
// Emit Complete so the UI poll loop clears its state; the dialog was closed on cancel.
|
||||
let _ = progress_tx.send(ExportProgress::Complete { output_path });
|
||||
return;
|
||||
}
|
||||
|
||||
match fatal.or_else(|| writer_result.err()) {
|
||||
Some(message) => {
|
||||
std::fs::remove_file(&output_path).ok();
|
||||
let _ = progress_tx.send(ExportProgress::Error { message });
|
||||
}
|
||||
None => {
|
||||
let _ = progress_tx.send(ExportProgress::Complete { output_path });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,9 +175,7 @@ impl GpuYuv {
|
||||
}
|
||||
|
||||
/// CPU reference for the exact math/layout the shader produces — used by unit tests so
|
||||
/// the packing and BT.709 coefficients stay verifiable without a GPU. Test-only, so it isn't
|
||||
/// compiled into (and flagged as unused by) release builds.
|
||||
#[cfg(test)]
|
||||
/// the packing and BT.709 coefficients stay verifiable without a GPU.
|
||||
fn cpu_reference(rgba: &[u8], width: u32, height: u32, full_range: bool) -> Vec<u8> {
|
||||
let w = width as usize;
|
||||
let h = height as usize;
|
||||
|
||||
@@ -45,179 +45,13 @@ pub fn save_rgba_image(
|
||||
encoder.encode_image(&rgb_img).map_err(|e| format!("JPEG encode failed: {e}"))
|
||||
}
|
||||
ImageFormat::WebP => {
|
||||
// `image` 0.25's WebP encoder is lossless-only, which ignored the quality slider and
|
||||
// produced needlessly large files. Encode lossy WebP via ffmpeg's libwebp instead so
|
||||
// the quality control is real; alpha is preserved (as YUVA420P) when requested.
|
||||
save_webp_ffmpeg(pixels, width, height, quality, allow_transparency, path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode a single frame as lossy WebP via ffmpeg's `libwebp` encoder.
|
||||
///
|
||||
/// `quality` is libwebp's 0–100 quality factor. When `allow_transparency` is true the source is
|
||||
/// converted to YUVA420P so libwebp keeps the alpha channel; otherwise it's flattened onto black
|
||||
/// and converted to YUV420P. Uses swscale's default BT.601 conversion (matching a plain
|
||||
/// `ffmpeg -i in.png out.webp`).
|
||||
fn save_webp_ffmpeg(
|
||||
pixels: &[u8],
|
||||
width: u32,
|
||||
height: u32,
|
||||
quality: u8,
|
||||
allow_transparency: bool,
|
||||
path: &Path,
|
||||
) -> Result<(), String> {
|
||||
use ffmpeg_next as ffmpeg;
|
||||
|
||||
ffmpeg::init().map_err(|e| format!("Failed to initialize ffmpeg: {e}"))?;
|
||||
|
||||
let codec = ffmpeg::encoder::find_by_name("libwebp")
|
||||
.or_else(|| ffmpeg::encoder::find(ffmpeg::codec::Id::WEBP))
|
||||
.ok_or("libwebp encoder not available in this ffmpeg build")?;
|
||||
|
||||
// Flatten onto black up front when alpha isn't wanted, so the source is fully opaque.
|
||||
let src_rgba: Vec<u8> = if allow_transparency {
|
||||
pixels.to_vec()
|
||||
} else {
|
||||
let mut v = pixels.to_vec();
|
||||
for px in v.chunks_exact_mut(4) {
|
||||
let a = px[3] as u32;
|
||||
px[0] = (px[0] as u32 * a / 255) as u8;
|
||||
px[1] = (px[1] as u32 * a / 255) as u8;
|
||||
px[2] = (px[2] as u32 * a / 255) as u8;
|
||||
px[3] = 255;
|
||||
}
|
||||
v
|
||||
};
|
||||
|
||||
let dst_pix = if allow_transparency {
|
||||
ffmpeg::format::Pixel::YUVA420P
|
||||
} else {
|
||||
ffmpeg::format::Pixel::YUV420P
|
||||
};
|
||||
|
||||
// RGBA → YUV(A)420P (swscale defaults: BT.601, limited range — what libwebp expects).
|
||||
let mut scaler = ffmpeg::software::scaling::Context::get(
|
||||
ffmpeg::format::Pixel::RGBA, width, height,
|
||||
dst_pix, width, height,
|
||||
ffmpeg::software::scaling::Flags::BILINEAR,
|
||||
)
|
||||
.map_err(|e| format!("Failed to create swscale context: {e}"))?;
|
||||
|
||||
let mut src = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::RGBA, width, height);
|
||||
// Copy row-by-row honoring the frame's stride (may exceed width*4 due to alignment padding).
|
||||
let stride = src.stride(0);
|
||||
let row_bytes = (width * 4) as usize;
|
||||
{
|
||||
let dst = src.data_mut(0);
|
||||
for y in 0..height as usize {
|
||||
let s = y * row_bytes;
|
||||
let d = y * stride;
|
||||
dst[d..d + row_bytes].copy_from_slice(&src_rgba[s..s + row_bytes]);
|
||||
}
|
||||
}
|
||||
|
||||
let mut yuv = ffmpeg::frame::Video::new(dst_pix, width, height);
|
||||
scaler.run(&src, &mut yuv).map_err(|e| format!("swscale conversion failed: {e}"))?;
|
||||
yuv.set_pts(Some(0));
|
||||
|
||||
let mut octx = ffmpeg::format::output(&path)
|
||||
.map_err(|e| format!("Failed to create WebP output: {e}"))?;
|
||||
|
||||
let mut enc = ffmpeg::codec::Context::new_with_codec(codec)
|
||||
.encoder()
|
||||
.video()
|
||||
.map_err(|e| format!("Failed to create WebP encoder: {e}"))?;
|
||||
enc.set_width(width);
|
||||
enc.set_height(height);
|
||||
enc.set_format(dst_pix);
|
||||
enc.set_time_base(ffmpeg::Rational(1, 1));
|
||||
|
||||
// libwebp private options: quality 0–100, lossy.
|
||||
let mut opts = ffmpeg::Dictionary::new();
|
||||
opts.set("quality", &quality.to_string());
|
||||
opts.set("lossless", "0");
|
||||
let mut enc = enc
|
||||
.open_with(opts)
|
||||
.map_err(|e| format!("Failed to open libwebp encoder: {e}"))?;
|
||||
|
||||
{
|
||||
let mut stream = octx.add_stream(codec)
|
||||
.map_err(|e| format!("Failed to add WebP stream: {e}"))?;
|
||||
stream.set_parameters(&enc);
|
||||
stream.set_time_base(ffmpeg::Rational(1, 1));
|
||||
}
|
||||
|
||||
octx.write_header().map_err(|e| format!("Failed to write WebP header: {e}"))?;
|
||||
enc.send_frame(&yuv).map_err(|e| format!("Failed to send WebP frame: {e}"))?;
|
||||
enc.send_eof().map_err(|e| format!("Failed to flush WebP encoder: {e}"))?;
|
||||
|
||||
let mut packet = ffmpeg::Packet::empty();
|
||||
while enc.receive_packet(&mut packet).is_ok() {
|
||||
packet.set_stream(0);
|
||||
packet
|
||||
.write_interleaved(&mut octx)
|
||||
.map_err(|e| format!("Failed to write WebP packet: {e}"))?;
|
||||
}
|
||||
|
||||
octx.write_trailer().map_err(|e| format!("Failed to finalize WebP: {e}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use lightningbeam_core::export::ImageFormat;
|
||||
|
||||
/// A gradient RGBA image so the encoder has real content to quantize/compress.
|
||||
fn gradient(width: u32, height: u32) -> Vec<u8> {
|
||||
let mut px = Vec::with_capacity((width * height * 4) as usize);
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
px.push((x * 255 / width.max(1)) as u8);
|
||||
px.push((y * 255 / height.max(1)) as u8);
|
||||
px.push(128);
|
||||
px.push(255);
|
||||
if allow_transparency {
|
||||
img.save(path).map_err(|e| format!("WebP save failed: {e}"))
|
||||
} else {
|
||||
let flat = flatten_alpha(img);
|
||||
flat.save(path).map_err(|e| format!("WebP save failed: {e}"))
|
||||
}
|
||||
}
|
||||
px
|
||||
}
|
||||
|
||||
/// The ffmpeg libwebp path must produce a valid *lossy* WebP (RIFF/WEBP container with a
|
||||
/// `VP8 ` chunk — lossless would be `VP8L`), and the quality knob must actually change size.
|
||||
#[test]
|
||||
fn webp_export_is_real_lossy() {
|
||||
let (w, h) = (96u32, 64u32);
|
||||
let px = gradient(w, h);
|
||||
let dir = std::env::temp_dir();
|
||||
let lo = dir.join("lb_webp_q10_test.webp");
|
||||
let hi = dir.join("lb_webp_q95_test.webp");
|
||||
|
||||
save_webp_ffmpeg(&px, w, h, 10, false, &lo).expect("low-quality webp encode");
|
||||
save_webp_ffmpeg(&px, w, h, 95, false, &hi).expect("high-quality webp encode");
|
||||
|
||||
let lo_bytes = std::fs::read(&lo).unwrap();
|
||||
let hi_bytes = std::fs::read(&hi).unwrap();
|
||||
|
||||
// RIFF....WEBP container.
|
||||
assert_eq!(&lo_bytes[0..4], b"RIFF", "not a RIFF container");
|
||||
assert_eq!(&lo_bytes[8..12], b"WEBP", "not a WEBP file");
|
||||
// Lossy VP8 chunk (`VP8 ` with trailing space), NOT lossless `VP8L`.
|
||||
assert_eq!(&lo_bytes[12..16], b"VP8 ", "expected lossy VP8, got {:?}", &lo_bytes[12..16]);
|
||||
// The quality knob is honored: q10 is meaningfully smaller than q95.
|
||||
assert!(lo_bytes.len() < hi_bytes.len(),
|
||||
"quality ignored: q10 {} bytes >= q95 {} bytes", lo_bytes.len(), hi_bytes.len());
|
||||
|
||||
std::fs::remove_file(&lo).ok();
|
||||
std::fs::remove_file(&hi).ok();
|
||||
}
|
||||
|
||||
/// The format enum still advertises a quality control for WebP (now that it works).
|
||||
#[test]
|
||||
fn webp_has_quality() {
|
||||
assert!(ImageFormat::WebP.has_quality());
|
||||
assert!(ImageFormat::Jpeg.has_quality());
|
||||
assert!(!ImageFormat::Png.has_quality());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
//! This module provides the export orchestrator and progress tracking
|
||||
//! for exporting audio and video from the timeline.
|
||||
|
||||
pub mod audio_exporter;
|
||||
pub mod dialog;
|
||||
pub mod gif_exporter;
|
||||
pub mod image_exporter;
|
||||
pub mod video_exporter;
|
||||
pub mod readback_pipeline;
|
||||
@@ -13,8 +13,7 @@ pub mod cpu_yuv_converter;
|
||||
pub mod gpu_yuv;
|
||||
pub mod hdr_frame;
|
||||
|
||||
use lightningbeam_core::export::{AudioExportSettings, GifExportSettings, ImageExportSettings, VideoExportSettings, ExportProgress};
|
||||
use gif_exporter::GifFrameMessage;
|
||||
use lightningbeam_core::export::{AudioExportSettings, ImageExportSettings, VideoExportSettings, ExportProgress};
|
||||
use lightningbeam_core::document::Document;
|
||||
use lightningbeam_core::renderer::ImageCache;
|
||||
use lightningbeam_core::video::VideoManager;
|
||||
@@ -69,11 +68,6 @@ pub struct VideoExportState {
|
||||
readback_pipeline: Option<readback_pipeline::ReadbackPipeline>,
|
||||
/// CPU YUV converter for RGBA→YUV420p conversion
|
||||
cpu_yuv_converter: Option<cpu_yuv_converter::CpuYuvConverter>,
|
||||
/// ProRes 422 export: forces the CPU (RGBA) readback path and converts to 10-bit 4:2:2 instead
|
||||
/// of 8-bit 4:2:0. `true` only for `VideoCodec::ProRes422` (SDR).
|
||||
prores: bool,
|
||||
/// CPU RGBA→YUV422P10LE converter, used only on the ProRes path.
|
||||
cpu_yuv422p10: Option<cpu_yuv_converter::CpuYuv422P10Converter>,
|
||||
/// Frames that have been submitted to GPU but not yet encoded
|
||||
frames_in_flight: usize,
|
||||
/// Next frame number to send to encoder (for ordering)
|
||||
@@ -137,38 +131,6 @@ pub struct ExportOrchestrator {
|
||||
|
||||
/// Single-frame image export state
|
||||
image_state: Option<ImageExportState>,
|
||||
|
||||
/// Animated GIF export state (frames rendered on the UI thread, encoded on `thread_handle`).
|
||||
gif_state: Option<GifExportState>,
|
||||
}
|
||||
|
||||
/// State for an in-progress animated GIF export. Frames are rendered + read back on the UI thread
|
||||
/// (one per `render_next_gif_frame` call) and streamed to the encoder thread over `frame_tx`.
|
||||
struct GifExportState {
|
||||
/// Resolved pixel dimensions (after applying any width/height overrides).
|
||||
width: u32,
|
||||
height: u32,
|
||||
/// Total frames to render.
|
||||
total_frames: usize,
|
||||
/// Next frame index to render (0-based).
|
||||
next_frame: usize,
|
||||
/// Document time (seconds) of frame 0.
|
||||
start_time: f64,
|
||||
/// Seconds between frames (1 / framerate).
|
||||
frame_step: f64,
|
||||
/// How the document is fit into the export frame.
|
||||
fit: lightningbeam_core::export::ExportFitMode,
|
||||
/// Preserve alpha as GIF transparency (else the encoder flattens onto black).
|
||||
transparency: bool,
|
||||
/// GPU resources allocated on the first render call, reused each frame.
|
||||
gpu_resources: Option<video_exporter::ExportGpuResources>,
|
||||
/// Output RGBA texture (kept separate from gpu_resources to avoid split-borrow issues).
|
||||
output_texture: Option<wgpu::Texture>,
|
||||
output_texture_view: Option<wgpu::TextureView>,
|
||||
/// Staging buffer for synchronous GPU→CPU readback (reused each frame).
|
||||
staging_buffer: Option<wgpu::Buffer>,
|
||||
/// Sender to the encoder thread; dropped after the final frame to signal completion.
|
||||
frame_tx: Option<Sender<GifFrameMessage>>,
|
||||
}
|
||||
|
||||
/// State for parallel audio+video export
|
||||
@@ -206,7 +168,6 @@ impl ExportOrchestrator {
|
||||
video_state: None,
|
||||
parallel_export: None,
|
||||
image_state: None,
|
||||
gif_state: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,7 +250,7 @@ impl ExportOrchestrator {
|
||||
/// unconsumed terminal message). Used to gate the UI poll loop so it doesn't run every
|
||||
/// repaint forever after an export finishes.
|
||||
pub fn has_pending_progress(&self) -> bool {
|
||||
self.parallel_export.is_some() || self.image_state.is_some() || self.gif_state.is_some() || self.progress_rx.is_some()
|
||||
self.parallel_export.is_some() || self.image_state.is_some() || self.progress_rx.is_some()
|
||||
}
|
||||
|
||||
/// Poll progress for parallel video+audio export
|
||||
@@ -573,9 +534,6 @@ impl ExportOrchestrator {
|
||||
}
|
||||
self.video_state = None;
|
||||
self.image_state = None;
|
||||
// Dropping gif_state drops its frame sender, unblocking the encoder thread's recv() so it
|
||||
// observes the cancel flag, removes the partial file, and exits.
|
||||
self.gif_state = None;
|
||||
self.progress_rx = None;
|
||||
self.thread_handle = None;
|
||||
}
|
||||
@@ -584,7 +542,6 @@ impl ExportOrchestrator {
|
||||
pub fn is_exporting(&self) -> bool {
|
||||
if self.parallel_export.is_some() { return true; }
|
||||
if self.image_state.is_some() { return true; }
|
||||
if self.gif_state.is_some() { return true; }
|
||||
if let Some(handle) = &self.thread_handle {
|
||||
!handle.is_finished()
|
||||
} else {
|
||||
@@ -762,196 +719,6 @@ impl ExportOrchestrator {
|
||||
result.map(|_| true)
|
||||
}
|
||||
|
||||
/// Enqueue an animated GIF export. Spawns the encoder thread now; call `render_next_gif_frame()`
|
||||
/// from the egui update loop (where the wgpu device/queue are available) to render + stream each
|
||||
/// frame to it.
|
||||
pub fn start_gif_export(
|
||||
&mut self,
|
||||
settings: GifExportSettings,
|
||||
output_path: PathBuf,
|
||||
doc_width: u32,
|
||||
doc_height: u32,
|
||||
) {
|
||||
self.cancel_flag.store(false, Ordering::Relaxed);
|
||||
|
||||
let width = settings.width.unwrap_or(doc_width).max(1);
|
||||
let height = settings.height.unwrap_or(doc_height).max(1);
|
||||
let total_frames = settings.total_frames();
|
||||
let delay_ms = settings.frame_delay_ms();
|
||||
let frame_step = 1.0 / settings.framerate;
|
||||
|
||||
let (progress_tx, progress_rx) = channel();
|
||||
let (frame_tx, frame_rx) = channel();
|
||||
self.progress_rx = Some(progress_rx);
|
||||
|
||||
let cancel_flag = Arc::clone(&self.cancel_flag);
|
||||
let loop_forever = settings.loop_forever;
|
||||
let transparency = settings.transparency;
|
||||
let handle = std::thread::spawn(move || {
|
||||
gif_exporter::run_gif_encoder(
|
||||
frame_rx, output_path, width, height, total_frames, delay_ms,
|
||||
loop_forever, transparency, progress_tx, cancel_flag,
|
||||
);
|
||||
});
|
||||
self.thread_handle = Some(handle);
|
||||
|
||||
self.gif_state = Some(GifExportState {
|
||||
width,
|
||||
height,
|
||||
total_frames,
|
||||
next_frame: 0,
|
||||
start_time: settings.start_time,
|
||||
frame_step,
|
||||
fit: settings.fit,
|
||||
transparency,
|
||||
gpu_resources: None,
|
||||
output_texture: None,
|
||||
output_texture_view: None,
|
||||
staging_buffer: None,
|
||||
frame_tx: Some(frame_tx),
|
||||
});
|
||||
}
|
||||
|
||||
/// Drive the animated GIF export: render + read back one frame per call and stream it to the
|
||||
/// encoder thread. Returns `Ok(true)` while more frames remain (call again next egui frame),
|
||||
/// `Ok(false)` once every frame has been sent (encoding then finishes on the background thread).
|
||||
pub fn render_next_gif_frame(
|
||||
&mut self,
|
||||
document: &mut Document,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
renderer: &mut vello::Renderer,
|
||||
image_cache: &mut ImageCache,
|
||||
video_manager: &Arc<std::sync::Mutex<VideoManager>>,
|
||||
raster_store: Option<&lightningbeam_core::raster_store::RasterStore>,
|
||||
) -> Result<bool, String> {
|
||||
if self.cancel_flag.load(Ordering::Relaxed) {
|
||||
// Dropping frame_tx unblocks the encoder thread's recv() so it can clean up.
|
||||
self.gif_state = None;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let state = match self.gif_state.as_mut() {
|
||||
Some(s) => s,
|
||||
None => return Ok(false),
|
||||
};
|
||||
|
||||
// All frames sent → drop the sender (signals the encoder to finalize) and finish.
|
||||
if state.next_frame >= state.total_frames {
|
||||
if let Some(tx) = state.frame_tx.take() {
|
||||
let _ = tx.send(GifFrameMessage::Done);
|
||||
drop(tx);
|
||||
}
|
||||
self.gif_state = None;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let w = state.width;
|
||||
let h = state.height;
|
||||
let fit = state.fit;
|
||||
let timestamp = state.start_time + state.next_frame as f64 * state.frame_step;
|
||||
|
||||
if state.gpu_resources.is_none() {
|
||||
state.gpu_resources = Some(video_exporter::ExportGpuResources::new(device, w, h));
|
||||
}
|
||||
if state.output_texture.is_none() {
|
||||
let tex = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("gif_export_output"),
|
||||
size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8Unorm,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
|
||||
view_formats: &[],
|
||||
});
|
||||
state.output_texture_view = Some(tex.create_view(&wgpu::TextureViewDescriptor::default()));
|
||||
state.output_texture = Some(tex);
|
||||
}
|
||||
|
||||
// Render the frame (transparency preserved through readback; the encoder flattens if needed).
|
||||
{
|
||||
let gpu = state.gpu_resources.as_mut().unwrap();
|
||||
let output_view = state.output_texture_view.as_ref().unwrap();
|
||||
let encoder = video_exporter::render_frame_to_gpu_rgba(
|
||||
document,
|
||||
timestamp,
|
||||
w, h,
|
||||
device, queue, renderer, image_cache, video_manager,
|
||||
gpu,
|
||||
output_view,
|
||||
None, // no floating raster selection during export
|
||||
state.transparency,
|
||||
raster_store,
|
||||
true, // GIF export composites on the shared device
|
||||
fit,
|
||||
)?;
|
||||
queue.submit(Some(encoder.finish()));
|
||||
}
|
||||
|
||||
// Synchronous readback (wgpu requires bytes_per_row aligned to 256).
|
||||
let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;
|
||||
let bytes_per_row = (w * 4 + align - 1) / align * align;
|
||||
if state.staging_buffer.is_none() {
|
||||
state.staging_buffer = Some(device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("gif_export_staging"),
|
||||
size: (bytes_per_row * h) as u64,
|
||||
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
|
||||
mapped_at_creation: false,
|
||||
}));
|
||||
}
|
||||
let staging = state.staging_buffer.as_ref().unwrap();
|
||||
|
||||
let mut copy_enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("gif_export_copy"),
|
||||
});
|
||||
let output_tex = state.output_texture.as_ref().unwrap();
|
||||
copy_enc.copy_texture_to_buffer(
|
||||
wgpu::TexelCopyTextureInfo {
|
||||
texture: output_tex,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d::ZERO,
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
},
|
||||
wgpu::TexelCopyBufferInfo {
|
||||
buffer: staging,
|
||||
layout: wgpu::TexelCopyBufferLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: Some(bytes_per_row),
|
||||
rows_per_image: Some(h),
|
||||
},
|
||||
},
|
||||
wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
|
||||
);
|
||||
queue.submit(Some(copy_enc.finish()));
|
||||
|
||||
let slice = staging.slice(..);
|
||||
slice.map_async(wgpu::MapMode::Read, |_| {});
|
||||
let _ = device.poll(wgpu::PollType::wait_indefinitely());
|
||||
|
||||
let pixels: Vec<u8> = {
|
||||
let mapped = slice.get_mapped_range();
|
||||
let mut out = Vec::with_capacity((w * h * 4) as usize);
|
||||
for row in 0..h {
|
||||
let start = (row * bytes_per_row) as usize;
|
||||
out.extend_from_slice(&mapped[start..start + (w * 4) as usize]);
|
||||
}
|
||||
out
|
||||
};
|
||||
staging.unmap();
|
||||
|
||||
let frame_num = state.next_frame;
|
||||
if let Some(tx) = state.frame_tx.as_ref() {
|
||||
// If the encoder thread died, stop — its dropped receiver returns an error here.
|
||||
if tx.send(GifFrameMessage::Frame { frame_num, pixels }).is_err() {
|
||||
self.gif_state = None;
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
state.next_frame += 1;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Wait for the export to complete
|
||||
///
|
||||
/// This blocks until the export thread finishes.
|
||||
@@ -1007,12 +774,6 @@ impl ExportOrchestrator {
|
||||
start_time: daw_backend::Seconds(settings.start_time),
|
||||
end_time: daw_backend::Seconds(settings.end_time),
|
||||
tempo_map: daw_backend::TempoMap::constant(settings.bpm),
|
||||
metadata: settings
|
||||
.metadata
|
||||
.pairs()
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.to_string(), v.to_string()))
|
||||
.collect(),
|
||||
};
|
||||
|
||||
// Use DAW backend export for all formats
|
||||
@@ -1111,8 +872,6 @@ impl ExportOrchestrator {
|
||||
let hdr = settings.hdr;
|
||||
let fit = settings.fit;
|
||||
let full_range = settings.color_range.is_full();
|
||||
let prores = matches!(settings.codec, lightningbeam_core::export::VideoCodec::ProRes422)
|
||||
&& !hdr.is_hdr();
|
||||
let handle = std::thread::spawn(move || {
|
||||
Self::run_video_encoder(settings, output_path, frame_rx, progress_tx, cancel_flag, total_frames);
|
||||
});
|
||||
@@ -1132,8 +891,6 @@ impl ExportOrchestrator {
|
||||
gpu_resources: None,
|
||||
readback_pipeline: None,
|
||||
cpu_yuv_converter: None,
|
||||
prores,
|
||||
cpu_yuv422p10: None,
|
||||
frames_in_flight: 0,
|
||||
next_frame_to_encode: 0,
|
||||
perf_metrics: Some(perf_metrics::ExportMetrics::new()),
|
||||
@@ -1364,10 +1121,7 @@ impl ExportOrchestrator {
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
|
||||
// Use the codec's real container for the temp video, not a hardcoded .mp4 — VP8 isn't a
|
||||
// valid MP4 codec, so an .mp4 temp made `write_header` fail for any VP8+audio export.
|
||||
let temp_video_path = temp_dir.join(format!("lightningbeam_video_{}.{}",
|
||||
timestamp, video_settings.codec.container_format()));
|
||||
let temp_video_path = temp_dir.join(format!("lightningbeam_video_{}.mp4", timestamp));
|
||||
let temp_audio_path = temp_dir.join(format!("lightningbeam_audio_{}.{}",
|
||||
timestamp,
|
||||
match audio_settings.format {
|
||||
@@ -1577,34 +1331,24 @@ impl ExportOrchestrator {
|
||||
// Enable GPU YUV only when the encoder's YUV420P planes are tight (no linesize
|
||||
// padding) — then the packed GPU planes copy in without row misalignment.
|
||||
// Otherwise fall back to RGBA readback + CPU swscale.
|
||||
// ProRes needs 10-bit 4:2:2 (built on the CPU from the RGBA readback), so it forces the
|
||||
// RGBA path — the GPU YUV converter only produces 8-bit 4:2:0.
|
||||
let gpu_yuv_tight = !state.prores && std::env::var("LB_DISABLE_GPU_YUV").is_err() && {
|
||||
let gpu_yuv_tight = std::env::var("LB_DISABLE_GPU_YUV").is_err() && {
|
||||
let probe = ffmpeg_next::frame::Video::new(
|
||||
ffmpeg_next::format::Pixel::YUV420P, width, height,
|
||||
);
|
||||
probe.stride(0) == width as usize && probe.stride(1) == (width / 2) as usize
|
||||
};
|
||||
if !gpu_yuv_tight && !state.prores {
|
||||
if !gpu_yuv_tight {
|
||||
println!("🎬 [VIDEO EXPORT] YUV planes are padded at {width}x{height}; using CPU YUV path");
|
||||
}
|
||||
state.readback_pipeline = Some(readback_pipeline::ReadbackPipeline::new(device, queue, width, height, gpu_yuv_tight, state.full_range));
|
||||
if state.prores {
|
||||
state.cpu_yuv422p10 = Some(cpu_yuv_converter::CpuYuv422P10Converter::new(width, height, state.full_range)?);
|
||||
println!("🎬 [VIDEO EXPORT] ProRes 422: 10-bit 4:2:2 (YUV422P10LE) CPU converter initialized");
|
||||
} else {
|
||||
state.cpu_yuv_converter = Some(cpu_yuv_converter::CpuYuvConverter::new(width, height, state.full_range)?);
|
||||
}
|
||||
state.cpu_yuv_converter = Some(cpu_yuv_converter::CpuYuvConverter::new(width, height, state.full_range)?);
|
||||
println!("🚀 [ASYNC PIPELINE] Triple-buffered pipeline initialized");
|
||||
println!("🚀 [CPU YUV] swscale converter initialized");
|
||||
}
|
||||
|
||||
let pipeline = state.readback_pipeline.as_mut().unwrap();
|
||||
let gpu_resources = state.gpu_resources.as_mut().unwrap();
|
||||
// Exactly one of these is present: cpu_yuv422p10 on the ProRes path, cpu_converter on the
|
||||
// SDR fallback path (or neither is used when the GPU YUV converter is active).
|
||||
let mut cpu_converter = state.cpu_yuv_converter.as_mut();
|
||||
let mut cpu_yuv422p10 = state.cpu_yuv422p10.as_mut();
|
||||
let cpu_converter = state.cpu_yuv_converter.as_mut().unwrap();
|
||||
let mut metrics = state.perf_metrics.as_mut();
|
||||
|
||||
// Poll for completed async readbacks (non-blocking)
|
||||
@@ -1631,17 +1375,12 @@ impl ExportOrchestrator {
|
||||
let data = pipeline.extract_rgba_data(result.buffer_id);
|
||||
let extraction_end = Instant::now();
|
||||
|
||||
// YUV planes: ProRes 10-bit 4:2:2, else GPU-converted (just slice), else CPU
|
||||
// swscale 8-bit 4:2:0 fallback (timed).
|
||||
// YUV planes: GPU-converted (just slice) or CPU swscale fallback (timed).
|
||||
let conversion_start = Instant::now();
|
||||
let (y, u, v) = if let Some(conv) = cpu_yuv422p10.as_deref_mut() {
|
||||
conv.convert(&data)?
|
||||
} else if pipeline.is_yuv_mode() {
|
||||
let (y, u, v) = if pipeline.is_yuv_mode() {
|
||||
pipeline.split_yuv(&data)
|
||||
} else {
|
||||
cpu_converter.as_deref_mut()
|
||||
.ok_or("SDR export missing its CPU YUV converter")?
|
||||
.convert(&data)?
|
||||
cpu_converter.convert(&data)?
|
||||
};
|
||||
let conversion_end = Instant::now();
|
||||
|
||||
@@ -1730,7 +1469,6 @@ impl ExportOrchestrator {
|
||||
state.gpu_resources = None;
|
||||
state.readback_pipeline = None;
|
||||
state.cpu_yuv_converter = None;
|
||||
state.cpu_yuv422p10 = None;
|
||||
state.perf_metrics = None;
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -1980,8 +1718,6 @@ impl ExportOrchestrator {
|
||||
// Pixel format the encoder frames are built in (matches setup_video_encoder).
|
||||
let pixel_format = if settings.hdr.is_hdr() {
|
||||
ffmpeg_next::format::Pixel::YUV420P10LE
|
||||
} else if matches!(settings.codec, VideoCodec::ProRes422) {
|
||||
ffmpeg_next::format::Pixel::YUV422P10LE // ProRes 422: 10-bit 4:2:2
|
||||
} else {
|
||||
ffmpeg_next::format::Pixel::YUV420P
|
||||
};
|
||||
@@ -2096,17 +1832,8 @@ impl ExportOrchestrator {
|
||||
|
||||
// Copy each plane row-by-row honoring the frame's stride (10-bit / arbitrary widths can have
|
||||
// row padding that a flat copy would misalign). `bytes_per_row` = samples × sample size.
|
||||
// Sample size + chroma subsampling depend on the pixel format:
|
||||
// YUV420P → 8-bit, 4:2:0 (chroma = w/2 × h/2)
|
||||
// YUV420P10LE → 10-bit, 4:2:0 (chroma = w/2 × h/2)
|
||||
// YUV422P10LE → 10-bit, 4:2:2 (chroma = w/2 × h, full-height) [ProRes]
|
||||
use ffmpeg_next::format::Pixel;
|
||||
let (sample_bytes, chroma_h_div) = match pixel_format {
|
||||
Pixel::YUV420P => (1usize, 2usize),
|
||||
Pixel::YUV420P10LE => (2usize, 2usize),
|
||||
Pixel::YUV422P10LE => (2usize, 1usize),
|
||||
_ => (1usize, 2usize),
|
||||
};
|
||||
let ten_bit = matches!(pixel_format, ffmpeg_next::format::Pixel::YUV420P10LE);
|
||||
let sample_bytes = if ten_bit { 2usize } else { 1usize };
|
||||
let copy_plane = |frame: &mut ffmpeg_next::frame::Video, idx: usize, src: &[u8], w: usize, h: usize| {
|
||||
let bytes_per_row = w * sample_bytes;
|
||||
let stride = frame.stride(idx);
|
||||
@@ -2121,8 +1848,8 @@ impl ExportOrchestrator {
|
||||
};
|
||||
let (w, h) = (width as usize, height as usize);
|
||||
copy_plane(&mut video_frame, 0, y_plane, w, h);
|
||||
copy_plane(&mut video_frame, 1, u_plane, w / 2, h / chroma_h_div);
|
||||
copy_plane(&mut video_frame, 2, v_plane, w / 2, h / chroma_h_div);
|
||||
copy_plane(&mut video_frame, 1, u_plane, w / 2, h / 2);
|
||||
copy_plane(&mut video_frame, 2, v_plane, w / 2, h / 2);
|
||||
|
||||
// Set PTS (presentation timestamp) in encoder's time base
|
||||
// Encoder time base is 1/(framerate * 1000), so PTS = timestamp * (framerate * 1000)
|
||||
|
||||
@@ -616,12 +616,9 @@ pub fn setup_video_encoder(
|
||||
// Configure encoder parameters BEFORE opening (critical!)
|
||||
encoder.set_width(aligned_width);
|
||||
encoder.set_height(aligned_height);
|
||||
// ProRes needs 10-bit 4:2:2; HDR needs 10-bit 4:2:0 BT.2020; other SDR is 8-bit 4:2:0.
|
||||
let is_prores = codec_id == ffmpeg::codec::Id::PRORES;
|
||||
// HDR encodes 10-bit BT.2020 (limited range); SDR keeps 8-bit full-range BT.709.
|
||||
if hdr.is_hdr() {
|
||||
encoder.set_format(ffmpeg::format::Pixel::YUV420P10LE);
|
||||
} else if is_prores {
|
||||
encoder.set_format(ffmpeg::format::Pixel::YUV422P10LE);
|
||||
} else {
|
||||
encoder.set_format(ffmpeg::format::Pixel::YUV420P);
|
||||
}
|
||||
@@ -653,10 +650,6 @@ pub fn setup_video_encoder(
|
||||
});
|
||||
color_opts.set("color_primaries", "bt709");
|
||||
color_opts.set("color_trc", "bt709");
|
||||
if is_prores {
|
||||
// prores_ks profile: 3 = HQ (4:2:2 10-bit). Matches the YUV422P10LE frames we feed.
|
||||
color_opts.set("profile", "3");
|
||||
}
|
||||
}
|
||||
|
||||
println!("📐 Video dimensions: {}×{} (aligned to {}×{}){}",
|
||||
@@ -1062,18 +1055,10 @@ fn composite_document_to_hdr(
|
||||
let success = gpu_resources.effect_processor.compile_effect(device, effect_def);
|
||||
if !success { eprintln!("Failed to compile effect: {}", effect_def.name); continue; }
|
||||
}
|
||||
let tempo_map = document.tempo_map();
|
||||
let effect_end_beats = effect_instance.timeline_start
|
||||
+ effect_instance.effective_duration(
|
||||
lightningbeam_core::clip::ClipDuration::Seconds(daw_backend::Seconds(
|
||||
lightningbeam_core::effect::EFFECT_DURATION,
|
||||
)),
|
||||
tempo_map,
|
||||
);
|
||||
let effect_inst = lightningbeam_core::effect::EffectInstance::new(
|
||||
effect_def,
|
||||
tempo_map.beats_to_seconds(effect_instance.timeline_start).seconds_to_f64(),
|
||||
tempo_map.beats_to_seconds(effect_end_beats).seconds_to_f64(),
|
||||
effect_instance.timeline_start,
|
||||
effect_instance.timeline_start + effect_instance.effective_duration(lightningbeam_core::effect::EFFECT_DURATION, document.tempo_map()),
|
||||
);
|
||||
let effect_output_handle = gpu_resources.buffer_pool.acquire(device, hdr_spec);
|
||||
if let Some(effect_output_view) = gpu_resources.buffer_pool.get_view(effect_output_handle) {
|
||||
@@ -1448,30 +1433,6 @@ mod tests {
|
||||
assert!(v[0] > 128, "V value: {}", v[0]);
|
||||
}
|
||||
|
||||
/// ProRes must actually open with the 10-bit 4:2:2 format we now feed it. Before the fix the
|
||||
/// SDR path handed prores_ks 8-bit YUV420P and `open` failed every time — so this opening
|
||||
/// successfully is the regression guard for "ProRes export always errored".
|
||||
#[test]
|
||||
fn prores_encoder_opens_with_yuv422p10() {
|
||||
ffmpeg::init().unwrap();
|
||||
// Skip cleanly if this ffmpeg build lacks a ProRes encoder (rather than false-fail).
|
||||
if ffmpeg::encoder::find(ffmpeg::codec::Id::PRORES).is_none()
|
||||
&& ffmpeg::encoder::find_by_name("prores_ks").is_none()
|
||||
{
|
||||
eprintln!("prores encoder not present in this ffmpeg build; skipping");
|
||||
return;
|
||||
}
|
||||
let r = setup_video_encoder(
|
||||
ffmpeg::codec::Id::PRORES,
|
||||
640, 480, 30.0, 20_000,
|
||||
lightningbeam_core::export::HdrExportMode::Sdr,
|
||||
false,
|
||||
);
|
||||
assert!(r.is_ok(), "ProRes encoder failed to open: {:?}", r.err());
|
||||
let (encoder, _codec) = r.unwrap();
|
||||
assert_eq!(encoder.format(), ffmpeg::format::Pixel::YUV422P10LE);
|
||||
}
|
||||
|
||||
// NOTE: `rgba_to_yuv420p` rounds dimensions up to multiples of 16 (H.264
|
||||
// macroblock alignment), so its plane lengths are the aligned sizes, not the
|
||||
// tight input dimensions. The former `test_rgba_to_yuv420p_dimensions` and
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,142 +0,0 @@
|
||||
//! Lucide icon font (ISC license — see `assets/fonts/LICENSE-lucide.txt`).
|
||||
#![allow(dead_code)] // a forward-looking icon palette; not all are wired up yet
|
||||
//!
|
||||
//! The font is embedded and registered as a named egui font family ("lucide"). Icons are drawn as
|
||||
//! text using [`font`] for the `FontId`, with the codepoint constants below (Lucide's private-use
|
||||
//! area). Run [`install`] once at startup.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use eframe::egui;
|
||||
|
||||
/// The egui font-family name the Lucide glyphs live under.
|
||||
pub const FAMILY: &str = "lucide";
|
||||
|
||||
/// Embed + register the Lucide icon font. Call once with the egui context at startup.
|
||||
pub fn install(ctx: &egui::Context) {
|
||||
let mut fonts = egui::FontDefinitions::default();
|
||||
fonts.font_data.insert(
|
||||
"lucide".to_owned(),
|
||||
Arc::new(egui::FontData::from_static(include_bytes!(
|
||||
"../../assets/fonts/lucide.ttf"
|
||||
))),
|
||||
);
|
||||
fonts
|
||||
.families
|
||||
.insert(egui::FontFamily::Name(FAMILY.into()), vec!["lucide".to_owned()]);
|
||||
ctx.set_fonts(fonts);
|
||||
}
|
||||
|
||||
/// A `FontId` in the Lucide family at `size`.
|
||||
pub fn font(size: f32) -> egui::FontId {
|
||||
egui::FontId::new(size, egui::FontFamily::Name(FAMILY.into()))
|
||||
}
|
||||
|
||||
// --- Codepoints (Lucide PUA). Add more as the mobile UI needs them. ---
|
||||
pub const MAXIMIZE: &str = "\u{e112}";
|
||||
pub const MINIMIZE: &str = "\u{e11a}";
|
||||
pub const ARROW_LEFT_RIGHT: &str = "\u{e24a}";
|
||||
pub const ARROW_RIGHT_FROM_LINE: &str = "\u{e458}"; // output port
|
||||
pub const ARROW_RIGHT_TO_LINE: &str = "\u{e459}"; // input port
|
||||
pub const GRIP_HORIZONTAL: &str = "\u{e0ea}";
|
||||
pub const CHEVRONS_UP: &str = "\u{e074}";
|
||||
pub const PLAY: &str = "\u{e13c}";
|
||||
pub const PAUSE: &str = "\u{e12e}";
|
||||
pub const REPEAT: &str = "\u{e146}"; // cycle / loop region toggle
|
||||
pub const TRASH: &str = "\u{e18d}"; // delete a take
|
||||
pub const SETTINGS: &str = "\u{e154}";
|
||||
pub const SEARCH: &str = "\u{e151}";
|
||||
pub const PLUS: &str = "\u{e13d}";
|
||||
pub const X: &str = "\u{e1b2}";
|
||||
pub const MENU: &str = "\u{e115}";
|
||||
// Intent-picker icons.
|
||||
pub const BRUSH: &str = "\u{e1d3}";
|
||||
pub const FILM: &str = "\u{e0d0}";
|
||||
pub const MUSIC: &str = "\u{e122}";
|
||||
pub const MIC: &str = "\u{e118}";
|
||||
pub const CLAPPERBOARD: &str = "\u{e29b}";
|
||||
pub const SQUARE_DASHED: &str = "\u{e1cb}";
|
||||
pub const FOLDER_OPEN: &str = "\u{e247}";
|
||||
// Tool icons (omnibutton).
|
||||
pub const MOUSE_POINTER_2: &str = "\u{e1c3}";
|
||||
pub const PENCIL: &str = "\u{e1f9}";
|
||||
pub const MOVE: &str = "\u{e121}";
|
||||
pub const VECTOR_SQUARE: &str = "\u{e67c}";
|
||||
pub const SQUARE: &str = "\u{e167}";
|
||||
pub const CIRCLE: &str = "\u{e076}";
|
||||
pub const MINUS: &str = "\u{e11c}";
|
||||
pub const HEXAGON: &str = "\u{e0f3}";
|
||||
pub const PAINT_BUCKET: &str = "\u{e2e6}";
|
||||
pub const PIPETTE: &str = "\u{e13b}";
|
||||
pub const TYPE: &str = "\u{e198}";
|
||||
pub const ERASER: &str = "\u{e28f}";
|
||||
pub const PEN_TOOL: &str = "\u{e131}";
|
||||
pub const BLEND: &str = "\u{e59c}";
|
||||
pub const WAND_SPARKLES: &str = "\u{e357}";
|
||||
pub const LASSO_SELECT: &str = "\u{e1cf}";
|
||||
pub const SCISSORS: &str = "\u{e14e}";
|
||||
// Create-menu icons.
|
||||
pub const AUDIO_WAVEFORM: &str = "\u{e55b}";
|
||||
pub const PIANO: &str = "\u{e561}";
|
||||
pub const FOLDER: &str = "\u{e0d7}";
|
||||
pub const FILE_PLUS: &str = "\u{e0c9}";
|
||||
pub const COPY: &str = "\u{e09e}";
|
||||
pub const LAYERS: &str = "\u{e529}";
|
||||
pub const ELLIPSIS: &str = "\u{e0b6}";
|
||||
// Undo / redo (stage header).
|
||||
pub const UNDO_2: &str = "\u{e2a1}";
|
||||
pub const REDO_2: &str = "\u{e2a0}";
|
||||
// Tool cursors (see custom_cursor.rs).
|
||||
pub const STAMP: &str = "\u{e3bb}";
|
||||
pub const SPRAY_CAN: &str = "\u{e495}";
|
||||
pub const BANDAGE: &str = "\u{e61d}";
|
||||
pub const DROPLET: &str = "\u{e0b4}";
|
||||
pub const TEXT_CURSOR: &str = "\u{e264}";
|
||||
pub const CROSSHAIR: &str = "\u{e0ac}";
|
||||
pub const POINTER: &str = "\u{e1e8}";
|
||||
pub const CONTRAST: &str = "\u{e09d}";
|
||||
pub const WIND: &str = "\u{e1b0}";
|
||||
pub const SUN_MOON: &str = "\u{e2b2}";
|
||||
pub const SPLINE: &str = "\u{e38b}";
|
||||
pub const DROPLETS: &str = "\u{e0b5}";
|
||||
pub const CIRCLE_DASHED: &str = "\u{e4b0}";
|
||||
pub const PALETTE: &str = "\u{e1dd}";
|
||||
pub const SHAPES: &str = "\u{e4b3}";
|
||||
pub const SCALING: &str = "\u{e2ec}";
|
||||
pub const FRAME: &str = "\u{e291}";
|
||||
// Timeline layer-row toggles.
|
||||
pub const VOLUME_2: &str = "\u{e1ab}"; // unmuted
|
||||
pub const VOLUME_X: &str = "\u{e1ac}"; // muted
|
||||
pub const HEADPHONES: &str = "\u{e0f1}"; // solo
|
||||
pub const LOCK: &str = "\u{e10b}";
|
||||
pub const LOCK_OPEN: &str = "\u{e10c}";
|
||||
pub const EYE: &str = "\u{e0ba}";
|
||||
pub const EYE_OFF: &str = "\u{e0bb}";
|
||||
pub const VIDEO: &str = "\u{e1a5}"; // camera enabled
|
||||
pub const VIDEO_OFF: &str = "\u{e1a6}"; // camera disabled
|
||||
|
||||
/// Lucide glyph for tools that have no bundled SVG icon. `None` means the tool has a real SVG
|
||||
/// icon in `src/assets/` and the caller should use that instead.
|
||||
pub fn tool_glyph(tool: lightningbeam_core::tool::Tool) -> Option<&'static str> {
|
||||
use lightningbeam_core::tool::Tool;
|
||||
Some(match tool {
|
||||
Tool::Pencil => PENCIL,
|
||||
Tool::Pen => PEN_TOOL,
|
||||
Tool::Airbrush => SPRAY_CAN,
|
||||
Tool::CloneStamp => STAMP,
|
||||
Tool::HealingBrush => BANDAGE,
|
||||
Tool::PatternStamp => SHAPES,
|
||||
Tool::DodgeBurn => SUN_MOON,
|
||||
Tool::Sponge => DROPLETS,
|
||||
Tool::BlurSharpen => CONTRAST,
|
||||
Tool::Gradient => BLEND,
|
||||
Tool::CustomShape => HEXAGON,
|
||||
Tool::SelectEllipse => CIRCLE_DASHED,
|
||||
Tool::MagicWand => WAND_SPARKLES,
|
||||
Tool::QuickSelect => BRUSH,
|
||||
Tool::Warp => SCALING,
|
||||
Tool::Liquify => DROPLET,
|
||||
Tool::SelectLasso => LASSO_SELECT,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -1,210 +0,0 @@
|
||||
//! Selection inspector bottom sheet. When something is selected/focused it rises above the
|
||||
//! transport, showing the focused object's properties by reusing `InfopanelPane` full-bleed.
|
||||
//! Jump-to chips slide the stack window to the related surface; the ✕ deselects.
|
||||
|
||||
use eframe::egui;
|
||||
use lightningbeam_core::pane::PaneType;
|
||||
use lightningbeam_core::selection::FocusSelection;
|
||||
|
||||
use super::{surface, MobileState, Palette, MOBILE_NS};
|
||||
use crate::panes::{NodePath, SharedPaneState};
|
||||
use crate::RenderContext;
|
||||
|
||||
const GRAB_H: f32 = 16.0;
|
||||
const HEAD_H: f32 = 44.0;
|
||||
|
||||
fn inspector_path() -> NodePath {
|
||||
vec![MOBILE_NS, 200]
|
||||
}
|
||||
|
||||
/// Whether anything is selected/focused (i.e. the inspector should be shown).
|
||||
pub fn is_active(shared: &SharedPaneState) -> bool {
|
||||
!shared.focus.is_none() || !shared.selection.is_empty()
|
||||
}
|
||||
|
||||
/// A cheap content-sensitive signature of the current selection, so the shell can detect when the
|
||||
/// selection *changes* (to re-show a manually-dismissed inspector for the new thing).
|
||||
pub fn selection_sig(shared: &SharedPaneState) -> u64 {
|
||||
let mut h: u64 = 0;
|
||||
for id in shared.selection.clip_instances() {
|
||||
h ^= (id.as_u128() as u64).wrapping_mul(0x9E3779B97F4A7C15);
|
||||
}
|
||||
for f in shared.selection.selected_fills() {
|
||||
h ^= (f.idx() as u64).wrapping_mul(0xD1B54A32D192ED03);
|
||||
}
|
||||
for e in shared.selection.selected_edges() {
|
||||
h ^= (e.idx() as u64).wrapping_mul(0xA24BAED4963EE407);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
/// The stack slot (see `super::STACK`) where the current selection lives, so we can tell if the
|
||||
/// sheet would cover it. Geometry/selection lives on the Stage; clips/layers on the Timeline; etc.
|
||||
#[allow(dead_code)] // selection→pane mapping; kept for reflow/jump heuristics
|
||||
pub fn target_slot(shared: &SharedPaneState) -> usize {
|
||||
match &*shared.focus {
|
||||
FocusSelection::Notes { .. } => 4, // PianoRoll
|
||||
FocusSelection::Nodes(_) => 5, // Node/Instrument
|
||||
FocusSelection::Assets(_) => 1, // Asset Library
|
||||
FocusSelection::ClipInstances(_) | FocusSelection::Layers(_) => 3, // Timeline
|
||||
FocusSelection::Geometry { .. } | FocusSelection::None => 2, // Stage
|
||||
}
|
||||
}
|
||||
|
||||
/// A short title describing what's selected.
|
||||
fn title(shared: &SharedPaneState) -> String {
|
||||
use lightningbeam_core::layer::{AnyLayer, AudioLayerType};
|
||||
let plural = |n: usize, s: &str| {
|
||||
if n == 1 {
|
||||
format!("1 {s}")
|
||||
} else {
|
||||
format!("{n} {s}s")
|
||||
}
|
||||
};
|
||||
match &*shared.focus {
|
||||
FocusSelection::Layers(ids) => {
|
||||
let doc = shared.action_executor.document();
|
||||
match ids.len() {
|
||||
0 => "Layer".to_string(),
|
||||
1 => {
|
||||
if let Some(l) = doc.get_layer(&ids[0]) {
|
||||
let ty = match l {
|
||||
AnyLayer::Vector(_) => "Vector",
|
||||
AnyLayer::Audio(a) => match a.audio_layer_type {
|
||||
AudioLayerType::Midi => "MIDI",
|
||||
AudioLayerType::Sampled => "Audio",
|
||||
},
|
||||
AnyLayer::Video(_) => "Video",
|
||||
AnyLayer::Effect(_) => "Effect",
|
||||
AnyLayer::Group(_) => "Group",
|
||||
AnyLayer::Raster(_) => "Raster",
|
||||
AnyLayer::Text(_) => "Text",
|
||||
};
|
||||
format!("{} · {} layer", l.name(), ty)
|
||||
} else {
|
||||
"Layer".to_string()
|
||||
}
|
||||
}
|
||||
n => plural(n, "layer"),
|
||||
}
|
||||
}
|
||||
FocusSelection::ClipInstances(ids) => plural(ids.len(), "clip"),
|
||||
FocusSelection::Notes { indices, .. } => plural(indices.len().max(1), "note"),
|
||||
FocusSelection::Nodes(ids) => plural(ids.len(), "node"),
|
||||
FocusSelection::Assets(ids) => plural(ids.len(), "asset"),
|
||||
FocusSelection::Geometry { .. } | FocusSelection::None => "Selection".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A jump-to chip: its label and the stack window it brings into view (top, count). These reframe
|
||||
/// to a related surface with the object still selected.
|
||||
struct Chip {
|
||||
label: &'static str,
|
||||
window: (usize, usize),
|
||||
}
|
||||
|
||||
const CHIPS: [Chip; 2] = [
|
||||
Chip { label: "Timeline", window: (3, 1) }, // Timeline = STACK index 3
|
||||
Chip { label: "Nodes", window: (5, 1) }, // Node/Instrument = STACK index 5
|
||||
];
|
||||
|
||||
pub fn render(
|
||||
ui: &mut egui::Ui,
|
||||
rect: egui::Rect,
|
||||
region: egui::Rect,
|
||||
rc: &mut RenderContext,
|
||||
state: &mut MobileState,
|
||||
pal: &Palette,
|
||||
landscape: bool,
|
||||
) {
|
||||
// Panel background + border, with rounded corners on the edge that faces the content (top in
|
||||
// portrait, left in landscape).
|
||||
let radius = if landscape {
|
||||
egui::CornerRadius { nw: 14, ne: 0, sw: 14, se: 0 }
|
||||
} else {
|
||||
egui::CornerRadius { nw: 14, ne: 14, sw: 0, se: 0 }
|
||||
};
|
||||
ui.painter().rect_filled(rect, radius, pal.surface_alt);
|
||||
ui.painter().rect_stroke(rect, radius, egui::Stroke::new(1.0, pal.line), egui::StrokeKind::Inside);
|
||||
|
||||
// Grab handle — top strip (portrait, drags height) or left strip (landscape, drags width).
|
||||
// `content_area` is the panel minus the grab strip.
|
||||
let content_area = if landscape {
|
||||
let grab = egui::Rect::from_min_max(rect.left_top(), egui::pos2(rect.left() + GRAB_H, rect.bottom()));
|
||||
let gresp = ui.interact(grab, ui.id().with("mobile_inspector_grab"), egui::Sense::drag());
|
||||
let pill = egui::Rect::from_center_size(grab.center(), egui::vec2(4.0, 34.0));
|
||||
ui.painter().rect_filled(pill, 2.0, if gresp.hovered() || gresp.dragged() { pal.accent } else { pal.line });
|
||||
if gresp.dragged() && region.width() > 1.0 {
|
||||
state.inspector_width_frac = (state.inspector_width_frac - gresp.drag_delta().x / region.width()).clamp(0.2, 0.7);
|
||||
}
|
||||
if gresp.hovered() || gresp.dragged() {
|
||||
ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeHorizontal);
|
||||
}
|
||||
egui::Rect::from_min_max(egui::pos2(grab.right(), rect.top()), rect.max)
|
||||
} else {
|
||||
let grab = egui::Rect::from_min_max(rect.left_top(), egui::pos2(rect.right(), rect.top() + GRAB_H));
|
||||
let gresp = ui.interact(grab, ui.id().with("mobile_inspector_grab"), egui::Sense::drag());
|
||||
let pill = egui::Rect::from_center_size(grab.center(), egui::vec2(34.0, 4.0));
|
||||
ui.painter().rect_filled(pill, 2.0, if gresp.hovered() || gresp.dragged() { pal.accent } else { pal.line });
|
||||
if gresp.dragged() && region.height() > 1.0 {
|
||||
state.inspector_frac = (state.inspector_frac - gresp.drag_delta().y / region.height()).clamp(0.2, 0.85);
|
||||
}
|
||||
if gresp.hovered() || gresp.dragged() {
|
||||
ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeVertical);
|
||||
}
|
||||
egui::Rect::from_min_max(egui::pos2(rect.left(), grab.bottom()), rect.max)
|
||||
};
|
||||
|
||||
// Single header row (egui widgets — they inherit the mobile touch sizing + theme visuals):
|
||||
// title on the left, [Timeline] [Nodes] jump buttons + ✕ close on the right.
|
||||
let head = egui::Rect::from_min_max(
|
||||
content_area.min,
|
||||
egui::pos2(content_area.right(), content_area.top() + HEAD_H),
|
||||
);
|
||||
let title_text = title(&rc.shared);
|
||||
let mut jump: Option<(usize, usize)> = None;
|
||||
let mut do_close = false;
|
||||
let mut hui = ui.new_child(
|
||||
egui::UiBuilder::new()
|
||||
.max_rect(head.shrink2(egui::vec2(12.0, 5.0)))
|
||||
.layout(egui::Layout::left_to_right(egui::Align::Center)),
|
||||
);
|
||||
hui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui
|
||||
.add(egui::Button::new(egui::RichText::new(super::icons::X).font(super::icons::font(18.0))).frame(false))
|
||||
.clicked()
|
||||
{
|
||||
do_close = true;
|
||||
}
|
||||
ui.add_space(4.0);
|
||||
// Reversed so the visual order stays Timeline, then Nodes.
|
||||
for chip in CHIPS.iter().rev() {
|
||||
if ui.button(chip.label).clicked() {
|
||||
jump = Some(chip.window);
|
||||
}
|
||||
}
|
||||
// Title fills the remaining space on the left.
|
||||
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
|
||||
ui.add(egui::Label::new(egui::RichText::new(title_text).color(pal.text)).truncate());
|
||||
});
|
||||
});
|
||||
if do_close {
|
||||
rc.shared.selection.clear();
|
||||
*rc.shared.focus = FocusSelection::None;
|
||||
}
|
||||
if let Some((top, count)) = jump {
|
||||
state.window_top = top;
|
||||
state.window_count = count;
|
||||
state.weights = [1.0, 1.0, 1.0];
|
||||
state.anim = None;
|
||||
}
|
||||
|
||||
// Properties content — reuse the Infopanel full-bleed.
|
||||
let content = egui::Rect::from_min_max(
|
||||
egui::pos2(content_area.left(), head.bottom()),
|
||||
content_area.max,
|
||||
);
|
||||
if content.height() > 1.0 {
|
||||
surface::render_surface_fullbleed(ui, content, &inspector_path(), PaneType::Infopanel, rc);
|
||||
}
|
||||
}
|
||||
@@ -1,184 +0,0 @@
|
||||
//! Mobile new-file intent picker — the phone equivalent of the desktop start screen.
|
||||
//!
|
||||
//! Mirrors the desktop "new project" flow: each intent calls
|
||||
//! [`EditorApp::create_new_project_with_focus`] (which builds the document, picks a default layer,
|
||||
//! and switches to editor mode) and then sets the initial mobile stack window so the relevant panes
|
||||
//! are in view.
|
||||
|
||||
use eframe::egui;
|
||||
|
||||
use super::{icons, Palette};
|
||||
use crate::EditorApp;
|
||||
|
||||
/// One intent card.
|
||||
struct Intent {
|
||||
label: &'static str,
|
||||
icon: &'static str,
|
||||
accent: egui::Color32,
|
||||
/// Argument to `create_new_project_with_focus` (0=Animation, 1=Video, 2=Music, 5=Painting).
|
||||
focus: usize,
|
||||
/// Initial mobile stack window: (window_top, window_count).
|
||||
window: (usize, usize),
|
||||
/// Initial pane weights (relative heights) for the window.
|
||||
weights: [f32; 3],
|
||||
}
|
||||
|
||||
fn intents(pal: &Palette) -> [Intent; 6] {
|
||||
let [coral, cyan, amber, pink, violet] = pal.accents;
|
||||
let even = [1.0, 1.0, 1.0];
|
||||
// Compose/Record: compressed Timeline ribbon on top; the tall Piano Roll (which now hosts the
|
||||
// instrument header + keyboard as one surface) fills the rest.
|
||||
let music = [0.4, 1.6, 1.0];
|
||||
[
|
||||
// Stage indices (see super::STACK): Stage=2, Timeline=3, PianoRoll=4, VirtualPiano=5.
|
||||
Intent { label: "Draw", icon: icons::BRUSH, accent: coral, focus: 5, window: (2, 1), weights: even },
|
||||
Intent { label: "Animate", icon: icons::FILM, accent: cyan, focus: 0, window: (2, 2), weights: even },
|
||||
Intent { label: "Compose", icon: icons::MUSIC, accent: amber, focus: 2, window: (3, 2), weights: music },
|
||||
Intent { label: "Record", icon: icons::MIC, accent: pink, focus: 2, window: (3, 2), weights: music },
|
||||
Intent { label: "Edit video", icon: icons::CLAPPERBOARD, accent: violet, focus: 1, window: (2, 2), weights: even },
|
||||
Intent { label: "Blank", icon: icons::SQUARE_DASHED, accent: pal.text_dim, focus: 0, window: (2, 2), weights: even },
|
||||
]
|
||||
}
|
||||
|
||||
pub fn render(app: &mut EditorApp, ctx: &egui::Context) {
|
||||
let pal = Palette::from_theme(&app.theme, ctx);
|
||||
egui::CentralPanel::default()
|
||||
.frame(egui::Frame::NONE.fill(pal.bg))
|
||||
.show(ctx, |ui| {
|
||||
let rect = ui.available_rect_before_wrap();
|
||||
let margin = 16.0;
|
||||
let left = rect.left() + margin;
|
||||
let right = rect.right() - margin;
|
||||
|
||||
// Header.
|
||||
ui.painter().text(
|
||||
egui::pos2(left, rect.top() + 22.0),
|
||||
egui::Align2::LEFT_TOP,
|
||||
"Start something",
|
||||
egui::FontId::proportional(22.0),
|
||||
pal.text,
|
||||
);
|
||||
|
||||
let landscape = rect.width() > rect.height();
|
||||
let content_top = rect.top() + 62.0;
|
||||
let content_bottom = rect.bottom() - margin;
|
||||
let gap = 10.0;
|
||||
|
||||
// Portrait: 3 rows × 2 cols of intent cards up top, recent list below.
|
||||
// Landscape: 2 rows × 3 cols on the left, recent list to the right.
|
||||
let (cols, rows) = if landscape { (3usize, 2usize) } else { (2usize, 3usize) };
|
||||
let (grid_rect, recent_rect) = if landscape {
|
||||
let split = left + (right - left) * 0.62;
|
||||
(
|
||||
egui::Rect::from_min_max(egui::pos2(left, content_top), egui::pos2(split - gap, content_bottom)),
|
||||
egui::Rect::from_min_max(egui::pos2(split + gap, content_top), egui::pos2(right, content_bottom)),
|
||||
)
|
||||
} else {
|
||||
let grid_h = (content_bottom - content_top) * 0.66;
|
||||
(
|
||||
egui::Rect::from_min_max(egui::pos2(left, content_top), egui::pos2(right, content_top + grid_h)),
|
||||
egui::Rect::from_min_max(egui::pos2(left, content_top + grid_h + 14.0), egui::pos2(right, content_bottom)),
|
||||
)
|
||||
};
|
||||
|
||||
// Floor at 0 so a very small window can't produce negative/inverted card rects.
|
||||
let col_w = ((grid_rect.width() - (cols as f32 - 1.0) * gap) / cols as f32).max(0.0);
|
||||
let card_h = ((grid_rect.height() - (rows as f32 - 1.0) * gap) / rows as f32).max(0.0);
|
||||
for (i, intent) in intents(&pal).iter().enumerate() {
|
||||
let col = (i % cols) as f32;
|
||||
let row = (i / cols) as f32;
|
||||
let cx = grid_rect.left() + col * (col_w + gap);
|
||||
let cy = grid_rect.top() + row * (card_h + gap);
|
||||
let card = egui::Rect::from_min_size(egui::pos2(cx, cy), egui::vec2(col_w, card_h));
|
||||
|
||||
let resp = ui.interact(card, ui.id().with(("mobile_intent", i)), egui::Sense::click());
|
||||
let p = ui.painter();
|
||||
p.rect_filled(card, 12.0, if resp.hovered() { pal.surface_alt } else { pal.surface });
|
||||
p.rect_stroke(card, 12.0, egui::Stroke::new(1.0, pal.line), egui::StrokeKind::Inside);
|
||||
p.text(
|
||||
egui::pos2(card.center().x, card.top() + card.height() * 0.40),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
intent.icon,
|
||||
icons::font(30.0),
|
||||
intent.accent,
|
||||
);
|
||||
p.text(
|
||||
egui::pos2(card.center().x, card.bottom() - 18.0),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
intent.label,
|
||||
egui::FontId::proportional(15.0),
|
||||
pal.text,
|
||||
);
|
||||
|
||||
if resp.clicked() {
|
||||
app.create_new_project_with_focus(intent.focus);
|
||||
app.mobile_state.window_top = intent.window.0;
|
||||
app.mobile_state.window_count = intent.window.1;
|
||||
app.mobile_state.weights = intent.weights;
|
||||
}
|
||||
}
|
||||
|
||||
// Recent projects list (below the grid in portrait, beside it in landscape).
|
||||
let rleft = recent_rect.left();
|
||||
let rright = recent_rect.right();
|
||||
ui.painter().text(
|
||||
egui::pos2(rleft, recent_rect.top()),
|
||||
egui::Align2::LEFT_TOP,
|
||||
"Recent",
|
||||
egui::FontId::proportional(13.0),
|
||||
pal.text_dim,
|
||||
);
|
||||
let list_top = recent_rect.top() + 22.0;
|
||||
let recents = app.config.get_recent_files();
|
||||
if recents.is_empty() {
|
||||
ui.painter().text(
|
||||
egui::pos2(rleft, list_top + 8.0),
|
||||
egui::Align2::LEFT_TOP,
|
||||
"No recent projects",
|
||||
egui::FontId::proportional(12.0),
|
||||
pal.text_dim,
|
||||
);
|
||||
} else {
|
||||
let row_h = 38.0;
|
||||
let row_gap = 6.0;
|
||||
let avail = recent_rect.bottom() - list_top;
|
||||
let max_rows = ((avail + row_gap) / (row_h + row_gap)).floor().max(0.0) as usize;
|
||||
let mut chosen: Option<std::path::PathBuf> = None;
|
||||
for (j, path) in recents.iter().take(max_rows).enumerate() {
|
||||
let ry = list_top + j as f32 * (row_h + row_gap);
|
||||
let row_rect =
|
||||
egui::Rect::from_min_max(egui::pos2(rleft, ry), egui::pos2(rright, ry + row_h));
|
||||
let resp =
|
||||
ui.interact(row_rect, ui.id().with(("mobile_recent", j)), egui::Sense::click());
|
||||
let p = ui.painter();
|
||||
p.rect_filled(row_rect, 8.0, if resp.hovered() { pal.surface_alt } else { pal.surface });
|
||||
p.rect_stroke(row_rect, 8.0, egui::Stroke::new(1.0, pal.line), egui::StrokeKind::Inside);
|
||||
p.text(
|
||||
egui::pos2(row_rect.left() + 12.0, row_rect.center().y),
|
||||
egui::Align2::LEFT_CENTER,
|
||||
icons::FOLDER_OPEN,
|
||||
icons::font(15.0),
|
||||
pal.text_dim,
|
||||
);
|
||||
let name = path
|
||||
.file_name()
|
||||
.map(|s| s.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| "Untitled".to_string());
|
||||
p.text(
|
||||
egui::pos2(row_rect.left() + 36.0, row_rect.center().y),
|
||||
egui::Align2::LEFT_CENTER,
|
||||
&name,
|
||||
egui::FontId::proportional(13.0),
|
||||
pal.text,
|
||||
);
|
||||
if resp.clicked() {
|
||||
chosen = Some(path.clone());
|
||||
}
|
||||
}
|
||||
if let Some(path) = chosen {
|
||||
app.load_from_file(path);
|
||||
app.app_mode = crate::AppMode::Editor;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,488 +0,0 @@
|
||||
//! Mobile / phone UI shell.
|
||||
//!
|
||||
//! Developed on desktop behind the `LB_MOBILE_UI` env var (see [`is_mobile_env`]).
|
||||
//!
|
||||
//! The shell is a single **vertical sliding-window stack** of the existing panes: a fixed
|
||||
//! top→bottom order, with a window of 2 or 3 consecutive panes visible at once. Dragging the
|
||||
//! dividers between panes and the top/bottom screen edges grows, shrinks, or slides the window
|
||||
//! (the R1–R6 state machine in `stack.rs`). A transport bar is pinned at the bottom as the floor.
|
||||
//!
|
||||
//! Each visible pane reuses the existing `PaneInstance::render_content` full-bleed (see
|
||||
//! `surface.rs`). Per the wireframe, the Toolbar becomes the omnibutton and the Infopanel becomes
|
||||
//! the selection inspector (later phases), so neither is in the stack.
|
||||
|
||||
use eframe::egui;
|
||||
use lightningbeam_core::pane::PaneType;
|
||||
|
||||
use crate::panes::NodePath;
|
||||
use crate::RenderContext;
|
||||
|
||||
pub mod icons;
|
||||
mod inspector;
|
||||
pub mod intent;
|
||||
mod omni;
|
||||
mod palette;
|
||||
mod stack;
|
||||
mod surface;
|
||||
mod topbar;
|
||||
mod transport;
|
||||
|
||||
use palette::Palette;
|
||||
|
||||
/// Reserved sentinel namespace for mobile pane-instance paths. Desktop layout paths are built
|
||||
/// from small child indices, so prefixing with `usize::MAX` guarantees mobile slots never alias a
|
||||
/// real layout path in the shared `pane_instances` map.
|
||||
pub const MOBILE_NS: usize = usize::MAX;
|
||||
|
||||
const TRANSPORT_H: f32 = 60.0;
|
||||
const TOPBAR_H: f32 = 40.0;
|
||||
|
||||
/// Clamp a desktop dialog width to fit the current screen (with side margins). A no-op on wide
|
||||
/// desktop screens (`min` keeps the desired width); on a phone-aspect window it shrinks to fit.
|
||||
pub fn dialog_width(ctx: &egui::Context, desired: f32) -> f32 {
|
||||
let avail = ctx.content_rect().width() - 24.0;
|
||||
desired.min(avail.max(200.0))
|
||||
}
|
||||
|
||||
/// Enlarge egui's spacing/sizing so the standard widgets (buttons, dropdowns, sliders, text fields)
|
||||
/// in panes and dialogs are touch-friendly. Applied every frame after the theme visuals when the
|
||||
/// mobile shell is active. The mobile chrome (transport, omnibutton, headers) is hand-sized already;
|
||||
/// this targets the egui-widget content inside panes.
|
||||
pub fn apply_touch_style(ctx: &egui::Context) {
|
||||
use egui::{FontId, TextStyle};
|
||||
ctx.style_mut(|s| {
|
||||
let sp = &mut s.spacing;
|
||||
// Minimum touch target height for buttons/sliders/checkboxes.
|
||||
sp.interact_size = egui::vec2(sp.interact_size.x.max(44.0), 38.0);
|
||||
sp.button_padding = egui::vec2(12.0, 9.0);
|
||||
sp.item_spacing = egui::vec2(10.0, 10.0);
|
||||
sp.slider_width = 200.0;
|
||||
sp.slider_rail_height = 10.0;
|
||||
sp.combo_width = 200.0;
|
||||
sp.combo_height = 360.0;
|
||||
sp.text_edit_width = 220.0;
|
||||
sp.icon_width = 24.0;
|
||||
sp.icon_width_inner = 14.0;
|
||||
sp.icon_spacing = 8.0;
|
||||
sp.scroll.bar_width = 16.0;
|
||||
sp.menu_margin = egui::Margin::same(8);
|
||||
// Larger text for touch legibility.
|
||||
s.text_styles.insert(TextStyle::Body, FontId::proportional(15.0));
|
||||
s.text_styles.insert(TextStyle::Button, FontId::proportional(15.0));
|
||||
s.text_styles.insert(TextStyle::Monospace, FontId::monospace(13.0));
|
||||
s.text_styles.insert(TextStyle::Small, FontId::proportional(12.0));
|
||||
});
|
||||
}
|
||||
|
||||
/// Returns true if the mobile UI is requested via the `LB_MOBILE_UI` env var.
|
||||
/// Any non-empty value other than "0" enables it.
|
||||
pub fn is_mobile_env() -> bool {
|
||||
std::env::var("LB_MOBILE_UI")
|
||||
.map(|v| !v.is_empty() && v != "0")
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// `LB_MOBILE_UI=2` develops the mobile shell in landscape (opens a landscape phone window).
|
||||
/// Orientation itself is aspect-based at render time; this only picks the initial window aspect.
|
||||
pub fn is_mobile_landscape_env() -> bool {
|
||||
std::env::var("LB_MOBILE_UI").map(|v| v == "2").unwrap_or(false)
|
||||
}
|
||||
|
||||
/// The panes that make up the vertical stack, in fixed top→bottom order.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum StackPane {
|
||||
Outliner,
|
||||
AssetLibrary,
|
||||
Stage,
|
||||
Timeline,
|
||||
/// The instrument surface: keyboard-primary on mobile, hosting the falling-notes roll above the
|
||||
/// keys (the standalone Virtual Piano is embedded here, so it isn't a separate stack slot).
|
||||
PianoRoll,
|
||||
/// Node editor, or (toggled) the instrument/preset browser.
|
||||
NodeInstrument,
|
||||
ScriptEditor,
|
||||
}
|
||||
|
||||
/// The stack order. Index into this is a pane's stable slot id.
|
||||
pub const STACK: [StackPane; 7] = [
|
||||
StackPane::Outliner,
|
||||
StackPane::AssetLibrary,
|
||||
StackPane::Stage,
|
||||
StackPane::Timeline,
|
||||
StackPane::PianoRoll,
|
||||
StackPane::NodeInstrument,
|
||||
StackPane::ScriptEditor,
|
||||
];
|
||||
|
||||
impl StackPane {
|
||||
/// The backing pane type. The Node/Instrument slot picks NodeEditor or PresetBrowser based on
|
||||
/// the toggle.
|
||||
pub fn pane_type(self, show_instruments: bool) -> PaneType {
|
||||
match self {
|
||||
StackPane::Outliner => PaneType::Outliner,
|
||||
StackPane::AssetLibrary => PaneType::AssetLibrary,
|
||||
StackPane::Stage => PaneType::Stage,
|
||||
StackPane::Timeline => PaneType::Timeline,
|
||||
StackPane::PianoRoll => PaneType::PianoRoll,
|
||||
StackPane::NodeInstrument => {
|
||||
if show_instruments {
|
||||
PaneType::PresetBrowser
|
||||
} else {
|
||||
PaneType::NodeEditor
|
||||
}
|
||||
}
|
||||
StackPane::ScriptEditor => PaneType::ScriptEditor,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn label(self, show_instruments: bool) -> &'static str {
|
||||
match self {
|
||||
StackPane::Outliner => "Outliner",
|
||||
StackPane::AssetLibrary => "Assets",
|
||||
StackPane::Stage => "Stage",
|
||||
StackPane::Timeline => "Timeline",
|
||||
StackPane::PianoRoll => "Instrument",
|
||||
StackPane::NodeInstrument => {
|
||||
if show_instruments {
|
||||
"Instruments"
|
||||
} else {
|
||||
"Nodes"
|
||||
}
|
||||
}
|
||||
StackPane::ScriptEditor => "Script",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `pane_instances` key for a stack slot.
|
||||
fn slot_path(slot: usize) -> NodePath {
|
||||
vec![MOBILE_NS, slot]
|
||||
}
|
||||
|
||||
/// An in-progress drag of a stack handle.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct StackDrag {
|
||||
pub handle: stack::Handle,
|
||||
/// Accumulated pointer offset (px) since the drag began (downward positive).
|
||||
pub offset: f32,
|
||||
}
|
||||
|
||||
/// A short ease between two stack layouts (resize snap, membership change, or fullscreen toggle).
|
||||
/// Both endpoints are full configs so the same animation covers panes resizing AND panes
|
||||
/// entering/leaving the window.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct LayoutAnim {
|
||||
pub from_top: usize,
|
||||
pub from_count: usize,
|
||||
pub from_w: [f32; 3],
|
||||
pub to_top: usize,
|
||||
pub to_count: usize,
|
||||
pub to_w: [f32; 3],
|
||||
/// egui time (seconds) when the animation started (may be back-dated to continue a drag).
|
||||
pub start: f64,
|
||||
}
|
||||
|
||||
/// Persistent mobile-shell state, cached on `EditorApp`.
|
||||
pub struct MobileState {
|
||||
/// Index into `STACK` of the topmost visible pane.
|
||||
pub window_top: usize,
|
||||
/// Number of visible panes (1, 2, or 3; 1 = a single pane filling the stack).
|
||||
pub window_count: usize,
|
||||
/// Relative heights of the visible panes (first `window_count` entries are used; normalized on
|
||||
/// use). Reset to even on any membership change; adjusted by intermediate divider snapping.
|
||||
pub weights: [f32; 3],
|
||||
/// Node/Instrument band: false = node editor, true = instrument/preset browser.
|
||||
pub show_instruments: bool,
|
||||
/// Inspector sheet height as a fraction of the region above the transport (portrait).
|
||||
pub inspector_frac: f32,
|
||||
/// Inspector side-panel width as a fraction of the region (landscape).
|
||||
pub inspector_width_frac: f32,
|
||||
/// Last frame's orientation (landscape?), to reset orientation-specific cached state on rotation.
|
||||
pub was_landscape: bool,
|
||||
/// Whether the inspector sheet is currently shown. Gated to appear on pointer *release* (a tap),
|
||||
/// not on press, so press+drag interactions aren't interrupted by the sheet popping up.
|
||||
pub inspector_visible: bool,
|
||||
/// Screen-y of the tap that opened the inspector — used to decide whether the tapped thing would
|
||||
/// be hidden by the sheet (only then do we reflow the stack).
|
||||
pub inspector_anchor_y: f32,
|
||||
/// Set when the user taps outside the sheet to dismiss it; suppresses re-showing until the
|
||||
/// selection changes. Reset when the selection signature below changes.
|
||||
pub inspector_dismissed: bool,
|
||||
/// Cheap signature of the current selection, to detect when it changes (re-show the inspector).
|
||||
pub inspector_sel_sig: u64,
|
||||
/// Whether the omnibutton radial tool menu is open.
|
||||
pub omni_open: bool,
|
||||
/// Whether the omnibutton "more" grid (all tools) is open.
|
||||
pub omni_grid_open: bool,
|
||||
/// Whether the omnibutton "+New" create grid is open.
|
||||
pub omni_create_open: bool,
|
||||
/// Top-bar overflow (⋯ commands) sheet open.
|
||||
pub overflow_open: bool,
|
||||
/// Top-bar command palette (⌕) open, and its search query.
|
||||
pub palette_open: bool,
|
||||
pub palette_query: String,
|
||||
/// Active handle drag (transient).
|
||||
pub drag: Option<StackDrag>,
|
||||
/// In-flight layout ease (transient).
|
||||
pub anim: Option<LayoutAnim>,
|
||||
}
|
||||
|
||||
impl Default for MobileState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
// Launch on {Stage, Timeline}.
|
||||
window_top: 2,
|
||||
window_count: 2,
|
||||
weights: [1.0, 1.0, 1.0],
|
||||
show_instruments: false,
|
||||
inspector_frac: 0.45,
|
||||
inspector_width_frac: 0.4,
|
||||
was_landscape: false,
|
||||
inspector_visible: false,
|
||||
inspector_anchor_y: 0.0,
|
||||
inspector_dismissed: false,
|
||||
inspector_sel_sig: 0,
|
||||
omni_open: false,
|
||||
omni_grid_open: false,
|
||||
omni_create_open: false,
|
||||
overflow_open: false,
|
||||
palette_open: false,
|
||||
palette_query: String::new(),
|
||||
drag: None,
|
||||
anim: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Render the whole mobile shell into `available_rect`.
|
||||
pub fn render_mobile_shell(
|
||||
ui: &mut egui::Ui,
|
||||
available_rect: egui::Rect,
|
||||
rc: &mut RenderContext,
|
||||
state: &mut MobileState,
|
||||
) {
|
||||
let pal = Palette::from_theme(rc.shared.theme, ui.ctx());
|
||||
|
||||
// Orientation (aspect-based; rotation = a resize). Published to panes before anything renders.
|
||||
let landscape = available_rect.width() > available_rect.height();
|
||||
rc.shared.is_portrait = !landscape;
|
||||
// Rotating while 3 panes are open drops to 2 (landscape caps the stack at 2).
|
||||
if landscape && state.window_count > 2 {
|
||||
state.window_count = 2;
|
||||
state.window_top = state.window_top.min(STACK.len().saturating_sub(2));
|
||||
}
|
||||
// On an orientation change, the cached inspector tap-anchor is in the old layout's coordinate
|
||||
// space; drop it to a non-covering default so the portrait reflow doesn't mis-decide until the
|
||||
// next tap re-anchors.
|
||||
if landscape != state.was_landscape {
|
||||
state.was_landscape = landscape;
|
||||
state.inspector_anchor_y = available_rect.top();
|
||||
}
|
||||
|
||||
// Background (device color; bands paint over most of it).
|
||||
ui.painter().rect_filled(available_rect, 0.0, pal.bg);
|
||||
|
||||
// In landscape the top bar is folded into the top pane header (no separate band), reclaiming its
|
||||
// height for the stack.
|
||||
let topbar_h = if landscape { 0.0 } else { TOPBAR_H };
|
||||
let topbar_rect = egui::Rect::from_min_max(
|
||||
available_rect.min,
|
||||
egui::pos2(available_rect.right(), available_rect.top() + topbar_h),
|
||||
);
|
||||
let transport_rect = egui::Rect::from_min_max(
|
||||
egui::pos2(available_rect.left(), available_rect.bottom() - TRANSPORT_H),
|
||||
available_rect.max,
|
||||
);
|
||||
// Region between the top bar and the transport, shared between the stack and (when something is
|
||||
// selected) the inspector sheet that rises above the transport.
|
||||
let region = egui::Rect::from_min_max(
|
||||
egui::pos2(available_rect.left(), topbar_rect.bottom()),
|
||||
egui::pos2(available_rect.right(), transport_rect.top()),
|
||||
);
|
||||
// The inspector sheet appears on pointer *release* (a tap), not on press — so press+drag on the
|
||||
// canvas isn't interrupted by the sheet popping up. Track visibility: hide when nothing's
|
||||
// selected; show once the pointer is up with a selection; keep it shown while it's being
|
||||
// interacted with (pointer down but still selected).
|
||||
let active = inspector::is_active(&rc.shared);
|
||||
let any_down = ui.input(|i| i.pointer.any_down());
|
||||
let sel_sig = inspector::selection_sig(&rc.shared);
|
||||
if sel_sig != state.inspector_sel_sig {
|
||||
// Selection changed → a fresh thing to inspect; clear any prior manual dismissal.
|
||||
state.inspector_dismissed = false;
|
||||
state.inspector_sel_sig = sel_sig;
|
||||
}
|
||||
if !active {
|
||||
state.inspector_visible = false;
|
||||
state.inspector_dismissed = false;
|
||||
} else if !state.inspector_dismissed && !any_down {
|
||||
if !state.inspector_visible {
|
||||
// Just opened (tap released): anchor to the release position for the reflow test below.
|
||||
state.inspector_anchor_y = ui
|
||||
.input(|i| i.pointer.interact_pos())
|
||||
.map(|p| p.y)
|
||||
.unwrap_or(region.bottom());
|
||||
}
|
||||
state.inspector_visible = true;
|
||||
}
|
||||
let mut inspector_shown = state.inspector_visible;
|
||||
|
||||
// Inspector geometry: a bottom sheet in portrait, a right-side vertical column in landscape.
|
||||
// Clamp defensively: on a very small region the desired minimum can exceed the available space,
|
||||
// and `f32::clamp` panics if `min > max`, so keep `min <= max`.
|
||||
let inspector_rect = if landscape {
|
||||
let lo = 180.0_f32.min(region.width());
|
||||
let hi = (region.width() - 140.0).max(lo);
|
||||
let w = (region.width() * state.inspector_width_frac).clamp(lo, hi);
|
||||
egui::Rect::from_min_max(egui::pos2(region.right() - w, region.top()), region.max)
|
||||
} else {
|
||||
let lo = 120.0_f32.min(region.height());
|
||||
let hi = (region.height() - 60.0).max(lo);
|
||||
let h = (region.height() * state.inspector_frac).clamp(lo, hi);
|
||||
egui::Rect::from_min_max(egui::pos2(region.left(), region.bottom() - h), region.max)
|
||||
};
|
||||
|
||||
// Tapping outside the inspector (but not on the transport) dismisses (hides) it. We only hide —
|
||||
// NOT clear the selection — so actions dispatched from overlays (context menu, "+New" grid) still
|
||||
// see the selection. It stays dismissed until the selection changes (sig above).
|
||||
if inspector_shown {
|
||||
let dismiss = ui.input(|i| {
|
||||
i.pointer.primary_pressed()
|
||||
&& i.pointer.press_origin().map_or(false, |p| {
|
||||
!inspector_rect.contains(p) && !transport_rect.contains(p)
|
||||
})
|
||||
});
|
||||
if dismiss {
|
||||
state.inspector_visible = false;
|
||||
state.inspector_dismissed = true;
|
||||
inspector_shown = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Reflow: shrink the stack to make room for the inspector. Landscape always carves horizontally
|
||||
// (side-by-side); portrait only carves when the tapped thing sits below the sheet (else overlay).
|
||||
// Restoring on dismiss is automatic — reflow only changes the render rect.
|
||||
let stack_rect = if !inspector_shown {
|
||||
region
|
||||
} else if landscape {
|
||||
egui::Rect::from_min_max(region.min, egui::pos2(inspector_rect.left(), region.bottom()))
|
||||
} else if state.inspector_anchor_y > inspector_rect.top() {
|
||||
egui::Rect::from_min_max(region.min, egui::pos2(region.right(), inspector_rect.top()))
|
||||
} else {
|
||||
region
|
||||
};
|
||||
|
||||
// Decide whether the instrument pane (PianoRoll = STACK index 4) reveals the roll, from the
|
||||
// *committed* (snapped) window weights — so the keyboard↔roll transition lands on a stack snap
|
||||
// rather than at an arbitrary mid-drag height. Roll shows once the pane is past the smallest snap.
|
||||
rc.shared.instrument_show_roll = {
|
||||
let idx = 4i32 - state.window_top as i32;
|
||||
if idx >= 0 && (idx as usize) < state.window_count {
|
||||
let sum: f32 = state.weights[..state.window_count].iter().map(|w| w.max(0.0)).sum();
|
||||
let w = if sum > 0.0 { state.weights[idx as usize].max(0.0) / sum } else { 0.0 };
|
||||
w > 0.30 // 0.25 preset ⇒ keyboard only; 0.33/0.5/0.75 ⇒ keyboard + roll
|
||||
} else {
|
||||
true
|
||||
}
|
||||
};
|
||||
|
||||
stack::render(ui, stack_rect, rc, state, &pal);
|
||||
|
||||
if inspector_shown {
|
||||
inspector::render(ui, inspector_rect, region, rc, state, &pal, landscape);
|
||||
}
|
||||
|
||||
// Transport floor: drawn last = always on top, the persistent spine.
|
||||
transport::render(ui, transport_rect, &mut rc.shared, &pal);
|
||||
|
||||
// Omnibutton FAB (radial tool menu) — drawn above the stack region, on top of everything else.
|
||||
omni::render(ui, region, rc, state, &pal);
|
||||
|
||||
// Instrument-browser request from the music pane's header → show the Preset Browser fullscreen.
|
||||
if *rc.shared.open_instrument_browser {
|
||||
*rc.shared.open_instrument_browser = false;
|
||||
state.show_instruments = true;
|
||||
state.window_top = 5; // Node/Instrument band (PresetBrowser when show_instruments)
|
||||
state.window_count = 1;
|
||||
state.weights = [1.0, 1.0, 1.0];
|
||||
state.anim = None;
|
||||
}
|
||||
|
||||
// Top bar (filename + ⌕ palette + ⋯ commands). Its menus overlay the whole shell, so it's last.
|
||||
// Portrait: its own band at the top. Landscape: folded into the middle of the top pane header.
|
||||
if landscape {
|
||||
let hh = stack::header_height(false);
|
||||
let top_header = egui::Rect::from_min_max(
|
||||
egui::pos2(stack_rect.left(), region.top()),
|
||||
egui::pos2(stack_rect.right(), region.top() + hh),
|
||||
);
|
||||
topbar::render_inline(ui, top_header, available_rect, rc, state, &pal);
|
||||
} else {
|
||||
topbar::render(ui, topbar_rect, available_rect, rc, state, &pal);
|
||||
}
|
||||
|
||||
// Long-press context menu (populated by whichever pane was long-pressed). Persistent popup,
|
||||
// dispatched via pending_menu_actions.
|
||||
render_context_menu(ui, available_rect, rc);
|
||||
}
|
||||
|
||||
/// Render the pane-populated long-press context menu (`shared.mobile_context_menu`) as a persistent
|
||||
/// popup: it stays open until an item is chosen or the user taps outside it. Styled to match the
|
||||
/// timeline's context menu (default themed popup frame + full-width, touch-height items).
|
||||
fn render_context_menu(ui: &mut egui::Ui, available: egui::Rect, rc: &mut RenderContext) {
|
||||
let Some(menu) = rc.shared.mobile_context_menu.clone() else {
|
||||
return;
|
||||
};
|
||||
// Keep the popup on screen (rough clamp against its estimated size).
|
||||
let est = egui::vec2(200.0, menu.items.len() as f32 * ui.spacing().interact_size.y + 16.0);
|
||||
let pos = egui::pos2(
|
||||
menu.pos.x.min(available.right() - est.x - 8.0).max(available.left() + 8.0),
|
||||
menu.pos.y.min(available.bottom() - est.y - 8.0).max(available.top() + 8.0),
|
||||
);
|
||||
|
||||
let mut chosen: Option<crate::menu::MenuAction> = None;
|
||||
let area = egui::Area::new(ui.id().with("mobile_ctx_menu"))
|
||||
.order(egui::Order::Foreground)
|
||||
.fixed_pos(pos)
|
||||
.interactable(true)
|
||||
.show(ui.ctx(), |ui| {
|
||||
egui::Frame::popup(ui.style()).show(ui, |ui| {
|
||||
ui.set_min_width(180.0);
|
||||
for (label, action) in &menu.items {
|
||||
let w = ui.available_width();
|
||||
let (rect, resp) =
|
||||
ui.allocate_exact_size(egui::vec2(w, ui.spacing().interact_size.y), egui::Sense::click());
|
||||
if ui.is_rect_visible(rect) {
|
||||
if resp.hovered() {
|
||||
ui.painter().rect_filled(rect, 2.0, ui.visuals().widgets.hovered.bg_fill);
|
||||
}
|
||||
let tc = if resp.hovered() {
|
||||
ui.visuals().widgets.hovered.text_color()
|
||||
} else {
|
||||
ui.visuals().widgets.inactive.text_color()
|
||||
};
|
||||
ui.painter().text(
|
||||
rect.min + egui::vec2(10.0, (rect.height() - 14.0) / 2.0),
|
||||
egui::Align2::LEFT_TOP,
|
||||
label,
|
||||
egui::FontId::proportional(14.0),
|
||||
tc,
|
||||
);
|
||||
}
|
||||
if resp.clicked() {
|
||||
chosen = Some(*action);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Dismiss on item choice or a primary click outside the popup. (The secondary click that opened
|
||||
// it won't dismiss, so there's no just-opened race.)
|
||||
let primary_click = ui.input(|i| i.pointer.button_clicked(egui::PointerButton::Primary));
|
||||
let dismiss = chosen.is_some() || (primary_click && !area.response.contains_pointer());
|
||||
if let Some(action) = chosen {
|
||||
rc.shared.pending_menu_actions.push(action);
|
||||
}
|
||||
if dismiss {
|
||||
*rc.shared.mobile_context_menu = None;
|
||||
}
|
||||
}
|
||||
@@ -1,337 +0,0 @@
|
||||
//! Omnibutton — a floating action button whose radial holds the active layer's drawing tools plus
|
||||
//! a "+New" petal (create layers / import / clip ops) and, when there are extra tools, a "⋯ more"
|
||||
//! petal that opens a full-tools grid. Tools drive `shared.selected_tool`; create actions dispatch
|
||||
//! `MenuAction`s. The radial fans up and to the left, away from the thumb. Pane-internal adds
|
||||
//! (nodes/notes/instruments) are handled inside their panes (P6), not here.
|
||||
|
||||
use eframe::egui;
|
||||
use lightningbeam_core::layer::{AnyLayer, LayerType};
|
||||
use lightningbeam_core::selection::FocusSelection;
|
||||
use lightningbeam_core::tool::Tool;
|
||||
|
||||
use super::{icons, MobileState, Palette};
|
||||
use crate::menu::MenuAction;
|
||||
use crate::RenderContext;
|
||||
|
||||
const FAB_R: f32 = 26.0;
|
||||
const IR: f32 = 20.0;
|
||||
const PRIMARY: [Tool; 14] = [
|
||||
Tool::Select,
|
||||
Tool::Draw,
|
||||
Tool::Transform,
|
||||
Tool::Rectangle,
|
||||
Tool::Ellipse,
|
||||
Tool::Line,
|
||||
Tool::Polygon,
|
||||
Tool::PaintBucket,
|
||||
Tool::Gradient,
|
||||
Tool::Eyedropper,
|
||||
Tool::Text,
|
||||
Tool::Erase,
|
||||
Tool::BezierEdit,
|
||||
Tool::Split,
|
||||
];
|
||||
const RING_CAP: usize = 10;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Special {
|
||||
More,
|
||||
New,
|
||||
}
|
||||
|
||||
fn tool_icon(t: Tool) -> &'static str {
|
||||
match t {
|
||||
Tool::Select | Tool::RegionSelect => icons::MOUSE_POINTER_2,
|
||||
Tool::SelectLasso => icons::LASSO_SELECT,
|
||||
Tool::Draw | Tool::Pencil | Tool::Pen => icons::PENCIL,
|
||||
Tool::Transform => icons::VECTOR_SQUARE,
|
||||
Tool::Rectangle => icons::SQUARE,
|
||||
Tool::Ellipse => icons::CIRCLE,
|
||||
Tool::Line => icons::MINUS,
|
||||
Tool::Polygon => icons::HEXAGON,
|
||||
Tool::PaintBucket => icons::PAINT_BUCKET,
|
||||
Tool::Gradient => icons::BLEND,
|
||||
Tool::Eyedropper => icons::PIPETTE,
|
||||
Tool::Text => icons::TYPE,
|
||||
Tool::Erase => icons::ERASER,
|
||||
Tool::BezierEdit => icons::PEN_TOOL,
|
||||
Tool::MagicWand | Tool::QuickSelect => icons::WAND_SPARKLES,
|
||||
Tool::Split => icons::SCISSORS,
|
||||
_ => icons::BRUSH,
|
||||
}
|
||||
}
|
||||
|
||||
fn active_layer_type(rc: &RenderContext) -> Option<LayerType> {
|
||||
rc.shared
|
||||
.active_layer_id
|
||||
.and_then(|id| rc.shared.action_executor.document().get_layer(&id))
|
||||
.map(|layer| match layer {
|
||||
AnyLayer::Vector(_) => LayerType::Vector,
|
||||
AnyLayer::Audio(_) => LayerType::Audio,
|
||||
AnyLayer::Video(_) => LayerType::Video,
|
||||
AnyLayer::Effect(_) => LayerType::Effect,
|
||||
AnyLayer::Group(_) => LayerType::Group,
|
||||
AnyLayer::Raster(_) => LayerType::Raster,
|
||||
AnyLayer::Text(_) => LayerType::Text,
|
||||
})
|
||||
}
|
||||
|
||||
/// The create-menu items: (label, icon, action, enabled). Split/Duplicate need a selected clip.
|
||||
fn create_items(rc: &RenderContext) -> Vec<(&'static str, &'static str, MenuAction, bool)> {
|
||||
let clip_sel = matches!(&*rc.shared.focus, FocusSelection::ClipInstances(ids) if !ids.is_empty());
|
||||
vec![
|
||||
("Vector", icons::PEN_TOOL, MenuAction::AddLayer, true),
|
||||
("Audio", icons::AUDIO_WAVEFORM, MenuAction::AddAudioTrack, true),
|
||||
("MIDI", icons::PIANO, MenuAction::AddMidiTrack, true),
|
||||
("Raster", icons::BRUSH, MenuAction::AddRasterLayer, true),
|
||||
("Video", icons::CLAPPERBOARD, MenuAction::AddVideoLayer, true),
|
||||
("Group", icons::FOLDER, MenuAction::Group, true),
|
||||
("Import", icons::FILE_PLUS, MenuAction::Import, true),
|
||||
("Split", icons::SCISSORS, MenuAction::SplitClip, clip_sel),
|
||||
("Duplicate", icons::COPY, MenuAction::DuplicateClip, clip_sel),
|
||||
]
|
||||
}
|
||||
|
||||
fn close_all(state: &mut MobileState) {
|
||||
state.omni_open = false;
|
||||
state.omni_grid_open = false;
|
||||
state.omni_create_open = false;
|
||||
}
|
||||
|
||||
pub fn render(ui: &mut egui::Ui, region: egui::Rect, rc: &mut RenderContext, state: &mut MobileState, pal: &Palette) {
|
||||
let ctx_tools: Vec<Tool> = Tool::for_layer_type(active_layer_type(rc)).to_vec();
|
||||
let primary: Vec<Tool> = PRIMARY
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|t| ctx_tools.contains(t))
|
||||
.take(RING_CAP)
|
||||
.collect();
|
||||
let has_more = ctx_tools.len() > primary.len();
|
||||
|
||||
// Tools are only relevant when the Stage is on screen. Off-Stage the omnibutton is purely a
|
||||
// "+New" create button.
|
||||
let stage_slot = super::STACK
|
||||
.iter()
|
||||
.position(|p| *p == super::StackPane::Stage)
|
||||
.unwrap_or(2);
|
||||
let stage_visible = (state.window_top..state.window_top + state.window_count).contains(&stage_slot);
|
||||
if !stage_visible {
|
||||
state.omni_open = false; // no tool radial off-Stage
|
||||
}
|
||||
|
||||
let fab_center = egui::pos2(region.right() - 18.0 - FAB_R, region.bottom() - 18.0 - FAB_R);
|
||||
|
||||
if state.omni_grid_open {
|
||||
// Full-tools grid.
|
||||
let cells: Vec<Cell> = ctx_tools
|
||||
.iter()
|
||||
.map(|t| Cell {
|
||||
icon: tool_icon(*t),
|
||||
label: t.display_name(),
|
||||
selected: *rc.shared.selected_tool == *t,
|
||||
enabled: true,
|
||||
})
|
||||
.collect();
|
||||
let (close, clicked) = draw_grid(ui, region, "Tools", &cells, pal);
|
||||
if let Some(i) = clicked {
|
||||
*rc.shared.selected_tool = ctx_tools[i];
|
||||
close_all(state);
|
||||
} else if close {
|
||||
close_all(state);
|
||||
}
|
||||
} else if state.omni_create_open {
|
||||
// Create grid.
|
||||
let items = create_items(rc);
|
||||
let cells: Vec<Cell> = items
|
||||
.iter()
|
||||
.map(|it| Cell {
|
||||
icon: it.1,
|
||||
label: it.0,
|
||||
selected: false,
|
||||
enabled: it.3,
|
||||
})
|
||||
.collect();
|
||||
let (close, clicked) = draw_grid(ui, region, "New", &cells, pal);
|
||||
if let Some(i) = clicked {
|
||||
rc.shared.pending_menu_actions.push(items[i].2);
|
||||
close_all(state);
|
||||
} else if close {
|
||||
close_all(state);
|
||||
}
|
||||
} else if state.omni_open {
|
||||
// Radial: scrim, then tool petals + special petals (more / new).
|
||||
let scrim = ui.interact(region, ui.id().with("mobile_omni_scrim"), egui::Sense::click());
|
||||
ui.painter().rect_filled(region, 0.0, pal.scrim);
|
||||
if scrim.clicked() {
|
||||
state.omni_open = false;
|
||||
}
|
||||
|
||||
let mut specials: Vec<Special> = Vec::new();
|
||||
if has_more {
|
||||
specials.push(Special::More);
|
||||
}
|
||||
specials.push(Special::New);
|
||||
let n = primary.len() + specials.len();
|
||||
|
||||
for k in 0..n {
|
||||
let frac = if n > 1 { k as f32 / (n as f32 - 1.0) } else { 0.5 };
|
||||
let deg = 74.0 + frac * 126.0; // 74°..200°, up and to the left
|
||||
let a = deg.to_radians();
|
||||
let radius = if k % 2 == 0 { 114.0 } else { 170.0 };
|
||||
let c = fab_center + egui::vec2(radius * a.cos(), -radius * a.sin());
|
||||
let rect = egui::Rect::from_center_size(c, egui::vec2(IR * 2.0, IR * 2.0));
|
||||
|
||||
if k < primary.len() {
|
||||
let t = primary[k];
|
||||
let resp = ui.interact(rect, ui.id().with(("mobile_omni_tool", k)), egui::Sense::click());
|
||||
let selected = *rc.shared.selected_tool == t;
|
||||
petal(ui, c, if selected { pal.accent } else { petal_bg(&resp, pal) }, tool_icon(t), if selected { pal.on_accent } else { pal.text }, pal);
|
||||
if resp.clicked() {
|
||||
*rc.shared.selected_tool = t;
|
||||
state.omni_open = false;
|
||||
}
|
||||
} else {
|
||||
match specials[k - primary.len()] {
|
||||
Special::More => {
|
||||
let resp = ui.interact(rect, ui.id().with("mobile_omni_more"), egui::Sense::click());
|
||||
petal(ui, c, petal_bg(&resp, pal), icons::MENU, pal.text, pal);
|
||||
if resp.clicked() {
|
||||
state.omni_grid_open = true;
|
||||
}
|
||||
}
|
||||
Special::New => {
|
||||
let resp = ui.interact(rect, ui.id().with("mobile_omni_new"), egui::Sense::click());
|
||||
petal(ui, c, pal.accent, icons::PLUS, pal.on_accent, pal);
|
||||
if resp.hovered() {
|
||||
ui.painter().circle_stroke(c, IR, egui::Stroke::new(1.5, pal.text));
|
||||
}
|
||||
if resp.clicked() {
|
||||
state.omni_create_open = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The FAB itself (on top). Closed → current tool icon; open → ✕.
|
||||
let fab_rect = egui::Rect::from_center_size(fab_center, egui::vec2(FAB_R * 2.0, FAB_R * 2.0));
|
||||
let fresp = ui.interact(fab_rect, ui.id().with("mobile_omni_fab"), egui::Sense::click());
|
||||
ui.painter().circle_filled(fab_center, FAB_R, pal.accent);
|
||||
let open = state.omni_open || state.omni_grid_open || state.omni_create_open;
|
||||
let glyph = if open {
|
||||
icons::X
|
||||
} else if stage_visible {
|
||||
tool_icon(*rc.shared.selected_tool)
|
||||
} else {
|
||||
icons::PLUS // off-Stage: a create button
|
||||
};
|
||||
ui.painter()
|
||||
.text(fab_center, egui::Align2::CENTER_CENTER, glyph, icons::font(22.0), pal.on_accent);
|
||||
if fresp.clicked() {
|
||||
if open {
|
||||
close_all(state);
|
||||
} else if stage_visible {
|
||||
state.omni_open = true;
|
||||
} else {
|
||||
state.omni_create_open = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn petal_bg(resp: &egui::Response, pal: &Palette) -> egui::Color32 {
|
||||
if resp.hovered() {
|
||||
pal.line
|
||||
} else {
|
||||
pal.surface_alt
|
||||
}
|
||||
}
|
||||
|
||||
fn petal(ui: &egui::Ui, c: egui::Pos2, bg: egui::Color32, glyph: &str, fg: egui::Color32, pal: &Palette) {
|
||||
ui.painter().circle_filled(c, IR, bg);
|
||||
ui.painter().circle_stroke(c, IR, egui::Stroke::new(1.0, pal.line));
|
||||
ui.painter().text(c, egui::Align2::CENTER_CENTER, glyph, icons::font(18.0), fg);
|
||||
}
|
||||
|
||||
struct Cell {
|
||||
icon: &'static str,
|
||||
label: &'static str,
|
||||
selected: bool,
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
/// A modal grid sheet of icon+label cells. Returns (backdrop-tapped, Some(clicked-enabled-index)).
|
||||
fn draw_grid(ui: &mut egui::Ui, region: egui::Rect, title: &str, cells: &[Cell], pal: &Palette) -> (bool, Option<usize>) {
|
||||
let scrim = ui.interact(region, ui.id().with(("mobile_omni_gridscrim", title)), egui::Sense::click());
|
||||
ui.painter().rect_filled(region, 0.0, pal.scrim);
|
||||
|
||||
let panel = region.shrink2(egui::vec2(16.0, 40.0));
|
||||
ui.painter().rect_filled(panel, 14.0, pal.surface);
|
||||
ui.painter().rect_stroke(panel, 14.0, egui::Stroke::new(1.0, pal.line), egui::StrokeKind::Inside);
|
||||
ui.painter().text(
|
||||
egui::pos2(panel.left() + 16.0, panel.top() + 18.0),
|
||||
egui::Align2::LEFT_CENTER,
|
||||
title,
|
||||
egui::FontId::proportional(14.0),
|
||||
pal.text,
|
||||
);
|
||||
|
||||
let cols = 4usize;
|
||||
let n = cells.len();
|
||||
let rows = n.div_ceil(cols).max(1);
|
||||
let grid = egui::Rect::from_min_max(
|
||||
egui::pos2(panel.left() + 10.0, panel.top() + 36.0),
|
||||
egui::pos2(panel.right() - 10.0, panel.bottom() - 10.0),
|
||||
);
|
||||
let cw = grid.width() / cols as f32;
|
||||
let chh = (grid.height() / rows as f32).min(96.0);
|
||||
let mut clicked = None;
|
||||
for (i, cell) in cells.iter().enumerate() {
|
||||
let col = (i % cols) as f32;
|
||||
let row = (i / cols) as f32;
|
||||
let r = egui::Rect::from_min_size(
|
||||
egui::pos2(grid.left() + col * cw, grid.top() + row * chh),
|
||||
egui::vec2(cw, chh),
|
||||
)
|
||||
.shrink(5.0);
|
||||
let resp = if cell.enabled {
|
||||
ui.interact(r, ui.id().with(("mobile_omni_cell", title, i)), egui::Sense::click())
|
||||
} else {
|
||||
ui.interact(r, ui.id().with(("mobile_omni_cell_off", title, i)), egui::Sense::hover())
|
||||
};
|
||||
let bg = if cell.selected {
|
||||
pal.accent
|
||||
} else if cell.enabled && resp.hovered() {
|
||||
pal.surface_alt
|
||||
} else {
|
||||
pal.surface
|
||||
};
|
||||
ui.painter().rect_filled(r, 10.0, bg);
|
||||
ui.painter().rect_stroke(r, 10.0, egui::Stroke::new(1.0, pal.line), egui::StrokeKind::Inside);
|
||||
let fg = if cell.selected {
|
||||
pal.on_accent
|
||||
} else if cell.enabled {
|
||||
pal.text
|
||||
} else {
|
||||
pal.line
|
||||
};
|
||||
ui.painter().text(
|
||||
egui::pos2(r.center().x, r.top() + r.height() * 0.38),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
cell.icon,
|
||||
icons::font(22.0),
|
||||
fg,
|
||||
);
|
||||
ui.painter().text(
|
||||
egui::pos2(r.center().x, r.bottom() - 12.0),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
cell.label,
|
||||
egui::FontId::proportional(9.5),
|
||||
if cell.selected { pal.on_accent } else if cell.enabled { pal.text_dim } else { pal.line },
|
||||
);
|
||||
if cell.enabled && resp.clicked() {
|
||||
clicked = Some(i);
|
||||
}
|
||||
}
|
||||
(scrim.clicked() && clicked.is_none(), clicked)
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
//! The mobile UI's semantic color palette, sourced from the shared theme CSS variables so it
|
||||
//! matches the main UI and responds to light/dark/user themes. Built once per frame from the active
|
||||
//! [`Theme`](crate::theme::Theme) and passed (by `Copy`) into the mobile render functions, replacing
|
||||
//! the per-file hardcoded color constants.
|
||||
|
||||
use eframe::egui::{Color32, Context};
|
||||
|
||||
use crate::theme::Theme;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Palette {
|
||||
/// App/device background behind everything.
|
||||
pub bg: Color32,
|
||||
/// Panels, cards, bands, sheets.
|
||||
pub surface: Color32,
|
||||
/// Raised / hovered surface.
|
||||
pub surface_alt: Color32,
|
||||
/// Band headers, the top bar.
|
||||
pub header: Color32,
|
||||
/// Borders and dividers.
|
||||
pub line: Color32,
|
||||
/// Primary text / icons.
|
||||
pub text: Color32,
|
||||
/// Secondary (dim) text / icons.
|
||||
pub text_dim: Color32,
|
||||
/// Selection / active accent.
|
||||
pub accent: Color32,
|
||||
/// Text / icons drawn on top of `accent`.
|
||||
pub on_accent: Color32,
|
||||
/// Modal backdrop (translucent).
|
||||
pub scrim: Color32,
|
||||
/// Decorative category accents (intent picker, chips): coral, cyan, amber, pink, violet.
|
||||
pub accents: [Color32; 5],
|
||||
}
|
||||
|
||||
impl Palette {
|
||||
pub fn from_theme(theme: &Theme, ctx: &Context) -> Self {
|
||||
let c = |name: &str, fb: Color32| theme.var(name, ctx).unwrap_or(fb);
|
||||
Self {
|
||||
bg: c("bg-app", Color32::from_rgb(0x2a, 0x2a, 0x2a)),
|
||||
surface: c("bg-panel", Color32::from_rgb(0x22, 0x22, 0x22)),
|
||||
surface_alt: c("bg-surface-raised", Color32::from_rgb(0x3f, 0x3f, 0x3f)),
|
||||
header: c("bg-header", Color32::from_rgb(0x35, 0x35, 0x35)),
|
||||
line: c("border-default", Color32::from_rgb(0x44, 0x44, 0x44)),
|
||||
text: c("text-primary", Color32::from_rgb(0xf6, 0xf6, 0xf6)),
|
||||
text_dim: c("text-secondary", Color32::from_rgb(0xaa, 0xaa, 0xaa)),
|
||||
accent: c("accent", Color32::from_rgb(0x39, 0x6c, 0xd8)),
|
||||
on_accent: c("text-on-accent", Color32::WHITE),
|
||||
scrim: c("scrim", Color32::from_rgba_unmultiplied(0x10, 0x14, 0x1a, 0xb0)),
|
||||
accents: [
|
||||
c("accent-coral", Color32::from_rgb(0xe8, 0x82, 0x6b)),
|
||||
c("accent-cyan", Color32::from_rgb(0x54, 0xc3, 0xe8)),
|
||||
c("accent-amber", Color32::from_rgb(0xf4, 0xa3, 0x40)),
|
||||
c("accent-pink", Color32::from_rgb(0xc7, 0x5b, 0x8a)),
|
||||
c("accent-violet", Color32::from_rgb(0x8a, 0x6e, 0xc0)),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,862 +0,0 @@
|
||||
//! The vertical sliding-window stack engine.
|
||||
//!
|
||||
//! A window of 2–3 consecutive panes (from [`super::STACK`]) is visible. Dragging the dividers
|
||||
//! between visible panes and the top/bottom screen edges grows, shrinks, or slides the window via
|
||||
//! the operations below (verified against the user's transition table):
|
||||
//!
|
||||
//! ```text
|
||||
//! R1 upper divider, UP -> slide window DOWN one (top+=1)
|
||||
//! R4 upper divider, DOWN -> drop BOTTOM pane (count 3->2)
|
||||
//! R3 lower divider, UP -> drop TOP pane (top+=1, count 3->2)
|
||||
//! R2 lower divider, DOWN -> slide window UP one (top-=1)
|
||||
//! R5 bottom edge, UP -> grow at bottom; if already 3, slide down
|
||||
//! R6 top edge, DOWN -> grow at top; if already 3, slide up
|
||||
//! ```
|
||||
//!
|
||||
//! At count==2 the single divider is "upper" when dragged up (R1) and "lower" when dragged
|
||||
//! down (R2). The window content interpolates continuously during a drag and snaps on release.
|
||||
|
||||
use eframe::egui;
|
||||
|
||||
use super::{icons, slot_path, LayoutAnim, MobileState, Palette, StackDrag, StackPane, STACK};
|
||||
use crate::RenderContext;
|
||||
|
||||
const N: usize = STACK.len();
|
||||
|
||||
/// Height of each band's drag header (and the bottom-edge footer). The whole header is the grab
|
||||
/// target — there's no thin divider bar.
|
||||
const HEADER_H: f32 = 52.0;
|
||||
/// Landscape headers are shorter — vertical space is scarce, and the top one hosts the app bar.
|
||||
const HEADER_H_LANDSCAPE: f32 = 34.0;
|
||||
/// Header height for the current orientation.
|
||||
pub fn header_height(is_portrait: bool) -> f32 {
|
||||
if is_portrait { HEADER_H } else { HEADER_H_LANDSCAPE }
|
||||
}
|
||||
/// Corner radius (px) for the rounded top of each header.
|
||||
const HEADER_RADIUS: u8 = 9;
|
||||
const FOOTER_H: f32 = 28.0;
|
||||
/// Width of a header right-side button (fullscreen, node toggle).
|
||||
const BTN_W: f32 = 44.0;
|
||||
/// Max pixels of drag to complete a window transition (so you don't drag half the screen).
|
||||
const TRIGGER_MAX: f32 = 150.0;
|
||||
|
||||
/// A draggable boundary of the stack.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Handle {
|
||||
TopEdge,
|
||||
/// Divider below visible pane `k` (between visible pane `k` and `k+1`).
|
||||
Divider(usize),
|
||||
BottomEdge,
|
||||
}
|
||||
|
||||
// --- R1..R6 as pure ops on (top, count) -> Option<(top, count)> ---
|
||||
|
||||
fn op_r1(top: usize, count: usize) -> Option<(usize, usize)> {
|
||||
(top + count < N).then(|| (top + 1, count)) // slide down
|
||||
}
|
||||
fn op_r2(top: usize, count: usize) -> Option<(usize, usize)> {
|
||||
(top > 0).then(|| (top - 1, count)) // slide up
|
||||
}
|
||||
fn op_r3(top: usize, count: usize) -> Option<(usize, usize)> {
|
||||
(count == 3).then(|| (top + 1, 2)) // drop top
|
||||
}
|
||||
fn op_r4(top: usize, count: usize) -> Option<(usize, usize)> {
|
||||
(count == 3).then(|| (top, 2)) // drop bottom
|
||||
}
|
||||
fn op_r5(top: usize, count: usize, max: usize) -> Option<(usize, usize)> {
|
||||
if count < max && top + count < N {
|
||||
Some((top, count + 1)) // grow bottom
|
||||
} else if count == max && top + count < N {
|
||||
Some((top + 1, count)) // slide down
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn op_r6(top: usize, count: usize, max: usize) -> Option<(usize, usize)> {
|
||||
if count < max && top > 0 {
|
||||
Some((top - 1, count + 1)) // grow top
|
||||
} else if count == max && top > 0 {
|
||||
Some((top - 1, count)) // slide up
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Given a handle and signed drag offset, resolve the target window config and progress `t`.
|
||||
/// Returns None if the drag direction has no valid operation (e.g. at a list boundary).
|
||||
fn resolve(
|
||||
handle: Handle,
|
||||
offset: f32,
|
||||
top: usize,
|
||||
count: usize,
|
||||
trigger: f32,
|
||||
max: usize,
|
||||
) -> Option<(usize, usize, f32)> {
|
||||
let going_up = offset < 0.0;
|
||||
let t = (offset.abs() / trigger.max(1.0)).clamp(0.0, 1.0);
|
||||
let target = match handle {
|
||||
Handle::TopEdge => (!going_up).then(|| op_r6(top, count, max)).flatten(),
|
||||
Handle::BottomEdge => going_up.then(|| op_r5(top, count, max)).flatten(),
|
||||
Handle::Divider(k) => {
|
||||
if count == 2 {
|
||||
if going_up { op_r1(top, count) } else { op_r2(top, count) }
|
||||
} else if k == 0 {
|
||||
// upper divider
|
||||
if going_up { op_r1(top, count) } else { op_r4(top, count) }
|
||||
} else {
|
||||
// lower divider
|
||||
if going_up { op_r3(top, count) } else { op_r2(top, count) }
|
||||
}
|
||||
}
|
||||
};
|
||||
target.map(|(tt, tc)| (tt, tc, t))
|
||||
}
|
||||
|
||||
// --- rect layout ---
|
||||
|
||||
/// Below/above these boundary positions, a divider release collapses a group → membership change.
|
||||
const COLLAPSE_LO: f32 = 0.12;
|
||||
const COLLAPSE_HI: f32 = 0.88;
|
||||
|
||||
// The only allowed pane-weight distributions per window size. Snapping to these keeps the minimum
|
||||
// pane height manageable (no pane below ~1/4).
|
||||
const PRESETS_1: [[f32; 3]; 1] = [[1.0, 0.0, 0.0]];
|
||||
const PRESETS_2: [[f32; 3]; 3] = [[0.25, 0.75, 0.0], [0.5, 0.5, 0.0], [0.75, 0.25, 0.0]];
|
||||
const PRESETS_3: [[f32; 3]; 3] = [
|
||||
[0.5, 0.25, 0.25],
|
||||
[1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0],
|
||||
[0.25, 0.25, 0.5],
|
||||
];
|
||||
|
||||
fn presets(count: usize) -> &'static [[f32; 3]] {
|
||||
match count {
|
||||
2 => &PRESETS_2,
|
||||
3 => &PRESETS_3,
|
||||
_ => &PRESETS_1,
|
||||
}
|
||||
}
|
||||
|
||||
/// The allowed preset nearest (L2) to the current weights.
|
||||
fn nearest_preset(cur: &[f32], count: usize) -> [f32; 3] {
|
||||
let dist = |p: &[f32; 3]| (0..count).map(|i| (p[i] - cur[i]).powi(2)).sum::<f32>();
|
||||
presets(count)
|
||||
.iter()
|
||||
.min_by(|a, b| dist(a).total_cmp(&dist(b)))
|
||||
.copied()
|
||||
.unwrap_or([1.0, 0.0, 0.0])
|
||||
}
|
||||
|
||||
// 3-pane snapping rides a single path parameter s in [0, 2] through the three presets, so the two
|
||||
// dividers move together and reach their snaps simultaneously. The dragged divider's boundary
|
||||
// position (pane0 for the upper divider; pane0+pane1 for the lower) maps onto s.
|
||||
const PATH3_UPPER: [f32; 3] = [0.5, 1.0 / 3.0, 0.25]; // pane0 at s = 0, 1, 2
|
||||
const PATH3_LOWER: [f32; 3] = [0.75, 2.0 / 3.0, 0.5]; // pane0+pane1 at s = 0, 1, 2
|
||||
/// A pane grown beyond this collapses the 3-pane window to 2 panes on release.
|
||||
const COLLAPSE_GROW: f32 = 0.66;
|
||||
|
||||
fn lerp3(a: [f32; 3], b: [f32; 3], t: f32) -> [f32; 3] {
|
||||
[
|
||||
lerp_f(a[0], b[0], t),
|
||||
lerp_f(a[1], b[1], t),
|
||||
lerp_f(a[2], b[2], t),
|
||||
]
|
||||
}
|
||||
|
||||
/// Weights along the 3-pane preset path at parameter `s` ∈ [0, 2].
|
||||
fn path3(s: f32) -> [f32; 3] {
|
||||
let s = s.clamp(0.0, 2.0);
|
||||
if s <= 1.0 {
|
||||
lerp3(PRESETS_3[0], PRESETS_3[1], s)
|
||||
} else {
|
||||
lerp3(PRESETS_3[1], PRESETS_3[2], s - 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Path parameter for divider `k` whose boundary position is `b` (within the path's range).
|
||||
fn s_from_boundary(k: usize, b: f32) -> f32 {
|
||||
let pts = if k == 0 { PATH3_UPPER } else { PATH3_LOWER };
|
||||
if b >= pts[1] {
|
||||
(pts[0] - b) / (pts[0] - pts[1]) // 0 at pts[0] … 1 at pts[1]
|
||||
} else {
|
||||
1.0 + (pts[1] - b) / (pts[1] - pts[2]) // 1 at pts[1] … 2 at pts[2]
|
||||
}
|
||||
}
|
||||
|
||||
/// The 3-pane path boundary range for divider `k`: (min, max) of its position across the presets.
|
||||
fn path3_range(k: usize) -> (f32, f32) {
|
||||
let pts = if k == 0 { PATH3_UPPER } else { PATH3_LOWER };
|
||||
(pts[2], pts[0])
|
||||
}
|
||||
|
||||
/// Live weights while dragging divider `k` to boundary `b` in a 3-pane window: follow the linked
|
||||
/// path within range, and beyond it group-resize from the nearer path endpoint (so the motion is
|
||||
/// continuous across the extreme presets — no jump in the other divider).
|
||||
fn live_weights3(k: usize, b_unclamped: f32) -> [f32; 3] {
|
||||
let (lo, hi) = path3_range(k);
|
||||
if b_unclamped >= lo && b_unclamped <= hi {
|
||||
path3(s_from_boundary(k, b_unclamped))
|
||||
} else {
|
||||
let anchor = if b_unclamped > hi { path3(0.0) } else { path3(2.0) };
|
||||
let (bmin, bmax) = boundary_bounds(3, k);
|
||||
to_arr(&weights_for_boundary(&anchor, k, b_unclamped.clamp(bmin, bmax)))
|
||||
}
|
||||
}
|
||||
/// Minimum normalized size a pane may be squeezed to during a live divider drag.
|
||||
const MIN_FRAC: f32 = 0.05;
|
||||
/// Duration (seconds) of the ease into a snapped divider position.
|
||||
const SNAP_ANIM_SECS: f64 = 0.10;
|
||||
|
||||
fn ease_out(p: f32) -> f32 {
|
||||
1.0 - (1.0 - p).powi(3)
|
||||
}
|
||||
|
||||
/// Normalized pane weights (the first `count` stored weights, summing to 1).
|
||||
fn nweights(weights: &[f32; 3], count: usize) -> Vec<f32> {
|
||||
let mut w: Vec<f32> = weights[..count].iter().map(|x| x.max(0.0001)).collect();
|
||||
let s: f32 = w.iter().sum();
|
||||
if s > 0.0 {
|
||||
for x in &mut w {
|
||||
*x /= s;
|
||||
}
|
||||
} else {
|
||||
let e = 1.0 / count as f32;
|
||||
w.iter_mut().for_each(|x| *x = e);
|
||||
}
|
||||
w
|
||||
}
|
||||
|
||||
/// The bottom Y of the given stack slot's band within `region` (using the current window +
|
||||
/// weights), or None if that slot isn't in the visible window. Used to decide whether the
|
||||
/// inspector sheet would cover the selected pane.
|
||||
#[allow(dead_code)] // kept for pane-coverage heuristics
|
||||
pub fn pane_bottom_in(state: &MobileState, region: egui::Rect, slot: usize) -> Option<f32> {
|
||||
let (top, count) = (state.window_top, state.window_count);
|
||||
if slot < top || slot >= top + count {
|
||||
return None;
|
||||
}
|
||||
let nw = nweights(&state.weights, count);
|
||||
let cum: f32 = nw[..=(slot - top)].iter().sum();
|
||||
Some(region.top() + cum * region.height())
|
||||
}
|
||||
|
||||
/// Lay out `count` panes in `rect` using normalized weights `nw`.
|
||||
fn config_rects(top: usize, count: usize, rect: egui::Rect, nw: &[f32]) -> Vec<(usize, egui::Rect)> {
|
||||
let h = rect.height();
|
||||
let mut out = Vec::with_capacity(count);
|
||||
let mut y = rect.top();
|
||||
for i in 0..count {
|
||||
let ph = nw[i] * h;
|
||||
out.push((
|
||||
top + i,
|
||||
egui::Rect::from_min_max(egui::pos2(rect.left(), y), egui::pos2(rect.right(), y + ph)),
|
||||
));
|
||||
y += ph;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn lerp_f(a: f32, b: f32, t: f32) -> f32 {
|
||||
a + (b - a) * t
|
||||
}
|
||||
fn lerp_rect(a: egui::Rect, b: egui::Rect, t: f32) -> egui::Rect {
|
||||
egui::Rect::from_min_max(
|
||||
egui::pos2(lerp_f(a.min.x, b.min.x, t), lerp_f(a.min.y, b.min.y, t)),
|
||||
egui::pos2(lerp_f(a.max.x, b.max.x, t), lerp_f(a.max.y, b.max.y, t)),
|
||||
)
|
||||
}
|
||||
fn collapsed(rect: egui::Rect, at_top: bool) -> egui::Rect {
|
||||
let y = if at_top { rect.top() } else { rect.bottom() };
|
||||
egui::Rect::from_min_max(egui::pos2(rect.left(), y), egui::pos2(rect.right(), y))
|
||||
}
|
||||
|
||||
/// Allowed range of divider `k`'s boundary position (normalized) given MIN_FRAC per pane.
|
||||
/// The boundary splits the window into the group above (panes 0..=k) and below (panes k+1..).
|
||||
fn boundary_bounds(count: usize, k: usize) -> (f32, f32) {
|
||||
let above = (k + 1) as f32;
|
||||
let below = (count - 1 - k) as f32;
|
||||
(above * MIN_FRAC, 1.0 - below * MIN_FRAC)
|
||||
}
|
||||
|
||||
/// New normalized weights when divider `k` is moved to boundary position `b`: the panes above the
|
||||
/// divider are scaled to fill `[0, b]` and those below to fill `[b, 1]`, each group keeping its
|
||||
/// internal proportions. This makes the *other* dividers in the group move with the dragged one.
|
||||
fn weights_for_boundary(nw: &[f32], k: usize, b: f32) -> Vec<f32> {
|
||||
let count = nw.len();
|
||||
let above_sum: f32 = nw[..=k].iter().sum();
|
||||
let below_sum: f32 = nw[k + 1..].iter().sum();
|
||||
let mut w = nw.to_vec();
|
||||
if above_sum > 0.0 {
|
||||
let s = b / above_sum;
|
||||
for x in &mut w[..=k] {
|
||||
*x *= s;
|
||||
}
|
||||
}
|
||||
if below_sum > 0.0 {
|
||||
let s = (1.0 - b) / below_sum;
|
||||
for x in &mut w[k + 1..count] {
|
||||
*x *= s;
|
||||
}
|
||||
}
|
||||
w
|
||||
}
|
||||
|
||||
fn to_arr(v: &[f32]) -> [f32; 3] {
|
||||
let mut a = [0.0; 3];
|
||||
for (i, x) in v.iter().take(3).enumerate() {
|
||||
a[i] = *x;
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
fn even_arr(count: usize) -> [f32; 3] {
|
||||
let mut a = [0.0; 3];
|
||||
let e = 1.0 / count as f32;
|
||||
for x in a.iter_mut().take(count) {
|
||||
*x = e;
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
/// Interpolate between two precomputed rect lists by `t` (used for edge membership transitions).
|
||||
fn interp_layout(
|
||||
c: &[(usize, egui::Rect)],
|
||||
tt: &[(usize, egui::Rect)],
|
||||
top_c: usize,
|
||||
top_t: usize,
|
||||
t: f32,
|
||||
rect: egui::Rect,
|
||||
) -> Vec<(usize, egui::Rect)> {
|
||||
let find = |v: &[(usize, egui::Rect)], slot: usize| v.iter().find(|(s, _)| *s == slot).map(|(_, r)| *r);
|
||||
let lo = c.first().map(|(s, _)| *s).unwrap_or(0).min(tt.first().map(|(s, _)| *s).unwrap_or(0));
|
||||
let hi = c.last().map(|(s, _)| *s + 1).unwrap_or(0).max(tt.last().map(|(s, _)| *s + 1).unwrap_or(0));
|
||||
let mut out = Vec::new();
|
||||
for slot in lo..hi {
|
||||
let r = match (find(c, slot), find(tt, slot)) {
|
||||
(Some(rc), Some(rt)) => lerp_rect(rc, rt, t),
|
||||
(Some(rc), None) => lerp_rect(rc, collapsed(rect, slot < top_t), t), // exiting
|
||||
(None, Some(rt)) => lerp_rect(collapsed(rect, slot < top_c), rt, t), // entering
|
||||
(None, None) => continue,
|
||||
};
|
||||
out.push((slot, r));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
// --- rendering ---
|
||||
|
||||
/// The interaction (drag) target for a band's boundary is its full-width header bar. Bands are
|
||||
/// laid out in the area above a reserved bottom footer (the BottomEdge handle). Band 0's header is
|
||||
/// the TopEdge handle; band i's header (i≥1) is the divider above it; the footer is the BottomEdge.
|
||||
pub fn render(ui: &mut egui::Ui, rect: egui::Rect, rc: &mut RenderContext, state: &mut MobileState, pal: &Palette) {
|
||||
let top = state.window_top;
|
||||
let count = state.window_count;
|
||||
// Landscape caps the window at 2 panes; portrait allows 3.
|
||||
let max_panes = if rc.shared.is_portrait { 3 } else { 2 };
|
||||
let header_h = header_height(rc.shared.is_portrait);
|
||||
|
||||
// Reserve a footer bar at the very bottom for the BottomEdge handle.
|
||||
let footer_rect = egui::Rect::from_min_max(
|
||||
egui::pos2(rect.left(), rect.bottom() - FOOTER_H),
|
||||
rect.max,
|
||||
);
|
||||
let content_area = egui::Rect::from_min_max(
|
||||
rect.min,
|
||||
egui::pos2(rect.right(), footer_rect.top()),
|
||||
);
|
||||
let pane_h = content_area.height() / count as f32;
|
||||
let trigger = pane_h.min(TRIGGER_MAX);
|
||||
|
||||
let now = ui.input(|i| i.time);
|
||||
// Resting band rects from the committed weights — used for interaction so handles sit at their
|
||||
// final positions even while the visuals animate.
|
||||
let nw = nweights(&state.weights, count);
|
||||
let rest_bands = config_rects(top, count, content_area, &nw);
|
||||
|
||||
// The draw layout: a live drag, an in-flight layout ease, or the resting config.
|
||||
let draw_layout = if let Some(d) = state.drag {
|
||||
match d.handle {
|
||||
Handle::Divider(k) if k + 1 < count => {
|
||||
// Live resize: move divider k's boundary. In 3-pane the dividers are linked along the
|
||||
// preset path; in 2-pane it's a simple group resize.
|
||||
let off_frac = d.offset / content_area.height().max(1.0);
|
||||
let b0: f32 = nw[..=k].iter().sum();
|
||||
let b = b0 + off_frac;
|
||||
let w = if count == 3 {
|
||||
live_weights3(k, b)
|
||||
} else {
|
||||
let (bmin, bmax) = boundary_bounds(count, k);
|
||||
to_arr(&weights_for_boundary(&nw, k, b.clamp(bmin, bmax)))
|
||||
};
|
||||
config_rects(top, count, content_area, &nweights(&w, count))
|
||||
}
|
||||
Handle::BottomEdge if count == 1 && top + 1 < N => {
|
||||
// Continuous reveal: dragging the bottom edge up grows the pane below out of the
|
||||
// bottom, its divider tracking the finger across the FULL height (not just `trigger`),
|
||||
// so one drag sweeps the current pane → split → revealed-pane-fullscreen.
|
||||
let frac = (-d.offset / content_area.height().max(1.0)).clamp(0.0, 1.0);
|
||||
config_rects(top, 2, content_area, &nweights(&[1.0 - frac, frac, 0.0], 2))
|
||||
}
|
||||
Handle::TopEdge if count == 1 && top > 0 => {
|
||||
// Symmetric reveal of the pane above: dragging the top edge down grows it from the top.
|
||||
let frac = (d.offset / content_area.height().max(1.0)).clamp(0.0, 1.0);
|
||||
config_rects(top - 1, 2, content_area, &nweights(&[frac, 1.0 - frac, 0.0], 2))
|
||||
}
|
||||
Handle::BottomEdge if count == 2 && max_panes == 2 => {
|
||||
// 2-pane (landscape) → 1-pane reveal: dragging the bottom edge up first slides the
|
||||
// window down to reveal the pane below, then collapses onto it — the whole sweep
|
||||
// mapped over the FULL height so the dragged boundary reaches the top (rather than
|
||||
// stalling at the even split after the slide).
|
||||
let frac = (-d.offset / content_area.height().max(1.0)).clamp(0.0, 1.0);
|
||||
let even2 = nweights(&even_arr(2), 2);
|
||||
let even1 = nweights(&even_arr(1), 1);
|
||||
if top + 2 < N {
|
||||
if frac <= 0.5 {
|
||||
let from = config_rects(top, 2, content_area, &even2);
|
||||
let to = config_rects(top + 1, 2, content_area, &even2);
|
||||
interp_layout(&from, &to, top, top + 1, frac / 0.5, content_area)
|
||||
} else {
|
||||
let from = config_rects(top + 1, 2, content_area, &even2);
|
||||
let to = config_rects(top + 2, 1, content_area, &even1);
|
||||
interp_layout(&from, &to, top + 1, top + 2, (frac - 0.5) / 0.5, content_area)
|
||||
}
|
||||
} else {
|
||||
// No pane below → just collapse onto the bottom pane.
|
||||
let from = config_rects(top, 2, content_area, &even2);
|
||||
let to = config_rects(top + 1, 1, content_area, &even1);
|
||||
interp_layout(&from, &to, top, top + 1, frac, content_area)
|
||||
}
|
||||
}
|
||||
Handle::TopEdge if count == 2 && max_panes == 2 => {
|
||||
// Symmetric 2→1 reveal from the top: dragging the top edge down slides the window up
|
||||
// to reveal the pane above, then collapses onto it.
|
||||
let frac = (d.offset / content_area.height().max(1.0)).clamp(0.0, 1.0);
|
||||
let even2 = nweights(&even_arr(2), 2);
|
||||
let even1 = nweights(&even_arr(1), 1);
|
||||
if top > 0 {
|
||||
if frac <= 0.5 {
|
||||
let from = config_rects(top, 2, content_area, &even2);
|
||||
let to = config_rects(top - 1, 2, content_area, &even2);
|
||||
interp_layout(&from, &to, top, top - 1, frac / 0.5, content_area)
|
||||
} else {
|
||||
let from = config_rects(top - 1, 2, content_area, &even2);
|
||||
let to = config_rects(top - 1, 1, content_area, &even1);
|
||||
interp_layout(&from, &to, top - 1, top - 1, (frac - 0.5) / 0.5, content_area)
|
||||
}
|
||||
} else {
|
||||
// No pane above → collapse onto the top pane (drop the bottom).
|
||||
let from = config_rects(top, 2, content_area, &even2);
|
||||
let to = config_rects(top, 1, content_area, &even1);
|
||||
interp_layout(&from, &to, top, top, frac, content_area)
|
||||
}
|
||||
}
|
||||
_ => resolve(d.handle, d.offset, top, count, trigger, max_panes)
|
||||
.map(|(tt, tc, t)| {
|
||||
let target = config_rects(tt, tc, content_area, &nweights(&even_arr(tc), tc));
|
||||
interp_layout(&rest_bands, &target, top, tt, t, content_area)
|
||||
})
|
||||
.unwrap_or_else(|| rest_bands.clone()),
|
||||
}
|
||||
} else if let Some(a) = state.anim {
|
||||
let p = (((now - a.start) / SNAP_ANIM_SECS) as f32).clamp(0.0, 1.0);
|
||||
if p >= 1.0 {
|
||||
state.anim = None;
|
||||
rest_bands.clone()
|
||||
} else {
|
||||
ui.ctx().request_repaint();
|
||||
let e = ease_out(p);
|
||||
let from = config_rects(a.from_top, a.from_count, content_area, &nweights(&a.from_w, a.from_count));
|
||||
let to = config_rects(a.to_top, a.to_count, content_area, &nweights(&a.to_w, a.to_count));
|
||||
interp_layout(&from, &to, a.from_top, a.to_top, e, content_area)
|
||||
}
|
||||
} else {
|
||||
rest_bands.clone()
|
||||
};
|
||||
|
||||
// 1) Pane content, carving the header off the top of each band.
|
||||
for (slot, brect) in &draw_layout {
|
||||
let hh = header_h.min(brect.height());
|
||||
let content_rect = egui::Rect::from_min_max(
|
||||
egui::pos2(brect.left(), brect.top() + hh),
|
||||
brect.max,
|
||||
);
|
||||
if content_rect.height() > 1.0 {
|
||||
let sp = STACK[*slot];
|
||||
super::surface::render_surface_fullbleed(
|
||||
ui,
|
||||
content_rect,
|
||||
&slot_path(*slot),
|
||||
sp.pane_type(state.show_instruments),
|
||||
rc,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 2) Header visuals (animated positions). The fullscreen icon shows "restore" when a single
|
||||
// pane fills the stack.
|
||||
let fullscreen = draw_layout.len() == 1;
|
||||
for (slot, brect) in &draw_layout {
|
||||
if brect.height() < 6.0 {
|
||||
continue;
|
||||
}
|
||||
let hr = egui::Rect::from_min_max(
|
||||
brect.left_top(),
|
||||
egui::pos2(brect.right(), brect.top() + header_h.min(brect.height())),
|
||||
);
|
||||
draw_header(ui, hr, STACK[*slot], state.show_instruments, fullscreen, pal);
|
||||
}
|
||||
draw_footer(ui, footer_rect, top + count >= N, pal);
|
||||
|
||||
// 3) Interactions on the resting header/footer rects (added last → they win the press).
|
||||
handle_interactions(ui, &rest_bands, content_area, footer_rect, trigger, now, state, max_panes, header_h);
|
||||
}
|
||||
|
||||
fn draw_header(ui: &egui::Ui, hr: egui::Rect, sp: StackPane, show_instruments: bool, fullscreen: bool, pal: &Palette) {
|
||||
let p = ui.painter();
|
||||
// Rounded top corners so the header reads as a tab atop the pane; square at the bottom where
|
||||
// it meets the pane content.
|
||||
p.rect_filled(
|
||||
hr,
|
||||
egui::CornerRadius { nw: HEADER_RADIUS, ne: HEADER_RADIUS, sw: 0, se: 0 },
|
||||
pal.header,
|
||||
);
|
||||
p.hline(hr.x_range(), hr.bottom(), egui::Stroke::new(1.0, pal.line));
|
||||
let cy = hr.center().y;
|
||||
// Grip glyph on the left (Lucide).
|
||||
p.text(
|
||||
egui::pos2(hr.left() + 15.0, cy),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
icons::GRIP_HORIZONTAL,
|
||||
icons::font(16.0),
|
||||
pal.text_dim,
|
||||
);
|
||||
p.text(
|
||||
egui::pos2(hr.left() + 32.0, cy),
|
||||
egui::Align2::LEFT_CENTER,
|
||||
sp.label(show_instruments),
|
||||
egui::FontId::proportional(15.0),
|
||||
pal.text,
|
||||
);
|
||||
// Right-side buttons: fullscreen / restore rightmost, Node/Instrument toggle just left of it.
|
||||
p.text(
|
||||
egui::pos2(hr.right() - BTN_W * 0.5, cy),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
if fullscreen { icons::MINIMIZE } else { icons::MAXIMIZE },
|
||||
icons::font(17.0),
|
||||
pal.text_dim,
|
||||
);
|
||||
if sp == StackPane::NodeInstrument {
|
||||
p.text(
|
||||
egui::pos2(hr.right() - BTN_W * 1.5, cy),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
icons::ARROW_LEFT_RIGHT,
|
||||
icons::font(17.0),
|
||||
pal.accent,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_footer(ui: &egui::Ui, fr: egui::Rect, at_end: bool, pal: &Palette) {
|
||||
let p = ui.painter();
|
||||
p.rect_filled(fr, 0.0, pal.header);
|
||||
p.hline(fr.x_range(), fr.top(), egui::Stroke::new(1.0, pal.line));
|
||||
let col = if at_end { pal.line } else { pal.text_dim };
|
||||
let cy = fr.center().y;
|
||||
let galley = p.layout_no_wrap(
|
||||
"pull up for more".to_string(),
|
||||
egui::FontId::proportional(11.0),
|
||||
col,
|
||||
);
|
||||
let total_w = 22.0 + galley.size().x;
|
||||
let start_x = fr.center().x - total_w * 0.5;
|
||||
p.text(
|
||||
egui::pos2(start_x + 8.0, cy),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
icons::CHEVRONS_UP,
|
||||
icons::font(14.0),
|
||||
col,
|
||||
);
|
||||
let gy = cy - galley.size().y * 0.5;
|
||||
p.galley(egui::pos2(start_x + 22.0, gy), galley, col);
|
||||
}
|
||||
|
||||
fn handle_key(h: Handle) -> (usize, usize) {
|
||||
match h {
|
||||
Handle::TopEdge => (0, 0),
|
||||
Handle::Divider(k) => (1, k + 1),
|
||||
Handle::BottomEdge => (2, 0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Toggle a slot between filling the stack (count==1) and a 2-pane split with an adjacent pane.
|
||||
fn toggle_fullscreen(state: &mut MobileState, slot: usize, now: f64, max: usize) {
|
||||
if state.window_count == 1 && state.window_top == slot {
|
||||
// Restore: split with the pane below if possible, else above.
|
||||
if let Some((t, c)) = op_r5(slot, 1, max).or_else(|| op_r6(slot, 1, max)) {
|
||||
set_window(state, t, c, now);
|
||||
}
|
||||
} else {
|
||||
set_window(state, slot, 1, now);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_interactions(
|
||||
ui: &mut egui::Ui,
|
||||
rest_bands: &[(usize, egui::Rect)],
|
||||
content_area: egui::Rect,
|
||||
footer_rect: egui::Rect,
|
||||
trigger: f32,
|
||||
now: f64,
|
||||
state: &mut MobileState,
|
||||
max: usize,
|
||||
header_h: f32,
|
||||
) {
|
||||
// (handle, header_rect, slot) — slot is Some for band headers, None for the footer.
|
||||
let mut handles: Vec<(Handle, egui::Rect, Option<usize>)> = Vec::new();
|
||||
for (i, (slot, brect)) in rest_bands.iter().enumerate() {
|
||||
let hr = egui::Rect::from_min_max(
|
||||
brect.left_top(),
|
||||
egui::pos2(brect.right(), brect.top() + header_h.min(brect.height())),
|
||||
);
|
||||
let handle = if i == 0 { Handle::TopEdge } else { Handle::Divider(i - 1) };
|
||||
handles.push((handle, hr, Some(*slot)));
|
||||
}
|
||||
handles.push((Handle::BottomEdge, footer_rect, None));
|
||||
|
||||
for (handle, hrect, slot_opt) in handles {
|
||||
let id = ui.id().with(("mobile_stack_handle", handle_key(handle)));
|
||||
let resp = ui.interact(hrect, id, egui::Sense::click_and_drag());
|
||||
|
||||
// Right-side header buttons are interacted AFTER the header (so they're on top and win the
|
||||
// press there); the rest of the header drives the drag.
|
||||
if let Some(slot) = slot_opt {
|
||||
let fs = egui::Rect::from_min_max(
|
||||
egui::pos2(hrect.right() - BTN_W, hrect.top()),
|
||||
hrect.max,
|
||||
);
|
||||
let fsresp = ui.interact(fs, ui.id().with(("mobile_fs", slot)), egui::Sense::click());
|
||||
if fsresp.clicked() {
|
||||
toggle_fullscreen(state, slot, now, max);
|
||||
}
|
||||
if STACK[slot] == StackPane::NodeInstrument {
|
||||
let nt = egui::Rect::from_min_max(
|
||||
egui::pos2(hrect.right() - 2.0 * BTN_W, hrect.top()),
|
||||
egui::pos2(hrect.right() - BTN_W, hrect.bottom()),
|
||||
);
|
||||
let ntresp = ui.interact(nt, ui.id().with("mobile_node_toggle"), egui::Sense::click());
|
||||
if ntresp.clicked() {
|
||||
state.show_instruments = !state.show_instruments;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if resp.drag_started() {
|
||||
state.drag = Some(StackDrag { handle, offset: 0.0 });
|
||||
state.anim = None; // a fresh drag cancels any in-flight ease
|
||||
}
|
||||
if resp.dragged() {
|
||||
if let Some(d) = &mut state.drag {
|
||||
if d.handle == handle {
|
||||
d.offset += resp.drag_delta().y;
|
||||
}
|
||||
}
|
||||
}
|
||||
if resp.drag_stopped() {
|
||||
if let Some(d) = state.drag.take() {
|
||||
commit_drag(d, state, content_area, trigger, now, max);
|
||||
}
|
||||
}
|
||||
if resp.hovered() || state.drag.map(|d| d.handle == handle).unwrap_or(false) {
|
||||
ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeVertical);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn commit_drag(d: StackDrag, state: &mut MobileState, content_area: egui::Rect, trigger: f32, now: f64, max: usize) {
|
||||
let h = content_area.height().max(1.0);
|
||||
match d.handle {
|
||||
Handle::Divider(k) if k + 1 < state.window_count => {
|
||||
commit_divider(state, k, d.offset / h, now);
|
||||
}
|
||||
Handle::BottomEdge if state.window_count == 1 && state.window_top + 1 < N => {
|
||||
// Snap the continuous reveal: near the top → revealed pane fullscreen; barely moved →
|
||||
// back to the current pane; in between → a 2-pane split at the nearest preset.
|
||||
let top = state.window_top;
|
||||
let frac = (-d.offset / h).clamp(0.0, 1.0);
|
||||
let from_w = [1.0 - frac, frac, 0.0];
|
||||
if frac >= COLLAPSE_HI {
|
||||
begin_anim(state, top, 2, from_w, top + 1, 1, even_arr(1), 0.0, now);
|
||||
} else if frac <= COLLAPSE_LO {
|
||||
begin_anim(state, top, 2, from_w, top, 1, even_arr(1), 0.0, now);
|
||||
} else {
|
||||
begin_anim(state, top, 2, from_w, top, 2, nearest_preset(&from_w, 2), 0.0, now);
|
||||
}
|
||||
}
|
||||
Handle::TopEdge if state.window_count == 1 && state.window_top > 0 => {
|
||||
let top = state.window_top;
|
||||
let frac = (d.offset / h).clamp(0.0, 1.0);
|
||||
let from_w = [frac, 1.0 - frac, 0.0];
|
||||
if frac >= COLLAPSE_HI {
|
||||
begin_anim(state, top - 1, 2, from_w, top - 1, 1, even_arr(1), 0.0, now);
|
||||
} else if frac <= COLLAPSE_LO {
|
||||
begin_anim(state, top - 1, 2, from_w, top, 1, even_arr(1), 0.0, now);
|
||||
} else {
|
||||
begin_anim(state, top - 1, 2, from_w, top - 1, 2, nearest_preset(&from_w, 2), 0.0, now);
|
||||
}
|
||||
}
|
||||
Handle::BottomEdge if state.window_count == 2 && max == 2 => {
|
||||
// Snap the two-phase 2→1 reveal to match the preview's phases: past the midpoint (phase 2,
|
||||
// collapsing) → revealed pane fullscreen; a middling drag (phase 1, sliding) → the next
|
||||
// 2-pane split; barely moved → stay put.
|
||||
let top = state.window_top;
|
||||
let frac = (-d.offset / h).clamp(0.0, 1.0);
|
||||
let even2 = even_arr(2);
|
||||
if top + 2 < N {
|
||||
if frac >= 0.5 {
|
||||
let t = ((frac - 0.5) / 0.5).clamp(0.0, 1.0);
|
||||
begin_anim(state, top + 1, 2, even2, top + 2, 1, even_arr(1), t, now);
|
||||
} else if frac > COLLAPSE_LO {
|
||||
let t = (frac / 0.5).clamp(0.0, 1.0);
|
||||
begin_anim(state, top, 2, even2, top + 1, 2, even2, t, now);
|
||||
}
|
||||
// else: barely moved → stay on the current [top, top+1] split.
|
||||
} else if frac >= 0.5 {
|
||||
begin_anim(state, top, 2, even2, top + 1, 1, even_arr(1), frac, now);
|
||||
}
|
||||
}
|
||||
Handle::TopEdge if state.window_count == 2 && max == 2 => {
|
||||
// Symmetric snap for the top-edge 2→1 reveal.
|
||||
let top = state.window_top;
|
||||
let frac = (d.offset / h).clamp(0.0, 1.0);
|
||||
let even2 = even_arr(2);
|
||||
if top > 0 {
|
||||
if frac >= 0.5 {
|
||||
let t = ((frac - 0.5) / 0.5).clamp(0.0, 1.0);
|
||||
begin_anim(state, top - 1, 2, even2, top - 1, 1, even_arr(1), t, now);
|
||||
} else if frac > COLLAPSE_LO {
|
||||
let t = (frac / 0.5).clamp(0.0, 1.0);
|
||||
begin_anim(state, top, 2, even2, top - 1, 2, even2, t, now);
|
||||
}
|
||||
// else: barely moved → stay on the current split.
|
||||
} else if frac >= 0.5 {
|
||||
begin_anim(state, top, 2, even2, top, 1, even_arr(1), frac, now);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Edges (and degenerate dividers): membership transition. Animate from the current
|
||||
// config to the new one, continuing from where the drag's interp left off (~progress t).
|
||||
let top = state.window_top;
|
||||
let count = state.window_count;
|
||||
if let Some((tt, tc, t)) = resolve(d.handle, d.offset, top, count, trigger, max) {
|
||||
if t >= 0.5 {
|
||||
let from_w = to_arr(&nweights(&state.weights, count));
|
||||
begin_anim(state, top, count, from_w, tt, tc, even_arr(tc), t, now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply a divider release. 3-pane and 2-pane have different snap rules (see below). Either way the
|
||||
/// result is eased in from whatever was on screen at release.
|
||||
fn commit_divider(state: &mut MobileState, k: usize, offset_frac: f32, now: f64) {
|
||||
let top = state.window_top;
|
||||
let count = state.window_count;
|
||||
let nw = nweights(&state.weights, count);
|
||||
let b0: f32 = nw[..=k].iter().sum();
|
||||
let b = b0 + offset_frac; // unclamped boundary position of divider k
|
||||
|
||||
if count == 3 {
|
||||
// `from` = what's on screen at release.
|
||||
let from_w = live_weights3(k, b);
|
||||
// Sizes of the two extreme panes after this drag, to test the "grown past 66%" rule.
|
||||
let pane_top = from_w[0]; // grown when dragging the upper divider down
|
||||
let pane_bot = from_w[2]; // grown when dragging the lower divider up
|
||||
|
||||
if k == 0 && pane_top >= COLLAPSE_GROW {
|
||||
// Upper pane grown out → drop the bottom pane; the surviving {0,1} divider snaps to
|
||||
// where it was dropped.
|
||||
if let Some((tt, tc)) = op_r4(top, 3) {
|
||||
let to_w = collapse_2pane(from_w[0], from_w[1]);
|
||||
begin_anim(state, top, 3, from_w, tt, tc, to_w, 0.0, now);
|
||||
}
|
||||
} else if k == 1 && pane_bot >= COLLAPSE_GROW {
|
||||
// Bottom pane grown out → drop the top pane; surviving {1,2} divider snaps to drop.
|
||||
if let Some((tt, tc)) = op_r3(top, 3) {
|
||||
let to_w = collapse_2pane(from_w[1], from_w[2]);
|
||||
begin_anim(state, top, 3, from_w, tt, tc, to_w, 0.0, now);
|
||||
}
|
||||
} else if b <= COLLAPSE_LO {
|
||||
if let Some((tt, tc)) = op_r1(top, 3) {
|
||||
begin_anim(state, top, 3, from_w, tt, tc, even_arr(tc), 0.0, now);
|
||||
}
|
||||
} else if b >= COLLAPSE_HI {
|
||||
if let Some((tt, tc)) = op_r2(top, 3) {
|
||||
begin_anim(state, top, 3, from_w, tt, tc, even_arr(tc), 0.0, now);
|
||||
}
|
||||
} else {
|
||||
// Snap both dividers together: round the path parameter to the nearest preset.
|
||||
let (lo, hi) = path3_range(k);
|
||||
let s = s_from_boundary(k, b.clamp(lo, hi)).round().clamp(0.0, 2.0);
|
||||
let to_w = PRESETS_3[s as usize];
|
||||
begin_anim(state, top, 3, from_w, top, 3, to_w, 0.0, now);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 2-pane: group resize snapping to the nearest 2-pane preset, but releasing near an edge
|
||||
// collapses to a single fullscreen pane (the one that grew). It does NOT slide in the next pane —
|
||||
// that's the job of the top/bottom edge handles.
|
||||
let (bmin, bmax) = boundary_bounds(count, k);
|
||||
let from_w = to_arr(&weights_for_boundary(&nw, k, b.clamp(bmin, bmax)));
|
||||
if b <= COLLAPSE_LO {
|
||||
// Divider dragged to the top → the top pane is squeezed out; the bottom pane goes fullscreen.
|
||||
begin_anim(state, top, count, from_w, top + 1, 1, even_arr(1), 0.0, now);
|
||||
} else if b >= COLLAPSE_HI {
|
||||
// Divider dragged to the bottom → the bottom pane is squeezed out; the top pane goes fullscreen.
|
||||
begin_anim(state, top, count, from_w, top, 1, even_arr(1), 0.0, now);
|
||||
} else {
|
||||
let to_w = nearest_preset(&from_w, count);
|
||||
begin_anim(state, top, count, from_w, top, count, to_w, 0.0, now);
|
||||
}
|
||||
}
|
||||
|
||||
/// Given the two surviving panes' (unnormalized) sizes after a 3→2 collapse, snap to the nearest
|
||||
/// 2-pane preset by the first pane's proportion.
|
||||
fn collapse_2pane(a: f32, b: f32) -> [f32; 3] {
|
||||
let total = (a + b).max(1e-4);
|
||||
let p0 = a / total;
|
||||
nearest_preset(&[p0, 1.0 - p0, 0.0], 2)
|
||||
}
|
||||
|
||||
/// Switch to a new window config, easing from `from_*` to the new state over the snap duration.
|
||||
/// `start_p` back-dates the animation so it can continue an in-progress drag.
|
||||
fn begin_anim(
|
||||
state: &mut MobileState,
|
||||
from_top: usize,
|
||||
from_count: usize,
|
||||
from_w: [f32; 3],
|
||||
to_top: usize,
|
||||
to_count: usize,
|
||||
to_w: [f32; 3],
|
||||
start_p: f32,
|
||||
now: f64,
|
||||
) {
|
||||
state.window_top = to_top;
|
||||
state.window_count = to_count;
|
||||
state.weights = to_w;
|
||||
state.anim = Some(LayoutAnim {
|
||||
from_top,
|
||||
from_count,
|
||||
from_w,
|
||||
to_top,
|
||||
to_count,
|
||||
to_w,
|
||||
start: now - start_p as f64 * SNAP_ANIM_SECS,
|
||||
});
|
||||
}
|
||||
|
||||
/// Switch the visible window and reset to even pane sizing, easing the transition.
|
||||
fn set_window(state: &mut MobileState, top: usize, count: usize, now: f64) {
|
||||
let from_w = to_arr(&nweights(&state.weights, state.window_count));
|
||||
begin_anim(state, state.window_top, state.window_count, from_w, top, count, even_arr(count), 0.0, now);
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
//! Full-bleed hero surface rendering: maps a mobile surface to an existing pane and renders
|
||||
//! its content into a rect with no header/divider chrome. This is the de-chromed core of
|
||||
//! `render_pane` (main.rs) — see the get-or-create + `render_content` pattern there.
|
||||
|
||||
use eframe::egui;
|
||||
use lightningbeam_core::pane::PaneType;
|
||||
|
||||
use crate::panes::{NodePath, PaneInstance, PaneRenderer};
|
||||
use crate::RenderContext;
|
||||
|
||||
/// Render `pane_type` full-bleed into `rect`, reusing (or creating) the cached pane instance
|
||||
/// keyed by `path`. The pane clips its own drawing to `rect`, so no extra clipping is needed.
|
||||
pub fn render_surface_fullbleed(
|
||||
ui: &mut egui::Ui,
|
||||
rect: egui::Rect,
|
||||
path: &NodePath,
|
||||
pane_type: PaneType,
|
||||
rc: &mut RenderContext,
|
||||
) {
|
||||
// Get-or-create the instance for this slot (recreate if the type changed).
|
||||
let needs_new = rc
|
||||
.pane_instances
|
||||
.get(path)
|
||||
.map(|inst| inst.pane_type() != pane_type)
|
||||
.unwrap_or(true);
|
||||
if needs_new {
|
||||
rc.pane_instances.insert(path.clone(), PaneInstance::new(pane_type));
|
||||
}
|
||||
|
||||
if let Some(inst) = rc.pane_instances.get_mut(path) {
|
||||
inst.render_content(ui, rect, path, &mut rc.shared);
|
||||
}
|
||||
}
|
||||
@@ -1,271 +0,0 @@
|
||||
//! The mobile top bar: the project filename on the left, and on the right ⌕ (command palette) and
|
||||
//! ⋯ (overflow commands). Both open a modal sheet whose items dispatch `MenuAction`s (which
|
||||
//! `main.rs::handle_menu_action` already implements). Per the wireframe these are the
|
||||
//! "commands/destinations", as opposed to the omnibutton's "tools/objects".
|
||||
|
||||
use eframe::egui;
|
||||
|
||||
use super::{icons, MobileState, Palette};
|
||||
use crate::menu::{MenuAction, MenuDef, MenuItemDef};
|
||||
use crate::RenderContext;
|
||||
|
||||
const BTN: f32 = 40.0;
|
||||
|
||||
/// Curated overflow (⋯) commands.
|
||||
fn overflow_items() -> [(&'static str, MenuAction); 9] {
|
||||
[
|
||||
("Save", MenuAction::Save),
|
||||
("Save As…", MenuAction::SaveAs),
|
||||
("Open File…", MenuAction::OpenFile),
|
||||
("New file…", MenuAction::NewFile),
|
||||
("Import…", MenuAction::Import),
|
||||
("Export…", MenuAction::Export),
|
||||
("Undo", MenuAction::Undo),
|
||||
("Redo", MenuAction::Redo),
|
||||
("Preferences", MenuAction::Preferences),
|
||||
]
|
||||
}
|
||||
|
||||
/// Flatten the whole menu tree into (path-label, action) for the command palette.
|
||||
fn flatten(defs: &'static [MenuDef], prefix: &str, out: &mut Vec<(String, MenuAction)>) {
|
||||
for d in defs {
|
||||
match d {
|
||||
MenuDef::Item(item) => out.push((format!("{prefix}{}", item.label), item.action)),
|
||||
MenuDef::Submenu { label, children } => {
|
||||
flatten(children, &format!("{prefix}{label} › "), out);
|
||||
}
|
||||
MenuDef::Separator => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn all_commands() -> Vec<(String, MenuAction)> {
|
||||
let mut v = Vec::new();
|
||||
flatten(MenuItemDef::menu_structure(), "", &mut v);
|
||||
v
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
ui: &mut egui::Ui,
|
||||
bar: egui::Rect,
|
||||
full: egui::Rect,
|
||||
rc: &mut RenderContext,
|
||||
state: &mut MobileState,
|
||||
pal: &Palette,
|
||||
) {
|
||||
ui.painter().rect_filled(bar, 0.0, pal.header);
|
||||
ui.painter().hline(bar.x_range(), bar.bottom(), egui::Stroke::new(1.0, pal.line));
|
||||
|
||||
// Filename (or app name when unsaved).
|
||||
let name = rc
|
||||
.shared
|
||||
.container_path
|
||||
.as_ref()
|
||||
.and_then(|p| p.file_name())
|
||||
.map(|s| s.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| "Lightningbeam".to_string());
|
||||
ui.painter().text(
|
||||
egui::pos2(bar.left() + 14.0, bar.center().y),
|
||||
egui::Align2::LEFT_CENTER,
|
||||
name,
|
||||
egui::FontId::proportional(14.0),
|
||||
pal.text,
|
||||
);
|
||||
|
||||
// Right cluster: ⌕ (palette) then ⋯ (overflow).
|
||||
let overflow_rect = egui::Rect::from_min_size(egui::pos2(bar.right() - BTN, bar.top()), egui::vec2(BTN, bar.height()));
|
||||
let palette_rect = egui::Rect::from_min_size(egui::pos2(bar.right() - 2.0 * BTN, bar.top()), egui::vec2(BTN, bar.height()));
|
||||
|
||||
let sresp = ui.interact(palette_rect, ui.id().with("mobile_topbar_search"), egui::Sense::click());
|
||||
ui.painter().text(palette_rect.center(), egui::Align2::CENTER_CENTER, icons::SEARCH, icons::font(17.0),
|
||||
if sresp.hovered() || state.palette_open { pal.text } else { pal.text_dim });
|
||||
if sresp.clicked() {
|
||||
state.palette_open = !state.palette_open;
|
||||
state.overflow_open = false;
|
||||
state.palette_query.clear();
|
||||
}
|
||||
|
||||
let oresp = ui.interact(overflow_rect, ui.id().with("mobile_topbar_overflow"), egui::Sense::click());
|
||||
ui.painter().text(overflow_rect.center(), egui::Align2::CENTER_CENTER, icons::ELLIPSIS, icons::font(18.0),
|
||||
if oresp.hovered() || state.overflow_open { pal.text } else { pal.text_dim });
|
||||
if oresp.clicked() {
|
||||
state.overflow_open = !state.overflow_open;
|
||||
state.palette_open = false;
|
||||
}
|
||||
|
||||
if state.overflow_open {
|
||||
render_overflow(ui, full, rc, state, pal);
|
||||
} else if state.palette_open {
|
||||
render_palette(ui, full, rc, state, pal);
|
||||
}
|
||||
}
|
||||
|
||||
/// Landscape: the app bar folded into the middle of the top pane header — filename + ⌕ + ⋯ as a
|
||||
/// centered cluster (the pane's own grip/label sits to the left, its buttons to the right). No bar
|
||||
/// background or divider line; the header already drew them.
|
||||
pub fn render_inline(
|
||||
ui: &mut egui::Ui,
|
||||
header: egui::Rect,
|
||||
full: egui::Rect,
|
||||
rc: &mut RenderContext,
|
||||
state: &mut MobileState,
|
||||
pal: &Palette,
|
||||
) {
|
||||
let name = rc
|
||||
.shared
|
||||
.container_path
|
||||
.as_ref()
|
||||
.and_then(|p| p.file_name())
|
||||
.map(|s| s.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| "Lightningbeam".to_string());
|
||||
let btn = header.height().min(BTN);
|
||||
let gap = 6.0;
|
||||
// Reserve gutters so the cluster never overlaps the pane header's own grip/label (left) or its
|
||||
// fullscreen / node-toggle buttons (right).
|
||||
let left_gutter = 150.0;
|
||||
let right_gutter = 2.0 * BTN + 16.0;
|
||||
let span_left = header.left() + left_gutter;
|
||||
let span_right = (header.right() - right_gutter).max(span_left);
|
||||
let span_w = span_right - span_left;
|
||||
|
||||
// Filename elided to fit whatever's left after the two buttons.
|
||||
let max_name_w = (span_w - gap - 2.0 * btn).max(0.0);
|
||||
let galley = {
|
||||
let mut job =
|
||||
egui::text::LayoutJob::simple_singleline(name, egui::FontId::proportional(14.0), pal.text);
|
||||
job.wrap.max_width = max_name_w;
|
||||
job.wrap.max_rows = 1;
|
||||
job.wrap.break_anywhere = true;
|
||||
ui.painter().layout_job(job)
|
||||
};
|
||||
let name_w = galley.rect.width();
|
||||
let name_h = galley.rect.height();
|
||||
let cluster_w = name_w + gap + 2.0 * btn;
|
||||
// Center within the reserved span, then clamp so the right edge stays inside it.
|
||||
let x0 = (span_left + (span_w - cluster_w) / 2.0).clamp(span_left, (span_right - cluster_w).max(span_left));
|
||||
let cy = header.center().y;
|
||||
let bt = header.top() + (header.height() - btn) / 2.0;
|
||||
|
||||
ui.painter().galley(egui::pos2(x0, cy - name_h / 2.0), galley, pal.text);
|
||||
|
||||
let palette_rect = egui::Rect::from_min_size(egui::pos2(x0 + name_w + gap, bt), egui::vec2(btn, btn));
|
||||
let overflow_rect = egui::Rect::from_min_size(egui::pos2(palette_rect.right(), bt), egui::vec2(btn, btn));
|
||||
|
||||
let sresp = ui.interact(palette_rect, ui.id().with("mobile_topbar_search"), egui::Sense::click());
|
||||
ui.painter().text(palette_rect.center(), egui::Align2::CENTER_CENTER, icons::SEARCH, icons::font(17.0),
|
||||
if sresp.hovered() || state.palette_open { pal.text } else { pal.text_dim });
|
||||
if sresp.clicked() {
|
||||
state.palette_open = !state.palette_open;
|
||||
state.overflow_open = false;
|
||||
state.palette_query.clear();
|
||||
}
|
||||
|
||||
let oresp = ui.interact(overflow_rect, ui.id().with("mobile_topbar_overflow"), egui::Sense::click());
|
||||
ui.painter().text(overflow_rect.center(), egui::Align2::CENTER_CENTER, icons::ELLIPSIS, icons::font(18.0),
|
||||
if oresp.hovered() || state.overflow_open { pal.text } else { pal.text_dim });
|
||||
if oresp.clicked() {
|
||||
state.overflow_open = !state.overflow_open;
|
||||
state.palette_open = false;
|
||||
}
|
||||
|
||||
if state.overflow_open {
|
||||
render_overflow(ui, full, rc, state, pal);
|
||||
} else if state.palette_open {
|
||||
render_palette(ui, full, rc, state, pal);
|
||||
}
|
||||
}
|
||||
|
||||
/// Common modal scrim + panel. Returns (backdrop-tapped, panel inner rect).
|
||||
fn open_panel(ui: &mut egui::Ui, full: egui::Rect, id: &str, pal: &Palette) -> (bool, egui::Rect) {
|
||||
let scrim = ui.interact(full, ui.id().with(("mobile_topbar_scrim", id)), egui::Sense::click());
|
||||
ui.painter().rect_filled(full, 0.0, pal.scrim);
|
||||
let panel = egui::Rect::from_min_max(
|
||||
egui::pos2(full.left() + 16.0, full.top() + 44.0),
|
||||
egui::pos2(full.right() - 16.0, full.bottom() - 60.0),
|
||||
);
|
||||
ui.painter().rect_filled(panel, 14.0, pal.surface);
|
||||
ui.painter().rect_stroke(panel, 14.0, egui::Stroke::new(1.0, pal.line), egui::StrokeKind::Inside);
|
||||
(scrim.clicked(), panel)
|
||||
}
|
||||
|
||||
fn command_button(ui: &mut egui::Ui, r: egui::Rect, label: &str, key: (&str, usize), pal: &Palette) -> bool {
|
||||
let resp = ui.interact(r, ui.id().with(("mobile_cmd", key.0, key.1)), egui::Sense::click());
|
||||
ui.painter().rect_filled(r, 8.0, if resp.hovered() { pal.surface_alt } else { pal.surface });
|
||||
ui.painter().hline(r.x_range(), r.bottom(), egui::Stroke::new(1.0, pal.line));
|
||||
ui.painter().text(
|
||||
egui::pos2(r.left() + 14.0, r.center().y),
|
||||
egui::Align2::LEFT_CENTER,
|
||||
label,
|
||||
egui::FontId::proportional(13.0),
|
||||
pal.text,
|
||||
);
|
||||
resp.clicked()
|
||||
}
|
||||
|
||||
fn render_overflow(ui: &mut egui::Ui, full: egui::Rect, rc: &mut RenderContext, state: &mut MobileState, pal: &Palette) {
|
||||
let (backdrop, panel) = open_panel(ui, full, "overflow", pal);
|
||||
let mut close = backdrop;
|
||||
let items = overflow_items();
|
||||
let row_h = 44.0;
|
||||
let inner = panel.shrink(8.0);
|
||||
for (i, (label, action)) in items.iter().enumerate() {
|
||||
let r = egui::Rect::from_min_size(
|
||||
egui::pos2(inner.left(), inner.top() + i as f32 * row_h),
|
||||
egui::vec2(inner.width(), row_h),
|
||||
);
|
||||
if r.bottom() > inner.bottom() {
|
||||
break;
|
||||
}
|
||||
if command_button(ui, r, label, ("of", i), pal) {
|
||||
rc.shared.pending_menu_actions.push(*action);
|
||||
close = true;
|
||||
}
|
||||
}
|
||||
if close {
|
||||
state.overflow_open = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn render_palette(ui: &mut egui::Ui, full: egui::Rect, rc: &mut RenderContext, state: &mut MobileState, pal: &Palette) {
|
||||
let (backdrop, panel) = open_panel(ui, full, "palette", pal);
|
||||
let mut close = backdrop;
|
||||
let inner = panel.shrink(8.0);
|
||||
|
||||
// Search field (real egui widget).
|
||||
let field = egui::Rect::from_min_size(inner.min, egui::vec2(inner.width(), 30.0));
|
||||
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(field).layout(egui::Layout::left_to_right(egui::Align::Center)));
|
||||
let te = child.add(
|
||||
egui::TextEdit::singleline(&mut state.palette_query)
|
||||
.hint_text("Search commands…")
|
||||
.desired_width(inner.width()),
|
||||
);
|
||||
// Focus the field only when it isn't already focused — re-requesting every frame would stomp
|
||||
// focus and prevent anything else in the panel from taking it.
|
||||
if !te.has_focus() {
|
||||
te.request_focus();
|
||||
}
|
||||
|
||||
// Filtered list.
|
||||
let q = state.palette_query.to_lowercase();
|
||||
let cmds = all_commands();
|
||||
let row_h = 38.0;
|
||||
let list_top = inner.top() + 38.0;
|
||||
let mut y = list_top;
|
||||
for (i, (label, action)) in cmds.iter().enumerate() {
|
||||
if !q.is_empty() && !label.to_lowercase().contains(&q) {
|
||||
continue;
|
||||
}
|
||||
let r = egui::Rect::from_min_size(egui::pos2(inner.left(), y), egui::vec2(inner.width(), row_h));
|
||||
if r.bottom() > inner.bottom() {
|
||||
break;
|
||||
}
|
||||
if command_button(ui, r, label, ("pal", i), pal) {
|
||||
rc.shared.pending_menu_actions.push(*action);
|
||||
close = true;
|
||||
}
|
||||
y += row_h;
|
||||
}
|
||||
if close {
|
||||
state.palette_open = false;
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
//! The transport "floor": the always-present bottom bar with play/pause, the timecode, and a
|
||||
//! project scrub. Wired to the audio controller exactly like `TimelinePane`'s header.
|
||||
|
||||
use eframe::egui;
|
||||
use daw_backend::Seconds;
|
||||
|
||||
use super::{icons, Palette};
|
||||
use crate::panes::SharedPaneState;
|
||||
|
||||
pub fn render(ui: &mut egui::Ui, rect: egui::Rect, shared: &mut SharedPaneState, pal: &Palette) {
|
||||
let painter = ui.painter_at(rect);
|
||||
painter.rect_filled(rect, 0.0, pal.surface);
|
||||
painter.hline(rect.x_range(), rect.top(), egui::Stroke::new(1.0, pal.line));
|
||||
|
||||
let cy = rect.center().y;
|
||||
|
||||
// --- Play / pause button (circle on the left) ---
|
||||
let btn_r = 18.0;
|
||||
let btn_center = egui::pos2(rect.left() + 20.0 + btn_r, cy);
|
||||
let btn_rect = egui::Rect::from_center_size(btn_center, egui::vec2(btn_r * 2.0, btn_r * 2.0));
|
||||
let btn_resp = ui.interact(btn_rect, ui.id().with("mobile_transport_play"), egui::Sense::click());
|
||||
painter.circle_filled(btn_center, btn_r, pal.accent);
|
||||
let glyph = if *shared.is_playing { icons::PAUSE } else { icons::PLAY };
|
||||
painter.text(
|
||||
btn_center,
|
||||
egui::Align2::CENTER_CENTER,
|
||||
glyph,
|
||||
icons::font(16.0),
|
||||
pal.on_accent,
|
||||
);
|
||||
if btn_resp.clicked() {
|
||||
*shared.is_playing = !*shared.is_playing;
|
||||
if let Some(controller_arc) = shared.audio_controller {
|
||||
let mut controller = controller_arc.lock().unwrap();
|
||||
if *shared.is_playing {
|
||||
controller.seek(Seconds(*shared.playback_time));
|
||||
controller.play();
|
||||
} else {
|
||||
controller.pause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Playhead readout: format follows the project's TimelineMode (set at creation by type). ---
|
||||
let t = *shared.playback_time;
|
||||
let tc = {
|
||||
use lightningbeam_core::document::TimelineMode;
|
||||
let doc = shared.action_executor.document();
|
||||
match doc.timeline_mode {
|
||||
TimelineMode::Measures => {
|
||||
let pos = lightningbeam_core::beat_time::time_to_measure(t, doc.tempo_map(), &doc.time_signature);
|
||||
format!("{}.{}.{:02}", pos.measure, pos.beat, pos.tick / 10)
|
||||
}
|
||||
TimelineMode::Frames => format_timecode(t, doc.framerate.max(1.0)),
|
||||
TimelineMode::Seconds => {
|
||||
let total = t.max(0.0);
|
||||
let m = (total / 60.0).floor() as u32;
|
||||
format!("{:02}:{:06.3}", m, total % 60.0)
|
||||
}
|
||||
}
|
||||
};
|
||||
let tc_left = btn_rect.right() + 12.0;
|
||||
painter.text(
|
||||
egui::pos2(tc_left, cy),
|
||||
egui::Align2::LEFT_CENTER,
|
||||
&tc,
|
||||
egui::FontId::monospace(13.0),
|
||||
pal.text,
|
||||
);
|
||||
let tc_width = 78.0;
|
||||
|
||||
// --- Project scrub (fills the remaining width) ---
|
||||
let duration = shared.action_executor.document().duration.max(1.0);
|
||||
let scrub_left = tc_left + tc_width;
|
||||
let scrub_rect = egui::Rect::from_min_max(
|
||||
egui::pos2(scrub_left, cy - 3.0),
|
||||
egui::pos2(rect.right() - 14.0, cy + 3.0),
|
||||
);
|
||||
let scrub_resp = ui.interact(
|
||||
scrub_rect.expand2(egui::vec2(0.0, 12.0)),
|
||||
ui.id().with("mobile_transport_scrub"),
|
||||
egui::Sense::click_and_drag(),
|
||||
);
|
||||
painter.rect_filled(scrub_rect, 3.0, pal.line);
|
||||
let frac = (*shared.playback_time / duration).clamp(0.0, 1.0) as f32;
|
||||
let filled = egui::Rect::from_min_max(
|
||||
scrub_rect.min,
|
||||
egui::pos2(scrub_rect.left() + scrub_rect.width() * frac, scrub_rect.bottom()),
|
||||
);
|
||||
painter.rect_filled(filled, 3.0, pal.accent.gamma_multiply(0.5));
|
||||
let head_x = scrub_rect.left() + scrub_rect.width() * frac;
|
||||
painter.vline(
|
||||
head_x,
|
||||
(scrub_rect.top() - 4.0)..=(scrub_rect.bottom() + 4.0),
|
||||
egui::Stroke::new(2.0, pal.accent),
|
||||
);
|
||||
|
||||
if (scrub_resp.dragged() || scrub_resp.clicked()) && scrub_rect.width() > 0.0 {
|
||||
if let Some(pos) = scrub_resp.interact_pointer_pos() {
|
||||
let f = ((pos.x - scrub_rect.left()) / scrub_rect.width()).clamp(0.0, 1.0) as f64;
|
||||
let new_time = f * duration;
|
||||
*shared.playback_time = new_time;
|
||||
if let Some(controller_arc) = shared.audio_controller {
|
||||
let mut controller = controller_arc.lock().unwrap();
|
||||
controller.seek(Seconds(new_time));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn format_timecode(seconds: f64, fps: f64) -> String {
|
||||
let total = seconds.max(0.0);
|
||||
let minutes = (total / 60.0).floor() as u32;
|
||||
let secs = (total % 60.0).floor() as u32;
|
||||
let frames = ((total - total.floor()) * fps).floor() as u32;
|
||||
format!("{:02}:{:02}:{:02}", minutes, secs, frames)
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
//! - Image Assets (static images)
|
||||
|
||||
use eframe::egui;
|
||||
use lightningbeam_core::clip::{ResolvedContent, VectorClip};
|
||||
use lightningbeam_core::clip::{AudioClipType, VectorClip};
|
||||
use lightningbeam_core::document::Document;
|
||||
use lightningbeam_core::layer::AnyLayer;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
@@ -918,11 +918,11 @@ impl AssetLibraryPane {
|
||||
continue;
|
||||
}
|
||||
|
||||
let (extra_info, drag_clip_type) = match &clip.resolve() {
|
||||
ResolvedContent::Audio { .. } => ("Sampled".to_string(), DragClipType::AudioSampled),
|
||||
ResolvedContent::Midi { .. } => ("MIDI".to_string(), DragClipType::AudioMidi),
|
||||
ResolvedContent::Recording => {
|
||||
// Skip recording-in-progress clips (and empty take folders) from asset library
|
||||
let (extra_info, drag_clip_type) = match &clip.clip_type {
|
||||
AudioClipType::Sampled { .. } => ("Sampled".to_string(), DragClipType::AudioSampled),
|
||||
AudioClipType::Midi { .. } => ("MIDI".to_string(), DragClipType::AudioMidi),
|
||||
AudioClipType::Recording => {
|
||||
// Skip recording-in-progress clips from asset library
|
||||
continue;
|
||||
}
|
||||
};
|
||||
@@ -932,7 +932,7 @@ impl AssetLibraryPane {
|
||||
name: clip.name.clone(),
|
||||
category: AssetCategory::Audio,
|
||||
drag_clip_type,
|
||||
duration: clip.content_duration().native(),
|
||||
duration: clip.duration,
|
||||
dimensions: None,
|
||||
extra_info,
|
||||
is_builtin: false,
|
||||
@@ -1118,15 +1118,15 @@ impl AssetLibraryPane {
|
||||
|
||||
for (id, clip) in &document.audio_clips {
|
||||
if !linked_audio_ids.contains(id) && clip.folder_id == current_folder {
|
||||
let (extra_info, drag_clip_type) = match &clip.resolve() {
|
||||
ResolvedContent::Audio { .. } => {
|
||||
let (extra_info, drag_clip_type) = match &clip.clip_type {
|
||||
AudioClipType::Sampled { .. } => {
|
||||
("Sampled".to_string(), DragClipType::AudioSampled)
|
||||
}
|
||||
ResolvedContent::Midi { .. } => {
|
||||
AudioClipType::Midi { .. } => {
|
||||
("MIDI".to_string(), DragClipType::AudioMidi)
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
// Skip recording-in-progress clips (and empty take folders)
|
||||
AudioClipType::Recording => {
|
||||
// Skip recording-in-progress clips
|
||||
continue;
|
||||
}
|
||||
};
|
||||
@@ -1136,7 +1136,7 @@ impl AssetLibraryPane {
|
||||
name: clip.name.clone(),
|
||||
category: AssetCategory::Audio,
|
||||
drag_clip_type,
|
||||
duration: clip.content_duration().native(),
|
||||
duration: clip.duration,
|
||||
dimensions: None,
|
||||
extra_info,
|
||||
is_builtin: false,
|
||||
@@ -1292,9 +1292,6 @@ impl AssetLibraryPane {
|
||||
lightningbeam_core::layer::AnyLayer::Raster(_) => {
|
||||
// Raster layers don't have their own clip instances
|
||||
}
|
||||
lightningbeam_core::layer::AnyLayer::Text(_) => {
|
||||
// Text layers don't have their own clip instances
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
@@ -1765,7 +1762,7 @@ impl AssetLibraryPane {
|
||||
let prefetched_waveform: Option<Vec<(f32, f32)>> =
|
||||
if asset_category == AssetCategory::Audio && !self.thumbnail_cache.has(&asset_id) {
|
||||
if let Some(clip) = document.audio_clips.get(&asset_id) {
|
||||
if let Some(audio_pool_index) = clip.audio_pool_index().as_ref() {
|
||||
if let AudioClipType::Sampled { audio_pool_index } = &clip.clip_type {
|
||||
shared.raw_audio_cache.get(audio_pool_index)
|
||||
.map(|raw| peaks_from_raw_audio(raw, THUMBNAIL_SIZE as usize))
|
||||
} else {
|
||||
@@ -1790,8 +1787,8 @@ impl AssetLibraryPane {
|
||||
AssetCategory::Audio => {
|
||||
if let Some(clip) = document.audio_clips.get(&asset_id) {
|
||||
let bg_color = egui::Color32::from_rgba_unmultiplied(40, 40, 40, 200);
|
||||
match &clip.resolve() {
|
||||
ResolvedContent::Audio { .. } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Sampled { .. } => {
|
||||
let wave_color = egui::Color32::from_rgb(100, 200, 100);
|
||||
if let Some(ref peaks) = prefetched_waveform {
|
||||
Some(generate_waveform_thumbnail(peaks, bg_color, wave_color))
|
||||
@@ -1799,15 +1796,15 @@ impl AssetLibraryPane {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
let note_color = egui::Color32::from_rgb(100, 200, 100);
|
||||
if let Some(events) = shared.midi_event_cache.get(midi_clip_id) {
|
||||
Some(generate_midi_thumbnail(events, clip.content_duration().native(), bg_color, note_color))
|
||||
Some(generate_midi_thumbnail(events, clip.duration, bg_color, note_color))
|
||||
} else {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
// Recording in progress - show placeholder
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
@@ -2344,8 +2341,8 @@ impl AssetLibraryPane {
|
||||
AssetCategory::Audio => {
|
||||
if let Some(clip) = document.audio_clips.get(&asset_id) {
|
||||
let bg_color = egui::Color32::from_rgba_unmultiplied(40, 40, 40, 200);
|
||||
match &clip.resolve() {
|
||||
ResolvedContent::Audio { audio_pool_index } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
let wave_color = egui::Color32::from_rgb(100, 200, 100);
|
||||
let waveform: Option<Vec<(f32, f32)>> = shared.raw_audio_cache.get(audio_pool_index)
|
||||
.map(|raw| peaks_from_raw_audio(raw, THUMBNAIL_SIZE as usize));
|
||||
@@ -2355,15 +2352,15 @@ impl AssetLibraryPane {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
let note_color = egui::Color32::from_rgb(100, 200, 100);
|
||||
if let Some(events) = shared.midi_event_cache.get(midi_clip_id) {
|
||||
Some(generate_midi_thumbnail(events, clip.content_duration().native(), bg_color, note_color))
|
||||
Some(generate_midi_thumbnail(events, clip.duration, bg_color, note_color))
|
||||
} else {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
@@ -2481,8 +2478,8 @@ impl AssetLibraryPane {
|
||||
AssetCategory::Audio => {
|
||||
if let Some(clip) = document.audio_clips.get(&asset_id) {
|
||||
let bg_color = egui::Color32::from_rgba_unmultiplied(40, 40, 40, 200);
|
||||
match &clip.resolve() {
|
||||
ResolvedContent::Audio { audio_pool_index } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
let wave_color = egui::Color32::from_rgb(100, 200, 100);
|
||||
let waveform: Option<Vec<(f32, f32)>> = shared.raw_audio_cache.get(audio_pool_index)
|
||||
.map(|raw| peaks_from_raw_audio(raw, THUMBNAIL_SIZE as usize));
|
||||
@@ -2492,15 +2489,15 @@ impl AssetLibraryPane {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
let note_color = egui::Color32::from_rgb(100, 200, 100);
|
||||
if let Some(events) = shared.midi_event_cache.get(midi_clip_id) {
|
||||
Some(generate_midi_thumbnail(events, clip.content_duration().native(), bg_color, note_color))
|
||||
Some(generate_midi_thumbnail(events, clip.duration, bg_color, note_color))
|
||||
} else {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
@@ -2802,7 +2799,7 @@ impl AssetLibraryPane {
|
||||
let prefetched_waveform: Option<Vec<(f32, f32)>> =
|
||||
if asset_category == AssetCategory::Audio && !self.thumbnail_cache.has(&asset_id) {
|
||||
if let Some(clip) = document.audio_clips.get(&asset_id) {
|
||||
if let Some(audio_pool_index) = clip.audio_pool_index().as_ref() {
|
||||
if let AudioClipType::Sampled { audio_pool_index } = &clip.clip_type {
|
||||
let waveform: Option<Vec<(f32, f32)>> = shared.raw_audio_cache.get(audio_pool_index)
|
||||
.map(|raw| peaks_from_raw_audio(raw, THUMBNAIL_SIZE as usize));
|
||||
if waveform.is_some() {
|
||||
@@ -2842,8 +2839,8 @@ impl AssetLibraryPane {
|
||||
// Check if it's sampled or MIDI
|
||||
if let Some(clip) = document.audio_clips.get(&asset_id) {
|
||||
let bg_color = egui::Color32::from_rgba_unmultiplied(40, 40, 40, 200);
|
||||
match &clip.resolve() {
|
||||
ResolvedContent::Audio { .. } => {
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Sampled { .. } => {
|
||||
let wave_color = egui::Color32::from_rgb(100, 200, 100);
|
||||
if let Some(ref peaks) = prefetched_waveform {
|
||||
println!("✅ Generating waveform thumbnail with {} peaks for asset {}", peaks.len(), asset_id);
|
||||
@@ -2853,17 +2850,17 @@ impl AssetLibraryPane {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
ResolvedContent::Midi { midi_clip_id } => {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
let bg_color = egui::Color32::from_rgba_unmultiplied(40, 40, 40, 200);
|
||||
let note_color = egui::Color32::from_rgb(100, 200, 100);
|
||||
|
||||
if let Some(events) = shared.midi_event_cache.get(midi_clip_id) {
|
||||
Some(generate_midi_thumbnail(events, clip.content_duration().native(), bg_color, note_color))
|
||||
Some(generate_midi_thumbnail(events, clip.duration, bg_color, note_color))
|
||||
} else {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
ResolvedContent::Recording => {
|
||||
AudioClipType::Recording => {
|
||||
Some(generate_placeholder_thumbnail(AssetCategory::Audio, 200))
|
||||
}
|
||||
}
|
||||
@@ -3187,7 +3184,7 @@ impl PaneRenderer for AssetLibraryPane {
|
||||
println!("🎨 [ASSET_LIB] Checking for thumbnails to invalidate (pools: {:?})", shared.audio_pools_with_new_waveforms);
|
||||
let mut invalidated_any = false;
|
||||
for (asset_id, clip) in &document_arc.audio_clips {
|
||||
if let Some(audio_pool_index) = clip.audio_pool_index().as_ref() {
|
||||
if let lightningbeam_core::clip::AudioClipType::Sampled { audio_pool_index } = &clip.clip_type {
|
||||
if shared.audio_pools_with_new_waveforms.contains(audio_pool_index) {
|
||||
println!("❌ [ASSET_LIB] Invalidating thumbnail for asset {} (pool {})", asset_id, audio_pool_index);
|
||||
self.thumbnail_cache.invalidate(asset_id);
|
||||
|
||||
@@ -160,27 +160,27 @@ pub fn gradient_stop_editor(
|
||||
delete_idx = Some(i);
|
||||
}
|
||||
|
||||
// Color picker popup — opens on click, closes on a press outside it.
|
||||
let stop_color = gradient.stops[i].color;
|
||||
let mut c32 = Color32::from_rgba_unmultiplied(
|
||||
stop_color.r, stop_color.g, stop_color.b, stop_color.a,
|
||||
);
|
||||
if crate::widgets::color_swatch::color_picker_popup(
|
||||
ui,
|
||||
resp.id.with("stop_color_popup"),
|
||||
&resp,
|
||||
&mut c32,
|
||||
resp.rect,
|
||||
) {
|
||||
// Color32 stores premultiplied RGB; unmultiply before storing as straight-alpha
|
||||
// ShapeColor to avoid darkening on round-trip.
|
||||
let [pr, pg, pb, a] = c32.to_array();
|
||||
let unpm = |c: u8| -> u8 {
|
||||
if a == 0 { 0 } else { ((c as u32 * 255 + a as u32 / 2) / a as u32).min(255) as u8 }
|
||||
};
|
||||
gradient.stops[i].color = ShapeColor::rgba(unpm(pr), unpm(pg), unpm(pb), a);
|
||||
changed = true;
|
||||
}
|
||||
// Color picker popup — opens on click, closes on click-outside.
|
||||
egui::containers::Popup::from_toggle_button_response(&resp)
|
||||
.show(|ui| {
|
||||
ui.spacing_mut().slider_width = 200.0;
|
||||
let stop = &mut gradient.stops[i];
|
||||
let mut c32 = Color32::from_rgba_unmultiplied(
|
||||
stop.color.r, stop.color.g, stop.color.b, stop.color.a,
|
||||
);
|
||||
if egui::color_picker::color_picker_color32(
|
||||
ui, &mut c32, egui::color_picker::Alpha::OnlyBlend,
|
||||
) {
|
||||
// Color32 stores premultiplied RGB; unmultiply before storing
|
||||
// as straight-alpha ShapeColor to avoid darkening on round-trip.
|
||||
let [pr, pg, pb, a] = c32.to_array();
|
||||
let unpm = |c: u8| -> u8 {
|
||||
if a == 0 { 0 } else { ((c as u32 * 255 + a as u32 / 2) / a as u32).min(255) as u8 }
|
||||
};
|
||||
stop.color = ShapeColor::rgba(unpm(pr), unpm(pg), unpm(pb), a);
|
||||
changed = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Apply drag to whichever stop selected_stop points at.
|
||||
|
||||
@@ -28,9 +28,14 @@ pub struct InfopanelPane {
|
||||
tool_section_open: bool,
|
||||
/// Whether the shape properties section is expanded
|
||||
shape_section_open: bool,
|
||||
/// Whether each tool's brush picker is expanded. The *selected* preset lives on the tool's
|
||||
/// `BrushSlot`, since it's part of the document-independent tool state, not panel chrome.
|
||||
brush_picker_expanded: std::collections::HashMap<crate::tools::BrushKind, bool>,
|
||||
/// Index of the selected paint brush preset (None = custom / unset)
|
||||
selected_brush_preset: Option<usize>,
|
||||
/// Whether the paint brush picker is expanded
|
||||
brush_picker_expanded: bool,
|
||||
/// Index of the selected eraser brush preset
|
||||
selected_eraser_preset: Option<usize>,
|
||||
/// Whether the eraser brush picker is expanded
|
||||
eraser_picker_expanded: bool,
|
||||
/// Cached preview textures, one per preset (populated lazily).
|
||||
brush_preview_textures: Vec<egui::TextureHandle>,
|
||||
/// Selected stop index for gradient editor in shape section.
|
||||
@@ -39,55 +44,28 @@ pub struct InfopanelPane {
|
||||
selected_tool_gradient_stop: Option<usize>,
|
||||
/// FPS value captured when a drag/focus-in starts (for single-undo-action on commit)
|
||||
fps_drag_start: Option<f64>,
|
||||
/// In-progress layer-name edit buffer, keyed by the layer being edited.
|
||||
layer_name_edit: Option<(Uuid, String)>,
|
||||
/// Layer opacity/volume captured when a slider drag starts (single-undo on commit).
|
||||
layer_opacity_drag_start: Option<f64>,
|
||||
layer_volume_drag_start: Option<f64>,
|
||||
/// Resize mode for the active raster layer's "to document size" action (scale vs canvas).
|
||||
raster_resize_mode: lightningbeam_core::raster_layer::RasterResizeMode,
|
||||
/// Font families already registered into egui for the family-picker previews
|
||||
/// (rendered each entry in its own font), so we only load each once.
|
||||
registered_preview_fonts: std::collections::HashSet<String>,
|
||||
}
|
||||
|
||||
impl InfopanelPane {
|
||||
pub fn new() -> Self {
|
||||
// Kick off background loading of the font-picker fonts at startup so the dropdown
|
||||
// is ready without a hitch by the time the user opens it.
|
||||
lightningbeam_core::fonts::start_preload();
|
||||
let presets = bundled_brushes();
|
||||
let default_eraser_idx = presets.iter().position(|p| p.name == "Brush");
|
||||
Self {
|
||||
tool_section_open: true,
|
||||
shape_section_open: true,
|
||||
brush_picker_expanded: std::collections::HashMap::new(),
|
||||
selected_brush_preset: None,
|
||||
brush_picker_expanded: false,
|
||||
selected_eraser_preset: default_eraser_idx,
|
||||
eraser_picker_expanded: false,
|
||||
brush_preview_textures: Vec::new(),
|
||||
selected_shape_gradient_stop: None,
|
||||
selected_tool_gradient_stop: None,
|
||||
fps_drag_start: None,
|
||||
layer_name_edit: None,
|
||||
layer_opacity_drag_start: None,
|
||||
layer_volume_drag_start: None,
|
||||
raster_resize_mode: lightningbeam_core::raster_layer::RasterResizeMode::Scale,
|
||||
registered_preview_fonts: std::collections::HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Register pre-loaded `bytes` for `family` into egui under `FontFamily::Name(family)`
|
||||
/// so a label can be rendered in that font. Cheap (no font IO); the add is idempotent
|
||||
/// and batched into one atlas rebuild at frame end, so the preview appears next frame.
|
||||
fn register_font_bytes(&mut self, ctx: &egui::Context, family: &str, bytes: Vec<u8>) {
|
||||
if family.is_empty() || !self.registered_preview_fonts.insert(family.to_string()) {
|
||||
return;
|
||||
}
|
||||
ctx.add_font(egui::epaint::text::FontInsert::new(
|
||||
family,
|
||||
egui::FontData::from_owned(bytes),
|
||||
vec![egui::epaint::text::InsertFontFamily {
|
||||
family: egui::FontFamily::Name(family.into()),
|
||||
priority: egui::epaint::text::FontPriority::Highest,
|
||||
}],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// Aggregated info about the current DCEL selection
|
||||
@@ -241,7 +219,7 @@ impl InfopanelPane {
|
||||
|| is_raster_select || is_raster_shape || matches!(
|
||||
tool,
|
||||
Tool::PaintBucket | Tool::RegionSelect | Tool::MagicWand | Tool::QuickSelect
|
||||
| Tool::Warp | Tool::Liquify | Tool::Gradient | Tool::Eyedropper
|
||||
| Tool::Warp | Tool::Liquify | Tool::Gradient
|
||||
);
|
||||
|
||||
if !has_options {
|
||||
@@ -275,11 +253,12 @@ impl InfopanelPane {
|
||||
return;
|
||||
}
|
||||
|
||||
// Raster paint tool: tool-unique controls, then the shared brush controls
|
||||
// (library, color, size/strength/hardness/spacing) that every brush tool gets.
|
||||
// Raster paint tool: delegate to per-tool impl.
|
||||
if let Some(def) = raster_tool_def {
|
||||
def.render_ui(ui, shared.raster_settings);
|
||||
self.render_raster_tool_options(ui, shared, def);
|
||||
if def.show_brush_preset_picker() {
|
||||
self.render_raster_tool_options(ui, shared, def.is_eraser());
|
||||
}
|
||||
}
|
||||
|
||||
match tool {
|
||||
@@ -333,14 +312,7 @@ impl InfopanelPane {
|
||||
ui.checkbox(shared.fill_enabled, "Fill Shape");
|
||||
}
|
||||
|
||||
Tool::Eyedropper => {
|
||||
// Which swatch the sampled color lands in.
|
||||
render_color_slot_row(ui, &mut shared.raster_settings.eyedropper_use_fg);
|
||||
}
|
||||
|
||||
Tool::PaintBucket => {
|
||||
render_color_slot_row(ui, &mut shared.raster_settings.fill_use_fg);
|
||||
|
||||
if active_is_raster {
|
||||
use crate::tools::FillThresholdMode;
|
||||
ui.horizontal(|ui| {
|
||||
@@ -599,66 +571,68 @@ impl InfopanelPane {
|
||||
});
|
||||
}
|
||||
|
||||
/// Render the shared controls every dab-painting tool has: the brush library, the FG/BG
|
||||
/// color choice, and size/strength/hardness/spacing/angle. `def` supplies the tool's brush
|
||||
/// slot and the label for the one slider whose meaning varies (opacity vs. exposure vs.
|
||||
/// flow vs. strength).
|
||||
/// Render all options for a raster paint tool (brush picker + sliders).
|
||||
/// `is_eraser` drives which shared state is read/written.
|
||||
fn render_raster_tool_options(
|
||||
&mut self,
|
||||
ui: &mut Ui,
|
||||
shared: &mut SharedPaneState,
|
||||
def: &'static dyn crate::tools::RasterToolDef,
|
||||
is_eraser: bool,
|
||||
) {
|
||||
let kind = def.brush_kind();
|
||||
self.render_brush_preset_grid(ui, shared, kind);
|
||||
self.render_brush_preset_grid(ui, shared, is_eraser);
|
||||
ui.add_space(2.0);
|
||||
|
||||
if def.uses_color() {
|
||||
render_color_slot_row(ui, &mut shared.raster_settings.brush_mut(kind).use_fg);
|
||||
let rs = &mut shared.raster_settings;
|
||||
|
||||
if !is_eraser {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Color:");
|
||||
ui.selectable_value(&mut rs.brush_use_fg, true, "FG");
|
||||
ui.selectable_value(&mut rs.brush_use_fg, false, "BG");
|
||||
});
|
||||
}
|
||||
|
||||
let slot = shared.raster_settings.brush_mut(kind);
|
||||
macro_rules! field {
|
||||
($eraser:ident, $brush:ident) => {
|
||||
if is_eraser { &mut rs.$eraser } else { &mut rs.$brush }
|
||||
}
|
||||
}
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Size:");
|
||||
ui.add(egui::Slider::new(&mut slot.radius, 1.0_f32..=500.0)
|
||||
.logarithmic(true)
|
||||
.suffix(" px"));
|
||||
ui.add(egui::Slider::new(field!(eraser_radius, brush_radius), 1.0_f32..=200.0).logarithmic(true).suffix(" px"));
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(format!("{}:", def.strength_label()));
|
||||
ui.add(egui::Slider::new(&mut slot.strength, 0.0_f32..=1.0)
|
||||
ui.label("Opacity:");
|
||||
ui.add(egui::Slider::new(field!(eraser_opacity, brush_opacity), 0.0_f32..=1.0)
|
||||
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Hardness:");
|
||||
ui.add(egui::Slider::new(&mut slot.hardness, 0.0_f32..=1.0)
|
||||
ui.add(egui::Slider::new(field!(eraser_hardness, brush_hardness), 0.0_f32..=1.0)
|
||||
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Spacing:");
|
||||
ui.add(egui::Slider::new(&mut slot.spacing, 0.01_f32..=20.0)
|
||||
ui.add(egui::Slider::new(field!(eraser_spacing, brush_spacing), 0.01_f32..=1.0)
|
||||
.logarithmic(true)
|
||||
.custom_formatter(|v, _| format!("{:.2}", v)));
|
||||
.custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
|
||||
});
|
||||
|
||||
if slot.settings.elliptical_dab_ratio > 1.001 {
|
||||
let bs = if is_eraser { &rs.active_eraser_settings } else { &rs.active_brush_settings };
|
||||
if bs.elliptical_dab_ratio > 1.001 {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Angle:");
|
||||
ui.add(egui::Slider::new(&mut slot.angle_offset, -180.0_f32..=180.0)
|
||||
ui.add(egui::Slider::new(&mut rs.brush_angle_offset, -180.0_f32..=180.0)
|
||||
.suffix("°")
|
||||
.custom_formatter(|v, _| format!("{:.0}°", v)));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Render the brush preset thumbnail grid (collapsible) for one tool's brush slot.
|
||||
fn render_brush_preset_grid(
|
||||
&mut self,
|
||||
ui: &mut Ui,
|
||||
shared: &mut SharedPaneState,
|
||||
kind: crate::tools::BrushKind,
|
||||
) {
|
||||
/// Render the brush preset thumbnail grid (collapsible).
|
||||
/// `is_eraser` drives which picker state and which shared settings are updated.
|
||||
fn render_brush_preset_grid(&mut self, ui: &mut Ui, shared: &mut SharedPaneState, is_eraser: bool) {
|
||||
let presets = bundled_brushes();
|
||||
if presets.is_empty() { return; }
|
||||
|
||||
@@ -684,8 +658,8 @@ impl InfopanelPane {
|
||||
}
|
||||
|
||||
// Read picker state into locals to avoid multiple &mut self borrows.
|
||||
let mut expanded = self.brush_picker_expanded.get(&kind).copied().unwrap_or(false);
|
||||
let selected = shared.raster_settings.brush(kind).preset;
|
||||
let mut expanded = if is_eraser { self.eraser_picker_expanded } else { self.brush_picker_expanded };
|
||||
let mut selected = if is_eraser { self.selected_eraser_preset } else { self.selected_brush_preset };
|
||||
|
||||
let gap = 3.0;
|
||||
let cols = 2usize;
|
||||
@@ -756,14 +730,25 @@ impl InfopanelPane {
|
||||
egui::FontId::proportional(9.5),
|
||||
if is_sel { egui::Color32::from_rgb(140, 190, 255) } else { egui::Color32::from_gray(160) });
|
||||
if resp.clicked() {
|
||||
selected = Some(idx);
|
||||
expanded = false;
|
||||
shared.raster_settings.brush_mut(kind).apply_preset(idx, &preset.settings);
|
||||
// If the user was on a preset-backed tool (Pencil/Pen/Airbrush)
|
||||
// and manually picked a different brush, revert to the generic tool.
|
||||
if kind == crate::tools::BrushKind::Paint
|
||||
&& matches!(*shared.selected_tool, Tool::Pencil | Tool::Pen | Tool::Airbrush)
|
||||
{
|
||||
*shared.selected_tool = Tool::Draw;
|
||||
let s = &preset.settings;
|
||||
let rs = &mut shared.raster_settings;
|
||||
if is_eraser {
|
||||
rs.eraser_opacity = s.opaque.clamp(0.0, 1.0);
|
||||
rs.eraser_hardness = s.hardness.clamp(0.0, 1.0);
|
||||
rs.eraser_spacing = s.dabs_per_radius;
|
||||
rs.active_eraser_settings = s.clone();
|
||||
} else {
|
||||
rs.brush_opacity = s.opaque.clamp(0.0, 1.0);
|
||||
rs.brush_hardness = s.hardness.clamp(0.0, 1.0);
|
||||
rs.brush_spacing = s.dabs_per_radius;
|
||||
rs.active_brush_settings = s.clone();
|
||||
// If the user was on a preset-backed tool (Pencil/Pen/Airbrush)
|
||||
// and manually picked a different brush, revert to the generic tool.
|
||||
if matches!(*shared.selected_tool, Tool::Pencil | Tool::Pen | Tool::Airbrush) {
|
||||
*shared.selected_tool = Tool::Draw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -772,9 +757,14 @@ impl InfopanelPane {
|
||||
}
|
||||
}
|
||||
|
||||
// The selected preset lives on the slot itself (written by apply_preset above); only the
|
||||
// expand/collapse state is panel-local.
|
||||
self.brush_picker_expanded.insert(kind, expanded);
|
||||
// Write back picker state.
|
||||
if is_eraser {
|
||||
self.eraser_picker_expanded = expanded;
|
||||
self.selected_eraser_preset = selected;
|
||||
} else {
|
||||
self.brush_picker_expanded = expanded;
|
||||
self.selected_brush_preset = selected;
|
||||
}
|
||||
}
|
||||
|
||||
// Transform section: deferred to Phase 2 (DCEL elements don't have instance transforms)
|
||||
@@ -1151,9 +1141,8 @@ impl InfopanelPane {
|
||||
}
|
||||
|
||||
/// Render layer info section
|
||||
fn render_layer_section(&mut self, ui: &mut Ui, path: &NodePath, shared: &mut SharedPaneState, layer_ids: &[Uuid]) {
|
||||
use lightningbeam_core::actions::{set_layer_properties::LayerProperty, SetLayerPropertiesAction};
|
||||
use lightningbeam_core::layer::AudioLayerType;
|
||||
fn render_layer_section(&self, ui: &mut Ui, path: &NodePath, shared: &SharedPaneState, layer_ids: &[Uuid]) {
|
||||
let document = shared.action_executor.document();
|
||||
|
||||
egui::CollapsingHeader::new("Layer")
|
||||
.id_salt(("layer_info", path))
|
||||
@@ -1161,152 +1150,51 @@ impl InfopanelPane {
|
||||
.show(ui, |ui| {
|
||||
ui.add_space(4.0);
|
||||
|
||||
if layer_ids.len() != 1 {
|
||||
ui.label(format!("{} layers selected", layer_ids.len()));
|
||||
ui.add_space(4.0);
|
||||
return;
|
||||
}
|
||||
let layer_id = layer_ids[0];
|
||||
if layer_ids.len() == 1 {
|
||||
if let Some(layer) = document.get_layer(&layer_ids[0]) {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Name:");
|
||||
ui.label(layer.name());
|
||||
});
|
||||
|
||||
// Snapshot the current values, then drop the document borrow before mutating shared.
|
||||
let Some((type_name, is_audio, name, opacity, volume, muted, soloed, locked)) = ({
|
||||
let document = shared.action_executor.document();
|
||||
document.get_layer(&layer_id).map(|layer| {
|
||||
let type_name = match layer {
|
||||
AnyLayer::Vector(_) => "Vector",
|
||||
AnyLayer::Audio(a) => match a.audio_layer_type {
|
||||
AudioLayerType::Midi => "MIDI",
|
||||
AudioLayerType::Sampled => "Audio",
|
||||
lightningbeam_core::layer::AudioLayerType::Midi => "MIDI",
|
||||
lightningbeam_core::layer::AudioLayerType::Sampled => "Audio",
|
||||
},
|
||||
AnyLayer::Video(_) => "Video",
|
||||
AnyLayer::Effect(_) => "Effect",
|
||||
AnyLayer::Group(_) => "Group",
|
||||
AnyLayer::Raster(_) => "Raster",
|
||||
AnyLayer::Text(_) => "Text",
|
||||
};
|
||||
(
|
||||
type_name,
|
||||
matches!(layer, AnyLayer::Audio(_)),
|
||||
layer.name().to_string(),
|
||||
layer.opacity(),
|
||||
layer.volume(),
|
||||
layer.muted(),
|
||||
layer.soloed(),
|
||||
layer.locked(),
|
||||
)
|
||||
})
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Type:");
|
||||
ui.label(type_name);
|
||||
});
|
||||
|
||||
// Name (editable; commits on Enter / focus loss).
|
||||
if self.layer_name_edit.as_ref().map(|(id, _)| *id) != Some(layer_id) {
|
||||
self.layer_name_edit = Some((layer_id, name.clone()));
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Opacity:");
|
||||
ui.label(format!("{:.0}%", layer.opacity() * 100.0));
|
||||
});
|
||||
|
||||
if matches!(layer, AnyLayer::Audio(_)) {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Volume:");
|
||||
ui.label(format!("{:.0}%", layer.volume() * 100.0));
|
||||
});
|
||||
}
|
||||
|
||||
if layer.muted() {
|
||||
ui.label("Muted");
|
||||
}
|
||||
if layer.locked() {
|
||||
ui.label("Locked");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ui.label(format!("{} layers selected", layer_ids.len()));
|
||||
}
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Name:");
|
||||
let buf = &mut self.layer_name_edit.as_mut().unwrap().1;
|
||||
let resp = ui.text_edit_singleline(buf);
|
||||
let commit = resp.lost_focus()
|
||||
&& (ui.input(|i| i.key_pressed(egui::Key::Enter)) || !resp.has_focus());
|
||||
if commit {
|
||||
let new_name = buf.trim().to_string();
|
||||
if !new_name.is_empty() && new_name != name {
|
||||
shared.pending_actions.push(Box::new(SetLayerPropertiesAction::new(
|
||||
layer_id,
|
||||
LayerProperty::Name(new_name),
|
||||
)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Type:");
|
||||
ui.label(type_name);
|
||||
});
|
||||
|
||||
// Opacity slider (single-undo: live-preview while dragging, commit on release).
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Opacity:");
|
||||
let mut op = opacity;
|
||||
let r = ui.add(egui::Slider::new(&mut op, 0.0..=1.0).show_value(false));
|
||||
ui.label(format!("{:.0}%", op * 100.0));
|
||||
if (r.drag_started() || r.gained_focus()) && self.layer_opacity_drag_start.is_none() {
|
||||
self.layer_opacity_drag_start = Some(opacity);
|
||||
}
|
||||
if r.changed() {
|
||||
if let Some(l) = shared.action_executor.document_mut().get_layer_mut(&layer_id) {
|
||||
l.set_opacity(op);
|
||||
}
|
||||
}
|
||||
if r.drag_stopped() || r.lost_focus() {
|
||||
if let Some(start) = self.layer_opacity_drag_start.take() {
|
||||
if (start - op).abs() > 1e-6 {
|
||||
if let Some(l) = shared.action_executor.document_mut().get_layer_mut(&layer_id) {
|
||||
l.set_opacity(start); // revert so the action owns the change
|
||||
}
|
||||
shared.pending_actions.push(Box::new(SetLayerPropertiesAction::new(
|
||||
layer_id,
|
||||
LayerProperty::Opacity(op),
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if is_audio {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Volume:");
|
||||
let mut vol = volume;
|
||||
let r = ui.add(egui::Slider::new(&mut vol, 0.0..=1.0).show_value(false));
|
||||
ui.label(format!("{:.0}%", vol * 100.0));
|
||||
if (r.drag_started() || r.gained_focus()) && self.layer_volume_drag_start.is_none() {
|
||||
self.layer_volume_drag_start = Some(volume);
|
||||
}
|
||||
if r.changed() {
|
||||
if let Some(l) = shared.action_executor.document_mut().get_layer_mut(&layer_id) {
|
||||
l.set_volume(vol);
|
||||
}
|
||||
}
|
||||
if r.drag_stopped() || r.lost_focus() {
|
||||
if let Some(start) = self.layer_volume_drag_start.take() {
|
||||
if (start - vol).abs() > 1e-6 {
|
||||
if let Some(l) = shared.action_executor.document_mut().get_layer_mut(&layer_id) {
|
||||
l.set_volume(start);
|
||||
}
|
||||
shared.pending_actions.push(Box::new(SetLayerPropertiesAction::new(
|
||||
layer_id,
|
||||
LayerProperty::Volume(vol),
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Toggles: mute / solo (audio) / lock.
|
||||
ui.horizontal(|ui| {
|
||||
if is_audio {
|
||||
if ui.selectable_label(muted, "Mute").clicked() {
|
||||
shared.pending_actions.push(Box::new(SetLayerPropertiesAction::new(
|
||||
layer_id,
|
||||
LayerProperty::Muted(!muted),
|
||||
)));
|
||||
}
|
||||
if ui.selectable_label(soloed, "Solo").clicked() {
|
||||
shared.pending_actions.push(Box::new(SetLayerPropertiesAction::new(
|
||||
layer_id,
|
||||
LayerProperty::Soloed(!soloed),
|
||||
)));
|
||||
}
|
||||
}
|
||||
if ui.selectable_label(locked, "Lock").clicked() {
|
||||
shared.pending_actions.push(Box::new(SetLayerPropertiesAction::new(
|
||||
layer_id,
|
||||
LayerProperty::Locked(!locked),
|
||||
)));
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(4.0);
|
||||
});
|
||||
@@ -1359,186 +1247,6 @@ impl InfopanelPane {
|
||||
});
|
||||
}
|
||||
|
||||
/// Render a text-layer section: edit the text content, font, size, color,
|
||||
/// alignment, and box size of the active text layer. Driven by the *active*
|
||||
/// layer so it appears whenever a text layer is selected.
|
||||
fn render_text_layer_section(&mut self, ui: &mut Ui, path: &NodePath, shared: &mut SharedPaneState, layer_id: Uuid) {
|
||||
use lightningbeam_core::text_layer::TextAlign;
|
||||
use lightningbeam_core::actions::{ResizeTextBoxAction, SetTextContentAction};
|
||||
|
||||
// Snapshot current values, then drop the document borrow before mutating shared.
|
||||
let snapshot = {
|
||||
let document = shared.action_executor.document();
|
||||
match document.get_layer(&layer_id) {
|
||||
Some(AnyLayer::Text(tl)) => {
|
||||
Some((tl.content.clone(), tl.box_origin, tl.box_width, tl.box_height))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
let Some((content0, origin, w0, h0)) = snapshot else { return };
|
||||
|
||||
// Families egui has actually bound (added fonts take effect next frame). Using an
|
||||
// unbound `FontFamily::Name` panics, so only preview-render families in this set.
|
||||
let bound_families: std::collections::HashSet<egui::FontFamily> =
|
||||
ui.ctx().fonts(|f| f.families().into_iter().collect());
|
||||
// Set when the font dropdown popup is open this frame (its closure only runs then),
|
||||
// so we can synchronously finish loading any not-yet-preloaded fonts the user sees.
|
||||
let mut dropdown_open = false;
|
||||
|
||||
egui::CollapsingHeader::new("Text")
|
||||
.id_salt(("text_layer", path))
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
ui.add_space(4.0);
|
||||
let mut content = content0.clone();
|
||||
let mut content_changed = false;
|
||||
|
||||
ui.label("Text:");
|
||||
if ui
|
||||
.add(egui::TextEdit::multiline(&mut content.text).desired_rows(2).desired_width(f32::INFINITY))
|
||||
.changed()
|
||||
{
|
||||
content_changed = true;
|
||||
}
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Size:");
|
||||
if ui
|
||||
.add(egui::DragValue::new(&mut content.font_size).range(1.0..=2000.0).speed(0.5))
|
||||
.changed()
|
||||
{
|
||||
content_changed = true;
|
||||
}
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Color:");
|
||||
// content.color is sRGB-encoded RGBA in 0..1 (matches peniko Color::new).
|
||||
let mut col = egui::Color32::from_rgba_unmultiplied(
|
||||
(content.color[0] * 255.0).round() as u8,
|
||||
(content.color[1] * 255.0).round() as u8,
|
||||
(content.color[2] * 255.0).round() as u8,
|
||||
(content.color[3] * 255.0).round() as u8,
|
||||
);
|
||||
if ui.color_edit_button_srgba(&mut col).changed() {
|
||||
content.color = [
|
||||
col.r() as f32 / 255.0,
|
||||
col.g() as f32 / 255.0,
|
||||
col.b() as f32 / 255.0,
|
||||
col.a() as f32 / 255.0,
|
||||
];
|
||||
content_changed = true;
|
||||
}
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Align:");
|
||||
for (label, a) in [("L", TextAlign::Left), ("C", TextAlign::Center), ("R", TextAlign::Right), ("J", TextAlign::Justify)] {
|
||||
if ui.selectable_label(content.align == a, label).clicked() {
|
||||
content.align = a;
|
||||
content_changed = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Font family picker (bundled families first, then system; embedded fonts
|
||||
// register under their own names on load).
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Font:");
|
||||
let current_label = if content.font_family.is_empty() {
|
||||
"(Default)".to_string()
|
||||
} else {
|
||||
content.font_family.clone()
|
||||
};
|
||||
egui::ComboBox::from_id_salt(("text_font", path))
|
||||
.selected_text(current_label)
|
||||
.show_ui(ui, |ui| {
|
||||
dropdown_open = true;
|
||||
if ui.selectable_label(content.font_family.is_empty(), "(Default)").clicked() {
|
||||
content.font_family.clear();
|
||||
content_changed = true;
|
||||
}
|
||||
// The popup closure only runs while the dropdown is open, so each
|
||||
// entry is rendered in its own font and queued for registration.
|
||||
for fam in lightningbeam_core::fonts::families() {
|
||||
let named = egui::FontFamily::Name(fam.as_str().into());
|
||||
// Render in its own font only once egui has bound it; otherwise
|
||||
// plain (and queue it for registration so it previews next frame).
|
||||
let label = if bound_families.contains(&named) {
|
||||
egui::RichText::new(&fam).family(named)
|
||||
} else {
|
||||
egui::RichText::new(&fam)
|
||||
};
|
||||
if ui.selectable_label(content.font_family == fam, label).clicked() {
|
||||
content.font_family = fam.clone();
|
||||
content_changed = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
// Missing-font indicator.
|
||||
if !content.font_family.is_empty()
|
||||
&& !lightningbeam_core::fonts::family_available(&content.font_family)
|
||||
{
|
||||
ui.colored_label(egui::Color32::from_rgb(230, 160, 60), "⚠ missing");
|
||||
}
|
||||
});
|
||||
|
||||
// Box size.
|
||||
let mut bw = w0;
|
||||
let mut bh = h0;
|
||||
let mut box_changed = false;
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Box:");
|
||||
if ui.add(egui::DragValue::new(&mut bw).range(1.0..=100000.0).speed(1.0)).changed() {
|
||||
box_changed = true;
|
||||
}
|
||||
ui.label("×");
|
||||
if ui.add(egui::DragValue::new(&mut bh).range(1.0..=100000.0).speed(1.0)).changed() {
|
||||
box_changed = true;
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(4.0);
|
||||
|
||||
if content_changed {
|
||||
shared.pending_actions.push(Box::new(SetTextContentAction::new(layer_id, content)));
|
||||
}
|
||||
if box_changed {
|
||||
shared.pending_actions.push(Box::new(ResizeTextBoxAction::new(layer_id, origin, bw, bh)));
|
||||
}
|
||||
});
|
||||
|
||||
// Register fonts the background preloader has finished (cheap: bytes already loaded),
|
||||
// a few per frame so each atlas rebuild stays small. Only fall back to synchronous
|
||||
// loading for families the user is actively viewing in the open dropdown that the
|
||||
// background hasn't reached yet. Repaint while there's more to do or the preloader
|
||||
// is still running, so registration keeps flowing without input.
|
||||
let ctx = ui.ctx().clone();
|
||||
const PER_FRAME: usize = 4;
|
||||
let mut budget = PER_FRAME;
|
||||
let mut more = false;
|
||||
for fam in lightningbeam_core::fonts::families() {
|
||||
if self.registered_preview_fonts.contains(&fam) {
|
||||
continue;
|
||||
}
|
||||
let bytes = lightningbeam_core::fonts::take_preloaded(&fam).or_else(|| {
|
||||
if dropdown_open { lightningbeam_core::fonts::family_font_bytes(&fam) } else { None }
|
||||
});
|
||||
if let Some(bytes) = bytes {
|
||||
if budget == 0 {
|
||||
more = true;
|
||||
break;
|
||||
}
|
||||
self.register_font_bytes(&ctx, &fam, bytes);
|
||||
budget -= 1;
|
||||
}
|
||||
}
|
||||
if more || !lightningbeam_core::fonts::preload_done() {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
/// Render clip instance info section
|
||||
fn render_clip_instance_section(&self, ui: &mut Ui, path: &NodePath, shared: &SharedPaneState, clip_ids: &[Uuid]) {
|
||||
let document = shared.action_executor.document();
|
||||
@@ -1562,7 +1270,6 @@ impl InfopanelPane {
|
||||
AnyLayer::Effect(l) => &l.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
AnyLayer::Text(_) => &[],
|
||||
};
|
||||
if let Some(ci) = instances.iter().find(|c| c.id == ci_id) {
|
||||
found = true;
|
||||
@@ -1587,33 +1294,21 @@ impl InfopanelPane {
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Start:");
|
||||
ui.label(format!("{:.2}s", document.tempo_map().beats_to_seconds(ci.effective_start()).seconds_to_f64()));
|
||||
ui.label(format!("{:.2}s", ci.effective_start()));
|
||||
});
|
||||
|
||||
let clip_dur = document.clip_trim_duration(&ci.clip_id)
|
||||
.unwrap_or_else(|| lightningbeam_core::clip::ClipDuration::Seconds(
|
||||
daw_backend::Seconds(
|
||||
(ci.trim_end.unwrap_or(daw_backend::ContentTime(1.0)) - ci.trim_start).raw(),
|
||||
),
|
||||
));
|
||||
let clip_dur = document.get_clip_duration(&ci.clip_id)
|
||||
.unwrap_or_else(|| ci.trim_end.unwrap_or(1.0) - ci.trim_start);
|
||||
let total_dur = ci.total_duration(clip_dur, document.tempo_map());
|
||||
let total_dur_secs = (document.tempo_map().beats_to_seconds(ci.effective_start() + total_dur)
|
||||
- document.tempo_map().beats_to_seconds(ci.effective_start())).seconds_to_f64();
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Duration:");
|
||||
ui.label(format!("{:.2}s", total_dur_secs));
|
||||
ui.label(format!("{:.2}s", total_dur));
|
||||
});
|
||||
|
||||
if ci.trim_start > daw_backend::ContentTime::ZERO {
|
||||
if ci.trim_start > 0.0 {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Trim Start:");
|
||||
// Content time in the clip's own domain: seconds for sampled
|
||||
// audio/video/vector, beats for MIDI. Label it accordingly.
|
||||
let unit = match clip_dur {
|
||||
lightningbeam_core::clip::ClipDuration::Beats(_) => "beats",
|
||||
lightningbeam_core::clip::ClipDuration::Seconds(_) => "s",
|
||||
};
|
||||
ui.label(format!("{:.2}{}", ci.trim_start.raw(), unit));
|
||||
ui.label(format!("{:.2}s", ci.trim_start));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1796,7 +1491,7 @@ impl InfopanelPane {
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Duration:");
|
||||
ui.label(format!("{:.2}s", clip.content_duration().to_seconds(document.tempo_map()).seconds_to_f64()));
|
||||
ui.label(format!("{:.2}s", clip.duration));
|
||||
});
|
||||
} else {
|
||||
// Could be an image asset or effect — show ID
|
||||
@@ -1906,7 +1601,6 @@ impl PaneRenderer for InfopanelPane {
|
||||
// active (independent of selection focus, since painting doesn't focus the layer).
|
||||
if let Some(active_id) = *shared.active_layer_id {
|
||||
self.render_raster_layer_section(ui, path, shared, active_id);
|
||||
self.render_text_layer_section(ui, path, shared, active_id);
|
||||
}
|
||||
|
||||
// Onion-skinning view settings — always available, regardless of selection.
|
||||
@@ -1921,17 +1615,3 @@ impl PaneRenderer for InfopanelPane {
|
||||
"Info Panel"
|
||||
}
|
||||
}
|
||||
|
||||
/// The FG/BG selector shared by every tool that reads or writes a color (brush, paint bucket,
|
||||
/// eyedropper, …), so the choice is made the same way and in the same place regardless of tool.
|
||||
/// The wording stays neutral because the eyedropper *writes* to the chosen swatch while the
|
||||
/// painting tools *read* from it.
|
||||
fn render_color_slot_row(ui: &mut Ui, use_fg: &mut bool) {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Color:");
|
||||
ui.selectable_value(use_fg, true, "FG")
|
||||
.on_hover_text("Foreground (stroke) color");
|
||||
ui.selectable_value(use_fg, false, "BG")
|
||||
.on_hover_text("Background (fill) color");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
//! Shared horizontal keyboard geometry (pitch → x), used by both the Virtual Piano and the mobile
|
||||
//! portrait "Synthesia" Piano Roll so their columns line up with the keys. It is **width-driven**
|
||||
//! (key width from the pane width, not its height) and supports a smooth horizontal **pan** (pixels)
|
||||
//! so the roll and keyboard scroll together and stay aligned.
|
||||
|
||||
/// Number of white keys strictly before each pitch-class within its octave.
|
||||
const WHITES_BEFORE: [i32; 12] = [0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6];
|
||||
/// White pitch-classes, indexed by white-key-within-octave.
|
||||
const WHITE_PC: [i32; 7] = [0, 2, 4, 5, 7, 9, 11];
|
||||
/// Target on-screen white-key width (px); the visible white-key count approximates it.
|
||||
const TARGET_WHITE_KEY_W: f32 = 40.0;
|
||||
|
||||
/// Absolute white-key index of a note, counting white keys from MIDI 0.
|
||||
fn awi(note: u8) -> i32 {
|
||||
(note as i32 / 12) * 7 + WHITES_BEFORE[(note % 12) as usize]
|
||||
}
|
||||
|
||||
/// The white note at a given absolute white-key index.
|
||||
fn white_note_from_awi(a: i32) -> u8 {
|
||||
let oct = a.div_euclid(7);
|
||||
let rem = a.rem_euclid(7) as usize;
|
||||
(oct * 12 + WHITE_PC[rem]).clamp(0, 127) as u8
|
||||
}
|
||||
|
||||
/// A horizontal piano key layout for a pane width, octave center, and horizontal pan.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct KeyboardLayout {
|
||||
pub white_key_width: f32,
|
||||
pub black_key_width: f32,
|
||||
/// `note_x(white n) == base_x + awi(n) * white_key_width`.
|
||||
base_x: f32,
|
||||
rect_left: f32,
|
||||
rect_width: f32,
|
||||
}
|
||||
|
||||
impl KeyboardLayout {
|
||||
pub fn is_black_key(note: u8) -> bool {
|
||||
matches!(note % 12, 1 | 3 | 6 | 8 | 10)
|
||||
}
|
||||
|
||||
/// Build a layout. `pan_x` shifts everything right (drag-right reveals lower keys); at `pan_x == 0`
|
||||
/// the octave-center key sits in the middle of the pane.
|
||||
pub fn from_width(origin_x: f32, width: f32, octave: i8, pan_x: f32) -> Self {
|
||||
let vis = ((width / TARGET_WHITE_KEY_W).round() as u32).clamp(7, 24) as f32;
|
||||
let white_key_width = width / vis;
|
||||
let black_key_width = white_key_width * 0.6;
|
||||
let center = (60 + octave as i32 * 12).clamp(0, 127) as u8;
|
||||
let base_x = origin_x + width / 2.0 - (awi(center) as f32 + 0.5) * white_key_width + pan_x;
|
||||
Self { white_key_width, black_key_width, base_x, rect_left: origin_x, rect_width: width }
|
||||
}
|
||||
|
||||
/// Snap a pan offset to the nearest whole key (for release-snap).
|
||||
pub fn snap_pan(&self, pan_x: f32) -> f32 {
|
||||
(pan_x / self.white_key_width).round() * self.white_key_width
|
||||
}
|
||||
|
||||
/// Left x of a key's rect (black keys straddle the white boundary).
|
||||
pub fn note_x(&self, note: u8) -> f32 {
|
||||
let x = self.base_x + awi(note) as f32 * self.white_key_width;
|
||||
if Self::is_black_key(note) {
|
||||
x - self.black_key_width / 2.0
|
||||
} else {
|
||||
x
|
||||
}
|
||||
}
|
||||
|
||||
pub fn note_width(&self, note: u8) -> f32 {
|
||||
if Self::is_black_key(note) {
|
||||
self.black_key_width
|
||||
} else {
|
||||
self.white_key_width
|
||||
}
|
||||
}
|
||||
|
||||
pub fn note_center_x(&self, note: u8) -> f32 {
|
||||
self.note_x(note) + self.note_width(note) / 2.0
|
||||
}
|
||||
|
||||
/// Leftmost visible white key (with one key of margin).
|
||||
pub fn first_visible_white(&self) -> u8 {
|
||||
let a = ((self.rect_left - self.base_x) / self.white_key_width).floor() as i32 - 1;
|
||||
white_note_from_awi(a.max(0))
|
||||
}
|
||||
|
||||
/// Rightmost visible note (with one key of margin).
|
||||
pub fn last_visible_note(&self) -> u8 {
|
||||
let a = ((self.rect_left + self.rect_width - self.base_x) / self.white_key_width).ceil() as i32 + 1;
|
||||
white_note_from_awi(a).min(127)
|
||||
}
|
||||
|
||||
pub fn visible_notes(&self) -> std::ops::RangeInclusive<u8> {
|
||||
self.first_visible_white()..=self.last_visible_note()
|
||||
}
|
||||
|
||||
/// Which key is at screen x (black keys, drawn on top, take precedence).
|
||||
pub fn x_to_note(&self, x: f32) -> u8 {
|
||||
for note in self.visible_notes() {
|
||||
if Self::is_black_key(note) {
|
||||
let c = self.note_center_x(note);
|
||||
if (x - c).abs() <= self.black_key_width / 2.0 {
|
||||
return note;
|
||||
}
|
||||
}
|
||||
}
|
||||
let a = ((x - self.base_x) / self.white_key_width).floor() as i32;
|
||||
white_note_from_awi(a.max(0)).min(127)
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,6 @@ pub mod gradient_editor;
|
||||
pub mod timeline;
|
||||
pub mod infopanel;
|
||||
pub mod outliner;
|
||||
pub mod keyboard_layout;
|
||||
pub mod piano_roll;
|
||||
pub mod virtual_piano;
|
||||
pub mod node_editor;
|
||||
@@ -189,6 +188,8 @@ pub struct SharedPaneState<'a> {
|
||||
pub selected_tool: &'a mut Tool,
|
||||
pub fill_color: &'a mut egui::Color32,
|
||||
pub stroke_color: &'a mut egui::Color32,
|
||||
/// Tracks which color (fill or stroke) was last interacted with, for eyedropper tool
|
||||
pub active_color_mode: &'a mut ColorMode,
|
||||
pub pending_view_action: &'a mut Option<crate::menu::MenuAction>,
|
||||
/// Tracks the priority of the best fallback pane for view actions
|
||||
/// Lower number = higher priority. None = no fallback pane seen yet
|
||||
@@ -211,15 +212,10 @@ pub struct SharedPaneState<'a> {
|
||||
pub editing_instance_id: Option<uuid::Uuid>,
|
||||
/// The parent layer ID containing the clip instance being edited
|
||||
pub editing_parent_layer_id: Option<uuid::Uuid>,
|
||||
/// The full clip_id path being edited, outermost → current (for breadcrumbs). Empty at root.
|
||||
pub editing_clip_path: Vec<uuid::Uuid>,
|
||||
/// Request to enter a movie clip for editing: (clip_id, instance_id, parent_layer_id)
|
||||
pub pending_enter_clip: &'a mut Option<(uuid::Uuid, uuid::Uuid, uuid::Uuid)>,
|
||||
/// Request to exit the current movie clip
|
||||
pub pending_exit_clip: &'a mut bool,
|
||||
/// Request to exit up to a specific editing depth (number of clips to keep); e.g. a breadcrumb
|
||||
/// click. `Some(0)` exits all the way to the document root.
|
||||
pub pending_exit_to_depth: &'a mut Option<usize>,
|
||||
/// Currently active layer ID
|
||||
pub active_layer_id: &'a mut Option<uuid::Uuid>,
|
||||
/// Current tool interaction state (mutable for tools to modify)
|
||||
@@ -360,37 +356,6 @@ pub struct SharedPaneState<'a> {
|
||||
/// on the first frame; panes (e.g. infopanel) convert the pixel data to egui
|
||||
/// TextureHandles. Each entry is `(width, height, sRGB-premultiplied RGBA bytes)`.
|
||||
pub brush_preview_pixels: &'a std::sync::Arc<std::sync::Mutex<Vec<(u32, u32, Vec<u8>)>>>,
|
||||
/// True when rendering the phone/mobile shell (panes can render more compactly).
|
||||
pub is_mobile: bool,
|
||||
/// Device orientation for the mobile shell: portrait (tall) vs landscape (wide). Defaults to
|
||||
/// `true`; the mobile shell sets it from the available rect. Panes that reflow (e.g. the Piano
|
||||
/// Roll's vertical vs conventional layout) key off this.
|
||||
pub is_portrait: bool,
|
||||
/// Shared keyboard octave offset (C4-relative), so the mobile Virtual Piano and the portrait
|
||||
/// Piano Roll agree on which keys are visible and stay column-aligned.
|
||||
pub keyboard_octave: &'a mut i8,
|
||||
/// Shared horizontal keyboard pan (px) for smooth left/right scroll of the mobile keyboard+roll.
|
||||
pub keyboard_pan_x: &'a mut f32,
|
||||
/// Whether the mobile instrument pane should show the falling-notes roll above the keys. Driven
|
||||
/// by the shell from the *snapped* pane size-class so the reveal happens at a stack snap point.
|
||||
pub instrument_show_roll: bool,
|
||||
/// Set by the mobile instrument header's "Presets" button; the shell opens the Preset Browser.
|
||||
pub open_instrument_browser: &'a mut bool,
|
||||
/// Set by the mobile instrument header's REC button; the Timeline pane picks it up and toggles
|
||||
/// recording (reusing its full count-in / clip-creation flow).
|
||||
pub pending_record_toggle: &'a mut bool,
|
||||
/// Mobile long-press context menu request. A pane sets this on `response.secondary_clicked()`
|
||||
/// (which fires on long-press) with the items relevant to what was pressed; the mobile shell
|
||||
/// renders one persistent popup and dispatches the chosen `MenuAction`. `None` = no menu.
|
||||
pub mobile_context_menu: &'a mut Option<MobileContextMenu>,
|
||||
}
|
||||
|
||||
/// A mobile long-press context menu: a screen position and a list of `(label, action)` items.
|
||||
/// Rendered by the mobile shell; each item dispatches its `MenuAction` via `pending_menu_actions`.
|
||||
#[derive(Clone)]
|
||||
pub struct MobileContextMenu {
|
||||
pub pos: egui::Pos2,
|
||||
pub items: Vec<(String, crate::menu::MenuAction)>,
|
||||
}
|
||||
|
||||
/// Trait for pane rendering
|
||||
|
||||
@@ -1,738 +0,0 @@
|
||||
//! Mobile (touch) node editor — Focus & Patch views (wireframe Plate 07).
|
||||
//!
|
||||
//! Swapped in for the desktop `draw_graph_editor` canvas when `shared.is_mobile`. Focus shows one
|
||||
//! module's parameters as big touch controls plus navigation; Patch does tap-to-cable wiring. All
|
||||
//! edits reuse the existing dispatch: mutating a param's `ValueType` in place is picked up by
|
||||
//! `check_parameter_changes`, and add/connect/disconnect go through `NodeGraphAction`.
|
||||
|
||||
use super::graph_data::{DataType, NodeTemplate, ValueType};
|
||||
use super::{actions, NodeGraphPane};
|
||||
use crate::mobile::icons;
|
||||
use eframe::egui;
|
||||
use egui_node_graph2::{InputId, InputParamKind, NodeDataTrait, NodeId, NodeTemplateTrait, OutputId};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub enum NodeViewMode {
|
||||
Focus,
|
||||
Patch,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum PortDir {
|
||||
In,
|
||||
Out,
|
||||
}
|
||||
|
||||
/// An armed port awaiting a compatible endpoint to cable to.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct PatchPick {
|
||||
node: NodeId,
|
||||
port: usize,
|
||||
dir: PortDir,
|
||||
typ: DataType,
|
||||
}
|
||||
|
||||
pub struct MobileNodeState {
|
||||
pub mode: NodeViewMode,
|
||||
/// The module currently shown in Focus (and centred in Patch).
|
||||
pub focus_node: Option<NodeId>,
|
||||
/// Whether the add-node picker overlay is open.
|
||||
pub show_add: bool,
|
||||
/// Search filter in the add-node picker.
|
||||
pub add_search: String,
|
||||
/// Armed port in Patch awaiting a compatible endpoint.
|
||||
patch_pick: Option<PatchPick>,
|
||||
}
|
||||
|
||||
impl Default for MobileNodeState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mode: NodeViewMode::Focus,
|
||||
focus_node: None,
|
||||
show_add: false,
|
||||
add_search: String::new(),
|
||||
patch_pick: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NodeGraphPane {
|
||||
pub(super) fn render_mobile(
|
||||
&mut self,
|
||||
ui: &mut egui::Ui,
|
||||
rect: egui::Rect,
|
||||
shared: &mut crate::panes::SharedPaneState,
|
||||
) {
|
||||
let bg = shared.theme.bg_color(&["#node-editor", ".pane-content"], ui.ctx(), egui::Color32::from_gray(28));
|
||||
ui.painter().rect_filled(rect, 0.0, bg);
|
||||
|
||||
// Resolve the focus node: keep the current one if it still exists, else the selected node,
|
||||
// else the first node in the graph.
|
||||
let focus_valid = self
|
||||
.mobile
|
||||
.focus_node
|
||||
.map_or(false, |id| self.state.graph.nodes.get(id).is_some());
|
||||
if !focus_valid {
|
||||
self.mobile.focus_node = self
|
||||
.state
|
||||
.selected_nodes
|
||||
.iter()
|
||||
.next()
|
||||
.copied()
|
||||
.or_else(|| self.state.graph.iter_nodes().next());
|
||||
}
|
||||
|
||||
// Header: Focus/Patch toggle + focused node name.
|
||||
let header_h = 44.0;
|
||||
let header = egui::Rect::from_min_max(rect.min, egui::pos2(rect.right(), rect.top() + header_h));
|
||||
let body = egui::Rect::from_min_max(egui::pos2(rect.left(), header.bottom()), rect.max);
|
||||
|
||||
let name = self
|
||||
.mobile
|
||||
.focus_node
|
||||
.and_then(|id| self.state.graph.nodes.get(id))
|
||||
.map(|n| n.label.clone())
|
||||
.unwrap_or_else(|| "—".to_string());
|
||||
ui.painter().line_segment(
|
||||
[egui::pos2(header.left(), header.bottom()), egui::pos2(header.right(), header.bottom())],
|
||||
egui::Stroke::new(1.0, egui::Color32::from_gray(60)),
|
||||
);
|
||||
let mut mode = self.mobile.mode;
|
||||
ui.scope_builder(
|
||||
egui::UiBuilder::new().max_rect(header.shrink(8.0)).layout(egui::Layout::left_to_right(egui::Align::Center)),
|
||||
|ui| {
|
||||
if ui.selectable_label(mode == NodeViewMode::Focus, "Focus").clicked() {
|
||||
mode = NodeViewMode::Focus;
|
||||
}
|
||||
if ui.selectable_label(mode == NodeViewMode::Patch, "Patch").clicked() {
|
||||
mode = NodeViewMode::Patch;
|
||||
}
|
||||
ui.add_space(8.0);
|
||||
ui.label(egui::RichText::new(name).strong());
|
||||
},
|
||||
);
|
||||
self.mobile.mode = mode;
|
||||
|
||||
// Keep backend-id map current so embedded `bottom_ui` (sampler/script/etc.) targets the
|
||||
// right backend node (mirrors the desktop pre-draw sync).
|
||||
self.user_state.node_backend_ids = self
|
||||
.node_id_map
|
||||
.iter()
|
||||
.map(|(&nid, bid)| (nid, bid.index()))
|
||||
.collect();
|
||||
|
||||
match self.mobile.mode {
|
||||
NodeViewMode::Focus => self.render_mobile_focus(ui, body, shared),
|
||||
NodeViewMode::Patch => self.render_mobile_patch(ui, body, shared),
|
||||
}
|
||||
|
||||
// Same dispatch tail as the desktop path: apply param edits and run any queued action.
|
||||
self.check_parameter_changes(shared);
|
||||
self.execute_pending_action(shared);
|
||||
|
||||
// Drain the custom-UI loads that have self-contained handlers (sampler + script sample).
|
||||
// Other bespoke interactions (sequencer grid, NAM model, script canvas) are a case-by-case
|
||||
// follow-up; clear their queues so they don't accumulate.
|
||||
if let Some(load) = self.user_state.pending_sampler_load.take() {
|
||||
self.handle_pending_sampler_load(load, shared);
|
||||
}
|
||||
if let Some(load) = self.user_state.pending_script_sample_load.take() {
|
||||
self.handle_pending_script_sample_load(load, shared);
|
||||
}
|
||||
self.user_state.pending_sequencer_changes.clear();
|
||||
self.user_state.pending_draw_param_changes.clear();
|
||||
self.user_state.pending_root_note_changes.clear();
|
||||
}
|
||||
|
||||
fn render_mobile_focus(
|
||||
&mut self,
|
||||
ui: &mut egui::Ui,
|
||||
body: egui::Rect,
|
||||
shared: &mut crate::panes::SharedPaneState,
|
||||
) {
|
||||
let Some(focus) = self.mobile.focus_node else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Full-width minimap strip across the top; everything else below it.
|
||||
let mm_h = 96.0;
|
||||
let mm = egui::Rect::from_min_max(body.min, egui::pos2(body.right(), body.top() + mm_h));
|
||||
// Reserve an add-node button strip at the bottom.
|
||||
let add_h = 46.0;
|
||||
let add_rect = egui::Rect::from_min_max(egui::pos2(body.left(), body.bottom() - add_h), body.max);
|
||||
let content = egui::Rect::from_min_max(egui::pos2(body.left(), mm.bottom()), egui::pos2(body.right(), add_rect.top()));
|
||||
|
||||
// Minimap first (tap a node to focus it).
|
||||
let mut jump: Option<NodeId> = self.render_minimap(ui, mm, focus);
|
||||
|
||||
// Connection travel chips (owned) + params.
|
||||
let (in_chips, out_chips) = self.focus_chips(focus);
|
||||
let inputs: Vec<(String, InputId)> = self
|
||||
.state
|
||||
.graph
|
||||
.nodes
|
||||
.get(focus)
|
||||
.map(|n| n.inputs.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
ui.scope_builder(
|
||||
egui::UiBuilder::new().max_rect(content.shrink(10.0)).layout(egui::Layout::top_down(egui::Align::Min)),
|
||||
|ui| {
|
||||
if !in_chips.is_empty() {
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.label(egui::RichText::new("in").weak());
|
||||
for (label, node) in &in_chips {
|
||||
if ui.button(label.as_str()).clicked() {
|
||||
jump = Some(*node);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if !out_chips.is_empty() {
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.label(egui::RichText::new("out").weak());
|
||||
for (label, node) in &out_chips {
|
||||
if ui.button(label.as_str()).clicked() {
|
||||
jump = Some(*node);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if !in_chips.is_empty() || !out_chips.is_empty() {
|
||||
ui.separator();
|
||||
}
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
let mut any = false;
|
||||
for (name, input_id) in &inputs {
|
||||
let Some(param) = self.state.graph.inputs.get_mut(*input_id) else {
|
||||
continue;
|
||||
};
|
||||
if matches!(param.kind, InputParamKind::ConnectionOnly) {
|
||||
continue;
|
||||
}
|
||||
any = true;
|
||||
render_param_row(ui, name, &mut param.value);
|
||||
ui.add_space(6.0);
|
||||
}
|
||||
if !any {
|
||||
ui.label(egui::RichText::new("No editable parameters").weak());
|
||||
}
|
||||
// Embedded desktop custom UI (sampler picker, sequencer grid, script UI, …).
|
||||
// Standard nodes render nothing here.
|
||||
if let Some(node) = self.state.graph.nodes.get(focus) {
|
||||
let _ = node.user_data.bottom_ui(ui, focus, &self.state.graph, &mut self.user_state);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// Add-node button.
|
||||
ui.scope_builder(
|
||||
egui::UiBuilder::new().max_rect(add_rect.shrink(8.0)).layout(egui::Layout::left_to_right(egui::Align::Center)),
|
||||
|ui| {
|
||||
if ui.button(egui::RichText::new("+ Add node").size(15.0)).clicked() {
|
||||
self.mobile.show_add = true;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
if let Some(nid) = jump {
|
||||
self.mobile.focus_node = Some(nid);
|
||||
}
|
||||
|
||||
if self.mobile.show_add {
|
||||
self.render_add_picker(ui, body, shared);
|
||||
}
|
||||
}
|
||||
|
||||
/// Connection chips for the focus node: (label, remote node) for upstream inputs and downstream
|
||||
/// outputs. Label names the remote endpoint (`node ▸ port`).
|
||||
fn focus_chips(&self, focus: NodeId) -> (Vec<(String, NodeId)>, Vec<(String, NodeId)>) {
|
||||
let mut ins = Vec::new();
|
||||
let mut outs = Vec::new();
|
||||
for (input_id, outputs) in self.state.graph.iter_connection_groups() {
|
||||
let in_node = self.state.graph.inputs.get(input_id).map(|p| p.node);
|
||||
for output_id in outputs {
|
||||
let out_node = self.state.graph.outputs.get(output_id).map(|p| p.node);
|
||||
if in_node == Some(focus) {
|
||||
if let Some(src) = out_node {
|
||||
ins.push((format!("{} · {}", self.node_label(src), self.output_port_name(src, output_id)), src));
|
||||
}
|
||||
}
|
||||
if out_node == Some(focus) {
|
||||
if let Some(dst) = in_node {
|
||||
outs.push((format!("{} · {}", self.node_label(dst), self.input_port_name(dst, input_id)), dst));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(ins, outs)
|
||||
}
|
||||
|
||||
fn node_label(&self, n: NodeId) -> String {
|
||||
self.state.graph.nodes.get(n).map(|x| x.label.clone()).unwrap_or_default()
|
||||
}
|
||||
fn output_port_name(&self, n: NodeId, oid: OutputId) -> String {
|
||||
self.state
|
||||
.graph
|
||||
.nodes
|
||||
.get(n)
|
||||
.and_then(|node| node.outputs.iter().find(|(_, id)| *id == oid).map(|(nm, _)| nm.clone()))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
fn input_port_name(&self, n: NodeId, iid: InputId) -> String {
|
||||
self.state
|
||||
.graph
|
||||
.nodes
|
||||
.get(n)
|
||||
.and_then(|node| node.inputs.iter().find(|(_, id)| *id == iid).map(|(nm, _)| nm.clone()))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Draw a minimap of the graph; returns a node id if the user tapped one.
|
||||
fn render_minimap(&self, ui: &mut egui::Ui, rect: egui::Rect, focus: NodeId) -> Option<NodeId> {
|
||||
let painter = ui.painter_at(rect);
|
||||
painter.rect_filled(rect, 4.0, egui::Color32::from_rgba_unmultiplied(0, 0, 0, 120));
|
||||
painter.rect_stroke(rect, 4.0, egui::Stroke::new(1.0, egui::Color32::from_gray(70)), egui::StrokeKind::Inside);
|
||||
|
||||
let nodes: Vec<(NodeId, egui::Pos2)> = self
|
||||
.state
|
||||
.graph
|
||||
.iter_nodes()
|
||||
.filter_map(|n| self.state.node_positions.get(n).map(|p| (n, *p)))
|
||||
.collect();
|
||||
if nodes.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let (mut mnx, mut mny, mut mxx, mut mxy) = (f32::MAX, f32::MAX, f32::MIN, f32::MIN);
|
||||
for (_, p) in &nodes {
|
||||
mnx = mnx.min(p.x);
|
||||
mny = mny.min(p.y);
|
||||
mxx = mxx.max(p.x);
|
||||
mxy = mxy.max(p.y);
|
||||
}
|
||||
let pad = 10.0;
|
||||
let inner = rect.shrink(pad);
|
||||
let span = egui::vec2((mxx - mnx).max(1.0), (mxy - mny).max(1.0));
|
||||
let map = |p: egui::Pos2| {
|
||||
egui::pos2(
|
||||
inner.left() + (p.x - mnx) / span.x * inner.width(),
|
||||
inner.top() + (p.y - mny) / span.y * inner.height(),
|
||||
)
|
||||
};
|
||||
|
||||
// Connection lines.
|
||||
for (input_id, outputs) in self.state.graph.iter_connection_groups() {
|
||||
let in_pos = self.state.graph.inputs.get(input_id).and_then(|p| self.state.node_positions.get(p.node)).copied();
|
||||
for output_id in outputs {
|
||||
let out_pos = self.state.graph.outputs.get(output_id).and_then(|p| self.state.node_positions.get(p.node)).copied();
|
||||
if let (Some(a), Some(b)) = (out_pos, in_pos) {
|
||||
painter.line_segment([map(a), map(b)], egui::Stroke::new(1.0, egui::Color32::from_gray(90)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let resp = ui.interact(rect, ui.id().with("node_minimap"), egui::Sense::click());
|
||||
let click = if resp.clicked() { resp.interact_pointer_pos() } else { None };
|
||||
let mut hit = None;
|
||||
let mut best_d = f32::MAX;
|
||||
for (nid, p) in &nodes {
|
||||
let c = map(*p);
|
||||
let focused = *nid == focus;
|
||||
let color = if focused { egui::Color32::from_rgb(0x4a, 0xa3, 0xff) } else { egui::Color32::from_gray(200) };
|
||||
painter.circle_filled(c, if focused { 7.0 } else { 5.0 }, color);
|
||||
if focused {
|
||||
painter.circle_stroke(c, 9.0, egui::Stroke::new(1.5, color));
|
||||
}
|
||||
// Tap selects the nearest node (dots are small, so don't require a precise hit).
|
||||
if let Some(cp) = click {
|
||||
let d = (cp - c).length();
|
||||
if d < best_d {
|
||||
best_d = d;
|
||||
hit = Some(*nid);
|
||||
}
|
||||
}
|
||||
}
|
||||
hit
|
||||
}
|
||||
|
||||
/// The add-node picker overlay: a searchable list of templates.
|
||||
fn render_add_picker(&mut self, ui: &mut egui::Ui, body: egui::Rect, shared: &mut crate::panes::SharedPaneState) {
|
||||
let panel = egui::Rect::from_center_size(body.center(), egui::vec2(body.width().min(320.0), body.height().min(420.0)));
|
||||
let mut chosen: Option<NodeTemplate> = None;
|
||||
let mut close = false;
|
||||
ui.scope_builder(egui::UiBuilder::new().max_rect(panel), |ui| {
|
||||
egui::Frame::popup(ui.style()).show(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(egui::RichText::new("Add node").strong());
|
||||
if ui.button("✕").clicked() {
|
||||
close = true;
|
||||
}
|
||||
});
|
||||
ui.add(egui::TextEdit::singleline(&mut self.mobile.add_search).hint_text("Search…"));
|
||||
let q = self.mobile.add_search.to_lowercase();
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
for template in NodeTemplate::all_finder_kinds() {
|
||||
let label = template.node_finder_label(&mut self.user_state).to_string();
|
||||
if !q.is_empty() && !label.to_lowercase().contains(&q) {
|
||||
continue;
|
||||
}
|
||||
if ui.button(&label).clicked() {
|
||||
chosen = Some(template);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
if let Some(t) = chosen {
|
||||
self.mobile_add_node(t, shared);
|
||||
self.mobile.show_add = false;
|
||||
self.mobile.add_search.clear();
|
||||
}
|
||||
if close {
|
||||
self.mobile.show_add = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a node (frontend + backend action) and focus it, mirroring the desktop CreatedNode path.
|
||||
fn mobile_add_node(&mut self, template: NodeTemplate, _shared: &mut crate::panes::SharedPaneState) {
|
||||
let Some(track_id) = self.track_id else {
|
||||
return;
|
||||
};
|
||||
// Place near the current focus node, else the origin.
|
||||
let base = self
|
||||
.mobile
|
||||
.focus_node
|
||||
.and_then(|id| self.state.node_positions.get(id).copied())
|
||||
.unwrap_or(egui::Pos2::ZERO);
|
||||
let pos = base + egui::vec2(160.0, 0.0);
|
||||
|
||||
let label = template.node_graph_label(&mut self.user_state);
|
||||
let user_data = template.user_data(&mut self.user_state);
|
||||
let new_id = self
|
||||
.state
|
||||
.graph
|
||||
.add_node(label, user_data, |graph, id| template.build_node(graph, &mut self.user_state, id));
|
||||
self.state.node_positions.insert(new_id, pos);
|
||||
|
||||
let node_type = template.backend_type_name().to_string();
|
||||
self.pending_action = Some(Box::new(actions::NodeGraphAction::AddNode(
|
||||
actions::AddNodeAction::new(track_id, node_type.clone(), (pos.x, pos.y)),
|
||||
)));
|
||||
self.pending_node_addition = Some((new_id, node_type, (pos.x, pos.y)));
|
||||
self.mobile.focus_node = Some(new_id);
|
||||
}
|
||||
|
||||
fn render_mobile_patch(
|
||||
&mut self,
|
||||
ui: &mut egui::Ui,
|
||||
body: egui::Rect,
|
||||
_shared: &mut crate::panes::SharedPaneState,
|
||||
) {
|
||||
let Some(focus) = self.mobile.focus_node else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Build owned row data (immutable reads) so rendering can freely queue ops.
|
||||
let inputs: Vec<(String, InputId)> = self.state.graph.nodes.get(focus).map(|n| n.inputs.clone()).unwrap_or_default();
|
||||
let outputs: Vec<(String, OutputId)> = self.state.graph.nodes.get(focus).map(|n| n.outputs.clone()).unwrap_or_default();
|
||||
|
||||
// Rows: (idx, name, type, cables[(remote_node, remote_port, other_node, other_port)]).
|
||||
let mut in_rows: Vec<(usize, String, DataType, Vec<(String, String, NodeId, usize)>)> = Vec::new();
|
||||
for (idx, (name, input_id)) in inputs.iter().enumerate() {
|
||||
let Some(typ) = self.state.graph.inputs.get(*input_id).map(|p| p.typ) else { continue };
|
||||
let mut cables = Vec::new();
|
||||
if let Some(outs) = self.state.graph.connections.get(*input_id) {
|
||||
for oid in outs {
|
||||
if let Some(src) = self.state.graph.outputs.get(*oid).map(|o| o.node) {
|
||||
let sp = self.output_port_index(src, *oid);
|
||||
cables.push((self.node_label(src), self.output_port_name(src, *oid), src, sp));
|
||||
}
|
||||
}
|
||||
}
|
||||
in_rows.push((idx, name.clone(), typ, cables));
|
||||
}
|
||||
let mut out_rows: Vec<(usize, String, DataType, Vec<(String, String, NodeId, usize)>)> = Vec::new();
|
||||
for (idx, (name, output_id)) in outputs.iter().enumerate() {
|
||||
let Some(typ) = self.state.graph.outputs.get(*output_id).map(|o| o.typ) else { continue };
|
||||
let mut cables = Vec::new();
|
||||
for (input_id, outs) in self.state.graph.iter_connection_groups() {
|
||||
if outs.iter().any(|o| o == output_id) {
|
||||
if let Some(dst) = self.state.graph.inputs.get(input_id).map(|i| i.node) {
|
||||
let dp = self.input_port_index(dst, input_id);
|
||||
cables.push((self.node_label(dst), self.input_port_name(dst, input_id), dst, dp));
|
||||
}
|
||||
}
|
||||
}
|
||||
out_rows.push((idx, name.clone(), typ, cables));
|
||||
}
|
||||
|
||||
// Queue ops during render, apply after (avoids borrowing self mid-render).
|
||||
enum Op {
|
||||
Disconnect(NodeId, usize, NodeId, usize), // (out_node, out_port, in_node, in_port)
|
||||
Connect(NodeId, usize, NodeId, usize),
|
||||
Pick(PatchPick),
|
||||
ClearPick,
|
||||
}
|
||||
let mut ops: Vec<Op> = Vec::new();
|
||||
let pick = self.mobile.patch_pick;
|
||||
|
||||
ui.scope_builder(
|
||||
egui::UiBuilder::new().max_rect(body.shrink(10.0)).layout(egui::Layout::top_down(egui::Align::Min)),
|
||||
|ui| {
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
// A Grid aligns each section's port arrows into a column. Tapping a port arrow
|
||||
// arms a cable from it (also for unconnected ports); tapping a cable chip removes
|
||||
// it. Inputs read `[cables] ⥓ name`; outputs read `name ↦ [cables]`.
|
||||
ui.label(egui::RichText::new("Inputs").weak());
|
||||
egui::Grid::new("patch_inputs").num_columns(3).spacing([8.0, 6.0]).show(ui, |ui| {
|
||||
for (idx, name, typ, cables) in &in_rows {
|
||||
ui.horizontal(|ui| {
|
||||
for (rnode, rport, sn, sp) in cables {
|
||||
if cable_chip(ui, rnode, rport, *typ).clicked() {
|
||||
ops.push(Op::Disconnect(*sn, *sp, focus, *idx));
|
||||
}
|
||||
}
|
||||
});
|
||||
if arrow_button(ui, icons::ARROW_RIGHT_TO_LINE, *typ).clicked() {
|
||||
ops.push(Op::Pick(PatchPick { node: focus, port: *idx, dir: PortDir::In, typ: *typ }));
|
||||
}
|
||||
ui.label(name.as_str());
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
ui.add_space(10.0);
|
||||
ui.label(egui::RichText::new("Outputs").weak());
|
||||
egui::Grid::new("patch_outputs").num_columns(3).spacing([8.0, 6.0]).show(ui, |ui| {
|
||||
for (idx, name, typ, cables) in &out_rows {
|
||||
ui.label(name.as_str());
|
||||
if arrow_button(ui, icons::ARROW_RIGHT_FROM_LINE, *typ).clicked() {
|
||||
ops.push(Op::Pick(PatchPick { node: focus, port: *idx, dir: PortDir::Out, typ: *typ }));
|
||||
}
|
||||
ui.horizontal(|ui| {
|
||||
for (rnode, rport, dn, dp) in cables {
|
||||
if cable_chip(ui, rnode, rport, *typ).clicked() {
|
||||
ops.push(Op::Disconnect(focus, *idx, *dn, *dp));
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// Compatible-endpoint picker overlay when a port is armed.
|
||||
if let Some(p) = pick {
|
||||
let candidates = self.patch_candidates(p);
|
||||
let panel = egui::Rect::from_center_size(body.center(), egui::vec2(body.width().min(320.0), body.height().min(360.0)));
|
||||
ui.scope_builder(egui::UiBuilder::new().max_rect(panel), |ui| {
|
||||
egui::Frame::popup(ui.style()).show(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
port_marker(ui, p.typ, p.dir);
|
||||
ui.label(egui::RichText::new("Connect to…").strong());
|
||||
if ui.button("✕").clicked() {
|
||||
ops.push(Op::ClearPick);
|
||||
}
|
||||
});
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
if candidates.is_empty() {
|
||||
ui.label(egui::RichText::new("No compatible ports").weak());
|
||||
}
|
||||
for (label, other, other_port) in &candidates {
|
||||
if ui.button(egui::RichText::new(label.as_str()).color(type_color(p.typ))).clicked() {
|
||||
// Orient the cable: armed In needs an Out source; armed Out needs an In target.
|
||||
match p.dir {
|
||||
PortDir::In => ops.push(Op::Connect(*other, *other_port, p.node, p.port)),
|
||||
PortDir::Out => ops.push(Op::Connect(p.node, p.port, *other, *other_port)),
|
||||
}
|
||||
ops.push(Op::ClearPick);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
for op in ops {
|
||||
match op {
|
||||
Op::Disconnect(on, op_, inn, ip) => self.mobile_disconnect(on, op_, inn, ip),
|
||||
Op::Connect(on, op_, inn, ip) => self.mobile_connect(on, op_, inn, ip),
|
||||
Op::Pick(p) => self.mobile.patch_pick = Some(p),
|
||||
Op::ClearPick => self.mobile.patch_pick = None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compatible endpoints on OTHER nodes for an armed port (opposite direction, same type).
|
||||
fn patch_candidates(&self, p: PatchPick) -> Vec<(String, NodeId, usize)> {
|
||||
let mut out = Vec::new();
|
||||
for node in self.state.graph.iter_nodes() {
|
||||
if node == p.node {
|
||||
continue;
|
||||
}
|
||||
let Some(n) = self.state.graph.nodes.get(node) else { continue };
|
||||
match p.dir {
|
||||
// Armed input → list outputs of matching type.
|
||||
PortDir::In => {
|
||||
for (i, (name, oid)) in n.outputs.iter().enumerate() {
|
||||
if self.state.graph.outputs.get(*oid).map(|o| o.typ) == Some(p.typ) {
|
||||
out.push((format!("{} · {}", self.node_label(node), name), node, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Armed output → list inputs of matching type.
|
||||
PortDir::Out => {
|
||||
for (i, (name, iid)) in n.inputs.iter().enumerate() {
|
||||
if self.state.graph.inputs.get(*iid).map(|x| x.typ) == Some(p.typ) {
|
||||
out.push((format!("{} · {}", self.node_label(node), name), node, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn output_port_index(&self, n: NodeId, oid: OutputId) -> usize {
|
||||
self.state.graph.nodes.get(n).and_then(|node| node.outputs.iter().position(|(_, id)| *id == oid)).unwrap_or(0)
|
||||
}
|
||||
fn input_port_index(&self, n: NodeId, iid: InputId) -> usize {
|
||||
self.state.graph.nodes.get(n).and_then(|node| node.inputs.iter().position(|(_, id)| *id == iid)).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Cable an output port → input port: update the frontend graph + dispatch `Connect`.
|
||||
fn mobile_connect(&mut self, out_node: NodeId, out_port: usize, in_node: NodeId, in_port: usize) {
|
||||
// Mobile has no subgraph navigation yet, so cables always target the track-level graph. If
|
||||
// subgraph nav is added, route through `va_context()` / `graph_connect_in_template` like desktop.
|
||||
debug_assert!(self.subgraph_stack.is_empty(), "mobile node connect assumes the top-level graph");
|
||||
let Some(track_id) = self.track_id else { return };
|
||||
let output_id = self.state.graph.nodes.get(out_node).and_then(|n| n.outputs.get(out_port)).map(|(_, id)| *id);
|
||||
let input_id = self.state.graph.nodes.get(in_node).and_then(|n| n.inputs.get(in_port)).map(|(_, id)| *id);
|
||||
let (Some(output_id), Some(input_id)) = (output_id, input_id) else { return };
|
||||
if let Some(conns) = self.state.graph.connections.get_mut(input_id) {
|
||||
if !conns.contains(&output_id) {
|
||||
conns.push(output_id);
|
||||
}
|
||||
} else {
|
||||
self.state.graph.connections.insert(input_id, vec![output_id]);
|
||||
}
|
||||
if let (Some(&from_id), Some(&to_id)) = (self.node_id_map.get(&out_node), self.node_id_map.get(&in_node)) {
|
||||
self.pending_action = Some(Box::new(actions::NodeGraphAction::Connect(
|
||||
actions::ConnectAction::new(track_id, from_id, out_port, to_id, in_port),
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a cable: update the frontend graph + dispatch `Disconnect`. Like `mobile_connect`, this
|
||||
/// assumes the track-level graph (mobile has no subgraph navigation).
|
||||
fn mobile_disconnect(&mut self, out_node: NodeId, out_port: usize, in_node: NodeId, in_port: usize) {
|
||||
debug_assert!(self.subgraph_stack.is_empty(), "mobile node disconnect assumes the top-level graph");
|
||||
let Some(track_id) = self.track_id else { return };
|
||||
let output_id = self.state.graph.nodes.get(out_node).and_then(|n| n.outputs.get(out_port)).map(|(_, id)| *id);
|
||||
let input_id = self.state.graph.nodes.get(in_node).and_then(|n| n.inputs.get(in_port)).map(|(_, id)| *id);
|
||||
let (Some(output_id), Some(input_id)) = (output_id, input_id) else { return };
|
||||
if let Some(conns) = self.state.graph.connections.get_mut(input_id) {
|
||||
conns.retain(|o| *o != output_id);
|
||||
}
|
||||
if let (Some(&from_id), Some(&to_id)) = (self.node_id_map.get(&out_node), self.node_id_map.get(&in_node)) {
|
||||
self.pending_action = Some(Box::new(actions::NodeGraphAction::Disconnect(
|
||||
actions::DisconnectAction::new(track_id, from_id, out_port, to_id, in_port),
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Desktop port color per signal type (matches `DataType::data_type_color`).
|
||||
fn type_color(t: DataType) -> egui::Color32 {
|
||||
match t {
|
||||
DataType::Audio => egui::Color32::from_rgb(100, 150, 255), // blue
|
||||
DataType::Midi => egui::Color32::from_rgb(100, 255, 100), // green
|
||||
DataType::CV => egui::Color32::from_rgb(255, 150, 100), // orange
|
||||
}
|
||||
}
|
||||
|
||||
/// A single Lucide direction arrow glyph, tinted by signal type.
|
||||
fn arrow(ui: &mut egui::Ui, glyph: &str, t: DataType) {
|
||||
ui.label(
|
||||
egui::RichText::new(glyph)
|
||||
.color(type_color(t))
|
||||
.family(egui::FontFamily::Name(icons::FAMILY.into()))
|
||||
.size(15.0),
|
||||
);
|
||||
}
|
||||
|
||||
/// A port marker: a Lucide direction arrow (out = from-line, in = to-line) tinted by signal type.
|
||||
fn port_marker(ui: &mut egui::Ui, t: DataType, dir: PortDir) {
|
||||
let glyph = match dir {
|
||||
PortDir::In => icons::ARROW_RIGHT_TO_LINE,
|
||||
PortDir::Out => icons::ARROW_RIGHT_FROM_LINE,
|
||||
};
|
||||
arrow(ui, glyph, t);
|
||||
}
|
||||
|
||||
/// A clickable, aligned port arrow (out = from-line, in = to-line) tinted by signal type. Tapping it
|
||||
/// arms a cable from the port. Frameless so it reads like the desktop port dot.
|
||||
fn arrow_button(ui: &mut egui::Ui, glyph: &str, t: DataType) -> egui::Response {
|
||||
ui.add(
|
||||
egui::Button::new(
|
||||
egui::RichText::new(glyph)
|
||||
.color(type_color(t))
|
||||
.family(egui::FontFamily::Name(icons::FAMILY.into()))
|
||||
.size(16.0),
|
||||
)
|
||||
.frame(false),
|
||||
)
|
||||
}
|
||||
|
||||
/// A cable badge naming the remote endpoint (`remote-node · remote-port`), framed and tinted by
|
||||
/// signal type. Returns a click response (used to disconnect the cable).
|
||||
fn cable_chip(ui: &mut egui::Ui, remote_node: &str, remote_port: &str, t: DataType) -> egui::Response {
|
||||
egui::Frame::group(ui.style())
|
||||
.inner_margin(egui::Margin::symmetric(6, 1))
|
||||
.show(ui, |ui| {
|
||||
ui.label(egui::RichText::new(format!("{remote_node} · {remote_port}")).color(type_color(t)));
|
||||
})
|
||||
.response
|
||||
.interact(egui::Sense::click())
|
||||
}
|
||||
|
||||
/// One parameter row: a touch control matching the desktop widget rule (enum → dropdown, ranged →
|
||||
/// slider, plain → stepper, string → field). Mutates the value in place; dispatch happens later via
|
||||
/// `check_parameter_changes`.
|
||||
fn render_param_row(ui: &mut egui::Ui, name: &str, value: &mut ValueType) {
|
||||
match value {
|
||||
ValueType::Float { value, min, max, unit, enum_labels, .. } => {
|
||||
ui.label(name);
|
||||
if let Some(labels) = enum_labels {
|
||||
let mut sel = (*value as usize).min(labels.len().saturating_sub(1));
|
||||
egui::ComboBox::from_id_salt(name)
|
||||
.width(ui.available_width().min(240.0))
|
||||
.selected_text(labels.get(sel).copied().unwrap_or("?"))
|
||||
.show_ui(ui, |ui| {
|
||||
for (i, label) in labels.iter().enumerate() {
|
||||
ui.selectable_value(&mut sel, i, *label);
|
||||
}
|
||||
});
|
||||
*value = sel as f32;
|
||||
} else if *max > *min {
|
||||
// Give the rail a visible color so the full track length reads (the theme's default
|
||||
// inactive fill can be near-transparent); trailing_fill shows progress along it.
|
||||
ui.scope(|ui| {
|
||||
let rail = egui::Color32::from_gray(90);
|
||||
ui.visuals_mut().widgets.inactive.bg_fill = rail;
|
||||
ui.visuals_mut().widgets.inactive.weak_bg_fill = rail;
|
||||
ui.visuals_mut().widgets.hovered.bg_fill = rail;
|
||||
ui.add(egui::Slider::new(value, *min..=*max).suffix(*unit).trailing_fill(true));
|
||||
});
|
||||
} else {
|
||||
ui.add(egui::DragValue::new(value).speed(0.1));
|
||||
}
|
||||
}
|
||||
ValueType::String { value } => {
|
||||
ui.label(name);
|
||||
ui.text_edit_singleline(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ pub mod actions;
|
||||
pub mod audio_backend;
|
||||
pub mod backend;
|
||||
pub mod graph_data;
|
||||
mod mobile;
|
||||
|
||||
use backend::{BackendNodeId, GraphBackend};
|
||||
use graph_data::{AllNodeTemplates, SubgraphNodeTemplates, VoiceAllocatorNodeTemplates, DataType, GraphState, NamModelInfo, NodeData, NodeTemplate, PendingAmpSimLoad, ValueType};
|
||||
@@ -143,9 +142,6 @@ pub struct NodeGraphPane {
|
||||
last_oscilloscope_poll: std::time::Instant,
|
||||
/// Backend track ID (u32) for oscilloscope queries
|
||||
backend_track_id: Option<u32>,
|
||||
|
||||
/// Mobile (touch) Focus/Patch view state.
|
||||
mobile: mobile::MobileNodeState,
|
||||
}
|
||||
|
||||
impl NodeGraphPane {
|
||||
@@ -175,7 +171,6 @@ impl NodeGraphPane {
|
||||
pending_script_resolutions: Vec::new(),
|
||||
last_oscilloscope_poll: std::time::Instant::now(),
|
||||
backend_track_id: None,
|
||||
mobile: mobile::MobileNodeState::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2370,13 +2365,6 @@ impl crate::panes::PaneRenderer for NodeGraphPane {
|
||||
painter.galley(text_pos, galley, text_color);
|
||||
return;
|
||||
}
|
||||
|
||||
// Mobile: touch-native Focus/Patch views instead of the desktop canvas.
|
||||
if shared.is_mobile {
|
||||
self.render_mobile(ui, rect, shared);
|
||||
return;
|
||||
}
|
||||
|
||||
// Poll oscilloscope data at ~20 FPS
|
||||
let has_oscilloscopes;
|
||||
if self.last_oscilloscope_poll.elapsed() >= std::time::Duration::from_millis(50) {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user