From 07f1b074ca845f1b09419201b1b57f8f613bdd6b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 16 Jan 2021 11:34:59 +0100 Subject: [PATCH] [grid] Allow putting a separator in a grid --- egui/src/grid.rs | 22 ++++++++++++++----- egui/src/placer.rs | 4 ++++ egui/src/ui.rs | 4 ++++ egui/src/widgets/separator.rs | 4 ++-- egui_demo_lib/src/apps/demo/widget_gallery.rs | 5 +---- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/egui/src/grid.rs b/egui/src/grid.rs index 7b572dec..6a341124 100644 --- a/egui/src/grid.rs +++ b/egui/src/grid.rs @@ -82,15 +82,25 @@ impl GridLayout { } pub(crate) fn available_rect(&self, region: &Region) -> Rect { - let mut rect = Rect::from_min_max(region.cursor, region.max_rect.max); - rect.set_height(rect.height().at_least(self.min_row_height)); - rect + // let mut rect = Rect::from_min_max(region.cursor, region.max_rect.max); + // rect.set_height(rect.height().at_least(self.min_row_height)); + // rect + self.available_rect_finite(region) } pub(crate) fn available_rect_finite(&self, region: &Region) -> Rect { - let mut rect = Rect::from_min_max(region.cursor, region.max_rect_finite().max); - rect.set_height(rect.height().at_least(self.min_row_height)); - rect + // If we want to allow width-filling widgets like `Separator` in one of the first cells + // then we need to make sure they don't spill out of the first cell: + let width = self + .prev_state + .col_width(self.col) + .or_else(|| self.curr_state.col_width(self.col)) + .unwrap_or_default(); + let height = region + .max_rect_finite() + .height() + .at_least(self.min_row_height); + Rect::from_min_size(region.cursor, vec2(width, height)) } pub(crate) fn next_cell(&self, cursor: Pos2, child_size: Vec2) -> Rect { diff --git a/egui/src/placer.rs b/egui/src/placer.rs index 54194feb..8673f28b 100644 --- a/egui/src/placer.rs +++ b/egui/src/placer.rs @@ -27,6 +27,10 @@ impl Placer { } } + pub(crate) fn is_grid(&self) -> bool { + self.grid.is_some() + } + pub(crate) fn layout(&self) -> &Layout { &self.layout } diff --git a/egui/src/ui.rs b/egui/src/ui.rs index e9d58e5a..27383088 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -1118,6 +1118,10 @@ impl Ui { self.placer.save_grid(); } + pub(crate) fn is_grid(&self) -> bool { + self.placer.is_grid() + } + /// Move to the next row in a grid layout or wrapping layout. /// Otherwise does nothing. pub fn end_row(&mut self) { diff --git a/egui/src/widgets/separator.rs b/egui/src/widgets/separator.rs index 9e3605be..f0305bf5 100644 --- a/egui/src/widgets/separator.rs +++ b/egui/src/widgets/separator.rs @@ -45,8 +45,8 @@ impl Widget for Separator { is_horizontal_line, } = self; - let is_horizontal_line = - is_horizontal_line.unwrap_or_else(|| !ui.layout().main_dir().is_horizontal()); + let is_horizontal_line = is_horizontal_line + .unwrap_or_else(|| ui.is_grid() || !ui.layout().main_dir().is_horizontal()); let available_space = ui.available_size_before_wrap_finite(); diff --git a/egui_demo_lib/src/apps/demo/widget_gallery.rs b/egui_demo_lib/src/apps/demo/widget_gallery.rs index 8312fdc3..fd4d5d1d 100644 --- a/egui_demo_lib/src/apps/demo/widget_gallery.rs +++ b/egui_demo_lib/src/apps/demo/widget_gallery.rs @@ -35,7 +35,6 @@ impl super::Demo for WidgetGallery { fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) { egui::Window::new(self.name()) .open(open) - .default_width(200.0) .resizable(false) .show(ctx, |ui| { use super::View; @@ -131,9 +130,7 @@ impl super::View for WidgetGallery { ui.end_row(); ui.label("Separator:"); - // Putting a separator in a grid is kind of meaningless since there is no well-defined direction. - // Normally you'd just do ui.separator(), but here we need to explicitly pick a dimension: - ui.add(egui::Separator::new().horizontal()); + ui.separator(); ui.end_row(); ui.label("CollapsingHeader:");