Skip to main content

dear_implot/context/
token.rs

1use super::callbacks::PlotScopeGuard;
2use super::core::PlotContextBinding;
3use crate::sys;
4
5/// Token that represents an active plot
6///
7/// The plot will be automatically ended when this token is dropped.
8pub struct PlotToken<'ui> {
9    binding: PlotContextBinding,
10    imgui_alive: Option<dear_imgui_rs::ContextAliveToken>,
11    _scope: PlotScopeGuard,
12    _lifetime: std::marker::PhantomData<&'ui ()>,
13}
14
15impl<'ui> PlotToken<'ui> {
16    /// Create a new PlotToken (internal use only)
17    pub(crate) fn new(
18        binding: PlotContextBinding,
19        imgui_alive: Option<dear_imgui_rs::ContextAliveToken>,
20    ) -> Self {
21        Self {
22            binding,
23            imgui_alive,
24            _scope: PlotScopeGuard::new(),
25            _lifetime: std::marker::PhantomData,
26        }
27    }
28
29    /// Manually end the plot
30    ///
31    /// This is called automatically when the token is dropped,
32    /// but you can call it manually if needed.
33    pub fn end(self) {
34        // The actual ending happens in Drop
35    }
36}
37
38impl<'ui> Drop for PlotToken<'ui> {
39    fn drop(&mut self) {
40        if let Some(alive) = &self.imgui_alive {
41            assert!(
42                alive.is_alive(),
43                "dear-implot: ImGui context has been dropped"
44            );
45        }
46        self.binding.bind("dear-implot: PlotToken");
47        unsafe {
48            sys::ImPlot_EndPlot();
49        }
50    }
51}