Skip to content

Commit

Permalink
Fix: Grid now follows style.visuals.striped setting if not explic…
Browse files Browse the repository at this point in the history
…itly 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.
  • Loading branch information
Wcubed authored Dec 23, 2023
1 parent 365a8d2 commit 76025f2
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions crates/egui/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -454,3 +455,10 @@ impl Grid {
})
}
}

fn striped_row_color(row: usize, style: &Style) -> Option<Color32> {
if row % 2 == 1 {
return Some(style.visuals.faint_bg_color);
}
None
}

0 comments on commit 76025f2

Please sign in to comment.