Import input snapshots into chosen layers or fixed references

This commit is contained in:
Dandaedre 2026-11-14 18:41:49 +01:00
parent 5c81657d39
commit a281d48315
6 changed files with 114 additions and 24 deletions

View File

@ -31,6 +31,8 @@ pub(super) struct CaptureUi {
pub open: bool,
refresh: Refresh,
input: u32,
target_layer: Option<usize>,
keep_refreshing: bool,
layers: std::collections::BTreeMap<usize, LayerPreview>,
host: String,
pending: Option<CaptureJob>,
@ -53,6 +55,12 @@ impl CaptureUi {
}
}
impl Editor {
pub(super) fn open_snapshot_import(&mut self) {
self.capture.target_layer = self.layout.selected().map(|i| self.layout.layer_info(i).id);
self.capture.keep_refreshing = false;
self.capture.open = true;
}
fn next_preview_refresh(&self) -> Option<u32> {
if self.capture.open
|| !self.device.online()
@ -295,12 +303,17 @@ impl Editor {
.map(|i| i.id)
.unwrap_or(0);
}
if self.capture.target_layer.is_some_and(|id| {
!(0..self.layout.layers().len()).any(|i| self.layout.layer_info(i).id == id)
}) {
self.capture.target_layer = None;
}
let mut open = true;
let mut capture = false;
let mut add = false;
let mut use_layer = false;
egui::Window::new("Capture Midra input").open(&mut open).default_width(560.0).collapsible(false).show(ctx, |ui| {
ui.label("Capture a still input preview for a selected layer or add it as a fixed reference image.");
egui::Window::new("Import input snapshot").open(&mut open).default_width(560.0).collapsible(false).show(ctx, |ui| {
ui.label("Capture an input snapshot, then import it into a layer or add it as a fixed reference image.");
ui.small("Snapshots are reduced-resolution images, not a live feed. Their size depends on the device's snapshot settings.");
let before = self.capture.input;
ui.add_enabled_ui(self.capture.pending.is_none(), |ui| {
@ -315,6 +328,22 @@ impl Editor {
if input.is_some_and(|i| !i.enabled) {
ui.label("Snapshots are disabled for this input. Enable input snapshots in WebRCS and refresh the connection.");
}
ui.label("Destination");
egui::ComboBox::from_id_salt("snapshot-destination").selected_text(
self.capture.target_layer.and_then(|id| (0..self.layout.layers().len()).find(|&i| self.layout.layer_info(i).id == id))
.map(|i| format!("Layer: {}", self.layout.layer_label(i))).unwrap_or_else(|| "Reference image".into())
).show_ui(ui, |ui| {
ui.selectable_value(&mut self.capture.target_layer, None, "Reference image");
for i in 0..self.layout.layers().len() {
ui.selectable_value(&mut self.capture.target_layer, Some(self.layout.layer_info(i).id), format!("Layer: {}", self.layout.layer_label(i)));
}
});
if self.capture.target_layer.is_some() {
ui.checkbox(&mut self.capture.keep_refreshing, "Keep refreshing this layer");
ui.add_enabled(self.capture.keep_refreshing, egui::Slider::new(&mut self.capture.refresh.seconds, 5..=300).text("Request interval").suffix(" s"));
} else {
ui.small("Reference images are fixed stills and never auto-refresh.");
}
ui.horizontal(|ui| {
capture = ui.add_enabled(!cooling_down && self.capture.pending.is_none() && input.is_some_and(|i| i.enabled), egui::Button::new("Capture still")).clicked();
if self.capture.pending.is_some() {
@ -328,11 +357,14 @@ impl Editor {
if let Some(reference) = &self.capture.preview {
ui.label(format!("{} · {} × {} px", reference.name, reference.size[0], reference.size[1]));
ui.add(egui::Image::new(&reference.texture).max_size(Vec2::new(512.0, 320.0)));
if let Some((severe, warning)) = reference.warning(self.layout.formats().output) {
if self.capture.target_layer.is_none() && let Some((severe, warning)) = reference.warning(self.layout.formats().output) {
ui.colored_label(if severe { ui.visuals().error_fg_color } else { theme::palette(ctx).amber }, warning);
}
use_layer = ui.add_enabled(self.layout.selected().is_some(), egui::Button::new("Use for selected layer")).clicked();
add = ui.button("Add as reference").clicked();
if self.capture.target_layer.is_some() {
use_layer = ui.button("Import into layer").clicked();
} else {
add = ui.button("Add as reference").clicked();
}
}
if !self.capture.error.is_empty() { ui.colored_label(ui.visuals().error_fg_color, &self.capture.error); }
});
@ -347,7 +379,9 @@ impl Editor {
self.start_input_capture(ctx, self.capture.input, false);
}
if use_layer
&& let Some(i) = self.layout.selected()
&& let Some(id) = self.capture.target_layer
&& let Some(i) =
(0..self.layout.layers().len()).find(|&i| self.layout.layer_info(i).id == id)
&& let Some(reference) = &self.capture.preview
{
self.capture.layers.insert(
@ -357,13 +391,12 @@ impl Editor {
host: self.device.host.clone(),
source: self.layout.formats().source_for[i],
texture: Some(reference.texture.clone()),
automatic: false,
automatic: self.capture.keep_refreshing,
name: reference.name.clone(),
enabled: true,
guides: 0.35,
},
);
self.edit_content = true;
self.capture.open = false;
}
if add && let Some(reference) = self.capture.preview.take() {
@ -525,6 +558,49 @@ mod tests {
assert_eq!(app.layer_preview(0).unwrap(), before);
}
#[test]
fn snapshot_import_targets_any_layer_or_a_fixed_reference_without_selection() {
let ctx = egui::Context::default();
let mut app = Editor::default();
app.device.host = "127.0.0.1".into();
app.capture.host = app.device.host.clone();
app.device.snapshot = Some(midra_layout::device::Snapshot {
identity: "Test".into(),
screens: vec![],
raw: serde_json::json!({"device":{"inputList":{"items":{
"INPUT_3":{"status":{"pp":{"isAvailable":true,"plug":"1"}},"snapshot":{"pp":{"enable":true}}}
}}}}),
});
app.layout.select(None);
app.capture.last_request = Some(std::time::Instant::now());
for (i, automatic) in [(0, true), (1, false)] {
app.open_snapshot_import();
app.capture.preview = Some(tiny_reference(&ctx, "imported snapshot"));
click_text(&mut app, &ctx, "Reference image");
let label = format!("Layer: {}", app.layout.layer_label(i));
click_text(&mut app, &ctx, &label);
if automatic {
click_text(&mut app, &ctx, "Keep refreshing this layer");
}
click_text(&mut app, &ctx, "Import into layer");
let id = app.layout.layer_info(i).id;
assert_eq!(app.capture.layers[&id].automatic, automatic);
assert!(app.layer_preview(i).is_some());
assert!(app.layout.selected_indices().is_empty());
assert!(app.references.is_empty());
}
app.open_snapshot_import();
app.capture.keep_refreshing = true; // ignored for a reference destination
app.capture.preview = Some(tiny_reference(&ctx, "fixed reference"));
let output = frame(&mut app, &ctx, vec![]);
assert!(!output.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Text(t) if t.galley.text() == "Keep refreshing this layer")));
let textures = [app.layer_preview(0), app.layer_preview(1)];
click_text(&mut app, &ctx, "Add as reference");
assert_eq!(app.references.len(), 1);
assert_eq!(app.project().references.len(), 1);
assert_eq!([app.layer_preview(0), app.layer_preview(1)], textures);
assert_eq!(app.capture.layers.len(), 2);
}
#[test]
fn capture_is_manual_throttled_and_only_adds_on_request() {
let ctx = egui::Context::default();
let mut app = Editor::default();
@ -535,7 +611,7 @@ mod tests {
raw: serde_json::json!({"device":{"inputList":{"items":{"INPUT_3":{"status":{"pp":{"isAvailable":true,"plug":"1"}},"snapshot":{"pp":{"enable":true}}}}}}}),
});
app.capture.host = app.device.host.clone();
app.capture.open = true;
app.open_snapshot_import();
let mut bytes = std::io::Cursor::new(Vec::new());
image::RgbaImage::from_pixel(2, 2, image::Rgba([255, 0, 0, 255]))
.write_to(&mut bytes, image::ImageFormat::Png)
@ -563,10 +639,10 @@ mod tests {
click_text(&mut app, &ctx, "Capture still");
assert!(app.capture.pending.is_none());
assert_eq!(app.capture.last_request, requested);
click_text(&mut app, &ctx, "Use for selected layer");
click_text(&mut app, &ctx, "Import into layer");
let selected = app.layout.selected().unwrap();
assert_eq!(app.layer_preview(selected).unwrap().1, 0.35);
assert!(app.edit_content);
assert!(!app.edit_content);
assert!(app.references.is_empty());
// Automatic refresh is opt-in, throttled, and pauses for manual capture.
assert_eq!(app.next_preview_refresh(), None);
@ -627,6 +703,7 @@ mod tests {
)
.unwrap();
app.capture.preview = Some(reference);
app.capture.target_layer = None;
app.capture.open = true;
assert_eq!(app.next_preview_refresh(), None);
click_text(&mut app, &ctx, "Add as reference");

View File

@ -63,7 +63,7 @@ impl Editor {
Self::request_layer_paste(ui.ctx());
}
ui.separator();
ui.weak("Screen references");
ui.weak("Images & input snapshots");
self.reference_actions_menu(ui);
}
pub(super) fn snapping_options(&mut self, ui: &mut egui::Ui) {

View File

@ -23,12 +23,12 @@ impl Editor {
if ui
.add_enabled(
self.device.online(),
egui::Button::new("Capture Midra input…"),
egui::Button::new("Import input snapshot…"),
)
.on_disabled_hover_text("Connect to Midra first.")
.clicked()
{
self.capture.open = true;
self.open_snapshot_import();
ui.close_menu();
}
if self.references.is_empty() {

View File

@ -39,10 +39,15 @@ new full-resolution frame. Signal indicators may be stale until reconnect/refres
## Editor and persistence
The dialog displays the image and its actual pixel dimensions. **Add as reference**
**Import input snapshot…** displays the image and its actual pixel dimensions.
Its destination selector lists every editor layer by stable ID and a Reference
image option, independently of current selection. Layer destinations offer
**Keep refreshing this layer**; this is staged until Import into layer and sets
that layer's automatic-refresh flag. Reference destinations never expose or apply
that flag and remain fixed images. Import does not alter selection or geometry. **Add as reference**
is explicit and obeys the 32-image/64 MiB combined-reference limits. Existing
upscale/aspect warnings apply. Capture in this separate import dialog only stages
an image until Use for selected layer or Add as reference is chosen.
an image until Import into layer or Add as reference is chosen.
Changing device address/disconnecting invalidates the pending preview; input changes
clear the old preview. A cancelled/late result cannot become a new reference.
@ -50,7 +55,7 @@ The initial image name contains the input number and configured device IP.
Added stills use the existing [project v10 reference object](midra-layout.md):
name, embedded PNG image_base64, visible, opacity, preserve_ratio,
native_size, offset. No new fields, version or migration are needed.
**Use for selected layer** assigns a session-only still texture to the selected
**Import into layer** assigns a session-only still texture to the destination
editor layer's stable ID and current local input. It uses the editor's native
source rectangle, input crop and dynamic crop/zoom mapping, including faded
overflow while editing. It does not infer or change device routing. Changing the

View File

@ -1028,13 +1028,19 @@ pauses offline and while the separate capture/import dialog is open. Reduced
snapshot resolution and freshness are controlled by the device. If snapshots
are disabled for an input, enable them in WebRCS and refresh the connection.
For fixed screen references, **Import → Capture Midra input…** opens the separate
capture dialog. Choose an input, capture and inspect the image, then **Add as
reference**. This embeds a fixed guide in the project; it never auto-refreshes.
The dialog also retains **Use for selected layer**. Existing size/aspect warnings
apply to reference images. Cancel/close discards a pending dialog capture, and
another request waits for completion or the 20-second timeout. No capture action
changes device settings or routing. See the [input preview contract](../interfaces/midra-input-preview.md).
**Import → Import input snapshot…** opens a capture/import window (also available
from the reference-images context menu). Choose the physical input and a
**Destination**: any editor layer, or **Reference image**. No layer selection is
required. Capture and inspect the still, then click **Import into layer** or
**Add as reference**. Layer destinations offer **Keep refreshing this layer**
and the shared refresh interval. This starts timed updates after import; leaving
it off imports a fixed layer still. Selection and layer geometry are unchanged.
Reference images never offer auto-refresh: they are fixed screen guides embedded
in the project, with the usual size/aspect warnings. Cancel/close discards a pending
dialog capture; another request waits for completion or the 20-second timeout.
No capture action changes device settings or routing. See the
[input preview contract](../interfaces/midra-input-preview.md).
Opening a project or restoring recovery now reads, validates and decodes embedded
references in a worker. The current editor stays intact until the whole project

View File

@ -4,6 +4,8 @@
## Unreleased
- Add Import input snapshot… with an explicit destination picker for any editor layer or a fixed reference image. Layer imports optionally keep refreshing; references never auto-refresh. Preserve selection/geometry and existing snapshot limits; regression tests cover both destinations without a selection.
- Simplify layer previews to Enable → Input → Capture still → Auto-refresh. Capture directly replaces that layer's image; auto-refresh is per layer and can fetch the first image. Preserve serialized low-rate requests, bind manual results to their original target, and keep manual stills unchanged by other layers' refreshes. Regression tests cover control order, recapture, stale results and automatic first capture.
- Move captured-input controls and the image preview into a dedicated Preview properties tab, with a matching right-click entry. Crop keeps its geometry controls; the existing preview context submenu remains available. Tab-switch regression checks include Preview.