Browse Source

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.
pull/3733/head
Wybe Westra 11 months ago
committed by GitHub
parent
commit
76025f2c15
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      crates/egui/src/grid.rs

24
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<Color32> {
if row % 2 == 1 {
return Some(style.visuals.faint_bg_color);
}
None
}

Loading…
Cancel
Save