Skip to main content

dear_imgui_rs/columns/
index.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4/// Concrete zero-based legacy Columns API column index.
5///
6/// This represents a real column only. Dear ImGui's `-1` current-column sentinel
7/// is represented by [`OldColumnRef::Current`] and [`OldColumnOffsetRef::Current`].
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub struct OldColumnIndex(usize);
11
12impl OldColumnIndex {
13    /// The first legacy column.
14    pub const ZERO: Self = Self(0);
15
16    /// Create a legacy column index from a Rust `usize`.
17    #[inline]
18    pub const fn new(index: usize) -> Self {
19        Self(index)
20    }
21
22    /// Return the zero-based Rust index.
23    #[inline]
24    pub const fn get(self) -> usize {
25        self.0
26    }
27
28    #[inline]
29    pub(super) fn into_i32(self, caller: &str) -> i32 {
30        i32::try_from(self.0).unwrap_or_else(|_| {
31            panic!("{caller} column index exceeded Dear ImGui's i32 range");
32        })
33    }
34
35    #[inline]
36    pub(super) fn from_i32(raw: i32, caller: &str) -> Self {
37        assert!(raw >= 0, "{caller} returned a negative column index");
38        Self(usize::try_from(raw).expect("non-negative column index must fit usize"))
39    }
40}
41
42impl From<usize> for OldColumnIndex {
43    #[inline]
44    fn from(index: usize) -> Self {
45        Self::new(index)
46    }
47}
48
49impl From<OldColumnIndex> for usize {
50    #[inline]
51    fn from(index: OldColumnIndex) -> Self {
52        index.get()
53    }
54}
55
56impl PartialEq<usize> for OldColumnIndex {
57    #[inline]
58    fn eq(&self, other: &usize) -> bool {
59        self.get() == *other
60    }
61}
62
63impl PartialEq<OldColumnIndex> for usize {
64    #[inline]
65    fn eq(&self, other: &OldColumnIndex) -> bool {
66        *self == other.get()
67    }
68}
69
70/// Legacy column selector for APIs that accept Dear ImGui's current-column sentinel.
71#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
72#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
73pub enum OldColumnRef {
74    /// Use the current legacy column.
75    #[default]
76    Current,
77    /// Use a concrete legacy column index.
78    Index(OldColumnIndex),
79}
80
81impl OldColumnRef {
82    /// Current legacy column.
83    pub const CURRENT: Self = Self::Current;
84
85    /// Select a concrete legacy column.
86    #[inline]
87    pub const fn index(index: OldColumnIndex) -> Self {
88        Self::Index(index)
89    }
90}
91
92impl From<OldColumnIndex> for OldColumnRef {
93    #[inline]
94    fn from(index: OldColumnIndex) -> Self {
95        Self::Index(index)
96    }
97}
98
99impl From<usize> for OldColumnRef {
100    #[inline]
101    fn from(index: usize) -> Self {
102        Self::Index(OldColumnIndex::new(index))
103    }
104}
105
106/// Legacy column offset-line selector.
107///
108/// Offset APIs operate on column boundary lines. Concrete column indices select
109/// the start line for that column, while [`OldColumnOffsetRef::Trailing`] selects
110/// the right-most line after the final column.
111#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
112#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
113pub enum OldColumnOffsetRef {
114    /// Use the current legacy column boundary.
115    #[default]
116    Current,
117    /// Use the start boundary for a concrete legacy column.
118    Column(OldColumnIndex),
119    /// Use the right-most boundary after the final legacy column.
120    Trailing,
121}
122
123impl OldColumnOffsetRef {
124    /// Current legacy column boundary.
125    pub const CURRENT: Self = Self::Current;
126
127    /// Right-most boundary after the final legacy column.
128    pub const TRAILING: Self = Self::Trailing;
129
130    /// Select the start boundary for a concrete legacy column.
131    #[inline]
132    pub const fn column(index: OldColumnIndex) -> Self {
133        Self::Column(index)
134    }
135}
136
137impl From<OldColumnIndex> for OldColumnOffsetRef {
138    #[inline]
139    fn from(index: OldColumnIndex) -> Self {
140        Self::Column(index)
141    }
142}
143
144impl From<usize> for OldColumnOffsetRef {
145    #[inline]
146    fn from(index: usize) -> Self {
147        Self::Column(OldColumnIndex::new(index))
148    }
149}