From 316d54483d0703d4e2b0a02d1002a37e9c031346 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Tue, 27 Aug 2024 13:16:20 -0500 Subject: [PATCH] Add file dialog sample (#3226) --- .github/workflows/clippy.yml | 2 ++ .github/workflows/test.yml | 10 +++--- .../samples/windows/file_dialogs/Cargo.toml | 12 +++++++ crates/samples/windows/file_dialogs/build.rs | 16 ++++++++++ .../samples/windows/file_dialogs/manifest.xml | 19 ++++++++++++ .../samples/windows/file_dialogs/src/main.rs | 31 +++++++++++++++++++ 6 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 crates/samples/windows/file_dialogs/Cargo.toml create mode 100644 crates/samples/windows/file_dialogs/build.rs create mode 100644 crates/samples/windows/file_dialogs/manifest.xml create mode 100644 crates/samples/windows/file_dialogs/src/main.rs diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 87bdc1e28b..0769ab8c4f 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -81,6 +81,8 @@ jobs: run: cargo clippy -p sample_enum_windows - name: Clippy sample_enum_windows_sys run: cargo clippy -p sample_enum_windows_sys + - name: Clippy sample_file_dialogs + run: cargo clippy -p sample_file_dialogs - name: Clippy sample_kernel_event run: cargo clippy -p sample_kernel_event - name: Clippy sample_memory_buffer diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1f6b0225fd..a501e0eb5a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -105,6 +105,8 @@ jobs: run: cargo test -p sample_enum_windows --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_enum_windows_sys run: cargo test -p sample_enum_windows_sys --target ${{ matrix.target }} ${{ matrix.etc }} + - name: Test sample_file_dialogs + run: cargo test -p sample_file_dialogs --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_kernel_event run: cargo test -p sample_kernel_event --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test sample_memory_buffer @@ -151,10 +153,10 @@ jobs: run: cargo test -p test_alternate_success_code --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_arch run: cargo test -p test_arch --target ${{ matrix.target }} ${{ matrix.etc }} - - name: Test test_arch_feature - run: cargo test -p test_arch_feature --target ${{ matrix.target }} ${{ matrix.etc }} - name: Clean run: cargo clean + - name: Test test_arch_feature + run: cargo test -p test_arch_feature --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_array run: cargo test -p test_array --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_bcrypt @@ -253,10 +255,10 @@ jobs: run: cargo test -p test_result --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_return_handle run: cargo test -p test_return_handle --target ${{ matrix.target }} ${{ matrix.etc }} - - name: Test test_return_struct - run: cargo test -p test_return_struct --target ${{ matrix.target }} ${{ matrix.etc }} - name: Clean run: cargo clean + - name: Test test_return_struct + run: cargo test -p test_return_struct --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_riddle run: cargo test -p test_riddle --target ${{ matrix.target }} ${{ matrix.etc }} - name: Test test_standalone diff --git a/crates/samples/windows/file_dialogs/Cargo.toml b/crates/samples/windows/file_dialogs/Cargo.toml new file mode 100644 index 0000000000..115cbdc4bb --- /dev/null +++ b/crates/samples/windows/file_dialogs/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "sample_file_dialogs" +version = "0.0.0" +edition = "2021" +publish = false + +[dependencies.windows] +path = "../../../libs/windows" +features = [ + "Win32_UI_Shell_Common", + "Win32_System_Com", +] diff --git a/crates/samples/windows/file_dialogs/build.rs b/crates/samples/windows/file_dialogs/build.rs new file mode 100644 index 0000000000..00f1a307ad --- /dev/null +++ b/crates/samples/windows/file_dialogs/build.rs @@ -0,0 +1,16 @@ +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + + if std::env::var("CARGO_CFG_TARGET_ENV").unwrap() == "msvc" { + println!("cargo:rerun-if-changed=manifest.xml"); + println!("cargo:rustc-link-arg-bins=/MANIFEST:EMBED"); + + println!( + "cargo:rustc-link-arg-bins=/MANIFESTINPUT:{}", + std::path::Path::new("manifest.xml") + .canonicalize() + .unwrap() + .display() + ); + } +} diff --git a/crates/samples/windows/file_dialogs/manifest.xml b/crates/samples/windows/file_dialogs/manifest.xml new file mode 100644 index 0000000000..a35c4d237f --- /dev/null +++ b/crates/samples/windows/file_dialogs/manifest.xml @@ -0,0 +1,19 @@ + + + + true + PerMonitorV2 + + + + + + + + diff --git a/crates/samples/windows/file_dialogs/src/main.rs b/crates/samples/windows/file_dialogs/src/main.rs new file mode 100644 index 0000000000..b70a925da8 --- /dev/null +++ b/crates/samples/windows/file_dialogs/src/main.rs @@ -0,0 +1,31 @@ +use windows::{core::*, Win32::System::Com::*, Win32::UI::Shell::Common::*, Win32::UI::Shell::*}; + +fn main() -> Result<()> { + unsafe { + CoIncrementMTAUsage()?; + + let dialog: IFileSaveDialog = CoCreateInstance(&FileSaveDialog, None, CLSCTX_ALL)?; + + dialog.SetFileTypes(&[ + COMDLG_FILTERSPEC { + pszName: w!("Text files"), + pszSpec: w!("*.txt"), + }, + COMDLG_FILTERSPEC { + pszName: w!("All files"), + pszSpec: w!("*.*"), + }, + ])?; + + if dialog.Show(None).is_ok() { + let result = dialog.GetResult()?; + let path = result.GetDisplayName(SIGDN_FILESYSPATH)?; + println!("user picked: {}", path.display()); + CoTaskMemFree(Some(path.0 as _)); + } else { + println!("user canceled"); + } + + Ok(()) + } +}