use cxx::let_cxx_string;
use crate::ffi;
use crate::widget::{AsWidget, Widget};
pub struct UiLoader {
ptr: *mut ffi::QUiLoader,
}
impl UiLoader {
pub fn new() -> Self {
let ptr = unsafe { ffi::QUiLoader_new() };
debug_assert!(!ptr.is_null(), "QUiLoader_new returned null");
Self { ptr }
}
pub fn load(
&self,
ui_path: &str,
parent: Option<&dyn AsWidget>,
) -> Option<Widget> {
debug_assert!(!self.ptr.is_null(), "UiLoader::load on null pointer");
let_cxx_string!(c_path = ui_path);
let parent_ptr = parent
.map(|p| p.widget_ptr())
.unwrap_or(std::ptr::null_mut());
let widget_ptr =
unsafe { ffi::QUiLoader_load(self.ptr, &c_path, parent_ptr) };
if widget_ptr.is_null() {
return None;
}
Some(Widget::from_raw(widget_ptr, parent.is_some()))
}
}
impl Default for UiLoader {
fn default() -> Self {
Self::new()
}
}
impl Drop for UiLoader {
fn drop(&mut self) {
if self.ptr.is_null() {
return;
}
unsafe { ffi::QUiLoader_delete(self.ptr) };
self.ptr = std::ptr::null_mut();
}
}