Skip to content

Commit

Permalink
Add Separator::grow and Separator::shrink
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Feb 3, 2023
1 parent 8c3d8b3 commit 55589a1
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions crates/egui/src/widgets/separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,35 @@ 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 {
/// How much space we take up. The line is painted in the middle of this.
///
/// In a vertical layout, with a horizontal Separator,
/// this is the height of the separator widget.
///
/// In a horizontal layout, with a vertical Separator,
/// this is the width of the separator widget.
pub fn spacing(mut self, spacing: f32) -> Self {
self.spacing = spacing;
self
}

/// Explicitly ask for a horizontal line.
///
/// By default you will get a horizontal line in vertical layouts,
/// and a vertical line in horizontal layouts.
pub fn horizontal(mut self) -> Self {
Expand All @@ -42,18 +51,40 @@ impl Separator {
}

/// Explicitly ask for a vertical line.
///
/// By default you will get a horizontal line in vertical layouts,
/// and a vertical line in horizontal layouts.
pub fn vertical(mut self) -> Self {
self.is_horizontal_line = Some(false);
self
}

/// Extend each end of the separator line by this much.
///
/// The default is to take up the available width/height of the parent.
///
/// This will make the line extend outside the parent ui.
pub fn grow(mut self, extra: f32) -> Self {
self.grow += extra;
self
}

/// Contract each end of the separator line by this much.
///
/// The default is to take up the available width/height of the parent.
///
/// This effectively adds margins to the line.
pub fn shrink(mut self, shrink: f32) -> Self {
self.grow -= shrink;
self
}
}

impl Widget for Separator {
fn ui(self, ui: &mut Ui) -> Response {
let Separator {
spacing,
grow,
is_horizontal_line,
} = self;

Expand All @@ -75,14 +106,14 @@ impl Widget for Separator {
let painter = ui.painter();
if is_horizontal_line {
painter.hline(
rect.x_range(),
(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.y_range(),
(rect.top() - grow)..=(rect.bottom() + grow),
stroke,
);
}
Expand Down

0 comments on commit 55589a1

Please sign in to comment.