dear_imgui_sys/
lib.rs

1//! Low-level FFI bindings for Dear ImGui with docking support
2//!
3//! This crate provides raw, unsafe bindings to the Dear ImGui C++ library,
4//! specifically the docking branch which includes docking and multi-viewport features.
5//!
6//! ## Features
7//!
8//! - **docking**: Enable docking and multi-viewport features (default)
9//! - **freetype**: Enable FreeType font rasterizer support
10//! - **wasm**: Enable WebAssembly compatibility
11//!
12//! ## WebAssembly Support
13//!
14//! When the `wasm` feature is enabled, this crate provides full WASM compatibility:
15//! - Disables platform-specific functions (file I/O, shell functions, etc.)
16//! - Configures Dear ImGui for WASM environment
17//! - Compatible with wasm-bindgen and web targets
18//!
19//! ## Safety
20//!
21//! This crate provides raw FFI bindings and is inherently unsafe. Users should
22//! prefer the high-level `dear-imgui` crate for safe Rust bindings.
23//!
24//! ## Usage
25//!
26//! This crate is typically not used directly. Instead, use the `dear-imgui` crate
27//! which provides safe, idiomatic Rust bindings built on top of these FFI bindings.
28
29#![allow(non_upper_case_globals)]
30#![allow(non_camel_case_types)]
31#![allow(non_snake_case)]
32#![allow(dead_code)]
33#![allow(unnecessary_transmutes)]
34#![allow(clippy::all)]
35
36use std::ops::{Deref, DerefMut, Index, IndexMut};
37
38// Include the generated bindings from bindgen
39include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
40
41// Platform-specific function wrappers
42pub mod wrapper_functions;
43
44/// Implement indexing for ImVector to provide array-like access
45impl<T> Index<usize> for ImVector<T> {
46    type Output = T;
47
48    fn index(&self, index: usize) -> &Self::Output {
49        if index >= self.Size as usize {
50            panic!(
51                "ImVector index {} out of bounds (size: {})",
52                index, self.Size
53            );
54        }
55        unsafe { &*self.Data.add(index) }
56    }
57}
58
59/// Implement mutable indexing for ImVector
60impl<T> IndexMut<usize> for ImVector<T> {
61    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
62        if index >= self.Size as usize {
63            panic!(
64                "ImVector index {} out of bounds (size: {})",
65                index, self.Size
66            );
67        }
68        unsafe { &mut *self.Data.add(index) }
69    }
70}
71
72/// Implement Deref to allow ImVector to be used as a slice
73impl<T> Deref for ImVector<T> {
74    type Target = [T];
75
76    fn deref(&self) -> &[T] {
77        unsafe {
78            if self.Size == 0 || self.Data.is_null() {
79                // Handle empty vector or null data pointer
80                &[]
81            } else {
82                std::slice::from_raw_parts(self.Data, self.Size as usize)
83            }
84        }
85    }
86}
87
88/// Implement DerefMut to allow mutable slice access to ImVector
89impl<T> DerefMut for ImVector<T> {
90    fn deref_mut(&mut self) -> &mut [T] {
91        unsafe {
92            if self.Size == 0 || self.Data.is_null() {
93                // Handle empty vector or null data pointer
94                &mut []
95            } else {
96                std::slice::from_raw_parts_mut(self.Data, self.Size as usize)
97            }
98        }
99    }
100}
101
102/// Implement iterator support for ImVector references
103impl<'a, T> IntoIterator for &'a ImVector<T> {
104    type Item = &'a T;
105    type IntoIter = std::slice::Iter<'a, T>;
106
107    fn into_iter(self) -> Self::IntoIter {
108        self.deref().iter()
109    }
110}
111
112/// Implement mutable iterator support for ImVector references
113impl<'a, T> IntoIterator for &'a mut ImVector<T> {
114    type Item = &'a mut T;
115    type IntoIter = std::slice::IterMut<'a, T>;
116
117    fn into_iter(self) -> Self::IntoIter {
118        self.deref_mut().iter_mut()
119    }
120}
121
122// MSVC ABI compatibility for ImVec2-returning functions
123#[cfg(target_env = "msvc")]
124impl From<ImVec2_Pod> for ImVec2 {
125    #[inline]
126    fn from(pod: ImVec2_Pod) -> ImVec2 {
127        ImVec2 { x: pod.x, y: pod.y }
128    }
129}
130
131#[cfg(target_env = "msvc")]
132impl From<ImVec2> for ImVec2_Pod {
133    #[inline]
134    fn from(v: ImVec2) -> ImVec2_Pod {
135        ImVec2_Pod { x: v.x, y: v.y }
136    }
137}
138
139// Re-export commonly used types for convenience
140pub use ImColor as Color;
141pub use ImVec2 as Vector2;
142pub use ImVec4 as Vector4;
143
144/// Version information for the Dear ImGui library
145pub const IMGUI_VERSION: &str = env!("CARGO_PKG_VERSION");
146
147/// Check if docking features are available
148#[cfg(feature = "docking")]
149pub const HAS_DOCKING: bool = true;
150
151#[cfg(not(feature = "docking"))]
152pub const HAS_DOCKING: bool = false;
153
154/// Check if FreeType support is available
155#[cfg(feature = "freetype")]
156pub const HAS_FREETYPE: bool = true;
157
158#[cfg(not(feature = "freetype"))]
159pub const HAS_FREETYPE: bool = false;
160
161/// Check if WASM support is available
162#[cfg(feature = "wasm")]
163pub const HAS_WASM: bool = true;
164
165#[cfg(not(feature = "wasm"))]
166pub const HAS_WASM: bool = false;
167
168impl ImVec2 {
169    #[inline]
170    pub const fn new(x: f32, y: f32) -> ImVec2 {
171        ImVec2 { x, y }
172    }
173
174    #[inline]
175    pub const fn zero() -> ImVec2 {
176        ImVec2 { x: 0.0, y: 0.0 }
177    }
178}
179
180impl From<[f32; 2]> for ImVec2 {
181    #[inline]
182    fn from(array: [f32; 2]) -> ImVec2 {
183        ImVec2::new(array[0], array[1])
184    }
185}
186
187impl From<(f32, f32)> for ImVec2 {
188    #[inline]
189    fn from((x, y): (f32, f32)) -> ImVec2 {
190        ImVec2::new(x, y)
191    }
192}
193
194impl From<ImVec2> for [f32; 2] {
195    #[inline]
196    fn from(v: ImVec2) -> [f32; 2] {
197        [v.x, v.y]
198    }
199}
200
201impl From<ImVec2> for (f32, f32) {
202    #[inline]
203    fn from(v: ImVec2) -> (f32, f32) {
204        (v.x, v.y)
205    }
206}
207
208impl From<mint::Vector2<f32>> for ImVec2 {
209    #[inline]
210    fn from(v: mint::Vector2<f32>) -> ImVec2 {
211        ImVec2::new(v.x, v.y)
212    }
213}
214
215impl ImVec4 {
216    #[inline]
217    pub const fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 {
218        ImVec4 { x, y, z, w }
219    }
220
221    #[inline]
222    pub const fn zero() -> ImVec4 {
223        ImVec4 {
224            x: 0.0,
225            y: 0.0,
226            z: 0.0,
227            w: 0.0,
228        }
229    }
230}
231
232impl From<[f32; 4]> for ImVec4 {
233    #[inline]
234    fn from(array: [f32; 4]) -> ImVec4 {
235        ImVec4::new(array[0], array[1], array[2], array[3])
236    }
237}
238
239impl From<(f32, f32, f32, f32)> for ImVec4 {
240    #[inline]
241    fn from((x, y, z, w): (f32, f32, f32, f32)) -> ImVec4 {
242        ImVec4::new(x, y, z, w)
243    }
244}
245
246impl From<ImVec4> for [f32; 4] {
247    #[inline]
248    fn from(v: ImVec4) -> [f32; 4] {
249        [v.x, v.y, v.z, v.w]
250    }
251}
252
253impl From<ImVec4> for (f32, f32, f32, f32) {
254    #[inline]
255    fn from(v: ImVec4) -> (f32, f32, f32, f32) {
256        (v.x, v.y, v.z, v.w)
257    }
258}
259
260impl From<mint::Vector4<f32>> for ImVec4 {
261    #[inline]
262    fn from(v: mint::Vector4<f32>) -> ImVec4 {
263        ImVec4::new(v.x, v.y, v.z, v.w)
264    }
265}