agg_gui/widgets/table/
config.rs1use std::sync::Arc;
7
8use crate::draw_ctx::DrawCtx;
9use crate::geometry::Rect;
10use crate::text::Font;
11use crate::theme::Visuals;
12
13#[derive(Clone, Copy, Debug)]
17pub enum ColumnSize {
18 Auto(f64),
19 Exact(f64),
20 Remainder { at_least: f64, clip: bool },
21}
22
23#[derive(Clone, Copy, Debug)]
24pub struct TableColumn {
25 pub size: ColumnSize,
26 pub resizable: bool,
27}
28
29impl TableColumn {
30 pub fn auto(initial: f64) -> Self {
31 Self {
32 size: ColumnSize::Auto(initial),
33 resizable: false,
34 }
35 }
36 pub fn exact(w: f64) -> Self {
37 Self {
38 size: ColumnSize::Exact(w),
39 resizable: false,
40 }
41 }
42 pub fn remainder() -> Self {
43 Self {
44 size: ColumnSize::Remainder {
45 at_least: 16.0,
46 clip: false,
47 },
48 resizable: false,
49 }
50 }
51 pub fn at_least(mut self, v: f64) -> Self {
52 if let ColumnSize::Remainder { ref mut at_least, .. } = self.size {
53 *at_least = v;
54 }
55 self
56 }
57 pub fn clip(mut self, on: bool) -> Self {
58 if let ColumnSize::Remainder { ref mut clip, .. } = self.size {
59 *clip = on;
60 }
61 self
62 }
63 pub fn resizable(mut self, on: bool) -> Self {
64 self.resizable = on;
65 self
66 }
67}
68
69pub const MIN_COL_W: f64 = 16.0;
71pub const RESIZE_HIT_HALF: f64 = 4.0;
73
74pub fn distribute_widths(
81 columns: &[TableColumn],
82 total_w: f64,
83 overrides: &[Option<f64>],
84) -> Vec<f64> {
85 let n = columns.len();
86 let mut out = vec![0.0_f64; n];
87 let mut remainder_indices: Vec<usize> = Vec::new();
88 let mut remainder_min = 0.0_f64;
89 let mut fixed_total = 0.0_f64;
90 for (i, c) in columns.iter().enumerate() {
91 if let Some(w) = overrides.get(i).copied().flatten() {
92 out[i] = w.max(MIN_COL_W);
93 fixed_total += out[i];
94 continue;
95 }
96 match c.size {
97 ColumnSize::Auto(w) | ColumnSize::Exact(w) => {
98 out[i] = w;
99 fixed_total += w;
100 }
101 ColumnSize::Remainder { at_least, .. } => {
102 remainder_indices.push(i);
103 remainder_min += at_least;
104 }
105 }
106 }
107 let leftover = (total_w - fixed_total).max(remainder_min);
108 if !remainder_indices.is_empty() {
109 let each = leftover / remainder_indices.len() as f64;
110 for &i in &remainder_indices {
111 let at_least = match columns[i].size {
112 ColumnSize::Remainder { at_least, .. } => at_least,
113 _ => 0.0,
114 };
115 out[i] = each.max(at_least);
116 }
117 }
118 out
119}
120
121#[derive(Clone, Debug)]
123pub enum TableRows {
124 Homogeneous { count: usize, height: f64 },
125 Heterogeneous { heights: Vec<f64> },
126}
127
128impl TableRows {
129 pub fn count(&self) -> usize {
130 match self {
131 TableRows::Homogeneous { count, .. } => *count,
132 TableRows::Heterogeneous { heights } => heights.len(),
133 }
134 }
135 pub fn height_at(&self, i: usize) -> f64 {
136 match self {
137 TableRows::Homogeneous { height, .. } => *height,
138 TableRows::Heterogeneous { heights } => heights.get(i).copied().unwrap_or(0.0),
139 }
140 }
141 pub fn total_height(&self) -> f64 {
142 match self {
143 TableRows::Homogeneous { count, height } => *count as f64 * *height,
144 TableRows::Heterogeneous { heights } => heights.iter().copied().sum(),
145 }
146 }
147 pub fn top_down_y_at(&self, i: usize) -> f64 {
148 match self {
149 TableRows::Homogeneous { height, .. } => i as f64 * *height,
150 TableRows::Heterogeneous { heights } => {
151 let take = i.min(heights.len());
152 heights[..take].iter().copied().sum()
153 }
154 }
155 }
156}
157
158pub struct CellInfo<'a> {
161 pub row: usize,
162 pub col: usize,
163 pub rect: Rect,
166 pub selected: bool,
167 pub visuals: &'a Visuals,
168 pub font: &'a Arc<Font>,
169}
170
171pub struct HeaderInfo<'a> {
172 pub col: usize,
173 pub rect: Rect,
174 pub visuals: &'a Visuals,
175 pub font: &'a Arc<Font>,
176}
177
178pub type CellPainter = Box<dyn FnMut(&CellInfo, &mut dyn DrawCtx)>;
179pub type HeaderPainter = Box<dyn FnMut(&HeaderInfo, &mut dyn DrawCtx)>;
180pub type HeaderClick = Box<dyn FnMut(usize, f64, f64) -> crate::event::EventResult>;
183pub type RowPredicate = Box<dyn Fn(usize) -> bool>;
185pub type RowsProvider = Box<dyn Fn() -> TableRows>;