Skip to content

Commit d1c33b2

Browse files
authored
Fix slow shrinking of wide columns (#53)
Removed a HACK that I regret. Downside is that you can shrink it smaller than the contents, and when you release the mouse it will spring back. But that's better than the slow resize hack.
1 parent 47b73a4 commit d1c33b2

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

demo/src/table_demo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl TableDemo {
7676
// During a sizing pass we don't truncate!
7777
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate);
7878
}
79-
ui.label("Extra long cell!");
79+
ui.label("Extra long cell that will be truncated with an ellipsis character because it is so long");
8080
}
8181
});
8282

egui_table/src/table.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,8 @@ impl SplitScrollDelegate for TableSplitScrollDelegate<'_> {
855855
.entry(column_id)
856856
.or_insert(column.current);
857857

858+
let layout_width = *column_width; // Width used when computing col_x
859+
858860
if ui.is_sizing_pass() || column.auto_size_this_frame {
859861
// Shrink to fit the widest element in the column:
860862
*column_width = used_width;
@@ -865,7 +867,8 @@ impl SplitScrollDelegate for TableSplitScrollDelegate<'_> {
865867

866868
let column_resize_id = self.id.with(column.id_for(col_nr)).with("resize");
867869

868-
let mut x = self.col_x[col_nr + 1] - scroll_offset.x; // Right side of the column
870+
// Right side of the column, adjusted for any width change since layout:
871+
let mut x = self.col_x[col_nr + 1] - scroll_offset.x + (*column_width - layout_width);
869872
let yrange = Rangef::new(*top, ui.clip_rect().bottom());
870873
let line_rect = egui::Rect::from_x_y_ranges(x..=x, yrange)
871874
.expand(ui.style().interaction.resize_grab_radius_side);
@@ -876,25 +879,13 @@ impl SplitScrollDelegate for TableSplitScrollDelegate<'_> {
876879
if resize_response.dragged()
877880
&& let Some(pointer) = ui.ctx().pointer_latest_pos()
878881
{
879-
let desired_new_width = *column_width + pointer.x - x;
880-
let desired_new_width = column.range.clamp(desired_new_width);
881-
882-
// We don't want to shrink below the size that was actually used.
883-
// However, we still want to allow content that shrinks when you try
884-
// to make the column less wide, so we allow some small shrinkage each frame:
885-
// big enough to allow shrinking over time, small enough not to look ugly when
886-
// shrinking fails. This is a bit of a HACK around immediate mode.
887-
// TODO: do something smarter by remembering success/failure to resize from one frame to the next.
888-
// Probably best solved by implementing `ui.intrinsic_size`.
889-
let max_shrinkage_per_frame = 8.0;
890-
let new_width = desired_new_width.at_least(used_width - max_shrinkage_per_frame);
882+
// Drag-to-resize.
883+
// TODO: use `ui.intrinsic_size` (once it exist) to prevent
884+
// resizing below what the content can fit within.
885+
let new_width = *column_width + pointer.x - x;
891886
let new_width = column.range.clamp(new_width);
892887
x += new_width - *column_width;
893888
*column_width = new_width;
894-
895-
if new_width != desired_new_width {
896-
ui.ctx().request_repaint(); // Get there faster
897-
}
898889
}
899890

900891
let dragging_something_else =

0 commit comments

Comments
 (0)