mirror of
https://github.com/emilk/egui.git
synced 2026-06-27 23:13:13 -04:00
egui_extras: follow closure-based conventions for TableBody.heterogeneous_rows
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use egui::TextStyle;
|
||||
use egui_extras::{Size, StripBuilder, TableBuilder, TableRow, TableRowBuilder};
|
||||
use egui_extras::{Size, StripBuilder, TableBuilder, TableRow};
|
||||
|
||||
/// Shows off a table with dynamic layout
|
||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||
@@ -91,7 +91,7 @@ impl TableDemo {
|
||||
});
|
||||
});
|
||||
})
|
||||
.body(|body| {
|
||||
.body(|mut body| {
|
||||
if !self.heterogeneous_rows {
|
||||
body.rows(text_height, self.num_rows, |row_index, mut row| {
|
||||
row.col(|ui| {
|
||||
@@ -107,47 +107,27 @@ impl TableDemo {
|
||||
});
|
||||
});
|
||||
} else {
|
||||
let row_builder = DemoRowBuilder {
|
||||
row_count: self.num_rows,
|
||||
};
|
||||
body.heterogeneous_rows(row_builder);
|
||||
let rows = DemoRows::new(self.num_rows);
|
||||
body.heterogeneous_rows(rows, DemoRows::populate_row);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct DemoRowBuilder {
|
||||
row_count: usize,
|
||||
}
|
||||
|
||||
struct DemoRows {
|
||||
row_count: usize,
|
||||
current_row: usize,
|
||||
}
|
||||
|
||||
impl Iterator for DemoRows {
|
||||
type Item = f32;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_row < self.row_count {
|
||||
let thick = self.current_row % 6 == 0;
|
||||
self.current_row += 1;
|
||||
Some(if thick { 30.0 } else { 18.0 })
|
||||
} else {
|
||||
None
|
||||
impl DemoRows {
|
||||
fn new(row_count: usize) -> Self {
|
||||
Self {
|
||||
row_count,
|
||||
current_row: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TableRowBuilder for DemoRowBuilder {
|
||||
fn row_heights(&self, _: &Vec<f32>) -> Box<dyn Iterator<Item = f32>> {
|
||||
Box::new(DemoRows {
|
||||
row_count: self.row_count,
|
||||
current_row: 0,
|
||||
})
|
||||
}
|
||||
|
||||
fn populate_row(&self, index: usize, mut row: TableRow<'_, '_>) {
|
||||
fn populate_row(index: usize, mut row: TableRow<'_, '_>) {
|
||||
let thick = index % 6 == 0;
|
||||
row.col(|ui| {
|
||||
ui.centered_and_justified(|ui| {
|
||||
@@ -172,6 +152,20 @@ impl TableRowBuilder for DemoRowBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for DemoRows {
|
||||
type Item = f32;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_row < self.row_count {
|
||||
let thick = self.current_row % 6 == 0;
|
||||
self.current_row += 1;
|
||||
Some(if thick { 30.0 } else { 18.0 })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clock_emoji(row_index: usize) -> String {
|
||||
char::from_u32(0x1f550 + row_index as u32 % 24)
|
||||
.unwrap()
|
||||
|
||||
@@ -351,15 +351,16 @@ pub struct TableBody<'a> {
|
||||
end_y: f32,
|
||||
}
|
||||
|
||||
pub trait TableRowBuilder {
|
||||
fn row_heights(&self, widths: &Vec<f32>) -> Box<dyn Iterator<Item = f32> + '_>;
|
||||
fn populate_row(&self, index: usize, row: TableRow<'_, '_>);
|
||||
}
|
||||
|
||||
impl<'a> TableBody<'a> {
|
||||
pub fn heterogeneous_rows(mut self, builder: impl TableRowBuilder) {
|
||||
let mut heights = builder.row_heights(&self.widths);
|
||||
pub fn widths(&self) -> &Vec<f32> {
|
||||
&self.widths
|
||||
}
|
||||
|
||||
pub fn heterogeneous_rows(
|
||||
&mut self,
|
||||
mut heights: impl Iterator<Item = f32>,
|
||||
mut populate_row: impl FnMut(usize, TableRow<'_, '_>),
|
||||
) {
|
||||
let max_height = self.end_y - self.start_y;
|
||||
let delta = self.start_y - self.layout.current_y();
|
||||
|
||||
@@ -398,7 +399,7 @@ impl<'a> TableBody<'a> {
|
||||
height: height,
|
||||
};
|
||||
self.row_nr += 1;
|
||||
builder.populate_row(row_index, tr);
|
||||
populate_row(row_index, tr);
|
||||
row_index += 1;
|
||||
current_height += height;
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user