Skip to content

Commit

Permalink
Add file dialog sample (#3226)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Aug 27, 2024
1 parent 88a80f2 commit 316d544
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions crates/samples/windows/file_dialogs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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",
]
16 changes: 16 additions & 0 deletions crates/samples/windows/file_dialogs/build.rs
Original file line number Diff line number Diff line change
@@ -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()
);
}
}
19 changes: 19 additions & 0 deletions crates/samples/windows/file_dialogs/manifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<asmv3:application>
<asmv3:windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</asmv3:windowsSettings>
</asmv3:application>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"/>
</dependentAssembly>
</dependency>
</assembly>
31 changes: 31 additions & 0 deletions crates/samples/windows/file_dialogs/src/main.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}

0 comments on commit 316d544

Please sign in to comment.