Skip to main content

dear_implot3d/ui/
scope.rs

1use crate::Plot3DUi;
2use crate::layout::NextItemStyle;
3use dear_imgui_rs::{DrawListMut, NativeWindowScope};
4
5/// Axis and box configuration, borrowed before the plot locks setup.
6///
7/// ```compile_fail
8/// use dear_implot3d::{Line3DFlags, Plot3DSetup};
9/// fn draw_during_setup(setup: &Plot3DSetup<'_>) {
10///     setup.plot_line_f32("line", &[0.0; 2], &[0.0; 2], &[0.0; 2], Line3DFlags::NONE);
11/// }
12/// ```
13pub struct Plot3DSetup<'plot> {
14    pub(crate) plot: &'plot Plot3DScope<'plot>,
15}
16
17impl Plot3DSetup<'_> {
18    pub(crate) fn with_bound_context<R>(&self, f: impl FnOnce() -> R) -> R {
19        self.plot.with_bound_context(f)
20    }
21}
22
23/// A drawing interface borrowed from an open plot after setup has finished.
24///
25/// ```compile_fail
26/// use dear_implot3d::Plot3DScope;
27/// fn reconfigure(plot: &Plot3DScope<'_>) { plot.setup_box_scale(1.0, 1.0, 1.0); }
28/// ```
29///
30/// ```compile_fail
31/// use dear_implot3d::{Plot3DScope, Plot3DUi};
32/// fn escape<'a>(ui: &Plot3DUi<'a>) -> &'a Plot3DScope<'a> {
33///     ui.begin_plot("plot").build(|plot| plot).unwrap()
34/// }
35/// ```
36pub struct Plot3DScope<'plot> {
37    pub(crate) ui: Plot3DUi<'plot>,
38    pub(crate) host: &'plot NativeWindowScope<'plot>,
39    pub(crate) next_item: NextItemStyle,
40}
41
42impl Plot3DScope<'_> {
43    pub(crate) fn with_bound_context<R>(&self, f: impl FnOnce() -> R) -> R {
44        assert!(
45            self.host.is_current(),
46            "ImPlot3D requires the plot's host window and table cell"
47        );
48        self.ui.with_bound_context(f)
49    }
50
51    /// Borrows the draw list of the window hosting this plot.
52    pub fn plot_draw_list(&self) -> DrawListMut<'_> {
53        // Upstream GetPlotDrawList returns exactly ImGui::GetWindowDrawList(). The core wrapper
54        // also remembers the window Begin instance for subsequent draw-list operations.
55        self.with_bound_context(|| self.ui.imgui.get_window_draw_list())
56    }
57}