Skip to main content

egui_components/
separator.rs

1//! `Separator` widget. Horizontal or vertical thin line that follows
2//! the theme's border color.
3
4use egui::{Response, Sense, Ui, Widget};
5use egui_components_theme::Theme;
6
7#[derive(Clone, Copy, PartialEq, Eq)]
8pub enum Orientation {
9    Horizontal,
10    Vertical,
11}
12
13pub struct Separator {
14    orientation: Orientation,
15    thickness: f32,
16    length: Option<f32>,
17}
18
19impl Separator {
20    pub fn horizontal() -> Self {
21        Self { orientation: Orientation::Horizontal, thickness: 1.0, length: None }
22    }
23    pub fn vertical() -> Self {
24        Self { orientation: Orientation::Vertical, thickness: 1.0, length: None }
25    }
26    pub fn length(mut self, l: f32) -> Self {
27        self.length = Some(l);
28        self
29    }
30    pub fn thickness(mut self, t: f32) -> Self {
31        self.thickness = t;
32        self
33    }
34}
35
36impl Widget for Separator {
37    fn ui(self, ui: &mut Ui) -> Response {
38        let theme = Theme::get(ui.ctx());
39        let color = theme.colors.border;
40        let (w, h) = match self.orientation {
41            Orientation::Horizontal => {
42                (self.length.unwrap_or(ui.available_width()), self.thickness)
43            }
44            Orientation::Vertical => {
45                (self.thickness, self.length.unwrap_or(ui.available_height().max(16.0)))
46            }
47        };
48        let (rect, response) = ui.allocate_exact_size(egui::vec2(w, h), Sense::hover());
49        if ui.is_rect_visible(rect) {
50            ui.painter().rect_filled(rect, 0.0, color);
51        }
52        response
53    }
54}