Add timed automatic refresh for Midra input previews
Beffroi/beffroi-tools/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2026-11-14 18:05:59 +01:00
parent 8747ce5271
commit 02ca8af9e2
5 changed files with 260 additions and 33 deletions

View File

@ -3,15 +3,34 @@ use midra_layout::device_capture;
type CaptureJob = std::sync::mpsc::Receiver<Result<super::super::reference::Reference, String>>;
struct LayerPreview {
input: u32,
host: String,
source: usize,
texture: egui::TextureHandle,
name: String,
enabled: bool,
guides: f32,
}
struct Refresh {
enabled: bool,
seconds: u64,
last_input: u32,
pending_input: Option<u32>,
}
impl Default for Refresh {
fn default() -> Self {
Self {
enabled: false,
seconds: 10,
last_input: 0,
pending_input: None,
}
}
}
#[derive(Default)]
pub(super) struct CaptureUi {
pub open: bool,
refresh: Refresh,
input: u32,
layers: std::collections::BTreeMap<usize, LayerPreview>,
host: String,
@ -28,12 +47,94 @@ impl CaptureUi {
self.open = false;
self.discard_result = true;
}
fn refresh_controls(&mut self, ui: &mut egui::Ui) {
ui.checkbox(&mut self.refresh.enabled, "Auto-refresh input previews");
ui.add_enabled(
self.refresh.enabled,
egui::Slider::new(&mut self.refresh.seconds, 5..=300)
.text("Request interval")
.suffix(" s"),
);
ui.small("Refreshes visible layer previews in turn, one input per interval. Paused offline or while this capture window is open.");
if self.refresh.enabled && !self.error.is_empty() {
ui.colored_label(ui.visuals().error_fg_color, &self.error);
}
}
fn cooling_down(&self) -> bool {
self.last_request
.is_some_and(|t| t.elapsed() < std::time::Duration::from_secs(5))
}
}
impl Editor {
fn next_preview_refresh(&self) -> Option<u32> {
if !self.capture.refresh.enabled
|| self.capture.open
|| !self.device.online()
|| self.capture.pending.is_some()
|| self
.capture
.last_request
.is_some_and(|t| t.elapsed().as_secs() < self.capture.refresh.seconds.max(5))
{
return None;
}
let available = self
.device
.snapshot
.as_ref()
.map(device_capture::inputs)
.unwrap_or_default();
let inputs: std::collections::BTreeSet<_> = (0..self.layout.layers().len())
.filter(|&i| self.layout.visible(i))
.filter_map(|i| {
self.capture
.layers
.get(&self.layout.layer_info(i).id)
.filter(|p| {
p.enabled
&& p.source == self.layout.formats().source_for[i]
&& p.host == self.device.host
&& available.iter().any(|a| a.id == p.input && a.enabled)
})
.map(|p| p.input)
})
.collect();
inputs
.iter()
.copied()
.find(|&i| i > self.capture.refresh.last_input)
.or_else(|| inputs.first().copied())
}
fn start_input_capture(&mut self, ctx: &egui::Context, input: u32, automatic: bool) {
self.capture.last_request = Some(std::time::Instant::now());
self.capture.discard_result = false;
self.capture.error.clear();
self.capture.refresh.pending_input = automatic.then_some(input);
if automatic {
self.capture.refresh.last_input = input;
} else {
self.capture.preview = None;
}
let (tx, rx) = std::sync::mpsc::channel();
self.capture.pending = Some(rx);
let host = self.device.host.clone();
let ctx = ctx.clone();
std::thread::spawn(move || {
let result = device_capture::capture(&host, input).and_then(|bytes| {
super::super::reference::Reference::load(
&ctx,
&egui::DroppedFile {
name: format!("Midra input {input} · {host}"),
bytes: Some(bytes.into()),
..Default::default()
},
)
});
let _ = tx.send(result);
ctx.request_repaint();
});
}
pub(super) fn layer_preview(&self, i: usize) -> Option<(egui::TextureId, f32)> {
self.capture
.layers
@ -82,6 +183,7 @@ impl Editor {
);
ui.small("Capture a still and use it for this layer to adjust guide opacity.");
}
self.capture.refresh_controls(ui);
if ui
.add_enabled(
self.device.online(),
@ -107,16 +209,37 @@ impl Editor {
};
if let Some(result) = result {
self.capture.pending = None;
let automatic = self.capture.refresh.pending_input.take();
if self.capture.discard_result {
self.capture.discard_result = false;
} else {
match result {
Ok(reference) => self.capture.preview = Some(reference),
Ok(reference) => {
if let Some(input) = automatic {
if self.capture.refresh.enabled {
for preview in
self.capture.layers.values_mut().filter(|p| {
p.input == input && p.host == self.device.host
})
{
preview.texture = reference.texture.clone();
}
}
} else {
self.capture.preview = Some(reference);
}
}
Err(error) => self.capture.error = error,
}
}
}
}
if let Some(input) = self.next_preview_refresh() {
self.start_input_capture(ctx, input, true);
}
if self.capture.refresh.enabled {
ctx.request_repaint_after(std::time::Duration::from_secs(1));
}
if !self.capture.open {
return;
}
@ -162,7 +285,8 @@ impl Editor {
if ui.button("Cancel capture").clicked() { self.capture.discard_result = true; }
}
});
ui.small("Manual capture only · at most one request every 5 seconds · no background refresh.");
ui.small("At most one request at a time, with at least 5 seconds between requests.");
self.capture.refresh_controls(ui);
if cooling_down { ui.small("Capture available again shortly."); ctx.request_repaint_after(std::time::Duration::from_secs(1)); }
if let Some(reference) = &self.capture.preview {
ui.label(format!("{} · {} × {} px", reference.name, reference.size[0], reference.size[1]));
@ -180,29 +304,7 @@ impl Editor {
self.capture.discard_result = true;
}
if capture {
self.capture.last_request = Some(std::time::Instant::now());
self.capture.discard_result = false;
self.capture.preview = None;
self.capture.error.clear();
let (tx, rx) = std::sync::mpsc::channel();
self.capture.pending = Some(rx);
let host = self.device.host.clone();
let input = self.capture.input;
let ctx = ctx.clone();
std::thread::spawn(move || {
let result = device_capture::capture(&host, input).and_then(|bytes| {
super::super::reference::Reference::load(
&ctx,
&egui::DroppedFile {
name: format!("Midra input {input} · {host}"),
bytes: Some(bytes.into()),
..Default::default()
},
)
});
let _ = tx.send(result);
ctx.request_repaint();
});
self.start_input_capture(ctx, self.capture.input, false);
}
if use_layer
&& let Some(i) = self.layout.selected()
@ -211,6 +313,8 @@ impl Editor {
self.capture.layers.insert(
self.layout.layer_info(i).id,
LayerPreview {
input: self.capture.input,
host: self.device.host.clone(),
source: self.layout.formats().source_for[i],
texture: reference.texture.clone(),
name: reference.name.clone(),
@ -286,7 +390,63 @@ mod tests {
assert_eq!(app.layer_preview(selected).unwrap().1, 0.35);
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);
app.capture.refresh.enabled = true;
assert_eq!(app.next_preview_refresh(), None);
app.capture.last_request =
Some(std::time::Instant::now() - std::time::Duration::from_secs(11));
assert_eq!(app.next_preview_refresh(), Some(3));
app.capture.refresh.last_input = 3;
assert_eq!(app.next_preview_refresh(), Some(3)); // round-robin wraps
let id = app.layout.layer_info(selected).id;
app.capture.layers.get_mut(&id).unwrap().enabled = false;
assert_eq!(app.next_preview_refresh(), None);
app.capture.layers.get_mut(&id).unwrap().enabled = true;
app.capture.layers.get_mut(&id).unwrap().host = "other device".into();
assert_eq!(app.next_preview_refresh(), None);
app.capture.layers.get_mut(&id).unwrap().host = app.device.host.clone();
// Failed refresh retains the last good texture and obeys the timer.
let texture = app.layer_preview(selected).unwrap().0;
let (tx, rx) = std::sync::mpsc::channel();
app.capture.pending = Some(rx);
app.capture.refresh.pending_input = Some(3);
app.capture.last_request = Some(std::time::Instant::now());
assert_eq!(app.next_preview_refresh(), None);
tx.send(Err("temporary failure".into())).unwrap();
frame(&mut app, &ctx, vec![]);
assert_eq!(app.layer_preview(selected).unwrap().0, texture);
assert_eq!(app.capture.error, "temporary failure");
assert!(app.references.is_empty());
// A successful automatic result updates the texture, never the references.
let (tx, rx) = std::sync::mpsc::channel();
app.capture.pending = Some(rx);
app.capture.refresh.pending_input = Some(3);
tx.send(Ok(app.capture.preview.take().unwrap())).unwrap();
frame(&mut app, &ctx, vec![]);
assert!(app.capture.preview.is_none());
assert!(app.references.is_empty());
assert_eq!(app.layer_preview(selected).unwrap().1, 0.35);
app.capture.refresh.enabled = false;
// Restore a dialog still for the separate Add as reference action.
let reference = super::super::super::reference::Reference::load(
&ctx,
&egui::DroppedFile {
name: "Captured input".into(),
bytes: Some({
let mut bytes = std::io::Cursor::new(Vec::new());
image::RgbaImage::new(2, 2)
.write_to(&mut bytes, image::ImageFormat::Png)
.unwrap();
bytes.into_inner().into()
}),
..Default::default()
},
)
.unwrap();
app.capture.preview = Some(reference);
app.capture.open = true;
assert_eq!(app.next_preview_refresh(), None);
click_text(&mut app, &ctx, "Add as reference");
assert_eq!(app.references.len(), 1);
assert_eq!(app.project().references.len(), 1);
@ -296,6 +456,51 @@ mod tests {
assert!(!app.capture.cooling_down());
}
#[test]
fn refresh_rotates_inputs_and_skips_changed_sources() {
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.refresh.enabled = true;
for (i, input) in [3, 7].into_iter().enumerate() {
app.capture.layers.insert(
app.layout.layer_info(i).id,
LayerPreview {
input,
host: app.device.host.clone(),
source: app.layout.formats().source_for[i],
texture: ctx.load_texture(
format!("Input {input}"),
egui::ColorImage::new([2, 2], Color32::RED),
Default::default(),
),
name: String::new(),
enabled: true,
guides: 0.35,
},
);
}
assert_eq!(app.next_preview_refresh(), Some(3));
app.capture.refresh.last_input = 3;
assert_eq!(app.next_preview_refresh(), Some(7));
app.capture.refresh.last_input = 7;
assert_eq!(app.next_preview_refresh(), Some(3));
let id = app.layout.layer_info(0).id;
app.capture.layers.get_mut(&id).unwrap().source = usize::MAX;
assert_eq!(app.next_preview_refresh(), Some(7));
app.capture.open = true;
assert_eq!(app.next_preview_refresh(), None);
app.capture.open = false;
app.device.snapshot = None;
assert_eq!(app.next_preview_refresh(), None);
}
#[test]
fn cancelling_keeps_request_in_flight_and_discards_its_result() {
let ctx = egui::Context::default();
let mut app = Editor::default();
@ -323,6 +528,8 @@ mod tests {
app.capture.layers.insert(
id,
LayerPreview {
input: 3,
host: app.device.host.clone(),
source: app.layout.formats().source_for[i],
texture,
name: "Input still".into(),

View File

@ -13,10 +13,10 @@ 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.
One explicit **Capture still** issues
Each 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 retries, polling, WebSocket subscription,
no proxy or redirect is followed. No immediate retries, WebSocket subscription,
still-library capture or source-routing/preset writes are involved.
Only one request may be in flight, with at least five seconds between starts.
@ -66,4 +66,17 @@ Layer-preview assignments, texture bytes, visibility toggles and guide opacity
are not saved/exported. Add as reference remains the separate persistent path;
generic artwork exports include those references only through the existing
background option. Reloading a project makes no capture requests.
Automatic refresh, live feeds and separate image-file saving are not implemented.
**Auto-refresh input previews** is opt-in and session-only, with a request interval
of 5–300 seconds (default 10). Assignments also retain the device address and
physical input number in memory. Visible layers with enabled previews, unchanged
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 across their assignments.
Manual 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
automatic result. Project replacement clears assignments and discards stale results.
Automatic refresh never changes fixed references or device settings. Timer settings
are not persisted; no save schema changes are involved.
Live feeds and separate image-file saving are not implemented.

View File

@ -1010,7 +1010,7 @@ indicators reflect the last connection refresh; an inactive input may return a
blank image. If input snapshots are disabled, enable them in WebRCS and refresh
the connection.
Capture is manual: no background polling, no live stream, one request in flight
Capture defaults to manual, with one request in flight
and a minimum five-second gap between requests. Cancel/close ignores the response;
another request waits for the old one to finish or time out (20 seconds).
The returned resolution/freshness comes from the device; this is not a full-size
@ -1020,7 +1020,12 @@ input and dynamic crop/zoom. In **Crop properties**, or right-click → **Input
& crop guides**, toggle **Show captured input** and adjust **Crop guide opacity**.
Guides paint over the image, initially at 35%; without an enabled preview, guides
are fully opaque and the slider is disabled. Selection handles stay fully visible.
Layer previews/settings are session-only and do not trigger refreshes or device writes.
Enable **Auto-refresh input previews** here or in the capture dialog to fetch
updated stills automatically. **Request interval** defaults to 10 seconds (5–300).
Visible, enabled previews refresh one input at a time; layers sharing an input
share the image. The timer pauses offline and while the capture dialog is open.
Failed requests retain the last image. Fixed reference images never auto-refresh.
Layer previews and timer settings are session-only; no device settings are changed.
**Add as reference** instead creates a fixed screen guide with independent
hide/show/remove controls, embedded in saved projects. Neither action changes

View File

@ -4,6 +4,8 @@
## Unreleased
- Add opt-in input-preview auto-refresh with a 5–300 second request interval (default 10). Visible previews refresh round-robin with shared input textures, one background request at a time; pause offline/manual capture, retain images on failures and keep fixed references unchanged. Timer controls and assignments remain session-only. Regression checks cover scheduling, throttling and response handling.
- Use captured input stills as toggleable layer previews, with crop guides composited above them and a 0–100% opacity slider in Crop properties and the layer context menu. Guides return to full opacity without an enabled preview; source/input/dynamic crop and ghost overflow share the same mapping. Layer-preview assignments/settings are session-only; capture remains manual and throttled.
- Group the guide and supporting documentation in this tool’s folder; update portal, cross-document and packaging links for the new paths.

View File

@ -35,8 +35,8 @@ device readback and physical-screen acceptance.
autosave pauses while loading.
- [x] Explicit Midra input snapshot capture: available-input picker, still
preview, Use for selected layer, preview visibility and guide-opacity controls,
plus Add as reference. Manual only, one request at a time and at least
five seconds between requests. No live polling or automatic refresh.
plus Add as reference. Optional timed refresh (default 10 seconds), one request
at a time and at least five seconds between requests; visible previews only.
- [x] Clipboard/import mapping review with per-row target/ignore, independent
size/position/available-zoom toggles, draw.io stroke handling and own-copy
round trips. Real draw.io clipboard sample and draw.io-exported SVG supported.
@ -77,7 +77,7 @@ device readback and physical-screen acceptance.
- [ ] **Non-Centered/masked Midra layer import.** Expand conversion only after
verifying those semantics. Source routing synchronization remains separate
from the opt-in shared-input settings editor.
- [ ] **Optional capture extensions, if wanted.** Automatic refresh, standalone
- [ ] **Optional capture extensions, if wanted.** Standalone
capture saving and persistence of layer-preview assignments are not implemented.
Captures can be session-only layer textures or saved fixed reference images;
low device load remains a requirement for any refresh extension.