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        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        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        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        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        unsafe {
51            sys::igSetScrollX_Float(scroll_x);
52        }
53    }
54
55    /// Sets the vertical scroll position
56    #[doc(alias = "SetScrollY")]
57    pub fn set_scroll_y(&self, scroll_y: f32) {
58        assert_finite_f32("Ui::set_scroll_y()", "scroll_y", scroll_y);
59        unsafe {
60            sys::igSetScrollY_Float(scroll_y);
61        }
62    }
63
64    /// Sets the horizontal scroll position to center on the given position
65    ///
66    /// The center_x_ratio parameter should be between 0.0 (left) and 1.0 (right)
67    #[doc(alias = "SetScrollFromPosX")]
68    pub fn set_scroll_from_pos_x(&self, local_x: f32, center_x_ratio: f32) {
69        assert_finite_f32("Ui::set_scroll_from_pos_x()", "local_x", local_x);
70        assert_unit_ratio(
71            "Ui::set_scroll_from_pos_x()",
72            "center_x_ratio",
73            center_x_ratio,
74        );
75        unsafe {
76            sys::igSetScrollFromPosX_Float(local_x, center_x_ratio);
77        }
78    }
79
80    /// Sets the vertical scroll position to center on the given position
81    ///
82    /// The center_y_ratio parameter should be between 0.0 (top) and 1.0 (bottom)
83    #[doc(alias = "SetScrollFromPosY")]
84    pub fn set_scroll_from_pos_y(&self, local_y: f32, center_y_ratio: f32) {
85        assert_finite_f32("Ui::set_scroll_from_pos_y()", "local_y", local_y);
86        assert_unit_ratio(
87            "Ui::set_scroll_from_pos_y()",
88            "center_y_ratio",
89            center_y_ratio,
90        );
91        unsafe {
92            sys::igSetScrollFromPosY_Float(local_y, center_y_ratio);
93        }
94    }
95
96    /// Scrolls to make the current item visible
97    ///
98    /// This is useful when you want to ensure a specific item is visible in a scrollable region
99    #[doc(alias = "SetScrollHereX")]
100    pub fn set_scroll_here_x(&self, center_x_ratio: f32) {
101        assert_unit_ratio("Ui::set_scroll_here_x()", "center_x_ratio", center_x_ratio);
102        unsafe {
103            sys::igSetScrollHereX(center_x_ratio);
104        }
105    }
106
107    /// Scrolls to make the current item visible vertically
108    ///
109    /// This is useful when you want to ensure a specific item is visible in a scrollable region
110    #[doc(alias = "SetScrollHereY")]
111    pub fn set_scroll_here_y(&self, center_y_ratio: f32) {
112        assert_unit_ratio("Ui::set_scroll_here_y()", "center_y_ratio", center_y_ratio);
113        unsafe {
114            sys::igSetScrollHereY(center_y_ratio);
115        }
116    }
117}