FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
monitors: the wheel zooms about the pointer, the middle button pans (#807)
ReReddo5f9908905c53
diff --git a/crates/ui-egui/src/panels/monitor.rs b/crates/ui-egui/src/panels/monitor.rsindex a57d0483a..369305352 100644--- a/crates/ui-egui/src/panels/monitor.rs+++ b/crates/ui-egui/src/panels/monitor.rs@@ -287,8 +287,9 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect, which: Which)// Click/drag in the picture: the Hand tool pans a magnified picture, otherwise focus.let pic_resp = ui.interact(pic, egui::Id::new((prefix, "pic")), if show_picture { Sense::click_and_drag() } else { Sense::hover() });app.auto.add(&format!("{prefix}.picture"), pic.intersect(video_area), "picture");- if show_picture {- monitor_view::pan_input(app, ui, which, video_area, pic);+ // (Comparison View always fits both frames)+ if show_picture && !compare {+ monitor_view::zoom_pan_input(app, ui, which, video_area, pic, display_w, ppp);}if which == Which::Program && show_picture {eyedropper(app, ui, &pic_resp, pic, target, rate, time);diff --git a/crates/ui-egui/src/panels/monitor_view.rs b/crates/ui-egui/src/panels/monitor_view.rsindex 15e6ab6fc..501ead7a5 100644--- a/crates/ui-egui/src/panels/monitor_view.rs+++ b/crates/ui-egui/src/panels/monitor_view.rs@@ -1,7 +1,8 @@//! Monitor view options (the View menu and the monitors' wrench menus): Playback / Paused//! Resolution, High Quality Playback, Display Mode (Composite Video, Alpha, Red, Green, Blue,//! Multi-Camera, Audio Waveform, Comparison View, Video and Audio Waveform Split), Magnification-//! (Fit, 10%…1600%, scroll or Hand-tool panning), rulers, guides (dragged out of the rulers, Add+//! (Fit, 10%…1600%, the wheel zooming about the pointer, middle-button or Hand-tool panning),+//! rulers, guides (dragged out of the rulers, Add//! Guide…, Clear Guides, Lock Guides, templates saved in the user preferences) and Snap in Program//! Monitor (graphic drags snap to guides, the frame edges and centre).//!@@ -354,14 +355,42 @@ pub fn picture_rect(area: Rect, fw: f32, fh: f32, v: &MonitorView, ppp: f32) ->}}-/// Pan a magnified picture: scroll over it, or drag with the Hand tool.-pub fn pan_input(app: &mut FilmcraftApp, ui: &mut egui::Ui, w: Which, area: Rect, pic: Rect) {+/// Zoom and pan the picture `pic` shown in `area`: the wheel (or a pinch) zooms about the pointer,+/// from Fit too, between the zoom menu's 10% and 1600%; a middle-button drag, or a drag with the+/// Hand tool, pans a magnified picture. `fw` is the picture's width in frame pixels.+pub fn zoom_pan_input(app: &mut FilmcraftApp, ui: &mut egui::Ui, w: Which, area: Rect, pic: Rect, fw: f32, ppp: f32) {+ let ctx = ui.ctx().clone();+ if ui.rect_contains_pointer(area)+ && let Some(p) = ctx.pointer_hover_pos()+ {+ // as fast as the Timeline's own wheel zoom+ let wheel = crate::panels::timeline::wheel_input(&ctx);+ let f = if (wheel.pinch - 1.0).abs() > 1e-4 { wheel.pinch } else { (1.0 + wheel.delta.y * 0.01).clamp(0.5, 2.0) };+ if (f - 1.0).abs() > 1e-4 && pic.width() > 0.0 && fw > 0.0 {+ let cur = pic.width() * ppp / fw;+ let z = (cur * f).clamp(ZOOMS[0].1, ZOOMS[ZOOMS.len() - 1].1);+ let k = z / cur;+ // the point of the picture under the pointer stays under it+ let centre = p - (p - pic.center()) * k;+ let max = ((pic.size() * k - area.size()) / 2.0).max(Vec2::ZERO);+ let v = view_mut(app, w);+ v.zoom = Some(z);+ v.pan = [(centre.x - area.center().x).clamp(-max.x, max.x), (centre.y - area.center().y).clamp(-max.y, max.y)];+ return;+ }+ }if view(app, w).zoom.is_none() {return;}let mut d = Vec2::ZERO;- if ui.rect_contains_pointer(area) {- d += ui.input(|i| i.smooth_scroll_delta);+ // a middle-button drag that began over the picture pans it, whatever the tool+ let held = egui::Id::new((prefix(w), "middle-pan"));+ let (pressed, down, moved) = ui.input(|i| (i.pointer.button_pressed(egui::PointerButton::Middle), i.pointer.middle_down(), i.pointer.delta()));+ let panning = down && (ui.data(|m| m.get_temp::<bool>(held)).unwrap_or(false) || (pressed && ui.rect_contains_pointer(area)));+ ui.data_mut(|m| m.insert_temp(held, panning));+ if panning {+ ctx.set_cursor_icon(egui::CursorIcon::Grabbing);+ d += moved;}if app.ui.tool == Tool::Hand {let resp = ui.interact(area, egui::Id::new((prefix(w), "pan")), Sense::drag());diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rsindex ca4fab0dc..70cde2b69 100644--- a/crates/ui-egui/src/panels/timeline.rs+++ b/crates/ui-egui/src/panels/timeline.rs@@ -1902,11 +1902,11 @@ fn multicam_menu(app: &mut FilmcraftApp, ui: &mut egui::Ui, picked: &[&TrackItem}/// What the wheel and the trackpad did this frame.-struct WheelInput {+pub(crate) struct WheelInput {/// Scroll in points (wheel lines and pages converted), positive = content moves right / down.- delta: egui::Vec2,+ pub(crate) delta: egui::Vec2,/// Pinch factor (1 = none).- pinch: f32,+ pub(crate) pinch: f32,command: bool,alt: bool,shift: bool,@@ -1915,7 +1915,7 @@ struct WheelInput {/// The wheel events of this frame, read from the events themselves: egui turns Cmd + wheel into a/// zoom and Shift + wheel into a sideways scroll before anyone asks, and the Timeline has its own/// meaning for both. The modifiers are those of the events (a script's wheel carries its own).-fn wheel_input(ctx: &egui::Context) -> WheelInput {+pub(crate) fn wheel_input(ctx: &egui::Context) -> WheelInput {ctx.input(|i| {let mut w = WheelInput { delta: egui::Vec2::ZERO, pinch: 1.0, command: false, alt: false, shift: false };for e in &i.events {diff --git a/crates/ui-egui/tests/view_ui.rs b/crates/ui-egui/tests/view_ui.rsindex 1339ad6c2..92b34a1fc 100644--- a/crates/ui-egui/tests/view_ui.rs+++ b/crates/ui-egui/tests/view_ui.rs@@ -183,6 +183,64 @@ fn view_menu_resolutions_and_display_modes() {assert_eq!((s["display"].as_str(), s["show_rulers"].as_bool()), (Some("Composite"), Some(true)));}+fn num(v: &Value) -> f64 {+ v.as_f64().unwrap_or_else(|| panic!("not a number: {v}"))+}++/// #807: the wheel over a monitor zooms about the pointer, a middle-button drag pans, Fit resets,+/// and each monitor keeps its own view.+#[test]+fn wheel_zooms_about_the_pointer_and_the_middle_button_pans() {+ let mut d = Driver::demo();+ // at 100% the picture is larger than the monitor, so the element is the monitor's area+ d.menu("view.magnification.100");+ let area = d.rect("program.picture");+ let centre = (area[0] + area[2] / 2.0, area[1] + area[3] / 2.0);+ let at = (area[0] + area[2] * 0.25, area[1] + area[3] * 0.4);+ // 50 points up: 1.5 times; the point under the pointer stays under it+ d.ok("ui.scroll", json!({"x": at.0, "y": at.1, "dx": 0.0, "dy": 50.0}));+ d.frames(2);+ let p = d.program();+ assert!((num(&p["zoom"]) - 1.5).abs() < 1e-4, "{p}");+ let want = ((centre.0 - at.0) * 0.5, (centre.1 - at.1) * 0.5);+ assert!((num(&p["pan"][0]) - want.0).abs() < 1.0 && (num(&p["pan"][1]) - want.1).abs() < 1.0, "pan {} for {want:?}", p["pan"]);++ // the middle button drags the picture, whatever the tool+ let (zoom, pan) = (num(&p["zoom"]), [num(&p["pan"][0]), num(&p["pan"][1])]);+ let send = |d: &mut Driver, e: egui::Event| {+ d.harness.input_mut().events.push(e);+ d.frames(1);+ };+ let button = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Middle, pressed, modifiers: Default::default() };+ let from = egui::pos2(centre.0 as f32, centre.1 as f32);+ send(&mut d, egui::Event::PointerMoved(from));+ send(&mut d, button(from, true));+ for k in 1..=4 {+ send(&mut d, egui::Event::PointerMoved(from + egui::vec2(-10.0 * k as f32, 5.0 * k as f32)));+ }+ send(&mut d, button(from + egui::vec2(-40.0, 20.0), false));+ let p = d.program();+ assert_eq!(num(&p["zoom"]), zoom, "a pan does not zoom");+ assert!((num(&p["pan"][0]) - (pan[0] - 40.0)).abs() < 1.0 && (num(&p["pan"][1]) - (pan[1] + 20.0)).abs() < 1.0, "pan {} after {pan:?}", p["pan"]);++ // zooming out stops at the menu's smallest level; Fit resets zoom and pan+ for _ in 0..12 {+ d.ok("ui.scroll", json!({"x": centre.0, "y": centre.1, "dx": 0.0, "dy": -50.0}));+ }+ d.frames(2);+ assert!((num(&d.program()["zoom"]) - 0.1).abs() < 1e-6, "stops at 10%");+ d.menu("view.magnification.fit");+ let p = d.program();+ assert!(p["zoom"].is_null() && p["pan"] == json!([0.0, 0.0]), "{p}");++ // from Fit, the wheel zooms in too; the Source monitor keeps its own view+ d.ok("ui.scroll", json!({"x": centre.0, "y": centre.1, "dx": 0.0, "dy": 20.0}));+ d.frames(2);+ assert!(d.program()["zoom"].as_f64().is_some(), "zoomed in from Fit");+ let source = d.ok("ui.inspect", json!({}))["ui"]["source"].clone();+ assert!(source["zoom"].is_null() && source["pan"] == json!([0.0, 0.0]), "{source}");+}+#[test]fn magnification_pans_and_guides_from_rulers() {let mut d = Driver::demo();diff --git a/docs/monitors.md b/docs/monitors.mdindex 3610c9ea3..f8cdd36d9 100644--- a/docs/monitors.md+++ b/docs/monitors.md@@ -15,7 +15,7 @@ focused monitor (Program by default). Code: `crates/ui-egui/src/panels/monitor_v| `view.display.multicam` | Display Mode ▸ Multi-Camera | the Program's multi-camera view (`multicam.toggleView`) || `view.display.audioWaveform`, `view.display.videoAndWaveform` | Display Mode ▸ Audio Waveform / Video and Audio Waveform Split | Source Monitor: the clip's waveform (click to move the source playhead); audio-only clips always show it || `view.display.comparison` | Display Mode ▸ Comparison View | Program: the reference frame (left) beside the current frame; the reference starts at the playhead and is stepped or reset with the buttons under it, or `view.compare.setReference {time\|seconds}` |-| `view.magnification.<fit\|10\|25\|50\|75\|100\|150\|200\|400\|800\|1600>` | View ▸ Magnification and the zoom dropdown | 100% = one frame pixel per screen pixel; scroll or drag with the Hand tool to pan |+| `view.magnification.<fit\|10\|25\|50\|75\|100\|150\|200\|400\|800\|1600>` | View ▸ Magnification and the zoom dropdown | 100% = one frame pixel per screen pixel. The mouse wheel (or a pinch) over a monitor zooms about the pointer, from Fit too, within 10%–1600%; drag with the middle mouse button, or with the Hand tool, to pan; Fit resets both. Each monitor keeps its own zoom and pan || `view.showRulers`, `view.showGuides`, `view.lockGuides`, `view.clearGuides` | View | toggles take `{"enabled": bool}` || `view.addGuide` | View ▸ Add Guide… | dialog, or `{"orientation": "vertical\|horizontal", "position": px}` || `view.snapInProgramMonitor` | View ▸ Snap in Program Monitor | graphic moves snap to the frame edges / centre and the guides (6 pt) |