From: Olaf Wintermann Date: Fri, 15 May 2026 17:34:13 +0000 (+0200) Subject: add UiObject/UiDoc specific call_mainthread method, that gets a data reference X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=HEAD;p=note.git add UiObject/UiDoc specific call_mainthread method, that gets a data reference --- diff --git a/application/src/main.rs b/application/src/main.rs index fe0c235..1f2f184 100644 --- a/application/src/main.rs +++ b/application/src/main.rs @@ -30,7 +30,6 @@ mod window; mod backend; use std::env; -use tokio::sync::mpsc; use ui_rs::{ui}; use ui_rs::ui::*; use crate::backend::{Backend, BackendHandle, DynCmd}; diff --git a/ui-rs/src/ui/toolkit.rs b/ui-rs/src/ui/toolkit.rs index 48e26f3..64d518a 100644 --- a/ui-rs/src/ui/toolkit.rs +++ b/ui-rs/src/ui/toolkit.rs @@ -174,6 +174,38 @@ impl UiDoc { ui_add_action(self.ctx.ptr, cstr.as_ptr(), Some(action_event_wrapper::), ptr as *mut c_void); } } + + pub fn call_mainthread(&self, mut f: F) + where F: FnMut(&mut T) + 'static { + let doc = self.clone(); + call_mainthread(move || { + let data = unsafe { doc.get_data_ptr() }; + unsafe { + f(&mut *data); + } + }); + } +} + +impl UiObject { + pub fn window_data(self, mut f: F) + where F: FnMut(&mut T) { + unsafe { + let wdata_ptr: *mut T = ui_object_get_windowdata(self.ptr).cast(); + assert!(!wdata_ptr.is_null()); + f(&mut *wdata_ptr); + } + } + + pub fn call_mainthread(&self, mut f: F) + where F: FnMut(&mut T) + 'static { + let obj = self.clone(); + call_mainthread(move || { + obj.window_data(|wdata| { + f(wdata); + }); + }); + } } pub trait UiModel { @@ -687,6 +719,29 @@ pub unsafe fn ui_list_get<'a, T>(list: *const ffi::UiList, index: usize) -> Opti } +/* ----------------------------------- Event Loop ---------------------------------- */ + +pub fn call_mainthread(f: F) +where F: FnOnce() { + let b = Box::new(f); + let ptr = Box::into_raw(b); + unsafe { + ui_call_mainthread(mainthread_callback::, ptr as *mut c_void); + } +} + +extern "C" fn mainthread_callback(data: *mut c_void) -> c_int +where F: FnOnce() { + unsafe { + // Reconstruct the Box + let callback: Box = Box::from_raw(data as *mut F); + + (*callback)(); + } + + 0 +} + /* ----------------------------------- Config ---------------------------------- */ pub fn app_configdir() -> String { @@ -721,27 +776,6 @@ pub fn app_set_configdir(path: &str) { } } -pub fn call_mainthread(f: F) -where F: FnOnce() { - let b = Box::new(f); - let ptr = Box::into_raw(b); - unsafe { - ui_call_mainthread(mainthread_callback::, ptr as *mut c_void); - } -} - -extern "C" fn mainthread_callback(data: *mut c_void) -> c_int -where F: FnOnce() { - unsafe { - // Reconstruct the Box - let callback: Box = Box::from_raw(data as *mut F); - - (*callback)(); - } - - 0 -} - /* -------------------------------- C functions -------------------------------- */ extern "C" {