PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge upstream and retain both save-type and clipboard regressions
jonathnwxh670ca408040c
diff --cc .github/workflows/ci.ymlindex be10a3de9,4303f0aac..135c184d3--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@@ -159,42 -275,10 +275,44 @@@ jobsenv:CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fontsCRAFT_FONTS_REQUIRED: "1"- run: cargo test --workspace --locked --features heif+ run: >-+ cargo nextest run --profile ci --workspace --locked --features heif --lib --bins --tests+ -E 'package(photocraft-text) | package(photocraft-io) | package(photocraft-ui-egui)'+ # Keep clipboard regressions runnable when an unrelated workspace test fails.+ clipboard:+ name: clipboard regressions (${{ matrix.os }})+ if: github.event_name == 'pull_request'+ runs-on: ${{ matrix.os }}+ timeout-minutes: 45+ permissions:+ contents: read+ strategy:+ fail-fast: false+ matrix:+ os: [ubuntu-latest, windows-latest]+ steps:+ - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4+ with:+ persist-credentials: false+ - name: Install Linux system dependencies (egui/winit/rfd)+ if: runner.os == 'Linux'+ run: |+ sudo apt-get update+ sudo apt-get install -y --no-install-recommends \+ libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev \+ libgl1-mesa-dev libgtk-3-dev+ - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable+ - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2+ with:+ shared-key: test+ save-if: false+ - name: Clipboard state and regression tests+ run: cargo test -p photocraft-ui-egui --locked --lib 'clipboard_tests::'+ - name: Native Windows clipboard contention+ if: runner.os == 'Windows'+ run: cargo test -p photocraft --locked --bin photocraft services::tests::busy_native_clipboard_preserves_copied_shape -- --ignored --exact --test-threads=1+corpus:timeout-minutes: 30# Real-file corpus tests (cargo feature `corpus`), on every PR and on main. The corpora arediff --cc apps/photocraft/src/services.rsindex b796b18b2,6c34fda33..6dfcd4bbc--- a/apps/photocraft/src/services.rs+++ b/apps/photocraft/src/services.rs@@@ -597,57 -653,41 +665,92 @@@ mod testsuse photocraft_ui_egui::{PhotocraftApp, prefs_ui};use serde_json::{Value, json};+ /// #6121: the file type picked in the save dialog gives the name its extension where the+ /// dialog left the one it opened with, and only there.+ #[test]+ fn the_picked_file_type_gives_the_name_its_extension() {+ let exts = |e: &[&str]| e.iter().map(|s| s.to_string()).collect::<Vec<_>>();+ let (png, psd) = (exts(&["png"]), exts(&["psd", "psb"]));+ let opened = Some("Untitled-1.psd");+ let cases = [+ // PNG picked, the name as the dialog opened (or renamed, its extension kept).+ ("/pics/Untitled-1.psd", opened, &png, "/pics/Untitled-1.png"),+ ("/pics/art.psd", opened, &png, "/pics/art.png"),+ ("/pics/ART.PSD", opened, &png, "/pics/ART.png"),+ // No extension: the type's. One PhotoCraft doesn't save is part of the name.+ ("/pics/art", opened, &png, "/pics/art.png"),+ ("/pics/scan.2024", opened, &png, "/pics/scan.2024.png"),+ // Typed, an extension PhotoCraft saves is the user's: kept, whatever the type says+ // (the PSD type untouched, or PNG picked and .jpg typed).+ ("/pics/art.png", opened, &psd, "/pics/art.png"),+ ("/pics/art.jpg", opened, &png, "/pics/art.jpg"),+ ("/pics/art.pcraft", opened, &png, "/pics/art.pcraft"),+ // Already the type's: kept, including its other extensions.+ ("/pics/art.png", opened, &png, "/pics/art.png"),+ ("/pics/art.PNG", opened, &png, "/pics/art.PNG"),+ ("/pics/big.psb", opened, &psd, "/pics/big.psb"),+ // The dialog opened on a .pcraft: that is the extension the type replaces.+ ("/pics/art.pcraft", Some("art.pcraft"), &png, "/pics/art.png"),+ ("/pics/art.psd", Some("art.pcraft"), &png, "/pics/art.psd"),+ ];+ for (typed, opened, chosen, want) in cases {+ assert_eq!(with_chosen_type(Path::new(typed), opened, chosen), PathBuf::from(want), "{typed} (opened {opened:?})");+ }+ // No type picked: the name as typed.+ assert_eq!(with_chosen_type(Path::new("/pics/art.psd"), opened, &[]), PathBuf::from("/pics/art.psd"));+ }++ /// Regression using the real clipboard on an isolated Windows CI runner.+ #[cfg(target_os = "windows")]+ #[test]+ #[ignore = "requires exclusive use of the Windows clipboard"]+ fn busy_native_clipboard_preserves_copied_shape() {+ for id in ["edit.paste", "edit.pasteSpecial.pasteInPlace"] {+ let mut native = native(None);+ let services = Services {+ clipboard_set_image: native.clipboard_set_image.take(),+ clipboard_get_image: native.clipboard_get_image.take(),+ ..Default::default()+ };+ let mut app = PhotocraftApp::new(Session::new(), services);+ app.run("file.new", json!({"width": 80, "height": 60})).unwrap();+ app.run("shape.create", json!({"kind": "rect", "rect": [10, 10, 40, 20], "fill": "#ff0000"})).unwrap();+ app.run("edit.copy", json!({})).unwrap();+ assert!(app.session.clipboard.as_ref().unwrap().layers.is_some());+ assert!(app.services.clipboard_get_image.as_mut().unwrap()().unwrap().is_some(), "native export succeeded");+ app.run("file.new", json!({"width": 80, "height": 60})).unwrap();++ // Another thread holds the OS clipboard open beyond arboard's bounded retry window.+ let (ready_tx, ready_rx) = std::sync::mpsc::channel();+ let (release_tx, release_rx) = std::sync::mpsc::channel();+ let holder = std::thread::spawn(move || {+ // A non-null window distinguishes this owner from arboard's null-window opens.+ let guard = winsafe::HWND::GetDesktopWindow().OpenClipboard().unwrap();+ ready_tx.send(()).unwrap();+ release_rx.recv_timeout(std::time::Duration::from_secs(10)).unwrap();+ drop(guard);+ });+ ready_rx.recv_timeout(std::time::Duration::from_secs(10)).unwrap();+ let contention = arboard::Clipboard::new().unwrap().get_image().err();+ let first = app.run(id, json!({}));+ release_tx.send(()).unwrap();+ holder.join().unwrap();+ assert!(matches!(contention, Some(arboard::Error::ClipboardOccupied)), "diagnostic must actually occupy the clipboard: {contention:?}");+ eprintln!("paste while clipboard occupied: {first:?}");+ assert!(first.is_err(), "report contention rather than a successful no-op");+ assert!(+ app.session.clipboard.as_ref().is_some_and(|clip| clip.layers.is_some()),+ "a temporary OS read failure must not discard the copied editable shape"+ );+ app.run(id, json!({})).unwrap();+ let state = app.session.active().unwrap();+ assert!(+ matches!(state.doc.layer(state.active_layer.unwrap()).unwrap().content, photocraft_doc::LayerContent::Shape(_)),+ "retry keeps the shape editable"+ );+ }+ }+#[cfg(all(unix, not(target_os = "macos")))]#[test]fn open_filters_cover_uppercase_and_mixed_case_extensions() {