diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index aa8b0b96f..1c8afd895 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -4,8 +4,8 @@ use craft::behavior::canvas::zoom; use craft::behavior::kurbo::{Rect as DocRect, Size}; use craft::behavior::{Proposal, ZoomScheme}; use craft::ui::canvas::{ - Canvas, CanvasScrollbars, NavigationOptions, PageBorder, PageFrame, Pasteboard, ShownBars, ToolHost, ViewCommands, ViewRequest, ViewTarget, - WheelScheme, canvas_space, tool_pass, + Canvas, CanvasScrollbars, NavigationOptions, PageBorder, PageFrame, Pasteboard, ShownBars, ToolHost, ViewTarget, WheelScheme, canvas_space, + tool_pass, }; use craft::ui::contract::{Bind, Props, Show, ShowContainer, response_of}; use craft::ui::display::{Shade, Text}; @@ -52,17 +52,17 @@ pub(crate) fn show(app: &mut SlideApp, ui: &mut Ui) { let ppp = ui.ctx().pixels_per_point(); let mut view = app.canvas_view; let mut jumped = false; - let fit = [ViewRequest::Place(ViewCommands::FitOnScreen)]; - let queued = if let Some(zoom) = app.ui.zoom { + if let Some(zoom) = app.ui.zoom { let device = f64::from(zoom) * scheme.hundred; if (zoom::device(&view, ppp) / scheme.hundred) as f32 != zoom { view = zoom::with_device(&view, device, size, ppp, &scheme); jumped = true; } - &[][..] } else { - &fit[..] - }; + // Fit to Window: a view without a rect opens fitted on this pass's rect before the pass's input, so a pinch or + // wheel zoom made at the fit still applies (a queued Fit on Screen would run after it and undo it). + view.rect = Rect::NOTHING; + } let strips = (app.ui.ruler && !app.anim_preview_active()).then(|| rulers::rulers(app)).flatten(); let canvas_id = ui.scope_id().with("slide_canvas"); let strips_clip = ui.clip_rect().intersect(room); @@ -71,7 +71,6 @@ pub(crate) fn show(app: &mut SlideApp, ui: &mut Ui) { .document(ViewTarget::new(size).fit_ceiling(scheme.max_device())) .view(Bind::given(view)) .navigation(navigation()) - .queued(queued) .keys_to_body(true) .with_rulers(strips) .rulers(rulers::ruler_text(app.session.prefs.units), rulers::ruler_text(app.session.prefs.units)); diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 6f3523b14..998090684 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -6,7 +6,7 @@ use craft::behavior::CursorIcon; use deckcraft_engine::Session; use deckcraft_geom::Point; use egui::accesskit::Role; -use egui::{Event, Modifiers, Pos2, pos2, vec2}; +use egui::{Event, Modifiers, MouseWheelUnit, Pos2, TouchPhase, pos2, vec2}; use egui_kittest::Harness; use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::{Value, json}; @@ -98,6 +98,106 @@ fn slide_point(h: &Harness<'_, SlideApp>, p: Pos2) -> Pos2 { pos2((p.x - rect.min.x) / scale, (p.y - rect.min.y) / scale) } +/// A platform-command line wheel notch, then enough frames for egui to spread it out. +fn ctrl_wheel(h: &mut Harness<'_, SlideApp>, lines: f32) { + h.event(Event::MouseWheel { + unit: MouseWheelUnit::Line, + delta: vec2(0.0, lines), + phase: TouchPhase::Move, + source: egui::MouseWheelSource::Wheel, + modifiers: Modifiers::COMMAND, + }); + h.run_steps(30); +} + +/// Within half a slide point, plus the half device pixel the slide moves to sit on the pixel grid (#184). +fn assert_near(a: Pos2, b: Pos2, what: &str) { + assert!((a - b).length() < 1.25, "{what}: {a:?} moved to {b:?}"); +} + +/// #106: wheel and pinch zoom zoomed toward the slide's corner instead of the pointer. +#[test] +fn wheel_and_pinch_zoom_keep_the_point_under_the_pointer() { + let mut h = harness(); + let fit = h.state().canvas_scale(); + let r = h.state().slide_rect().unwrap(); + let p = r.min + vec2(r.width() * 0.8, r.height() * 0.7); + h.event(Event::PointerMoved(p)); + h.run_steps(1); + let under = slide_point(&h, p); + + h.event(Event::Zoom(2.0)); + h.run_steps(2); + let pinched = h.state().canvas_scale(); + assert!(pinched > fit * 1.5, "a pinch zooms in: {fit} -> {pinched}"); + assert_near(under, slide_point(&h, p), "pinch in"); + + ctrl_wheel(&mut h, 1.0); + assert!(h.state().canvas_scale() > pinched, "Ctrl/Cmd + wheel up zooms in"); + assert_near(under, slide_point(&h, p), "wheel in"); + + ctrl_wheel(&mut h, -1.0); + ctrl_wheel(&mut h, -1.0); + assert!(h.state().canvas_scale() < pinched, "Ctrl/Cmd + wheel down zooms out"); + assert_near(under, slide_point(&h, p), "wheel out"); + + h.event(Event::Zoom(0.8)); + h.run_steps(2); + assert_near(under, slide_point(&h, p), "pinch out"); + + // Zoomed out below the window size, the slide is centred again. + h.event(Event::Zoom(0.25)); + h.run_steps(2); + assert!(h.state().canvas_scale() < fit); + let (canvas, slide) = (h.state().canvas_rect().unwrap(), h.state().slide_rect().unwrap()); + assert!((canvas.center() - slide.center()).length() < 1.0, "{canvas:?} {slide:?}"); +} + +/// Zoom In/Out (keys, buttons, slider) keeps the middle of the view where it is. +#[test] +fn zoom_commands_keep_the_middle_of_the_view() { + let mut h = harness(); + let c = h.state().canvas_rect().unwrap().center(); + let mid = slide_point(&h, c); + for _ in 0..4 { + let before = h.state().canvas_scale(); + h.state_mut().run("view.zoomIn", json!({})).unwrap(); + h.run_steps(2); + assert!(h.state().canvas_scale() > before, "zoom in zooms"); + assert_near(mid, slide_point(&h, c), "zoom in"); + } + let before = h.state().canvas_scale(); + h.state_mut().run("view.zoomOut", json!({})).unwrap(); + h.run_steps(2); + assert!(h.state().canvas_scale() < before, "zoom out zooms"); + assert_near(mid, slide_point(&h, c), "zoom out"); +} + +/// #830: when the Format or Animation Painter is armed, the cursor shows the copy icon so the user can see the tool is +/// waiting for a click. +#[test] +fn format_painter_sets_the_cursor() { + let mut h = harness(); + h.hover_at(pos2(600.0, 400.0)); + h.run_steps(2); + assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Copy, "no painter is armed yet"); + h.state_mut().session.painter = Some((Default::default(), false)); + h.run_steps(3); + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Copy, "the format painter arms the Copy cursor"); +} + +/// #830: the Animation Painter arms the same cursor. +#[test] +fn animation_painter_sets_the_cursor() { + let mut h = harness(); + h.hover_at(pos2(600.0, 400.0)); + h.run_steps(2); + assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Copy, "no painter is armed yet"); + h.state_mut().session.anim_painter = Some((vec![], false)); + h.run_steps(3); + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Copy, "the animation painter arms the Copy cursor"); +} + fn app_with_table() -> (Harness<'static, SlideApp>, deckcraft_model::ShapeId) { let mut s = Session::with_new(); let data = json!([["Hello world", "b"], ["c", ""]]); diff --git a/crates/ui-egui/src/panels/animtimeline.rs b/crates/ui-egui/src/panels/animtimeline.rs index e5a8c58bf..aee585e12 100644 --- a/crates/ui-egui/src/panels/animtimeline.rs +++ b/crates/ui-egui/src/panels/animtimeline.rs @@ -109,4 +109,23 @@ mod tests { assert_eq!(b[1], None); assert_eq!(b[2], Some(Bar { anim: 2, step: Some(1), start: 0.0, span: 0.0 })); } + + #[test] + fn drags_snap_and_clamp() { + let anim = deckcraft_model::Animation { delay_ms: 500, duration_ms: 500, ..Default::default() }; + let bar = Bar { anim: 0, step: Some(0), start: 0.5, span: 0.5 }; + let end = bar.start + bar.span; + assert_eq!(timing_of(&bar, &anim, (-1.0, -0.5)), Some(Retime::Delay(0)), "a span moved before the step starts with no delay"); + let Some(Retime::Delay(late)) = timing_of(&bar, &anim, (1.0e9, 1.0e9 + bar.span)) else { panic!("a moved span changes the delay") }; + assert!(late > anim.delay_ms && late <= MAX_MS, "{late}"); + let Some(Retime::Duration(short)) = timing_of(&bar, &anim, (bar.start, bar.start + 0.001)) else { + panic!("a moved end changes the duration") + }; + assert!(short < anim.duration_ms && short >= MIN_DURATION_MS, "{short}"); + let Some(Retime::Duration(long)) = timing_of(&bar, &anim, (bar.start, 1.0e9)) else { panic!("a moved end changes the duration") }; + assert!(long > anim.duration_ms && long <= MAX_MS, "{long}"); + assert_eq!(timing_of(&bar, &anim, (bar.start, end)), None, "an unchanged span is no edit"); + assert_eq!(timing_of(&bar, &anim, (f64::NAN, end)), None); + assert_eq!(timing_of(&bar, &anim, (bar.start, f64::INFINITY)), None); + } } diff --git a/crates/ui-egui/src/panels/effect_presets_tests.rs b/crates/ui-egui/src/panels/effect_presets_tests.rs index 04edb2ede..530dcdc04 100644 --- a/crates/ui-egui/src/panels/effect_presets_tests.rs +++ b/crates/ui-egui/src/panels/effect_presets_tests.rs @@ -32,6 +32,39 @@ fn click(h: &mut Harness<'_, SlideApp>, label: &str) { h.run_steps(3); } +#[test] +fn preset_tiles_set_shadow_glow_and_soft_edges() { + let mut h = harness(); + let sizes = h.query_all_by_label("Size").count(); + click(&mut h, "Bottom shadow"); + let shadow = effects(&h).outer_shadow.expect("the bottom tile adds an outer shadow"); + assert!(shadow.dist > 0.0 && (shadow.dir - 90.0).abs() < 45.0, "the shadow falls below the shape: {shadow:?}"); + assert!(h.query_all_by_label("Size").count() > sizes, "the shadow's Size slider appears once there is a shadow"); + + click(&mut h, "Inner shadow"); + let fx = effects(&h); + assert!(fx.outer_shadow.is_none() && fx.inner_shadow.is_some(), "{fx:?}"); + click(&mut h, "No shadow"); + let fx = effects(&h); + assert!(fx.outer_shadow.is_none() && fx.inner_shadow.is_none(), "{fx:?}"); + + // A radius tile keeps the glow's colour and transparency. + h.state_mut().session.execute("shape.effects", &json!({"glowProps": {"color": "accent2", "transparency": 0.35, "radius": 8.0}})).unwrap(); + h.run_steps(3); + let before = effects(&h).glow.unwrap(); + click(&mut h, "18 pt glow"); + let glow = effects(&h).glow.unwrap(); + assert!(glow.radius > before.radius, "the 18 pt tile widens the 8 pt glow"); + assert_eq!(glow.color, before.color, "a radius tile keeps the glow colour"); + click(&mut h, "No glow"); + assert!(effects(&h).glow.is_none()); + + click(&mut h, "10 pt soft edges"); + assert!(effects(&h).soft_edge.is_some_and(|radius| radius > 0.0)); + click(&mut h, "No soft edges"); + assert_eq!(effects(&h).soft_edge, None); +} + #[test] fn the_current_preset_tile_is_marked_selected() { let mut h = harness(); diff --git a/crates/ui-egui/src/panels/fillui.rs b/crates/ui-egui/src/panels/fillui.rs index cfaf24f5b..d7a1a4405 100644 --- a/crates/ui-egui/src/panels/fillui.rs +++ b/crates/ui-egui/src/panels/fillui.rs @@ -7,6 +7,7 @@ use craft::color_ui::ui::gradient::{GradientEditor, StopFields}; use craft::i18n::Word; use craft::theme::Srgba; use craft::ui::buttons::IconButton; +use craft::ui::contract::overlay::Anchor; use craft::ui::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _, clicked}; use craft::ui::layout_components::Row; use craft::ui::runtime::env::Label; @@ -291,7 +292,7 @@ pub(crate) fn gradient_editor(app: &mut crate::SlideApp, ui: &mut Ui, label: &st if let Some(request) = editor.color_requested && let Some(rect) = request.rect { - app.fill_editor.stop = Some((request.index, rect.left_bottom())); + app.fill_editor.stop = Some((request.index, rect)); } stop_menu(app, ui, label, g, &mut out); out.filter(|edited| edited != g) @@ -470,10 +471,11 @@ pub(crate) fn pattern_editor(app: &mut crate::SlideApp, ui: &mut Ui, p: &Pattern out.filter(|pattern| pattern != pt) } -/// The open stop-colour popover; delayed samples carry their target in their answer key. +/// The open stop-colour popover: the stop and its marker's rect, which the popover hangs from. Delayed samples carry +/// their target in their answer key. #[derive(Default)] pub struct FillEditorState { - stop: Option<(usize, egui::Pos2)>, + stop: Option<(usize, Rect)>, } /// Converts a gradient to framework stops. @@ -534,14 +536,15 @@ fn stop_menu(app: &mut crate::SlideApp, ui: &mut Ui, label: &str, g: &Gradient, } } let open = app.fill_editor.stop; - if let Some((i, point)) = open { + if let Some((i, marker)) = open { let Some(key) = keys.get(i) else { app.fill_editor.stop = None; return; }; + // Hung from the marker, so the press of the double click that opened it is not a press outside. let mut menu = Popover::new(Word::plain("Stop color"), Bind::given(open.is_some())) .id_salt(label) - .at_point(point) + .overlay(|overlay| overlay.anchor(Anchor::Rect(marker))) .show(ui, |ui, cx| color_menu(app, ui, cx, key, None)); let picked = menu.settle(|_, value| value.take()).flatten(); if let Some(Some(color)) = picked { @@ -563,6 +566,55 @@ fn recolor_stop(out: &mut Option, g: &Gradient, i: usize, color: Color #[cfg(test)] mod tests { use super::*; + use egui::accesskit::Role; + use egui_kittest::Harness; + use egui_kittest::kittest::Queryable; + + fn selected_gradient(h: &Harness<'_, crate::SlideApp>) -> Gradient { + let shapes = h.state().session.active().unwrap().selected_shapes(); + let Some(deckcraft_model::Fill::Gradient(g)) = shapes[0].fill.clone() else { panic!("the shape keeps a gradient fill") }; + g + } + + /// #2175: double-clicking a gradient stop selects it and opens its colour menu; a colour picked there colours that + /// stop. + #[test] + fn double_clicking_a_stop_opens_its_color_menu() { + let mut session = deckcraft_engine::Session::with_new(); + session.execute("slide.new", &json!({"layout": "blank"})).unwrap(); + let id = session.execute("shape.insert", &json!({"preset": "rect", "rect": [40.0, 40.0, 120.0, 80.0]})).unwrap()["id"].clone(); + session.execute("edit.select", &json!({"ids": [id]})).unwrap(); + session.execute("shape.fill", &json!({"gradient": {"stops": [[0, "accent1"], [1, "accent2"]], "angle": 90}})).unwrap(); + let mut h = crate::test_harness::harness_with( + session, + crate::Services::default(), + Harness::builder().with_size(vec2(1800.0, 1500.0)).with_step_dt(1.0 / 60.0), + |_| {}, + ); + h.state_mut().ui.pane = Some("format".into()); + h.state_mut().ui.format_tab = "fill".into(); + h.run_steps(3); + let before = selected_gradient(&h); + assert!(h.query_by_label("Theme Colors").is_none(), "the menu starts closed"); + let stop = h.get_by_role_and_label(Role::GraphicsObject, "Color stop 2 of 2").rect().center(); + h.hover_at(stop); + h.run_steps(1); + for _ in 0..2 { + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos: stop, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }); + h.run_steps(1); + } + } + h.run_steps(2); + assert!(h.query_by_label("Theme Colors").is_some(), "the colour menu opened"); + h.get_by_label(SchemeSlot::Accent6.label()).click(); + h.run_steps(2); + let after = selected_gradient(&h); + assert_eq!(after.stops.len(), before.stops.len(), "no stop was added"); + assert_ne!(after.stops[1].color, before.stops[1].color, "the double-clicked stop took the colour"); + assert_eq!(after.stops[1].color, ColorRef::scheme(SchemeSlot::Accent6)); + assert_eq!(after.stops[0].color, before.stops[0].color, "the other stop keeps its colour"); + } #[test] fn gradient_json_retains_flip_mode() { diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index a7a748329..d1ab0650e 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -260,6 +260,9 @@ mod drawtable_tests; #[cfg(test)] mod inktoshape_tests; +#[cfg(test)] +mod picture_styles_tests; + #[cfg(test)] mod height_tests { use deckcraft_engine::Session; diff --git a/crates/ui-egui/src/ribbon/picture_styles_tests.rs b/crates/ui-egui/src/ribbon/picture_styles_tests.rs new file mode 100644 index 000000000..042e0068e --- /dev/null +++ b/crates/ui-egui/src/ribbon/picture_styles_tests.rs @@ -0,0 +1,61 @@ +//! Picture Format ▸ Picture Styles: the ribbon row follows the window width. +#![allow(clippy::unwrap_used, clippy::expect_used)] + +use deckcraft_engine::Session; +use deckcraft_engine::cmd::picture_style::STYLES; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use serde_json::json; + +use crate::{Services, SlideApp}; + +/// A blank slide with one selected picture, on the Picture Format tab, in a `width` px window. +fn picture_format(width: f32) -> Harness<'static, SlideApp> { + let mut s = Session::with_new(); + s.execute("slide.layout", &json!({"layout": "blank"})).unwrap(); + // This fixture's pixels are generated here and belong to this test. + let pixels = (0..4).flat_map(|_| [40, 120, 200, 255]).collect(); + let png = deckcraft_render::Image { width: 2, height: 2, pixels }.to_png(); + let data = deckcraft_engine::cmd::base64_encode(&png); + let id = s.execute("insert.picture", &json!({"data": data, "name": "a.png", "rect": [200, 100, 400, 250]})).unwrap()["id"].clone(); + s.execute("edit.select", &json!({"ids": [id]})).unwrap(); + let mut h = crate::test_harness::harness_with(s, Services::default(), Harness::builder().with_size(egui::vec2(width, 900.0)), |_| {}); + h.state_mut().ui.tab = "Picture Format".into(); + h.run_steps(4); + h +} + +/// Picture Styles tiles in the ribbon row (the More menu is closed). +fn inline_styles(h: &Harness<'_, SlideApp>) -> usize { + STYLES.iter().filter(|(_, label)| h.query_by_label(label).is_some()).count() +} + +/// Whether the ribbon row overflows the window and shows its scroll arrow. +fn scrolls(h: &Harness<'_, SlideApp>) -> bool { + h.query_by_label("Scroll ribbon right").is_some() +} + +/// The right edge of the rightmost Picture Styles tile in the row. +fn styles_end(h: &Harness<'_, SlideApp>) -> f32 { + STYLES.iter().filter_map(|(_, label)| h.query_by_label(label)).map(|node| node.rect().max.x).fold(f32::NEG_INFINITY, f32::max) +} + +#[test] +fn a_resize_alone_settles_the_styles_row() { + // No pointer event follows the resize, so only the app's own frames can bring the row to the new width. The row + // scrolls rather than giving up styles (`RibbonFit::Scroll`, a fixed `tiles(6, 2)` strip), so the settled layout + // shows in the scroll arrow and in where the styles sit. + const NARROW: f32 = 1280.0; + const WIDE: f32 = 2600.0; + let mut h = picture_format(NARROW); + h.run_ok(); + assert!(inline_styles(&h) > 0, "the row shows styles"); + assert!(scrolls(&h), "the Picture Format row overflows a {NARROW} px window"); + h.set_size(egui::vec2(WIDE, 900.0)); + assert!(h.run_ok().is_some(), "the app settles after a resize"); + assert!(!scrolls(&h), "the row still scrolls after growing to {WIDE} px"); + assert!(styles_end(&h) <= WIDE, "every style sits inside the {WIDE} px window"); + h.set_size(egui::vec2(NARROW, 900.0)); + h.run_ok(); + assert!(scrolls(&h), "the row does not scroll again after shrinking to {NARROW} px"); +}