diff --git a/apps/deckcraft-web/src/presenter.rs b/apps/deckcraft-web/src/presenter.rs index 60ce92f66..231402aef 100644 --- a/apps/deckcraft-web/src/presenter.rs +++ b/apps/deckcraft-web/src/presenter.rs @@ -10,7 +10,7 @@ use craft::ui::runtime::frame::FrameInput; use craft::ui::shell::{AppShell, embed}; use craft::web_host::link::{LinkLimits, LinkState, WindowLink}; use craft::web_host::popup::{PopupOpener, PopupPeer}; -use deckcraft_ui_egui::presenter::{Preview, Snapshot, SnapshotDelta, merge}; +use deckcraft_ui_egui::presenter::{Command, Preview, Snapshot, SnapshotDelta, merge}; // PLAN: assumed pub const SHOW_SCOPE: craft::ui::runtime::commands::CommandScope in presenter_view (show-w7) use deckcraft_ui_egui::presenter_view::{NextPic, PresenterModel, PreviewPic, SHOW_SCOPE, presenter_body}; use deckcraft_ui_egui::show::{Blank, GridSource}; @@ -46,6 +46,8 @@ enum Picture { pub struct PopupApp { peer: PopupPeer, snapshot: Option, + /// The number of the last delta received. + seq: Option, textures: TextureStore, current_revision: u64, next_revision: u64, @@ -65,6 +67,7 @@ impl PopupApp { Ok(Self { peer, snapshot: None, + seq: None, textures: TextureStore::in_context(ctx, "deckcraft.popup", TextureStoreOptions::frames()), current_revision: 0, next_revision: 0, @@ -76,6 +79,11 @@ impl PopupApp { // take drains only this pass's arrivals; a second pass sees no replay of earlier deltas. for payload in self.peer.take() { let Some(delta) = SnapshotDelta::from_payload(&payload) else { continue }; + if self.seq.is_some_and(|seq| delta.seq != seq.wrapping_add(1)) { + // A full inbox dropped a delta, which may have carried an image: ask for every image again. + let _ = self.peer.post(&Command::Resync.to_payload()); + } + self.seq = Some(delta.seq); if delta.current_changed { self.current_revision = self.current_revision.wrapping_add(1); self.textures.forget(ctx, &Picture::Current); diff --git a/crates/ui-egui/src/presenter.rs b/crates/ui-egui/src/presenter.rs index 4809f246e..29bc8976b 100644 --- a/crates/ui-egui/src/presenter.rs +++ b/crates/ui-egui/src/presenter.rs @@ -74,8 +74,19 @@ pub struct DeltaCache { next: Option, slides: Option>, acknowledged: bool, + sent: u64, } impl DeltaCache { + /// The sequence number the next successful send carries. + pub fn seq(&self) -> u64 { + self.sent + } + + /// The window missed a delta, and with it maybe an image: the next send carries every image again. + pub fn resend(&mut self) { + self.acknowledged = false; + } + /// Sends changed images, acknowledging them only after the transport succeeds. pub fn send(&mut self, mut snapshot: Snapshot, refresh: bool, send: impl FnOnce(Snapshot, bool, bool) -> Result<(), E>) -> Result<(), E> { if refresh { @@ -103,6 +114,7 @@ impl DeltaCache { self.next = next; self.slides = slides; self.acknowledged = true; + self.sent = self.sent.wrapping_add(1); Ok(()) } } @@ -126,6 +138,8 @@ pub enum Command { /// Zero-based slide index. slide: usize, }, + /// The window missed a snapshot (its inbox was full): the audience sends every image again. + Resync, } impl Command { /// Reads an action, refusing malformed messages and nonfinite zoom coordinates. @@ -153,6 +167,10 @@ pub struct SnapshotDelta { pub current_changed: bool, /// Whether to replace the next preview, including replacement with no image. pub next_changed: bool, + /// The send's number ([`DeltaCache::seq`]); a number that does not follow the last one received means a delta + /// was lost. + #[serde(default)] + pub seq: u64, } impl SnapshotDelta { @@ -170,7 +188,7 @@ impl SnapshotDelta { /// Keeps omitted images and grid thumbnails from the previous accepted snapshot. pub fn merge(previous: Option<&Snapshot>, delta: SnapshotDelta) -> Snapshot { - let SnapshotDelta { mut snapshot, current_changed, next_changed } = delta; + let SnapshotDelta { mut snapshot, current_changed, next_changed, .. } = delta; if let Some(previous) = previous { if !current_changed { snapshot.current = previous.current.clone(); @@ -463,7 +481,7 @@ mod tests { incoming.next = None; incoming.elapsed += 1.0; let decode = |snapshot: Snapshot, current_changed, next_changed| { - let delta = SnapshotDelta { snapshot, current_changed, next_changed }; + let delta = SnapshotDelta { snapshot, current_changed, next_changed, seq: 0 }; SnapshotDelta::from_payload(&delta.to_payload()).unwrap() }; let kept = merge(Some(&previous), decode(incoming.clone(), false, false)); @@ -518,7 +536,7 @@ mod tests { ] { assert_eq!(Command::from_payload(&command.to_payload()), Some(command)); } - let envelope = SnapshotDelta { snapshot: snapshot(), current_changed: true, next_changed: false }; + let envelope = SnapshotDelta { snapshot: snapshot(), current_changed: true, next_changed: false, seq: 0 }; let wire = envelope.to_payload(); let decoded = SnapshotDelta::from_payload(&wire).unwrap(); assert_eq!(decoded.snapshot.current, envelope.snapshot.current); diff --git a/crates/ui-egui/src/show/presenter.rs b/crates/ui-egui/src/show/presenter.rs index a5c0673ba..ef5127cbc 100644 --- a/crates/ui-egui/src/show/presenter.rs +++ b/crates/ui-egui/src/show/presenter.rs @@ -193,6 +193,7 @@ fn apply_command(show: &mut Show, doc: &Arc, now: f64, command: Co let action = show.state.goto(doc, slide); return show.apply(action, doc, now); } + Command::Resync => show.remote_images.resend(), _ => {} } true @@ -254,8 +255,9 @@ fn remote_pass( slides: show.presenter_grid.then(|| show.remote_grid.clone()), }; let refresh = link.take_ready(); + let seq = show.remote_images.seq(); if let Err(error) = show.remote_images.send(snapshot, refresh, |snapshot, current_changed, next_changed| { - link.post(&SnapshotDelta { snapshot, current_changed, next_changed }.to_payload()) + link.post(&SnapshotDelta { snapshot, current_changed, next_changed, seq }.to_payload()) }) { message = Some(link_message(ui, error)); } diff --git a/crates/ui-egui/src/show/tests.rs b/crates/ui-egui/src/show/tests.rs index 46fdd0cde..243798453 100644 --- a/crates/ui-egui/src/show/tests.rs +++ b/crates/ui-egui/src/show/tests.rs @@ -1163,6 +1163,16 @@ fn input_state_tracks_the_modes_that_move_show_click_targets() { assert_ne!(show.input_state(), base, "a blank screen has no slide click targets"); } +/// Steps the app, at most two seconds of frames, until `done`. +fn step_until(h: &mut egui_kittest::Harness<'static, SlideApp>, done: impl Fn() -> bool) { + for _ in 0..120 { + h.step(); + if done() { + break; + } + } +} + #[test] fn remote_presenter_uses_the_audience_state_and_closes_with_the_show() { use crate::presenter::{Command, Snapshot, SnapshotDelta, merge}; @@ -1250,13 +1260,16 @@ fn remote_presenter_uses_the_audience_state_and_closes_with_the_show() { let posted = bridge.borrow().posts.len(); // Every accepted ready handshake is emitted by WindowLink, including a popup reload. bridge.borrow_mut().ready = true; - for _ in 0..120 { - h.step(); - if bridge.borrow().posts.len() > posted { - break; - } - } + step_until(&mut h, || bridge.borrow().posts.len() > posted); assert_eq!(bridge.borrow().posts.get(posted), Some(&(false, true, true, true, true)), "the first post after reload resends both previews"); + // A popup whose full inbox dropped a delta asks again; the images it may have lost come back. + let posted = bridge.borrow().posts.len(); + step_until(&mut h, || bridge.borrow().posts.len() > posted); + assert_eq!(bridge.borrow().posts.last(), Some(&(false, false, false, false, false)), "unchanged previews are left out"); + let posted = bridge.borrow().posts.len(); + bridge.borrow_mut().commands.push(Command::Resync); + step_until(&mut h, || bridge.borrow().posts.len() > posted); + assert_eq!(bridge.borrow().posts.get(posted), Some(&(false, true, true, true, true)), "a resync resends both previews"); // #1866: All slides opens in the web Presenter View, as on desktop; the audience keeps its slide. let posted = bridge.borrow().posts.len(); bridge.borrow_mut().commands.push(run(ShowCommands::Grid));