Skip to content

Commit 108c543

Browse files
authored
Add row_ui to allow interacting with the whole row at once (#38)
This adds `TableDelegate::row_ui` to allow interacting or styling the whole row at once
1 parent 75cafd8 commit 108c543

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

demo/src/table_demo.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::BTreeMap;
22

3-
use egui::{Align2, Context, Id, Margin, NumExt as _, Sense, Vec2};
3+
use egui::{Align2, Context, Id, Margin, NumExt as _, Sense, Ui, Vec2};
44

55
#[derive(serde::Deserialize, serde::Serialize)]
66
pub struct TableDemo {
@@ -163,6 +163,18 @@ impl egui_table::TableDelegate for TableDemo {
163163
});
164164
}
165165

166+
// You can use row_ui to add some style or interaction to the entire row.
167+
fn row_ui(&mut self, ui: &mut Ui, _row_nr: u64) {
168+
if ui.rect_contains_pointer(ui.max_rect()) {
169+
ui.painter()
170+
.rect_filled(ui.max_rect(), 0.0, ui.visuals().code_bg_color);
171+
}
172+
173+
if ui.response().interact(Sense::click()).clicked() {
174+
// Handle row clicks
175+
}
176+
}
177+
166178
fn cell_ui(&mut self, ui: &mut egui::Ui, cell_info: &egui_table::CellInfo) {
167179
let egui_table::CellInfo { row_nr, col_nr, .. } = *cell_info;
168180

egui_table/src/main.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

egui_table/src/table.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ pub trait TableDelegate {
192192
/// The [`CellInfo::row_nr`] is which header row (usually 0).
193193
fn header_cell_ui(&mut self, ui: &mut Ui, cell: &HeaderCellInfo);
194194

195+
/// The contents of a row.
196+
///
197+
/// Individual cell [`Ui`]s will be children of the ui passed to this fn, so you can e.g. use
198+
/// [`Ui::style_mut`] to style the whole row.
199+
///
200+
/// This might be called multiple times per row (e.g. for sticky and non-sticky columns).
201+
fn row_ui(&mut self, _ui: &mut Ui, _row_nr: u64) {}
202+
195203
/// The contents of a cell in the table.
196204
///
197205
/// The [`CellInfo::row_nr`] is ignoring header rows.
@@ -678,6 +686,19 @@ impl TableSplitScrollDelegate<'_> {
678686
self.header_row_y.last() + self.get_row_top_offset(row_nr + 1),
679687
);
680688

689+
let row_x_range = self.col_x[0]..=self.col_x[self.col_x.len() - 1];
690+
let row_rect = Rect::from_x_y_ranges(row_x_range, y_range).translate(-offset);
691+
692+
let mut row_ui = ui.new_child(
693+
UiBuilder::new()
694+
.max_rect(row_rect)
695+
.id_salt(("row", row_nr))
696+
.layout(egui::Layout::left_to_right(egui::Align::Center)),
697+
);
698+
row_ui.set_min_size(row_rect.size());
699+
700+
self.table_delegate.row_ui(&mut row_ui, row_nr);
701+
681702
for col_nr in col_range.clone() {
682703
let column = &self.table.columns[col_nr];
683704
let mut cell_rect =
@@ -696,7 +717,7 @@ impl TableSplitScrollDelegate<'_> {
696717
if column.auto_size_this_frame {
697718
ui_builder = ui_builder.sizing_pass();
698719
}
699-
let mut cell_ui = ui.new_child(ui_builder);
720+
let mut cell_ui = row_ui.new_child(ui_builder);
700721
cell_ui.shrink_clip_rect(clip_rect);
701722

702723
self.table_delegate.cell_ui(

0 commit comments

Comments
 (0)