Skip to main content

dear_imgui_rs/window/
scroll.rs

1//! Window scrolling
2//!
3//! Read and control the current window scroll offsets as well as their maxima
4//! to implement custom scrolling behaviors.
5//!
6use crate::Ui;
7use crate::sys;
8
9fn assert_finite_f32(caller: &str, name: &str, value: f32) {
10    assert!(value.is_finite(), "{caller} {name} must be finite");
11}
12
13fn assert_unit_ratio(caller: &str, name: &str, value: f32) {
14    assert_finite_f32(caller, name, value);
15    assert!(
16        (0.0..=1.0).contains(&value),
17        "{caller} {name} must be between 0.0 and 1.0"
18    );
19}
20
21impl Ui {
22    /// Returns the current scroll position of the window
23    #[doc(alias = "GetScrollX")]
24    pub fn scroll_x(&self) -> f32 {
25        self.run_with_bound_context(|| unsafe { sys::igGetScrollX() })
26    }
27
28    /// Returns the current vertical scroll position of the window
29    #[doc(alias = "GetScrollY")]
30    pub fn scroll_y(&self) -> f32 {
31        self.run_with_bound_context(|| unsafe { sys::igGetScrollY() })
32    }
33
34    /// Returns the maximum horizontal scroll position
35    #[doc(alias = "GetScrollMaxX")]
36    pub fn scroll_max_x(&self) -> f32 {
37        self.run_with_bound_context(|| unsafe { sys::igGetScrollMaxX() })
38    }
39
40    /// Returns the maximum vertical scroll position
41    #[doc(alias = "GetScrollMaxY")]
42    pub fn scroll_max_y(&self) -> f32 {
43        self.run_with_bound_context(|| unsafe { sys::igGetScrollMaxY() })
44    }
45
46    /// Sets the horizontal scroll position
47    #[doc(alias = "SetScrollX")]
48    pub fn set_scroll_x(&self, scroll_x: f32) {
49        assert_finite_f32("Ui::set_scroll_x()", "scroll_x", scroll_x);
50        self.run_with_bound_context(|| unsafe { sys::igSetScrollX_Float(scroll_x) });
51    }
52
53    /// Sets the vertical scroll position
54    #[doc(alias = "SetScrollY")]
55    pub fn set_scroll_y(&self, scroll_y: f32) {
56        assert_finite_f32("Ui::set_scroll_y()", "scroll_y", scroll_y);
57        self.run_with_bound_context(|| unsafe { sys::igSetScrollY_Float(scroll_y) });
58    }
59
60    /// Sets the horizontal scroll position to center on the given position
61    ///
62    /// The center_x_ratio parameter should be between 0.0 (left) and 1.0 (right)
63    #[doc(alias = "SetScrollFromPosX")]
64    pub fn set_scroll_from_pos_x(&self, local_x: f32, center_x_ratio: f32) {
65        assert_finite_f32("Ui::set_scroll_from_pos_x()", "local_x", local_x);
66        assert_unit_ratio(
67            "Ui::set_scroll_from_pos_x()",
68            "center_x_ratio",
69            center_x_ratio,
70        );
71        self.run_with_bound_context(|| unsafe {
72            sys::igSetScrollFromPosX_Float(local_x, center_x_ratio);
73        });
74    }
75
76    /// Sets the vertical scroll position to center on the given position
77    ///
78    /// The center_y_ratio parameter should be between 0.0 (top) and 1.0 (bottom)
79    #[doc(alias = "SetScrollFromPosY")]
80    pub fn set_scroll_from_pos_y(&self, local_y: f32, center_y_ratio: f32) {
81        assert_finite_f32("Ui::set_scroll_from_pos_y()", "local_y", local_y);
82        assert_unit_ratio(
83            "Ui::set_scroll_from_pos_y()",
84            "center_y_ratio",
85            center_y_ratio,
86        );
87        self.run_with_bound_context(|| unsafe {
88            sys::igSetScrollFromPosY_Float(local_y, center_y_ratio);
89        });
90    }
91
92    /// Scrolls to make the current item visible
93    ///
94    /// This is useful when you want to ensure a specific item is visible in a scrollable region
95    #[doc(alias = "SetScrollHereX")]
96    pub fn set_scroll_here_x(&self, center_x_ratio: f32) {
97        assert_unit_ratio("Ui::set_scroll_here_x()", "center_x_ratio", center_x_ratio);
98        self.run_with_bound_context(|| unsafe { sys::igSetScrollHereX(center_x_ratio) });
99    }
100
101    /// Scrolls to make the current item visible vertically
102    ///
103    /// This is useful when you want to ensure a specific item is visible in a scrollable region
104    #[doc(alias = "SetScrollHereY")]
105    pub fn set_scroll_here_y(&self, center_y_ratio: f32) {
106        assert_unit_ratio("Ui::set_scroll_here_y()", "center_y_ratio", center_y_ratio);
107        self.run_with_bound_context(|| unsafe { sys::igSetScrollHereY(center_y_ratio) });
108    }
109}