FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#171: read the OS cursor for drops from the file manager
Mitchb630643ec945
diff --git a/apps/filmcraft/src/main.rs b/apps/filmcraft/src/main.rsindex 18fb4d7b0..c14cb1c8a 100644--- a/apps/filmcraft/src/main.rs+++ b/apps/filmcraft/src/main.rs@@ -228,6 +228,7 @@ fn main() -> eframe::Result {Some(Box::new(|dir: &str| rfd::FileDialog::new().set_directory(dir).pick_folder().map(|p| p.to_string_lossy().into_owned())));app.hooks.open_path = Some(Box::new(open_path));// Settings ▸ General ▸ Interface Language ▸ System Language (#218).+ app.hooks.cursor_screen_position = Some(Box::new(filmcraft_platform::cursor::cursor_screen_position));app.hooks.system_languages = Some(Box::new(|| sys_locale::get_locales().collect()));app.hooks.raise_without_focus = Some(Box::new(|| {window_raise::raise_without_focus();diff --git a/crates/platform/Cargo.toml b/crates/platform/Cargo.tomlindex 55e3be066..9ec69b939 100644--- a/crates/platform/Cargo.toml+++ b/crates/platform/Cargo.toml@@ -50,6 +50,7 @@ windows = { version = "0.62", features = ["Win32_Media_MediaFoundation","Win32_System_Com","Win32_System_LibraryLoader",+ "Win32_UI_WindowsAndMessaging",] }# Linux: libva (VA-API) is loaded at run time with libloading (ISC; already in the lockfile throughdiff --git a/crates/platform/src/cursor.rs b/crates/platform/src/cursor.rsnew file mode 100644index 000000000..aa238b841--- /dev/null+++ b/crates/platform/src/cursor.rs@@ -0,0 +1,22 @@+//! The mouse position in screen pixels, for when the windowing layer has none.+//!+//! While files are dragged in from the OS file manager, winit on Windows delivers no cursor events+//! (it only reports hovered and dropped files), so the UI cannot learn where a drop landed.++/// The cursor's position on the virtual screen in physical pixels, or `None` where the OS cannot+/// say or this platform's windowing layer already reports it during a drag.+#[cfg(target_os = "windows")]+#[allow(unsafe_code)]+pub fn cursor_screen_position() -> Option<(i32, i32)> {+ use windows::Win32::Foundation::POINT;+ use windows::Win32::UI::WindowsAndMessaging::GetCursorPos;+ let mut p = POINT { x: 0, y: 0 };+ // SAFETY: `p` is a valid, writable POINT for the duration of the call.+ unsafe { GetCursorPos(&mut p) }.ok()?;+ Some((p.x, p.y))+}++#[cfg(not(target_os = "windows"))]+pub fn cursor_screen_position() -> Option<(i32, i32)> {+ None+}diff --git a/crates/platform/src/lib.rs b/crates/platform/src/lib.rsindex da0164e29..93e89458a 100644--- a/crates/platform/src/lib.rs+++ b/crates/platform/src/lib.rs@@ -34,6 +34,7 @@mod annexb;#[cfg_attr(not(any(target_os = "windows", target_os = "linux")), allow(dead_code))]mod biplanar;+pub mod cursor;#[cfg(target_os = "macos")]pub mod hardware_encode;pub mod hybrid;diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex b846cc991..1a297a961 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -104,6 +104,8 @@ pub struct AudioDevices {#[derive(Default)]pub struct HostHooks {pub pick_files: Option<Box<dyn FnMut(&[&str]) -> Vec<String>>>,+ /// The cursor in physical screen pixels. Where the windowing layer reports no pointer while files are dragged in from the OS (Windows), a drop is placed where this says it landed.+ pub cursor_screen_position: Option<Box<dyn Fn() -> Option<(i32, i32)>>>,pub pick_save: Option<Box<dyn FnMut(&str) -> Option<String>>>,pub pick_open_project: Option<Box<dyn FnMut() -> Option<String>>>,/// Save dialog with a filter: (filter name, extensions, suggested file name) → path.@@ -1141,7 +1143,14 @@ impl FilmcraftApp {}}if !paths.is_empty() {- let pointer = ctx.input(|i| i.pointer.hover_pos().or(i.pointer.latest_pos()));+ // The OS reports no cursor motion while files are dragged in (winit, Windows): ask it where the drop landed.+ let os_pointer = self+ .hooks+ .cursor_screen_position+ .as_ref()+ .and_then(|f| f())+ .and_then(|px| ctx.input(|i| i.viewport().inner_rect.map(|r| screen_px_to_ui(px, i.pixels_per_point, r.min))));+ let pointer = os_pointer.or_else(|| ctx.input(|i| i.pointer.hover_pos().or(i.pointer.latest_pos())));let imported = self.session.execute("file.import", json!({"paths": paths, "bin": self.import_bin().0}));if let Ok(v) = imported&& let Some(pos) = pointer@@ -1929,3 +1938,28 @@ mod audio_recovery_tests {app.stop();}}++/// A physical-pixel screen position as a point in the window's UI space, given the window's content origin in points.+fn screen_px_to_ui(px: (i32, i32), pixels_per_point: f32, content_min: egui::Pos2) -> egui::Pos2 {+ let ppp = if pixels_per_point.is_finite() && pixels_per_point > 0.0 { pixels_per_point } else { 1.0 };+ egui::pos2(px.0 as f32 / ppp - content_min.x, px.1 as f32 / ppp - content_min.y)+}++#[cfg(test)]+mod screen_px_tests {+ use super::screen_px_to_ui;++ #[test]+ fn maps_screen_pixels_into_the_window() {+ let p = screen_px_to_ui((1300, 900), 2.0, egui::pos2(100.0, 50.0));+ assert_eq!((p.x, p.y), (550.0, 400.0));+ }++ #[test]+ fn a_hostile_scale_does_not_divide_by_zero() {+ for ppp in [0.0, -1.0, f32::NAN, f32::INFINITY] {+ let p = screen_px_to_ui((10, 20), ppp, egui::pos2(0.0, 0.0));+ assert!(p.x.is_finite() && p.y.is_finite());+ }+ }+}