diff --git a/src/lib.rs b/src/lib.rs index 0394c1c..e7887bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -402,11 +402,13 @@ impl ImGui { CURRENT_UI = Some(Ui { imgui: mem::transmute(self as &'a ImGui), frame_size, + needs_cleanup: false, }); } Ui { imgui: self, frame_size, + needs_cleanup: true, } } } @@ -491,6 +493,7 @@ pub struct DrawList<'a> { pub struct Ui<'ui> { imgui: &'ui ImGui, frame_size: FrameSize, + needs_cleanup: bool, } static FMT: &'static [u8] = b"%s\0"; @@ -545,7 +548,6 @@ impl<'ui> Ui<'ui> { raw: &mut *sys::igGetDrawData(), }; f(&self, draw_data)?; - CURRENT_UI = None; } Ok(()) } @@ -572,6 +574,17 @@ impl<'ui> Ui<'ui> { } } +impl<'a> Drop for Ui<'a> { + fn drop(&mut self) { + if self.needs_cleanup { + unsafe { + sys::igEndFrame(); + CURRENT_UI = None; + } + } + } +} + impl<'a> Ui<'a> { pub unsafe fn current_ui() -> Option<&'a Ui<'a>> { CURRENT_UI.as_ref()