Skip to main content

dear_imgui_rs/widget/table/
setup.rs

1use super::{TableColumnFlags, TableColumnIndent, TableColumnWidth};
2
3/// Opaque application data associated with a table column.
4///
5/// Dear ImGui copies this value unchanged into table sort specifications. Zero is a valid value;
6/// it is not treated as an absent ID or hashed through Dear ImGui's widget ID stack.
7#[repr(transparent)]
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
9pub struct TableColumnUserData(u32);
10
11impl TableColumnUserData {
12    /// Creates opaque table column user data.
13    #[inline]
14    pub const fn new(value: u32) -> Self {
15        Self(value)
16    }
17
18    /// Returns the unchanged opaque value.
19    #[inline]
20    pub const fn get(self) -> u32 {
21        self.0
22    }
23}
24
25impl From<u32> for TableColumnUserData {
26    #[inline]
27    fn from(value: u32) -> Self {
28        Self::new(value)
29    }
30}
31
32impl From<TableColumnUserData> for u32 {
33    #[inline]
34    fn from(value: TableColumnUserData) -> Self {
35        value.get()
36    }
37}
38
39/// Table column setup information
40#[derive(Clone, Debug)]
41pub struct TableColumnSetup<Name> {
42    pub name: Name,
43    pub flags: TableColumnFlags,
44    pub width: Option<TableColumnWidth>,
45    pub indent: Option<TableColumnIndent>,
46    pub user_data: TableColumnUserData,
47}
48
49impl<Name> TableColumnSetup<Name> {
50    /// Creates a new table column setup
51    pub fn new(name: Name) -> Self {
52        Self {
53            name,
54            flags: TableColumnFlags::NONE,
55            width: None,
56            indent: None,
57            user_data: TableColumnUserData::default(),
58        }
59    }
60
61    /// Sets the column flags
62    pub fn flags(mut self, flags: TableColumnFlags) -> Self {
63        self.flags = flags;
64        self
65    }
66
67    /// Sets a fixed initial column width in pixels.
68    pub fn fixed_width(mut self, width: f32) -> Self {
69        self.width = Some(TableColumnWidth::Fixed(width));
70        self
71    }
72
73    /// Sets an initial stretch weight for this column.
74    pub fn stretch_weight(mut self, weight: f32) -> Self {
75        self.width = Some(TableColumnWidth::Stretch(weight));
76        self
77    }
78
79    /// Sets this column's indentation policy.
80    pub fn indent(mut self, indent: TableColumnIndent) -> Self {
81        self.indent = Some(indent);
82        self
83    }
84
85    /// Enables or disables indentation for this column.
86    pub fn indent_enabled(mut self, enabled: bool) -> Self {
87        self.indent = Some(if enabled {
88            TableColumnIndent::Enable
89        } else {
90            TableColumnIndent::Disable
91        });
92        self
93    }
94
95    /// Sets the opaque user data associated with this column.
96    ///
97    /// Dear ImGui returns the value unchanged in table sort specifications. The default is zero,
98    /// which remains ordinary application data rather than an absence sentinel.
99    ///
100    /// The old `user_id` API intentionally has no compatibility alias because widget IDs and
101    /// opaque column data have different semantics.
102    ///
103    /// ```compile_fail
104    /// # use dear_imgui_rs::TableColumnSetup;
105    /// let _ = TableColumnSetup::new("column").user_id(1_u32);
106    /// ```
107    pub fn user_data(mut self, user_data: impl Into<TableColumnUserData>) -> Self {
108        self.user_data = user_data.into();
109        self
110    }
111
112    pub(crate) fn map_name<M>(self, map: impl FnOnce(Name) -> M) -> TableColumnSetup<M> {
113        TableColumnSetup {
114            name: map(self.name),
115            flags: self.flags,
116            width: self.width,
117            indent: self.indent,
118            user_data: self.user_data,
119        }
120    }
121}