Skip to main content

dear_implot3d/ui/
token.rs

1use std::marker::PhantomData;
2
3use crate::{debug_end_plot, sys};
4use dear_imgui_rs::{DrawListMut, Ui};
5
6use super::binding::Plot3DContextBinding;
7
8/// RAII token that ends the plot on drop
9///
10/// This token is returned by `Plot3DBuilder::build()` and automatically calls
11/// `ImPlot3D_EndPlot()` when it goes out of scope, ensuring proper cleanup.
12pub struct Plot3DToken<'ui> {
13    pub(crate) binding: Plot3DContextBinding,
14    pub(crate) ui: &'ui Ui,
15    pub(crate) _lifetime: PhantomData<&'ui Ui>,
16}
17
18impl<'ui> Plot3DToken<'ui> {
19    /// Get the active 3D plot draw list as a frame-bound wrapper.
20    pub fn plot_draw_list(&self) -> Option<DrawListMut<'_>> {
21        self.binding.with_bound_context(|| {
22            let draw_list = unsafe { sys::ImPlot3D_GetPlotDrawList() };
23            if draw_list.is_null() {
24                None
25            } else {
26                Some(unsafe { DrawListMut::from_raw_mut(self.ui, draw_list) })
27            }
28        })
29    }
30}
31
32impl Drop for Plot3DToken<'_> {
33    fn drop(&mut self) {
34        let _ = self.binding.try_with_bound_context(|| unsafe {
35            debug_end_plot();
36            sys::ImPlot3D_EndPlot();
37        });
38    }
39}