From 76331e13efebf7c40a88ea81d3e42b297116b81c Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 18 Jan 2024 11:01:27 -0600 Subject: [PATCH] Add task dialog samples (#2802) --- .github/workflows/clippy.yml | 2 + .github/workflows/test.yml | 4 +- .../windows-sys/task_dialog/Cargo.toml | 12 +++++ .../samples/windows-sys/task_dialog/build.rs | 16 +++++++ .../windows-sys/task_dialog/manifest.xml | 19 ++++++++ .../windows-sys/task_dialog/src/main.rs | 44 +++++++++++++++++++ crates/samples/windows/task_dialog/Cargo.toml | 12 +++++ crates/samples/windows/task_dialog/build.rs | 16 +++++++ .../samples/windows/task_dialog/manifest.xml | 19 ++++++++ .../samples/windows/task_dialog/src/main.rs | 43 ++++++++++++++++++ 10 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 crates/samples/windows-sys/task_dialog/Cargo.toml create mode 100644 crates/samples/windows-sys/task_dialog/build.rs create mode 100644 crates/samples/windows-sys/task_dialog/manifest.xml create mode 100644 crates/samples/windows-sys/task_dialog/src/main.rs create mode 100644 crates/samples/windows/task_dialog/Cargo.toml create mode 100644 crates/samples/windows/task_dialog/build.rs create mode 100644 crates/samples/windows/task_dialog/manifest.xml create mode 100644 crates/samples/windows/task_dialog/src/main.rs diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 7104bf229d..9f69a9da0c 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -63,6 +63,8 @@ jobs: cargo clippy -p sample_shell && cargo clippy -p sample_simple && cargo clippy -p sample_spellchecker && + cargo clippy -p sample_task_dialog && + cargo clippy -p sample_task_dialog_sys && cargo clippy -p sample_thread_pool_work && cargo clippy -p sample_thread_pool_work_sys && cargo clippy -p sample_uiautomation && diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 30fa52b2df..9f1d9777ad 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -70,6 +70,8 @@ jobs: cargo test -p sample_shell && cargo test -p sample_simple && cargo test -p sample_spellchecker && + cargo test -p sample_task_dialog && + cargo test -p sample_task_dialog_sys && cargo test -p sample_thread_pool_work && cargo test -p sample_thread_pool_work_sys && cargo test -p sample_uiautomation && @@ -100,8 +102,8 @@ jobs: cargo test -p test_does_not_return && cargo test -p test_enums && cargo test -p test_error && - cargo test -p test_event && cargo clean && + cargo test -p test_event && cargo test -p test_extensions && cargo test -p test_handles && cargo test -p test_helpers && diff --git a/crates/samples/windows-sys/task_dialog/Cargo.toml b/crates/samples/windows-sys/task_dialog/Cargo.toml new file mode 100644 index 0000000000..c6001abb56 --- /dev/null +++ b/crates/samples/windows-sys/task_dialog/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "sample_task_dialog_sys" +version = "0.0.0" +edition = "2021" +publish = false + +[dependencies.windows-sys] +path = "../../../libs/sys" +features = [ + "Win32_UI_Controls", + "Win32_UI_WindowsAndMessaging", +] diff --git a/crates/samples/windows-sys/task_dialog/build.rs b/crates/samples/windows-sys/task_dialog/build.rs new file mode 100644 index 0000000000..00f1a307ad --- /dev/null +++ b/crates/samples/windows-sys/task_dialog/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-sys/task_dialog/manifest.xml b/crates/samples/windows-sys/task_dialog/manifest.xml new file mode 100644 index 0000000000..a35c4d237f --- /dev/null +++ b/crates/samples/windows-sys/task_dialog/manifest.xml @@ -0,0 +1,19 @@ + + + + true + PerMonitorV2 + + + + + + + + diff --git a/crates/samples/windows-sys/task_dialog/src/main.rs b/crates/samples/windows-sys/task_dialog/src/main.rs new file mode 100644 index 0000000000..37583d4025 --- /dev/null +++ b/crates/samples/windows-sys/task_dialog/src/main.rs @@ -0,0 +1,44 @@ +use windows_sys::{core::*, Win32::Foundation::*, Win32::UI::Controls::*}; + +fn main() { + unsafe { + let mut config = TASKDIALOGCONFIG { + cbSize: std::mem::size_of::() as _, + ..std::mem::zeroed() + }; + + let buttons = [TASKDIALOG_BUTTON { + nButtonID: 123, + pszButtonText: w!("Let's do it"), + }]; + + config.pszWindowTitle = w!("Window title"); + config.pszMainInstruction = w!("Main instruction"); + config.pszContent = w!("Content"); + config.pButtons = buttons.as_ptr(); + config.cButtons = buttons.len() as _; + config.pfCallback = Some(callback); + config.dwFlags = TDF_USE_COMMAND_LINKS | TDF_ALLOW_DIALOG_CANCELLATION; + + let mut selection = 0; + + TaskDialogIndirect( + &config, + &mut selection, + std::ptr::null_mut(), + std::ptr::null_mut(), + ); + + if selection == buttons[0].nButtonID { + println!("custom button"); + }; + } +} + +extern "system" fn callback(_: HWND, notification: u32, _: WPARAM, _: LPARAM, _: isize) -> HRESULT { + if notification == TDN_BUTTON_CLICKED as _ { + println!("button clicked"); + } + + 0 +} diff --git a/crates/samples/windows/task_dialog/Cargo.toml b/crates/samples/windows/task_dialog/Cargo.toml new file mode 100644 index 0000000000..028232b4e4 --- /dev/null +++ b/crates/samples/windows/task_dialog/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "sample_task_dialog" +version = "0.0.0" +edition = "2021" +publish = false + +[dependencies.windows] +path = "../../../libs/windows" +features = [ + "Win32_UI_Controls", + "Win32_UI_WindowsAndMessaging", +] diff --git a/crates/samples/windows/task_dialog/build.rs b/crates/samples/windows/task_dialog/build.rs new file mode 100644 index 0000000000..00f1a307ad --- /dev/null +++ b/crates/samples/windows/task_dialog/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/task_dialog/manifest.xml b/crates/samples/windows/task_dialog/manifest.xml new file mode 100644 index 0000000000..a35c4d237f --- /dev/null +++ b/crates/samples/windows/task_dialog/manifest.xml @@ -0,0 +1,19 @@ + + + + true + PerMonitorV2 + + + + + + + + diff --git a/crates/samples/windows/task_dialog/src/main.rs b/crates/samples/windows/task_dialog/src/main.rs new file mode 100644 index 0000000000..df0c8733c5 --- /dev/null +++ b/crates/samples/windows/task_dialog/src/main.rs @@ -0,0 +1,43 @@ +use windows::{core::*, Win32::Foundation::*, Win32::UI::Controls::*}; + +fn main() -> Result<()> { + unsafe { + let mut config = TASKDIALOGCONFIG { + cbSize: std::mem::size_of::() as _, + ..Default::default() + }; + + let buttons = [TASKDIALOG_BUTTON { + nButtonID: 123, + pszButtonText: w!("Let's do it"), + }]; + + config.pszWindowTitle = w!("Window title"); + config.pszMainInstruction = w!("Main instruction"); + config.pszContent = w!("Content"); + config.pButtons = buttons.as_ptr(); + config.cButtons = buttons.len() as _; + config.pfCallback = Some(callback); + + config.dwFlags = + TASKDIALOG_FLAGS(TDF_USE_COMMAND_LINKS.0 | TDF_ALLOW_DIALOG_CANCELLATION.0); + + let mut selection = 0; + + TaskDialogIndirect(&config, Some(&mut selection), None, None)?; + + if selection == buttons[0].nButtonID { + println!("custom button"); + }; + + Ok(()) + } +} + +extern "system" fn callback(_: HWND, notification: u32, _: WPARAM, _: LPARAM, _: isize) -> HRESULT { + if notification == TDN_BUTTON_CLICKED.0 as _ { + println!("button clicked"); + } + + HRESULT(0) +}