aboutsummaryrefslogtreecommitdiff
path: root/src/core/components/help.rs
blob: fb7ede889de54faa47761603df06d50fd76eff6a (plain)
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
use eframe::egui;
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};

#[derive(Clone, Copy)]
pub struct HelpWindowOptions {
    pub min_size: egui::Vec2,
    pub max_size_factor: f32,
    pub default_width_factor: f32,
    pub default_height_factor: f32,
}

impl Default for HelpWindowOptions {
    fn default() -> Self {
        Self {
            min_size: egui::vec2(320.0, 240.0),
            max_size_factor: 0.9,
            default_width_factor: 0.5,
            default_height_factor: 0.6,
        }
    }
}

pub fn show_help_window(
    ctx: &egui::Context,
    cache: &mut CommonMarkCache,
    id_source: impl std::hash::Hash,
    title: &str,
    markdown_content: &str,
    is_open: &mut bool,
    options: HelpWindowOptions,
) {
    if !*is_open {
        return;
    }

    let viewport = ctx.available_rect();
    let max_size = egui::vec2(
        viewport.width() * options.max_size_factor,
        viewport.height() * options.max_size_factor,
    );
    let default_size = egui::vec2(
        (viewport.width() * options.default_width_factor)
            .clamp(options.min_size.x, max_size.x.max(options.min_size.x)),
        (viewport.height() * options.default_height_factor)
            .clamp(options.min_size.y, max_size.y.max(options.min_size.y)),
    );

    let mut open = *is_open;
    egui::Window::new(title)
        .id(egui::Id::new(id_source))
        .collapsible(false)
        .resizable(true)
        .default_size(default_size)
        .min_size(options.min_size)
        .max_size(max_size)
        .open(&mut open)
        .show(ctx, |ui| {
            egui::ScrollArea::vertical()
                .auto_shrink([false, false])
                .show(ui, |ui| {
                    CommonMarkViewer::new().show(ui, cache, markdown_content);
                });
        });

    *is_open = open;
}