Skip to main content

armas_basic/components/
separator.rs

1//! Separator Component (shadcn/ui style)
2//!
3//! Simple horizontal or vertical divider line.
4
5use crate::ext::ArmasContextExt;
6use egui::{Response, Ui, Vec2};
7
8/// Separator orientation
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum SeparatorOrientation {
11    #[default]
12    /// Horizontal separator
13    Horizontal,
14    /// Vertical separator
15    Vertical,
16}
17
18/// Simple separator/divider component
19///
20/// # Example
21///
22/// ```rust,no_run
23/// # use egui::Ui;
24/// # fn example(ui: &mut Ui) {
25/// use armas_basic::Separator;
26///
27/// // Horizontal separator (default)
28/// Separator::new().show(ui);
29///
30/// // Vertical separator
31/// Separator::new().vertical().show(ui);
32/// # }
33/// ```
34pub struct Separator {
35    orientation: SeparatorOrientation,
36    length: Option<f32>,
37}
38
39impl Separator {
40    /// Create a new horizontal separator
41    #[must_use]
42    pub const fn new() -> Self {
43        Self {
44            orientation: SeparatorOrientation::Horizontal,
45            length: None,
46        }
47    }
48
49    /// Set horizontal orientation
50    #[must_use]
51    pub const fn horizontal(mut self) -> Self {
52        self.orientation = SeparatorOrientation::Horizontal;
53        self
54    }
55
56    /// Set vertical orientation
57    #[must_use]
58    pub const fn vertical(mut self) -> Self {
59        self.orientation = SeparatorOrientation::Vertical;
60        self
61    }
62
63    /// Set custom length (width for horizontal, height for vertical)
64    #[must_use]
65    pub const fn length(mut self, length: f32) -> Self {
66        self.length = Some(length);
67        self
68    }
69
70    /// Show the separator
71    pub fn show(self, ui: &mut Ui) -> Response {
72        let theme = ui.ctx().armas_theme();
73        let color = theme.border();
74
75        let size = match self.orientation {
76            SeparatorOrientation::Horizontal => {
77                let width = self.length.unwrap_or_else(|| ui.available_width());
78                Vec2::new(width, 1.0)
79            }
80            SeparatorOrientation::Vertical => {
81                let height = self.length.unwrap_or_else(|| ui.available_height());
82                Vec2::new(1.0, height)
83            }
84        };
85
86        let (rect, response) = ui.allocate_exact_size(size, egui::Sense::hover());
87
88        if ui.is_rect_visible(rect) {
89            ui.painter().rect_filled(rect, 0.0, color);
90        }
91
92        response
93    }
94}
95
96impl Default for Separator {
97    fn default() -> Self {
98        Self::new()
99    }
100}