dear-imgui-rs 0.15.1

High-level Rust bindings to Dear ImGui v1.92.7 with docking, WGPU/GL backends, and extensions (ImPlot/ImPlot3D, ImNodes, ImGuizmo, file browser, reflection-based UI)
Documentation
use crate::sys;
use std::marker::PhantomData;

use super::super::util::finite_vec2;
use super::DrawListMut;

/// Tracks a clip rectangle pushed onto a draw-list clip stack.
///
/// The clip rectangle is popped when the token is dropped or when [`Self::pop`]
/// is called.
#[must_use]
pub struct DrawListClipRectToken<'draw_list> {
    draw_list: *mut sys::ImDrawList,
    _phantom: PhantomData<&'draw_list ()>,
}

impl<'draw_list> DrawListClipRectToken<'draw_list> {
    fn new(draw_list: *mut sys::ImDrawList) -> Self {
        Self {
            draw_list,
            _phantom: PhantomData,
        }
    }

    /// Pop the clip rectangle immediately instead of waiting for drop.
    #[doc(alias = "PopClipRect")]
    pub fn pop(self) {}

    /// Pop the clip rectangle immediately instead of waiting for drop.
    #[doc(alias = "PopClipRect")]
    pub fn end(self) {}
}

impl Drop for DrawListClipRectToken<'_> {
    fn drop(&mut self) {
        unsafe { sys::ImDrawList_PopClipRect(self.draw_list) }
    }
}

impl<'ui> DrawListMut<'ui> {
    /// Push a clip rectangle, optionally intersecting with the current clip rect.
    #[doc(alias = "PushClipRect")]
    pub fn push_clip_rect(
        &self,
        clip_rect_min: impl Into<sys::ImVec2>,
        clip_rect_max: impl Into<sys::ImVec2>,
        intersect_with_current: bool,
    ) -> DrawListClipRectToken<'_> {
        let clip_rect_min = finite_vec2(
            "DrawListMut::push_clip_rect()",
            "clip_rect_min",
            clip_rect_min,
        );
        let clip_rect_max = finite_vec2(
            "DrawListMut::push_clip_rect()",
            "clip_rect_max",
            clip_rect_max,
        );

        unsafe {
            sys::ImDrawList_PushClipRect(
                self.draw_list,
                clip_rect_min,
                clip_rect_max,
                intersect_with_current,
            )
        };
        DrawListClipRectToken::new(self.draw_list)
    }

    /// Push a full-screen clip rectangle.
    #[doc(alias = "PushClipRectFullScreen")]
    pub fn push_clip_rect_full_screen(&self) -> DrawListClipRectToken<'_> {
        unsafe { sys::ImDrawList_PushClipRectFullScreen(self.draw_list) };
        DrawListClipRectToken::new(self.draw_list)
    }

    /// Get current minimum clip rectangle point.
    pub fn clip_rect_min(&self) -> [f32; 2] {
        let out = unsafe { sys::ImDrawList_GetClipRectMin(self.draw_list) };
        out.into()
    }

    /// Get current maximum clip rectangle point.
    pub fn clip_rect_max(&self) -> [f32; 2] {
        let out = unsafe { sys::ImDrawList_GetClipRectMax(self.draw_list) };
        out.into()
    }

    /// Convenience: push a clip rect, run f, pop.
    pub fn with_clip_rect<F, R>(
        &self,
        clip_rect_min: impl Into<sys::ImVec2>,
        clip_rect_max: impl Into<sys::ImVec2>,
        f: F,
    ) -> R
    where
        F: FnOnce() -> R,
    {
        let _clip_rect = self.push_clip_rect(clip_rect_min, clip_rect_max, false);
        f()
    }
}