mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 18:23:52 -06:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
8.2 KiB
Rust
192 lines
8.2 KiB
Rust
//! Browser presenter peer; the audience owns playback and this page emits requests.
|
|
|
|
use craft::i18n::Word;
|
|
use craft::ui::contract::{Politeness, Props as _, Show as _};
|
|
use craft::ui::fonts::{I18nScripts, UiFonts};
|
|
use craft::ui::notices::{Notice, NoticeLayout};
|
|
use craft::ui::primitives::{CachedTexture, Revision, TextureStore, TextureStoreOptions};
|
|
use craft::ui::runtime::env::WindowKey;
|
|
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::{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};
|
|
|
|
const MAX_SNAPSHOT: usize = 16 * 1024 * 1024;
|
|
const MAX_COMMAND: usize = 8192;
|
|
|
|
fn limits(max_in: usize, max_out: usize) -> LinkLimits {
|
|
let mut limits = LinkLimits::default();
|
|
limits.max_in = max_in;
|
|
limits.max_out = max_out;
|
|
limits.max_queued = 64;
|
|
limits
|
|
}
|
|
|
|
/// Creates the audience's presenter link; opening it remains a user action.
|
|
pub fn opener(ctx: &egui::Context) -> Option<Box<dyn WindowLink>> {
|
|
let ctx = ctx.clone();
|
|
PopupOpener::new("deckcraft", "popup,width=1100,height=700", limits(MAX_COMMAND, MAX_SNAPSHOT), move || ctx.request_repaint())
|
|
.ok()
|
|
.map(|link| Box::new(link) as Box<dyn WindowLink>)
|
|
}
|
|
|
|
// Separate variants keep slide zero distinct from either large preview.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
enum Picture {
|
|
Current,
|
|
Next,
|
|
Slide(usize),
|
|
}
|
|
|
|
/// A presenter page with its own show command scope and no playback state.
|
|
pub struct PopupApp {
|
|
peer: PopupPeer,
|
|
snapshot: Option<Snapshot>,
|
|
/// The number of the last delta received.
|
|
seq: Option<u64>,
|
|
textures: TextureStore<Picture>,
|
|
current_revision: u64,
|
|
next_revision: u64,
|
|
slides_revision: u64,
|
|
}
|
|
|
|
impl PopupApp {
|
|
/// Installs the page runtime and connects to its same-origin audience window.
|
|
pub fn new(ctx: &egui::Context, install: craft::ui::runtime::InstallSpec) -> Result<Self, String> {
|
|
embed::install(ctx, install, &UiFonts::new(&I18nScripts)).map_err(|error| error.to_string())?;
|
|
let wake = ctx.clone();
|
|
let peer = PopupPeer::connect("deckcraft", limits(MAX_SNAPSHOT, MAX_COMMAND), move || wake.request_repaint())
|
|
.ok_or_else(|| "The audience window is unavailable".to_owned())?;
|
|
if let Some(document) = web_sys::window().and_then(|window| window.document()) {
|
|
document.set_title("Presenter View — DeckCraft");
|
|
}
|
|
Ok(Self {
|
|
peer,
|
|
snapshot: None,
|
|
seq: None,
|
|
textures: TextureStore::in_context(ctx, "deckcraft.popup", TextureStoreOptions::frames()),
|
|
current_revision: 0,
|
|
next_revision: 0,
|
|
slides_revision: 0,
|
|
})
|
|
}
|
|
|
|
fn receive(&mut self, ctx: &egui::Context) {
|
|
// 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);
|
|
}
|
|
if delta.next_changed {
|
|
self.next_revision = self.next_revision.wrapping_add(1);
|
|
self.textures.forget(ctx, &Picture::Next);
|
|
}
|
|
if delta.snapshot.slides.is_some() {
|
|
self.slides_revision = self.slides_revision.wrapping_add(1);
|
|
self.textures.retain(ctx, |key| !matches!(key, Picture::Slide(_)));
|
|
}
|
|
self.snapshot = Some(merge(self.snapshot.as_ref(), delta));
|
|
}
|
|
}
|
|
|
|
fn picture(&self, ctx: &egui::Context, key: Picture, revision: u64, preview: Option<&Preview>) -> Option<CachedTexture> {
|
|
let preview = preview?;
|
|
let revision = Revision::new(revision);
|
|
self.textures.state(ctx, &key, revision).shown().or_else(|| Some(self.textures.put(ctx, key, revision, preview.image()?)))
|
|
}
|
|
|
|
fn page(&mut self, ui: &mut egui::Ui, cx: &mut craft::ui::contract::BodyCx<'_>) {
|
|
let notice = if self.peer.state() == LinkState::Closed {
|
|
Some(("The audience window has closed.", ""))
|
|
} else if js_sys::Date::now() - self.peer.last_heard() > 10_000.0 {
|
|
Some(("The audience window is not responding.", "Return to the audience window to resume the connection."))
|
|
} else if self.snapshot.is_none() {
|
|
Some(("Waiting for the audience window…", ""))
|
|
} else {
|
|
None
|
|
};
|
|
if let Some((title, detail)) = notice {
|
|
Notice::new(craft::theme::variant::Tone::Info, Word::plain(detail))
|
|
.id_salt("audience-link-state")
|
|
.title(Word::plain(title))
|
|
.layout(NoticeLayout::Centred)
|
|
.announce(Politeness::Polite)
|
|
.show(ui);
|
|
return;
|
|
}
|
|
let Some(snapshot) = &self.snapshot else { return };
|
|
let current = self.picture(ui.ctx(), Picture::Current, self.current_revision, snapshot.current.as_ref());
|
|
let next = self.picture(ui.ctx(), Picture::Next, self.next_revision, snapshot.next.as_ref());
|
|
// Slide(0), Slide(1), Slide(2) remain tiles 1, 2, 3; a malformed image must not shift later picks.
|
|
let slides: Vec<_> = snapshot
|
|
.slides
|
|
.as_deref()
|
|
.unwrap_or_default()
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(index, preview)| self.picture(ui.ctx(), Picture::Slide(index), self.slides_revision, Some(preview)))
|
|
.collect::<Option<_>>()
|
|
.unwrap_or_default();
|
|
let model = PresenterModel {
|
|
aspect: snapshot.aspect,
|
|
slide: snapshot.slide,
|
|
total: snapshot.total,
|
|
ended: snapshot.ended,
|
|
elapsed: snapshot.elapsed,
|
|
paused: snapshot.paused,
|
|
notes: &snapshot.notes,
|
|
notes_px: snapshot.notes_px,
|
|
blank: match snapshot.blank.as_deref() {
|
|
Some("black") => Some(Blank::Black),
|
|
Some("white") => Some(Blank::White),
|
|
_ => None,
|
|
},
|
|
grid: snapshot.grid,
|
|
zoom_armed: snapshot.zoom_armed,
|
|
zoomed: snapshot.zoomed,
|
|
zoom_frame: None,
|
|
current: current.as_ref().map_or(PreviewPic::Pending, PreviewPic::Shown),
|
|
next: if let Some(next) = &next {
|
|
NextPic::Shown(next)
|
|
} else if snapshot.slide.saturating_add(1) >= snapshot.total {
|
|
NextPic::End
|
|
} else {
|
|
NextPic::Pending
|
|
},
|
|
slides: GridSource::Pictures(&slides),
|
|
};
|
|
let mut out = Vec::new();
|
|
presenter_body(ui, cx, &model, &mut out);
|
|
for command in out {
|
|
let _ = self.peer.post(&command.to_payload());
|
|
}
|
|
}
|
|
}
|
|
|
|
impl eframe::App for PopupApp {
|
|
fn raw_input_hook(&mut self, ctx: &egui::Context, _input: &mut egui::RawInput) {
|
|
embed::before_input(ctx, |_| FrameInput::new(deckcraft_ui_egui::theme::root_env()));
|
|
}
|
|
|
|
fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
embed::logic(ctx, || embed::CloseDecision::Close);
|
|
}
|
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
|
|
self.receive(ui.ctx());
|
|
ui.ctx().request_repaint_after(std::time::Duration::from_millis(500));
|
|
AppShell::new(WindowKey::main()).page(true, SHOW_SCOPE, |_| None, |ui, cx, app: &mut Self| app.page(ui, cx)).show(ui, self);
|
|
}
|
|
}
|