Skip to main content

dear_imgui_rs/layout/
clip_rect.rs

1use super::validation::assert_finite_vec2;
2use crate::Ui;
3use crate::sys;
4
5create_token!(
6    /// Tracks a pushed clip rect that will be popped on drop.
7    pub struct ClipRectToken<'ui>;
8
9    /// Pops a clip rect pushed with [`Ui::push_clip_rect`].
10    drop { unsafe { sys::igPopClipRect() } }
11);
12
13impl Ui {
14    /// Push a clipping rectangle in screen space.
15    #[doc(alias = "PushClipRect")]
16    pub fn push_clip_rect(
17        &self,
18        min: impl Into<[f32; 2]>,
19        max: impl Into<[f32; 2]>,
20        intersect_with_current: bool,
21    ) {
22        let min = min.into();
23        let max = max.into();
24        assert_finite_vec2("Ui::push_clip_rect()", "min", min);
25        assert_finite_vec2("Ui::push_clip_rect()", "max", max);
26        let min_v = sys::ImVec2 {
27            x: min[0],
28            y: min[1],
29        };
30        let max_v = sys::ImVec2 {
31            x: max[0],
32            y: max[1],
33        };
34        unsafe { sys::igPushClipRect(min_v, max_v, intersect_with_current) }
35    }
36
37    /// Pop a clipping rectangle from the stack.
38    #[doc(alias = "PopClipRect")]
39    pub fn pop_clip_rect(&self) {
40        unsafe { sys::igPopClipRect() }
41    }
42
43    /// Run a closure with a clip rect pushed and automatically popped.
44    pub fn with_clip_rect<R>(
45        &self,
46        min: impl Into<[f32; 2]>,
47        max: impl Into<[f32; 2]>,
48        intersect_with_current: bool,
49        f: impl FnOnce() -> R,
50    ) -> R {
51        self.push_clip_rect(min, max, intersect_with_current);
52        let _t = ClipRectToken::new(self);
53        f()
54    }
55
56    /// Returns true if the specified rectangle (min,max) is visible (not clipped).
57    #[doc(alias = "IsRectVisible")]
58    pub fn is_rect_visible_min_max(
59        &self,
60        rect_min: impl Into<[f32; 2]>,
61        rect_max: impl Into<[f32; 2]>,
62    ) -> bool {
63        let mn = rect_min.into();
64        let mx = rect_max.into();
65        assert_finite_vec2("Ui::is_rect_visible_min_max()", "rect_min", mn);
66        assert_finite_vec2("Ui::is_rect_visible_min_max()", "rect_max", mx);
67        let mn_v = sys::ImVec2 { x: mn[0], y: mn[1] };
68        let mx_v = sys::ImVec2 { x: mx[0], y: mx[1] };
69        unsafe { sys::igIsRectVisible_Vec2(mn_v, mx_v) }
70    }
71
72    /// Returns true if a rectangle of given size at the current cursor pos is visible.
73    #[doc(alias = "IsRectVisible")]
74    pub fn is_rect_visible_with_size(&self, size: impl Into<[f32; 2]>) -> bool {
75        let s = size.into();
76        assert_finite_vec2("Ui::is_rect_visible_with_size()", "size", s);
77        let v = sys::ImVec2 { x: s[0], y: s[1] };
78        unsafe { sys::igIsRectVisible_Nil(v) }
79    }
80}