1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 06:40:06 -04:00

egui_extras: follow closure-based conventions for TableBody.heterogeneous_rows

This commit is contained in:
Wayne Warren
2022-04-03 11:18:13 -06:00
parent ab4930fde7
commit 44bd8c1cc4
2 changed files with 33 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
use egui::TextStyle; 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 /// Shows off a table with dynamic layout
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
@@ -91,7 +91,7 @@ impl TableDemo {
}); });
}); });
}) })
.body(|body| { .body(|mut body| {
if !self.heterogeneous_rows { if !self.heterogeneous_rows {
body.rows(text_height, self.num_rows, |row_index, mut row| { body.rows(text_height, self.num_rows, |row_index, mut row| {
row.col(|ui| { row.col(|ui| {
@@ -107,47 +107,27 @@ impl TableDemo {
}); });
}); });
} else { } else {
let row_builder = DemoRowBuilder { let rows = DemoRows::new(self.num_rows);
row_count: self.num_rows, body.heterogeneous_rows(rows, DemoRows::populate_row);
};
body.heterogeneous_rows(row_builder);
} }
}); });
} }
} }
struct DemoRowBuilder {
row_count: usize,
}
struct DemoRows { struct DemoRows {
row_count: usize, row_count: usize,
current_row: usize, current_row: usize,
} }
impl Iterator for DemoRows { impl DemoRows {
type Item = f32; fn new(row_count: usize) -> Self {
Self {
fn next(&mut self) -> Option<Self::Item> { row_count,
if self.current_row < self.row_count { current_row: 0,
let thick = self.current_row % 6 == 0;
self.current_row += 1;
Some(if thick { 30.0 } else { 18.0 })
} else {
None
} }
} }
}
impl TableRowBuilder for DemoRowBuilder { fn populate_row(index: usize, mut row: TableRow<'_, '_>) {
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<'_, '_>) {
let thick = index % 6 == 0; let thick = index % 6 == 0;
row.col(|ui| { row.col(|ui| {
ui.centered_and_justified(|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 { fn clock_emoji(row_index: usize) -> String {
char::from_u32(0x1f550 + row_index as u32 % 24) char::from_u32(0x1f550 + row_index as u32 % 24)
.unwrap() .unwrap()

View File

@@ -351,15 +351,16 @@ pub struct TableBody<'a> {
end_y: f32, 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> { impl<'a> TableBody<'a> {
pub fn heterogeneous_rows(mut self, builder: impl TableRowBuilder) { pub fn widths(&self) -> &Vec<f32> {
let mut heights = builder.row_heights(&self.widths); &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 max_height = self.end_y - self.start_y;
let delta = self.start_y - self.layout.current_y(); let delta = self.start_y - self.layout.current_y();
@@ -398,7 +399,7 @@ impl<'a> TableBody<'a> {
height: height, height: height,
}; };
self.row_nr += 1; self.row_nr += 1;
builder.populate_row(row_index, tr); populate_row(row_index, tr);
row_index += 1; row_index += 1;
current_height += height; current_height += height;
continue; continue;