use super::source::{SortComparer, SortDirection};
use crate::*;
use std::cmp::Ordering;
use std::fmt;
use std::rc::Rc;
use waft_foundation::{App, ChangeNotifier, ChangeNotifierData, Handle, HandleId, Ref};
use waft_painting::{AlignmentGeometry, EdgeInsetsGeometry, TextOverflow};
use waft_widgets::*;
pub(crate) const DEFAULT_WIDTH: f64 = 120.0;
const DEFAULT_MIN_WIDTH: f64 = 20.0;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TableViewFrozenEdge {
#[default]
None,
Leading,
Trailing,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TableViewSortCycle {
#[default]
AscendingDescending,
AscendingDescendingNone,
DescendingAscending,
DescendingAscendingNone,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub(crate) enum ColumnChange {
#[default]
None,
Width,
Header,
HeaderToolTip,
FrozenEdge,
CanResize,
Visibility,
CanSort,
CustomSortComparer,
SortDirection,
Other,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct TableViewCellStyle {
pub(crate) padding: [f64; 4],
pub(crate) font_size: f64,
}
pub type TableViewCellTemplate<T> = Rc<dyn Fn(&mut App, BuildContext, &T) -> WidgetRef>;
enum ColumnCell<T> {
Text(Rc<dyn Fn(&T) -> String>),
Template(TableViewCellTemplate<T>),
}
pub struct TableViewColumn<T> {
notifier: ChangeNotifierData,
cell: ColumnCell<T>,
header: String,
header_tool_tip: Option<String>,
width: GridLength,
min_width: f64,
min_width_set: bool,
max_width: f64,
can_resize: bool,
actual_width: f64,
frozen_edge: TableViewFrozenEdge,
visible: bool,
can_sort: bool,
sort_cycle: TableViewSortCycle,
sort_key: Option<SortComparer<T>>,
custom_sort_comparer: Option<SortComparer<T>>,
sort_direction: SortDirection,
owner: Option<HandleId>,
change: ColumnChange,
}
impl<T: 'static> TableViewColumn<T> {
fn new(app: &mut App, header: String, cell: ColumnCell<T>) -> Ref<Self> {
app.create(Self {
notifier: ChangeNotifierData::new(),
cell,
header,
header_tool_tip: None,
width: GridLength::pixel(DEFAULT_WIDTH),
min_width: DEFAULT_MIN_WIDTH,
min_width_set: false,
max_width: f64::INFINITY,
can_resize: true,
actual_width: DEFAULT_WIDTH,
frozen_edge: TableViewFrozenEdge::None,
visible: true,
can_sort: true,
sort_cycle: TableViewSortCycle::AscendingDescending,
sort_key: None,
custom_sort_comparer: None,
sort_direction: SortDirection::None,
owner: None,
change: ColumnChange::None,
})
}
pub fn text(
app: &mut App,
header: impl Into<String>,
binding: impl Fn(&T) -> String + 'static,
) -> Ref<Self> {
Self::new(app, header.into(), ColumnCell::Text(Rc::new(binding)))
}
pub fn template(
app: &mut App,
header: impl Into<String>,
cell_template: impl Fn(&mut App, BuildContext, &T) -> WidgetRef + 'static,
) -> Ref<Self> {
Self::new(
app,
header.into(),
ColumnCell::Template(Rc::new(cell_template)),
)
}
pub fn dispose(self: Handle<Self>, app: &mut App) {
ChangeNotifierData::dispose(app, self);
}
pub fn header(self: Handle<Self>, app: &App) -> &str {
&app.get(self).header
}
pub fn set_header(self: Handle<Self>, app: &mut App, value: impl Into<String>) {
let value = value.into();
if app.get(self).header != value {
app.get_mut(self).header = value;
self.changed(app, ColumnChange::Header);
}
}
pub fn header_tool_tip(self: Handle<Self>, app: &App) -> Option<&str> {
app.get(self).header_tool_tip.as_deref()
}
pub fn set_header_tool_tip(self: Handle<Self>, app: &mut App, value: Option<String>) {
if app.get(self).header_tool_tip != value {
app.get_mut(self).header_tool_tip = value;
self.changed(app, ColumnChange::HeaderToolTip);
}
}
pub fn width(self: Handle<Self>, app: &App) -> GridLength {
app.get(self).width
}
pub fn set_width(self: Handle<Self>, app: &mut App, value: GridLength) {
if app.get(self).width != value {
app.get_mut(self).width = value;
self.width_changed(app);
}
}
pub fn min_width(self: Handle<Self>, app: &App) -> f64 {
app.get(self).min_width
}
pub fn set_min_width(self: Handle<Self>, app: &mut App, value: f64) {
let column = app.get_mut(self);
column.min_width_set = true;
if column.min_width != value {
column.min_width = value;
self.width_changed(app);
}
}
pub fn max_width(self: Handle<Self>, app: &App) -> f64 {
app.get(self).max_width
}
pub fn set_max_width(self: Handle<Self>, app: &mut App, value: f64) {
if app.get(self).max_width != value {
app.get_mut(self).max_width = value;
self.width_changed(app);
}
}
pub fn can_resize(self: Handle<Self>, app: &App) -> bool {
app.get(self).can_resize
}
pub fn set_can_resize(self: Handle<Self>, app: &mut App, value: bool) {
if app.get(self).can_resize != value {
app.get_mut(self).can_resize = value;
self.changed(app, ColumnChange::CanResize);
}
}
pub fn actual_width(self: Handle<Self>, app: &App) -> f64 {
app.get(self).actual_width
}
pub fn frozen_edge(self: Handle<Self>, app: &App) -> TableViewFrozenEdge {
app.get(self).frozen_edge
}
pub fn set_frozen_edge(self: Handle<Self>, app: &mut App, value: TableViewFrozenEdge) {
if app.get(self).frozen_edge != value {
app.get_mut(self).frozen_edge = value;
self.changed(app, ColumnChange::FrozenEdge);
}
}
pub fn visible(self: Handle<Self>, app: &App) -> bool {
app.get(self).visible
}
pub fn set_visible(self: Handle<Self>, app: &mut App, value: bool) {
if app.get(self).visible != value {
app.get_mut(self).visible = value;
self.changed(app, ColumnChange::Visibility);
}
}
pub fn can_sort(self: Handle<Self>, app: &App) -> bool {
app.get(self).can_sort
}
pub fn set_can_sort(self: Handle<Self>, app: &mut App, value: bool) {
if app.get(self).can_sort != value {
app.get_mut(self).can_sort = value;
self.changed(app, ColumnChange::CanSort);
}
}
pub fn sort_cycle(self: Handle<Self>, app: &App) -> TableViewSortCycle {
app.get(self).sort_cycle
}
pub fn set_sort_cycle(self: Handle<Self>, app: &mut App, value: TableViewSortCycle) {
app.get_mut(self).sort_cycle = value;
}
pub fn set_sort_key<K: Ord>(
self: Handle<Self>,
app: &mut App,
key: impl Fn(&T) -> K + 'static,
) {
app.get_mut(self).sort_key = Some(Rc::new(move |a, b| key(a).cmp(&key(b))));
self.changed(app, ColumnChange::Other);
}
pub fn set_custom_sort_comparer(
self: Handle<Self>,
app: &mut App,
comparer: impl Fn(&T, &T) -> Ordering + 'static,
) {
app.get_mut(self).custom_sort_comparer = Some(Rc::new(comparer));
self.changed(app, ColumnChange::CustomSortComparer);
}
pub fn sort_direction(self: Handle<Self>, app: &App) -> SortDirection {
app.get(self).sort_direction
}
}
impl<T: 'static> TableViewColumn<T> {
pub(crate) fn set_owner(self: Handle<Self>, app: &mut App, owner: Option<HandleId>) -> bool {
let column = app.get_mut(self);
if let (Some(existing), Some(new)) = (column.owner, owner)
&& existing != new
{
debug_assert!(
false,
"TableViewColumn re-owned without removing it from the previous TableView first"
);
return false;
}
column.owner = owner;
true
}
pub(crate) fn owner(self: Handle<Self>, app: &App) -> Option<HandleId> {
app.get(self).owner
}
pub(crate) fn change(self: Handle<Self>, app: &App) -> ColumnChange {
app.get(self).change
}
pub(crate) fn min_width_set(self: Handle<Self>, app: &App) -> bool {
app.get(self).min_width_set
}
pub(crate) fn set_actual_width(self: Handle<Self>, app: &mut App, width: f64) {
app.get_mut(self).actual_width = width;
}
pub(crate) fn set_sort_state(self: Handle<Self>, app: &mut App, direction: SortDirection) {
if app.get(self).sort_direction != direction {
app.get_mut(self).sort_direction = direction;
self.changed(app, ColumnChange::SortDirection);
}
}
pub(crate) fn sort_comparer(self: Handle<Self>, app: &App) -> Option<SortComparer<T>> {
let column = app.get(self);
if let Some(comparer) = &column.custom_sort_comparer {
return Some(comparer.clone());
}
if let Some(key) = &column.sort_key {
return Some(key.clone());
}
match &column.cell {
ColumnCell::Text(binding) => {
let binding = binding.clone();
Some(Rc::new(move |a, b| binding(a).cmp(&binding(b))))
}
ColumnCell::Template(_) => None,
}
}
pub(crate) fn is_sortable(self: Handle<Self>, app: &App) -> bool {
self.can_sort(app) && self.sort_comparer(app).is_some()
}
pub(crate) fn generate_element(
self: Handle<Self>,
app: &mut App,
context: BuildContext,
item: &T,
style: TableViewCellStyle,
) -> WidgetRef {
match &app.get(self).cell {
ColumnCell::Text(binding) => {
let text = binding(item);
let r = ThemeResources::of(app, context);
let [left, top, right, bottom] = style.padding;
Align::new()
.alignment(AlignmentGeometry::CENTER_LEFT)
.child(
Padding::new(EdgeInsetsGeometry::from_ltrb(left, top, right, bottom))
.child(
Text::new(text)
.style(
waft_painting::TextStyle::new()
.font_family(r.content_control_theme_font_family)
.font_size(style.font_size)
.font_weight(waft_embedder::FontWeight::NORMAL),
)
.max_lines(1)
.soft_wrap(false)
.overflow(TextOverflow::Ellipsis),
),
)
.into_widget()
}
ColumnCell::Template(template) => {
let template = template.clone();
template(app, context, item)
}
}
}
fn width_changed(self: Handle<Self>, app: &mut App) {
let column = app.get_mut(self);
let pixels = if column.width.unit == GridUnitType::Pixel {
column.width.value
} else {
DEFAULT_WIDTH
};
let lo = column.min_width;
let hi = lo.max(column.max_width);
column.actual_width = pixels.clamp(lo, hi);
self.changed(app, ColumnChange::Width);
}
fn changed(self: Handle<Self>, app: &mut App, change: ColumnChange) {
app.get_mut(self).change = change;
self.notify_listeners(app);
}
}
impl<T: 'static> ChangeNotifier for TableViewColumn<T> {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.notifier
}
}
impl<T> fmt::Debug for TableViewColumn<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TableViewColumn")
.field("header", &self.header)
.field("width", &self.width)
.field("actual_width", &self.actual_width)
.field("sort_direction", &self.sort_direction)
.finish_non_exhaustive()
}
}