Skip to content

Commit 91535fe

Browse files
committed
Add RowSelectionChanged action.
1 parent a02ee5f commit 91535fe

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎demos/shared/src/sparse/ui.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::BTreeSet;
12
use crate::sparse::{CellKind, CellKindChoice, SparseMapRenderer, SparseMapSource, generate_data};
23
use egui::{Response, Ui};
34
use egui_deferred_table::{Action, DeferredTable, apply_reordering};
@@ -70,6 +71,8 @@ struct UiState {
7071

7172
row_ordering_input: String,
7273
column_ordering_input: String,
74+
75+
row_selections: BTreeSet<usize>,
7376
}
7477

7578
pub fn show_table(ui: &mut Ui, state: &mut SparseTableState) -> (Response, Vec<Action>) {
@@ -121,6 +124,9 @@ pub fn handle_actions(actions: Vec<Action>, state: &mut SparseTableState) {
121124
state.ui_state.row_ordering_input =
122125
list_to_string(state.renderer.row_ordering.as_mut().unwrap());
123126
}
127+
Action::RowSelectionChanged { selection } => {
128+
state.ui_state.row_selections = selection;
129+
}
124130
}
125131
}
126132
}
@@ -229,6 +235,14 @@ pub fn show_controls(ui: &mut Ui, state: &mut SparseTableState) {
229235
);
230236
}
231237
});
238+
239+
ui.separator();
240+
241+
egui::Frame::group(ui.style()).show(ui, |ui| {
242+
ui.label("Row selections");
243+
ui.label(format!("{:?}", state.ui_state.row_selections));
244+
});
245+
232246
});
233247

234248
ui.horizontal(|ui| {

‎demos/shared/src/spreadsheet/ui.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl SpreadsheetState {
7979
.column_parameters(column_params)
8080
.row_parameters(row_params)
8181
.highlight_hovered_cell()
82+
.selectable_rows_disabled()
8283
.show_and_edit(ui, &mut self.data_source, &mut self.renderer, &mut self.editor, &mut self.edit_state)
8384
}
8485
}
@@ -123,6 +124,11 @@ pub fn handle_actions(actions: Vec<Action>, state: &mut SpreadsheetState) {
123124
state.data_source.move_row(from, to);
124125
state.value.take();
125126
}
127+
Action::RowSelectionChanged { selection } => {
128+
let _ = selection;
129+
// row selection currently disabled
130+
unreachable!()
131+
}
126132
}
127133
}
128134
}

‎src/lib.rs‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ impl<'a, DataSource> DeferredTable<'a, DataSource> {
234234
let minimum_resize_size = (style.interaction.resize_grab_radius_side * 2.0) + 2.0;
235235

236236
let mut clear_drag_state = false;
237+
let mut request_row_selection_changed_action = false;
237238

238239
// TODO allow these to be overridden
239240
let default_column_parameters = AxisParameters::default();
@@ -879,9 +880,12 @@ impl<'a, DataSource> DeferredTable<'a, DataSource> {
879880
if response.clicked() {
880881
match cell_kind {
881882
CellKind::RowHeader => {
882-
match row_was_selected {
883-
true => { temp_state.row_selections.remove(&mapped_row_index); },
884-
false => { temp_state.row_selections.insert(mapped_row_index); },
883+
if self.parameters.selectable_rows {
884+
match row_was_selected {
885+
true => { temp_state.row_selections.remove(&mapped_row_index); },
886+
false => { temp_state.row_selections.insert(mapped_row_index); },
887+
}
888+
request_row_selection_changed_action = true;
885889
}
886890
}
887891
// TODO selectable columns?
@@ -1169,6 +1173,19 @@ impl<'a, DataSource> DeferredTable<'a, DataSource> {
11691173
temp_state.drag_state = None;
11701174
}
11711175

1176+
if request_row_selection_changed_action {
1177+
let potentially_visible_selections = temp_state
1178+
.row_selections
1179+
.iter()
1180+
.filter(|&mapped_row_id| *mapped_row_id < dimensions.row_count)
1181+
.cloned()
1182+
.collect::<BTreeSet<_>>();
1183+
1184+
actions.push(Action::RowSelectionChanged {
1185+
selection: potentially_visible_selections,
1186+
});
1187+
}
1188+
11721189
let repaint = match drag_action.take() {
11731190
None => false,
11741191
Some(DragAction::SetWidth(index, new_width)) => {
@@ -1195,7 +1212,7 @@ impl<'a, DataSource> DeferredTable<'a, DataSource> {
11951212
opaque_faint_bg_color: Color32,
11961213
opaque_faint_selected_bg_color: Color32,
11971214
ui: &mut Ui,
1198-
mut row_counter: usize,
1215+
row_counter: usize,
11991216
row_was_selected: bool,
12001217
) -> Color32 {
12011218
if row_was_selected {
@@ -1400,6 +1417,11 @@ pub enum Action {
14001417
from: usize,
14011418
to: usize,
14021419
},
1420+
1421+
/// Generated when the user selected or deselects one or more rows.
1422+
RowSelectionChanged {
1423+
selection: BTreeSet<usize>,
1424+
},
14031425
}
14041426

14051427
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]

0 commit comments

Comments
 (0)