Compare commits

...
Author SHA1 Message Date
Zinadin Zidan c405f4005f Insert Picture: standalone EMF and WMF files insert as pictures (#2392) (#2534)
* Insert Picture: standalone EMF and WMF files insert as pictures (#2392)

content_type had no metafile arm, so an .emf or .wmf fell to
application/octet-stream and insert.picture refused it as not a picture.
Sniff the header with craft_metafile::sniff and give the MIME types the
PPTX writer saves as .emf / .wmf parts.

* Insert Picture metafile test: the reopened picture keeps its bounds (#2392)

The saved-and-reopened EMF and WMF pictures are checked to sit at the
requested rectangle, not only to be pictures holding the same bytes.
2026-12-03 16:02:40 +01:00
gmrmad1 076a95b713 show: each mouse-wheel notch turns a slide, without the trackpad cooldown (#1724) (#2158)
* show: each mouse-wheel notch turns a slide, without the trackpad cooldown (#1724)

* review: wheel steps through visible slides and reaches the end screen (coderabbit/hidden slides, last-slide notch)

* review: apply wheel steps in frame order and count the first visible slide (coderabbit/hidden-slide steps, event order)

* review: process end-screen wheel notches in arrival order (coderabbit/end-screen step order)

* review: finish multi-line notches on the end screen and end after a hidden last slide (coderabbit/end-screen line count, hidden tail)
2026-12-03 15:39:21 +01:00
12 changed files with 426 additions and 14 deletions
Generated
+1
View File
@@ -2469,6 +2469,7 @@ name = "deckcraft-engine"
version = "0.20.0"
dependencies = [
"chrono",
"craft-metafile",
"craft-mime",
"deckcraft-anim",
"deckcraft-color",
+1 -1
View File
@@ -40,7 +40,7 @@ pub use interact::{Click, HitShape, Target};
pub use mesh::{MeshVertex, layer_mesh};
pub use morph::{MorphFrame, MorphPath, MorphText, morph_crop, morph_frame, morph_pairs, morph_xfrm};
pub use path::{MotionPath, default_path};
pub use show::{LinkJump, ShowAction, ShowState, safe_url, show_order};
pub use show::{LinkJump, ShowAction, ShowState, looping, safe_url, show_order};
pub use timeline::{EffectTiming, Sequence, StepStamp, Timeline, TriggerPlay};
pub use transition::{DEFAULT_ASPECT, Layer, Source, transition_frame, transition_layers, uses_slide_parts};
+45 -10
View File
@@ -117,7 +117,7 @@ fn custom_indices(pres: &Presentation, name: &str) -> Option<Vec<usize>> {
if order.is_empty() { None } else { Some(order) }
}
fn looping(pres: &Presentation) -> bool {
pub fn looping(pres: &Presentation) -> bool {
pres.show.loop_until_esc || pres.show.show_type == "kiosk"
}
@@ -236,19 +236,54 @@ impl ShowState {
}
fn next_index(&self, pres: &Presentation) -> Option<usize> {
let order = self.visit(pres);
match order.iter().position(|&i| i == self.slide) {
Some(p) => order.get(p + 1).copied(),
None => order.iter().copied().find(|&i| i > self.slide),
}
self.visible_step(pres, self.slide, true, 1)
}
fn prev_index(&self, pres: &Presentation) -> Option<usize> {
let order = self.visit(pres);
match order.iter().position(|&i| i == self.slide) {
Some(p) => p.checked_sub(1).and_then(|q| order.get(q).copied()),
None => order.iter().copied().rev().find(|&i| i < self.slide),
self.visible_step(pres, self.slide, false, 1)
}
/// Show the end screen without advancing a step (the pointer wheel past the last visible
/// slide). A later forward input leaves the show through the end screen's own handling.
pub fn end(&mut self) {
self.ended = true;
self.playing = false;
}
/// `steps` slides after (`forward`) or before `from` in this show's visible order (the custom
/// show or range, hidden slides skipped), without entering them; `None` when the order ends
/// first. The pointer wheel turns slides through this, so it skips hidden slides as ←/→ do.
pub fn visible_step(&self, pres: &Presentation, from: usize, forward: bool, steps: usize) -> Option<usize> {
self.visible_step_in(pres, from, forward, steps, false)
}
/// Like [`Self::visible_step`], but stops at the first/last visible slide instead of running
/// past it: one fast wheel gesture lands on the end slide rather than leaving the order.
pub fn visible_step_clamped(&self, pres: &Presentation, from: usize, forward: bool, steps: usize) -> Option<usize> {
self.visible_step_in(pres, from, forward, steps, true)
}
fn visible_step_in(&self, pres: &Presentation, from: usize, forward: bool, steps: usize, clamp: bool) -> Option<usize> {
if steps == 0 {
return None;
}
let order = self.visit(pres);
// A `from` that is not in the order (a hidden slide opened by number) reaches the
// nearest visible slide in the direction, which counts as the first step.
let (start, used) = match order.iter().position(|&i| i == from) {
Some(p) => (p, 0),
None if forward => (order.iter().position(|&i| i > from)?, 1),
None => (order.iter().rposition(|&i| i < from)?, 1),
};
let rest = steps.saturating_sub(used);
let q = if forward {
if clamp { start.saturating_add(rest).min(order.len().checked_sub(1)?) } else { start.checked_add(rest)? }
} else if clamp {
start.saturating_sub(rest)
} else {
start.checked_sub(rest)?
};
order.get(q).copied()
}
/// Click / → / Space: finish the playing step, else play the next step, else go to the next slide.
+15
View File
@@ -763,6 +763,21 @@ fn show_skips_hidden_slides() {
assert_eq!(s.goto(&p, 99), ShowAction::None);
}
#[test]
fn a_wheel_step_from_a_hidden_slide_counts_its_first_visible_neighbour() {
// A hidden slide opened by number is not in the show order; the nearest visible slide in the
// direction is the first step, and the rest of the notches still apply.
let mut p = pres(5);
Arc::make_mut(&mut p.slides[1]).hidden = true;
let s = ShowState::new(&p);
assert_eq!(s.visible_step_clamped(&p, 1, true, 2), Some(3));
assert_eq!(s.visible_step_clamped(&p, 1, false, 2), Some(0));
assert_eq!(s.visible_step(&p, 1, true, 2), Some(3));
// Clamped steps stop at the ends of the visible order.
assert_eq!(s.visible_step_clamped(&p, 4, true, 3), Some(4));
assert_eq!(s.visible_step_clamped(&p, 0, false, 3), Some(0));
}
#[test]
fn show_remembers_a_slide_was_reached_by_going_back() {
// #1531: the UI plays the left slide's transition reversed only for Back.
+3
View File
@@ -10,6 +10,8 @@ workspace = true
[dependencies]
craft-mime = { workspace = true }
# Tells EMF from WMF when a picture is inserted (#2392).
craft-metafile = { workspace = true }
deckcraft-geom = { workspace = true }
deckcraft-color = { workspace = true }
deckcraft-model = { workspace = true }
@@ -32,6 +34,7 @@ chrono = { workspace = true }
[dev-dependencies]
deckcraft-anim = { workspace = true }
zip = { workspace = true }
craft-metafile = { workspace = true, features = ["write"] }
# The browser clock for comment timestamps (wasm has no std::time::SystemTime).
[target.'cfg(target_arch = "wasm32")'.dependencies]
+6
View File
@@ -33,6 +33,12 @@ pub fn content_type(name: &str, bytes: &[u8]) -> &'static str {
"image/bmp"
} else if bytes.starts_with(b"II*\0") || bytes.starts_with(b"MM\0*") {
"image/tiff"
} else if let Some(kind) = craft_metafile::sniff(bytes) {
// The MIME types the PPTX writer saves as `.emf` / `.wmf` parts (#2392).
match kind {
craft_metafile::Kind::Emf => "image/x-emf",
craft_metafile::Kind::Wmf => "image/x-wmf",
}
} else if bytes.starts_with(b"fLaC") {
"audio/flac"
} else if bytes.starts_with(b"OggS") {
+62
View File
@@ -0,0 +1,62 @@
//! Standalone EMF and WMF files inserted as pictures (#2392).
use std::io::{Cursor, Read};
use craft_metafile::write::{Op, emf, wmf};
use deckcraft_engine::Session;
use deckcraft_engine::cmd::base64_encode;
use deckcraft_geom::{KurboShape as _, Rect};
use deckcraft_model::{ShapeId, ShapeKind};
use serde_json::json;
/// Two solid rectangles, red at the top left and blue at the bottom right, on a 200 x 100 pt
/// picture.
fn two_rects() -> (Vec<Op>, Rect) {
let fill = |r: Rect, rgb: [u8; 3]| Op::Fill { path: r.to_path(0.1), even_odd: false, rgb };
(
vec![fill(Rect::new(0.0, 0.0, 100.0, 50.0), [0xC7, 0x30, 0x40]), fill(Rect::new(100.0, 50.0, 200.0, 100.0), [0x20, 0x70, 0xD0])],
Rect::new(0.0, 0.0, 200.0, 100.0),
)
}
#[test]
fn standalone_emf_and_wmf_files_insert_as_pictures_and_save_as_metafile_parts() {
let (ops, region) = two_rects();
let emf = emf(&ops, region, "DeckCraft", "two rects").expect("EMF");
let wmf = wmf(&ops, region).expect("WMF").bytes;
for (name, bytes, ext) in [("source.emf", emf, "emf"), ("source.wmf", wmf, "wmf")] {
let mut s = Session::with_new();
let rect = [200.0, 120.0, 480.0, 270.0];
let r = s.execute("insert.picture", &json!({"data": base64_encode(&bytes), "name": name, "rect": rect})).expect(name);
let id = ShapeId(r["id"].as_u64().expect("id") as u32);
let doc = &s.doc().expect("doc").doc;
let sh = doc.slides.iter().flat_map(|sl| &sl.shapes).find(|sh| sh.id == id).expect("inserted shape");
let ShapeKind::Picture { fill } = &sh.kind else { panic!("{name} is a picture: {:?}", sh.kind) };
let x = sh.xfrm.expect("xfrm");
assert_eq!([x.x, x.y, x.w, x.h], rect, "{name} sits at the requested bounds");
assert_eq!(doc.media(fill.media).expect("media").data.as_slice(), bytes.as_slice(), "{name} is embedded as given");
// Saved as a metafile part, and read back as the same picture.
let pptx = deckcraft_pptx::export(doc).expect("export");
let mut z = zip::ZipArchive::new(Cursor::new(&pptx)).expect("zip");
let part = z.file_names().find(|n| n.starts_with("ppt/media/") && n.ends_with(&format!(".{ext}"))).map(str::to_string);
let part = part.unwrap_or_else(|| panic!("{name} is saved as a .{ext} part: {:?}", z.file_names().collect::<Vec<_>>()));
let mut saved = Vec::new();
z.by_name(&part).expect("part").read_to_end(&mut saved).expect("read");
assert_eq!(saved, bytes, "{part} holds the file's bytes");
let back = deckcraft_pptx::import(&pptx).expect("import");
let reopened = back
.slides
.iter()
.flat_map(|sl| &sl.shapes)
.find(|sh| matches!(&sh.kind, ShapeKind::Picture { fill } if back.media(fill.media).is_some_and(|m| *m.data == bytes)));
let reopened = reopened.unwrap_or_else(|| panic!("{name} reopens as a picture"));
let x = reopened.xfrm.expect("reopened xfrm");
let near = |a: f64, b: f64| (a - b).abs() < 0.01;
assert!(
near(x.x, rect[0]) && near(x.y, rect[1]) && near(x.w, rect[2]) && near(x.h, rect[3]),
"{name} reopens at the requested bounds: {:?}",
[x.x, x.y, x.w, x.h]
);
}
}
+1
View File
@@ -1,2 +1,3 @@
mod chart_native;
mod insert_metafile;
mod show_clicks;
+7
View File
@@ -82,6 +82,11 @@ pub struct Show {
notes_px: f32,
timings: Vec<(usize, f64)>,
last_move: f64,
/// Leftover fraction of a discrete wheel line. High-resolution and free-spin wheels send
/// fractions of a line, so the delta accumulates here and each whole line is one slide.
wheel_carry: f32,
/// A discrete wheel line was handled this frame, so the canvas tool must not turn the page too.
wheel_taken: bool,
pub ended: bool,
/// The slide whose media was last started (auto-play runs once per visit).
media_slide: Option<usize>,
@@ -232,6 +237,8 @@ impl Show {
notes_px: NOTES_DEFAULT,
timings: vec![],
last_move: 0.0,
wheel_carry: 0.0,
wheel_taken: false,
ended: false,
media_slide: None,
last_viewed: None,
+186 -3
View File
@@ -775,9 +775,18 @@ fn a_first_animation_after_or_with_previous_plays_when_the_slide_appears() {
/// A running show on the first of three slides, with presenter view off.
fn show_of_three() -> egui_kittest::Harness<'static, SlideApp> {
show_of_with_hidden(3, &[])
}
/// A running show on the first of `slides`, with `hidden` slides hidden.
fn show_of_with_hidden(slides: usize, hidden: &[usize]) -> egui_kittest::Harness<'static, SlideApp> {
let mut session = deckcraft_engine::Session::with_new();
session.execute("slide.new", &json!({})).unwrap();
session.execute("slide.new", &json!({})).unwrap();
for _ in 1..slides {
session.execute("slide.new", &json!({})).unwrap();
}
for &i in hidden {
session.execute("slide.hide", &json!({"index": i, "hidden": true})).unwrap();
}
let mut harness =
harness_with(session, Services::default(), egui_kittest::Harness::builder().with_size(vec2(1000.0, 700.0)).with_step_dt(1.0 / 60.0), |_| {});
harness.state_mut().ui.presenter_view = false;
@@ -824,10 +833,184 @@ fn ctrl_wheel_zooms_the_show_and_the_plain_wheel_turns_the_page() {
assert!((show.view.zoom - fit).abs() < fit * 1e-3);
show_wheel(&mut h, centre, -3.0, egui::Modifiers::NONE);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 1, "the plain wheel turns the page");
// #1724: a -3.0 wheel event is three lines, so three slides (clamped to the last of three).
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 2, "the plain wheel turns the page");
}
}
/// #1724: two discrete wheel notches inside the trackpad's old 0.35 s cooldown turn two slides.
/// One notch is one slide; the second notch must not be dropped by a gesture gate.
#[test]
fn two_wheel_notches_inside_the_trackpad_cooldown_turn_two_slides() {
let mut h = show_of_three();
let centre = pos2(500.0, 350.0);
let notch = |harness: &mut egui_kittest::Harness<'static, SlideApp>, steps: usize| {
harness.input_mut().events.push(egui::Event::PointerMoved(centre));
harness.input_mut().events.push(egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Line,
delta: vec2(0.0, -1.0),
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Wheel,
modifiers: egui::Modifiers::NONE,
});
harness.run_steps(steps);
};
notch(&mut h, 4);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 1, "one notch turns one slide");
notch(&mut h, 4);
assert_eq!(
h.state().show.as_ref().expect("still showing").state.slide,
2,
"a second notch inside the old trackpad cooldown still turns the next slide"
);
}
/// #1724: on the end screen a discrete wheel notch keeps the old semantics: forward exits
/// the show, back resumes the last slide instead of jumping.
#[test]
fn wheel_on_the_end_screen_exits_forward_and_resumes_back() {
let mut h = show_of_three();
let centre = pos2(500.0, 350.0);
let line = |harness: &mut egui_kittest::Harness<'static, SlideApp>, y: f32| {
harness.input_mut().events.push(egui::Event::PointerMoved(centre));
harness.input_mut().events.push(egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Line,
delta: vec2(0.0, y),
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Wheel,
modifiers: egui::Modifiers::NONE,
});
harness.run_steps(4);
};
line(&mut h, -1.0);
line(&mut h, -1.0);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 2);
line(&mut h, -1.0);
assert!(h.state().show.as_ref().expect("still showing").ended, "past the last slide: the end screen");
// Back resumes the last slide instead of jumping to the one before it.
line(&mut h, 1.0);
let show = h.state().show.as_ref().expect("still showing");
assert!(!show.ended, "a backward notch leaves the end screen");
assert_eq!(show.state.slide, 2, "it resumes the last slide");
// Forward past the end again, then a forward notch exits.
line(&mut h, -1.0);
assert!(h.state().show.as_ref().expect("still showing").ended);
line(&mut h, -1.0);
assert!(h.state().show.is_none(), "a forward notch on the end screen exits the show");
}
/// Backward from an end screen reached on a hidden tail resumes the last visible slide, not the
/// hidden slide the show was left on.
#[test]
fn wheel_back_on_an_end_screen_from_a_hidden_tail_resumes_the_last_visible_slide() {
let mut h = show_of_with_hidden(4, &[3]);
let doc = h.state().session.active().unwrap().doc.clone();
h.state_mut().show.as_mut().unwrap().state.goto(&doc, 3);
h.run_steps(2);
show_wheel(&mut h, pos2(500.0, 350.0), -1.0, egui::Modifiers::NONE);
assert!(h.state().show.as_ref().expect("still showing").ended, "the hidden tail has no forward slide");
show_wheel(&mut h, pos2(500.0, 350.0), 1.0, egui::Modifiers::NONE);
let show = h.state().show.as_ref().expect("still showing");
assert!(!show.ended, "a backward notch leaves the end screen");
assert_eq!(show.state.slide, 2, "it resumes the last visible slide, not the hidden one");
}
/// A looping show wraps a forward wheel notch from the last visible slide to the first instead
/// of entering the end screen.
#[test]
fn wheel_forward_on_the_last_slide_wraps_in_a_looping_show() {
let mut h = show_of_three();
let st = h.state_mut().session.active_mut().unwrap();
std::sync::Arc::make_mut(&mut st.doc).show.loop_until_esc = true;
let centre = pos2(500.0, 350.0);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 2);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
let show = h.state().show.as_ref().expect("still showing");
assert!(!show.ended, "the looping show does not end");
assert_eq!(show.state.slide, 0, "it wraps to the first visible slide");
}
/// The wheel turns slides through the show's visible order, so a hidden slide is skipped as
/// ←/→ skip it instead of opening it through `ShowState::goto`.
#[test]
fn a_wheel_notch_skips_a_hidden_slide() {
let mut h = show_of_with_hidden(3, &[1]);
let centre = pos2(500.0, 350.0);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 2, "the hidden middle slide is skipped");
show_wheel(&mut h, centre, 1.0, egui::Modifiers::NONE);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 0, "back also skips it");
}
/// Two notches in one frame are applied in the order they arrived: a backward then a forward
/// notch at the first slide still moves forward (a signed net would cancel them out).
#[test]
fn a_backward_then_forward_notch_in_one_frame_still_moves_forward() {
let mut h = show_of_three();
let centre = pos2(500.0, 350.0);
h.input_mut().events.push(egui::Event::PointerMoved(centre));
for y in [1.0_f32, -1.0] {
h.input_mut().events.push(egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Line,
delta: vec2(0.0, y),
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Wheel,
modifiers: egui::Modifiers::NONE,
});
}
h.run_steps(4);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 1, "the forward notch still moves one slide");
}
/// A backward notch with two lines on the end screen resumes the last slide and still steps one
/// more back, matching the per-line contract of the ordinary-slide path.
#[test]
fn two_backward_lines_on_the_end_screen_reach_the_penultimate_slide() {
let mut h = show_of_three();
let centre = pos2(500.0, 350.0);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
assert!(h.state().show.as_ref().expect("still showing").ended, "at the end screen");
show_wheel(&mut h, centre, 2.0, egui::Modifiers::NONE);
let show = h.state().show.as_ref().expect("still showing");
assert!(!show.ended, "two backward lines leave the end screen");
assert_eq!(show.state.slide, 1, "the last slide then one more back");
}
/// A hidden slide after the last visible one has no forward neighbour; a notch there still shows
/// the end screen instead of doing nothing.
#[test]
fn a_forward_notch_on_a_hidden_slide_after_the_last_visible_ones_ends_the_show() {
let mut h = show_of_with_hidden(4, &[3]);
let doc = h.state().session.active().unwrap().doc.clone();
h.state_mut().show.as_mut().unwrap().state.goto(&doc, 3);
h.run_steps(2);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 3);
show_wheel(&mut h, pos2(500.0, 350.0), -1.0, egui::Modifiers::NONE);
assert!(h.state().show.as_ref().expect("still showing").ended, "no visible slide ahead: the end screen");
}
/// A completed forward wheel notch past the last slide reaches the end screen and then exits,
/// the same way a trackpad flick does; a partial line still does nothing.
#[test]
fn a_forward_wheel_notch_past_the_last_slide_reaches_the_end_screen() {
let mut h = show_of_three();
let centre = pos2(500.0, 350.0);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
assert_eq!(h.state().show.as_ref().expect("still showing").state.slide, 2);
// A partial line on the last slide does not end the show.
show_wheel(&mut h, centre, -0.4, egui::Modifiers::NONE);
assert!(!h.state().show.as_ref().expect("still showing").ended, "a partial notch does not reach the end screen");
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
assert!(h.state().show.as_ref().expect("still showing").ended, "a completed forward notch reaches the end screen");
show_wheel(&mut h, centre, -1.0, egui::Modifiers::NONE);
assert!(h.state().show.is_none(), "forward on the end screen exits the show");
}
/// A zoom at the top of the letterboxed slide keeps the slide over the fitted picture, as production's zoom window
/// stays on the slide: its top edge stays at the picture's, not pushed up to the window's.
#[test]
+1
View File
@@ -112,6 +112,7 @@ impl CanvasTool for ShowTool<'_> {
if !self.show.zoomed
&& !self.show.pen
&& !self.show.grid
&& !self.show.wheel_taken
&& let Some(wheel) = space.wheel()
&& !wheel.zooms
{
+98
View File
@@ -143,6 +143,12 @@ fn canvas_pass(app: &mut SlideApp, ui: &mut Ui, bcx: &mut BodyCx<'_>, show: &mut
if show.reading {
bars::show(ui, show, p.doc, p.room, p.now);
}
// A discrete mouse-wheel notch turns one slide per line at once; point-based trackpad
// gestures keep the canvas's own page turn.
show.wheel_taken = false;
if !show.pen && !show.zoomed {
wheel_lines(&ctx, show, p.doc, out);
}
let mut media_wake = None;
with_env(
ui,
@@ -220,6 +226,98 @@ fn canvas_pass(app: &mut SlideApp, ui: &mut Ui, bcx: &mut BodyCx<'_>, show: &mut
media_wake
}
/// Discrete wheel input must not share the trackpad's gesture gate. Keep every completed line in
/// the order it arrived, including several notches in one frame, so movement at a boundary is not
/// lost: a backward then forward notch at the first slide still moves forward. High-resolution
/// and free-spin wheels send fractions of a line, accumulated in `show.wheel_carry`; a change of
/// direction drops the leftover. The line events are taken out of the frame so the canvas's own
/// wheel navigation does not turn the page a second time.
fn wheel_lines(ctx: &egui::Context, show: &mut Show, doc: &Presentation, out: &mut Out) {
// A multi-pass layout replays the frame's events; only the pass that is kept may move slides,
// but every pass must take the lines out so the canvas does not also turn the page.
let act = !ctx.will_discard();
let mut steps = Vec::new();
let mut handled = false;
ctx.input_mut(|input| {
let slides = doc.slides.len();
input.events.retain(|event| {
let egui::Event::MouseWheel { unit, delta, modifiers, .. } = event else { return true };
if !matches!(unit, egui::MouseWheelUnit::Line | egui::MouseWheelUnit::Page)
|| modifiers.ctrl
|| modifiers.command
|| modifiers.shift
|| !delta.y.is_finite()
|| delta.y == 0.0
|| delta.y.abs() < delta.x.abs()
{
return true;
}
if act {
if show.wheel_carry * delta.y < 0.0 {
show.wheel_carry = 0.0;
}
show.wheel_carry += delta.y;
// The small slack keeps sums such as 10 × 0.1 from falling just short of a line.
let whole = (show.wheel_carry.abs() + 1e-4).floor();
show.wheel_carry -= whole.copysign(show.wheel_carry);
let count = whole.min(slides as f32) as usize;
if count > 0 {
steps.push(if delta.y < 0.0 { count as i32 } else { -(count as i32) });
}
}
handled = true;
false
});
});
show.wheel_taken = handled;
if !handled || !act {
return;
}
// Each line is applied in arrival order against the state the preceding line left.
for by in steps {
let count = by.unsigned_abs() as usize;
if show.ended {
if by > 0 {
out.keep = false;
break;
}
show.ended = false;
// Resume the last visible slide, even when the show ended from a hidden tail.
let Some(target) = show.state.last_index(doc) else { continue };
out.actions.push(show.state.goto(doc, target));
if count > 1
&& let Some(target) = show.state.visible_step_clamped(doc, target, false, count - 1)
{
out.actions.push(show.state.goto(doc, target));
}
continue;
}
let forward = by > 0;
match show.state.visible_step_clamped(doc, show.state.slide, forward, count) {
Some(target) if target != show.state.slide => out.actions.push(show.state.goto(doc, target)),
Some(_) | None if forward => {
// A looping show wraps from its last visible slide to the first instead of ending.
if deckcraft_anim::looping(doc)
&& let Some(first) = show.state.first_index(doc)
{
out.actions.push(show.state.goto(doc, first));
if count > 1
&& let Some(target) = show.state.visible_step_clamped(doc, first, true, count - 1)
{
out.actions.push(show.state.goto(doc, target));
}
} else {
// A notch that cannot move forward shows the end screen instead of being
// swallowed, as a trackpad flick past the last slide does.
show.state.end();
show.ended = true;
}
}
_ => {}
}
}
}
/// The slide's rect on screen when fitted to `rect`, as the unzoomed show draws it.
fn fitted_picture(rect: Rect, target: &ViewTarget, zoom: &ShowZoomInputs<'_>) -> Rect {
let mut view = CanvasView::new(rect, Point::ZERO, 1.0);