Visual Studio Code, rebuilt in Rust
Rust
//! Electron's message_box_win.cc `ShowTaskDialogWstr`: a task dialog owned by the window, with the buttons
//! `task_dialog_buttons` gives and the checkbox as its verification text.
use std::ffi::c_void;
use futures::channel::oneshot;
use gpui::{PromptLevel, Window};
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use windows::Win32::Foundation::HWND;
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::UI::Controls::{
TASKDIALOG_BUTTON, TASKDIALOGCONFIG, TASKDIALOGCONFIG_0, TD_ERROR_ICON, TD_INFORMATION_ICON, TD_WARNING_ICON, TDF_ALLOW_DIALOG_CANCELLATION, TDF_POSITION_RELATIVE_TO_WINDOW, TDF_SIZE_TO_CONTENT, TDF_VERIFICATION_FLAG_CHECKED, TaskDialogIndirect,
};
use windows::core::{BOOL, HSTRING, PCWSTR};
use super::{OpenDialog, task_dialog_answer, task_dialog_buttons, task_dialog_texts};
/// The window's HWND; none on GPUI's test platform.
pub fn owner(window: &Window) -> Option<HWND> {
let RawWindowHandle::Win32(handle) = HasWindowHandle::window_handle(window).ok()?.as_raw() else { return None };
Some(HWND(handle.hwnd.get() as *mut c_void))
}
/// Shows the dialog modal to `owner` and answers the pressed button's index in `buttons` with the checkbox state.
pub fn show(owner: HWND, level: PromptLevel, message: &str, detail: Option<&str>, buttons: &[String], checkbox: Option<(String, bool)>, sender: oneshot::Sender<(usize, bool)>) {
let _open = OpenDialog::new();
let title = HSTRING::from(crate::product::name_long());
let (instruction, content) = task_dialog_texts(message, detail);
let (instruction, content) = (instruction.map(HSTRING::from), HSTRING::from(content));
let labels: Vec<(i32, HSTRING)> = task_dialog_buttons(buttons).into_iter().map(|(id, label)| (id, HSTRING::from(label))).collect();
let task_buttons: Vec<TASKDIALOG_BUTTON> = labels.iter().map(|(id, label)| TASKDIALOG_BUTTON { nButtonID: *id, pszButtonText: PCWSTR(label.as_ptr()) }).collect();
let verification = checkbox.as_ref().map(|(label, _)| HSTRING::from(label));
let mut flags = TDF_SIZE_TO_CONTENT | TDF_ALLOW_DIALOG_CANCELLATION | TDF_POSITION_RELATIVE_TO_WINDOW;
if checkbox.is_some_and(|(_, checked)| checked) {
flags |= TDF_VERIFICATION_FLAG_CHECKED;
}
let icon = match level {
PromptLevel::Info => TD_INFORMATION_ICON,
PromptLevel::Warning => TD_WARNING_ICON,
PromptLevel::Critical => TD_ERROR_ICON,
};
// SAFETY: the module handle of this executable.
let module = unsafe { GetModuleHandleW(None) }.unwrap_or_default();
let config = TASKDIALOGCONFIG {
cbSize: size_of::<TASKDIALOGCONFIG>() as u32,
hwndParent: owner,
hInstance: module.into(),
dwFlags: flags,
pszWindowTitle: PCWSTR(title.as_ptr()),
Anonymous1: TASKDIALOGCONFIG_0 { pszMainIcon: icon },
pszMainInstruction: instruction.as_ref().map_or(PCWSTR::null(), |instruction| PCWSTR(instruction.as_ptr())),
pszContent: PCWSTR(content.as_ptr()),
cButtons: task_buttons.len() as u32,
pButtons: task_buttons.as_ptr(),
pszVerificationText: verification.as_ref().map_or(PCWSTR::null(), |label| PCWSTR(label.as_ptr())),
..Default::default()
};
let (mut id, mut checked) = (0, BOOL(0));
// SAFETY: every string and the button array outlive the modal call. A failed dialog leaves `id` 0: cancel.
let _ = unsafe { TaskDialogIndirect(&config, Some(&raw mut id), None, Some(&raw mut checked)) };
let _ = sender.send((task_dialog_answer(id, buttons.len()), checked.as_bool()));
}