Table: don't allow resize of last column if it is Size::Remainder
This commit is contained in:
parent
51cc9c9a9a
commit
c029f25c13
1 changed files with 21 additions and 2 deletions
|
@ -82,6 +82,9 @@ impl<'a> TableBuilder<'a> {
|
||||||
|
|
||||||
/// Make the columns resizable by dragging.
|
/// Make the columns resizable by dragging.
|
||||||
///
|
///
|
||||||
|
/// If the _last_ column is [`Size::Remainder`], then it won't be resizable
|
||||||
|
/// (and instead use up the remainder).
|
||||||
|
///
|
||||||
/// Default is `false`.
|
/// Default is `false`.
|
||||||
///
|
///
|
||||||
/// If you have multiple [`Table`]:s in the same [`Ui`]
|
/// If you have multiple [`Table`]:s in the same [`Ui`]
|
||||||
|
@ -135,7 +138,7 @@ impl<'a> TableBuilder<'a> {
|
||||||
let widths = widths
|
let widths = widths
|
||||||
.unwrap_or_else(|| sizing.to_lengths(available_width, ui.spacing().item_spacing.x));
|
.unwrap_or_else(|| sizing.to_lengths(available_width, ui.spacing().item_spacing.x));
|
||||||
|
|
||||||
let table_top = ui.min_rect().bottom();
|
let table_top = ui.cursor().top();
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut layout = StripLayout::new(ui, CellDirection::Horizontal);
|
let mut layout = StripLayout::new(ui, CellDirection::Horizontal);
|
||||||
|
@ -154,6 +157,7 @@ impl<'a> TableBuilder<'a> {
|
||||||
table_top,
|
table_top,
|
||||||
resize_id,
|
resize_id,
|
||||||
sizing,
|
sizing,
|
||||||
|
available_width,
|
||||||
widths,
|
widths,
|
||||||
scroll,
|
scroll,
|
||||||
striped,
|
striped,
|
||||||
|
@ -184,13 +188,14 @@ impl<'a> TableBuilder<'a> {
|
||||||
let widths = widths
|
let widths = widths
|
||||||
.unwrap_or_else(|| sizing.to_lengths(available_width, ui.spacing().item_spacing.x));
|
.unwrap_or_else(|| sizing.to_lengths(available_width, ui.spacing().item_spacing.x));
|
||||||
|
|
||||||
let table_top = ui.min_rect().bottom();
|
let table_top = ui.cursor().top();
|
||||||
|
|
||||||
Table {
|
Table {
|
||||||
ui,
|
ui,
|
||||||
table_top,
|
table_top,
|
||||||
resize_id,
|
resize_id,
|
||||||
sizing,
|
sizing,
|
||||||
|
available_width,
|
||||||
widths,
|
widths,
|
||||||
scroll,
|
scroll,
|
||||||
striped,
|
striped,
|
||||||
|
@ -207,6 +212,7 @@ pub struct Table<'a> {
|
||||||
table_top: f32,
|
table_top: f32,
|
||||||
resize_id: Option<egui::Id>,
|
resize_id: Option<egui::Id>,
|
||||||
sizing: Sizing,
|
sizing: Sizing,
|
||||||
|
available_width: f32,
|
||||||
widths: Vec<f32>,
|
widths: Vec<f32>,
|
||||||
scroll: bool,
|
scroll: bool,
|
||||||
striped: bool,
|
striped: bool,
|
||||||
|
@ -223,6 +229,7 @@ impl<'a> Table<'a> {
|
||||||
table_top,
|
table_top,
|
||||||
resize_id,
|
resize_id,
|
||||||
sizing,
|
sizing,
|
||||||
|
mut available_width,
|
||||||
widths,
|
widths,
|
||||||
scroll,
|
scroll,
|
||||||
striped,
|
striped,
|
||||||
|
@ -256,6 +263,16 @@ impl<'a> Table<'a> {
|
||||||
for (i, width) in new_widths.iter_mut().enumerate() {
|
for (i, width) in new_widths.iter_mut().enumerate() {
|
||||||
x += *width + spacing_x;
|
x += *width + spacing_x;
|
||||||
|
|
||||||
|
// If the last column is Size::Remainder, then let it fill the remainder!
|
||||||
|
let last_column = i + 1 == sizing.sizes.len();
|
||||||
|
if last_column {
|
||||||
|
if let Size::Remainder { range: (min, max) } = sizing.sizes[i] {
|
||||||
|
let eps = 0.1; // just to avoid some rounding errors.
|
||||||
|
*width = (available_width - eps).clamp(min, max);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let resize_id = ui.id().with("__panel_resize").with(i);
|
let resize_id = ui.id().with("__panel_resize").with(i);
|
||||||
|
|
||||||
let mut p0 = egui::pos2(x, table_top);
|
let mut p0 = egui::pos2(x, table_top);
|
||||||
|
@ -301,6 +318,8 @@ impl<'a> Table<'a> {
|
||||||
ui.visuals().widgets.noninteractive.bg_stroke
|
ui.visuals().widgets.noninteractive.bg_stroke
|
||||||
};
|
};
|
||||||
ui.painter().line_segment([p0, p1], stroke);
|
ui.painter().line_segment([p0, p1], stroke);
|
||||||
|
|
||||||
|
available_width -= *width + spacing_x;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.data().insert_persisted(resize_id, new_widths);
|
ui.data().insert_persisted(resize_id, new_widths);
|
||||||
|
|
Loading…
Reference in a new issue