Skip to main content

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