diff --git a/architecture.md b/architecture.md index 6639b21..ff7073f 100644 --- a/architecture.md +++ b/architecture.md @@ -478,7 +478,12 @@ MIDI bindings, or saved state. Profiles without an explicit layout synthesize one visible entry for every declared fader in declaration order. Validation rejects duplicate layout IDs, duplicate fader references, and dangling fader references. Each fader layout entry also stores a `slim`, `standard`, or `thick` -density. Omitted density defaults to `standard` for existing profiles. Vertical +density. Layout moves and separator insertion normalize the TOML tables' source +positions to a shared layout anchor before serialization. Parsed tables retain +their old source positions, so vector order alone does not survive serialization; +the normalized anchor preserves the requested order and each entry's comments +across Save and server restart. +Omitted density defaults to `standard` for existing profiles. Vertical mode keeps the stacked card layout: Slim is a single terminal row while the other sizes retain progressively more detail. Horizontal mode uses side-by-side console-style fader strips with bottom-up meters, state, percentage, pickup diff --git a/config/src/document.rs b/config/src/document.rs index 66ff9b5..390684f 100644 --- a/config/src/document.rs +++ b/config/src/document.rs @@ -425,7 +425,7 @@ pickup_tolerance = 0.02 separator["id"] = value(id); separator["label"] = value(label); ordered.insert(position.min(ordered.len()), separator); - *tables = ordered.into_iter().collect(); + replace_layout_order(tables, ordered); Ok(()) }) } @@ -524,7 +524,7 @@ pickup_tolerance = 0.02 })?; let entry = ordered.remove(index); ordered.insert(position.min(ordered.len()), entry); - *tables = ordered.into_iter().collect(); + replace_layout_order(tables, ordered); Ok(()) }) } @@ -717,6 +717,19 @@ fn materialize_mixer_layout(document: &mut DocumentMut) -> Result<&mut ArrayOfTa Ok(tables) } +fn replace_layout_order(tables: &mut ArrayOfTables, mut ordered: Vec) { + // Parsed tables carry source positions, and toml_edit sorts by those when + // serializing. Reordering the vector alone therefore undoes a move on + // save/preview. Give this flat layout array one common source anchor so + // stable serialization follows its new order, retaining each table's decor. + if let Some(anchor) = ordered.iter().filter_map(Table::position).min() { + for table in &mut ordered { + table.set_position(anchor); + } + } + *tables = ordered.into_iter().collect(); +} + fn layout_entry_id(table: &Table) -> Option<&str> { match table.get("kind").and_then(Item::as_str) { Some("fader") => table.get("fader").and_then(Item::as_str), diff --git a/config/tests/editor.rs b/config/tests/editor.rs index 1cf2544..4ecadda 100644 --- a/config/tests/editor.rs +++ b/config/tests/editor.rs @@ -245,6 +245,9 @@ fn mixer_layout_draft_reorders_separators_and_retains_hidden_faders() { draft .set_fader_density("media-fader", fader_config::FaderDensity::Thick) .expect("fader density must be editable"); + // Real sessions reopen a saved layout: parsed tables retain source positions. + let saved = draft.finish().unwrap(); + let mut draft = ProfileDocument::parse(&saved).unwrap(); draft .move_layout_entry("media-fader", 0) .expect("hidden fader must remain reorderable"); @@ -290,6 +293,73 @@ fn mixer_layout_draft_reorders_separators_and_retains_hidden_faders() { ); } +#[test] +fn saved_layout_moves_and_insertions_survive_repeated_reloads() { + let source = format!( + "{PROFILE}\n\ + [[faders]]\nid = \"other-fader\"\ntarget = {{ kind = \"group\", id = \"media\" }}\npickup = true\npickup_tolerance = 0.02\n\ + # first fader comment\n[[mixer_layout]]\nkind = \"fader\"\nfader = \"media-fader\"\ndensity = \"slim\"\n\ + # separator comment\n[[mixer_layout]]\nkind = \"separator\"\nid = \"heading\"\nlabel = \"Heading\"\n\ + # hidden fader comment\n[[mixer_layout]]\nkind = \"fader\"\nfader = \"other-fader\"\nhidden = true\n" + ); + let mut draft = ProfileDocument::parse(&source).unwrap(); + let ids = |draft: &ProfileDocument| { + draft + .preview() + .unwrap() + .mixer_layout() + .iter() + .map(|entry| match entry { + MixerLayoutEntry::Fader { fader, .. } => fader.clone(), + MixerLayoutEntry::Separator { id, .. } => id.clone(), + }) + .collect::>() + }; + for (id, position, expected) in [ + ("media-fader", 2, ["heading", "other-fader", "media-fader"]), + ("media-fader", 0, ["media-fader", "heading", "other-fader"]), + ("other-fader", 0, ["other-fader", "media-fader", "heading"]), + ("heading", 1, ["other-fader", "heading", "media-fader"]), + ] { + draft.move_layout_entry(id, position).unwrap(); + assert_eq!(ids(&draft), expected); + draft = ProfileDocument::parse(&draft.finish().unwrap()).unwrap(); + assert_eq!(ids(&draft), expected); + } + draft + .add_separator("new-heading", "New heading", 0) + .unwrap(); + draft = ProfileDocument::parse(&draft.finish().unwrap()).unwrap(); + assert_eq!( + ids(&draft), + ["new-heading", "other-fader", "heading", "media-fader"] + ); + let saved = draft.source(); + for comment in [ + "first fader comment", + "separator comment", + "hidden fader comment", + ] { + assert_eq!(saved.matches(comment).count(), 1); + } + let profile = draft.preview().unwrap(); + assert!(matches!( + &profile.mixer_layout()[1], + MixerLayoutEntry::Fader { hidden: true, .. } + )); + assert!(matches!( + &profile.mixer_layout()[3], + MixerLayoutEntry::Fader { + density: fader_config::FaderDensity::Slim, + .. + } + )); + assert_eq!( + profile.bindings(), + fader_config::Profile::parse(PROFILE).unwrap().bindings() + ); +} + #[test] fn selector_and_binding_edits_preserve_unrelated_capture_and_actions() { let mut draft = ProfileDocument::parse(PROFILE).unwrap(); diff --git a/fader_master/src/local.rs b/fader_master/src/local.rs index f83c129..719f78d 100644 --- a/fader_master/src/local.rs +++ b/fader_master/src/local.rs @@ -1071,6 +1071,63 @@ mod tests { ); } + #[test] + fn persisted_layout_reorders_through_ipc_after_each_server_restart() { + let root = tempdir().unwrap(); + let source = format!( + "{}\n[[faders]]\nid = \"second-fader\"\ntarget = {{ kind = \"group\", id = \"media\" }}\npickup = true\npickup_tolerance = 0.02\n\ + [[mixer_layout]]\nkind = \"fader\"\nfader = \"media-fader\"\n\ + [[mixer_layout]]\nkind = \"fader\"\nfader = \"second-fader\"\n", + include_str!("../../examples/profiles/desktop.toml") + ); + let profile_path = root.path().join("desktop.toml"); + fs::write(&profile_path, source).unwrap(); + for destination in [1, 0, 1] { + let source = fs::read_to_string(&profile_path).unwrap(); + let mut server = LocalServer::pulse( + root.path().join("control.sock"), + Gain::UNITY, + ProfileDocument::parse(&source).unwrap(), + profile_path.clone(), + root.path().join("config.toml"), + root.path().join("state.toml"), + ) + .unwrap(); + for command in [ + RequestCommand::EditConfiguration { + expected_revision: 0, + edit: crate::ipc::ConfigurationEdit::MoveLayoutEntry { + id: "media-fader".into(), + position: destination, + }, + }, + RequestCommand::Snapshot, + RequestCommand::SaveConfiguration { + expected_revision: 1, + }, + RequestCommand::Snapshot, + ] { + let response = direct_request(&mut server, command); + let ResponseResult::Snapshot { snapshot } = response.result else { + panic!("layout request failed: {:?}", response.result); + }; + assert!( + matches!( + &snapshot.configuration.mixer_layout[destination], + crate::ipc::MixerLayoutEntry::Fader { fader, .. } if fader == "media-fader" + ), + "confirmed layout reverted at destination {destination}" + ); + } + let saved = + fader_config::Profile::parse(&fs::read_to_string(&profile_path).unwrap()).unwrap(); + assert!(matches!( + &saved.mixer_layout()[destination], + fader_config::MixerLayoutEntry::Fader { fader, .. } if fader == "media-fader" + )); + } + } + fn direct_request( server: &mut LocalServer, command: crate::ipc::RequestCommand, diff --git a/task.md b/task.md index b4e4b5e..4b6cdee 100644 --- a/task.md +++ b/task.md @@ -400,6 +400,11 @@ Dependencies: M8, M2, M1. Config also persists pickup-status presentation as Detailed, Compact, Symbol, Color, or Hidden. Both mixer orientations share the same state mapping, and hiding the indicator does not disable pickup protection. + Reordering saved layouts now resets TOML source-order metadata: parsed table + positions previously undid accepted moves during serialization, causing the + optimistic TUI swap to snap back. Regression tests cover repeated file reloads, + both move directions, separator insertion, hidden entries and comments, plus + IPC edit/refresh/Save and reordering after server restart. - [x] **M9.5 Guided fader editor:** replace pipe-delimited table commands with a selected-fader form. Application faders edit capture field/mode/pattern; groups select eligible application faders and show the absolute group mode;