mirror of
https://github.com/emilk/egui.git
synced 2026-09-03 07:10:04 -04:00
Implement table row selection and hover highlighting (#3347)
* Based on #3105 by @vvv. ## Additions and Changes - Add `TableBuilder::sense()` and `StripBuilder::sense()` to enable detecting clicks or drags on table and strip cells. - Add `TableRow::select()` which takes a boolean that sets the highlight state for all cells added after a call to it. This allows highlighting an entire row or specific cells. - Add `TableRow::response()` which returns the union of the `Response` of all cells added to the row up to that point. This makes it easy to detect interactions with an entire row. See below for an alternative design. - Add `TableRow::index()` and `TableRow::col_index()` helpers. - Remove explicit `row_index` from callback passed to `TableBody::rows()` and `TableBody::heterogeneous_rows()`, possible due to the above. This is a breaking change but makes the callback compatible with `TableBody::row()`. - Update Table example to demonstrate all of the above. ## Design Decisions An alternative design to `TableRow::response()` would be to return the row response from `TableBody`s `row()`, `rows()` and `heterogeneous_rows()` functions. `row()` could just return the response. `rows()` and `heterogeneous_rows()` could return a tuple of the hovered row index and that rows response. I feel like this might be the cleaner soluction if only returning the hovered rows response isn't too limiting. I didn't implement `TableBuilder::select_rows()` as described [here](https://github.com/emilk/egui/pull/3105#issuecomment-1618062533) because it requires an immutable borrow of the selection state for the lifetime of the `TableBuilder`. This makes updating the selection state from within the body unnecessarily complicated. Additionally the current design allows for selecting specific cells, though that could be possible by modifying `TableBuilder::select_rows()` to provide row and column indices like below. ```rust pub fn select_cells(is_selected: impl Fn(usize, usize) -> bool) -> Self ``` ## Hover Highlighting EDIT: Thanks to @samitbasu we now have hover highlighting too. ~This is not implemented yet. Ideally we'd have an api that allows to choose between highlighting the hovered cell, column or row. Should cells containing interactive widgets, be highlighted when hovering over the widget or only when hovering over the cell itself? I'd like to implement that before this gets merged though.~ Feedback is more than welcome. I'd be happy to make any changes necessary to get this merged. * Closes #1519 * Closes #1553 * Closes #3069 --------- Co-authored-by: Samit Basu <basu.samit@gmail.com> Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
layout::{CellDirection, CellSize, StripLayout},
|
||||
layout::{CellDirection, CellSize, StripLayout, StripLayoutFlags},
|
||||
sizing::Sizing,
|
||||
Size,
|
||||
};
|
||||
@@ -46,6 +46,7 @@ pub struct StripBuilder<'a> {
|
||||
sizing: Sizing,
|
||||
clip: bool,
|
||||
cell_layout: egui::Layout,
|
||||
sense: egui::Sense,
|
||||
}
|
||||
|
||||
impl<'a> StripBuilder<'a> {
|
||||
@@ -55,8 +56,9 @@ impl<'a> StripBuilder<'a> {
|
||||
Self {
|
||||
ui,
|
||||
sizing: Default::default(),
|
||||
cell_layout,
|
||||
clip: false,
|
||||
cell_layout,
|
||||
sense: egui::Sense::hover(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +76,13 @@ impl<'a> StripBuilder<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// What should strip cells sense for? Default: [`egui::Sense::hover()`].
|
||||
#[inline]
|
||||
pub fn sense(mut self, sense: egui::Sense) -> Self {
|
||||
self.sense = sense;
|
||||
self
|
||||
}
|
||||
|
||||
/// Allocate space for one column/row.
|
||||
#[inline]
|
||||
pub fn size(mut self, size: Size) -> Self {
|
||||
@@ -102,7 +111,12 @@ impl<'a> StripBuilder<'a> {
|
||||
self.ui.available_rect_before_wrap().width(),
|
||||
self.ui.spacing().item_spacing.x,
|
||||
);
|
||||
let mut layout = StripLayout::new(self.ui, CellDirection::Horizontal, self.cell_layout);
|
||||
let mut layout = StripLayout::new(
|
||||
self.ui,
|
||||
CellDirection::Horizontal,
|
||||
self.cell_layout,
|
||||
self.sense,
|
||||
);
|
||||
strip(Strip {
|
||||
layout: &mut layout,
|
||||
direction: CellDirection::Horizontal,
|
||||
@@ -125,7 +139,12 @@ impl<'a> StripBuilder<'a> {
|
||||
self.ui.available_rect_before_wrap().height(),
|
||||
self.ui.spacing().item_spacing.y,
|
||||
);
|
||||
let mut layout = StripLayout::new(self.ui, CellDirection::Vertical, self.cell_layout);
|
||||
let mut layout = StripLayout::new(
|
||||
self.ui,
|
||||
CellDirection::Vertical,
|
||||
self.cell_layout,
|
||||
self.sense,
|
||||
);
|
||||
strip(Strip {
|
||||
layout: &mut layout,
|
||||
direction: CellDirection::Vertical,
|
||||
@@ -171,9 +190,11 @@ impl<'a, 'b> Strip<'a, 'b> {
|
||||
#[cfg_attr(debug_assertions, track_caller)]
|
||||
pub fn cell(&mut self, add_contents: impl FnOnce(&mut Ui)) {
|
||||
let (width, height) = self.next_cell_size();
|
||||
let striped = false;
|
||||
self.layout
|
||||
.add(self.clip, striped, width, height, add_contents);
|
||||
let flags = StripLayoutFlags {
|
||||
clip: self.clip,
|
||||
..Default::default()
|
||||
};
|
||||
self.layout.add(flags, width, height, add_contents);
|
||||
}
|
||||
|
||||
/// Add an empty cell.
|
||||
|
||||
Reference in New Issue
Block a user