dear_imgui_rs/
columns.rs

1//! Legacy columns API
2//!
3//! Thin wrappers for the old Columns layout system. New code should prefer
4//! the `table` API (`widget::table`) which supersedes Columns with more
5//! features and better user experience.
6//!
7#![allow(
8    clippy::cast_possible_truncation,
9    clippy::cast_sign_loss,
10    clippy::as_conversions
11)]
12use crate::Ui;
13use crate::sys;
14use bitflags::bitflags;
15
16bitflags! {
17    /// Flags for old columns system
18    #[repr(transparent)]
19    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20    pub struct OldColumnFlags: i32 {
21        /// No flags
22        const NONE = sys::ImGuiOldColumnFlags_None as i32;
23        /// Disable column dividers
24        const NO_BORDER = sys::ImGuiOldColumnFlags_NoBorder as i32;
25        /// Disable resizing columns by dragging dividers
26        const NO_RESIZE = sys::ImGuiOldColumnFlags_NoResize as i32;
27        /// Disable column width preservation when the total width changes
28        const NO_PRESERVE_WIDTHS = sys::ImGuiOldColumnFlags_NoPreserveWidths as i32;
29        /// Disable forcing columns to fit within window
30        const NO_FORCE_WITHIN_WINDOW = sys::ImGuiOldColumnFlags_NoForceWithinWindow as i32;
31        /// Restore pre-1.51 behavior of extending the parent window contents size
32        const GROW_PARENT_CONTENTS_SIZE = sys::ImGuiOldColumnFlags_GrowParentContentsSize as i32;
33    }
34}
35
36impl Default for OldColumnFlags {
37    fn default() -> Self {
38        OldColumnFlags::NONE
39    }
40}
41
42/// # Columns
43impl Ui {
44    /// Creates columns layout.
45    ///
46    /// # Arguments
47    /// * `count` - Number of columns (must be >= 1)
48    /// * `id` - Optional ID for the columns (can be empty string)
49    /// * `border` - Whether to draw borders between columns
50    #[doc(alias = "Columns")]
51    pub fn columns(&self, count: i32, id: impl AsRef<str>, border: bool) {
52        unsafe { sys::igColumns(count, self.scratch_txt(id), border) }
53    }
54
55    /// Begin columns layout with advanced flags.
56    ///
57    /// # Arguments
58    /// * `id` - ID for the columns
59    /// * `count` - Number of columns (must be >= 1)
60    /// * `flags` - Column flags
61    #[doc(alias = "BeginColumns")]
62    pub fn begin_columns(&self, id: impl AsRef<str>, count: i32, flags: OldColumnFlags) {
63        unsafe { sys::igBeginColumns(self.scratch_txt(id), count, flags.bits()) }
64    }
65
66    /// End columns layout.
67    #[doc(alias = "EndColumns")]
68    pub fn end_columns(&self) {
69        unsafe { sys::igEndColumns() }
70    }
71
72    /// Switches to the next column.
73    ///
74    /// If the current row is finished, switches to first column of the next row
75    #[doc(alias = "NextColumn")]
76    pub fn next_column(&self) {
77        unsafe { sys::igNextColumn() }
78    }
79
80    /// Returns the index of the current column
81    #[doc(alias = "GetColumnIndex")]
82    pub fn current_column_index(&self) -> i32 {
83        unsafe { sys::igGetColumnIndex() }
84    }
85
86    /// Returns the width of the current column (in pixels)
87    #[doc(alias = "GetColumnWidth")]
88    pub fn current_column_width(&self) -> f32 {
89        unsafe { sys::igGetColumnWidth(-1) }
90    }
91
92    /// Returns the width of the given column (in pixels)
93    #[doc(alias = "GetColumnWidth")]
94    pub fn column_width(&self, column_index: i32) -> f32 {
95        unsafe { sys::igGetColumnWidth(column_index) }
96    }
97
98    /// Sets the width of the current column (in pixels)
99    #[doc(alias = "SetColumnWidth")]
100    pub fn set_current_column_width(&self, width: f32) {
101        unsafe { sys::igSetColumnWidth(-1, width) };
102    }
103
104    /// Sets the width of the given column (in pixels)
105    #[doc(alias = "SetColumnWidth")]
106    pub fn set_column_width(&self, column_index: i32, width: f32) {
107        unsafe { sys::igSetColumnWidth(column_index, width) };
108    }
109
110    /// Returns the offset of the current column (in pixels from the left side of the content region)
111    #[doc(alias = "GetColumnOffset")]
112    pub fn current_column_offset(&self) -> f32 {
113        unsafe { sys::igGetColumnOffset(-1) }
114    }
115
116    /// Returns the offset of the given column (in pixels from the left side of the content region)
117    #[doc(alias = "GetColumnOffset")]
118    pub fn column_offset(&self, column_index: i32) -> f32 {
119        unsafe { sys::igGetColumnOffset(column_index) }
120    }
121
122    /// Sets the offset of the current column (in pixels from the left side of the content region)
123    #[doc(alias = "SetColumnOffset")]
124    pub fn set_current_column_offset(&self, offset_x: f32) {
125        unsafe { sys::igSetColumnOffset(-1, offset_x) };
126    }
127
128    /// Sets the offset of the given column (in pixels from the left side of the content region)
129    #[doc(alias = "SetColumnOffset")]
130    pub fn set_column_offset(&self, column_index: i32, offset_x: f32) {
131        unsafe { sys::igSetColumnOffset(column_index, offset_x) };
132    }
133
134    /// Returns the current amount of columns
135    #[doc(alias = "GetColumnsCount")]
136    pub fn column_count(&self) -> i32 {
137        unsafe { sys::igGetColumnsCount() }
138    }
139
140    // ============================================================================
141    // Advanced column utilities
142    // ============================================================================
143
144    /// Push column clip rect for the given column index.
145    /// This is useful for custom drawing within columns.
146    #[doc(alias = "PushColumnClipRect")]
147    pub fn push_column_clip_rect(&self, column_index: i32) {
148        unsafe { sys::igPushColumnClipRect(column_index) }
149    }
150
151    /// Push columns background for drawing.
152    #[doc(alias = "PushColumnsBackground")]
153    pub fn push_columns_background(&self) {
154        unsafe { sys::igPushColumnsBackground() }
155    }
156
157    /// Pop columns background.
158    #[doc(alias = "PopColumnsBackground")]
159    pub fn pop_columns_background(&self) {
160        unsafe { sys::igPopColumnsBackground() }
161    }
162
163    /// Get columns ID for the given string ID and count.
164    #[doc(alias = "GetColumnsID")]
165    pub fn get_columns_id(&self, str_id: impl AsRef<str>, count: i32) -> u32 {
166        unsafe { sys::igGetColumnsID(self.scratch_txt(str_id), count) }
167    }
168
169    // ============================================================================
170    // Column state utilities
171    // ============================================================================
172
173    /// Check if any column is being resized.
174    /// Note: This is a placeholder implementation as the underlying C++ function is not available
175    pub fn is_any_column_resizing(&self) -> bool {
176        // TODO: Implement when the proper C++ binding is available
177        // The ImGui_GetCurrentWindow function is not available in our bindings
178        false
179    }
180
181    /// Get the total width of all columns.
182    pub fn get_columns_total_width(&self) -> f32 {
183        let count = self.column_count();
184        if count <= 0 {
185            return 0.0;
186        }
187
188        let mut total_width = 0.0;
189        for i in 0..count {
190            total_width += self.column_width(i);
191        }
192        total_width
193    }
194
195    /// Set all columns to equal width.
196    pub fn set_columns_equal_width(&self) {
197        let count = self.column_count();
198        if count <= 1 {
199            return;
200        }
201
202        let total_width = self.get_columns_total_width();
203        let equal_width = total_width / count as f32;
204
205        for i in 0..count {
206            self.set_column_width(i, equal_width);
207        }
208    }
209
210    /// Get column width as a percentage of total width.
211    pub fn get_column_width_percentage(&self, column_index: i32) -> f32 {
212        let total_width = self.get_columns_total_width();
213        if total_width <= 0.0 {
214            return 0.0;
215        }
216
217        let column_width = self.column_width(column_index);
218        (column_width / total_width) * 100.0
219    }
220
221    /// Set column width as a percentage of total width.
222    pub fn set_column_width_percentage(&self, column_index: i32, percentage: f32) {
223        let total_width = self.get_columns_total_width();
224        if total_width <= 0.0 {
225            return;
226        }
227
228        let new_width = (total_width * percentage) / 100.0;
229        self.set_column_width(column_index, new_width);
230    }
231}