Skip to main content

egui_charts/ui/drawing_toolbar/components/
separator.rs

1//! Separator component for visual dividers in the toolbar.
2
3use crate::theme::ThemeExt;
4use crate::tokens::DESIGN_TOKENS;
5use egui::{Color32, Rect, Sense, Ui, Vec2};
6
7/// Draw a horizontal separator line.
8///
9/// Creates a thin horizontal line to visually separate toolbar sections.
10/// Uses theme-aware coloring.
11///
12/// # Arguments
13///
14/// * `ui` - The egui UI context
15/// * `width` - Total width of the separator area
16pub fn draw_separator(ui: &mut Ui, width: f32) {
17    let separator_height = 1.0;
18    let (rect, _) = ui.allocate_exact_size(Vec2::new(width, separator_height), Sense::hover());
19    ui.painter().rect_filled(
20        rect,
21        0.0,
22        ui.visuals().widgets.noninteractive.bg_stroke.color,
23    );
24}
25
26/// Draw a separator with horizontal margins.
27///
28/// Same as `draw_separator` but with configurable left/right margins.
29///
30/// # Arguments
31///
32/// * `ui` - The egui UI context
33/// * `width` - Total width of the separator area
34/// * `margin` - Horizontal margin on each side
35pub fn draw_separator_with_margin(ui: &mut Ui, width: f32, margin: f32) {
36    let sep_rect = Rect::from_min_size(
37        ui.cursor().min + Vec2::new(margin, 0.0),
38        Vec2::new(width - margin * 2.0, 1.0),
39    );
40    ui.painter().rect_filled(
41        sep_rect,
42        0.0,
43        ui.visuals().widgets.noninteractive.bg_stroke.color,
44    );
45    ui.add_space(DESIGN_TOKENS.stroke.hairline);
46}
47
48/// Left inset of the drawing toolbar's group separators, in logical points.
49const LEFT_INSET: f32 = 9.0;
50/// Right inset of the drawing toolbar's group separators, in logical points.
51const RIGHT_INSET: f32 = 10.0;
52
53/// Draw a styled separator for the drawing toolbar.
54///
55/// Creates a thin separator line with proper margins and a `spacing.lg` gap above and below.
56/// At the default width it is 33px wide and 1px high, inset 9px left and 10px right.
57///
58/// # Arguments
59///
60/// * `ui` - The egui UI context
61/// * `toolbar_width` - Total width of the toolbar (e.g., 52px)
62pub fn draw_separator_styled(ui: &mut Ui, toolbar_width: f32) {
63    let gap = ui.active_theme().spacing.lg;
64
65    // Add gap above
66    ui.add_space(gap);
67
68    // Draw 1px separator with margins
69    let sep_width = toolbar_width - LEFT_INSET - RIGHT_INSET;
70    let sep_rect = Rect::from_min_size(
71        ui.cursor().min + Vec2::new(LEFT_INSET, 0.0),
72        Vec2::new(sep_width, 1.0),
73    );
74    let color = if ui.active_theme().dark {
75        Color32::from_gray(74)
76    } else {
77        Color32::from_gray(235)
78    };
79    ui.painter().rect_filled(sep_rect, 0.0, color);
80
81    // Allocate space for the separator line
82    ui.add_space(1.0);
83
84    // Add gap below
85    ui.add_space(gap);
86}