use crate::draw::ImColor32;
use crate::internal::len_i32;
use crate::sys;
use crate::ui::Ui;
use crate::widget::table::{
TableBackgroundChannelToken, TableColumnChannelToken, TableColumnIndex, TableContextMenuTarget,
assert_current_table, assert_current_table_cell, assert_valid_table_column,
assert_valid_table_column_in,
};
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct TableHeaderData {
pub index: TableColumnIndex,
pub text_color: ImColor32,
pub bg_color0: ImColor32,
pub bg_color1: ImColor32,
}
impl TableHeaderData {
pub fn new(
index: impl Into<TableColumnIndex>,
text_color: ImColor32,
bg_color0: ImColor32,
bg_color1: ImColor32,
) -> Self {
Self {
index: index.into(),
text_color,
bg_color0,
bg_color1,
}
}
}
impl Ui {
#[doc(alias = "TableGetHeaderAngledMaxLabelWidth")]
pub fn table_get_header_angled_max_label_width(&self) -> f32 {
unsafe { sys::igTableGetHeaderAngledMaxLabelWidth() }
}
#[doc(alias = "TableAngledHeadersRow")]
pub fn table_angled_headers_row(&self) {
unsafe { sys::igTableAngledHeadersRow() }
}
pub fn table_angled_headers_row_ex_with_data(
&self,
row_id: u32,
angle: f32,
max_label_width: f32,
headers: &[TableHeaderData],
) {
if headers.is_empty() {
unsafe { sys::igTableAngledHeadersRow() }
return;
}
let count = len_i32(
"Ui::table_angled_headers_row_ex_with_data()",
"headers",
headers.len(),
);
let table = assert_current_table("Ui::table_angled_headers_row_ex_with_data()");
let mut data: Vec<sys::ImGuiTableHeaderData> = Vec::with_capacity(headers.len());
for h in headers {
assert_valid_table_column_in(
table,
h.index,
"Ui::table_angled_headers_row_ex_with_data()",
);
data.push(sys::ImGuiTableHeaderData {
Index: h
.index
.into_imgui_column_idx("Ui::table_angled_headers_row_ex_with_data()"),
TextColor: u32::from(h.text_color),
BgColor0: u32::from(h.bg_color0),
BgColor1: u32::from(h.bg_color1),
});
}
unsafe {
sys::igTableAngledHeadersRowEx(row_id, angle, max_label_width, data.as_ptr(), count);
}
}
#[doc(alias = "TablePushBackgroundChannel")]
pub fn table_push_background_channel(&self) {
assert_current_table_cell("Ui::table_push_background_channel()");
unsafe { sys::igTablePushBackgroundChannel() }
}
#[doc(alias = "TablePopBackgroundChannel")]
pub fn table_pop_background_channel(&self) {
assert_current_table_cell("Ui::table_pop_background_channel()");
unsafe { sys::igTablePopBackgroundChannel() }
}
#[doc(alias = "TablePushColumnChannel")]
pub fn table_push_column_channel(&self, column: impl Into<TableColumnIndex>) {
let column_n = assert_valid_table_column(column.into(), "Ui::table_push_column_channel()");
unsafe { sys::igTablePushColumnChannel(column_n) }
}
#[doc(alias = "TablePopColumnChannel")]
pub fn table_pop_column_channel(&self) {
assert_current_table_cell("Ui::table_pop_column_channel()");
unsafe { sys::igTablePopColumnChannel() }
}
#[must_use = "dropping the token pops the table background draw channel immediately"]
#[doc(alias = "TablePushBackgroundChannel")]
pub fn table_background_channel(&self) -> TableBackgroundChannelToken<'_> {
self.table_push_background_channel();
TableBackgroundChannelToken::new(self)
}
#[must_use = "dropping the token pops the table column draw channel immediately"]
#[doc(alias = "TablePushColumnChannel")]
pub fn table_column_channel(
&self,
column: impl Into<TableColumnIndex>,
) -> TableColumnChannelToken<'_> {
self.table_push_column_channel(column);
TableColumnChannelToken::new(self)
}
pub fn with_table_background_channel<R>(&self, f: impl FnOnce() -> R) -> R {
let _token = self.table_background_channel();
f()
}
pub fn with_table_column_channel<R>(
&self,
column: impl Into<TableColumnIndex>,
f: impl FnOnce() -> R,
) -> R {
let _token = self.table_column_channel(column);
f()
}
#[doc(alias = "TableOpenContextMenu")]
pub fn table_open_context_menu(&self, target: impl Into<TableContextMenuTarget>) {
let target = target.into();
let column_n = match target {
TableContextMenuTarget::CurrentColumn => -1,
TableContextMenuTarget::Column(index) => {
index.into_i32("Ui::table_open_context_menu()")
}
TableContextMenuTarget::Table => {
let table = assert_current_table("Ui::table_open_context_menu()");
unsafe { (*table).ColumnsCount }
}
};
unsafe { sys::igTableOpenContextMenu(column_n) }
}
}