use parking_lot::ReentrantMutex;
use crate::PlotUi;
pub struct Context {
raw: *mut sys::ImPlotContext,
}
lazy_static! {
static ref CTX_MUTEX: ReentrantMutex<()> = ReentrantMutex::new(());
}
fn no_current_context() -> bool {
let ctx = unsafe { sys::ImPlot_GetCurrentContext() };
ctx.is_null()
}
impl Context {
pub fn create() -> Self {
let _guard = CTX_MUTEX.lock();
assert!(
no_current_context(),
"A new active context cannot be created, because another one already exists"
);
let ctx = unsafe { sys::ImPlot_CreateContext() };
unsafe {
sys::ImPlot_SetCurrentContext(ctx);
}
Self { raw: ctx }
}
pub fn get_plot_ui(&self) -> PlotUi {
PlotUi { context: self }
}
}
impl Drop for Context {
fn drop(&mut self) {
let _guard = CTX_MUTEX.lock();
unsafe {
sys::ImPlot_DestroyContext(self.raw);
}
}
}