irox_egui_extras/
about.rs

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors
//

use egui::Ui;

pub const IMPORTANT_NAMES: &[(&str, &str)] = &[
    ("CARGO_PKG_NAME", "Package Name"),
    ("CARGO_PKG_DESCRIPTION", "Package Description"),
    ("CARGO_PKG_VERSION", "Package Version"),
    ("CARGO_PKG_REPOSITORY", "Repository"),
    ("GIT_COMMIT_FULLHASH", "Git Commit Hash"),
    ("GIT_COMMIT_DATETIME", "Git Commit Date"),
    ("GIT_COMMIT_AUTHOR", "Git Commit Author"),
    ("GIT_DESCRIBE", "Git Commit Description"),
    ("GIT_IS_CLEAN", "Is clean (not dirty) build"),
    ("PROFILE", "Build Profile"),
    ("BUILD_TIME", "Build Time"),
    ("HOST", "Build Host Platform"),
    ("RUSTUP_TOOLCHAIN", "Build Toolchain"),
    ("RUSTC_VERSION", "Rust Version"),
    ("CARGO_VERSION", "Cargo Version"),
];

pub struct AboutWindow;

impl AboutWindow {
    pub fn show<'a, F: Fn() -> &'a std::collections::BTreeMap<&'a str, &'a str>>(
        providerfn: F,
        ui: &mut Ui,
    ) {
        egui::ScrollArea::vertical().show(ui, |ui| {
            egui::Grid::new("about_grid_display")
                .num_columns(2)
                .striped(true)
                .spacing([40.0, 4.0])
                .show(ui, |ui| {
                    for (k, v) in providerfn() {
                        ui.label(*k);
                        ui.label(*v);
                        ui.end_row();
                    }
                });
        });
    }

    ///
    /// Show only those items listed as 'important' above
    pub fn show_important<'a, F: Fn() -> &'a std::collections::BTreeMap<&'a str, &'a str>>(
        providerfn: F,
        ui: &mut Ui,
    ) {
        let data = providerfn();
        egui::Grid::new("about_grid_display")
            .num_columns(2)
            .striped(true)
            .spacing([40.0, 4.0])
            .show(ui, |ui| {
                let mut repo = Option::<&str>::None;
                for (key, disp) in IMPORTANT_NAMES {
                    if let Some(v) = data.get(key) {
                        ui.label(*disp);
                        match *key {
                            "CARGO_PKG_REPOSITORY" => {
                                ui.hyperlink(*v);
                                if v.contains("github") {
                                    repo = Some(*v);
                                }
                            }
                            "GIT_COMMIT_FULLHASH" => {
                                if let Some(repo) = repo {
                                    ui.hyperlink_to(*v, format!("{repo}/commit/{}", *v));
                                } else {
                                    ui.label(*v);
                                }
                            }
                            _ => {
                                ui.label(*v);
                            }
                        };
                        ui.end_row();
                    }
                }
            });
    }

    pub fn show_grouped<
        F: Fn() -> &'static std::collections::BTreeMap<
            &'static str,
            std::collections::BTreeMap<&'static str, &'static str>,
        >,
    >(
        providerfn: F,
        ui: &mut Ui,
    ) {
        let data = providerfn();
        egui::Grid::new("about_grid_display")
            .num_columns(2)
            .striped(true)
            .spacing([40.0, 4.0])
            .show(ui, |ui| {
                let mut repo = Option::<&str>::None;
                for group in data.values() {
                    for (key, disp) in IMPORTANT_NAMES {
                        if let Some(v) = group.get(key) {
                            ui.label(*disp);
                            match *key {
                                "CARGO_PKG_REPOSITORY" => {
                                    ui.hyperlink(*v);
                                    if v.contains("github") {
                                        repo = Some(*v);
                                    }
                                }
                                "GIT_COMMIT_FULLHASH" => {
                                    if let Some(repo) = repo {
                                        ui.hyperlink_to(*v, format!("{repo}/commit/{}", *v));
                                    } else {
                                        ui.label(*v);
                                    }
                                }
                                _ => {
                                    ui.label(*v);
                                }
                            };
                            ui.end_row();
                        }
                    }
                }
            });
        for (gname, group) in data {
            ui.collapsing(*gname, |ui| {
                AboutWindow::show(|| group, ui);
            });
        }
    }
}