use std::{hash::Hash, sync::Arc};
use crate::{color::*, containers::*, layout::*, mutex::MutexGuard, paint::*, widgets::*, *};
pub struct Ui {
id: Id,
next_auto_id: u64,
painter: Painter,
style: Arc<Style>,
layout: Layout,
region: Region,
}
impl Ui {
pub fn new(ctx: CtxRef, layer_id: LayerId, id: Id, max_rect: Rect, clip_rect: Rect) -> Self {
let style = ctx.style();
let layout = Layout::default();
let region = layout.region_from_max_rect(max_rect);
Ui {
id,
next_auto_id: id.with("auto").value(),
painter: Painter::new(ctx, layer_id, clip_rect),
style,
layout,
region,
}
}
pub fn child_ui(&mut self, max_rect: Rect, layout: Layout) -> Self {
self.next_auto_id = self.next_auto_id.wrapping_add(1);
let region = layout.region_from_max_rect(max_rect);
Ui {
id: self.id.with("child"),
next_auto_id: Id::new(self.next_auto_id).with("child").value(),
painter: self.painter.clone(),
style: self.style.clone(),
layout,
region,
}
}
pub fn __test() -> Self {
let mut ctx = CtxRef::default();
ctx.begin_frame(Default::default());
let id = Id::new("__test");
let layer_id = LayerId::new(Order::Middle, id);
let rect = Rect::from_min_size(Pos2::new(0.0, 0.0), vec2(1000.0, 1000.0));
Self::new(ctx, layer_id, id, rect, rect)
}
pub fn id(&self) -> Id {
self.id
}
pub fn style(&self) -> &Style {
&self.style
}
pub fn style_mut(&mut self) -> &mut Style {
Arc::make_mut(&mut self.style)
}
pub fn set_style(&mut self, style: impl Into<Arc<Style>>) {
self.style = style.into();
}
pub fn ctx(&self) -> &CtxRef {
self.painter.ctx()
}
pub fn painter(&self) -> &Painter {
&self.painter
}
pub fn layout(&self) -> &Layout {
&self.layout
}
pub fn painter_at(&self, rect: Rect) -> Painter {
self.painter().sub_region(rect)
}
pub fn layer_id(&self) -> LayerId {
self.painter().layer_id()
}
pub fn input(&self) -> &InputState {
self.ctx().input()
}
pub fn memory(&self) -> MutexGuard<'_, Memory> {
self.ctx().memory()
}
pub fn output(&self) -> MutexGuard<'_, Output> {
self.ctx().output()
}
pub fn fonts(&self) -> &Fonts {
self.ctx().fonts()
}
pub fn clip_rect(&self) -> Rect {
self.painter.clip_rect()
}
pub fn set_clip_rect(&mut self, clip_rect: Rect) {
self.painter.set_clip_rect(clip_rect);
}
}
impl Ui {
pub fn min_rect(&self) -> Rect {
self.region.min_rect
}
pub fn min_size(&self) -> Vec2 {
self.min_rect().size()
}
pub fn max_rect(&self) -> Rect {
self.region.max_rect
}
pub(crate) fn force_set_min_rect(&mut self, min_rect: Rect) {
self.region.min_rect = min_rect;
}
pub fn max_rect_finite(&self) -> Rect {
self.region.max_rect_finite()
}
pub fn set_max_size(&mut self, size: Vec2) {
self.set_max_width(size.x);
self.set_max_height(size.y);
}
pub fn set_max_width(&mut self, width: f32) {
#![allow(clippy::float_cmp)]
if self.layout.main_dir() == Direction::RightToLeft {
debug_assert_eq!(self.min_rect().max.x, self.max_rect().max.x);
self.region.max_rect.min.x =
self.region.max_rect.max.x - width.at_least(self.min_rect().width());
} else {
debug_assert_eq!(self.min_rect().min.x, self.region.max_rect.min.x);
self.region.max_rect.max.x =
self.region.max_rect.min.x + width.at_least(self.min_rect().width());
}
}
pub fn set_max_height(&mut self, height: f32) {
#![allow(clippy::float_cmp)]
if self.layout.main_dir() == Direction::BottomUp {
debug_assert_eq!(self.min_rect().max.y, self.region.max_rect.max.y);
self.region.max_rect.min.y =
self.region.max_rect.max.y - height.at_least(self.min_rect().height());
} else {
debug_assert_eq!(self.min_rect().min.y, self.region.max_rect.min.y);
self.region.max_rect.max.y =
self.region.max_rect.min.y + height.at_least(self.min_rect().height());
}
}
pub fn set_min_size(&mut self, size: Vec2) {
self.set_min_width(size.x);
self.set_min_height(size.y);
}
pub fn set_min_width(&mut self, width: f32) {
#![allow(clippy::float_cmp)]
if self.layout.main_dir() == Direction::RightToLeft {
debug_assert_eq!(self.region.min_rect.max.x, self.region.max_rect.max.x);
let min_rect = &mut self.region.min_rect;
min_rect.min.x = min_rect.min.x.min(min_rect.max.x - width);
} else {
debug_assert_eq!(self.region.min_rect.min.x, self.region.max_rect.min.x);
let min_rect = &mut self.region.min_rect;
min_rect.max.x = min_rect.max.x.max(min_rect.min.x + width);
}
self.region.max_rect = self.region.max_rect.union(self.min_rect());
}
pub fn set_min_height(&mut self, height: f32) {
#![allow(clippy::float_cmp)]
if self.layout.main_dir() == Direction::BottomUp {
debug_assert_eq!(self.region.min_rect.max.y, self.region.max_rect.max.y);
let min_rect = &mut self.region.min_rect;
min_rect.min.y = min_rect.min.y.min(min_rect.max.y - height);
} else {
debug_assert_eq!(self.region.min_rect.min.y, self.region.max_rect.min.y);
let min_rect = &mut self.region.min_rect;
min_rect.max.y = min_rect.max.y.max(min_rect.min.y + height);
}
self.region.max_rect = self.region.max_rect.union(self.min_rect());
}
pub fn shrink_width_to_current(&mut self) {
self.set_max_width(self.min_rect().width())
}
pub fn shrink_height_to_current(&mut self) {
self.set_max_height(self.min_rect().height())
}
pub fn expand_to_include_rect(&mut self, rect: Rect) {
self.region.expand_to_include_rect(rect);
}
pub fn set_width_range(&mut self, width: std::ops::RangeInclusive<f32>) {
self.set_min_width(*width.start());
self.set_max_width(*width.end());
}
pub fn set_width(&mut self, width: f32) {
self.set_min_width(width);
self.set_max_width(width);
}
pub fn available_size(&self) -> Vec2 {
self.layout.available_size(&self.region)
}
pub fn available_width(&self) -> f32 {
self.available_size().x
}
pub fn available_size_before_wrap(&self) -> Vec2 {
self.layout.available_size_before_wrap(&self.region)
}
pub fn available_size_before_wrap_finite(&self) -> Vec2 {
self.layout
.available_rect_before_wrap_finite(&self.region)
.size()
}
pub fn available_rect_before_wrap(&self) -> Rect {
self.layout.available_rect_before_wrap(&self.region)
}
pub fn available_rect_before_wrap_finite(&self) -> Rect {
self.layout.available_rect_before_wrap_finite(&self.region)
}
}
impl Ui {
pub fn make_persistent_id<IdSource>(&self, id_source: IdSource) -> Id
where
IdSource: Hash + std::fmt::Debug,
{
self.id.with(&id_source)
}
#[deprecated = "This id now returned from ui.allocate_space"]
pub fn make_position_id(&self) -> Id {
Id::new(self.next_auto_id)
}
pub(crate) fn auto_id_with<IdSource>(&self, id_source: IdSource) -> Id
where
IdSource: Hash + std::fmt::Debug,
{
Id::new(self.next_auto_id).with(id_source)
}
}
impl Ui {
pub fn interact(&self, rect: Rect, id: Id, sense: Sense) -> Response {
self.ctx().interact(
self.clip_rect(),
self.style().spacing.item_spacing,
self.layer_id(),
id,
rect,
sense,
)
}
pub fn rect_contains_mouse(&self, rect: Rect) -> bool {
self.ctx()
.rect_contains_mouse(self.layer_id(), self.clip_rect().intersect(rect))
}
pub fn ui_contains_mouse(&self) -> bool {
if let Some(mouse_pos) = self.input().mouse.pos {
self.clip_rect().contains(mouse_pos)
&& self.ctx().layer_id_at(mouse_pos) == Some(self.layer_id())
} else {
false
}
}
#[deprecated = "Use: interact(rect, id, Sense::hover())"]
pub fn interact_hover(&self, rect: Rect) -> Response {
self.interact(rect, self.auto_id_with("hover_rect"), Sense::hover())
}
#[deprecated = "Use: rect_contains_mouse()"]
pub fn hovered(&self, rect: Rect) -> bool {
self.interact(rect, self.id, Sense::hover()).hovered
}
pub fn advance_cursor(&mut self, amount: f32) {
self.layout.advance_cursor(&mut self.region, amount);
}
pub fn allocate_response(&mut self, desired_size: Vec2, sense: Sense) -> Response {
let (id, rect) = self.allocate_space(desired_size);
self.interact(rect, id, sense)
}
pub fn allocate_space(&mut self, desired_size: Vec2) -> (Id, Rect) {
let original_available = self.available_size_before_wrap();
let too_wide = desired_size.x > original_available.x;
let too_high = desired_size.y > original_available.y;
let rect = self.allocate_space_impl(desired_size);
let debug_expand_width = self.style().visuals.debug_expand_width;
let debug_expand_height = self.style().visuals.debug_expand_height;
if (debug_expand_width && too_wide) || (debug_expand_height && too_high) {
self.painter
.rect_stroke(rect, 0.0, (1.0, Color32::LIGHT_BLUE));
let color = color::Color32::from_rgb(200, 0, 0);
let width = 2.5;
let paint_line_seg = |a, b| self.painter().line_segment([a, b], (width, color));
if debug_expand_width && too_wide {
paint_line_seg(rect.left_top(), rect.left_bottom());
paint_line_seg(rect.left_center(), rect.right_center());
paint_line_seg(
pos2(rect.left() + original_available.x, rect.top()),
pos2(rect.left() + original_available.x, rect.bottom()),
);
paint_line_seg(rect.right_top(), rect.right_bottom());
}
if debug_expand_height && too_high {
paint_line_seg(rect.left_top(), rect.right_top());
paint_line_seg(rect.center_top(), rect.center_bottom());
paint_line_seg(rect.left_bottom(), rect.right_bottom());
}
}
self.next_auto_id = self.next_auto_id.wrapping_add(1);
let id = Id::new(self.next_auto_id);
(id, rect)
}
fn allocate_space_impl(&mut self, desired_size: Vec2) -> Rect {
let item_spacing = self.style().spacing.item_spacing;
let outer_child_rect = self
.layout
.next_space(&self.region, desired_size, item_spacing);
let inner_child_rect = self.layout.justify_or_align(outer_child_rect, desired_size);
self.layout.advance_after_outer_rect(
&mut self.region,
outer_child_rect,
inner_child_rect,
item_spacing,
);
self.region.expand_to_include_rect(inner_child_rect);
inner_child_rect
}
pub(crate) fn advance_cursor_after_rect(&mut self, rect: Rect) -> Id {
let item_spacing = self.style().spacing.item_spacing;
self.layout
.advance_after_outer_rect(&mut self.region, rect, rect, item_spacing);
self.region.expand_to_include_rect(rect);
self.next_auto_id = self.next_auto_id.wrapping_add(1);
Id::new(self.next_auto_id)
}
pub(crate) fn cursor(&self) -> Pos2 {
self.region.cursor
}
pub fn allocate_ui<R>(
&mut self,
desired_size: Vec2,
add_contents: impl FnOnce(&mut Self) -> R,
) -> (R, Response) {
let item_spacing = self.style().spacing.item_spacing;
let outer_child_rect = self
.layout
.next_space(&self.region, desired_size, item_spacing);
let inner_child_rect = self.layout.justify_or_align(outer_child_rect, desired_size);
let mut child_ui = self.child_ui(inner_child_rect, self.layout);
let ret = add_contents(&mut child_ui);
let final_child_rect = child_ui.region.min_rect;
self.layout.advance_after_outer_rect(
&mut self.region,
outer_child_rect.union(final_child_rect),
final_child_rect,
item_spacing,
);
self.region.expand_to_include_rect(final_child_rect);
let response = self.interact(final_child_rect, child_ui.id, Sense::hover());
(ret, response)
}
pub fn allocate_painter(&mut self, desired_size: Vec2, sense: Sense) -> (Response, Painter) {
let response = self.allocate_response(desired_size, sense);
let clip_rect = self.clip_rect().intersect(response.rect);
let painter = Painter::new(self.ctx().clone(), self.layer_id(), clip_rect);
(response, painter)
}
pub fn scroll_to_cursor(&mut self, align: Align) {
let scroll_y = self.region.cursor.y;
self.ctx().frame_state().scroll_target = Some((scroll_y, align));
}
}
impl Ui {
pub fn add(&mut self, widget: impl Widget) -> Response {
widget.ui(self)
}
pub fn label(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into())
}
pub fn colored_label(
&mut self,
color: impl Into<Color32>,
label: impl Into<Label>,
) -> Response {
self.add(label.into().text_color(color))
}
pub fn heading(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into().heading())
}
pub fn monospace(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into().monospace())
}
pub fn small(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into().small())
}
pub fn hyperlink(&mut self, url: impl Into<String>) -> Response {
self.add(Hyperlink::new(url))
}
#[deprecated = "Use `text_edit_singleline` or `text_edit_multiline`"]
pub fn text_edit(&mut self, text: &mut String) -> Response {
self.text_edit_multiline(text)
}
pub fn text_edit_singleline(&mut self, text: &mut String) -> Response {
self.add(TextEdit::singleline(text))
}
pub fn text_edit_multiline(&mut self, text: &mut String) -> Response {
self.add(TextEdit::multiline(text))
}
#[must_use = "You should check if the user clicked this with `if ui.button(...).clicked { ... } "]
pub fn button(&mut self, text: impl Into<String>) -> Response {
self.add(Button::new(text))
}
#[must_use = "You should check if the user clicked this with `if ui.small_button(...).clicked { ... } "]
pub fn small_button(&mut self, text: impl Into<String>) -> Response {
self.add(Button::new(text).small())
}
pub fn checkbox(&mut self, checked: &mut bool, text: impl Into<String>) -> Response {
self.add(Checkbox::new(checked, text))
}
pub fn radio(&mut self, selected: bool, text: impl Into<String>) -> Response {
self.add(RadioButton::new(selected, text))
}
pub fn radio_value<Value: PartialEq>(
&mut self,
current_value: &mut Value,
selected_value: Value,
text: impl Into<String>,
) -> Response {
let response = self.radio(*current_value == selected_value, text);
if response.clicked {
*current_value = selected_value;
}
response
}
pub fn selectable_label(&mut self, checked: bool, text: impl Into<String>) -> Response {
self.add(SelectableLabel::new(checked, text))
}
pub fn selectable_value<Value: PartialEq>(
&mut self,
current_value: &mut Value,
selected_value: Value,
text: impl Into<String>,
) -> Response {
let response = self.selectable_label(*current_value == selected_value, text);
if response.clicked {
*current_value = selected_value;
}
response
}
pub fn separator(&mut self) -> Response {
self.add(Separator::new())
}
pub fn drag_angle(&mut self, radians: &mut f32) -> Response {
#![allow(clippy::float_cmp)]
let mut degrees = radians.to_degrees();
let response = self.add(DragValue::f32(&mut degrees).speed(1.0).suffix("°"));
if degrees != radians.to_degrees() {
*radians = degrees.to_radians();
}
response
}
pub fn drag_angle_tau(&mut self, radians: &mut f32) -> Response {
#![allow(clippy::float_cmp)]
use std::f32::consts::TAU;
let mut taus = *radians / TAU;
let response = self
.add(DragValue::f32(&mut taus).speed(0.01).suffix("τ"))
.on_hover_text("1τ = one turn, 0.5τ = half a turn, etc. 0.25τ = 90°");
if taus != *radians / TAU {
*radians = taus * TAU;
}
response
}
pub fn image(&mut self, texture_id: TextureId, desired_size: impl Into<Vec2>) -> Response {
self.add(Image::new(texture_id, desired_size))
}
}
impl Ui {
pub fn color_edit_button_srgba(&mut self, srgba: &mut Color32) -> Response {
color_picker::color_edit_button_srgba(self, srgba, color_picker::Alpha::BlendOrAdditive)
}
pub fn color_edit_button_hsva(&mut self, hsva: &mut Hsva) -> Response {
color_picker::color_edit_button_hsva(self, hsva, color_picker::Alpha::BlendOrAdditive)
}
pub fn color_edit_button_srgb(&mut self, srgb: &mut [u8; 3]) -> Response {
let mut hsva = Hsva::from_srgb(*srgb);
let response =
color_picker::color_edit_button_hsva(self, &mut hsva, color_picker::Alpha::Opaque);
*srgb = hsva.to_srgb();
response
}
pub fn color_edit_button_rgb(&mut self, rgb: &mut [f32; 3]) -> Response {
let mut hsva = Hsva::from_rgb(*rgb);
let response =
color_picker::color_edit_button_hsva(self, &mut hsva, color_picker::Alpha::Opaque);
*rgb = hsva.to_rgb();
response
}
pub fn color_edit_button_srgba_premultiplied(&mut self, srgba: &mut [u8; 4]) -> Response {
let mut color = Color32(*srgba);
let response = self.color_edit_button_srgba(&mut color);
*srgba = color.0;
response
}
pub fn color_edit_button_srgba_unmultiplied(&mut self, srgba: &mut [u8; 4]) -> Response {
let mut hsva = Hsva::from_srgba_unmultiplied(*srgba);
let response =
color_picker::color_edit_button_hsva(self, &mut hsva, color_picker::Alpha::OnlyBlend);
*srgba = hsva.to_srgba_unmultiplied();
response
}
pub fn color_edit_button_rgba_premultiplied(&mut self, rgba: &mut [f32; 4]) -> Response {
let mut hsva = Hsva::from_rgba_premultiplied(*rgba);
let response = color_picker::color_edit_button_hsva(
self,
&mut hsva,
color_picker::Alpha::BlendOrAdditive,
);
*rgba = hsva.to_rgba_premultiplied();
response
}
pub fn color_edit_button_rgba_unmultiplied(&mut self, rgba: &mut [f32; 4]) -> Response {
let mut hsva = Hsva::from_rgba_unmultiplied(*rgba);
let response =
color_picker::color_edit_button_hsva(self, &mut hsva, color_picker::Alpha::OnlyBlend);
*rgba = hsva.to_rgba_unmultiplied();
response
}
}
impl Ui {
pub fn wrap<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
let child_rect = self.available_rect_before_wrap();
let mut child_ui = self.child_ui(child_rect, self.layout);
let ret = add_contents(&mut child_ui);
let size = child_ui.min_size();
let response = self.allocate_response(size, Sense::hover());
(ret, response)
}
pub fn with_layer_id<R>(
&mut self,
layer_id: LayerId,
add_contents: impl FnOnce(&mut Self) -> R,
) -> (R, Response) {
self.wrap(|ui| {
ui.painter.set_layer_id(layer_id);
add_contents(ui)
})
}
#[deprecated = "Use `ui.allocate_ui` instead"]
pub fn add_custom_contents(
&mut self,
desired_size: Vec2,
add_contents: impl FnOnce(&mut Ui),
) -> Rect {
self.allocate_ui(desired_size, add_contents).1.rect
}
pub fn collapsing<R>(
&mut self,
heading: impl Into<String>,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> CollapsingResponse<R> {
CollapsingHeader::new(heading).show(self, add_contents)
}
pub fn indent<R>(
&mut self,
id_source: impl Hash,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
assert!(
self.layout.is_vertical(),
"You can only indent vertical layouts, found {:?}",
self.layout
);
let indent = vec2(self.style().spacing.indent, 0.0);
let child_rect =
Rect::from_min_max(self.region.cursor + indent, self.max_rect().right_bottom());
let mut child_ui = Self {
id: self.id.with(id_source),
..self.child_ui(child_rect, self.layout)
};
let ret = add_contents(&mut child_ui);
let size = child_ui.min_size();
let line_start = child_rect.min - indent * 0.5;
let line_start = self.painter().round_pos_to_pixels(line_start);
let line_end = pos2(line_start.x, line_start.y + size.y - 2.0);
self.painter.line_segment(
[line_start, line_end],
self.style().visuals.widgets.noninteractive.bg_stroke,
);
let response = self.allocate_response(indent + size, Sense::hover());
(ret, response)
}
#[deprecated]
pub fn left_column(&mut self, width: f32) -> Self {
#[allow(deprecated)]
self.column(Align::Min, width)
}
#[deprecated]
pub fn centered_column(&mut self, width: f32) -> Self {
#[allow(deprecated)]
self.column(Align::Center, width)
}
#[deprecated]
pub fn right_column(&mut self, width: f32) -> Self {
#[allow(deprecated)]
self.column(Align::Max, width)
}
#[deprecated]
pub fn column(&mut self, column_position: Align, width: f32) -> Self {
let x = match column_position {
Align::Min => 0.0,
Align::Center => self.available_width() / 2.0 - width / 2.0,
Align::Max => self.available_width() - width,
};
self.child_ui(
Rect::from_min_size(
self.region.cursor + vec2(x, 0.0),
vec2(width, self.available_size_before_wrap().y),
),
self.layout,
)
}
pub fn horizontal<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
self.horizontal_with_main_wrap(false, add_contents)
}
pub fn horizontal_for_text<R>(
&mut self,
text_style: TextStyle,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
self.wrap(|ui| {
let font = &ui.fonts()[text_style];
let row_height = font.row_height();
let space_width = font.glyph_width(' ');
let style = ui.style_mut();
style.spacing.interact_size.y = row_height;
style.spacing.item_spacing.x = space_width;
style.spacing.item_spacing.y = 0.0;
ui.horizontal(add_contents).0
})
}
pub fn horizontal_wrapped<R>(
&mut self,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
self.horizontal_with_main_wrap(true, add_contents)
}
pub fn horizontal_wrapped_for_text<R>(
&mut self,
text_style: TextStyle,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
self.wrap(|ui| {
let font = &ui.fonts()[text_style];
let row_height = font.row_height();
let space_width = font.glyph_width(' ');
let style = ui.style_mut();
style.spacing.interact_size.y = row_height;
style.spacing.item_spacing.x = space_width;
style.spacing.item_spacing.y = 0.0;
ui.horizontal_wrapped(add_contents).0
})
}
fn horizontal_with_main_wrap<R>(
&mut self,
main_wrap: bool,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
let initial_size = vec2(
self.available_size_before_wrap_finite().x,
self.style().spacing.interact_size.y,
);
let layout = if self.layout.prefer_right_to_left() {
Layout::right_to_left()
} else {
Layout::left_to_right()
}
.with_main_wrap(main_wrap);
self.allocate_ui(initial_size, |ui| ui.with_layout(layout, add_contents).0)
}
pub fn vertical<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
self.with_layout(Layout::top_down(Align::Min), add_contents)
}
pub fn vertical_centered<R>(
&mut self,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
self.with_layout(Layout::top_down(Align::Center), add_contents)
}
pub fn vertical_centered_justified<R>(
&mut self,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
self.with_layout(
Layout::top_down(Align::Center).with_cross_justify(true),
add_contents,
)
}
pub fn with_layout<R>(
&mut self,
layout: Layout,
add_contents: impl FnOnce(&mut Self) -> R,
) -> (R, Response) {
let mut child_ui = self.child_ui(self.available_rect_before_wrap(), layout);
let ret = add_contents(&mut child_ui);
let rect = child_ui.min_rect();
let item_spacing = self.style().spacing.item_spacing;
self.layout
.advance_after_outer_rect(&mut self.region, rect, rect, item_spacing);
self.region.expand_to_include_rect(rect);
(ret, self.interact(rect, child_ui.id, Sense::hover()))
}
pub fn columns<F, R>(&mut self, num_columns: usize, add_contents: F) -> R
where
F: FnOnce(&mut [Self]) -> R,
{
let spacing = self.style().spacing.item_spacing.x;
let total_spacing = spacing * (num_columns as f32 - 1.0);
let column_width = (self.available_width() - total_spacing) / (num_columns as f32);
let top_left = self.region.cursor;
let mut columns: Vec<Self> = (0..num_columns)
.map(|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()));
column_ui.set_width(column_width);
column_ui
})
.collect();
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);
}
let total_required_width = total_spacing + max_column_width * (num_columns 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
}
}
impl Ui {
pub fn debug_paint_cursor(&self) {
self.layout.debug_paint_cursor(&self.region, &self.painter);
}
}