dear_imgui_rs/widget/
mod.rs

1//! Standard widgets
2//!
3//! Collection of common Dear ImGui widgets exposed with an idiomatic Rust
4//! API. Most widgets follow a small builder pattern for configuration, and
5//! also provide convenience methods on [`Ui`].
6//!
7//! Examples:
8//! ```no_run
9//! # use dear_imgui_rs::*;
10//! # let mut ctx = Context::create();
11//! # let ui = ctx.frame();
12//! // Buttons
13//! if ui.button("Click me") { /* ... */ }
14//!
15//! // Sliders
16//! let mut value = 0.5f32;
17//! ui.slider_f32("Value", &mut value, 0.0, 1.0);
18//!
19//! // Inputs
20//! let mut text = String::new();
21//! ui.input_text("Name", &mut text).build();
22//! ```
23//!
24//! Submodules group related widgets: `button`, `color`, `combo`, `drag`,
25//! `image`, `input`, `list_box`, `menu`, `misc`, `plot`, `popup`, `progress`,
26//! `selectable`, `slider`, `tab`, `table`, `text`, `tooltip`, `tree`.
27//!
28use crate::sys;
29#[cfg(feature = "serde")]
30use serde::{Deserialize, Serialize};
31
32pub mod button;
33pub mod color;
34pub mod combo;
35pub mod drag;
36pub mod image;
37pub mod input;
38pub mod list_box;
39pub mod menu;
40pub mod misc;
41pub mod multi_select;
42pub mod plot;
43pub mod popup;
44pub mod progress;
45pub mod selectable;
46pub mod slider;
47pub mod tab;
48pub mod table;
49pub mod text;
50pub mod tooltip;
51pub mod tree;
52
53// Re-export important types
54pub use popup::PopupFlags;
55pub use table::{TableBgTarget, TableBuilder, TableColumnSetup};
56
57// Widget implementations
58pub use self::button::*;
59pub use self::color::*;
60pub use self::combo::*;
61pub use self::drag::*;
62pub use self::image::*;
63pub use self::input::*;
64pub use self::list_box::*;
65pub use self::menu::*;
66pub use self::misc::*;
67pub use self::multi_select::*;
68pub use self::plot::*;
69pub use self::popup::*;
70pub use self::progress::*;
71pub use self::selectable::*;
72pub use self::slider::*;
73pub use self::tab::*;
74pub use self::table::*;
75pub use self::tooltip::*;
76pub use self::tree::*;
77
78// ButtonFlags is defined in misc.rs and re-exported
79
80bitflags::bitflags! {
81    /// Flags for tree node widgets
82    #[repr(transparent)]
83    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
84    pub struct TreeNodeFlags: i32 {
85        /// No flags
86        const NONE = 0;
87        /// Draw as selected
88        const SELECTED = sys::ImGuiTreeNodeFlags_Selected as i32;
89        /// Draw frame with background (e.g. for CollapsingHeader)
90        const FRAMED = sys::ImGuiTreeNodeFlags_Framed as i32;
91        /// Hit testing to allow subsequent widgets to overlap this one
92        const ALLOW_ITEM_OVERLAP = sys::ImGuiTreeNodeFlags_AllowOverlap as i32;
93        /// Don't do a TreePush() when open (e.g. for CollapsingHeader) = no extra indent nor pushing on ID stack
94        const NO_TREE_PUSH_ON_OPEN = sys::ImGuiTreeNodeFlags_NoTreePushOnOpen as i32;
95        /// Don't automatically and temporarily open node when Logging is active (by default logging will automatically open tree nodes)
96        const NO_AUTO_OPEN_ON_LOG = sys::ImGuiTreeNodeFlags_NoAutoOpenOnLog as i32;
97        /// Default node to be open
98        const DEFAULT_OPEN = sys::ImGuiTreeNodeFlags_DefaultOpen as i32;
99        /// Need double-click to open node
100        const OPEN_ON_DOUBLE_CLICK = sys::ImGuiTreeNodeFlags_OpenOnDoubleClick as i32;
101        /// Only open when clicking on the arrow part. If ImGuiTreeNodeFlags_OpenOnDoubleClick is also set, single-click arrow or double-click all box to open.
102        const OPEN_ON_ARROW = sys::ImGuiTreeNodeFlags_OpenOnArrow as i32;
103        /// No collapsing, no arrow (use as a convenience for leaf nodes)
104        const LEAF = sys::ImGuiTreeNodeFlags_Leaf as i32;
105        /// Display a bullet instead of arrow
106        const BULLET = sys::ImGuiTreeNodeFlags_Bullet as i32;
107        /// Use FramePadding (even for an unframed text node) to vertically align text baseline to regular widget height. Equivalent to calling AlignTextToFramePadding().
108        const FRAME_PADDING = sys::ImGuiTreeNodeFlags_FramePadding as i32;
109        /// Extend hit box to the right-most edge, even if not framed. This is not the default in order to allow adding other items on the same line. In the future we may refactor the hit system to be front-to-back, allowing natural overlaps and then this can become the default.
110        const SPAN_AVAIL_WIDTH = sys::ImGuiTreeNodeFlags_SpanAvailWidth as i32;
111        /// Extend hit box to the left-most and right-most edges (bypass the indented area).
112        const SPAN_FULL_WIDTH = sys::ImGuiTreeNodeFlags_SpanFullWidth as i32;
113        /// (WIP) Nav: left direction goes to parent. Only for the tree node, not the tree push.
114        const NAV_LEFT_JUMPS_BACK_HERE = sys::ImGuiTreeNodeFlags_NavLeftJumpsToParent as i32;
115        /// Combination of Leaf and NoTreePushOnOpen
116        const COLLAPSING_HEADER = Self::FRAMED.bits() | Self::NO_TREE_PUSH_ON_OPEN.bits();
117    }
118}
119
120bitflags::bitflags! {
121    /// Flags for combo box widgets
122    #[repr(transparent)]
123    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
124    pub struct ComboBoxFlags: i32 {
125        /// No flags
126        const NONE = 0;
127        /// Align the popup toward the left by default
128        const POPUP_ALIGN_LEFT = sys::ImGuiComboFlags_PopupAlignLeft as i32;
129        /// Max ~4 items visible. Tip: If you want your combo popup to be a specific size you can use SetNextWindowSizeConstraints() prior to calling BeginCombo()
130        const HEIGHT_SMALL = sys::ImGuiComboFlags_HeightSmall as i32;
131        /// Max ~8 items visible (default)
132        const HEIGHT_REGULAR = sys::ImGuiComboFlags_HeightRegular as i32;
133        /// Max ~20 items visible
134        const HEIGHT_LARGE = sys::ImGuiComboFlags_HeightLarge as i32;
135        /// As many fitting items as possible
136        const HEIGHT_LARGEST = sys::ImGuiComboFlags_HeightLargest as i32;
137        /// Display on the preview box without the square arrow button
138        const NO_ARROW_BUTTON = sys::ImGuiComboFlags_NoArrowButton as i32;
139        /// Display only a square arrow button
140        const NO_PREVIEW = sys::ImGuiComboFlags_NoPreview as i32;
141        /// Width dynamically calculated from preview contents
142        const WIDTH_FIT_PREVIEW = sys::ImGuiComboFlags_WidthFitPreview as i32;
143    }
144}
145
146bitflags::bitflags! {
147    /// Flags for table widgets
148    #[repr(transparent)]
149    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
150    pub struct TableFlags: i32 {
151        /// No flags
152        const NONE = 0;
153        /// Enable resizing columns
154        const RESIZABLE = sys::ImGuiTableFlags_Resizable as i32;
155        /// Enable reordering columns in header row (need calling TableSetupColumn() + TableHeadersRow() to display headers)
156        const REORDERABLE = sys::ImGuiTableFlags_Reorderable as i32;
157        /// Enable hiding/disabling columns in context menu
158        const HIDEABLE = sys::ImGuiTableFlags_Hideable as i32;
159        /// Enable sorting. Call TableGetSortSpecs() to obtain sort specs. Also see ImGuiTableFlags_SortMulti and ImGuiTableFlags_SortTristate.
160        const SORTABLE = sys::ImGuiTableFlags_Sortable as i32;
161        /// Disable persisting columns order, width and sort settings in the .ini file
162        const NO_SAVED_SETTINGS = sys::ImGuiTableFlags_NoSavedSettings as i32;
163        /// Right-click on columns body/contents will display table context menu. By default it is available in TableHeadersRow().
164        const CONTEXT_MENU_IN_BODY = sys::ImGuiTableFlags_ContextMenuInBody as i32;
165        /// Set each RowBg color with ImGuiCol_TableRowBg or ImGuiCol_TableRowBgAlt (equivalent of calling TableSetBgColor with ImGuiTableBgFlags_RowBg0 on each row manually)
166        const ROW_BG = sys::ImGuiTableFlags_RowBg as i32;
167        /// Draw horizontal borders between rows
168        const BORDERS_INNER_H = sys::ImGuiTableFlags_BordersInnerH as i32;
169        /// Draw horizontal borders at the top and bottom
170        const BORDERS_OUTER_H = sys::ImGuiTableFlags_BordersOuterH as i32;
171        /// Draw vertical borders between columns
172        const BORDERS_INNER_V = sys::ImGuiTableFlags_BordersInnerV as i32;
173        /// Draw vertical borders on the left and right sides
174        const BORDERS_OUTER_V = sys::ImGuiTableFlags_BordersOuterV as i32;
175        /// Draw horizontal borders
176        const BORDERS_H = Self::BORDERS_INNER_H.bits() | Self::BORDERS_OUTER_H.bits();
177        /// Draw vertical borders
178        const BORDERS_V = Self::BORDERS_INNER_V.bits() | Self::BORDERS_OUTER_V.bits();
179        /// Draw inner borders
180        const BORDERS_INNER = Self::BORDERS_INNER_V.bits() | Self::BORDERS_INNER_H.bits();
181        /// Draw outer borders
182        const BORDERS_OUTER = Self::BORDERS_OUTER_V.bits() | Self::BORDERS_OUTER_H.bits();
183        /// Draw all borders
184        const BORDERS = Self::BORDERS_INNER.bits() | Self::BORDERS_OUTER.bits();
185        /// [ALPHA] Disable vertical borders in columns Body (borders will always appears in Headers). -> May move to style
186        const NO_BORDERS_IN_BODY = sys::ImGuiTableFlags_NoBordersInBody as i32;
187        /// [ALPHA] Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers). -> May move to style
188        const NO_BORDERS_IN_BODY_UNTIL_RESIZE = sys::ImGuiTableFlags_NoBordersInBodyUntilResize as i32;
189        /// Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching contents width
190        const SIZING_FIXED_FIT = sys::ImGuiTableFlags_SizingFixedFit as i32;
191        /// Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching the maximum contents width of all columns. Implicitly enable ImGuiTableFlags_NoKeepColumnsVisible.
192        const SIZING_FIXED_SAME = sys::ImGuiTableFlags_SizingFixedSame as i32;
193        /// Columns default to _WidthStretch with default weights proportional to each columns contents widths.
194        const SIZING_STRETCH_PROP = sys::ImGuiTableFlags_SizingStretchProp as i32;
195        /// Columns default to _WidthStretch with default weights all equal, unless overridden by TableSetupColumn().
196        const SIZING_STRETCH_SAME = sys::ImGuiTableFlags_SizingStretchSame as i32;
197        /// Make outer width auto-fit to columns, overriding outer_size.x value. Only available when ScrollX/ScrollY are disabled and Stretch columns are not used.
198        const NO_HOST_EXTEND_X = sys::ImGuiTableFlags_NoHostExtendX as i32;
199        /// Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit). Only available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible.
200        const NO_HOST_EXTEND_Y = sys::ImGuiTableFlags_NoHostExtendY as i32;
201        /// Disable keeping column always minimally visible when ScrollX is on and table gets too small. Not recommended if columns are resizable.
202        const NO_KEEP_COLUMNS_VISIBLE = sys::ImGuiTableFlags_NoKeepColumnsVisible as i32;
203        /// Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth.
204        const PRECISE_WIDTHS = sys::ImGuiTableFlags_PreciseWidths as i32;
205        /// Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with TableSetupScrollFreeze().
206        const NO_CLIP = sys::ImGuiTableFlags_NoClip as i32;
207        /// Default if BordersOuterV is on. Enable outer-most padding. Generally desirable if you have headers.
208        const PAD_OUTER_X = sys::ImGuiTableFlags_PadOuterX as i32;
209        /// Default if BordersOuterV is off. Disable outer-most padding.
210        const NO_PAD_OUTER_X = sys::ImGuiTableFlags_NoPadOuterX as i32;
211        /// Disable inner padding between columns (double inner padding if BordersOuterV is on, single inner padding if BordersOuterV is off).
212        const NO_PAD_INNER_X = sys::ImGuiTableFlags_NoPadInnerX as i32;
213        /// Enable horizontal scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. Changes default sizing policy. Because this creates a child window, ScrollY is currently generally recommended when using ScrollX.
214        const SCROLL_X = sys::ImGuiTableFlags_ScrollX as i32;
215        /// Enable vertical scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size.
216        const SCROLL_Y = sys::ImGuiTableFlags_ScrollY as i32;
217        /// Hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1).
218        const SORT_MULTI = sys::ImGuiTableFlags_SortMulti as i32;
219        /// Allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0).
220        const SORT_TRISTATE = sys::ImGuiTableFlags_SortTristate as i32;
221        /// Highlight column headers when hovered (may not be visible if table header is declaring a background color)
222        const HIGHLIGHT_HOVERED_COLUMN = sys::ImGuiTableFlags_HighlightHoveredColumn as i32;
223    }
224}
225
226#[cfg(feature = "serde")]
227impl Serialize for TableFlags {
228    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
229    where
230        S: serde::Serializer,
231    {
232        serializer.serialize_i32(self.bits())
233    }
234}
235
236#[cfg(feature = "serde")]
237impl<'de> Deserialize<'de> for TableFlags {
238    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
239    where
240        D: serde::Deserializer<'de>,
241    {
242        let bits = i32::deserialize(deserializer)?;
243        Ok(TableFlags::from_bits_truncate(bits))
244    }
245}
246
247bitflags::bitflags! {
248    /// Flags for table columns
249    #[repr(transparent)]
250    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
251    pub struct TableColumnFlags: i32 {
252        /// No flags
253        const NONE = 0;
254        /// Overriding width becomes fixed width
255        const WIDTH_FIXED = sys::ImGuiTableColumnFlags_WidthFixed as i32;
256        /// Overriding width becomes weight
257        const WIDTH_STRETCH = sys::ImGuiTableColumnFlags_WidthStretch as i32;
258        /// Disable manual resizing
259        const NO_RESIZE = sys::ImGuiTableColumnFlags_NoResize as i32;
260        /// Disable manual reordering this column
261        const NO_REORDER = sys::ImGuiTableColumnFlags_NoReorder as i32;
262        /// Disable ability to hide/disable this column
263        const NO_HIDE = sys::ImGuiTableColumnFlags_NoHide as i32;
264        /// Disable clipping for this column
265        const NO_CLIP = sys::ImGuiTableColumnFlags_NoClip as i32;
266        /// Disable ability to sort on this field
267        const NO_SORT = sys::ImGuiTableColumnFlags_NoSort as i32;
268        /// Disable ability to sort in the ascending direction
269        const NO_SORT_ASCENDING = sys::ImGuiTableColumnFlags_NoSortAscending as i32;
270        /// Disable ability to sort in the descending direction
271        const NO_SORT_DESCENDING = sys::ImGuiTableColumnFlags_NoSortDescending as i32;
272        /// TableHeadersRow() will not submit label for this column
273        const NO_HEADER_LABEL = sys::ImGuiTableColumnFlags_NoHeaderLabel as i32;
274        /// Disable header text width contribution to automatic column width
275        const NO_HEADER_WIDTH = sys::ImGuiTableColumnFlags_NoHeaderWidth as i32;
276        /// Make the initial sort direction Ascending when first sorting on this column
277        const PREFER_SORT_ASCENDING = sys::ImGuiTableColumnFlags_PreferSortAscending as i32;
278        /// Make the initial sort direction Descending when first sorting on this column
279        const PREFER_SORT_DESCENDING = sys::ImGuiTableColumnFlags_PreferSortDescending as i32;
280        /// Use current Indent value when entering cell
281        const INDENT_ENABLE = sys::ImGuiTableColumnFlags_IndentEnable as i32;
282        /// Disable indenting for this column
283        const INDENT_DISABLE = sys::ImGuiTableColumnFlags_IndentDisable as i32;
284        /// Display an angled header for this column (when angled headers feature is enabled)
285        const ANGLED_HEADER = sys::ImGuiTableColumnFlags_AngledHeader as i32;
286        /// Status: is enabled == not hidden
287        const IS_ENABLED = sys::ImGuiTableColumnFlags_IsEnabled as i32;
288        /// Status: is visible == is enabled AND not clipped by scrolling
289        const IS_VISIBLE = sys::ImGuiTableColumnFlags_IsVisible as i32;
290        /// Status: is currently part of the sort specs
291        const IS_SORTED = sys::ImGuiTableColumnFlags_IsSorted as i32;
292        /// Status: is hovered by mouse
293        const IS_HOVERED = sys::ImGuiTableColumnFlags_IsHovered as i32;
294    }
295}
296
297#[cfg(feature = "serde")]
298impl Serialize for TableColumnFlags {
299    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
300    where
301        S: serde::Serializer,
302    {
303        serializer.serialize_i32(self.bits())
304    }
305}
306
307#[cfg(feature = "serde")]
308impl<'de> Deserialize<'de> for TableColumnFlags {
309    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
310    where
311        D: serde::Deserializer<'de>,
312    {
313        let bits = i32::deserialize(deserializer)?;
314        Ok(TableColumnFlags::from_bits_truncate(bits))
315    }
316}