Skip to main content

libsurfer/
dialog.rs

1use crate::message::Message;
2use ecolor::Color32;
3use egui::{Layout, RichText};
4use emath::Align;
5
6#[derive(Debug, Default, Copy, Clone)]
7pub struct ReloadWaveformDialog {
8    /// `true` to persist the setting returned by the dialog.
9    do_not_show_again: bool,
10}
11
12#[derive(Debug, Default, Copy, Clone)]
13pub struct OpenSiblingStateFileDialog {
14    do_not_show_again: bool,
15}
16
17/// Draw a dialog that asks the user if it wants to load a state file situated in the same directory as the waveform file.
18pub(crate) fn draw_open_sibling_state_file_dialog(
19    ctx: &egui::Context,
20    dialog: OpenSiblingStateFileDialog,
21    msgs: &mut Vec<Message>,
22) {
23    let mut do_not_show_again = dialog.do_not_show_again;
24    egui::Window::new("State file detected")
25            .auto_sized()
26            .collapsible(false)
27            .fixed_pos(ctx.available_rect().center())
28            .show(ctx, |ui| {
29                let label = ui.label(RichText::new("A state file was detected in the same directory as the loaded file.\nLoad state?").heading());
30                ui.set_width(label.rect.width());
31                ui.add_space(5.0);
32                ui.checkbox(
33                    &mut do_not_show_again,
34                    "Remember my decision for this session",
35                );
36                ui.add_space(14.0);
37                ui.with_layout(Layout::right_to_left(Align::TOP), |ui| {
38                    // Sets the style when focused
39                    ui.style_mut().visuals.widgets.active.weak_bg_fill = Color32::BLUE;
40                    let load_button = ui.button("Load");
41                    let dont_load_button = ui.button("Don't load");
42                    ctx.memory_mut(|mem| {
43                        if !matches!(mem.focused(), Some(id) if id == load_button.id || id == dont_load_button.id)
44                        {
45                            mem.request_focus(load_button.id);
46                        }
47                    });
48
49                    if load_button.clicked() {
50                        msgs.push(Message::CloseOpenSiblingStateFileDialog {
51                            load_state: true,
52                            do_not_show_again,
53                        });
54                    } else if dont_load_button.clicked() {
55                        msgs.push(Message::CloseOpenSiblingStateFileDialog {
56                            load_state: false,
57                            do_not_show_again,
58                        });
59                    } else if do_not_show_again != dialog.do_not_show_again {
60                        msgs.push(Message::UpdateOpenSiblingStateFileDialog(OpenSiblingStateFileDialog {
61                            do_not_show_again,
62                        }));
63                    }
64                });
65            });
66}
67
68/// Draw a dialog that asks for user confirmation before re-loading a file.
69/// This is triggered by a file loading event from disk.
70pub(crate) fn draw_reload_waveform_dialog(
71    ctx: &egui::Context,
72    dialog: ReloadWaveformDialog,
73    msgs: &mut Vec<Message>,
74) {
75    let mut do_not_show_again = dialog.do_not_show_again;
76    egui::Window::new("File Change")
77        .auto_sized()
78        .collapsible(false)
79        .fixed_pos(ctx.available_rect().center())
80        .show(ctx, |ui| {
81            let label = ui.label(RichText::new("File on disk has changed. Reload?").heading());
82            ui.set_width(label.rect.width());
83            ui.add_space(5.0);
84            ui.checkbox(
85                &mut do_not_show_again,
86                "Remember my decision for this session",
87            );
88            ui.add_space(14.0);
89            ui.with_layout(Layout::right_to_left(Align::TOP), |ui| {
90                // Sets the style when focused
91                ui.style_mut().visuals.widgets.active.weak_bg_fill = Color32::BLUE;
92                let reload_button = ui.button("Reload");
93                let leave_button = ui.button("Leave");
94                ctx.memory_mut(|mem| {
95                    if !matches!(mem.focused(), Some(id) if id == reload_button.id || id == leave_button.id)
96                    {
97                        mem.request_focus(reload_button.id);
98                    }
99                });
100
101                if reload_button.clicked() {
102                    msgs.push(Message::CloseReloadWaveformDialog {
103                        reload_file: true,
104                        do_not_show_again,
105                    });
106                } else if leave_button.clicked() {
107                    msgs.push(Message::CloseReloadWaveformDialog {
108                        reload_file: false,
109                        do_not_show_again,
110                    });
111                } else if do_not_show_again != dialog.do_not_show_again {
112                    msgs.push(Message::UpdateReloadWaveformDialog(ReloadWaveformDialog {
113                        do_not_show_again,
114                    }));
115                }
116            });
117        });
118}