use super::*;
use crate::context::binding::{CTX_MUTEX, with_bound_context};
impl Ui {
pub(super) fn assert_finite_f32(caller: &str, name: &str, value: f32) {
assert!(value.is_finite(), "{caller} {name} must be finite");
}
pub(super) fn assert_finite_vec2(caller: &str, name: &str, value: [f32; 2]) {
assert!(
value[0].is_finite() && value[1].is_finite(),
"{caller} {name} must contain finite values"
);
}
pub(crate) fn new(ctx: *mut sys::ImGuiContext, ctx_alive: crate::ContextAliveToken) -> Self {
Ui {
ctx,
ctx_alive,
buffer: UnsafeCell::new(UiBuffer::new(1024)),
}
}
pub(crate) fn context_raw(&self) -> *mut sys::ImGuiContext {
self.ctx
}
pub(crate) fn context_alive_token(&self) -> crate::ContextAliveToken {
self.ctx_alive.clone()
}
pub(crate) fn run_with_bound_context<R>(&self, f: impl FnOnce() -> R) -> R {
let _guard = CTX_MUTEX.lock();
with_bound_context(self.ctx, f)
}
pub fn with_bound_context<R>(&self, f: impl FnOnce() -> R) -> R {
self.run_with_bound_context(f)
}
#[doc(alias = "GetIO")]
pub fn io(&self) -> &crate::io::Io {
self.run_with_bound_context(|| unsafe {
let io = sys::igGetIO_Nil();
if io.is_null() {
panic!("Ui::io() requires an active ImGui context");
}
&*(io as *const crate::io::Io)
})
}
pub(crate) fn scratch_txt(&self, txt: impl AsRef<str>) -> *const std::os::raw::c_char {
unsafe {
let handle = &mut *self.buffer.get();
handle.scratch_txt(txt)
}
}
pub(crate) fn scratch_txt_two(
&self,
txt_0: impl AsRef<str>,
txt_1: impl AsRef<str>,
) -> (*const std::os::raw::c_char, *const std::os::raw::c_char) {
unsafe {
let handle = &mut *self.buffer.get();
handle.scratch_txt_two(txt_0, txt_1)
}
}
pub(crate) fn scratch_txt_with_opt(
&self,
txt_0: impl AsRef<str>,
txt_1: Option<impl AsRef<str>>,
) -> (*const std::os::raw::c_char, *const std::os::raw::c_char) {
unsafe {
let handle = &mut *self.buffer.get();
handle.scratch_txt_with_opt(txt_0, txt_1)
}
}
pub(crate) fn scratch_buffer(&self) -> &UnsafeCell<UiBuffer> {
&self.buffer
}
#[doc(alias = "GetID")]
pub fn get_id(&self, label: &str) -> Id {
let label = self.scratch_txt(label);
self.run_with_bound_context(|| unsafe { Id::from(sys::igGetID_Str(label)) })
}
}