FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Timeline file drops: merge origin/main; document the platform cursor exception and mixed-DPI limit
Brandon Thomas1bf590c73ca2
diff --cc apps/filmcraft/src/main.rsindex c14cb1c8a,fe89ca9ce..af0bb8919--- a/apps/filmcraft/src/main.rs+++ b/apps/filmcraft/src/main.rs@@@ -228,8 -229,10 +229,11 @@@ fn main() -> eframe::ResultSome(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()));+ // Settings ▸ Appearance ▸ Appearance Mode ▸ Sync with system on Linux desktops whose+ // compositor reports no theme to winit (no polling: see appearance.rs).+ app.hooks.system_theme = appearance::service();app.hooks.raise_without_focus = Some(Box::new(|| {window_raise::raise_without_focus();}));diff --cc crates/engine/src/settings_tests.rsindex 1686bf57e,6744a9c34..241f36f6d--- a/crates/engine/src/settings_tests.rs+++ b/crates/engine/src/settings_tests.rs@@@ -552,43 -552,69 +552,110 @@@ fn hardware_decoding_setting_drives_theassert!(settings::field("playback.hardwareDecoding").is_some_and(|f| f.wired && matches!(f.kind, Kind::Choice(_))));}+/// Settings ▸ Timeline ▸ "Place files dropped onto the Timeline directly on the Timeline": on by default.+#[test]+fn drop_import_to_timeline_defaults_to_on() {+ assert!(settings::TimelinePrefs::default().drop_import_to_timeline);+ assert!(Preferences::default().timeline.drop_import_to_timeline);+}++/// The key is saved under `timeline.dropImportToTimeline`, read back by `prefs.get`, and persisted.+#[test]+fn drop_import_to_timeline_round_trips_through_prefs() {+ let dir = tmp_dir("drop-import-to-timeline");+ let path = dir.join("preferences.json");+ let mut s = Session { prefs_path: Some(path.clone()), ..Session::default() };+ set(&mut s, "timeline.dropImportToTimeline", json!(false));+ assert!(!s.prefs.timeline.drop_import_to_timeline);+ assert_eq!(s.execute("prefs.get", json!({"key": "timeline.dropImportToTimeline"})).unwrap(), json!(false));+ assert!(!Preferences::load(&path).timeline.drop_import_to_timeline);+ // a non-boolean is rejected and the stored value is kept+ assert!(s.execute("prefs.set", json!({"key": "timeline.dropImportToTimeline", "value": "no"})).is_err());+ assert!(!s.prefs.timeline.drop_import_to_timeline);+ std::fs::remove_dir_all(dir).unwrap();+}++/// Preferences files written before the key existed load it as on.+#[test]+fn drop_import_to_timeline_defaults_to_on_for_older_prefs() {+ assert!(serde_json::from_value::<settings::TimelinePrefs>(json!({})).unwrap().drop_import_to_timeline);+ let old = Preferences::from_value(json!({"version": 2, "timeline": {"stillImageDuration": 3}}));+ assert!(old.timeline.drop_import_to_timeline);+ assert_eq!(old.timeline.still_image_duration, 3.0);+}++/// The key is a Boolean field of the Timeline category in the settings registry.+#[test]+fn drop_import_to_timeline_is_in_the_settings_registry() {+ let (cat, f) = settings::fields().into_iter().find(|(_, f)| f.key == "timeline.dropImportToTimeline").expect("registry field");+ assert_eq!(cat, "timeline");+ assert!(matches!(f.kind, Kind::Bool));+ assert_eq!(settings::field("timeline.dropImportToTimeline").map(|f| f.key), Some("timeline.dropImportToTimeline"));+}+++ /// New users keep the dark Darkest theme (following the system is opt-in); a file saved before+ /// appearance modes keeps showing its Color Theme as a fixed Dark or Light mode.+ #[test]+ fn appearance_defaults_and_legacy_theme_migrate() {+ let a = Preferences::default().appearance;+ assert_eq!((a.appearance_mode.as_str(), a.dark_theme.as_str(), a.light_theme.as_str()), ("dark", "darkest", "light"));+ assert_eq!(a.shown_theme(Some(true)), "darkest", "Dark mode ignores a light system");+ let dir = tmp_dir("prefs-appearance-migrate");+ let path = dir.join("preferences.json");+ for (theme, mode, dark, light) in [("darkest", "dark", "darkest", "light"), ("dark", "dark", "dark", "light"), ("light", "light", "darkest", "light")] {+ std::fs::write(&path, json!({"version": 2, "appearance": {"colorTheme": theme}}).to_string()).unwrap();+ let a = Preferences::load(&path).appearance;+ assert_eq!((a.appearance_mode.as_str(), a.dark_theme.as_str(), a.light_theme.as_str()), (mode, dark, light), "{theme}");+ assert_eq!(a.shown_theme(None), theme);+ }+ // a v1 file without an appearance section, and a hostile theme, get the defaults+ std::fs::write(&path, r#"{"autoSave":{"enabled":true}}"#).unwrap();+ assert_eq!(Preferences::load(&path).appearance, settings::AppearancePrefs::default());+ std::fs::write(&path, r#"{"appearance":{"colorTheme":"neon","appearanceMode":7,"darkTheme":"light"}}"#).unwrap();+ assert_eq!(Preferences::load(&path).appearance, settings::AppearancePrefs::default());+ // a file with a mode keeps it+ std::fs::write(&path, r#"{"appearance":{"colorTheme":"darkest","appearanceMode":"auto","lightTheme":"light","darkTheme":"dark"}}"#).unwrap();+ let a = Preferences::load(&path).appearance;+ assert_eq!((a.appearance_mode.as_str(), a.shown_theme(Some(true)), a.shown_theme(Some(false)), a.shown_theme(None)), ("auto", "light", "dark", "dark"));+ let _ = std::fs::remove_dir_all(&dir);+ }++ #[test]+ fn appearance_choices_validate_and_legacy_theme_still_selects() {+ let mut s = Session::default();+ s.execute("prefs.set", json!({"values": {"appearance.appearanceMode": "auto", "appearance.darkTheme": "dark"}})).unwrap();+ assert_eq!((s.prefs.appearance.appearance_mode.as_str(), s.prefs.appearance.dark_theme.as_str()), ("auto", "dark"));+ for (key, value) in [+ ("appearance.darkTheme", json!("light")),+ ("appearance.lightTheme", json!("darkest")),+ ("appearance.appearanceMode", json!("system")),+ ("appearance.appearanceMode", json!(1)),+ ] {+ assert!(s.execute("prefs.set", json!({"key": key, "value": value})).is_err(), "{key} {value}");+ }+ // the single Color Theme of older clients selects that theme and fixes the mode to its family+ set(&mut s, "appearance.colorTheme", json!("light"));+ assert_eq!((s.prefs.appearance.appearance_mode.as_str(), s.prefs.appearance.light_theme.as_str()), ("light", "light"));+ assert_eq!(s.prefs.appearance.dark_theme, "dark", "the dark choice is kept");+ set(&mut s, "appearance.colorTheme", json!("darkest"));+ assert_eq!((s.prefs.appearance.appearance_mode.as_str(), s.prefs.appearance.dark_theme.as_str()), ("dark", "darkest"));+ // the legacy Color Theme follows the mode and theme choices, so clients reading it aren't stale+ assert_eq!(s.prefs.appearance.color_theme, "darkest");+ set(&mut s, "appearance.appearanceMode", json!("light"));+ assert_eq!(s.prefs.appearance.color_theme, "light");+ set(&mut s, "appearance.appearanceMode", json!("dark"));+ set(&mut s, "appearance.darkTheme", json!("dark"));+ assert_eq!(s.prefs.appearance.color_theme, "dark");+ set(&mut s, "appearance.appearanceMode", json!("auto"));+ assert_eq!(s.prefs.appearance.color_theme, "dark", "Auto: the dark theme");+ set(&mut s, "appearance.colorTheme", json!("light"));+ assert_eq!((s.prefs.appearance.appearance_mode.as_str(), s.prefs.appearance.color_theme.as_str()), ("light", "light"));+ // the header button's cycle+ let mut a = settings::AppearancePrefs::default();+ let mut seen = Vec::new();+ for _ in 0..4 {+ a.appearance_mode = a.next_mode().into();+ seen.push(a.appearance_mode.clone());+ }+ assert_eq!(seen, ["auto", "light", "dark", "auto"]);+ }diff --cc crates/platform/src/cursor.rsindex aa238b841,000000000..847441699mode 100644,000000..100644--- a/crates/platform/src/cursor.rs+++ b/crates/platform/src/cursor.rs@@@ -1,22 -1,0 +1,28 @@@+//! 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.++//!++//! This is the one non-media OS call in this crate: `crates/platform` is the only crate allowed++//! `unsafe` (AGENTS.md §0.3, ADR 0001), so the single `GetCursorPos` call lives here rather than++//! in the UI. It is read-only, has a safe `Option`-returning API, and every other platform (and a++//! failed call) falls back to the last pointer position egui saw. Drop it once winit reports the++//! pointer position during an OS drag-and-drop.++/// 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 --cc crates/ui-egui/src/lib.rsindex 861723fad,82cf9317c..f5f775d98--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@@ -1954,28 -1951,3 +1993,31 @@@ mod audio_recovery_testsapp.stop();}}++/// A physical-pixel screen position as a point in the window's UI space, given the window's content origin in points.++///++/// Assumes the whole virtual screen uses the window's `pixels_per_point`: with monitors of mixed++/// DPI the point can be off when the cursor and the window's origin are on different monitors.+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());+ }+ }+}