use std::marker::PhantomData;
use std::mem;
+use std::ptr::null_mut;
+use std::sync::{Arc, Mutex};
use crate::ui::ffi::{UiCallback, UiThreadFunc};
pub struct UiContext {
ui_add_action(self.ctx.ptr, cstr.as_ptr(), Some(action_event_wrapper::<T>), ptr as *mut c_void);
}
}
-
- pub fn call_mainthread<F>(&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<T> UiObject<T> {
UiObjRef::from_ptr(self.ptr)
}
- pub fn window_data<F>(&mut self, mut f: F)
- where F: FnMut(&mut T) {
+ pub fn obj_proxy(&self) -> UiObjProxy<T> {
+ UiObjProxy::from_ptr(self.ptr)
+ }
+
+ pub fn window_data<F>(&mut self, f: F)
+ where F: FnOnce(&mut T) {
unsafe {
let wdata_ptr: *mut T = ui_object_get_windowdata(self.ptr).cast();
if !wdata_ptr.is_null() {
}
}
- pub fn call_mainthread<F>(&self, mut f: F)
- where F: FnMut(&mut T) + 'static {
- let mut obj = self.clone();
- call_mainthread(move || {
- obj.window_data(|wdata| {
- f(wdata);
- });
- });
- }
-
pub fn call_action(&self, action: &str) {
self.ctx.call_action(action);
}
}
}
+impl<T> Default for UiObjRef<T> {
+ fn default() -> Self {
+ UiObjRef::from_ptr(std::ptr::null_mut())
+ }
+}
+
+
+pub struct UiObjProxy<T> {
+ ptr: *mut ffi::UiObject,
+ _data: PhantomData<T>
+}
+
+unsafe impl<T> Send for UiObjProxy<T> {}
+
+impl<T> UiObjProxy<T> {
+ fn from_ptr(ptr: *mut ffi::UiObject) -> UiObjProxy<T> {
+ unsafe {
+ ui_object_ref(ptr);
+ }
+ UiObjProxy { ptr: ptr, _data: PhantomData }
+ }
+
+ pub fn call_mainthread<F>(self, f: F)
+ where F: FnOnce(&mut T) + Send + 'static {
+ call_mainthread(|| {
+ let mut obj: UiObject<T> = UiObject::from_ptr(self.ptr);
+ obj.window_data(|wdata| {
+ f(wdata);
+ });
+ drop(self);
+ });
+ }
+}
+
+impl<T> Drop for UiObjProxy<T> {
+ fn drop(&mut self) {
+ unsafe {
+ ui_mainthread_object_unref(self.ptr);
+ }
+ }
+}
+
+
pub struct UiText {
pub ptr: *mut ffi::UiText
}
}
}
+ pub fn selected_index(&self) -> i32 {
+ unsafe {
+ ui_list_getselection(self.ptr)
+ }
+ }
+
pub fn set_selection(&mut self, sel: &Vec<i32>) {
unsafe {
ui_list_set_selected_indices(self.ptr, sel.as_ptr(), sel.len() as c_int);
/* ----------------------------------- Event Loop ---------------------------------- */
pub fn call_mainthread<F>(f: F)
-where F: FnOnce() {
+where F: FnOnce() + Send {
let b = Box::new(f);
let ptr = Box::into_raw(b);
unsafe {
pub fn ui_object_ref(obj: *mut ffi::UiObject);
pub fn ui_object_unref(obj: *mut ffi::UiObject);
+ fn ui_mainthread_object_unref(obj: *mut ffi::UiObject);
+ fn ui_mainthread_document_unref(doc: *mut c_void);
+
fn ui_list_new(ctx: *mut ffi::UiContext, name: *const c_char) -> *mut ffi::UiList;
fn ui_list_new2(ctx: *mut ffi::UiContext, name: *const c_char, init: UiListInitFunc, userdata: *mut c_void) -> *mut ffi::UiList;
fn ui_list_class_set_first(list: *mut ffi::UiList, func: UiListFirstFunc);