feat: simple grid row color picker (#2519)
* feat: simple grid row color picker * feat: allow painting initial grid row * avoid coloring callback for striped when not needed Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * fix: switch from rgba to color32 for row grid color assignment * fix warning * make `paint_row` private --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
parent
92c4e23aac
commit
9ddf7abeee
|
|
@ -46,6 +46,9 @@ impl State {
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// type alias for boxed function to determine row color during grid generation
|
||||||
|
type ColorPickerFn = Box<dyn Fn(usize, &Style) -> Option<Color32>>;
|
||||||
|
|
||||||
pub(crate) struct GridLayout {
|
pub(crate) struct GridLayout {
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
style: std::sync::Arc<Style>,
|
style: std::sync::Arc<Style>,
|
||||||
|
|
@ -66,7 +69,7 @@ pub(crate) struct GridLayout {
|
||||||
spacing: Vec2,
|
spacing: Vec2,
|
||||||
min_cell_size: Vec2,
|
min_cell_size: Vec2,
|
||||||
max_cell_size: Vec2,
|
max_cell_size: Vec2,
|
||||||
striped: bool,
|
color_picker: Option<ColorPickerFn>,
|
||||||
|
|
||||||
// Cursor:
|
// Cursor:
|
||||||
col: usize,
|
col: usize,
|
||||||
|
|
@ -101,7 +104,7 @@ impl GridLayout {
|
||||||
spacing: ui.spacing().item_spacing,
|
spacing: ui.spacing().item_spacing,
|
||||||
min_cell_size: ui.spacing().interact_size,
|
min_cell_size: ui.spacing().interact_size,
|
||||||
max_cell_size: Vec2::INFINITY,
|
max_cell_size: Vec2::INFINITY,
|
||||||
striped: false,
|
color_picker: None,
|
||||||
|
|
||||||
col: 0,
|
col: 0,
|
||||||
row: 0,
|
row: 0,
|
||||||
|
|
@ -214,6 +217,20 @@ impl GridLayout {
|
||||||
self.col += 1;
|
self.col += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn paint_row(&mut self, cursor: &mut Rect, painter: &Painter) {
|
||||||
|
// handle row color painting based on color-picker function
|
||||||
|
let Some(color_picker) = self.color_picker.as_ref() else { return };
|
||||||
|
let Some(row_color) = color_picker(self.row, &self.style) else { return };
|
||||||
|
let Some(height) = self.prev_state.row_height(self.row) else {return };
|
||||||
|
// Paint background for coming row:
|
||||||
|
let size = Vec2::new(self.prev_state.full_width(self.spacing.x), height);
|
||||||
|
let rect = Rect::from_min_size(cursor.min, size);
|
||||||
|
let rect = rect.expand2(0.5 * self.spacing.y * Vec2::Y);
|
||||||
|
let rect = rect.expand2(2.0 * Vec2::X); // HACK: just looks better with some spacing on the sides
|
||||||
|
|
||||||
|
painter.rect_filled(rect, 2.0, row_color);
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn end_row(&mut self, cursor: &mut Rect, painter: &Painter) {
|
pub(crate) fn end_row(&mut self, cursor: &mut Rect, painter: &Painter) {
|
||||||
cursor.min.x = self.initial_available.min.x;
|
cursor.min.x = self.initial_available.min.x;
|
||||||
cursor.min.y += self.spacing.y;
|
cursor.min.y += self.spacing.y;
|
||||||
|
|
@ -225,17 +242,7 @@ impl GridLayout {
|
||||||
self.col = 0;
|
self.col = 0;
|
||||||
self.row += 1;
|
self.row += 1;
|
||||||
|
|
||||||
if self.striped && self.row % 2 == 1 {
|
self.paint_row(cursor, painter);
|
||||||
if let Some(height) = self.prev_state.row_height(self.row) {
|
|
||||||
// Paint background for coming row:
|
|
||||||
let size = Vec2::new(self.prev_state.full_width(self.spacing.x), height);
|
|
||||||
let rect = Rect::from_min_size(cursor.min, size);
|
|
||||||
let rect = rect.expand2(0.5 * self.spacing.y * Vec2::Y);
|
|
||||||
let rect = rect.expand2(2.0 * Vec2::X); // HACK: just looks better with some spacing on the sides
|
|
||||||
|
|
||||||
painter.rect_filled(rect, 2.0, self.style.visuals.faint_bg_color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn save(&self) {
|
pub(crate) fn save(&self) {
|
||||||
|
|
@ -278,12 +285,12 @@ impl GridLayout {
|
||||||
pub struct Grid {
|
pub struct Grid {
|
||||||
id_source: Id,
|
id_source: Id,
|
||||||
num_columns: Option<usize>,
|
num_columns: Option<usize>,
|
||||||
striped: Option<bool>,
|
|
||||||
min_col_width: Option<f32>,
|
min_col_width: Option<f32>,
|
||||||
min_row_height: Option<f32>,
|
min_row_height: Option<f32>,
|
||||||
max_cell_size: Vec2,
|
max_cell_size: Vec2,
|
||||||
spacing: Option<Vec2>,
|
spacing: Option<Vec2>,
|
||||||
start_row: usize,
|
start_row: usize,
|
||||||
|
color_picker: Option<ColorPickerFn>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Grid {
|
impl Grid {
|
||||||
|
|
@ -292,15 +299,24 @@ impl Grid {
|
||||||
Self {
|
Self {
|
||||||
id_source: Id::new(id_source),
|
id_source: Id::new(id_source),
|
||||||
num_columns: None,
|
num_columns: None,
|
||||||
striped: None,
|
|
||||||
min_col_width: None,
|
min_col_width: None,
|
||||||
min_row_height: None,
|
min_row_height: None,
|
||||||
max_cell_size: Vec2::INFINITY,
|
max_cell_size: Vec2::INFINITY,
|
||||||
spacing: None,
|
spacing: None,
|
||||||
start_row: 0,
|
start_row: 0,
|
||||||
|
color_picker: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Setting this will allow for dynamic coloring of rows of the grid object
|
||||||
|
pub fn with_row_color<F>(mut self, color_picker: F) -> Self
|
||||||
|
where
|
||||||
|
F: Fn(usize, &Style) -> Option<Color32> + 'static,
|
||||||
|
{
|
||||||
|
self.color_picker = Some(Box::new(color_picker));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Setting this will allow the last column to expand to take up the rest of the space of the parent [`Ui`].
|
/// Setting this will allow the last column to expand to take up the rest of the space of the parent [`Ui`].
|
||||||
pub fn num_columns(mut self, num_columns: usize) -> Self {
|
pub fn num_columns(mut self, num_columns: usize) -> Self {
|
||||||
self.num_columns = Some(num_columns);
|
self.num_columns = Some(num_columns);
|
||||||
|
|
@ -311,9 +327,17 @@ impl Grid {
|
||||||
///
|
///
|
||||||
/// This can make a table easier to read.
|
/// This can make a table easier to read.
|
||||||
/// Default is whatever is in [`crate::Visuals::striped`].
|
/// Default is whatever is in [`crate::Visuals::striped`].
|
||||||
pub fn striped(mut self, striped: bool) -> Self {
|
pub fn striped(self, striped: bool) -> Self {
|
||||||
self.striped = Some(striped);
|
if striped {
|
||||||
self
|
self.with_row_color(move |row, style| {
|
||||||
|
if row % 2 == 1 {
|
||||||
|
return Some(style.visuals.faint_bg_color);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set minimum width of each column.
|
/// Set minimum width of each column.
|
||||||
|
|
@ -364,14 +388,13 @@ impl Grid {
|
||||||
let Self {
|
let Self {
|
||||||
id_source,
|
id_source,
|
||||||
num_columns,
|
num_columns,
|
||||||
striped,
|
|
||||||
min_col_width,
|
min_col_width,
|
||||||
min_row_height,
|
min_row_height,
|
||||||
max_cell_size,
|
max_cell_size,
|
||||||
spacing,
|
spacing,
|
||||||
start_row,
|
start_row,
|
||||||
|
color_picker,
|
||||||
} = self;
|
} = 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_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 min_row_height = min_row_height.unwrap_or_else(|| ui.spacing().interact_size.y);
|
||||||
let spacing = spacing.unwrap_or_else(|| ui.spacing().item_spacing);
|
let spacing = spacing.unwrap_or_else(|| ui.spacing().item_spacing);
|
||||||
|
|
@ -387,9 +410,10 @@ impl Grid {
|
||||||
ui.allocate_ui_at_rect(max_rect, |ui| {
|
ui.allocate_ui_at_rect(max_rect, |ui| {
|
||||||
ui.set_visible(prev_state.is_some()); // Avoid visible first-frame jitter
|
ui.set_visible(prev_state.is_some()); // Avoid visible first-frame jitter
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
let grid = GridLayout {
|
let is_color = color_picker.is_some();
|
||||||
|
let mut grid = GridLayout {
|
||||||
num_columns,
|
num_columns,
|
||||||
striped,
|
color_picker,
|
||||||
min_cell_size: vec2(min_col_width, min_row_height),
|
min_cell_size: vec2(min_col_width, min_row_height),
|
||||||
max_cell_size,
|
max_cell_size,
|
||||||
spacing,
|
spacing,
|
||||||
|
|
@ -397,6 +421,13 @@ impl Grid {
|
||||||
..GridLayout::new(ui, id, prev_state)
|
..GridLayout::new(ui, id, prev_state)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// paint first incoming row
|
||||||
|
if is_color {
|
||||||
|
let mut cursor = ui.cursor();
|
||||||
|
let painter = ui.painter();
|
||||||
|
grid.paint_row(&mut cursor, painter);
|
||||||
|
}
|
||||||
|
|
||||||
ui.set_grid(grid);
|
ui.set_grid(grid);
|
||||||
let r = add_contents(ui);
|
let r = add_contents(ui);
|
||||||
ui.save_grid();
|
ui.save_grid();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue