windows: Fix panic when quitting dialogs that do not have a cancel button (#40348)

`TaskDialogIndirect` may return `IDCANCEL` when the user quits the
dialog via escape or alt+f4, so we need to account for that.

Fixes ZED-25H

Release Notes:

- Fixed panic when hitting escape in dialogs on windows
This commit is contained in:
Lukas Wirth
2025-10-16 09:44:44 +00:00
committed by GitHub
parent f2e8d0cc08
commit e016c05959
+11 -9
View File
@@ -643,10 +643,12 @@ impl PlatformWindow for WindowsWindow {
let mut btn_encoded = Vec::new(); let mut btn_encoded = Vec::new();
for (index, btn) in answers.iter().enumerate() { for (index, btn) in answers.iter().enumerate() {
let encoded = HSTRING::from(btn.label().as_ref()); let encoded = HSTRING::from(btn.label().as_ref());
let button_id = if btn.is_cancel() { let button_id = match btn {
IDCANCEL.0 PromptButton::Ok(_) => IDOK.0,
} else { PromptButton::Cancel(_) => IDCANCEL.0,
index as i32 - 100 // the first few low integer values are reserved for known buttons
// so for simplicity we just go backwards from -1
PromptButton::Other(_) => -(index as i32) - 1,
}; };
button_id_map.push(button_id); button_id_map.push(button_id);
buttons.push(TASKDIALOG_BUTTON { buttons.push(TASKDIALOG_BUTTON {
@@ -664,11 +666,11 @@ impl PlatformWindow for WindowsWindow {
.context("unable to create task dialog") .context("unable to create task dialog")
.log_err(); .log_err();
let clicked = button_id_map if let Some(clicked) =
.iter() button_id_map.iter().position(|&button_id| button_id == res)
.position(|&button_id| button_id == res) {
.unwrap(); let _ = done_tx.send(clicked);
let _ = done_tx.send(clicked); }
} }
}) })
.detach(); .detach();