From dc2911dac66c838e508fc3872bcbaee62e6d482a Mon Sep 17 00:00:00 2001 From: Alexander Khosrowshahi Date: Mon, 9 Jun 2025 12:27:47 -0400 Subject: Add Checkbox input support to python api --- rust/src/interaction/handler.rs | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'rust/src/interaction/handler.rs') diff --git a/rust/src/interaction/handler.rs b/rust/src/interaction/handler.rs index f854f612..11929c94 100644 --- a/rust/src/interaction/handler.rs +++ b/rust/src/interaction/handler.rs @@ -27,6 +27,7 @@ pub fn register_interaction_handler(custom: R) { getOpenFileNameInput: Some(cb_get_open_file_name_input::), getSaveFileNameInput: Some(cb_get_save_file_name_input::), getDirectoryNameInput: Some(cb_get_directory_name_input::), + getCheckboxInput: Some(cb_get_checkbox_input::), getFormInput: Some(cb_get_form_input::), showMessageBox: Some(cb_show_message_box::), openUrl: Some(cb_open_url::), @@ -245,6 +246,26 @@ pub trait InteractionHandler: Sync + Send + 'static { form.get_field_with_name(prompt) .and_then(|f| f.try_value_string()) } + + fn get_checkbox_input( + &mut self, + prompt: &str, + title: &str, + default: Option, + ) -> Option { + let mut form = Form::new(title.to_owned()); + form.add_field(FormInputField::Checkbox { + prompt: prompt.to_string(), + default: default.map(|b| b == 1), + value: default.map(|b| b == 1)?, + }); + if !self.get_form_input(&mut form) { + return None; + } + form.get_field_with_name(prompt) + .and_then(|f| f.try_value_int()) + .map(|b| if b != 0 { 1 } else { 0 }) + } } pub struct InteractionHandlerTask { @@ -543,6 +564,27 @@ unsafe extern "C" fn cb_get_directory_name_input( } } +unsafe extern "C" fn cb_get_checkbox_input( + ctxt: *mut c_void, + result_ffi: *mut i64, + prompt: *const c_char, + title: *const c_char, + default_choice: *const i64, +) -> bool { + let ctxt = ctxt as *mut R; + let prompt = raw_to_string(prompt).unwrap(); + let title = raw_to_string(title).unwrap(); + let default = (!default_choice.is_null()).then(|| *default_choice); + let result = (*ctxt).get_checkbox_input(&prompt, &title, default); + if let Some(result) = result { + unsafe { *result_ffi = result }; + true + } else { + unsafe { *result_ffi = 0 }; + false + } +} + unsafe extern "C" fn cb_get_form_input( ctxt: *mut c_void, fields: *mut BNFormInputField, -- cgit v1.3.1