]> uap-core.de Git - note.git/commitdiff
add file dialog functions
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 12 May 2026 19:44:32 +0000 (21:44 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 12 May 2026 19:44:32 +0000 (21:44 +0200)
ui-rs/src/ui/window.rs

index 408e9c1e574b7088d88f99088d7ca9b0fda884f6..8b71fd47ccfff8ff0b8700987c02d973704648e9 100644 (file)
@@ -30,9 +30,9 @@
 #[allow(unused_imports)]
 
 use std::ffi::{c_char, c_int, c_void};
-use std::ffi::CString;
+use std::ffi::{c_uint, CString};
 use std::marker::PhantomData;
-use crate::ui::{event, ffi, toolkit, ui_object_ref, AppContext, EventWrapper, NoAppData, UiActions, UiModel};
+use crate::ui::{event, event_wrapper_oneshot, ffi, toolkit, ui_object_ref, AppContext, EventWrapper, NoAppData, UiActions, UiModel};
 use crate::ui::ffi::{UiButtonArgs, UiCallback, UiContext, UiDestructor, UiDialogArgs, UiObject};
 use crate::ui::widget::widget_fn;
 
@@ -241,6 +241,21 @@ impl<T> toolkit::UiObject<T> {
         build(&mut builder);
         builder.create();
     }
+
+    pub fn openfile_dialog<F>(&mut self, multiselect: bool, callback: F)
+    where F: FnMut(&mut event::Event<T>) + 'static {
+        openfile_dialog(self, multiselect as c_uint, callback);
+    }
+
+    pub fn selectdirectory_dialog<F>(&mut self, callback: F)
+    where F: FnMut(&mut event::Event<T>) + 'static {
+        openfile_dialog(self, 2, callback);
+    }
+
+    pub fn savefile_dialog<F>(&mut self, name: &str, callback: F)
+    where F: FnMut(&mut event::Event<T>) + 'static {
+        savefile_dialog(self, name, callback);
+    }
 }
 
 pub fn dialog_builder() -> DialogBuilder<NoAppData> {
@@ -260,6 +275,25 @@ where
     builder.create();
 }
 
+fn openfile_dialog<T, F>(obj: &toolkit::UiObject<T>, mode: c_uint, f: F)
+where F: FnMut(&mut event::Event<T>) + 'static {
+    let b: Box<EventWrapper<T>> = Box::new(EventWrapper { callback: Box::new(f) });
+    let ptr = Box::into_raw(b);
+    unsafe {
+        ui_openfiledialog(obj.ptr, mode, Some(event_wrapper_oneshot::<T>), ptr as *mut c_void);
+    }
+}
+
+fn savefile_dialog<T, F>(obj: &toolkit::UiObject<T>, name: &str, f: F)
+where F: FnMut(&mut event::Event<T>) + 'static {
+    let cstr = CString::new(name).unwrap();
+    let b: Box<EventWrapper<T>> = Box::new(EventWrapper { callback: Box::new(f) });
+    let ptr = Box::into_raw(b);
+    unsafe {
+        ui_savefiledialog(obj.ptr, cstr.as_ptr(), Some(event_wrapper_oneshot::<T>), ptr as *mut c_void);
+    }
+}
+
 
 /* ---------------------------------- C functions ---------------------------------- */
 
@@ -293,4 +327,7 @@ extern "C" {
     fn ui_dialog_args_set_result(args: *mut UiDialogArgs, callback: UiCallback);
     fn ui_dialog_args_set_resultdata(args: *mut UiDialogArgs, data: *mut c_void);
     fn ui_dialog_args_free(args: *mut UiDialogArgs);
+
+    fn ui_openfiledialog(obj: *mut ffi::UiObject, mode: c_uint, callback: UiCallback, data: *mut c_void);
+    fn ui_savefiledialog(obj: *mut ffi::UiObject, name: *const c_char, callback: UiCallback, data: *mut c_void);
 }