Skip to main content

libsurfer/
server_file_window.rs

1use egui::{Context, Key, ScrollArea, TextWrapMode, Window};
2
3use crate::{SystemState, message::Message, wave_source::LoadOptions};
4
5impl SystemState {
6    pub fn draw_surver_file_window(&self, ctx: &Context, msgs: &mut Vec<Message>) {
7        let mut open = true;
8        let mut selected_file_idx = *self.surver_selected_file.borrow();
9        let mut load_options = *self.surver_load_options.borrow();
10        let mut should_load = false;
11
12        Window::new("Select wave file")
13            .resizable(true)
14            .open(&mut open)
15            .show(ctx, |ui| {
16                ScrollArea::both().id_salt("file_list").show(ui, |ui| {
17                    ui.vertical(|ui| {
18                        ui.style_mut().wrap_mode = Some(TextWrapMode::Extend);
19                        if let Some(file_infos) = self.user.surver_file_infos.as_ref() {
20                            for (i, file_info) in file_infos.iter().enumerate() {
21                                // Only make item selectable if last_load_ok is true
22                                ui.add_enabled_ui(file_info.last_load_ok, |ui| {
23                                    let response = ui
24                                        .selectable_label(
25                                            Some(i) == selected_file_idx && file_info.last_load_ok,
26                                            &file_info.filename,
27                                        )
28                                        .on_hover_ui(|ui| {
29                                            ui.set_max_width(ui.spacing().tooltip_width);
30                                            if file_info.last_load_ok {
31                                                ui.label(format!(
32                                                    "Size: {} bytes",
33                                                    file_info.bytes
34                                                ));
35                                            } else {
36                                                ui.colored_label(
37                                                    egui::Color32::RED,
38                                                    "File cannot be loaded. See logs for details.",
39                                                );
40                                            }
41                                        });
42
43                                    // Handle single click to select
44                                    if response.clicked() && file_info.last_load_ok {
45                                        selected_file_idx = Some(i);
46                                        *self.surver_selected_file.borrow_mut() = Some(i);
47                                    }
48
49                                    // Handle double-click to select and load
50                                    if response.double_clicked() && file_info.last_load_ok {
51                                        selected_file_idx = Some(i);
52                                        *self.surver_selected_file.borrow_mut() = Some(i);
53                                        should_load = true;
54                                    }
55                                });
56                            }
57                        }
58                    });
59                });
60
61                // Handle keyboard navigation
62                if ui.input(|i| i.key_pressed(Key::Escape)) {
63                    msgs.push(Message::SetSurverFileWindowVisible(false));
64                    return;
65                }
66
67                if ui.input(|i| i.key_pressed(Key::Enter)) && selected_file_idx.is_some() {
68                    should_load = true;
69                }
70
71                if self.user.selected_server_file_index.is_some() {
72                    ui.separator();
73                    ui.radio_value(
74                        &mut load_options,
75                        LoadOptions::Clear,
76                        "Clean load (do not keep any variables)",
77                    );
78                    ui.radio_value(
79                        &mut load_options,
80                        LoadOptions::KeepAvailable,
81                        "Reload and keep available variables",
82                    );
83                    ui.radio_value(
84                        &mut load_options,
85                        LoadOptions::KeepAll,
86                        "Reload and keep all variables",
87                    );
88                }
89
90                ui.separator();
91                ui.horizontal(|ui| {
92                    if ui.button("Cancel").clicked() {
93                        msgs.push(Message::SetSurverFileWindowVisible(false));
94                    }
95
96                    // Disable Select button when nothing is selected
97                    ui.add_enabled_ui(selected_file_idx.is_some(), |ui| {
98                        if ui.button("Select").clicked() {
99                            should_load = true;
100                        }
101                    });
102                });
103            });
104
105        // Handle file loading
106        if should_load && let Some(file_idx) = selected_file_idx {
107            msgs.push(Message::SetSurverFileWindowVisible(false));
108            msgs.push(Message::LoadSurverFileByIndex(
109                Some(file_idx),
110                if self.user.selected_server_file_index.is_some() {
111                    load_options
112                } else {
113                    LoadOptions::Clear
114                },
115            ));
116        }
117
118        if !open {
119            msgs.push(Message::SetSurverFileWindowVisible(false));
120        }
121        *self.surver_load_options.borrow_mut() = load_options;
122    }
123}