use parking_lot::ReentrantMutex;
use crate::sys;
use crate::PlotUi;
#[rustversion::attr(since(1.48), doc(alias = "ImPlotContext"))]
pub struct Context {
raw: *mut sys::ImPlotContext,
}
static CTX_MUTEX: ReentrantMutex<()> = parking_lot::const_reentrant_mutex(());
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 }
}
pub fn use_light_colors(&self) {
unsafe {
let style = sys::ImPlot_GetStyle();
assert_ne!(style, std::ptr::null_mut());
sys::ImPlot_StyleColorsLight(style);
}
}
pub fn use_dark_colors(&self) {
unsafe {
let style = sys::ImPlot_GetStyle();
assert_ne!(style, std::ptr::null_mut());
sys::ImPlot_StyleColorsDark(style);
}
}
pub fn use_classic_colors(&self) {
unsafe {
let style = sys::ImPlot_GetStyle();
assert_ne!(style, std::ptr::null_mut());
sys::ImPlot_StyleColorsClassic(style);
}
}
}
impl Drop for Context {
fn drop(&mut self) {
let _guard = CTX_MUTEX.lock();
unsafe {
sys::ImPlot_DestroyContext(self.raw);
}
}
}