Skip to main content

dear_implot3d/style/
tokens.rs

1use crate::Plot3DUi;
2use crate::sys;
3use crate::ui::Plot3DContextBinding;
4use dear_imgui_rs::ContextAliveToken;
5use std::marker::PhantomData;
6use std::rc::Rc;
7
8/// Token for managing style variable changes.
9#[must_use]
10pub struct StyleVarToken<'ui> {
11    pub(super) binding: Plot3DContextBinding,
12    pub(super) imgui_alive: Option<ContextAliveToken>,
13    pub(super) was_popped: bool,
14    pub(super) _lifetime: PhantomData<&'ui Plot3DUi<'ui>>,
15    pub(super) _not_send_or_sync: PhantomData<Rc<()>>,
16}
17
18impl StyleVarToken<'_> {
19    /// Pop this style variable from the stack.
20    pub fn pop(mut self) {
21        self.pop_inner();
22    }
23
24    fn pop_inner(&mut self) {
25        if self.was_popped {
26            panic!("Attempted to pop an ImPlot3D style var token twice.");
27        }
28        assert_imgui_alive(&self.imgui_alive, "dear-implot3d: StyleVarToken");
29        let _guard = self.binding.bind();
30        unsafe { sys::ImPlot3D_PopStyleVar(1) };
31        self.was_popped = true;
32    }
33}
34
35impl Drop for StyleVarToken<'_> {
36    fn drop(&mut self) {
37        if !self.was_popped {
38            self.pop_inner();
39        }
40    }
41}
42
43/// Token for managing style color changes.
44#[must_use]
45pub struct StyleColorToken<'ui> {
46    pub(super) binding: Plot3DContextBinding,
47    pub(super) imgui_alive: Option<ContextAliveToken>,
48    pub(super) was_popped: bool,
49    pub(super) _lifetime: PhantomData<&'ui Plot3DUi<'ui>>,
50    pub(super) _not_send_or_sync: PhantomData<Rc<()>>,
51}
52
53impl StyleColorToken<'_> {
54    /// Pop this style color from the stack.
55    pub fn pop(mut self) {
56        self.pop_inner();
57    }
58
59    fn pop_inner(&mut self) {
60        if self.was_popped {
61            panic!("Attempted to pop an ImPlot3D style color token twice.");
62        }
63        assert_imgui_alive(&self.imgui_alive, "dear-implot3d: StyleColorToken");
64        let _guard = self.binding.bind();
65        unsafe { sys::ImPlot3D_PopStyleColor(1) };
66        self.was_popped = true;
67    }
68}
69
70impl Drop for StyleColorToken<'_> {
71    fn drop(&mut self) {
72        if !self.was_popped {
73            self.pop_inner();
74        }
75    }
76}
77
78/// Token for managing colormap changes.
79#[must_use]
80pub struct ColormapToken<'ui> {
81    pub(super) binding: Plot3DContextBinding,
82    pub(super) imgui_alive: Option<ContextAliveToken>,
83    pub(super) was_popped: bool,
84    pub(super) _lifetime: PhantomData<&'ui Plot3DUi<'ui>>,
85    pub(super) _not_send_or_sync: PhantomData<Rc<()>>,
86}
87
88impl ColormapToken<'_> {
89    /// Pop this colormap from the stack.
90    pub fn pop(mut self) {
91        self.pop_inner();
92    }
93
94    fn pop_inner(&mut self) {
95        if self.was_popped {
96            panic!("Attempted to pop an ImPlot3D colormap token twice.");
97        }
98        assert_imgui_alive(&self.imgui_alive, "dear-implot3d: ColormapToken");
99        let _guard = self.binding.bind();
100        unsafe { sys::ImPlot3D_PopColormap(1) };
101        self.was_popped = true;
102    }
103}
104
105impl Drop for ColormapToken<'_> {
106    fn drop(&mut self) {
107        if !self.was_popped {
108            self.pop_inner();
109        }
110    }
111}
112
113fn assert_imgui_alive(alive: &Option<ContextAliveToken>, caller: &str) {
114    if let Some(alive) = alive {
115        assert!(alive.is_alive(), "{caller}: ImGui context has been dropped");
116    }
117}