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    ) -> ClipRectToken<'_> {
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        self.run_with_bound_context(|| unsafe {
35            sys::igPushClipRect(min_v, max_v, intersect_with_current)
36        });
37        ClipRectToken::new(self)
38    }
39
40    /// Run a closure with a clip rect pushed and automatically popped.
41    pub fn with_clip_rect<R>(
42        &self,
43        min: impl Into<[f32; 2]>,
44        max: impl Into<[f32; 2]>,
45        intersect_with_current: bool,
46        f: impl FnOnce() -> R,
47    ) -> R {
48        let _t = self.push_clip_rect(min, max, intersect_with_current);
49        f()
50    }
51
52    /// Returns true if the specified rectangle (min,max) is visible (not clipped).
53    #[doc(alias = "IsRectVisible")]
54    pub fn is_rect_visible_min_max(
55        &self,
56        rect_min: impl Into<[f32; 2]>,
57        rect_max: impl Into<[f32; 2]>,
58    ) -> bool {
59        let mn = rect_min.into();
60        let mx = rect_max.into();
61        assert_finite_vec2("Ui::is_rect_visible_min_max()", "rect_min", mn);
62        assert_finite_vec2("Ui::is_rect_visible_min_max()", "rect_max", mx);
63        let mn_v = sys::ImVec2 { x: mn[0], y: mn[1] };
64        let mx_v = sys::ImVec2 { x: mx[0], y: mx[1] };
65        self.run_with_bound_context(|| unsafe { sys::igIsRectVisible_Vec2(mn_v, mx_v) })
66    }
67
68    /// Returns true if a rectangle of given size at the current cursor pos is visible.
69    #[doc(alias = "IsRectVisible")]
70    pub fn is_rect_visible_with_size(&self, size: impl Into<[f32; 2]>) -> bool {
71        let s = size.into();
72        assert_finite_vec2("Ui::is_rect_visible_with_size()", "size", s);
73        let v = sys::ImVec2 { x: s[0], y: s[1] };
74        self.run_with_bound_context(|| unsafe { sys::igIsRectVisible_Nil(v) })
75    }
76}