libsurfer/
dialog.rs

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