1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use egui::{vec2, Response, Sense, Ui};

use super::common::{set_common_props, CommonProps};

pub static PADDING_LEFT: f32 = 4.0;

#[derive(Default)]
pub struct TableHeader<'a> {
    common_props: Option<CommonProps>,
    children: Option<Box<dyn FnMut(&mut Ui) + 'a>>,
}

impl<'a> TableHeader<'a> {
    pub fn common_props(mut self, v: CommonProps) -> Self {
        self.common_props = Some(v);
        self
    }

    pub fn children(mut self, v: impl FnMut(&mut Ui) + 'a) -> Self {
        self.children = Some(Box::new(v));
        self
    }

    pub fn show(self, ui: &mut Ui) -> Response {
        ui.horizontal(|ui| {
            set_common_props(ui, &self.common_props);
            let available_space = ui.available_size_before_wrap();
            let size = vec2(PADDING_LEFT, available_space.y);
            let (rect, response) = ui.allocate_at_least(size, Sense::hover());
            if ui.is_rect_visible(response.rect) {
                let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
                let painter = ui.painter();
                painter.vline(rect.left(), rect.top()..=rect.bottom(), stroke);
            }

            (self.children.unwrap().as_mut())(ui)
        })
        .response
    }
}