armas_basic/components/
separator.rs1use crate::ext::ArmasContextExt;
6use egui::{Response, Ui, Vec2};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum SeparatorOrientation {
11 #[default]
12 Horizontal,
14 Vertical,
16}
17
18pub struct Separator {
35 orientation: SeparatorOrientation,
36 length: Option<f32>,
37}
38
39impl Separator {
40 #[must_use]
42 pub const fn new() -> Self {
43 Self {
44 orientation: SeparatorOrientation::Horizontal,
45 length: None,
46 }
47 }
48
49 #[must_use]
51 pub const fn horizontal(mut self) -> Self {
52 self.orientation = SeparatorOrientation::Horizontal;
53 self
54 }
55
56 #[must_use]
58 pub const fn vertical(mut self) -> Self {
59 self.orientation = SeparatorOrientation::Vertical;
60 self
61 }
62
63 #[must_use]
65 pub const fn length(mut self, length: f32) -> Self {
66 self.length = Some(length);
67 self
68 }
69
70 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}