Skip to main content

dear_implot_sys/
lib.rs

1//! Low-level FFI bindings for ImPlot via the cimplot C API
2//!
3//! This crate provides raw, unsafe bindings to the ImPlot library using the
4//! cimplot C API, designed to work together with `dear-imgui-sys` (which uses
5//! cimgui for Dear ImGui). This avoids C++ ABI issues and keeps builds
6//! consistent across platforms and toolchains.
7//!
8//! ## Features
9//!
10//! - Docking is part of the pinned Dear ImGui core; there is no `docking` feature.
11//! - **prebuilt**: Allow verified prebuilt archive download/extraction.
12//! - **build-from-source**: Force the native source route.
13//! - **bindgen**: Enable explicit binding regeneration with
14//!   `DEAR_IMGUI_RS_REGEN_BINDINGS=1`; ordinary builds use checked-in bindings.
15//! - **freetype**: Enable FreeType font rasterizer support
16//! - **wasm**: Enable WebAssembly compatibility
17//!
18//! ## Safety
19//!
20//! This crate provides raw FFI bindings and is inherently unsafe. Users should
21//! prefer the high-level `dear-implot` crate for safe Rust bindings.
22//!
23//! ## Usage
24//!
25//! This crate is typically not used directly. Instead, use the `dear-implot` crate
26//! which provides safe, idiomatic Rust bindings built on top of these FFI bindings.
27
28#![allow(non_upper_case_globals)]
29#![allow(non_camel_case_types)]
30#![allow(non_snake_case)]
31#![allow(dead_code)]
32#![allow(unnecessary_transmutes)]
33#![allow(unsafe_op_in_unsafe_fn)]
34#![allow(clippy::all)]
35#![allow(unpredictable_function_pointer_comparisons)]
36
37// Re-export ImGui types from dear-imgui-sys to ensure compatibility
38pub use dear_imgui_sys::{
39    ImDrawData, ImDrawList, ImFontAtlas, ImGuiCond, ImGuiContext, ImGuiDragDropFlags, ImGuiIO,
40    ImGuiMouseButton, ImGuiStyle, ImTextureID, ImVec2, ImVec4,
41};
42
43// Include the generated bindings from bindgen
44include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
45
46#[cfg(all(feature = "native-test-probe", not(target_arch = "wasm32")))]
47#[doc(hidden)]
48pub mod native_test_probe {
49    unsafe extern "C" {
50        pub fn dear_implot_rectangle_probe_plot(
51            plot_context: *mut super::ImPlotContext,
52            kind: std::ffi::c_int,
53            invert_x: bool,
54            invert_y: bool,
55            bounds: *mut f32,
56        ) -> std::ffi::c_int;
57        pub fn dear_implot_line_probe_plot(
58            plot_context: *mut super::ImPlotContext,
59            weight: f32,
60            pixel_density: f32,
61            anti_aliased: bool,
62            alpha: f32,
63            positions: *mut f32,
64            uvs: *mut f32,
65            expected_uvs: *mut f32,
66            expected_width: *mut f32,
67            colors: *mut u32,
68            expected_color: *mut u32,
69        ) -> std::ffi::c_int;
70    }
71}
72
73// TODO: Platform-specific function wrappers for MSVC ABI compatibility
74// #[cfg(target_os = "windows")]
75// pub mod wrapper_functions;
76
77// Convenience type aliases and implementations
78use std::ops::Range;
79
80impl From<Range<f64>> for ImPlotRange {
81    fn from(from: Range<f64>) -> Self {
82        ImPlotRange {
83            Min: from.start,
84            Max: from.end,
85        }
86    }
87}
88
89impl From<[f64; 2]> for ImPlotRange {
90    fn from(from: [f64; 2]) -> Self {
91        ImPlotRange {
92            Min: from[0],
93            Max: from[1],
94        }
95    }
96}
97
98impl From<(f64, f64)> for ImPlotRange {
99    fn from(from: (f64, f64)) -> Self {
100        ImPlotRange {
101            Min: from.0,
102            Max: from.1,
103        }
104    }
105}
106
107impl From<ImVec2> for ImPlotRange {
108    fn from(from: ImVec2) -> Self {
109        ImPlotRange {
110            Min: from.x as f64,
111            Max: from.y as f64,
112        }
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[test]
121    fn test_implot_range_conversions() {
122        let range1: ImPlotRange = (0.0..10.0).into();
123        assert_eq!(range1.Min, 0.0);
124        assert_eq!(range1.Max, 10.0);
125
126        let range2: ImPlotRange = [1.0, 5.0].into();
127        assert_eq!(range2.Min, 1.0);
128        assert_eq!(range2.Max, 5.0);
129
130        let range3: ImPlotRange = (2.0, 8.0).into();
131        assert_eq!(range3.Min, 2.0);
132        assert_eq!(range3.Max, 8.0);
133    }
134}