egui_data_table/draw.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
use std::mem::{replace, take};
use egui::{
Align, Color32, Event, Layout, PointerButton, Rect, Response, RichText, Sense, Stroke, Widget,
};
use egui_extras::Column;
use tap::prelude::{Pipe, Tap};
use crate::{
viewer::{EmptyRowCreateContext, RowViewer, TrivialConfig},
DataTable, UiAction,
};
use self::state::*;
use format as f;
pub(crate) mod state;
mod tsv;
/* ------------------------------------------ Rendering ----------------------------------------- */
pub struct Renderer<'a, R, V: RowViewer<R>> {
table: &'a mut DataTable<R>,
viewer: &'a mut V,
state: Option<Box<UiState<R>>>,
config: TrivialConfig,
}
impl<'a, R, V: RowViewer<R>> egui::Widget for Renderer<'a, R, V> {
fn ui(self, ui: &mut egui::Ui) -> Response {
self.show(ui)
}
}
impl<'a, R, V: RowViewer<R>> Renderer<'a, R, V> {
pub fn new(table: &'a mut DataTable<R>, viewer: &'a mut V) -> Self {
Self {
state: Some(table.ui.take().unwrap_or_default().tap_mut(|x| {
if table.rows.is_empty() {
table
.rows
.push(viewer.new_empty_row_for(EmptyRowCreateContext::InsertNewLine));
x.force_mark_dirty();
}
x.validate_identity(viewer);
})),
table,
config: viewer.trivial_config(),
viewer,
}
}
pub fn with_table_row_height(mut self, height: f32) -> Self {
self.config.table_row_height = Some(height);
self
}
pub fn with_max_undo_history(mut self, max_undo_history: usize) -> Self {
self.config.max_undo_history = max_undo_history;
self
}
pub fn show(mut self, ui: &mut egui::Ui) -> Response {
let ctx = &ui.ctx().clone();
let ui_id = ui.id();
let style = ui.style().clone();
let painter = ui.painter().clone();
let visual = &style.visuals;
let viewer = &mut *self.viewer;
let s = self.state.as_mut().unwrap();
let mut resp_total = None::<Response>;
let mut resp_ret = None::<Response>;
let mut commands = Vec::<Command<R>>::new();
let ui_layer_id = ui.layer_id();
// NOTE: unlike RED and YELLOW which can be acquirable through 'error_bg_color' and
// 'warn_bg_color', there's no 'green' color which can be acquired from inherent theme.
// Following logic simply gets 'green' color from current background's brightness.
let green = if visual.window_fill.g() > 128 {
Color32::DARK_GREEN
} else {
Color32::GREEN
};
let mut builder = egui_extras::TableBuilder::new(ui).column(Column::auto());
for &column in s.vis_cols().iter() {
builder = builder.column(viewer.column_render_config(column.0));
}
if replace(&mut s.cci_want_move_scroll, false) {
let interact_row = s.interactive_cell().0;
builder = builder.scroll_to_row(interact_row.0, None);
}
builder
.columns(Column::auto(), s.num_columns() - s.vis_cols().len())
.drag_to_scroll(false) // Drag is used for selection;
.striped(true)
.max_scroll_height(f32::MAX)
.sense(Sense::click_and_drag().tap_mut(|s| s.focusable = true))
.header(20., |mut h| {
h.set_selected(s.cci_has_focus);
h.col(|ui| {
ui.centered_and_justified(|ui| {
ui.monospace("POS / ID");
});
});
h.set_selected(false);
let has_any_hidden_col = s.vis_cols().len() != s.num_columns();
for (vis_col, &col) in s.vis_cols().iter().enumerate() {
let vis_col = VisColumnPos(vis_col);
let mut painter = None;
let (col_rect, resp) = h.col(|ui| {
ui.horizontal_centered(|ui| {
if let Some(pos) = s.sort().iter().position(|(c, ..)| c == &col) {
let is_asc = s.sort()[pos].1 .0 as usize;
ui.colored_label(
[green, Color32::RED][is_asc],
RichText::new(format!("{}{}", ["โ", "โ"][is_asc], pos + 1,))
.monospace(),
);
} else {
ui.monospace(" ");
}
egui::Label::new(viewer.column_name(col.0))
.selectable(false)
.ui(ui);
});
painter = Some(ui.painter().clone());
});
// Set drag payload for column reordering.
resp.dnd_set_drag_payload(vis_col);
if resp.dragged() {
egui::popup::show_tooltip_text(
ctx,
ui_layer_id,
"_EGUI_DATATABLE__COLUMN_MOVE__".into(),
viewer.column_name(col.0),
);
}
if resp.hovered() && viewer.is_sortable_column(col.0) {
if let Some(p) = &painter {
p.rect_filled(
col_rect,
egui::Rounding::ZERO,
visual.selection.bg_fill.gamma_multiply(0.2),
);
}
}
if viewer.is_sortable_column(col.0) && resp.clicked_by(PointerButton::Primary) {
let mut sort = s.sort().to_owned();
match sort.iter_mut().find(|(c, ..)| c == &col) {
Some((_, asc)) => match asc.0 {
true => asc.0 = false,
false => sort.retain(|(c, ..)| c != &col),
},
None => {
sort.push((col, IsAscending(true)));
}
}
commands.push(Command::SetColumnSort(sort));
}
if resp.dnd_hover_payload::<VisColumnPos>().is_some() {
if let Some(p) = &painter {
p.rect_filled(
col_rect,
egui::Rounding::ZERO,
visual.selection.bg_fill.gamma_multiply(0.5),
);
}
}
if let Some(payload) = resp.dnd_release_payload::<VisColumnPos>() {
commands.push(Command::CcReorderColumn {
from: *payload,
to: vis_col
.0
.pipe(|v| v + (payload.0 < v) as usize)
.pipe(VisColumnPos),
})
}
resp.context_menu(|ui| {
if ui.button("Hide").clicked() {
commands.push(Command::CcHideColumn(col));
ui.close_menu();
}
if !s.sort().is_empty() && ui.button("Clear Sort").clicked() {
commands.push(Command::SetColumnSort(Vec::new()));
ui.close_menu();
}
if has_any_hidden_col {
ui.separator();
ui.label("Hidden");
for col in (0..s.num_columns()).map(ColumnIdx) {
if !s.vis_cols().contains(&col)
&& ui.button(viewer.column_name(col.0)).clicked()
{
commands.push(Command::CcShowColumn {
what: col,
at: vis_col,
});
ui.close_menu();
}
}
}
});
}
// Account for header response to calculate total response.
resp_total = Some(h.response());
})
.tap_mut(|table| {
table.ui_mut().separator();
})
.body(|body: egui_extras::TableBody<'_>| {
resp_ret =
Some(self.show_body(body, painter, commands, ctx, &style, ui_id, resp_total));
});
resp_ret.unwrap_or_else(|| ui.label("??"))
}
#[allow(clippy::too_many_arguments)]
fn show_body(
&mut self,
body: egui_extras::TableBody<'_>,
mut _painter: egui::Painter,
mut commands: Vec<Command<R>>,
ctx: &egui::Context,
style: &egui::Style,
ui_id: egui::Id,
mut resp_total: Option<Response>,
) -> Response {
let viewer = &mut *self.viewer;
let s = self.state.as_mut().unwrap();
let table = &mut *self.table;
let visual = &style.visuals;
let visible_cols = s.vis_cols().clone();
let no_rounding = egui::Rounding::ZERO;
let mut actions = Vec::<UiAction>::new();
let mut edit_started = false;
let hotkeys = viewer.hotkeys(&s.ui_action_context());
// Preemptively consume all hotkeys.
'detect_hotkey: {
// Detect hotkey inputs only when the table has focus. While editing, let the
// editor consume input.
if !s.cci_has_focus {
break 'detect_hotkey;
}
if !s.is_editing() {
ctx.input_mut(|i| {
i.events.retain(|x| {
match x {
Event::Copy => actions.push(UiAction::CopySelection),
Event::Cut => actions.push(UiAction::CutSelection),
// Try to parse clipboard contents and detect if it's compatible
// with cells being pasted.
Event::Paste(clipboard) => {
if !clipboard.is_empty() {
// If system clipboard is not empty, try to update the internal
// clipboard with system clipboard content before applying
// paste operation.
s.try_update_clipboard_from_string(viewer, clipboard);
}
if i.modifiers.shift {
actions.push(UiAction::PasteInsert)
} else {
actions.push(UiAction::PasteInPlace)
}
}
_ => return true,
}
false
})
});
}
for (hotkey, action) in &hotkeys {
ctx.input_mut(|inp| {
if inp.consume_shortcut(hotkey) {
actions.push(*action);
}
})
}
}
// Validate persistency state.
#[cfg(feature = "persistency")]
if viewer.persist_ui_state() {
s.validate_persistency(ctx, ui_id, viewer);
}
// Validate ui state. Defer this as late as possible; since it may not be
// called if the table area is out of the visible space.
s.validate_cc(&mut table.rows, viewer);
// Checkout `cc_rows` to satisfy borrow checker. We need to access to
// state mutably within row rendering; therefore, we can't simply borrow
// `cc_rows` during the whole logic!
let cc_row_heights = take(&mut s.cc_row_heights);
let mut row_height_updates = Vec::new();
let vis_row_digits = s.cc_rows.len().max(1).ilog10();
let row_id_digits = table.len().max(1).ilog10();
let body_max_rect = body.max_rect();
let pointer_interact_pos = ctx.input(|i| i.pointer.latest_pos().unwrap_or_default());
let pointer_primary_down = ctx.input(|i| i.pointer.button_down(PointerButton::Primary));
s.cci_page_row_count = 0;
/* ----------------------------- Primary Rendering Function ----------------------------- */
// - Extracted as a closure to differentiate behavior based on row height
// configuration. (heterogeneous or homogeneous row heights)
let render_fn = |mut row: egui_extras::TableRow| {
s.cci_page_row_count += 1;
let vis_row = VisRowPos(row.index());
let row_id = s.cc_rows[vis_row.0];
let prev_row_height = cc_row_heights[vis_row.0];
let mut row_elem_start = Default::default();
// Check if current row is edition target
let edit_state = s.row_editing_cell(row_id);
let mut editing_cell_rect = Rect::NOTHING;
let interactive_row = s.is_interactive_row(vis_row);
let check_mouse_dragging_selection = {
let s_cci_has_focus = s.cci_has_focus;
let s_cci_has_selection = s.has_cci_selection();
move |resp: &egui::Response| {
let cci_hovered: bool = s_cci_has_focus
&& s_cci_has_selection
&& resp.rect.contains(pointer_interact_pos);
let sel_drag = cci_hovered && pointer_primary_down;
let sel_click = !s_cci_has_selection && resp.hovered() && pointer_primary_down;
sel_drag || sel_click
}
};
/* -------------------------------- Header Rendering -------------------------------- */
// Mark row background filled if being edited.
row.set_selected(edit_state.is_some());
// Render row header button
let (_, head_resp) = row.col(|ui| {
// Calculate the position where values start.
row_elem_start = ui.max_rect().right_top();
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
ui.separator();
ui.monospace(
RichText::from(f!("{:ยท>width$}", row_id.0, width = row_id_digits as usize))
.strong(),
);
ui.monospace(
RichText::from(f!(
"{:ยท>width$}",
vis_row.0 + 1,
width = vis_row_digits as usize
))
.weak(),
);
});
});
if check_mouse_dragging_selection(&head_resp) {
s.cci_sel_update_row(vis_row);
}
/* -------------------------------- Columns Rendering ------------------------------- */
// Overridable maximum height
let mut new_maximum_height = 0.;
// Render cell contents regardless of the edition state.
for (vis_col, col) in visible_cols.iter().enumerate() {
let vis_col = VisColumnPos(vis_col);
let linear_index = vis_row.linear_index(visible_cols.len(), vis_col);
let selected = s.is_selected(vis_row, vis_col);
let cci_selected = s.is_selected_cci(vis_row, vis_col);
let is_editing = edit_state.is_some();
let is_interactive_cell = interactive_row.is_some_and(|x| x == vis_col);
let mut response_consumed = s.is_editing();
// Mark background filled if selected.
row.set_selected(is_editing || cci_selected);
let (rect, resp) = row.col(|ui| {
let ui_max_rect = ui.max_rect();
if is_interactive_cell {
ui.painter().rect_filled(
ui_max_rect.expand(3.),
no_rounding,
visual.warn_fg_color.gamma_multiply(0.2),
);
} else if !cci_selected && selected {
ui.painter()
.rect_filled(ui_max_rect, no_rounding, visual.extreme_bg_color);
}
// Actual widget rendering happens within this line.
// ui.set_enabled(false);
ui.style_mut()
.visuals
.widgets
.noninteractive
.fg_stroke
.color = visual.strong_text_color();
// FIXME: After egui 0.27, now the widgets spawned inside this closure
// intercepts interactions, which is basically natural behavior(Upper layer
// widgets). However, this change breaks current implementation which relies on
// the previous table behavior.
ui.add_enabled_ui(false, |ui| {
viewer.show_cell_view(ui, &table.rows[row_id.0], col.0);
});
if selected {
ui.painter().rect_stroke(
ui_max_rect,
no_rounding,
Stroke {
width: 1.,
color: visual.weak_text_color(),
},
);
}
if interactive_row.is_some() && !is_editing {
let st = Stroke {
width: 1.,
color: visual.warn_fg_color.gamma_multiply(0.5),
};
let xr = ui_max_rect.x_range();
let yr = ui_max_rect.y_range();
ui.painter().hline(xr, yr.min, st);
ui.painter().hline(xr, yr.max, st);
}
if edit_state.is_some_and(|(_, vis)| vis == vis_col) {
editing_cell_rect = ui_max_rect;
}
});
new_maximum_height = rect.height().max(new_maximum_height);
// -- Mouse Actions --
if check_mouse_dragging_selection(&resp) {
// Expand cci selection
response_consumed = true;
s.cci_sel_update(linear_index);
}
if resp.clicked_by(PointerButton::Primary) && is_interactive_cell {
response_consumed = true;
commands.push(Command::CcEditStart(
row_id,
vis_col,
viewer.clone_row(&table.rows[row_id.0]).into(),
));
edit_started = true;
}
/* --------------------------- Context Menu Rendering --------------------------- */
(resp.clone() | head_resp.clone()).context_menu(|ui| {
response_consumed = true;
ui.set_min_size(egui::vec2(250., 10.));
if !selected {
commands.push(Command::CcSetSelection(vec![VisSelection(
linear_index,
linear_index,
)]));
} else if !is_interactive_cell {
s.set_interactive_cell(vis_row, vis_col);
}
let sel_multi_row = s.cursor_as_selection().is_some_and(|sel| {
let mut min = usize::MAX;
let mut max = usize::MIN;
for sel in sel {
min = min.min(sel.0 .0);
max = max.max(sel.1 .0);
}
let (r_min, _) = VisLinearIdx(min).row_col(s.vis_cols().len());
let (r_max, _) = VisLinearIdx(max).row_col(s.vis_cols().len());
r_min != r_max
});
let cursor_x = ui.cursor().min.x;
let clip = s.has_clipboard_contents();
let b_undo = s.has_undo();
let b_redo = s.has_redo();
let mut n_sep_menu = 0;
let mut draw_sep = false;
[
Some((selected, "๐ป", "Selection: Copy", UiAction::CopySelection)),
Some((selected, "๐ป", "Selection: Cut", UiAction::CutSelection)),
Some((selected, "๐", "Selection: Clear", UiAction::DeleteSelection)),
Some((
sel_multi_row,
"๐",
"Selection: Fill",
UiAction::SelectionDuplicateValues,
)),
None,
Some((clip, "โฟ", "Clipboard: Paste", UiAction::PasteInPlace)),
Some((clip, "๐ ", "Clipboard: Insert", UiAction::PasteInsert)),
None,
Some((true, "๐", "Row: Duplicate", UiAction::DuplicateRow)),
Some((true, "๐", "Row: Delete", UiAction::DeleteRow)),
None,
Some((b_undo, "โ", "Undo", UiAction::Undo)),
Some((b_redo, "โ", "Redo", UiAction::Redo)),
]
.map(|opt| {
if let Some((icon, label, action)) =
opt.filter(|x| x.0).map(|x| (x.1, x.2, x.3))
{
if draw_sep {
draw_sep = false;
ui.separator();
}
let hotkey = hotkeys
.iter()
.find_map(|(k, a)| (a == &action).then(|| ctx.format_shortcut(k)));
ui.horizontal(|ui| {
ui.monospace(icon);
ui.add_space(cursor_x + 20. - ui.cursor().min.x);
let btn = egui::Button::new(label)
.shortcut_text(hotkey.unwrap_or_else(|| "๐".into()));
let r = ui.centered_and_justified(|ui| ui.add(btn)).inner;
if r.clicked() {
actions.push(action);
ui.close_menu();
}
});
n_sep_menu += 1;
} else if n_sep_menu > 0 {
n_sep_menu = 0;
draw_sep = true;
}
});
});
// Forward DnD event if not any event was consumed by the response.
// FIXME: Upgrading egui 0.29 make interaction rectangle of response object
// larger(in y axis) than actually visible column cell size. To deal with this,
// I've used returned content area rectangle instead, expanding its width to
// response size.
let drop_area_rect = rect.with_max_x(resp.rect.max.x);
let contains_pointer = ctx
.pointer_hover_pos()
.is_some_and(|pos| drop_area_rect.contains(pos));
if !response_consumed && contains_pointer {
if let Some(new_value) =
viewer.on_cell_view_response(&table.rows[row_id.0], col.0, &resp)
{
commands.push(Command::SetCells {
slab: vec![*new_value].into_boxed_slice(),
values: vec![(row_id, *col, RowSlabIndex(0))].into_boxed_slice(),
});
}
}
}
/* -------------------------------- Editor Rendering -------------------------------- */
if let Some((should_focus, vis_column)) = edit_state {
let column = s.vis_cols()[vis_column.0];
egui::Window::new("")
.id(ui_id.with(row_id).with(column))
.constrain_to(body_max_rect)
.fixed_pos(editing_cell_rect.min)
.auto_sized()
.min_size(editing_cell_rect.size())
.max_width(editing_cell_rect.width())
.title_bar(false)
.frame(egui::Frame::none().rounding(egui::Rounding::same(3.)))
.show(ctx, |ui| {
ui.with_layout(Layout::top_down_justified(Align::LEFT), |ui| {
if let Some(resp) =
viewer.show_cell_editor(ui, s.unwrap_editing_row_data(), column.0)
{
if should_focus {
resp.request_focus()
}
new_maximum_height = resp.rect.height().max(new_maximum_height);
} else {
commands.push(Command::CcCommitEdit);
}
});
});
}
// Accumulate response
if let Some(resp) = &mut resp_total {
*resp = resp.union(row.response());
} else {
resp_total = Some(row.response());
}
// Update row height cache if necessary.
if self.config.table_row_height.is_none() && prev_row_height != new_maximum_height {
row_height_updates.push((vis_row, new_maximum_height));
}
}; // ~ render_fn
// Actual rendering
if let Some(height) = self.config.table_row_height {
body.rows(height, cc_row_heights.len(), render_fn);
} else {
body.heterogeneous_rows(cc_row_heights.iter().cloned(), render_fn);
}
/* ----------------------------------- Event Handling ----------------------------------- */
if ctx.input(|i| i.pointer.button_released(PointerButton::Primary)) {
let mods = ctx.input(|i| i.modifiers);
if let Some(sel) = s.cci_take_selection(mods).filter(|_| !edit_started) {
commands.push(Command::CcSetSelection(sel));
}
}
// Control overall focus status.
if let Some(resp) = resp_total.clone() {
if resp.clicked() | resp.dragged() {
s.cci_has_focus = true;
} else if resp.clicked_elsewhere() {
s.cci_has_focus = false;
}
}
// Check in borrowed `cc_rows` back to state.
s.cc_row_heights = cc_row_heights.tap_mut(|values| {
if !row_height_updates.is_empty() {
ctx.request_repaint();
}
for (row_index, row_height) in row_height_updates {
values[row_index.0] = row_height;
}
});
// Handle queued actions
commands.extend(
actions
.into_iter()
.flat_map(|action| s.try_apply_ui_action(table, viewer, action)),
);
// Handle queued commands
for cmd in commands {
match cmd {
Command::CcUpdateSystemClipboard(new_content) => {
ctx.output_mut(|x| {
x.copied_text = new_content;
});
}
cmd => {
if matches!(cmd, Command::CcCommitEdit) {
// If any commit action is detected, release any remaining focus.
ctx.memory_mut(|x| {
if let Some(fc) = x.focused() {
x.surrender_focus(fc)
}
});
}
s.push_new_command(table, viewer, cmd, self.config.max_undo_history);
}
}
}
// Total response
resp_total.unwrap()
}
}
impl<'a, R, V: RowViewer<R>> Drop for Renderer<'a, R, V> {
fn drop(&mut self) {
self.table.ui = self.state.take();
}
}