dear-imgui-rs 0.12.0

High-level Rust bindings to Dear ImGui v1.92.7 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//! Input types (mouse, keyboard, cursors)
//!
//! Strongly-typed identifiers for mouse buttons, mouse cursors and keyboard
//! keys used by Dear ImGui. Backends typically translate platform events into
//! these enums when feeding input into `Io`.
//!
//! See [`io`] for the per-frame input state and configuration.
//!
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::as_conversions,
    clippy::unnecessary_cast
)]
use crate::sys;
use bitflags::bitflags;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Mouse button identifier
#[repr(i32)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MouseButton {
    /// Left mouse button
    Left = sys::ImGuiMouseButton_Left as i32,
    /// Right mouse button
    Right = sys::ImGuiMouseButton_Right as i32,
    /// Middle mouse button
    Middle = sys::ImGuiMouseButton_Middle as i32,
    /// Extra mouse button 1 (typically Back)
    Extra1 = 3,
    /// Extra mouse button 2 (typically Forward)
    Extra2 = 4,
}

/// Mouse cursor types
#[repr(i32)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MouseCursor {
    /// No cursor
    None = sys::ImGuiMouseCursor_None as i32,
    /// Arrow cursor
    Arrow = sys::ImGuiMouseCursor_Arrow as i32,
    /// Text input I-beam cursor
    TextInput = sys::ImGuiMouseCursor_TextInput as i32,
    /// Resize all directions cursor
    ResizeAll = sys::ImGuiMouseCursor_ResizeAll as i32,
    /// Resize north-south cursor
    ResizeNS = sys::ImGuiMouseCursor_ResizeNS as i32,
    /// Resize east-west cursor
    ResizeEW = sys::ImGuiMouseCursor_ResizeEW as i32,
    /// Resize northeast-southwest cursor
    ResizeNESW = sys::ImGuiMouseCursor_ResizeNESW as i32,
    /// Resize northwest-southeast cursor
    ResizeNWSE = sys::ImGuiMouseCursor_ResizeNWSE as i32,
    /// Hand cursor
    Hand = sys::ImGuiMouseCursor_Hand as i32,
    /// Not allowed cursor
    NotAllowed = sys::ImGuiMouseCursor_NotAllowed as i32,
}

/// Source of mouse-like input events.
///
/// Backends can use this to mark whether a mouse event originates from a
/// physical mouse, a touch screen, or a pen/stylus so Dear ImGui can
/// correctly handle multiple input sources.
#[repr(i32)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MouseSource {
    /// Events coming from a physical mouse
    Mouse = sys::ImGuiMouseSource_Mouse as i32,
    /// Events coming from a touch screen
    TouchScreen = sys::ImGuiMouseSource_TouchScreen as i32,
    /// Events coming from a pen or stylus
    Pen = sys::ImGuiMouseSource_Pen as i32,
}

/// Key identifier for keyboard input
#[repr(i32)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Key {
    /// No key
    None = sys::ImGuiKey_None as i32,
    /// Tab key
    Tab = sys::ImGuiKey_Tab as i32,
    /// Left arrow key
    LeftArrow = sys::ImGuiKey_LeftArrow as i32,
    /// Right arrow key
    RightArrow = sys::ImGuiKey_RightArrow as i32,
    /// Up arrow key
    UpArrow = sys::ImGuiKey_UpArrow as i32,
    /// Down arrow key
    DownArrow = sys::ImGuiKey_DownArrow as i32,
    /// Page up key
    PageUp = sys::ImGuiKey_PageUp as i32,
    /// Page down key
    PageDown = sys::ImGuiKey_PageDown as i32,
    /// Home key
    Home = sys::ImGuiKey_Home as i32,
    /// End key
    End = sys::ImGuiKey_End as i32,
    /// Insert key
    Insert = sys::ImGuiKey_Insert as i32,
    /// Delete key
    Delete = sys::ImGuiKey_Delete as i32,
    /// Backspace key
    Backspace = sys::ImGuiKey_Backspace as i32,
    /// Space key
    Space = sys::ImGuiKey_Space as i32,
    /// Enter key
    Enter = sys::ImGuiKey_Enter as i32,
    /// Escape key
    Escape = sys::ImGuiKey_Escape as i32,
    /// Left Ctrl key
    LeftCtrl = sys::ImGuiKey_LeftCtrl as i32,
    /// Left Shift key
    LeftShift = sys::ImGuiKey_LeftShift as i32,
    /// Left Alt key
    LeftAlt = sys::ImGuiKey_LeftAlt as i32,
    /// Left Super key
    LeftSuper = sys::ImGuiKey_LeftSuper as i32,
    /// Right Ctrl key
    RightCtrl = sys::ImGuiKey_RightCtrl as i32,
    /// Right Shift key
    RightShift = sys::ImGuiKey_RightShift as i32,
    /// Right Alt key
    RightAlt = sys::ImGuiKey_RightAlt as i32,
    /// Right Super key
    RightSuper = sys::ImGuiKey_RightSuper as i32,
    /// Ctrl modifier (for `io.KeyMods` / `io.KeyCtrl`)
    ModCtrl = sys::ImGuiMod_Ctrl as i32,
    /// Shift modifier (for `io.KeyMods` / `io.KeyShift`)
    ModShift = sys::ImGuiMod_Shift as i32,
    /// Alt modifier (for `io.KeyMods` / `io.KeyAlt`)
    ModAlt = sys::ImGuiMod_Alt as i32,
    /// Super/Cmd modifier (for `io.KeyMods` / `io.KeySuper`)
    ModSuper = sys::ImGuiMod_Super as i32,
    /// Menu key
    Menu = sys::ImGuiKey_Menu as i32,
    /// 0 key
    Key0 = sys::ImGuiKey_0 as i32,
    /// 1 key
    Key1 = sys::ImGuiKey_1 as i32,
    /// 2 key
    Key2 = sys::ImGuiKey_2 as i32,
    /// 3 key
    Key3 = sys::ImGuiKey_3 as i32,
    /// 4 key
    Key4 = sys::ImGuiKey_4 as i32,
    /// 5 key
    Key5 = sys::ImGuiKey_5 as i32,
    /// 6 key
    Key6 = sys::ImGuiKey_6 as i32,
    /// 7 key
    Key7 = sys::ImGuiKey_7 as i32,
    /// 8 key
    Key8 = sys::ImGuiKey_8 as i32,
    /// 9 key
    Key9 = sys::ImGuiKey_9 as i32,
    /// A key
    A = sys::ImGuiKey_A as i32,
    /// B key
    B = sys::ImGuiKey_B as i32,
    /// C key
    C = sys::ImGuiKey_C as i32,
    /// D key
    D = sys::ImGuiKey_D as i32,
    /// E key
    E = sys::ImGuiKey_E as i32,
    /// F key
    F = sys::ImGuiKey_F as i32,
    /// G key
    G = sys::ImGuiKey_G as i32,
    /// H key
    H = sys::ImGuiKey_H as i32,
    /// I key
    I = sys::ImGuiKey_I as i32,
    /// J key
    J = sys::ImGuiKey_J as i32,
    /// K key
    K = sys::ImGuiKey_K as i32,
    /// L key
    L = sys::ImGuiKey_L as i32,
    /// M key
    M = sys::ImGuiKey_M as i32,
    /// N key
    N = sys::ImGuiKey_N as i32,
    /// O key
    O = sys::ImGuiKey_O as i32,
    /// P key
    P = sys::ImGuiKey_P as i32,
    /// Q key
    Q = sys::ImGuiKey_Q as i32,
    /// R key
    R = sys::ImGuiKey_R as i32,
    /// S key
    S = sys::ImGuiKey_S as i32,
    /// T key
    T = sys::ImGuiKey_T as i32,
    /// U key
    U = sys::ImGuiKey_U as i32,
    /// V key
    V = sys::ImGuiKey_V as i32,
    /// W key
    W = sys::ImGuiKey_W as i32,
    /// X key
    X = sys::ImGuiKey_X as i32,
    /// Y key
    Y = sys::ImGuiKey_Y as i32,
    /// Z key
    Z = sys::ImGuiKey_Z as i32,
    /// F1 key
    F1 = sys::ImGuiKey_F1 as i32,
    /// F2 key
    F2 = sys::ImGuiKey_F2 as i32,
    /// F3 key
    F3 = sys::ImGuiKey_F3 as i32,
    /// F4 key
    F4 = sys::ImGuiKey_F4 as i32,
    /// F5 key
    F5 = sys::ImGuiKey_F5 as i32,
    /// F6 key
    F6 = sys::ImGuiKey_F6 as i32,
    /// F7 key
    F7 = sys::ImGuiKey_F7 as i32,
    /// F8 key
    F8 = sys::ImGuiKey_F8 as i32,
    /// F9 key
    F9 = sys::ImGuiKey_F9 as i32,
    /// F10 key
    F10 = sys::ImGuiKey_F10 as i32,
    /// F11 key
    F11 = sys::ImGuiKey_F11 as i32,
    /// F12 key
    F12 = sys::ImGuiKey_F12 as i32,

    // --- Punctuation and extra named keys ---
    /// Apostrophe (') key
    Apostrophe = sys::ImGuiKey_Apostrophe as i32,
    /// Comma (,) key
    Comma = sys::ImGuiKey_Comma as i32,
    /// Minus (-) key
    Minus = sys::ImGuiKey_Minus as i32,
    /// Period (.) key
    Period = sys::ImGuiKey_Period as i32,
    /// Slash (/) key
    Slash = sys::ImGuiKey_Slash as i32,
    /// Semicolon (;) key
    Semicolon = sys::ImGuiKey_Semicolon as i32,
    /// Equal (=) key
    Equal = sys::ImGuiKey_Equal as i32,
    /// Left bracket ([) key
    LeftBracket = sys::ImGuiKey_LeftBracket as i32,
    /// Backslash (\) key
    Backslash = sys::ImGuiKey_Backslash as i32,
    /// Right bracket (]) key
    RightBracket = sys::ImGuiKey_RightBracket as i32,
    /// Grave accent (`) key
    GraveAccent = sys::ImGuiKey_GraveAccent as i32,
    /// CapsLock key
    CapsLock = sys::ImGuiKey_CapsLock as i32,
    /// ScrollLock key
    ScrollLock = sys::ImGuiKey_ScrollLock as i32,
    /// NumLock key
    NumLock = sys::ImGuiKey_NumLock as i32,
    /// PrintScreen key
    PrintScreen = sys::ImGuiKey_PrintScreen as i32,
    /// Pause key
    Pause = sys::ImGuiKey_Pause as i32,

    // --- Keypad ---
    /// Numpad 0
    Keypad0 = sys::ImGuiKey_Keypad0 as i32,
    /// Numpad 1
    Keypad1 = sys::ImGuiKey_Keypad1 as i32,
    /// Numpad 2
    Keypad2 = sys::ImGuiKey_Keypad2 as i32,
    /// Numpad 3
    Keypad3 = sys::ImGuiKey_Keypad3 as i32,
    /// Numpad 4
    Keypad4 = sys::ImGuiKey_Keypad4 as i32,
    /// Numpad 5
    Keypad5 = sys::ImGuiKey_Keypad5 as i32,
    /// Numpad 6
    Keypad6 = sys::ImGuiKey_Keypad6 as i32,
    /// Numpad 7
    Keypad7 = sys::ImGuiKey_Keypad7 as i32,
    /// Numpad 8
    Keypad8 = sys::ImGuiKey_Keypad8 as i32,
    /// Numpad 9
    Keypad9 = sys::ImGuiKey_Keypad9 as i32,
    /// Numpad decimal
    KeypadDecimal = sys::ImGuiKey_KeypadDecimal as i32,
    /// Numpad divide
    KeypadDivide = sys::ImGuiKey_KeypadDivide as i32,
    /// Numpad multiply
    KeypadMultiply = sys::ImGuiKey_KeypadMultiply as i32,
    /// Numpad subtract
    KeypadSubtract = sys::ImGuiKey_KeypadSubtract as i32,
    /// Numpad add
    KeypadAdd = sys::ImGuiKey_KeypadAdd as i32,
    /// Numpad enter
    KeypadEnter = sys::ImGuiKey_KeypadEnter as i32,
    /// Numpad equal
    KeypadEqual = sys::ImGuiKey_KeypadEqual as i32,

    /// OEM 102 key (ISO < > |)
    Oem102 = sys::ImGuiKey_Oem102 as i32,
}

impl From<MouseButton> for sys::ImGuiMouseButton {
    #[inline]
    fn from(value: MouseButton) -> sys::ImGuiMouseButton {
        value as sys::ImGuiMouseButton
    }
}

impl From<MouseSource> for sys::ImGuiMouseSource {
    #[inline]
    fn from(value: MouseSource) -> sys::ImGuiMouseSource {
        value as sys::ImGuiMouseSource
    }
}

impl From<Key> for sys::ImGuiKey {
    #[inline]
    fn from(value: Key) -> sys::ImGuiKey {
        value as sys::ImGuiKey
    }
}

// Key modifier flags are available via io.KeyCtrl/KeyShift/KeyAlt/KeySuper.
// Backends should submit modifier state via `Key::ModCtrl`/`ModShift`/`ModAlt`/`ModSuper` using `Io::add_key_event`.

bitflags! {
    /// Modifier flags for building an ImGui key chord.
    #[repr(transparent)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct KeyMods: i32 {
        /// Ctrl modifier
        const CTRL = sys::ImGuiMod_Ctrl as i32;
        /// Shift modifier
        const SHIFT = sys::ImGuiMod_Shift as i32;
        /// Alt modifier
        const ALT = sys::ImGuiMod_Alt as i32;
        /// Super/Cmd modifier
        const SUPER = sys::ImGuiMod_Super as i32;
    }
}

impl Default for KeyMods {
    fn default() -> Self {
        KeyMods::empty()
    }
}

impl KeyMods {
    #[inline]
    pub(crate) fn raw(self) -> sys::ImGuiKeyChord {
        self.bits() as sys::ImGuiKeyChord
    }
}

/// A key chord (key + optional modifier flags), used by ImGui shortcut routing APIs.
///
/// This is a thin wrapper over `sys::ImGuiKeyChord` (an `int`).
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct KeyChord(sys::ImGuiKeyChord);

impl KeyChord {
    /// Create a chord from a key (no modifiers).
    pub fn new(key: Key) -> Self {
        Self(key as sys::ImGuiKeyChord)
    }

    /// Add modifier flags to the chord.
    pub fn with_mods(self, mods: KeyMods) -> Self {
        Self(self.0 | mods.raw())
    }

    /// Returns the raw `ImGuiKeyChord` value.
    pub fn raw(self) -> sys::ImGuiKeyChord {
        self.0
    }
}

impl From<Key> for KeyChord {
    fn from(value: Key) -> Self {
        Self::new(value)
    }
}

bitflags! {
    /// Independent input flags accepted by `Shortcut()`.
    ///
    /// The route policy is a single-choice setting represented by
    /// [`ShortcutRoute`].
    #[repr(transparent)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct ShortcutFlags: i32 {
        const NONE = sys::ImGuiInputFlags_None as i32;
        const REPEAT = sys::ImGuiInputFlags_Repeat as i32;
        const ROUTE_FROM_ROOT_WINDOW = sys::ImGuiInputFlags_RouteFromRootWindow as i32;
    }
}

impl Default for ShortcutFlags {
    fn default() -> Self {
        ShortcutFlags::NONE
    }
}

bitflags! {
    /// Options accepted only by the global shortcut route.
    #[repr(transparent)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct ShortcutGlobalRouteFlags: i32 {
        const NONE = sys::ImGuiInputFlags_None as i32;
        const OVER_FOCUSED = sys::ImGuiInputFlags_RouteOverFocused as i32;
        const OVER_ACTIVE = sys::ImGuiInputFlags_RouteOverActive as i32;
        const UNLESS_BG_FOCUSED = sys::ImGuiInputFlags_RouteUnlessBgFocused as i32;
    }
}

/// Single route policy for `Shortcut()` and `SetNextItemShortcut()`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ShortcutRoute {
    /// Route to the active item only.
    Active,
    /// Route to windows in the focus stack. This is Dear ImGui's `Shortcut()`
    /// default when no explicit route is provided.
    Focused,
    /// Focused route with higher priority than the active item.
    FocusedOverActive,
    /// Global route with optional global-only priority modifiers.
    Global(ShortcutGlobalRouteFlags),
    /// Poll keys directly without route registration.
    Always,
}

impl ShortcutRoute {
    #[inline]
    const fn raw(self) -> sys::ImGuiInputFlags {
        match self {
            Self::Active => sys::ImGuiInputFlags_RouteActive as sys::ImGuiInputFlags,
            Self::Focused => sys::ImGuiInputFlags_RouteFocused as sys::ImGuiInputFlags,
            Self::FocusedOverActive => {
                sys::ImGuiInputFlags_RouteFocused as sys::ImGuiInputFlags
                    | sys::ImGuiInputFlags_RouteOverActive as sys::ImGuiInputFlags
            }
            Self::Global(flags) => {
                sys::ImGuiInputFlags_RouteGlobal as sys::ImGuiInputFlags | flags.bits()
            }
            Self::Always => sys::ImGuiInputFlags_RouteAlways as sys::ImGuiInputFlags,
        }
    }

    /// Returns the underlying Dear ImGui bits for this route policy.
    pub const fn bits(self) -> i32 {
        self.raw()
    }
}

/// Complete shortcut options assembled from independent flags and an optional
/// single route policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ShortcutOptions {
    pub flags: ShortcutFlags,
    pub route: Option<ShortcutRoute>,
}

impl ShortcutOptions {
    pub const fn new() -> Self {
        Self {
            flags: ShortcutFlags::NONE,
            route: None,
        }
    }

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

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

    /// Returns the underlying Dear ImGui bits for these options.
    pub fn bits(self) -> i32 {
        self.raw()
    }

    #[inline]
    pub(crate) fn raw(self) -> sys::ImGuiInputFlags {
        self.flags.bits() | self.route.map_or(0, ShortcutRoute::raw)
    }
}

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

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

impl From<ShortcutRoute> for ShortcutOptions {
    fn from(route: ShortcutRoute) -> Self {
        Self::new().route(route)
    }
}

/// Backwards-compatible name for shortcut options.
pub type InputFlags = ShortcutOptions;

bitflags! {
    /// Flags specific to `SetNextItemShortcut()`.
    #[repr(transparent)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct NextItemShortcutFlags: i32 {
        const NONE = sys::ImGuiInputFlags_None as i32;
        const TOOLTIP = sys::ImGuiInputFlags_Tooltip as i32;
    }
}

/// Complete options accepted by `SetNextItemShortcut()`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NextItemShortcutOptions {
    pub shortcut: ShortcutOptions,
    pub flags: NextItemShortcutFlags,
}

impl NextItemShortcutOptions {
    pub const fn new() -> Self {
        Self {
            shortcut: ShortcutOptions::new(),
            flags: NextItemShortcutFlags::NONE,
        }
    }

    pub fn shortcut(mut self, options: impl Into<ShortcutOptions>) -> Self {
        self.shortcut = options.into();
        self
    }

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

    pub fn route(mut self, route: ShortcutRoute) -> Self {
        self.shortcut.route = Some(route);
        self
    }

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

    pub fn tooltip(mut self, value: bool) -> Self {
        self.flags.set(NextItemShortcutFlags::TOOLTIP, value);
        self
    }

    /// Returns the underlying Dear ImGui bits for these options.
    pub fn bits(self) -> i32 {
        self.raw()
    }

    #[inline]
    pub(crate) fn raw(self) -> sys::ImGuiInputFlags {
        self.shortcut.raw() | self.flags.bits()
    }
}

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

impl From<ShortcutOptions> for NextItemShortcutOptions {
    fn from(shortcut: ShortcutOptions) -> Self {
        Self::new().shortcut(shortcut)
    }
}

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

impl From<ShortcutRoute> for NextItemShortcutOptions {
    fn from(route: ShortcutRoute) -> Self {
        Self::new().route(route)
    }
}

impl From<NextItemShortcutFlags> for NextItemShortcutOptions {
    fn from(flags: NextItemShortcutFlags) -> Self {
        Self::new().next_item_flags(flags)
    }
}

bitflags! {
    /// Input flags accepted by `SetItemKeyOwner()`.
    #[repr(transparent)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct ItemKeyOwnerFlags: i32 {
        const NONE = sys::ImGuiInputFlags_None as i32;

        const LOCK_THIS_FRAME = sys::ImGuiInputFlags_LockThisFrame as i32;
        const LOCK_UNTIL_RELEASE = sys::ImGuiInputFlags_LockUntilRelease as i32;

        const COND_HOVERED = sys::ImGuiInputFlags_CondHovered as i32;
        const COND_ACTIVE = sys::ImGuiInputFlags_CondActive as i32;
    }
}

impl Default for ItemKeyOwnerFlags {
    fn default() -> Self {
        ItemKeyOwnerFlags::NONE
    }
}

impl ItemKeyOwnerFlags {
    #[inline]
    pub(crate) fn raw(self) -> sys::ImGuiInputFlags {
        self.bits() as sys::ImGuiInputFlags
    }
}

impl Default for NextItemShortcutFlags {
    fn default() -> Self {
        NextItemShortcutFlags::NONE
    }
}

bitflags! {
    /// Input text flags for text input widgets
    #[repr(transparent)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct InputTextFlags: i32 {
        /// No flags
        const NONE = sys::ImGuiInputTextFlags_None as i32;
        /// Allow 0123456789.+-*/
        const CHARS_DECIMAL = sys::ImGuiInputTextFlags_CharsDecimal as i32;
        /// Allow 0123456789ABCDEFabcdef
        const CHARS_HEXADECIMAL = sys::ImGuiInputTextFlags_CharsHexadecimal as i32;
        /// Turn a..z into A..Z
        const CHARS_UPPERCASE = sys::ImGuiInputTextFlags_CharsUppercase as i32;
        /// Filter out spaces, tabs
        const CHARS_NO_BLANK = sys::ImGuiInputTextFlags_CharsNoBlank as i32;
        /// Select entire text when first taking mouse focus
        const AUTO_SELECT_ALL = sys::ImGuiInputTextFlags_AutoSelectAll as i32;
        /// Return 'true' when Enter is pressed (as opposed to every time the value was modified)
        const ENTER_RETURNS_TRUE = sys::ImGuiInputTextFlags_EnterReturnsTrue as i32;
        /// Callback on pressing TAB (for completion handling)
        const CALLBACK_COMPLETION = sys::ImGuiInputTextFlags_CallbackCompletion as i32;
        /// Callback on pressing Up/Down arrows (for history handling)
        const CALLBACK_HISTORY = sys::ImGuiInputTextFlags_CallbackHistory as i32;
        /// Callback on each iteration (user can query cursor and modify text)
        const CALLBACK_ALWAYS = sys::ImGuiInputTextFlags_CallbackAlways as i32;
        /// Callback on character inputs to replace or discard them
        const CALLBACK_CHAR_FILTER = sys::ImGuiInputTextFlags_CallbackCharFilter as i32;
        /// Pressing TAB input a '\t' character into the text field
        const ALLOW_TAB_INPUT = sys::ImGuiInputTextFlags_AllowTabInput as i32;
        /// In multi-line mode, unfocus with Enter, add new line with Ctrl+Enter
        const CTRL_ENTER_FOR_NEW_LINE = sys::ImGuiInputTextFlags_CtrlEnterForNewLine as i32;
        /// Disable following the cursor horizontally
        const NO_HORIZONTAL_SCROLL = sys::ImGuiInputTextFlags_NoHorizontalScroll as i32;
        /// Overwrite mode
        const ALWAYS_OVERWRITE = sys::ImGuiInputTextFlags_AlwaysOverwrite as i32;
        /// Read-only mode
        const READ_ONLY = sys::ImGuiInputTextFlags_ReadOnly as i32;
        /// Password mode, display all characters as '*'
        const PASSWORD = sys::ImGuiInputTextFlags_Password as i32;
        /// Disable undo/redo
        const NO_UNDO_REDO = sys::ImGuiInputTextFlags_NoUndoRedo as i32;
        /// Allow 0123456789.+-*/eE (Scientific notation input)
        const CHARS_SCIENTIFIC = sys::ImGuiInputTextFlags_CharsScientific as i32;
        /// Callback on buffer capacity changes request
        const CALLBACK_RESIZE = sys::ImGuiInputTextFlags_CallbackResize as i32;
        /// Callback on any edit (note that InputText() already returns true on edit)
        const CALLBACK_EDIT = sys::ImGuiInputTextFlags_CallbackEdit as i32;
    }
}

impl InputTextFlags {
    #[inline]
    pub(crate) fn raw(self) -> sys::ImGuiInputTextFlags {
        self.bits() as sys::ImGuiInputTextFlags
    }
}

#[cfg(feature = "serde")]
impl Serialize for InputTextFlags {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_i32(self.bits())
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for InputTextFlags {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let bits = i32::deserialize(deserializer)?;
        Ok(InputTextFlags::from_bits_truncate(bits))
    }
}

// TODO: Add NavInput enum once we have proper constants in sys crate

impl crate::Ui {
    /// Check if a key is being held down
    #[doc(alias = "IsKeyDown")]
    pub fn is_key_down(&self, key: Key) -> bool {
        unsafe { sys::igIsKeyDown_Nil(key as sys::ImGuiKey) }
    }

    /// Check if a key was pressed (went from !Down to Down)
    #[doc(alias = "IsKeyPressed")]
    pub fn is_key_pressed(&self, key: Key) -> bool {
        unsafe { sys::igIsKeyPressed_Bool(key as sys::ImGuiKey, true) }
    }

    /// Check if a key was pressed (went from !Down to Down), with repeat
    #[doc(alias = "IsKeyPressed")]
    pub fn is_key_pressed_with_repeat(&self, key: Key, repeat: bool) -> bool {
        unsafe { sys::igIsKeyPressed_Bool(key as sys::ImGuiKey, repeat) }
    }

    /// Check if a key was released (went from Down to !Down)
    #[doc(alias = "IsKeyReleased")]
    pub fn is_key_released(&self, key: Key) -> bool {
        unsafe { sys::igIsKeyReleased_Nil(key as sys::ImGuiKey) }
    }

    /// Check if a key chord was pressed (e.g. `Ctrl+S`).
    #[doc(alias = "IsKeyChordPressed")]
    pub fn is_key_chord_pressed(&self, key_chord: KeyChord) -> bool {
        unsafe { sys::igIsKeyChordPressed_Nil(key_chord.raw()) }
    }

    /// Call ImGui shortcut routing with default flags.
    #[doc(alias = "Shortcut")]
    pub fn shortcut(&self, key_chord: KeyChord) -> bool {
        self.shortcut_with_flags(key_chord, ShortcutOptions::new())
    }

    /// Call ImGui shortcut routing with explicit input options.
    #[doc(alias = "Shortcut")]
    pub fn shortcut_with_flags(
        &self,
        key_chord: KeyChord,
        flags: impl Into<ShortcutOptions>,
    ) -> bool {
        unsafe { sys::igShortcut_Nil(key_chord.raw(), flags.into().raw()) }
    }

    /// Set the next item's shortcut with default flags.
    #[doc(alias = "SetNextItemShortcut")]
    pub fn set_next_item_shortcut(&self, key_chord: KeyChord) {
        self.set_next_item_shortcut_with_flags(key_chord, NextItemShortcutOptions::new());
    }

    /// Set the next item's shortcut with explicit options.
    #[doc(alias = "SetNextItemShortcut")]
    pub fn set_next_item_shortcut_with_flags(
        &self,
        key_chord: KeyChord,
        flags: impl Into<NextItemShortcutOptions>,
    ) {
        let flags = flags.into();
        unsafe { sys::igSetNextItemShortcut(key_chord.raw(), flags.raw()) }
    }

    /// Overrides `io.WantCaptureKeyboard` for the next frame.
    #[doc(alias = "SetNextFrameWantCaptureKeyboard")]
    pub fn set_next_frame_want_capture_keyboard(&self, want_capture_keyboard: bool) {
        unsafe { sys::igSetNextFrameWantCaptureKeyboard(want_capture_keyboard) }
    }

    /// Overrides `io.WantCaptureMouse` for the next frame.
    #[doc(alias = "SetNextFrameWantCaptureMouse")]
    pub fn set_next_frame_want_capture_mouse(&self, want_capture_mouse: bool) {
        unsafe { sys::igSetNextFrameWantCaptureMouse(want_capture_mouse) }
    }

    /// Check if a mouse button is being held down
    #[doc(alias = "IsMouseDown")]
    pub fn is_mouse_down(&self, button: MouseButton) -> bool {
        unsafe { sys::igIsMouseDown_Nil(button.into()) }
    }

    /// Check if a mouse button was clicked (went from !Down to Down)
    #[doc(alias = "IsMouseClicked")]
    pub fn is_mouse_clicked(&self, button: MouseButton) -> bool {
        unsafe { sys::igIsMouseClicked_Bool(button.into(), false) }
    }

    /// Check if a mouse button was clicked, with repeat
    #[doc(alias = "IsMouseClicked")]
    pub fn is_mouse_clicked_with_repeat(&self, button: MouseButton, repeat: bool) -> bool {
        unsafe { sys::igIsMouseClicked_Bool(button.into(), repeat) }
    }

    /// Check if a mouse button was released (went from Down to !Down)
    #[doc(alias = "IsMouseReleased")]
    pub fn is_mouse_released(&self, button: MouseButton) -> bool {
        unsafe { sys::igIsMouseReleased_Nil(button.into()) }
    }

    /// Check if a mouse button was double-clicked
    #[doc(alias = "IsMouseDoubleClicked")]
    pub fn is_mouse_double_clicked(&self, button: MouseButton) -> bool {
        unsafe { sys::igIsMouseDoubleClicked_Nil(button.into()) }
    }

    /// Returns `true` if the mouse position is valid (not NaN).
    ///
    /// This checks the current mouse position as known by Dear ImGui.
    #[doc(alias = "IsMousePosValid")]
    pub fn is_mouse_pos_valid(&self) -> bool {
        unsafe { sys::igIsMousePosValid(std::ptr::null()) }
    }

    /// Returns `true` if the provided mouse position is valid (not NaN).
    #[doc(alias = "IsMousePosValid")]
    pub fn is_mouse_pos_valid_at(&self, pos: [f32; 2]) -> bool {
        let v = sys::ImVec2_c {
            x: pos[0],
            y: pos[1],
        };
        unsafe { sys::igIsMousePosValid(&v as *const sys::ImVec2_c) }
    }

    /// Returns `true` if the mouse button was released and the given delay has passed.
    #[doc(alias = "IsMouseReleasedWithDelay")]
    pub fn is_mouse_released_with_delay(&self, button: MouseButton, delay: f32) -> bool {
        unsafe { sys::igIsMouseReleasedWithDelay(button.into(), delay) }
    }

    /// Get mouse position in screen coordinates
    #[doc(alias = "GetMousePos")]
    pub fn mouse_pos(&self) -> [f32; 2] {
        let pos = unsafe { sys::igGetMousePos() };
        [pos.x, pos.y]
    }

    /// Get mouse position when a specific button was clicked
    #[doc(alias = "GetMousePosOnOpeningCurrentPopup")]
    pub fn mouse_pos_on_opening_current_popup(&self) -> [f32; 2] {
        let pos = unsafe { sys::igGetMousePosOnOpeningCurrentPopup() };
        [pos.x, pos.y]
    }

    /// Check if mouse is hovering given rectangle
    #[doc(alias = "IsMouseHoveringRect")]
    pub fn is_mouse_hovering_rect(&self, r_min: [f32; 2], r_max: [f32; 2]) -> bool {
        unsafe {
            sys::igIsMouseHoveringRect(
                sys::ImVec2::new(r_min[0], r_min[1]),
                sys::ImVec2::new(r_max[0], r_max[1]),
                true,
            )
        }
    }

    /// Check if mouse is hovering given rectangle (with clipping test)
    #[doc(alias = "IsMouseHoveringRect")]
    pub fn is_mouse_hovering_rect_with_clip(
        &self,
        r_min: [f32; 2],
        r_max: [f32; 2],
        clip: bool,
    ) -> bool {
        unsafe {
            sys::igIsMouseHoveringRect(
                sys::ImVec2::new(r_min[0], r_min[1]),
                sys::ImVec2::new(r_max[0], r_max[1]),
                clip,
            )
        }
    }

    /// Check if mouse is dragging
    #[doc(alias = "IsMouseDragging")]
    pub fn is_mouse_dragging(&self, button: MouseButton) -> bool {
        unsafe { sys::igIsMouseDragging(button as i32, -1.0) }
    }

    /// Check if mouse is dragging with threshold
    #[doc(alias = "IsMouseDragging")]
    pub fn is_mouse_dragging_with_threshold(
        &self,
        button: MouseButton,
        lock_threshold: f32,
    ) -> bool {
        unsafe { sys::igIsMouseDragging(button as i32, lock_threshold) }
    }

    /// Get mouse drag delta
    #[doc(alias = "GetMouseDragDelta")]
    pub fn mouse_drag_delta(&self, button: MouseButton) -> [f32; 2] {
        let delta = unsafe { sys::igGetMouseDragDelta(button as i32, -1.0) };
        [delta.x, delta.y]
    }

    /// Get mouse drag delta with threshold
    #[doc(alias = "GetMouseDragDelta")]
    pub fn mouse_drag_delta_with_threshold(
        &self,
        button: MouseButton,
        lock_threshold: f32,
    ) -> [f32; 2] {
        let delta = unsafe { sys::igGetMouseDragDelta(button as i32, lock_threshold) };
        [delta.x, delta.y]
    }

    /// Reset mouse drag delta for a specific button
    #[doc(alias = "ResetMouseDragDelta")]
    pub fn reset_mouse_drag_delta(&self, button: MouseButton) {
        unsafe { sys::igResetMouseDragDelta(button as i32) }
    }

    /// Returns true if the last item toggled its selection state in a multi-select scope.
    ///
    /// This only makes sense when used between `BeginMultiSelect()` /
    /// `EndMultiSelect()` (or helpers built on top of them).
    #[doc(alias = "IsItemToggledSelection")]
    pub fn is_item_toggled_selection(&self) -> bool {
        unsafe { sys::igIsItemToggledSelection() }
    }
}