1use crate::{
2 imgui::{self, Context, Ui},
3 util::Share,
4};
5use std::{ffi::c_void, ptr, sync::OnceLock};
6
7pub type MallocFn = unsafe extern "C" fn(size: usize, user_data: *mut c_void) -> *mut c_void;
8
9pub type FreeFn = unsafe extern "C" fn(ptr: *mut c_void, user_data: *mut c_void);
10
11pub static IG_CONTEXT: OnceLock<Share<Context>> = OnceLock::new();
13
14thread_local! {
15 pub static IG_UI: Ui<'static> = Ui::from_ctx(unsafe { imgui_context() });
17}
18
19pub unsafe fn init_imgui(
21 ctx: *mut imgui::sys::ImGuiContext,
22 malloc: Option<MallocFn>,
23 free: Option<FreeFn>,
24) {
25 imgui::sys::igSetCurrentContext(ctx);
26 imgui::sys::igSetAllocatorFunctions(malloc, free, ptr::null_mut());
27 IG_CONTEXT.get_or_init(|| Share::new(Context::current()));
28}
29
30#[inline]
32pub unsafe fn imgui_context() -> &'static Context {
33 IG_CONTEXT
34 .get()
35 .expect("imgui context not initialized")
36 .get()
37}
38
39#[inline]
41pub unsafe fn with_ui<R>(body: impl FnOnce(&Ui<'static>) -> R) -> R {
42 IG_UI.with(body)
43}