use std::ptr;
use std::rc::Rc;
use crate::clipboard::ClipboardContext;
use crate::fonts::SharedFontAtlas;
use crate::sys;
use super::Context;
use super::binding::{CTX_MUTEX, clear_current_context, no_current_context};
impl Context {
pub fn suspend(self) -> SuspendedContext {
let _guard = CTX_MUTEX.lock();
assert!(
self.is_current_context(),
"context to be suspended is not the active context"
);
clear_current_context();
SuspendedContext(self)
}
}
#[derive(Debug)]
pub struct SuspendedContext(pub(super) Context);
impl SuspendedContext {
pub fn try_create() -> crate::error::ImGuiResult<Self> {
Self::try_create_internal(None)
}
pub fn try_create_with_shared_font_atlas(
shared_font_atlas: SharedFontAtlas,
) -> crate::error::ImGuiResult<Self> {
Self::try_create_internal(Some(shared_font_atlas))
}
pub fn create() -> Self {
Self::try_create().expect("Failed to create Dear ImGui context")
}
pub fn create_with_shared_font_atlas(shared_font_atlas: SharedFontAtlas) -> Self {
Self::try_create_with_shared_font_atlas(shared_font_atlas)
.expect("Failed to create Dear ImGui context")
}
fn try_create_internal(
mut shared_font_atlas: Option<SharedFontAtlas>,
) -> crate::error::ImGuiResult<Self> {
let _guard = CTX_MUTEX.lock();
let shared_font_atlas_ptr = match &mut shared_font_atlas {
Some(atlas) => atlas.as_ptr_mut(),
None => ptr::null_mut(),
};
let raw = unsafe { sys::igCreateContext(shared_font_atlas_ptr) };
if raw.is_null() {
return Err(crate::error::ImGuiError::ContextCreation {
reason: "ImGui_CreateContext returned null".to_string(),
});
}
let ctx = Context {
raw,
alive: Rc::new(()),
shared_font_atlas,
ini_filename: None,
log_filename: None,
platform_name: None,
renderer_name: None,
clipboard_ctx: Box::new(ClipboardContext::dummy()),
ui: crate::ui::Ui::new(),
};
if ctx.is_current_context() {
clear_current_context();
}
Ok(SuspendedContext(ctx))
}
pub fn activate(self) -> Result<Context, SuspendedContext> {
let _guard = CTX_MUTEX.lock();
if no_current_context() {
unsafe {
sys::igSetCurrentContext(self.0.raw);
}
Ok(self.0)
} else {
Err(self)
}
}
}