Merge branch 'rust-ui' of https://git.skyler.io/skyler/Lightningbeam into rust-ui

This commit is contained in:
Skyler Lehmkuhl
2026-03-22 18:16:25 -04:00
32 changed files with 2896 additions and 470 deletions
@@ -109,6 +109,17 @@ pub trait Action: Send {
fn midi_notes_after_rollback(&self) -> Option<(u32, &[(f64, u8, u8, f64)])> {
None
}
/// Return full MIDI event data (CC, pitch bend, etc.) reflecting the state after execute/redo.
/// Used to keep the frontend MIDI event cache in sync after undo/redo.
fn midi_events_after_execute(&self) -> Option<(u32, &[daw_backend::audio::midi::MidiEvent])> {
None
}
/// Return full MIDI event data reflecting the state after rollback/undo.
fn midi_events_after_rollback(&self) -> Option<(u32, &[daw_backend::audio::midi::MidiEvent])> {
None
}
}
/// Action executor that wraps the document and manages undo/redo
@@ -280,6 +291,16 @@ impl ActionExecutor {
self.redo_stack.last().and_then(|a| a.midi_notes_after_rollback())
}
/// Get full MIDI event data from the last action on the undo stack (after redo).
pub fn last_undo_midi_events(&self) -> Option<(u32, &[daw_backend::audio::midi::MidiEvent])> {
self.undo_stack.last().and_then(|a| a.midi_events_after_execute())
}
/// Get full MIDI event data from the last action on the redo stack (after undo).
pub fn last_redo_midi_events(&self) -> Option<(u32, &[daw_backend::audio::midi::MidiEvent])> {
self.redo_stack.last().and_then(|a| a.midi_events_after_rollback())
}
/// Get the description of the next action to redo
pub fn redo_description(&self) -> Option<String> {
self.redo_stack.last().map(|a| a.description())
@@ -23,6 +23,7 @@ pub mod rename_folder;
pub mod delete_folder;
pub mod move_asset_to_folder;
pub mod update_midi_notes;
pub mod update_midi_events;
pub mod loop_clip_instances;
pub mod remove_clip_instances;
pub mod set_keyframe;
@@ -56,6 +57,7 @@ pub use rename_folder::RenameFolderAction;
pub use delete_folder::{DeleteFolderAction, DeleteStrategy};
pub use move_asset_to_folder::MoveAssetToFolderAction;
pub use update_midi_notes::UpdateMidiNotesAction;
pub use update_midi_events::UpdateMidiEventsAction;
pub use loop_clip_instances::LoopClipInstancesAction;
pub use remove_clip_instances::RemoveClipInstancesAction;
pub use set_keyframe::SetKeyframeAction;
@@ -0,0 +1,76 @@
use crate::action::Action;
use crate::document::Document;
use uuid::Uuid;
/// Action to replace all MIDI events in a clip (CC, pitch bend, notes, etc.) with undo/redo.
///
/// Used when editing per-note CC or pitch bend from the piano roll. Stores full
/// `MidiEvent` lists rather than the simplified note-tuple format of `UpdateMidiNotesAction`.
pub struct UpdateMidiEventsAction {
/// Layer containing the MIDI clip
pub layer_id: Uuid,
/// Backend MIDI clip ID
pub midi_clip_id: u32,
/// Full event list before the edit
pub old_events: Vec<daw_backend::audio::midi::MidiEvent>,
/// Full event list after the edit
pub new_events: Vec<daw_backend::audio::midi::MidiEvent>,
/// Human-readable description
pub description_text: String,
}
impl Action for UpdateMidiEventsAction {
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
fn description(&self) -> String {
self.description_text.clone()
}
fn execute_backend(
&mut self,
backend: &mut crate::action::BackendContext,
_document: &Document,
) -> Result<(), String> {
let controller = match backend.audio_controller.as_mut() {
Some(c) => c,
None => return Ok(()),
};
let track_id = backend
.layer_to_track_map
.get(&self.layer_id)
.ok_or_else(|| format!("Layer {} not mapped to backend track", self.layer_id))?;
controller.update_midi_clip_events(*track_id, self.midi_clip_id, self.new_events.clone());
Ok(())
}
fn rollback_backend(
&mut self,
backend: &mut crate::action::BackendContext,
_document: &Document,
) -> Result<(), String> {
let controller = match backend.audio_controller.as_mut() {
Some(c) => c,
None => return Ok(()),
};
let track_id = backend
.layer_to_track_map
.get(&self.layer_id)
.ok_or_else(|| format!("Layer {} not mapped to backend track", self.layer_id))?;
controller.update_midi_clip_events(*track_id, self.midi_clip_id, self.old_events.clone());
Ok(())
}
fn midi_events_after_execute(&self) -> Option<(u32, &[daw_backend::audio::midi::MidiEvent])> {
Some((self.midi_clip_id, &self.new_events))
}
fn midi_events_after_rollback(&self) -> Option<(u32, &[daw_backend::audio::midi::MidiEvent])> {
Some((self.midi_clip_id, &self.old_events))
}
}
@@ -133,6 +133,15 @@ impl Default for TimeSignature {
fn default_bpm() -> f64 { 120.0 }
/// How time is displayed in the timeline
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum TimelineMode {
#[default]
Seconds,
Measures,
Frames,
}
/// Asset category for folder tree access
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssetCategory {
@@ -226,6 +235,10 @@ pub struct Document {
#[serde(default)]
pub script_folders: AssetFolderTree,
/// How time is displayed in the timeline (saved with document)
#[serde(default)]
pub timeline_mode: TimelineMode,
/// Current UI layout state (serialized for save/load)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ui_layout: Option<LayoutNode>,
@@ -270,6 +283,7 @@ impl Default for Document {
effect_folders: AssetFolderTree::new(),
script_definitions: HashMap::new(),
script_folders: AssetFolderTree::new(),
timeline_mode: TimelineMode::Seconds,
ui_layout: None,
ui_layout_base: None,
current_time: 0.0,
@@ -24,6 +24,8 @@ use vello::Scene;
/// Cache for decoded image data to avoid re-decoding every frame
pub struct ImageCache {
cache: HashMap<Uuid, Arc<ImageBrush>>,
/// CPU path: tiny-skia pixmaps decoded from the same assets (premultiplied RGBA8)
cpu_cache: HashMap<Uuid, Arc<tiny_skia::Pixmap>>,
}
impl ImageCache {
@@ -31,6 +33,7 @@ impl ImageCache {
pub fn new() -> Self {
Self {
cache: HashMap::new(),
cpu_cache: HashMap::new(),
}
}
@@ -47,14 +50,28 @@ impl ImageCache {
Some(arc_image)
}
/// Get or decode an image as a premultiplied tiny-skia Pixmap (CPU render path).
pub fn get_or_decode_cpu(&mut self, asset: &ImageAsset) -> Option<Arc<tiny_skia::Pixmap>> {
if let Some(cached) = self.cpu_cache.get(&asset.id) {
return Some(Arc::clone(cached));
}
let pixmap = decode_image_to_pixmap(asset)?;
let arc = Arc::new(pixmap);
self.cpu_cache.insert(asset.id, Arc::clone(&arc));
Some(arc)
}
/// Clear cache entry when an image asset is deleted or modified
pub fn invalidate(&mut self, id: &Uuid) {
self.cache.remove(id);
self.cpu_cache.remove(id);
}
/// Clear all cached images
pub fn clear(&mut self) {
self.cache.clear();
self.cpu_cache.clear();
}
}
@@ -64,6 +81,25 @@ impl Default for ImageCache {
}
}
/// Decode an image asset to a premultiplied tiny-skia Pixmap (CPU render path).
fn decode_image_to_pixmap(asset: &ImageAsset) -> Option<tiny_skia::Pixmap> {
let data = asset.data.as_ref()?;
let img = image::load_from_memory(data).ok()?;
let rgba = img.to_rgba8();
let mut pixmap = tiny_skia::Pixmap::new(asset.width, asset.height)?;
for (dst, src) in pixmap.pixels_mut().iter_mut().zip(rgba.pixels()) {
let [r, g, b, a] = src.0;
// Convert straight alpha (image crate output) to premultiplied (tiny-skia internal format)
let af = a as f32 / 255.0;
let pr = (r as f32 * af).round() as u8;
let pg = (g as f32 * af).round() as u8;
let pb = (b as f32 * af).round() as u8;
// from_rgba only fails when channel > alpha; premultiplied values are always ≤ alpha
*dst = tiny_skia::PremultipliedColorU8::from_rgba(pr, pg, pb, a).unwrap();
}
Some(pixmap)
}
/// Decode an image asset to peniko ImageBrush
fn decode_image_asset(asset: &ImageAsset) -> Option<ImageBrush> {
// Get the raw file data
@@ -1368,8 +1404,8 @@ fn render_vector_graph_cpu(
pixmap: &mut tiny_skia::PixmapMut<'_>,
transform: tiny_skia::Transform,
opacity: f32,
_document: &Document,
_image_cache: &mut ImageCache,
document: &Document,
image_cache: &mut ImageCache,
) {
// 1. Fills
for (i, fill) in graph.fills.iter().enumerate() {
@@ -1412,8 +1448,25 @@ fn render_vector_graph_cpu(
}
}
// Image fill — not yet implemented for CPU renderer; fall through to solid or skip
// TODO: decode image to Pixmap and use as Pattern shader
// Image fill — decode to Pixmap and use as a Pattern shader
if let Some(image_asset_id) = face.image_fill {
if let Some(asset) = document.get_image_asset(&image_asset_id) {
if let Some(img_pixmap) = image_cache.get_or_decode_cpu(asset) {
let pattern = tiny_skia::Pattern::new(
tiny_skia::Pixmap::as_ref(&img_pixmap),
tiny_skia::SpreadMode::Pad,
tiny_skia::FilterQuality::Bilinear,
opacity,
tiny_skia::Transform::identity(),
);
let mut paint = tiny_skia::Paint::default();
paint.shader = pattern;
paint.anti_alias = true;
pixmap.fill_path(&ts_path, &paint, fill_type, transform, None);
filled = true;
}
}
}
// Solid colour fill
if !filled {