pub struct StyledSpacer;
impl Default for StyledSpacer {
fn default() -> Self {
Self::new()
}
}
impl StyledSpacer {
pub fn new() -> Self {
StyledSpacer
}
pub fn show(self, ui: &mut egui::Ui) -> egui::Response {
let space = if ui.layout().is_horizontal() {
ui.available_width()
} else {
ui.available_height()
};
if space.is_finite() && space > 0.0 {
ui.add_space(space);
}
ui.response()
}
}
crate::impl_styled_widget!(StyledSpacer);
#[cfg(test)]
mod tests {
use super::*;
fn screen(w: f32, h: f32) -> egui::RawInput {
egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(
egui::Pos2::ZERO,
egui::vec2(w, h),
)),
..Default::default()
}
}
#[test]
fn spacer_pushes_sibling_to_right_edge() {
let ctx = egui::Context::default();
let mut a_rect = egui::Rect::NOTHING;
let mut b_rect = egui::Rect::NOTHING;
let screen_w = 400.0;
let _ = ctx.run_ui(screen(screen_w, 400.0), |ui| {
crate::StyledRow::new().full_width().show(ui, |ui| {
a_rect = ui.label("A").rect;
StyledSpacer::new().show(ui);
b_rect = ui.label("B").rect;
});
});
assert!(
b_rect.right() >= screen_w - 2.0,
"B right={} should be near screen right={screen_w}",
b_rect.right()
);
assert!(
a_rect.right() < b_rect.left(),
"A ({}) should be left of B ({})",
a_rect.right(),
b_rect.left()
);
}
#[test]
fn spacer_pushes_sibling_to_bottom_in_column() {
let ctx = egui::Context::default();
let screen_h = 300.0;
let mut a_rect = egui::Rect::NOTHING;
let mut b_rect = egui::Rect::NOTHING;
let _ = ctx.run_ui(screen(400.0, screen_h), |ui| {
crate::StyledColumn::new().full_height().show(ui, |ui| {
a_rect = ui.label("A").rect;
StyledSpacer::new().show(ui);
b_rect = ui.label("B").rect;
});
});
assert!(
b_rect.bottom() >= screen_h - 2.0,
"B bottom={} should be near screen bottom={screen_h}",
b_rect.bottom()
);
assert!(
a_rect.bottom() < b_rect.top(),
"A ({}) should be above B ({})",
a_rect.bottom(),
b_rect.top()
);
}
#[test]
fn styled_bg_row_stays_content_height_in_tall_parent() {
let ctx = egui::Context::default();
let screen_h = 600.0;
let mut h = 0.0f32;
let _ = ctx.run_ui(screen(640.0, screen_h), |ui| {
let r = crate::StyledRow::new()
.full_width()
.bg(egui::Color32::RED)
.padding(10.0)
.show(ui, |ui| {
ui.label("Left");
StyledSpacer::new().show(ui);
ui.label("Right");
});
h = r.response.rect.height();
});
assert!(
h < 100.0,
"styled bg row in a 600px parent should be ~one line tall, got {h}"
);
}
#[test]
fn spacer_in_row_zero_space_left_is_noop() {
let ctx = egui::Context::default();
let _ = ctx.run_ui(screen(400.0, 400.0), |ui| {
crate::StyledRow::new().full_width().show(ui, |ui| {
ui.set_min_width(ui.available_width());
StyledSpacer::new().show(ui);
});
});
}
}