use crate::ui::Ui;
use crate::widget::table::{TableColumnIndex, optional_user_id_from_raw};
use crate::{Id, sys};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SortDirection {
None = sys::ImGuiSortDirection_None as u8,
Ascending = sys::ImGuiSortDirection_Ascending as u8,
Descending = sys::ImGuiSortDirection_Descending as u8,
}
impl From<SortDirection> for sys::ImGuiSortDirection {
#[inline]
fn from(value: SortDirection) -> sys::ImGuiSortDirection {
match value {
SortDirection::None => sys::ImGuiSortDirection_None,
SortDirection::Ascending => sys::ImGuiSortDirection_Ascending,
SortDirection::Descending => sys::ImGuiSortDirection_Descending,
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct TableColumnSortSpec {
pub column_user_id: Option<Id>,
pub column_index: TableColumnIndex,
pub sort_order: i16,
pub sort_direction: SortDirection,
}
pub struct TableSortSpecs<'a> {
raw: *mut sys::ImGuiTableSortSpecs,
_marker: std::marker::PhantomData<&'a Ui>,
}
impl<'a> TableSortSpecs<'a> {
pub(crate) unsafe fn from_raw(raw: *mut sys::ImGuiTableSortSpecs) -> Self {
Self {
raw,
_marker: std::marker::PhantomData,
}
}
pub fn is_dirty(&self) -> bool {
unsafe { (*self.raw).SpecsDirty }
}
pub fn clear_dirty(&mut self) {
unsafe { (*self.raw).SpecsDirty = false }
}
pub fn len(&self) -> usize {
unsafe { (*self.raw).SpecsCount as usize }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&self) -> TableSortSpecsIter<'_> {
TableSortSpecsIter {
specs: self,
index: 0,
}
}
}
pub struct TableSortSpecsIter<'a> {
specs: &'a TableSortSpecs<'a>,
index: usize,
}
impl<'a> Iterator for TableSortSpecsIter<'a> {
type Item = TableColumnSortSpec;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.specs.len() {
return None;
}
unsafe {
let ptr = (*self.specs.raw).Specs;
if ptr.is_null() {
return None;
}
let spec = &*ptr.add(self.index);
self.index += 1;
let d = spec.SortDirection as u8;
let dir = if d == sys::ImGuiSortDirection_None as u8 {
SortDirection::None
} else if d == sys::ImGuiSortDirection_Ascending as u8 {
SortDirection::Ascending
} else if d == sys::ImGuiSortDirection_Descending as u8 {
SortDirection::Descending
} else {
SortDirection::None
};
Some(TableColumnSortSpec {
column_user_id: optional_user_id_from_raw(spec.ColumnUserID),
column_index: TableColumnIndex::from_imgui_column_idx(
spec.ColumnIndex,
"TableSortSpecsIter::next()",
),
sort_order: spec.SortOrder,
sort_direction: dir,
})
}
}
}