arcdps\globals/
imgui.rs

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
11/// ImGui context.
12pub static IG_CONTEXT: OnceLock<Share<Context>> = OnceLock::new();
13
14thread_local! {
15    /// ImGui UI.
16    pub static IG_UI: Ui<'static> = Ui::from_ctx(unsafe { imgui_context() });
17}
18
19/// Initializes ImGui information.
20pub unsafe fn init_imgui(
21    ctx: *mut imgui::sys::ImGuiContext,
22    malloc: Option<MallocFn>,
23    free: Option<FreeFn>,
24) {
25    unsafe {
26        imgui::sys::igSetCurrentContext(ctx);
27        imgui::sys::igSetAllocatorFunctions(malloc, free, ptr::null_mut());
28        IG_CONTEXT.get_or_init(|| Share::new(Context::current()));
29    }
30}
31
32/// Retrieves the [`imgui::Context`].
33#[inline]
34pub unsafe fn imgui_context() -> &'static Context {
35    unsafe {
36        IG_CONTEXT
37            .get()
38            .expect("imgui context not initialized")
39            .get()
40    }
41}
42
43/// Retrieves the [`imgui::Ui`] for rendering.
44#[inline]
45pub unsafe fn with_ui<R>(body: impl FnOnce(&Ui<'static>) -> R) -> R {
46    IG_UI.with(body)
47}