From 2e46ec0b581c9a941775a0edb45f7ba86fac5cc3 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 13 Nov 2026 18:55:00 +0100 Subject: [PATCH] Load linked Midra Preview layers into the editor --- crates/midra-layout/src/device_ui.rs | 173 ++++++++++++++++++++++++++ crates/midra-layout/src/gui.rs | 16 ++- crates/midra-layout/src/project_ui.rs | 1 + docs/interfaces/midra-device-send.md | 12 ++ docs/midra-layout.md | 12 ++ 5 files changed, 205 insertions(+), 9 deletions(-) diff --git a/crates/midra-layout/src/device_ui.rs b/crates/midra-layout/src/device_ui.rs index 3142ace..5735b65 100644 --- a/crates/midra-layout/src/device_ui.rs +++ b/crates/midra-layout/src/device_ui.rs @@ -27,6 +27,7 @@ pub(super) struct DeviceUi { pub(super) host: String, pending: Option>>, snapshot: Option, + loading: Option>, screen: usize, destinations: Vec, error: String, @@ -39,6 +40,7 @@ impl DeviceUi { } pub(super) fn reset_mappings(&mut self) { self.destinations.fill(Destination::Skip); + self.loading = None; } pub(super) fn with_connection_preferences(host: String, allow_program: bool) -> Self { Self { @@ -172,6 +174,88 @@ impl DeviceUi { } } impl Editor { + pub(super) fn midra_load_button(&mut self, ui: &mut egui::Ui, all: bool) { + let enabled = self.device.online() && self.device.pending.is_none(); + let tooltip = if self.device.message.is_empty() { + "Read fresh Preview geometry and cropping from Midra into the linked editor layers. Undo restores the previous layout." + } else { + &self.device.message + }; + if ui + .add_enabled( + enabled, + egui::Button::new(if self.device.loading.is_some() { + "Loading…" + } else if all { + "Load linked layers" + } else { + "Load mapped layers" + }), + ) + .on_hover_text(tooltip) + .clicked() + { + let indices: Vec<_> = if all { + (0..self.layout.layers().len()) + .filter(|&i| self.layout.layer_info(i).midra_link.is_some()) + .collect() + } else { + self.layout.selected_indices() + }; + let links: Result, String> = indices + .into_iter() + .map(|i| { + let info = self.layout.layer_info(i); + info.midra_link + .clone() + .map(|link| (info.id, link)) + .ok_or_else(|| format!("{} has no Midra link.", info.name)) + }) + .collect(); + match links { + Ok(links) if !links.is_empty() => { + self.device.fetch(ui.ctx()); + self.device.loading = Some(links); + } + result => { + self.device.error = result.err().unwrap_or_else(|| { + "Choose layers and link their Midra destinations first.".into() + }); + self.device.prepare_send = true; + self.device.open = true; + } + } + ui.close_menu(); + } + } + fn finish_linked_load(&mut self) { + if self.device.pending.is_some() { + return; + } + let Some(links) = self.device.loading.take() else { + return; + }; + let result = self + .device + .snapshot + .as_ref() + .ok_or_else(|| self.device.error.clone()) + .and_then(|snapshot| load_linked_layers(&mut self.layout, snapshot, &links)); + match result { + Ok(()) => { + self.sync(); + self.device.message = format!( + "Loaded {} linked Preview layers. Undo restores the previous layout.", + links.len() + ); + } + Err(error) => { + self.device.error = error; + self.device.prepare_send = true; + self.device.open = true; + } + } + } pub(super) fn midra_apply_button(&mut self, ui: &mut egui::Ui) { if ui.add_enabled( self.device.online() && self.device.pending.is_none(), @@ -273,6 +357,7 @@ impl Editor { self.device.target = Target::Preview; } self.device.poll(); + self.finish_linked_load(); self.connection_window(ctx); if !self.device.open { return; @@ -328,6 +413,7 @@ impl Editor { if let Err(e) = self.layout.set_midra_link(i, link) { self.device.error = e; } } } + self.midra_load_button(ui, false); self.midra_send_button(ui); return; } @@ -534,6 +620,41 @@ fn send_review( None } +fn load_linked_layers( + layout: &mut midra_layout::Layout, + snapshot: &device::Snapshot, + links: &[(usize, midra_layout::MidraLink)], +) -> Result<(), String> { + let mut values = Vec::new(); + for (id, link) in links { + let screen = snapshot + .screens + .iter() + .find(|s| s.id == link.screen) + .ok_or_else(|| format!("Linked screen {} is unavailable.", link.screen))?; + if screen.resolution != layout.formats().output { + return Err(format!( + "Screen {} resolution does not match the editor output.", + screen.id + )); + } + let index = screen + .layers + .iter() + .position(|l| l.id == link.layer) + .ok_or_else(|| { + format!( + "Screen {} layer {} is unavailable.", + link.screen, link.layer + ) + })?; + screen.import(&[index])?; + let layer = &screen.layers[index]; + values.push((Some(*id), layer.geometry, layer.framing)); + } + layout.import_device_layers(&values) +} + fn container_label(geometry: midra_layout::Geometry) -> String { let center = geometry.center(); format!( @@ -575,6 +696,58 @@ mod tests { } } #[test] + fn linked_load_is_atomic_undoable_and_keeps_links() { + let snapshot = linked_test_snapshot(); + let mut layout = midra_layout::Layout::default(); + let link = midra_layout::MidraLink { + screen: "1".into(), + layer: "2".into(), + }; + layout.set_midra_link(0, Some(link.clone())).unwrap(); + let id = layout.layer_info(0).id; + let before = layout.layers().clone(); + let bad = midra_layout::MidraLink { + screen: "9".into(), + layer: "1".into(), + }; + assert!( + load_linked_layers(&mut layout, &snapshot, &[(id, link.clone()), (id, bad)]).is_err() + ); + assert_eq!(*layout.layers(), before); + load_linked_layers(&mut layout, &snapshot, &[(id, link.clone())]).unwrap(); + assert_eq!(layout.layers()[0], snapshot.screens[0].layers[1].geometry); + assert_eq!(layout.layers()[1], before[1]); + assert_eq!(layout.layer_info(0).midra_link, Some(link.clone())); + layout.undo(); + assert_eq!(*layout.layers(), before); + layout.set_locked(0, true); + assert!(load_linked_layers(&mut layout, &snapshot, &[(id, link)]).is_err()); + assert_eq!(*layout.layers(), before); + } + #[test] + fn linked_load_finishes_from_fresh_read_without_sending() { + let mut app = Editor::default(); + let link = midra_layout::MidraLink { + screen: "1".into(), + layer: "1".into(), + }; + app.layout.set_midra_link(0, Some(link.clone())).unwrap(); + app.device.loading = Some(vec![(app.layout.layer_info(0).id, link)]); + let (tx, rx) = mpsc::channel(); + app.device.pending = Some(rx); + let snapshot = linked_test_snapshot(); + let expected = snapshot.screens[0].layers[0].geometry; + tx.send(Ok(snapshot)).unwrap(); + app.device.poll(); + app.finish_linked_load(); + assert_eq!(app.layout.layers()[0], expected); + assert!(app.device.loading.is_none()); + assert!(app.device.pending.is_none()); + assert!(!app.device.sending); + assert!(!app.device.open); + assert!(app.device.message.contains("Loaded 1")); + } + #[test] fn all_linked_plans_include_hidden_locked_unselected_layers_across_screens() { let mut snapshot = linked_test_snapshot(); let second = snapshot.raw["device"]["screenList"]["items"]["1"].clone(); diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index c506577..01b86bc 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -2429,16 +2429,14 @@ impl Editor { self.midra_send_button(ui); }, ); - ink.text( - 382.0, - 10.0, - if !self.layout.selected_indices().is_empty() { - "Shift-click or drag a box to select layers" - } else { - "Select a layer to move or paste" + ui.scope_builder( + egui::UiBuilder::new().max_rect(Rect::from_min_size( + r.min + Vec2::new(382.0, 5.0), + Vec2::new(145.0, 25.0), + )), + |ui| { + self.midra_load_button(ui, true); }, - 12.0, - palette.muted, ); ink.text( r.width() - 278.0, diff --git a/crates/midra-layout/src/project_ui.rs b/crates/midra-layout/src/project_ui.rs index fe8c59a..5a8c03d 100644 --- a/crates/midra-layout/src/project_ui.rs +++ b/crates/midra-layout/src/project_ui.rs @@ -143,6 +143,7 @@ impl Editor { pub(super) fn export_menu(&mut self, ui: &mut egui::Ui) { self.midra_apply_button(ui); self.midra_send_button(ui); + self.midra_load_button(ui, true); if ui.button("Configure Midra export…").clicked() { self.device.prepare_send = true; self.device.open = true; diff --git a/docs/interfaces/midra-device-send.md b/docs/interfaces/midra-device-send.md index b8b7027..0c13b8c 100644 --- a/docs/interfaces/midra-device-send.md +++ b/docs/interfaces/midra-device-send.md @@ -94,3 +94,15 @@ alone does not prove the physical video output updated. The trigger itself is no included in geometry comparisons, because it is a command rather than geometry. **Send all linked layers** sends immediately using saved links and the current target and sizing settings, without opening the export window. The button shows progress and its tooltip reports completion. Invalid links or send failures open the export window with the error. + +**Load linked layers**, beside the connection status and in Export, reads fresh +Midra Preview geometry and crop framing into all linked editor layers, regardless +of selection. **Apply changes… → Load mapped layers** reads only the selected +editor layers using the links chosen in that window. Both actions are read-only +on the device and always load Preview (independent of the send target). They do +not recall device memories or presets. Loading is one atomic Undo step; links +remain saved. Missing screens/layers, mismatched output resolutions, unsupported +geometry, and hidden or locked editor destinations reject the complete import +without changing the document. Unlock/show destinations before loading. Results +appear in the transfer window or the load button's status tooltip. No project or +settings schema changes are involved. diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 6015bd3..48f04da 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -631,3 +631,15 @@ Destination choices in **Configure Midra export…** now save immediately on the Every explicit send now includes WebRCS’s preset apply pulse (`xUpdate` false → true). This applies staged geometry when automatic updates are disabled, including a repeat send where WebRCS already shows the desired values. **Send all linked layers** sends immediately using saved links and the current target and sizing settings, without opening the export window. The button shows progress and its tooltip reports completion. Invalid links or send failures open the export window with the error. + +**Load linked layers**, beside the connection status and in Export, reads fresh +Midra Preview geometry and crop framing into all linked editor layers, regardless +of selection. **Apply changes… → Load mapped layers** reads only the selected +editor layers using the links chosen in that window. Both actions are read-only +on the device and always load Preview (independent of the send target). They do +not recall device memories or presets. Loading is one atomic Undo step; links +remain saved. Missing screens/layers, mismatched output resolutions, unsupported +geometry, and hidden or locked editor destinations reject the complete import +without changing the document. Unlock/show destinations before loading. Results +appear in the transfer window or the load button's status tooltip. No project or +settings schema changes are involved.