Capture preview stills on input selection

This commit is contained in:
Dandaedre 2026-11-14 18:52:04 +01:00
parent 2388d9e4c0
commit 841ade21e6
4 changed files with 162 additions and 9 deletions

View File

@ -1,5 +1,6 @@
use super::*;
use midra_layout::device_capture;
type CaptureTarget = (usize, usize, u32, String);
type CaptureJob = std::sync::mpsc::Receiver<Result<super::super::reference::Reference, String>>;
fn input_label(inputs: &[device_capture::Input], selected: u32) -> String {
@ -53,6 +54,7 @@ struct LayerPreview {
source: usize,
texture: Option<egui::TextureHandle>,
automatic: bool,
capture_on_selection: bool,
name: String,
enabled: bool,
guides: f32,
@ -79,10 +81,11 @@ pub(super) struct CaptureUi {
target_layer: Option<usize>,
keep_refreshing: bool,
show_empty_inputs: bool,
capture_on_selection: bool,
layers: std::collections::BTreeMap<usize, LayerPreview>,
host: String,
pending: Option<CaptureJob>,
pending_layer: Option<(usize, usize, u32, String)>,
pending_layer: Option<CaptureTarget>,
preview: Option<super::super::reference::Reference>,
error: String,
last_request: Option<std::time::Instant>,
@ -91,6 +94,7 @@ pub(super) struct CaptureUi {
impl CaptureUi {
pub(super) fn clear_preview(&mut self) {
self.preview = None;
self.capture_on_selection = false;
self.layers.clear();
self.open = false;
self.discard_result = true;
@ -107,6 +111,46 @@ impl Editor {
self.capture.open = true;
}
// Selection captures share the manual five-second throttle, not the refresh interval.
// Keep only the latest input per target while another request is in flight.
fn next_selection_capture(&mut self) -> Option<(u32, Option<CaptureTarget>)> {
let available = self
.device
.snapshot
.as_ref()
.map(device_capture::inputs)
.unwrap_or_default();
let valid_input =
|input| self.device.online() && available.iter().any(|a| a.id == input && a.enabled);
self.capture.capture_on_selection &= self.capture.open && valid_input(self.capture.input);
for (id, preview) in &mut self.capture.layers {
preview.capture_on_selection &= preview.enabled
&& preview.host == self.device.host
&& valid_input(preview.input)
&& (0..self.layout.layers().len()).any(|i| {
self.layout.layer_info(i).id == *id
&& self.layout.formats().source_for[i] == preview.source
});
}
if self.capture.pending.is_some() || self.capture.cooling_down() {
return None;
}
if self.capture.capture_on_selection {
self.capture.capture_on_selection = false;
return Some((self.capture.input, None));
}
for (id, preview) in &mut self.capture.layers {
if preview.capture_on_selection {
preview.capture_on_selection = false;
return Some((
preview.input,
Some((*id, preview.source, preview.input, preview.host.clone())),
));
}
}
None
}
fn next_preview_refresh(&self) -> Option<u32> {
if self.capture.open
|| !self.device.online()
@ -209,6 +253,7 @@ impl Editor {
name: String::new(),
enabled: false,
automatic: false,
capture_on_selection: false,
guides: 0.35,
});
if preview.source != source || preview.host != self.device.host {
@ -217,6 +262,7 @@ impl Editor {
preview.texture = None;
preview.name.clear();
preview.automatic = false;
preview.capture_on_selection = false;
}
ui.checkbox(&mut preview.enabled, "Enable input preview");
let mut capture = false;
@ -226,6 +272,8 @@ impl Editor {
if before != preview.input {
preview.texture = None;
preview.name.clear();
preview.capture_on_selection = true;
ui.ctx().request_repaint();
}
let available = online && inputs.iter().any(|input| input.id == preview.input && input.enabled);
capture = ui.add_enabled(available && idle && !cooling, egui::Button::new("Capture still"))
@ -240,7 +288,7 @@ impl Editor {
}
if !preview.name.is_empty() { ui.small(&preview.name); }
if !online { ui.small("Connect to Midra to choose and capture an input."); }
else if preview.input == 0 { ui.small("Choose an input, then capture a still or enable auto-refresh."); }
else if preview.input == 0 { ui.small("Choose an input to capture its first still automatically."); }
else if !available { ui.small("Input snapshots are unavailable. Enable snapshots in WebRCS, then refresh the connection."); }
if !idle { ui.spinner(); ui.small("Fetching an input snapshot…"); }
else if cooling { ui.small("Capture available again shortly."); ui.ctx().request_repaint_after(std::time::Duration::from_secs(1)); }
@ -249,6 +297,11 @@ impl Editor {
ui.colored_label(ui.visuals().error_fg_color, &self.capture.error);
}
if capture {
self.capture
.layers
.get_mut(&id)
.unwrap()
.capture_on_selection = false;
let input = self.capture.layers[&id].input;
self.start_input_capture(ui.ctx(), input, false);
self.capture.pending_layer = Some((id, source, input, self.device.host.clone()));
@ -258,6 +311,10 @@ impl Editor {
if self.capture.host != self.device.host || !self.device.online() {
self.capture.discard_result = true;
self.capture.preview = None;
self.capture.capture_on_selection = false;
for preview in self.capture.layers.values_mut() {
preview.capture_on_selection = false;
}
self.capture.host = self.device.host.clone();
}
if let Some(rx) = &self.capture.pending {
@ -314,6 +371,10 @@ impl Editor {
}
}
}
if let Some((input, direct)) = self.next_selection_capture() {
self.start_input_capture(ctx, input, false);
self.capture.pending_layer = direct;
}
if let Some(input) = self.next_preview_refresh() {
self.start_input_capture(ctx, input, true);
}
@ -321,7 +382,8 @@ impl Editor {
.capture
.layers
.values()
.any(|p| p.enabled && p.automatic)
.any(|p| p.enabled && (p.automatic || p.capture_on_selection))
|| self.capture.capture_on_selection
{
ctx.request_repaint_after(std::time::Duration::from_secs(1));
}
@ -336,6 +398,7 @@ impl Editor {
.map(device_capture::inputs)
.unwrap_or_default();
if !inputs.iter().any(|i| i.id == self.capture.input) {
let before = self.capture.input;
self.capture.input = inputs
.iter()
.find(|i| i.signal && i.enabled)
@ -346,6 +409,10 @@ impl Editor {
})
.map(|i| i.id)
.unwrap_or(0);
if before != self.capture.input && self.capture.preview.is_none() {
self.capture.capture_on_selection = true;
ctx.request_repaint();
}
}
if self.capture.target_layer.is_some_and(|id| {
!(0..self.layout.layers().len()).any(|i| self.layout.layer_info(i).id == id)
@ -363,7 +430,7 @@ impl Editor {
ui.add_enabled_ui(self.capture.pending.is_none(), |ui| {
input_picker(ui, "import-snapshot-input", &mut self.capture.input, &inputs, &mut self.capture.show_empty_inputs);
});
if before != self.capture.input { self.capture.preview = None; self.capture.pending = None; self.capture.error.clear(); }
if before != self.capture.input { self.capture.preview = None; self.capture.capture_on_selection = true; self.capture.error.clear(); ctx.request_repaint(); }
let input = inputs.iter().find(|i| i.id == self.capture.input);
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.");
@ -416,6 +483,7 @@ impl Editor {
self.capture.discard_result = true;
}
if capture {
self.capture.capture_on_selection = false;
self.start_input_capture(ctx, self.capture.input, false);
}
if use_layer
@ -432,6 +500,7 @@ impl Editor {
source: self.layout.formats().source_for[i],
texture: Some(reference.texture.clone()),
automatic: self.capture.keep_refreshing,
capture_on_selection: false,
name: reference.name.clone(),
enabled: true,
guides: 0.35,
@ -506,6 +575,22 @@ mod tests {
click_text(&mut app, &ctx, "Input 3");
assert!(app.capture.layers[&id].enabled);
assert_eq!(app.capture.layers[&id].input, 3);
assert!(app.capture.layers[&id].capture_on_selection);
assert_eq!(app.next_selection_capture(), None); // five-second throttle
app.capture.refresh.seconds = 300;
app.capture.last_request =
Some(std::time::Instant::now() - std::time::Duration::from_secs(6));
// An in-flight request still blocks the selection capture, even after cooldown.
let (_tx, rx) = std::sync::mpsc::channel();
app.capture.pending = Some(rx);
assert_eq!(app.next_selection_capture(), None);
app.capture.pending = None;
assert_eq!(
app.next_selection_capture(),
Some((3, Some((id, source, 3, app.device.host.clone()))))
);
assert_eq!(app.next_selection_capture(), None); // one still, not recurring
app.capture.last_request = Some(std::time::Instant::now());
assert!(app.layer_preview(i).is_none());
let output = frame(&mut app, &ctx, vec![]);
let y = |label| {
@ -554,6 +639,56 @@ mod tests {
assert_eq!(app.capture.layers[&id].name, "recaptured still");
}
#[test]
fn selection_capture_coalesces_and_cancels_invalid_targets() {
let ctx = egui::Context::default();
let mut app = Editor::default();
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}}},
"INPUT_7":{"status":{"pp":{"isAvailable":true,"plug":"1"}},"snapshot":{"pp":{"enable":true}}}
}}}}),
});
app.capture.host = app.device.host.clone();
app.capture.last_request = Some(std::time::Instant::now());
app.capture.open = true;
app.capture.input = 3;
app.capture.capture_on_selection = true;
assert_eq!(app.next_selection_capture(), None);
app.capture.input = 7; // only the most recent selection is captured
app.capture.last_request = None;
assert_eq!(app.next_selection_capture(), Some((7, None)));
assert_eq!(app.next_selection_capture(), None);
app.capture.capture_on_selection = true;
app.capture.open = false;
assert_eq!(app.next_selection_capture(), None);
let i = app.layout.selected().unwrap();
let id = app.layout.layer_info(i).id;
app.format_draft = Some(FormatDraft::new(&app.layout, i));
app.property_tab = PropertyTab::Preview;
frame(&mut app, &ctx, vec![]);
let p = app.capture.layers.get_mut(&id).unwrap();
p.enabled = true;
p.input = 3;
p.capture_on_selection = true;
p.host = "different-host".into();
assert_eq!(app.next_selection_capture(), None);
let p = app.capture.layers.get_mut(&id).unwrap();
p.host = app.device.host.clone();
p.capture_on_selection = true;
p.enabled = false;
assert_eq!(app.next_selection_capture(), None);
let p = app.capture.layers.get_mut(&id).unwrap();
p.enabled = true;
p.capture_on_selection = true;
p.source += 1;
assert_eq!(app.next_selection_capture(), None);
app.capture.capture_on_selection = true;
app.capture.clear_preview();
assert!(!app.capture.capture_on_selection);
}
#[test]
fn automatic_capture_updates_only_opted_in_layers_even_when_inputs_match() {
let ctx = egui::Context::default();
let mut app = Editor::default();
@ -574,6 +709,7 @@ mod tests {
name: String::new(),
enabled: true,
automatic: i == 0,
capture_on_selection: false,
guides: 0.35,
},
);
@ -848,6 +984,7 @@ mod tests {
Default::default(),
)),
automatic: true,
capture_on_selection: false,
name: String::new(),
enabled: true,
guides: 0.35,
@ -901,6 +1038,7 @@ mod tests {
source: app.layout.formats().source_for[i],
texture: Some(texture),
automatic: false,
capture_on_selection: false,
name: "Input still".into(),
enabled: true,
guides: 0.25,

View File

@ -13,7 +13,7 @@ The connected snapshot's `/device/inputList/items/INPUT_N` supplies:
- `snapshot.pp.enable`: capture is disabled in the editor when WebRCS snapshots
are disabled. The application does not write this field or snapshot size.
Each explicit **Capture still** or automatic timer request issues
Each input-selection capture, explicit **Capture still**, or automatic timer request issues
`GET http://<configured-IP>/api/device/snapshots/inputs/N`, with N an integer
1–16 and `Cache-Control: no-cache`. IPv4 and IPv6 literal addresses are accepted;
no proxy or redirect is followed. No immediate retries, WebSocket subscription,
@ -76,7 +76,7 @@ local input hides that assignment; opening/New/Recovery clears assignments.
Reordering layers does not move an assignment to another layer.
In the layer Preview tab, the workflow is **Enable input preview → Input →
Capture still → Auto-refresh this preview**. Enable and auto-refresh are separate
automatic first still → optional Capture still / Auto-refresh this preview**. Enable and auto-refresh are separate
per-layer session booleans. An enabled preview may have an input selected but no
texture yet. Capture still directly replaces only that layer's texture; repeating
it recaptures. Auto-refresh can fetch the first image without a manual capture.
@ -107,7 +107,16 @@ local inputs and the current device address are eligible; unavailable or
snapshot-disabled inputs are skipped. Unique inputs refresh round-robin, one
request per interval, sharing the returned texture only across matching automatic
assignments. Manual stills are never replaced by another layer's automatic refresh.
Manual and automatic capture share one worker slot and the five-second minimum.
Selecting an input queues a one-shot capture in either picker, including the first
signal-bearing input selected when opening snapshot import. Selection captures
use the shared five-second minimum rather than waiting the configured refresh
interval, and take priority over periodic refreshes. While cooling down or fetching,
only the latest choice per target is retained. Closing snapshot import, disabling a
preview, changing its source/device or removing its layer cancels its queued request.
Snapshot-disabled/unavailable inputs are not requested. A failed selection capture
is not retried automatically unless auto-refresh is enabled. Capture still remains
available for explicit recapture; importing the dialog image remains explicit.
Manual, selection and automatic capture share one worker slot and the five-second minimum.
Automatic capture pauses offline, during project loading and while the capture
dialog is open. Errors retain the last good image and wait for the next interval;
there are no immediate retries. Turning off refresh prevents applying an in-flight

View File

@ -1011,8 +1011,12 @@ For a layer, open **Layer properties → Preview** (or right-click → **Input p
a **No signal** label. The same filter applies to the snapshot-import picker.
Signal indicators reflect the last connection refresh. This only chooses an image source; it
does not change Midra input routing or the editor's source-format identity.
3. Click **Capture still**, directly below the input selector. The image loads
straight into this layer. Click it again to replace the still.
3. Selecting an input captures a first still automatically and loads it straight
into this layer. It waits only for the shared five-second request throttle, not
the auto-refresh interval. Click **Capture still**, directly below the input
selector, to replace the still. Selecting a different input queues its first
capture too; rapid changes keep only the latest choice. The snapshot-import
picker also captures on selection, but still requires an explicit import.
4. Alternatively, enable **Auto-refresh this preview** below Capture. It fetches
the first image automatically and then updates it on the timer.

View File

@ -4,6 +4,8 @@
## Unreleased
- Capture a first still automatically when selecting a preview input, in both pickers. Queue the latest choice behind the shared five-second throttle and active request, without waiting the auto-refresh cycle; retain explicit recapture/import. Regression tests cover throttling, coalescing and cancelling stale targets.
- Hide no-signal inputs by default in both preview pickers; add a shared session-only Show inputs without signal toggle and explicit No signal labels. Preserve existing choices when filtering or signal loss occurs, and leave initial selection empty when no input has signal. UI tests cover both pickers and the empty state.
- 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.