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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::gui::text::{body, button1, tooltip};
use crate::gui::{
center_x, header_body_controls, style_ui, Style, CUSTOM_BLUE, CUSTOM_ORANGE, CUSTOM_RED,
FOREST_GREEN, TEXT_SIZE_DIALOGUE_BODY, TEXT_SIZE_DIALOGUE_TITLE,
};
use crate::server::{Page, Progress, Scheduler, Server, ServerSignal};
use eframe::egui;
use eframe::egui::{Direction, Label, Layout, Pos2, RichText, ScrollArea, Vec2, Window};
use egui_extras::{Size, StripBuilder};
impl Server {
pub(crate) fn show_selection(&mut self, ui: &mut egui::Ui) {
ui.add_enabled_ui(matches!(self.status, Progress::None), |ui| {
header_body_controls(ui, |strip| {
strip.cell(|ui| {
ui.centered_and_justified(|ui| ui.heading(self.task.title()));
});
strip.empty();
strip.strip(|builder| {
center_x(builder, 1520.0, |ui| self.show_selection_blocks(ui));
});
strip.empty();
strip.strip(|builder| self.show_selection_controls(builder));
});
});
if !matches!(self.status, Progress::None) {
self.show_selection_status(ui.ctx());
}
if ui.input().key_pressed(egui::Key::Escape) && !matches!(self.status, Progress::None) {
self.blocks.get_mut(self.active_block.unwrap()).unwrap().1 =
std::mem::replace(&mut self.status, Progress::None);
}
}
fn show_selection_blocks(&mut self, ui: &mut egui::Ui) {
enum Interaction {
None,
StartBlock(usize),
}
let mut interaction = Interaction::None;
let names = self.task.block_labels();
let is_done: Vec<_> = self.blocks.iter().map(|(_, done)| done).collect();
let cols = self.config().blocks_per_row() as usize;
let rows = (names.len() + cols - 1) / cols;
let row_height = 70.0;
let height = (row_height + 10.0) * rows as f32 + 20.0;
let (col_width, col_spacing) = match cols {
1 => (720.0, 0.0),
2 => (600.0, 120.0),
3 => (440.0, 60.0),
_ => (330.0, 40.0),
};
StripBuilder::new(ui)
.size(Size::remainder())
.size(Size::exact(height).at_most(800.0))
.size(Size::remainder())
.vertical(|mut strip| {
strip.empty();
strip.cell(|ui| {
ScrollArea::vertical().show(ui, |ui| {
style_ui(ui, Style::SelectButton);
StripBuilder::new(ui)
.size(Size::remainder())
.sizes(Size::exact(row_height), rows)
.size(Size::remainder())
.vertical(|mut strip| {
strip.empty();
for row in 0..rows {
strip.strip(|mut builder| {
let this_cols = if row < rows - 1 || names.len() % cols == 0
{
cols
} else {
names.len() % cols
};
builder = builder
.size(Size::remainder())
.size(Size::exact(col_width));
for _ in 1..this_cols {
builder = builder
.size(Size::exact(col_spacing))
.size(Size::exact(col_width));
}
builder = builder.size(Size::remainder());
builder.horizontal(|mut strip| {
for j in 0..this_cols {
let which = row * cols + j;
strip.empty();
strip.cell(|ui| {
ui.centered_and_justified(|ui| {
let (style, hint) = match is_done[which] {
Progress::None => {
(Style::TodoButton, None)
}
Progress::Success(t) => (
Style::DoneButton,
Some(tooltip(format!(
"Completed @ {}",
t.format("%Y-%m-%d %H:%M:%S")
))),
),
Progress::Interrupt(t) => (
Style::InterruptedButton,
Some(tooltip(format!(
"Interrupted @ {}",
t.format("%Y-%m-%d %H:%M:%S")
))),
),
Progress::Failure(t, e) => (
Style::FailedButton,
Some(tooltip(format!(
"Failed @ {}\n\n{e}",
t.format("%Y-%m-%d %H:%M:%S")
))),
),
Progress::CleanupError(t, e) => (
Style::SoftFailedButton,
Some(tooltip(format!(
"Failed in clean-up @ {}\n\n{e}",
t.format("%Y-%m-%d %H:%M:%S")
))),
),
Progress::LastRun(t) => (
Style::TodoButton,
Some(tooltip(format!(
"Last run @ {}",
t.format("%Y-%m-%d %H:%M:%S")
))),
),
};
style_ui(ui, style);
let response = if let Some(hint) = hint {
ui.button(&names[which])
.on_hover_text(hint)
} else {
ui.button(&names[which])
};
if response.clicked() {
interaction =
Interaction::StartBlock(which);
}
});
});
}
strip.empty();
});
});
}
strip.empty();
});
});
});
strip.empty();
});
match interaction {
Interaction::None => {}
Interaction::StartBlock(i) => {
if self.scheduler.is_none() {
println!("\nStarting experiment block {i}...");
self.active_block = Some(i);
self.page = Page::Loading;
match Scheduler::new(self, ui.ctx()) {
Ok(scheduler) => self.scheduler = Some(scheduler),
Err(e) => self.sync_reader.push(ServerSignal::BlockCrashed(
e.wrap_err("Failed to initialize scheduler."),
)),
}
}
}
}
}
fn show_selection_controls(&mut self, builder: StripBuilder) {
enum Interaction {
None,
Back,
}
let mut interaction = Interaction::None;
center_x(builder, 200.0, |ui| {
ui.horizontal_centered(|ui| {
style_ui(ui, Style::CancelButton);
if ui.button(button1("Back")).clicked() {
interaction = Interaction::Back;
}
});
});
match interaction {
Interaction::None => {}
Interaction::Back => self.page = Page::Startup,
}
}
fn show_selection_status(&mut self, ctx: &egui::Context) {
let header = body(self.active_block.map_or("", |i| &self.blocks[i].0)).strong();
let content = match &self.status {
Progress::None => None,
Progress::Success(_) => Some(body("Block completed successfully!").color(FOREST_GREEN)),
Progress::Interrupt(_) => {
Some(body("Block was interrupted by user.").color(CUSTOM_BLUE))
}
Progress::Failure(_, e) => {
Some(body(format!("\nError in block execution:\n\n{e:?}\n")).color(CUSTOM_RED))
}
Progress::CleanupError(_, e) => Some(
body(format!("\nFailed to clean up after block:\n\n{e:?}\n")).color(CUSTOM_ORANGE),
),
Progress::LastRun(_) => None,
};
if let Some(content) = content {
let mut open = true;
Window::new(header.size(TEXT_SIZE_DIALOGUE_TITLE))
.collapsible(false)
.open(&mut open)
.vscroll(true)
.hscroll(false)
.min_width(920.0)
.fixed_size(Vec2::new(920.0, 360.0))
.fixed_pos(Pos2::new(500.0, 280.0))
.show(ctx, |ui| {
ui.with_layout(
Layout::centered_and_justified(Direction::LeftToRight),
|ui| {
ui.add_sized(
[760.0, 340.0],
Label::new(content.clone().size(TEXT_SIZE_DIALOGUE_BODY)),
);
},
)
.response
.context_menu(|ui| {
if ui
.button(RichText::new("Copy").size(TEXT_SIZE_DIALOGUE_BODY))
.clicked()
{
ui.close_menu();
ui.output().copied_text = content.text().trim().to_owned();
}
});
});
if !open {
self.blocks.get_mut(self.active_block.unwrap()).unwrap().1 =
std::mem::replace(&mut self.status, Progress::None);
self.active_block = None;
}
}
}
}