From 126be51ac318a7de7831e9d083c97efa566f7409 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 13 Dec 2022 21:02:39 +0100 Subject: [PATCH] Add Visuals::striped as global default for Grids and Tables --- crates/egui/src/grid.rs | 9 +++++---- crates/egui/src/style.rs | 10 ++++++++++ crates/egui_extras/src/table.rs | 14 ++++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/crates/egui/src/grid.rs b/crates/egui/src/grid.rs index 54344428..6be1ffa3 100644 --- a/crates/egui/src/grid.rs +++ b/crates/egui/src/grid.rs @@ -278,7 +278,7 @@ impl GridLayout { pub struct Grid { id_source: Id, num_columns: Option, - striped: bool, + striped: Option, min_col_width: Option, min_row_height: Option, max_cell_size: Vec2, @@ -292,7 +292,7 @@ impl Grid { Self { id_source: Id::new(id_source), num_columns: None, - striped: false, + striped: None, min_col_width: None, min_row_height: None, max_cell_size: Vec2::INFINITY, @@ -310,9 +310,9 @@ impl Grid { /// If `true`, add a subtle background color to every other row. /// /// This can make a table easier to read. - /// Default: `false`. + /// Default is whatever is in [`crate::Visuals::striped`]. pub fn striped(mut self, striped: bool) -> Self { - self.striped = striped; + self.striped = Some(striped); self } @@ -371,6 +371,7 @@ impl Grid { spacing, start_row, } = self; + let striped = striped.unwrap_or(ui.visuals().striped); 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); diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index 6623d720..963f1b90 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -492,6 +492,10 @@ pub struct Visuals { /// Show a background behind collapsing headers. pub collapsing_header_frame: bool, + + /// Wether or not Grids and Tables should be striped by default + /// (have alternating rows differently colored). + pub striped: bool, } impl Visuals { @@ -735,6 +739,8 @@ impl Visuals { clip_rect_margin: 3.0, // should be at least half the size of the widest frame stroke + max WidgetVisuals::expansion button_frame: true, collapsing_header_frame: false, + + striped: false, } } @@ -1259,6 +1265,8 @@ impl Visuals { clip_rect_margin, button_frame, collapsing_header_frame, + + striped, } = self; ui.collapsing("Background Colors", |ui| { @@ -1315,6 +1323,8 @@ impl Visuals { ui.checkbox(button_frame, "Button has a frame"); ui.checkbox(collapsing_header_frame, "Collapsing header has a frame"); + ui.checkbox(striped, "By default, add stripes to grids and tables?"); + ui.vertical_centered(|ui| reset_button(ui, self)); } } diff --git a/crates/egui_extras/src/table.rs b/crates/egui_extras/src/table.rs index ced9e696..875aed1f 100644 --- a/crates/egui_extras/src/table.rs +++ b/crates/egui_extras/src/table.rs @@ -218,7 +218,7 @@ impl Default for TableScrollOptions { pub struct TableBuilder<'a> { ui: &'a mut Ui, columns: Vec, - striped: bool, + striped: Option, resizable: bool, cell_layout: egui::Layout, scroll_options: TableScrollOptions, @@ -230,16 +230,18 @@ impl<'a> TableBuilder<'a> { Self { ui, columns: Default::default(), - striped: false, + striped: None, resizable: false, cell_layout, scroll_options: Default::default(), } } - /// Enable striped row background for improved readability (default: `false`) + /// Enable striped row background for improved readability. + /// + /// Default is whatever is in [`egui::Visuals::striped`]. pub fn striped(mut self, striped: bool) -> Self { - self.striped = striped; + self.striped = Some(striped); self } @@ -373,6 +375,8 @@ impl<'a> TableBuilder<'a> { scroll_options, } = self; + let striped = striped.unwrap_or(ui.visuals().striped); + let state_id = ui.id().with("__table_state"); let initial_widths = @@ -431,6 +435,8 @@ impl<'a> TableBuilder<'a> { scroll_options, } = self; + let striped = striped.unwrap_or(ui.visuals().striped); + let state_id = ui.id().with("__table_state"); let initial_widths =