From 76025f2c159dc19e977e5610763af6c3642024c7 Mon Sep 17 00:00:00 2001 From: Wybe Westra Date: Sat, 23 Dec 2023 15:58:48 +0100 Subject: [PATCH] Fix: `Grid` now follows `style.visuals.striped` setting if not explicitly overwritten (#3723) The docs of [`Visuals.striped`](https://docs.rs/egui/latest/egui/style/struct.Visuals.html#structfield.striped) mention that they control the default striping of Grid. However, this seems to be broken. This pr makes the Grid follow the striped setting if it doesn't yet have a row coloring set. --- crates/egui/src/grid.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/egui/src/grid.rs b/crates/egui/src/grid.rs index 1ecb319a9..374902e3a 100644 --- a/crates/egui/src/grid.rs +++ b/crates/egui/src/grid.rs @@ -341,14 +341,12 @@ impl Grid { /// Default is whatever is in [`crate::Visuals::striped`]. pub fn striped(self, striped: bool) -> Self { if striped { - self.with_row_color(move |row, style| { - if row % 2 == 1 { - return Some(style.visuals.faint_bg_color); - } - None - }) + self.with_row_color(striped_row_color) } else { - self + // Explicitly set the row color to nothing. + // Needed so that when the style.visuals.striped value is checked later on, + // it is clear that the user does not want stripes on this specific Grid. + self.with_row_color(|_row: usize, _style: &Style| None) } } @@ -410,11 +408,14 @@ impl Grid { max_cell_size, spacing, start_row, - color_picker, + mut color_picker, } = self; let min_col_width = min_col_width.unwrap_or_else(|| ui.spacing().interact_size.x); let min_row_height = min_row_height.unwrap_or_else(|| ui.spacing().interact_size.y); let spacing = spacing.unwrap_or_else(|| ui.spacing().item_spacing); + if color_picker.is_none() && ui.visuals().striped { + color_picker = Some(Box::new(striped_row_color)); + } let id = ui.make_persistent_id(id_source); let prev_state = State::load(ui.ctx(), id); @@ -454,3 +455,10 @@ impl Grid { }) } } + +fn striped_row_color(row: usize, style: &Style) -> Option { + if row % 2 == 1 { + return Some(style.visuals.faint_bg_color); + } + None +}