egui/widgets/
separator.rs1use crate::{Response, Sense, Ui, Vec2, Widget, vec2, widget_style::SeparatorStyle};
2
3#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
15pub struct Separator {
16 spacing: Option<f32>,
17 grow: f32,
18 is_horizontal_line: Option<bool>,
19}
20
21impl Default for Separator {
22 fn default() -> Self {
23 Self {
24 spacing: None,
25 grow: 0.0,
26 is_horizontal_line: None,
27 }
28 }
29}
30
31impl Separator {
32 #[inline]
40 pub fn spacing(mut self, spacing: f32) -> Self {
41 self.spacing = Some(spacing);
42 self
43 }
44
45 #[inline]
50 pub fn horizontal(mut self) -> Self {
51 self.is_horizontal_line = Some(true);
52 self
53 }
54
55 #[inline]
60 pub fn vertical(mut self) -> Self {
61 self.is_horizontal_line = Some(false);
62 self
63 }
64
65 #[inline]
71 pub fn grow(mut self, extra: f32) -> Self {
72 self.grow += extra;
73 self
74 }
75
76 #[inline]
82 pub fn shrink(mut self, shrink: f32) -> Self {
83 self.grow -= shrink;
84 self
85 }
86}
87
88impl Widget for Separator {
89 fn ui(self, ui: &mut Ui) -> Response {
90 let Self {
91 spacing,
92 grow,
93 is_horizontal_line,
94 } = self;
95
96 let id = ui.next_auto_id();
98 let response: Option<Response> = ui.ctx().read_response(id);
99 let state = response.map(|r| r.widget_state()).unwrap_or_default();
100 let SeparatorStyle {
101 spacing: spacing_style,
102 stroke,
103 } = ui.style().separator_style(state);
104
105 let spacing = spacing.unwrap_or(spacing_style);
107
108 let is_horizontal_line = is_horizontal_line
109 .unwrap_or_else(|| ui.is_grid() || !ui.layout().main_dir().is_horizontal());
110
111 let available_space = if ui.is_sizing_pass() {
112 Vec2::ZERO
113 } else {
114 ui.available_size_before_wrap()
115 };
116
117 let size = if is_horizontal_line {
118 vec2(available_space.x, spacing)
119 } else {
120 vec2(spacing, available_space.y)
121 };
122
123 let (rect, response) = ui.allocate_at_least(size, Sense::hover());
124
125 if ui.is_rect_visible(response.rect) {
126 let painter = ui.painter();
127 if is_horizontal_line {
128 painter.hline(
129 (rect.left() - grow)..=(rect.right() + grow),
130 rect.center().y,
131 stroke,
132 );
133 } else {
134 painter.vline(
135 rect.center().x,
136 (rect.top() - grow)..=(rect.bottom() + grow),
137 stroke,
138 );
139 }
140 }
141
142 response
143 }
144}