From f75f6b0b3d16a148c02eb6ed0aa2e41ef1def72b Mon Sep 17 00:00:00 2001 From: v0x0g Date: Wed, 3 Jul 2024 11:07:58 +1000 Subject: [PATCH 1/2] Create `egui::Ui::columns_const()` --- crates/egui/src/ui.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/crates/egui/src/ui.rs b/crates/egui/src/ui.rs index f24a8a3792a..63eebba45bf 100644 --- a/crates/egui/src/ui.rs +++ b/crates/egui/src/ui.rs @@ -2389,6 +2389,58 @@ impl Ui { result } + /// Temporarily split a [`Ui`] into several columns. + /// + /// The same as [Self::columns()], but uses a constant for the column count. + /// This allows for compile-time bounds checking, and makes the compiler happy. + /// + /// ``` + /// # egui::__run_test_ui(|ui| { + /// ui.columns_const(|[col_1, col_2]| { + /// col_1.label("First column"); + /// col_2.label("Second column"); + /// }); + /// # }); + /// ``` + #[inline] + pub fn columns_const( + &mut self, + add_contents: impl FnOnce(&mut [Self; NUM_COL]) -> R, + ) -> R { + // TODO(emilk): ensure there is space + let spacing = self.spacing().item_spacing.x; + let total_spacing = spacing * (NUM_COL as f32 - 1.0); + let column_width = (self.available_width() - total_spacing) / (NUM_COL as f32); + let top_left = self.cursor().min; + + let mut columns = std::array::from_fn(|col_idx| { + let pos = top_left + vec2((col_idx as f32) * (column_width + spacing), 0.0); + let child_rect = Rect::from_min_max( + pos, + pos2(pos.x + column_width, self.max_rect().right_bottom().y), + ); + let mut column_ui = + self.child_ui(child_rect, Layout::top_down_justified(Align::LEFT), None); + column_ui.set_width(column_width); + column_ui + }); + let result = add_contents(&mut columns); + + let mut max_column_width = column_width; + let mut max_height = 0.0; + for column in &columns { + max_column_width = max_column_width.max(column.min_rect().width()); + max_height = column.min_size().y.max(max_height); + } + + // Make sure we fit everything next frame: + let total_required_width = total_spacing + max_column_width * (NUM_COL as f32); + + let size = vec2(self.available_width().max(total_required_width), max_height); + self.advance_cursor_after_rect(Rect::from_min_size(top_left, size)); + result + } + /// Create something that can be drag-and-dropped. /// /// The `id` needs to be globally unique. From 38c3648f5b98a652ad43ba5f8f1ee1793f9d6699 Mon Sep 17 00:00:00 2001 From: v0x0g Date: Wed, 3 Jul 2024 14:07:12 +1000 Subject: [PATCH 2/2] Fix missing backticks in docs --- crates/egui/src/ui.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/egui/src/ui.rs b/crates/egui/src/ui.rs index 63eebba45bf..8f3359c6901 100644 --- a/crates/egui/src/ui.rs +++ b/crates/egui/src/ui.rs @@ -2391,7 +2391,7 @@ impl Ui { /// Temporarily split a [`Ui`] into several columns. /// - /// The same as [Self::columns()], but uses a constant for the column count. + /// The same as [`Self::columns()`], but uses a constant for the column count. /// This allows for compile-time bounds checking, and makes the compiler happy. /// /// ```