dear-imgui-rs 0.17.0

High-level Rust bindings to Dear ImGui v1.92.9b with docking, WGPU/GL backends, and extensions (ImPlot/ImPlot3D, ImNodes, ImGuizmo, file browser, reflection-based UI)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use super::validation::{
    assert_color_single_choice_mask, color_button_supported_mask, color_data_type_mask,
    color_display_mask, color_edit_supported_mask, color_input_mask, color_picker_mask,
    color_picker_supported_mask, validate_color_button_flags, validate_color_edit_flags,
    validate_color_picker_flags, validate_color_supported_bits,
};
use crate::sys;

bitflags::bitflags! {
    /// Independently composable flags accepted by `ColorEdit3()`,
    /// `ColorEdit4()`, and `Io::set_color_edit_options()`.
    #[repr(transparent)]
    #[derive(Copy, Clone, Debug, Default, Hash, Eq, PartialEq)]
    pub struct ColorEditFlags: u32 {
        /// No flags.
        const NONE = 0;
        /// Ignore Alpha component (will only read 3 components from the input pointer).
        const NO_ALPHA = sys::ImGuiColorEditFlags_NoAlpha as u32;
        /// Disable picker when clicking on color square.
        const NO_PICKER = sys::ImGuiColorEditFlags_NoPicker as u32;
        /// Disable toggling options menu when right-clicking on inputs/small preview.
        const NO_OPTIONS = sys::ImGuiColorEditFlags_NoOptions as u32;
        /// Disable color square preview next to the inputs.
        const NO_SMALL_PREVIEW = sys::ImGuiColorEditFlags_NoSmallPreview as u32;
        /// Disable inputs sliders/text widgets.
        const NO_INPUTS = sys::ImGuiColorEditFlags_NoInputs as u32;
        /// Disable tooltip when hovering the preview.
        const NO_TOOLTIP = sys::ImGuiColorEditFlags_NoTooltip as u32;
        /// Disable display of inline text label.
        const NO_LABEL = sys::ImGuiColorEditFlags_NoLabel as u32;
        /// Disable drag and drop target/source.
        const NO_DRAG_DROP = sys::ImGuiColorEditFlags_NoDragDrop as u32;
        /// Disable rendering R/G/B/A color markers.
        const NO_COLOR_MARKERS = sys::ImGuiColorEditFlags_NoColorMarkers as u32;
        /// Show vertical alpha bar/gradient in the picker.
        const ALPHA_BAR = sys::ImGuiColorEditFlags_AlphaBar as u32;
        /// Disable alpha in the preview.
        const ALPHA_OPAQUE = sys::ImGuiColorEditFlags_AlphaOpaque as u32;
        /// Disable the checkerboard background behind transparent colors.
        const ALPHA_NO_BG = sys::ImGuiColorEditFlags_AlphaNoBg as u32;
        /// Display half opaque / half checkerboard, instead of opaque.
        const ALPHA_PREVIEW_HALF = sys::ImGuiColorEditFlags_AlphaPreviewHalf as u32;
        /// Disable 0.0f..1.0f limits in RGBA edition.
        const HDR = sys::ImGuiColorEditFlags_HDR as u32;
        /// Disable rotation of the saturation/value triangle in the color picker.
        const PICKER_NO_ROTATE = sys::ImGuiColorEditFlags_PickerNoRotate as u32;
    }
}

bitflags::bitflags! {
    /// Independently composable flags accepted by `ColorPicker3()` and
    /// `ColorPicker4()`.
    #[repr(transparent)]
    #[derive(Copy, Clone, Debug, Default, Hash, Eq, PartialEq)]
    pub struct ColorPickerFlags: u32 {
        /// No flags.
        const NONE = 0;
        /// Ignore Alpha component (will only read 3 components from the input pointer).
        const NO_ALPHA = sys::ImGuiColorEditFlags_NoAlpha as u32;
        /// Disable toggling options menu when right-clicking.
        const NO_OPTIONS = sys::ImGuiColorEditFlags_NoOptions as u32;
        /// Disable color square preview next to the inputs.
        const NO_SMALL_PREVIEW = sys::ImGuiColorEditFlags_NoSmallPreview as u32;
        /// Disable inputs sliders/text widgets.
        const NO_INPUTS = sys::ImGuiColorEditFlags_NoInputs as u32;
        /// Disable tooltip when hovering the preview.
        const NO_TOOLTIP = sys::ImGuiColorEditFlags_NoTooltip as u32;
        /// Disable display of inline text label.
        const NO_LABEL = sys::ImGuiColorEditFlags_NoLabel as u32;
        /// Disable bigger color preview on right side of the picker.
        const NO_SIDE_PREVIEW = sys::ImGuiColorEditFlags_NoSidePreview as u32;
        /// Show vertical alpha bar/gradient in the picker.
        const ALPHA_BAR = sys::ImGuiColorEditFlags_AlphaBar as u32;
        /// Disable alpha in the preview.
        const ALPHA_OPAQUE = sys::ImGuiColorEditFlags_AlphaOpaque as u32;
        /// Disable the checkerboard background behind transparent colors.
        const ALPHA_NO_BG = sys::ImGuiColorEditFlags_AlphaNoBg as u32;
        /// Display half opaque / half checkerboard, instead of opaque.
        const ALPHA_PREVIEW_HALF = sys::ImGuiColorEditFlags_AlphaPreviewHalf as u32;
        /// Disable 0.0f..1.0f limits in RGBA edition.
        const HDR = sys::ImGuiColorEditFlags_HDR as u32;
        /// Disable rotation of the saturation/value triangle.
        const PICKER_NO_ROTATE = sys::ImGuiColorEditFlags_PickerNoRotate as u32;
    }
}

bitflags::bitflags! {
    /// Independently composable flags accepted by `ColorButton()`.
    #[repr(transparent)]
    #[derive(Copy, Clone, Debug, Default, Hash, Eq, PartialEq)]
    pub struct ColorButtonFlags: u32 {
        /// No flags.
        const NONE = 0;
        /// Ignore Alpha component. For `ColorButton()` this does the same as [`Self::ALPHA_OPAQUE`].
        const NO_ALPHA = sys::ImGuiColorEditFlags_NoAlpha as u32;
        /// Disable tooltip when hovering the preview.
        const NO_TOOLTIP = sys::ImGuiColorEditFlags_NoTooltip as u32;
        /// Disable drag and drop source.
        const NO_DRAG_DROP = sys::ImGuiColorEditFlags_NoDragDrop as u32;
        /// Disable border (which is enforced by default).
        const NO_BORDER = sys::ImGuiColorEditFlags_NoBorder as u32;
        /// Disable alpha in the preview.
        const ALPHA_OPAQUE = sys::ImGuiColorEditFlags_AlphaOpaque as u32;
        /// Disable the checkerboard background behind transparent colors.
        const ALPHA_NO_BG = sys::ImGuiColorEditFlags_AlphaNoBg as u32;
        /// Display half opaque / half checkerboard, instead of opaque.
        const ALPHA_PREVIEW_HALF = sys::ImGuiColorEditFlags_AlphaPreviewHalf as u32;
    }
}

/// Single display mode for color edit widgets.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum ColorDisplayMode {
    Rgb,
    Hsv,
    Hex,
}

impl ColorDisplayMode {
    const fn raw(self) -> u32 {
        match self {
            Self::Rgb => sys::ImGuiColorEditFlags_DisplayRGB as u32,
            Self::Hsv => sys::ImGuiColorEditFlags_DisplayHSV as u32,
            Self::Hex => sys::ImGuiColorEditFlags_DisplayHex as u32,
        }
    }
}

/// Single numeric representation for color edit widgets and defaults.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum ColorDataType {
    Uint8,
    Float,
}

impl ColorDataType {
    const fn raw(self) -> u32 {
        match self {
            Self::Uint8 => sys::ImGuiColorEditFlags_Uint8 as u32,
            Self::Float => sys::ImGuiColorEditFlags_Float as u32,
        }
    }
}

/// Single picker implementation for color picker widgets and defaults.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum ColorPickerMode {
    HueBar,
    HueWheel,
}

impl ColorPickerMode {
    const fn raw(self) -> u32 {
        match self {
            Self::HueBar => sys::ImGuiColorEditFlags_PickerHueBar as u32,
            Self::HueWheel => sys::ImGuiColorEditFlags_PickerHueWheel as u32,
        }
    }
}

/// Single input/output color space for color edit and picker widgets.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum ColorInputMode {
    Rgb,
    Hsv,
}

impl ColorInputMode {
    const fn raw(self) -> u32 {
        match self {
            Self::Rgb => sys::ImGuiColorEditFlags_InputRGB as u32,
            Self::Hsv => sys::ImGuiColorEditFlags_InputHSV as u32,
        }
    }
}

bitflags::bitflags! {
    /// Display sub-editors visible inside `ColorPicker*()`.
    #[repr(transparent)]
    #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
    pub struct ColorPickerDisplayFlags: u32 {
        const RGB = sys::ImGuiColorEditFlags_DisplayRGB as u32;
        const HSV = sys::ImGuiColorEditFlags_DisplayHSV as u32;
        const HEX = sys::ImGuiColorEditFlags_DisplayHex as u32;
    }
}

/// Options accepted by `ColorEdit3()`, `ColorEdit4()`, and
/// `Io::set_color_edit_options()`.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct ColorEditOptions {
    pub flags: ColorEditFlags,
    pub display_mode: Option<ColorDisplayMode>,
    pub data_type: Option<ColorDataType>,
    pub picker_mode: Option<ColorPickerMode>,
    pub input_mode: Option<ColorInputMode>,
}

impl ColorEditOptions {
    pub const fn new() -> Self {
        Self {
            flags: ColorEditFlags::NONE,
            display_mode: None,
            data_type: None,
            picker_mode: None,
            input_mode: None,
        }
    }

    pub fn flags(mut self, flags: ColorEditFlags) -> Self {
        self.flags = flags;
        self
    }

    pub fn display_mode(mut self, mode: ColorDisplayMode) -> Self {
        self.display_mode = Some(mode);
        self
    }

    pub fn data_type(mut self, data_type: ColorDataType) -> Self {
        self.data_type = Some(data_type);
        self
    }

    pub fn picker_mode(mut self, mode: ColorPickerMode) -> Self {
        self.picker_mode = Some(mode);
        self
    }

    pub fn input_mode(mut self, mode: ColorInputMode) -> Self {
        self.input_mode = Some(mode);
        self
    }

    pub fn bits(self) -> u32 {
        self.flags.bits()
            | self.display_mode.map_or(0, ColorDisplayMode::raw)
            | self.data_type.map_or(0, ColorDataType::raw)
            | self.picker_mode.map_or(0, ColorPickerMode::raw)
            | self.input_mode.map_or(0, ColorInputMode::raw)
    }

    pub(crate) fn from_bits(bits: u32, caller: &str) -> Self {
        let options = Self {
            flags: ColorEditFlags::from_bits_retain(bits & ColorEditFlags::all().bits()),
            display_mode: match bits & color_display_mask() {
                0 => None,
                value if value == ColorDisplayMode::Rgb.raw() => Some(ColorDisplayMode::Rgb),
                value if value == ColorDisplayMode::Hsv.raw() => Some(ColorDisplayMode::Hsv),
                value if value == ColorDisplayMode::Hex.raw() => Some(ColorDisplayMode::Hex),
                _ => panic!("{caller} contains an invalid color display mode"),
            },
            data_type: match bits & color_data_type_mask() {
                0 => None,
                value if value == ColorDataType::Uint8.raw() => Some(ColorDataType::Uint8),
                value if value == ColorDataType::Float.raw() => Some(ColorDataType::Float),
                _ => panic!("{caller} contains an invalid color data type"),
            },
            picker_mode: match bits & color_picker_mask() {
                0 => None,
                value if value == ColorPickerMode::HueBar.raw() => Some(ColorPickerMode::HueBar),
                value if value == ColorPickerMode::HueWheel.raw() => {
                    Some(ColorPickerMode::HueWheel)
                }
                _ => panic!("{caller} contains an invalid color picker mode"),
            },
            input_mode: match bits & color_input_mask() {
                0 => None,
                value if value == ColorInputMode::Rgb.raw() => Some(ColorInputMode::Rgb),
                value if value == ColorInputMode::Hsv.raw() => Some(ColorInputMode::Hsv),
                _ => panic!("{caller} contains an invalid color input mode"),
            },
        };
        options.validate(caller);
        options
    }

    pub(crate) fn with_default_choices(self) -> Self {
        let mut bits = self.bits();
        let defaults = sys::ImGuiColorEditFlags_DefaultOptions_ as u32;
        for mask in [
            color_display_mask(),
            color_data_type_mask(),
            color_picker_mask(),
            color_input_mask(),
        ] {
            if bits & mask == 0 {
                bits |= defaults & mask;
            }
        }
        Self::from_bits(bits, "ColorEditOptions::with_default_choices()")
    }

    pub(crate) fn validate(self, caller: &str) {
        validate_color_edit_flags(caller, self.flags);
        validate_color_supported_bits(caller, self.bits(), color_edit_supported_mask());
        assert_color_single_choice_mask(caller, self.bits(), color_display_mask(), "display mode");
        assert_color_single_choice_mask(caller, self.bits(), color_data_type_mask(), "data type");
        assert_color_single_choice_mask(caller, self.bits(), color_picker_mask(), "picker mode");
        assert_color_single_choice_mask(caller, self.bits(), color_input_mask(), "input mode");
    }
}

impl Default for ColorEditOptions {
    fn default() -> Self {
        Self::new()
    }
}

impl From<ColorEditFlags> for ColorEditOptions {
    fn from(flags: ColorEditFlags) -> Self {
        Self::new().flags(flags)
    }
}

/// Options accepted by `ColorPicker3()` and `ColorPicker4()`.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct ColorPickerOptions {
    pub flags: ColorPickerFlags,
    pub display_flags: ColorPickerDisplayFlags,
    pub data_type: Option<ColorDataType>,
    pub picker_mode: Option<ColorPickerMode>,
    pub input_mode: Option<ColorInputMode>,
}

impl ColorPickerOptions {
    pub const fn new() -> Self {
        Self {
            flags: ColorPickerFlags::NONE,
            display_flags: ColorPickerDisplayFlags::empty(),
            data_type: None,
            picker_mode: None,
            input_mode: None,
        }
    }

    pub fn flags(mut self, flags: ColorPickerFlags) -> Self {
        self.flags = flags;
        self
    }

    pub fn display_flags(mut self, flags: ColorPickerDisplayFlags) -> Self {
        self.display_flags = flags;
        self
    }

    pub fn data_type(mut self, data_type: ColorDataType) -> Self {
        self.data_type = Some(data_type);
        self
    }

    pub fn picker_mode(mut self, mode: ColorPickerMode) -> Self {
        self.picker_mode = Some(mode);
        self
    }

    pub fn input_mode(mut self, mode: ColorInputMode) -> Self {
        self.input_mode = Some(mode);
        self
    }

    pub fn bits(self) -> u32 {
        self.flags.bits()
            | self.display_flags.bits()
            | self.data_type.map_or(0, ColorDataType::raw)
            | self.picker_mode.map_or(0, ColorPickerMode::raw)
            | self.input_mode.map_or(0, ColorInputMode::raw)
    }

    pub(crate) fn validate(self, caller: &str) {
        validate_color_picker_flags(caller, self.flags);
        let unsupported_display =
            self.display_flags.bits() & !ColorPickerDisplayFlags::all().bits();
        assert!(
            unsupported_display == 0,
            "{caller} received unsupported ColorPickerDisplayFlags bits: 0x{unsupported_display:X}"
        );
        validate_color_supported_bits(caller, self.bits(), color_picker_supported_mask());
        assert_color_single_choice_mask(caller, self.bits(), color_data_type_mask(), "data type");
        assert_color_single_choice_mask(caller, self.bits(), color_picker_mask(), "picker mode");
        assert_color_single_choice_mask(caller, self.bits(), color_input_mask(), "input mode");
    }
}

impl Default for ColorPickerOptions {
    fn default() -> Self {
        Self::new()
    }
}

impl From<ColorPickerFlags> for ColorPickerOptions {
    fn from(flags: ColorPickerFlags) -> Self {
        Self::new().flags(flags)
    }
}

/// Options accepted by `ColorButton()`.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct ColorButtonOptions {
    pub flags: ColorButtonFlags,
    pub input_mode: Option<ColorInputMode>,
}

impl ColorButtonOptions {
    pub const fn new() -> Self {
        Self {
            flags: ColorButtonFlags::NONE,
            input_mode: None,
        }
    }

    pub fn flags(mut self, flags: ColorButtonFlags) -> Self {
        self.flags = flags;
        self
    }

    pub fn input_mode(mut self, mode: ColorInputMode) -> Self {
        self.input_mode = Some(mode);
        self
    }

    pub fn bits(self) -> u32 {
        self.flags.bits() | self.input_mode.map_or(0, ColorInputMode::raw)
    }

    pub(crate) fn validate(self, caller: &str) {
        validate_color_button_flags(caller, self.flags);
        validate_color_supported_bits(caller, self.bits(), color_button_supported_mask());
        assert_color_single_choice_mask(caller, self.bits(), color_input_mask(), "input mode");
    }
}

impl Default for ColorButtonOptions {
    fn default() -> Self {
        Self::new()
    }
}

impl From<ColorButtonFlags> for ColorButtonOptions {
    fn from(flags: ColorButtonFlags) -> Self {
        Self::new().flags(flags)
    }
}