use crate::*;
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
pub struct Separator {
spacing: f32,
grow: f32,
is_horizontal_line: Option<bool>,
}
impl Default for Separator {
fn default() -> Self {
Self {
spacing: 6.0,
grow: 0.0,
is_horizontal_line: None,
}
}
}
impl Separator {
#[inline]
pub fn spacing(mut self, spacing: f32) -> Self {
self.spacing = spacing;
self
}
#[inline]
pub fn horizontal(mut self) -> Self {
self.is_horizontal_line = Some(true);
self
}
#[inline]
pub fn vertical(mut self) -> Self {
self.is_horizontal_line = Some(false);
self
}
#[inline]
pub fn grow(mut self, extra: f32) -> Self {
self.grow += extra;
self
}
#[inline]
pub fn shrink(mut self, shrink: f32) -> Self {
self.grow -= shrink;
self
}
}
impl Widget for Separator {
fn ui(self, ui: &mut Ui) -> Response {
let Self {
spacing,
grow,
is_horizontal_line,
} = self;
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();
let size = if is_horizontal_line {
vec2(available_space.x, spacing)
} else {
vec2(spacing, available_space.y)
};
let (rect, response) = ui.allocate_at_least(size, Sense::hover());
if ui.is_rect_visible(response.rect) {
let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
let painter = ui.painter();
if is_horizontal_line {
painter.hline(
(rect.left() - grow)..=(rect.right() + grow),
painter.round_to_pixel(rect.center().y),
stroke,
);
} else {
painter.vline(
painter.round_to_pixel(rect.center().x),
(rect.top() - grow)..=(rect.bottom() + grow),
stroke,
);
}
}
response
}
}