Skip to main content

dear_imgui_rs/widget/table/
core.rs

1use crate::sys;
2use crate::ui::Ui;
3use crate::widget::table::{
4    SortDirection, TABLE_MAX_COLUMNS, TableBgTarget, TableBuilder, TableColumnFlags,
5    TableColumnIndent, TableColumnIndex, TableColumnRef, TableColumnSetup, TableColumnStateFlags,
6    TableColumnUserData, TableColumnWidth, TableFlags, TableHoveredColumn, TableHoveredRow,
7    TableOptions, TableRowFlags, TableRowIndex, TableSortSpecs, TableToken, assert_current_table,
8    assert_current_table_cell, assert_current_table_has_flags, assert_current_table_row,
9    assert_non_negative_finite_f32, assert_table_column_width_phase, assert_table_setup_phase,
10    assert_valid_table_column, assert_valid_table_column_raw_in, current_table_if_any,
11    resolve_table_column, table_column_count_to_i32, table_freeze_count_to_i32,
12};
13use std::borrow::Cow;
14use std::ffi::CStr;
15
16/// # Table Widgets
17impl Ui {
18    /// Start a Table builder for ergonomic setup + headers + options.
19    ///
20    /// Example
21    /// ```no_run
22    /// # use dear_imgui_rs::*;
23    /// # fn demo(ui: &Ui) {
24    /// ui.table("perf")
25    ///     .flags(TableFlags::RESIZABLE | TableFlags::SORTABLE)
26    ///     .outer_size([600.0, 240.0])
27    ///     .freeze(1, 1)
28    ///     .column("Name").width(140.0).done()
29    ///     .column("Value").weight(1.0).done()
30    ///     .headers(true)
31    ///     .build(|ui| {
32    ///         ui.table_next_row();
33    ///         ui.table_next_column(); ui.text("CPU");
34    ///         ui.table_next_column(); ui.text("Intel");
35    ///     });
36    /// # }
37    /// ```
38    pub fn table<'ui>(&'ui self, str_id: impl Into<Cow<'ui, str>>) -> TableBuilder<'ui> {
39        TableBuilder::new(self, str_id)
40    }
41    /// Begins a table with no flags and with standard sizing constraints.
42    ///
43    /// This does no work on styling the headers (the top row) -- see either
44    /// [begin_table_header](Self::begin_table_header) or the more complex
45    /// [table_setup_column](Self::table_setup_column).
46    ///
47    /// # Panics
48    ///
49    /// Panics if `column_count` is zero or reaches Dear ImGui's column limit.
50    #[must_use = "if return is dropped immediately, table is ended immediately."]
51    #[doc(alias = "BeginTable")]
52    pub fn begin_table(
53        &self,
54        str_id: impl AsRef<str>,
55        column_count: usize,
56    ) -> Option<TableToken<'_>> {
57        self.begin_table_with_flags(str_id, column_count, TableFlags::NONE)
58    }
59
60    /// Begins a table with flags and with standard sizing constraints.
61    ///
62    /// # Panics
63    ///
64    /// Panics for an invalid column count or incompatible table options.
65    #[must_use = "if return is dropped immediately, table is ended immediately."]
66    pub fn begin_table_with_flags(
67        &self,
68        str_id: impl AsRef<str>,
69        column_count: usize,
70        flags: impl Into<TableOptions>,
71    ) -> Option<TableToken<'_>> {
72        self.begin_table_with_sizing(str_id, column_count, flags, [0.0, 0.0], 0.0)
73    }
74
75    /// Begins a table with all flags and sizing constraints. This is the base method,
76    /// and gives users the most flexibility.
77    ///
78    /// # Panics
79    ///
80    /// Panics for an invalid column count or table option, non-finite sizing, a negative
81    /// `inner_width` when horizontal scrolling is enabled, or an active table draw-channel scope
82    /// on the current table cell.
83    #[must_use = "if return is dropped immediately, table is ended immediately."]
84    pub fn begin_table_with_sizing(
85        &self,
86        str_id: impl AsRef<str>,
87        column_count: usize,
88        flags: impl Into<TableOptions>,
89        outer_size: impl Into<[f32; 2]>,
90        inner_width: f32,
91    ) -> Option<TableToken<'_>> {
92        let options = flags.into();
93        options.validate("Ui::begin_table_with_sizing()");
94        assert!(
95            inner_width.is_finite(),
96            "Ui::begin_table_with_sizing() inner_width must be finite"
97        );
98        assert!(
99            !options.flags.contains(TableFlags::SCROLL_X) || inner_width >= 0.0,
100            "Ui::begin_table_with_sizing() inner_width must be non-negative when SCROLL_X is enabled"
101        );
102        let outer_size = outer_size.into();
103        assert!(
104            outer_size[0].is_finite() && outer_size[1].is_finite(),
105            "Ui::begin_table_with_sizing() outer_size must contain finite values"
106        );
107        let str_id_ptr = self.scratch_txt(str_id);
108        let outer_size_vec: sys::ImVec2 = outer_size.into();
109        let column_count = table_column_count_to_i32(column_count);
110
111        let should_render = self.run_with_bound_context(|| {
112            self.assert_can_begin_table();
113            unsafe {
114                sys::igBeginTable(
115                    str_id_ptr,
116                    column_count,
117                    options.raw(),
118                    outer_size_vec,
119                    inner_width,
120                )
121            }
122        });
123
124        if should_render {
125            Some(TableToken::new(self))
126        } else {
127            None
128        }
129    }
130
131    /// Begins a table with no flags and with standard sizing constraints.
132    ///
133    /// Takes an array of table header information, the length of which determines
134    /// how many columns will be created.
135    ///
136    /// # Panics
137    ///
138    /// Panics if the array is empty, reaches Dear ImGui's column limit, or contains invalid column
139    /// setup data.
140    #[must_use = "if return is dropped immediately, table is ended immediately."]
141    pub fn begin_table_header<Name: AsRef<str>, const N: usize>(
142        &self,
143        str_id: impl AsRef<str>,
144        column_data: [TableColumnSetup<Name>; N],
145    ) -> Option<TableToken<'_>> {
146        self.begin_table_header_with_flags(str_id, column_data, TableFlags::NONE)
147    }
148
149    /// Begins a table with flags and with standard sizing constraints.
150    ///
151    /// Takes an array of table header information, the length of which determines
152    /// how many columns will be created.
153    ///
154    /// # Panics
155    ///
156    /// Panics for an invalid column count, table option, or column setup value.
157    #[must_use = "if return is dropped immediately, table is ended immediately."]
158    pub fn begin_table_header_with_flags<Name: AsRef<str>, const N: usize>(
159        &self,
160        str_id: impl AsRef<str>,
161        column_data: [TableColumnSetup<Name>; N],
162        flags: impl Into<TableOptions>,
163    ) -> Option<TableToken<'_>> {
164        if let Some(token) = self.begin_table_with_flags(str_id, N, flags) {
165            // Setup columns
166            for column in &column_data {
167                self.table_setup_column_with_indent_and_user_data(
168                    &column.name,
169                    column.flags,
170                    column.width,
171                    column.indent,
172                    column.user_data,
173                );
174            }
175            self.table_headers_row();
176            Some(token)
177        } else {
178            None
179        }
180    }
181
182    /// Setup a column for the current table.
183    ///
184    /// # Panics
185    ///
186    /// Panics outside a table, after table layout has started, after all declared columns have
187    /// already been configured, or for an invalid flag/width combination or non-finite width.
188    #[doc(alias = "TableSetupColumn")]
189    pub fn table_setup_column(
190        &self,
191        label: impl AsRef<str>,
192        flags: TableColumnFlags,
193        width: Option<TableColumnWidth>,
194    ) {
195        self.table_setup_column_with_indent(label, flags, width, None);
196    }
197
198    /// Setup a column for the current table with opaque application data.
199    ///
200    /// # Panics
201    ///
202    /// Has the same validation and phase requirements as [`Ui::table_setup_column`].
203    pub fn table_setup_column_with_user_data(
204        &self,
205        label: impl AsRef<str>,
206        flags: TableColumnFlags,
207        width: Option<TableColumnWidth>,
208        user_data: impl Into<TableColumnUserData>,
209    ) {
210        self.table_setup_column_with_indent_and_user_data(label, flags, width, None, user_data);
211    }
212
213    /// Setup a column for the current table, including explicit indent policy.
214    ///
215    /// # Panics
216    ///
217    /// Has the same validation and phase requirements as [`Ui::table_setup_column`].
218    pub fn table_setup_column_with_indent(
219        &self,
220        label: impl AsRef<str>,
221        flags: TableColumnFlags,
222        width: Option<TableColumnWidth>,
223        indent: Option<TableColumnIndent>,
224    ) {
225        self.table_setup_column_with_indent_and_user_data(label, flags, width, indent, 0);
226    }
227
228    /// Setup a column with explicit indent policy and opaque application data.
229    ///
230    /// # Panics
231    ///
232    /// Panics outside a table, after table layout has started, after all declared columns have
233    /// already been configured, or for invalid flags, indent/width combinations, or non-finite
234    /// width/weight values.
235    pub fn table_setup_column_with_indent_and_user_data(
236        &self,
237        label: impl AsRef<str>,
238        flags: TableColumnFlags,
239        width: Option<TableColumnWidth>,
240        indent: Option<TableColumnIndent>,
241        user_data: impl Into<TableColumnUserData>,
242    ) {
243        flags.validate_for_setup(
244            "Ui::table_setup_column_with_indent_and_user_data()",
245            width,
246            indent,
247        );
248        let init_width_or_weight = width.map_or(0.0, TableColumnWidth::value);
249        assert!(
250            init_width_or_weight.is_finite(),
251            "Ui::table_setup_column_with_indent_and_user_data() width or weight must be finite"
252        );
253        let raw_flags = flags.bits()
254            | width.map_or(0, TableColumnWidth::raw_flags)
255            | indent.map_or(0, TableColumnIndent::raw_flags);
256        let user_data = user_data.into().get();
257        let label_ptr = self.scratch_txt(label);
258        self.run_with_bound_context(|| {
259            let table = assert_current_table("Ui::table_setup_column_with_indent_and_user_data()");
260            assert!(
261                unsafe { i32::from((*table).DeclColumnsCount) < (*table).ColumnsCount },
262                "Ui::table_setup_column_with_indent_and_user_data() called more times than the table column count"
263            );
264            assert_table_setup_phase("Ui::table_setup_column_with_indent_and_user_data()");
265            unsafe {
266                sys::igTableSetupColumn(label_ptr, raw_flags, init_width_or_weight, user_data);
267            }
268        });
269    }
270
271    /// Setup a column with a fixed initial width.
272    ///
273    /// # Panics
274    ///
275    /// Has the same validation and phase requirements as [`Ui::table_setup_column`].
276    pub fn table_setup_column_fixed_width(
277        &self,
278        label: impl AsRef<str>,
279        flags: TableColumnFlags,
280        width: f32,
281    ) {
282        self.table_setup_column(label, flags, Some(TableColumnWidth::Fixed(width)));
283    }
284
285    /// Setup a column with a stretch weight.
286    ///
287    /// # Panics
288    ///
289    /// Has the same validation and phase requirements as [`Ui::table_setup_column`].
290    pub fn table_setup_column_stretch_weight(
291        &self,
292        label: impl AsRef<str>,
293        flags: TableColumnFlags,
294        weight: f32,
295    ) {
296        self.table_setup_column(label, flags, Some(TableColumnWidth::Stretch(weight)));
297    }
298
299    /// Submit all header cells based on data provided to `TableSetupColumn()` and submit the
300    /// context-menu target.
301    ///
302    /// # Panics
303    ///
304    /// Panics outside a table, while a table draw-channel scope is active, or while this cell
305    /// hosts a native extension.
306    #[doc(alias = "TableHeadersRow")]
307    pub fn table_headers_row(&self) {
308        self.run_with_bound_context(|| {
309            assert_current_table("Ui::table_headers_row()");
310            self.assert_table_cell_can_change("Ui::table_headers_row()");
311            unsafe {
312                sys::igTableHeadersRow();
313            }
314        });
315    }
316
317    /// Append into the next column, or the first column of the next row when currently in the last
318    /// column.
319    ///
320    /// Returns `false` when no table is current.
321    ///
322    /// # Panics
323    ///
324    /// Panics while a table draw-channel scope is active or this cell hosts a native extension.
325    #[doc(alias = "TableNextColumn")]
326    pub fn table_next_column(&self) -> bool {
327        self.run_with_bound_context(|| {
328            self.assert_table_cell_can_change("Ui::table_next_column()");
329            unsafe { sys::igTableNextColumn() }
330        })
331    }
332
333    /// Append into the specified column.
334    ///
335    /// Returns `false` when no table is current.
336    ///
337    /// # Panics
338    ///
339    /// Panics if `column` is outside the current table, a table draw-channel scope is active,
340    /// or this cell hosts a native extension.
341    #[doc(alias = "TableSetColumnIndex")]
342    pub fn table_set_column_index(&self, column: impl Into<TableColumnIndex>) -> bool {
343        let column = column.into();
344        let column_n = column.into_i32("Ui::table_set_column_index()");
345        self.run_with_bound_context(|| {
346            self.assert_table_cell_can_change("Ui::table_set_column_index()");
347            if let Some(table) = current_table_if_any() {
348                assert_valid_table_column_raw_in(table, column_n, "Ui::table_set_column_index()");
349            }
350            unsafe { sys::igTableSetColumnIndex(column_n) }
351        })
352    }
353
354    /// Append into the next row.
355    ///
356    /// # Panics
357    ///
358    /// Panics outside a table, while a table draw-channel scope is active, or while this cell
359    /// hosts a native extension.
360    #[doc(alias = "TableNextRow")]
361    pub fn table_next_row(&self) {
362        self.table_next_row_with_flags(TableRowFlags::NONE, 0.0);
363    }
364
365    /// Append into the next row with flags and minimum height.
366    ///
367    /// # Panics
368    ///
369    /// Panics outside a table, while a table draw-channel scope is active, while this cell
370    /// hosts a native extension, or when `min_row_height` is negative or non-finite.
371    pub fn table_next_row_with_flags(&self, flags: TableRowFlags, min_row_height: f32) {
372        assert_non_negative_finite_f32(
373            "Ui::table_next_row_with_flags()",
374            "min_row_height",
375            min_row_height,
376        );
377        self.run_with_bound_context(|| {
378            assert_current_table("Ui::table_next_row_with_flags()");
379            self.assert_table_cell_can_change("Ui::table_next_row_with_flags()");
380            unsafe { sys::igTableNextRow(flags.bits(), min_row_height) };
381        });
382    }
383
384    /// Freeze columns/rows so they stay visible when scrolling.
385    ///
386    /// # Panics
387    ///
388    /// Panics outside a table, after the table setup phase, or when either freeze count exceeds
389    /// Dear ImGui's supported range.
390    #[doc(alias = "TableSetupScrollFreeze")]
391    pub fn table_setup_scroll_freeze(&self, frozen_cols: usize, frozen_rows: usize) {
392        let frozen_cols = table_freeze_count_to_i32(
393            "Ui::table_setup_scroll_freeze()",
394            "frozen_cols",
395            frozen_cols,
396            TABLE_MAX_COLUMNS,
397        );
398        let frozen_rows = table_freeze_count_to_i32(
399            "Ui::table_setup_scroll_freeze()",
400            "frozen_rows",
401            frozen_rows,
402            128,
403        );
404        self.run_with_bound_context(|| {
405            assert_table_setup_phase("Ui::table_setup_scroll_freeze()");
406            unsafe { sys::igTableSetupScrollFreeze(frozen_cols, frozen_rows) }
407        });
408    }
409
410    /// Submit one header cell at the current column position.
411    ///
412    /// # Panics
413    ///
414    /// Panics unless a table cell is current.
415    #[doc(alias = "TableHeader")]
416    pub fn table_header(&self, label: impl AsRef<str>) {
417        let label_ptr = self.scratch_txt(label);
418        self.run_with_bound_context(|| {
419            assert_current_table_cell("Ui::table_header()");
420            unsafe { sys::igTableHeader(label_ptr) }
421        });
422    }
423
424    /// Return the current table's column count, or zero when no table is current.
425    #[doc(alias = "TableGetColumnCount")]
426    pub fn table_get_column_count(&self) -> usize {
427        usize::try_from(self.run_with_bound_context(|| unsafe { sys::igTableGetColumnCount() }))
428            .expect("Dear ImGui returned a negative table column count")
429    }
430
431    /// Return current column index, or `None` when no table cell is current.
432    #[doc(alias = "TableGetColumnIndex")]
433    pub fn table_get_column_index(&self) -> Option<TableColumnIndex> {
434        self.run_with_bound_context(|| {
435            current_table_if_any()?;
436            let raw = unsafe { sys::igTableGetColumnIndex() };
437            (raw >= 0).then(|| TableColumnIndex::from_i32(raw, "Ui::table_get_column_index()"))
438        })
439    }
440
441    /// Return current row index, or `None` when no table row is current.
442    #[doc(alias = "TableGetRowIndex")]
443    pub fn table_get_row_index(&self) -> Option<TableRowIndex> {
444        self.run_with_bound_context(|| {
445            current_table_if_any()?;
446            let raw = unsafe { sys::igTableGetRowIndex() };
447            (raw >= 0).then(|| TableRowIndex::from_i32(raw, "Ui::table_get_row_index()"))
448        })
449    }
450
451    /// Return an owned copy of a column's name. It remains valid after further column setup
452    /// and after the table or frame ends.
453    ///
454    /// Returns an empty string when no table is current.
455    ///
456    /// # Panics
457    ///
458    /// Panics when a table is current and the requested/current column is invalid.
459    #[doc(alias = "TableGetColumnName")]
460    pub fn table_get_column_name(&self, column: impl Into<TableColumnRef>) -> String {
461        let column = column.into();
462        self.run_with_bound_context(|| {
463            let Some(table) = current_table_if_any() else {
464                return String::new();
465            };
466            let column_n = resolve_table_column(table, column, "Ui::table_get_column_name()");
467            unsafe {
468                let ptr = sys::igTableGetColumnName_Int(column_n);
469                if ptr.is_null() {
470                    String::new()
471                } else {
472                    CStr::from_ptr(ptr).to_str().unwrap_or("").to_owned()
473                }
474            }
475        })
476    }
477
478    /// Return the flags of a column by index.
479    ///
480    /// Returns empty flags when no table is current.
481    ///
482    /// # Panics
483    ///
484    /// Panics when a table is current and the requested/current column is invalid.
485    #[doc(alias = "TableGetColumnFlags")]
486    pub fn table_get_column_flags(
487        &self,
488        column: impl Into<TableColumnRef>,
489    ) -> TableColumnStateFlags {
490        let column = column.into();
491        self.run_with_bound_context(|| {
492            let Some(table) = current_table_if_any() else {
493                return TableColumnStateFlags::empty();
494            };
495            let column_n = resolve_table_column(table, column, "Ui::table_get_column_flags()");
496            unsafe { TableColumnStateFlags::from_bits_retain(sys::igTableGetColumnFlags(column_n)) }
497        })
498    }
499
500    /// Enable or disable a column by index.
501    ///
502    /// # Panics
503    ///
504    /// Panics outside a table, when the table lacks [`TableFlags::HIDEABLE`], or when the
505    /// requested/current column is invalid.
506    #[doc(alias = "TableSetColumnEnabled")]
507    pub fn table_set_column_enabled(&self, column: impl Into<TableColumnRef>, enabled: bool) {
508        let column = column.into();
509        self.run_with_bound_context(|| {
510            let table = assert_current_table_has_flags(
511                TableFlags::HIDEABLE,
512                "Ui::table_set_column_enabled()",
513            );
514            let column_n = resolve_table_column(table, column, "Ui::table_set_column_enabled()");
515            unsafe { sys::igTableSetColumnEnabled(column_n, enabled) }
516        });
517    }
518
519    /// Return the hovered column, unused table space, or [`TableHoveredColumn::None`] when no
520    /// table column is hovered.
521    #[doc(alias = "TableGetHoveredColumn")]
522    pub fn table_get_hovered_column(&self) -> TableHoveredColumn {
523        self.run_with_bound_context(|| {
524            let raw = unsafe { sys::igTableGetHoveredColumn() };
525            if raw < 0 {
526                return TableHoveredColumn::None;
527            }
528            if let Some(table) = current_table_if_any() {
529                let column_count = unsafe { (*table).ColumnsCount };
530                if raw == column_count {
531                    return TableHoveredColumn::UnusedSpace;
532                }
533            }
534            TableHoveredColumn::Column(TableColumnIndex::from_i32(
535                raw,
536                "Ui::table_get_hovered_column()",
537            ))
538        })
539    }
540
541    /// Set column width for a fixed-width column.
542    ///
543    /// # Panics
544    ///
545    /// Panics outside a table, after table layout is locked, before layout metrics are available,
546    /// for an invalid column, or when `width` is negative or non-finite.
547    #[doc(alias = "TableSetColumnWidth")]
548    pub fn table_set_column_width(&self, column: impl Into<TableColumnIndex>, width: f32) {
549        assert_non_negative_finite_f32("Ui::table_set_column_width()", "width", width);
550        let column = column.into();
551        self.run_with_bound_context(|| {
552            assert_table_column_width_phase("Ui::table_set_column_width()");
553            let column_n = assert_valid_table_column(column, "Ui::table_set_column_width()");
554            unsafe { sys::igTableSetColumnWidth(column_n, width) }
555        });
556    }
557
558    /// Set a table background color target.
559    ///
560    /// Color must be an ImGui-packed ImU32 in ABGR order (IM_COL32).
561    /// Use `crate::colors::Color::to_imgui_u32()` to convert RGBA floats.
562    ///
563    /// # Panics
564    ///
565    /// Panics unless a table row is current or when the requested/current column is invalid.
566    #[doc(alias = "TableSetBgColor")]
567    pub fn table_set_cell_bg_color_u32(&self, color: u32, column: impl Into<TableColumnRef>) {
568        let column = column.into();
569        self.run_with_bound_context(|| {
570            let table = assert_current_table_row("Ui::table_set_cell_bg_color_u32()");
571            let column_n = resolve_table_column(table, column, "Ui::table_set_cell_bg_color_u32()");
572            unsafe { sys::igTableSetBgColor(TableBgTarget::CellBg as i32, color, column_n) }
573        });
574    }
575
576    /// Set a table cell background color using RGBA color (0..=1 floats).
577    ///
578    /// # Panics
579    ///
580    /// Has the same phase and column requirements as [`Ui::table_set_cell_bg_color_u32`].
581    pub fn table_set_cell_bg_color(&self, rgba: [f32; 4], column: impl Into<TableColumnRef>) {
582        let col = crate::colors::Color::from_array(rgba).to_imgui_u32();
583        self.table_set_cell_bg_color_u32(col, column);
584    }
585
586    /// Set the first row background color for the current table row.
587    ///
588    /// # Panics
589    ///
590    /// Panics unless a table row is current.
591    #[doc(alias = "TableSetBgColor")]
592    pub fn table_set_row_bg0_color_u32(&self, color: u32) {
593        self.run_with_bound_context(|| {
594            assert_current_table_row("Ui::table_set_row_bg0_color_u32()");
595            unsafe { sys::igTableSetBgColor(TableBgTarget::RowBg0 as i32, color, -1) }
596        });
597    }
598
599    /// Set the first row background color using RGBA color (0..=1 floats).
600    ///
601    /// # Panics
602    ///
603    /// Panics unless a table row is current.
604    pub fn table_set_row_bg0_color(&self, rgba: [f32; 4]) {
605        let col = crate::colors::Color::from_array(rgba).to_imgui_u32();
606        self.table_set_row_bg0_color_u32(col);
607    }
608
609    /// Set the second row background color for the current table row.
610    ///
611    /// # Panics
612    ///
613    /// Panics unless a table row is current.
614    #[doc(alias = "TableSetBgColor")]
615    pub fn table_set_row_bg1_color_u32(&self, color: u32) {
616        self.run_with_bound_context(|| {
617            assert_current_table_row("Ui::table_set_row_bg1_color_u32()");
618            unsafe { sys::igTableSetBgColor(TableBgTarget::RowBg1 as i32, color, -1) }
619        });
620    }
621
622    /// Set the second row background color using RGBA color (0..=1 floats).
623    ///
624    /// # Panics
625    ///
626    /// Panics unless a table row is current.
627    pub fn table_set_row_bg1_color(&self, rgba: [f32; 4]) {
628        let col = crate::colors::Color::from_array(rgba).to_imgui_u32();
629        self.table_set_row_bg1_color_u32(col);
630    }
631
632    /// Return hovered row from the previous frame.
633    #[doc(alias = "TableGetHoveredRow")]
634    pub fn table_get_hovered_row(&self) -> TableHoveredRow {
635        self.run_with_bound_context(|| {
636            if current_table_if_any().is_none() {
637                return TableHoveredRow::None;
638            }
639            let raw = unsafe { sys::igTableGetHoveredRow() };
640            if raw < 0 {
641                return TableHoveredRow::None;
642            }
643            TableHoveredRow::Row(TableRowIndex::from_i32(raw, "Ui::table_get_hovered_row()"))
644        })
645    }
646
647    /// Header row height in pixels.
648    ///
649    /// # Panics
650    ///
651    /// Panics outside a table.
652    #[doc(alias = "TableGetHeaderRowHeight")]
653    pub fn table_get_header_row_height(&self) -> f32 {
654        self.run_with_bound_context(|| {
655            assert_current_table("Ui::table_get_header_row_height()");
656            unsafe { sys::igTableGetHeaderRowHeight() }
657        })
658    }
659
660    /// Set sort direction for a column. Optionally append to existing sort specs (multi-sort).
661    ///
662    /// # Panics
663    ///
664    /// Panics outside a table, when the table lacks [`TableFlags::SORTABLE`], for an invalid
665    /// column, or when [`SortDirection::None`] is used without [`TableFlags::SORT_TRISTATE`].
666    #[doc(alias = "TableSetColumnSortDirection")]
667    pub fn table_set_column_sort_direction(
668        &self,
669        column: impl Into<TableColumnIndex>,
670        dir: SortDirection,
671        append_to_sort_specs: bool,
672    ) {
673        let column = column.into();
674        self.run_with_bound_context(|| {
675            let table = assert_current_table("Ui::table_set_column_sort_direction()");
676            let table_flags = TableFlags::from_bits_retain(unsafe { (*table).Flags });
677            assert!(
678                table_flags.contains(TableFlags::SORTABLE),
679                "Ui::table_set_column_sort_direction() requires the current table to have SORTABLE"
680            );
681            if dir == SortDirection::None {
682                assert!(
683                    table_flags.contains(TableFlags::SORT_TRISTATE),
684                    "Ui::table_set_column_sort_direction() requires SORT_TRISTATE for SortDirection::None"
685                );
686            }
687            let column_n =
688                assert_valid_table_column(column, "Ui::table_set_column_sort_direction()");
689            unsafe {
690                sys::igTableSetColumnSortDirection(column_n, dir.into(), append_to_sort_specs)
691            }
692        });
693    }
694
695    /// Get current table sort specifications, if any.
696    /// When non-None and `is_dirty()` is true, the application should sort its data and
697    /// then call [`TableSortSpecs::clear_dirty`] while this table is still current. Returns `None`
698    /// outside a table or when the table has no sort specifications. On a sortable table, this may
699    /// lock table layout, so finish all setup calls first.
700    #[doc(alias = "TableGetSortSpecs")]
701    pub fn table_get_sort_specs(&self) -> Option<TableSortSpecs> {
702        self.run_with_bound_context(|| unsafe {
703            let table = current_table_if_any()?;
704            let ptr = sys::igTableGetSortSpecs();
705            if ptr.is_null() {
706                None
707            } else {
708                Some(TableSortSpecs::from_raw(self, table, ptr))
709            }
710        })
711    }
712}