From 697c1d75fd5c0622853dbeb0d5e5cee1ead4d507 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Mon, 18 May 2026 19:44:20 +0200 Subject: [PATCH] add obj to Event --- application/src/window.rs | 2 +- ui-rs/src/ui/event.rs | 8 +++++++- ui-rs/src/ui/toolkit.rs | 17 +++++++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/application/src/window.rs b/application/src/window.rs index 507afa1..e2931fe 100644 --- a/application/src/window.rs +++ b/application/src/window.rs @@ -51,7 +51,7 @@ impl MainWindow { #[action] pub fn new_notebook(&mut self, _event: &ActionEvent) { - println!("new notebook"); + } #[action] diff --git a/ui-rs/src/ui/event.rs b/ui-rs/src/ui/event.rs index b39967f..e1d43f5 100644 --- a/ui-rs/src/ui/event.rs +++ b/ui-rs/src/ui/event.rs @@ -29,10 +29,11 @@ #![allow(dead_code)] use std::ffi::{c_char, c_void, CStr, CString}; -use crate::ui::{event, ffi, UiDouble, UiInteger, UiString, UiText, UiRange, ListSelection}; +use crate::ui::{event, ffi, UiDouble, UiInteger, UiString, UiText, UiRange, ListSelection, UiObject}; use crate::ui::ffi::{UiEvent}; pub struct Event<'a, T> { + pub obj: &'a mut UiObject, pub data: &'a mut T, pub event_type: EventType<'a>, pub intval: i32, @@ -151,7 +152,11 @@ pub extern "C" fn event_wrapper(e: *const ffi::UiEvent, data: *mut c_void) { let mut event_data = get_event_data(e); let event_type = get_event_type(&mut event_data); + let obj_ptr = unsafe { ui_event_get_obj(e) }; + let mut obj: UiObject = UiObject::from_ptr(obj_ptr); + let mut event = Event { + obj: &mut obj, data: wdata, event_type: event_type, intval: ev_int, @@ -196,6 +201,7 @@ pub extern "C" fn action_event_wrapper(e: *const ffi::UiEvent, data: *mut c_v } extern "C" { + fn ui_event_get_obj(event: *const UiEvent) -> *mut ffi::UiObject; fn ui_event_get_windowdata(event: *const UiEvent) -> *const c_void; fn ui_event_get_document(event: *const UiEvent) -> *const c_void; fn ui_event_get_eventdata(event: *const UiEvent) -> *mut c_void; diff --git a/ui-rs/src/ui/toolkit.rs b/ui-rs/src/ui/toolkit.rs index 050dd1b..481e6d9 100644 --- a/ui-rs/src/ui/toolkit.rs +++ b/ui-rs/src/ui/toolkit.rs @@ -195,12 +195,25 @@ impl UiDoc { } impl UiObject { + pub fn from_ptr(ptr: *mut ffi::UiObject) -> UiObject { + let ctx_ptr = unsafe { ui_object_get_context(ptr) }; + unsafe { + ui_object_ref(ptr); + } + UiObject { + ptr: ptr, + ctx: UiContext::from_ptr(ctx_ptr), + _data: PhantomData + } + } + 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); + if !wdata_ptr.is_null() { + f(&mut *wdata_ptr); + } } } -- 2.47.3