| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use egui; |
| 24 |
|
| 25 |
use super::theme; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
pub enum ConfirmOutcome { |
| 31 |
|
| 32 |
None, |
| 33 |
|
| 34 |
Confirmed, |
| 35 |
|
| 36 |
Cancelled, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
pub enum NameModalOutcome { |
| 41 |
|
| 42 |
None, |
| 43 |
|
| 44 |
Submitted(String), |
| 45 |
|
| 46 |
Cancelled, |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub fn modal_window<R>( |
| 56 |
ctx: &egui::Context, |
| 57 |
title: &str, |
| 58 |
resizable: bool, |
| 59 |
default_width: Option<f32>, |
| 60 |
add_contents: impl FnOnce(&mut egui::Ui) -> R, |
| 61 |
) -> Option<R> { |
| 62 |
modal_window_with_open(ctx, title, None, resizable, default_width, add_contents) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pub fn tool_window<R>( |
| 73 |
ctx: &egui::Context, |
| 74 |
title: &str, |
| 75 |
open: &mut bool, |
| 76 |
default_width: f32, |
| 77 |
min_width: f32, |
| 78 |
add_contents: impl FnOnce(&mut egui::Ui) -> R, |
| 79 |
) -> Option<R> { |
| 80 |
egui::Window::new(title) |
| 81 |
.open(open) |
| 82 |
.resizable(true) |
| 83 |
.collapsible(true) |
| 84 |
.default_width(default_width) |
| 85 |
.min_width(min_width) |
| 86 |
.show(ctx, |ui| add_contents(ui)) |
| 87 |
.and_then(|r| r.inner) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pub fn modal_window_with_open<R>( |
| 93 |
ctx: &egui::Context, |
| 94 |
title: &str, |
| 95 |
open: Option<&mut bool>, |
| 96 |
resizable: bool, |
| 97 |
default_width: Option<f32>, |
| 98 |
add_contents: impl FnOnce(&mut egui::Ui) -> R, |
| 99 |
) -> Option<R> { |
| 100 |
let mut window = egui::Window::new(title) |
| 101 |
.collapsible(false) |
| 102 |
.resizable(resizable) |
| 103 |
.anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0]); |
| 104 |
if let Some(o) = open { |
| 105 |
window = window.open(o); |
| 106 |
} |
| 107 |
if let Some(w) = default_width { |
| 108 |
window = window.default_width(w); |
| 109 |
} |
| 110 |
window.show(ctx, |ui| add_contents(ui)).map(|r| r.inner.unwrap()) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
pub fn confirm_action_row( |
| 122 |
ui: &mut egui::Ui, |
| 123 |
confirm_label: &str, |
| 124 |
can_confirm: bool, |
| 125 |
danger: bool, |
| 126 |
) -> ConfirmOutcome { |
| 127 |
let mut outcome = ConfirmOutcome::None; |
| 128 |
ui.horizontal(|ui| { |
| 129 |
if ui.button("Cancel").clicked() { |
| 130 |
outcome = ConfirmOutcome::Cancelled; |
| 131 |
} |
| 132 |
let label = if danger { |
| 133 |
egui::RichText::new(confirm_label).color(theme::accent_red()) |
| 134 |
} else { |
| 135 |
egui::RichText::new(confirm_label) |
| 136 |
}; |
| 137 |
if ui.add_enabled(can_confirm, egui::Button::new(label)).clicked() { |
| 138 |
outcome = ConfirmOutcome::Confirmed; |
| 139 |
} |
| 140 |
}); |
| 141 |
outcome |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
pub struct ConfirmSpec<'a> { |
| 146 |
pub title: &'a str, |
| 147 |
pub prompt: &'a str, |
| 148 |
pub detail: Option<&'a str>, |
| 149 |
pub confirm_label: &'a str, |
| 150 |
pub danger: bool, |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
pub fn confirm_modal(ctx: &egui::Context, spec: &ConfirmSpec) -> ConfirmOutcome { |
| 157 |
let mut outcome = ConfirmOutcome::None; |
| 158 |
modal_window(ctx, spec.title, false, None, |ui| { |
| 159 |
ui.label(spec.prompt); |
| 160 |
if let Some(detail) = spec.detail { |
| 161 |
ui.add_space(theme::space::SM); |
| 162 |
ui.label( |
| 163 |
egui::RichText::new(detail) |
| 164 |
.small() |
| 165 |
.color(theme::text_secondary()), |
| 166 |
); |
| 167 |
} |
| 168 |
ui.add_space(theme::space::LG); |
| 169 |
outcome = confirm_action_row(ui, spec.confirm_label, true, spec.danger); |
| 170 |
}); |
| 171 |
outcome |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
pub fn name_modal( |
| 182 |
ctx: &egui::Context, |
| 183 |
title: &str, |
| 184 |
hint: Option<&str>, |
| 185 |
label: &str, |
| 186 |
input: &mut String, |
| 187 |
submit_label: &str, |
| 188 |
error: Option<&str>, |
| 189 |
) -> NameModalOutcome { |
| 190 |
let mut outcome = NameModalOutcome::None; |
| 191 |
modal_window(ctx, title, false, None, |ui| { |
| 192 |
if let Some(h) = hint { |
| 193 |
ui.label(egui::RichText::new(h).small().color(theme::text_muted())); |
| 194 |
ui.add_space(theme::space::SM); |
| 195 |
} |
| 196 |
ui.label(label); |
| 197 |
let resp = ui.text_edit_singleline(input); |
| 198 |
if input.is_empty() && ui.memory(|m| m.focused().is_none()) { |
| 199 |
resp.request_focus(); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if let Some(err) = error { |
| 204 |
ui.add_space(theme::space::XS); |
| 205 |
ui.label( |
| 206 |
egui::RichText::new(err) |
| 207 |
.small() |
| 208 |
.color(theme::accent_red()), |
| 209 |
); |
| 210 |
if !resp.has_focus() { |
| 211 |
resp.request_focus(); |
| 212 |
} |
| 213 |
} |
| 214 |
if resp.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) { |
| 215 |
outcome = NameModalOutcome::Submitted(input.trim().to_string()); |
| 216 |
} |
| 217 |
ui.add_space(theme::space::MD); |
| 218 |
ui.horizontal(|ui| { |
| 219 |
if ui.button("Cancel").clicked() { |
| 220 |
outcome = NameModalOutcome::Cancelled; |
| 221 |
} |
| 222 |
if ui.button(submit_label).clicked() { |
| 223 |
outcome = NameModalOutcome::Submitted(input.trim().to_string()); |
| 224 |
} |
| 225 |
}); |
| 226 |
}); |
| 227 |
outcome |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
pub struct EmptyStateCta<'a> { |
| 234 |
pub label: &'a str, |
| 235 |
pub tooltip: Option<&'a str>, |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
pub fn empty_state( |
| 245 |
ui: &mut egui::Ui, |
| 246 |
heading: &str, |
| 247 |
body: Option<&str>, |
| 248 |
cta: Option<EmptyStateCta>, |
| 249 |
) -> bool { |
| 250 |
let mut clicked = false; |
| 251 |
ui.vertical_centered(|ui| { |
| 252 |
ui.add_space(ui.available_height() * 0.15); |
| 253 |
ui.label( |
| 254 |
egui::RichText::new(heading) |
| 255 |
.size(20.0) |
| 256 |
.color(theme::text_secondary()), |
| 257 |
); |
| 258 |
if let Some(body_text) = body { |
| 259 |
ui.add_space(theme::space::MD); |
| 260 |
ui.label(egui::RichText::new(body_text).color(theme::text_muted())); |
| 261 |
} |
| 262 |
if let Some(cta) = cta { |
| 263 |
ui.add_space(theme::space::LG); |
| 264 |
let btn = secondary_button(ui, cta.label); |
| 265 |
let btn = if let Some(t) = cta.tooltip { btn.on_hover_text(t) } else { btn }; |
| 266 |
if btn.clicked() { |
| 267 |
clicked = true; |
| 268 |
} |
| 269 |
} |
| 270 |
}); |
| 271 |
clicked |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
pub fn format_bytes(bytes: u64) -> String { |
| 278 |
if bytes < 1024 { |
| 279 |
format!("{bytes} B") |
| 280 |
} else if bytes < 1024 * 1024 { |
| 281 |
format!("{:.1} KB", bytes as f64 / 1024.0) |
| 282 |
} else if bytes < 1024 * 1024 * 1024 { |
| 283 |
format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0)) |
| 284 |
} else { |
| 285 |
format!("{:.2} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0)) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
pub fn info_banner(ui: &mut egui::Ui, body: &str) { |
| 292 |
egui::Frame::new() |
| 293 |
.fill(theme::bg_tertiary()) |
| 294 |
.corner_radius(egui::CornerRadius::same(4)) |
| 295 |
.inner_margin(egui::Margin::same(8)) |
| 296 |
.show(ui, |ui| { |
| 297 |
ui.label( |
| 298 |
egui::RichText::new(body) |
| 299 |
.small() |
| 300 |
.color(theme::text_secondary()), |
| 301 |
); |
| 302 |
}); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
pub fn warning_banner(ui: &mut egui::Ui, body: &str) { |
| 310 |
egui::Frame::new() |
| 311 |
.fill(theme::bg_tertiary()) |
| 312 |
.corner_radius(egui::CornerRadius::same(4)) |
| 313 |
.inner_margin(egui::Margin::same(8)) |
| 314 |
.show(ui, |ui| { |
| 315 |
ui.label(egui::RichText::new(body).color(theme::accent_yellow())); |
| 316 |
}); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
pub fn toolbar_toggle( |
| 327 |
ui: &mut egui::Ui, |
| 328 |
label: &str, |
| 329 |
active: bool, |
| 330 |
tooltip: &str, |
| 331 |
count: Option<usize>, |
| 332 |
) -> bool { |
| 333 |
let text = match count { |
| 334 |
Some(n) if n > 0 => format!("{label} ({n})"), |
| 335 |
_ => label.to_string(), |
| 336 |
}; |
| 337 |
let colour = if active { theme::accent_blue() } else { theme::text_muted() }; |
| 338 |
ui.button(egui::RichText::new(text).color(colour)) |
| 339 |
.on_hover_text(tooltip) |
| 340 |
.clicked() |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
pub fn toggle_pills<T: Clone + PartialEq>( |
| 349 |
ui: &mut egui::Ui, |
| 350 |
current: &T, |
| 351 |
options: &[(T, &str, &str)], |
| 352 |
) -> Option<T> { |
| 353 |
let mut chosen = None; |
| 354 |
ui.horizontal(|ui| { |
| 355 |
for (value, label, tooltip) in options { |
| 356 |
let is_active = value == current; |
| 357 |
if ui |
| 358 |
.selectable_label(is_active, *label) |
| 359 |
.on_hover_text(*tooltip) |
| 360 |
.clicked() |
| 361 |
&& !is_active |
| 362 |
{ |
| 363 |
chosen = Some(value.clone()); |
| 364 |
} |
| 365 |
} |
| 366 |
}); |
| 367 |
chosen |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
pub fn primary_button(ui: &mut egui::Ui, label: &str) -> egui::Response { |
| 382 |
ui.add(egui::Button::new(egui::RichText::new(label).strong())) |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
pub fn secondary_button(ui: &mut egui::Ui, label: &str) -> egui::Response { |
| 388 |
ui.button(label) |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
pub fn danger_button(ui: &mut egui::Ui, label: &str) -> egui::Response { |
| 394 |
ui.add(egui::Button::new(egui::RichText::new(label).color(theme::accent_red()))) |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
pub fn danger_small_button(ui: &mut egui::Ui, label: &str) -> egui::Response { |
| 400 |
ui.add(egui::Button::new(egui::RichText::new(label).color(theme::accent_red())).small()) |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
pub fn section_header(ui: &mut egui::Ui, label: &str) { |
| 407 |
ui.label(egui::RichText::new(label).strong().color(theme::text_secondary())); |
| 408 |
ui.separator(); |
| 409 |
ui.add_space(theme::space::SM); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
pub fn subsection_label(ui: &mut egui::Ui, label: &str) { |
| 414 |
ui.label(egui::RichText::new(label).strong().color(theme::text_secondary())); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
pub fn filter_section<R>( |
| 423 |
ui: &mut egui::Ui, |
| 424 |
label: &str, |
| 425 |
active: bool, |
| 426 |
add_contents: impl FnOnce(&mut egui::Ui) -> R, |
| 427 |
) { |
| 428 |
let header = if active { format!("{label} *") } else { label.to_string() }; |
| 429 |
egui::CollapsingHeader::new(header) |
| 430 |
.default_open(active) |
| 431 |
.show(ui, |ui| { |
| 432 |
add_contents(ui); |
| 433 |
}); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
pub fn selectable_row(ui: &mut egui::Ui, active: bool, label: impl Into<String>) -> egui::Response { |
| 444 |
let text = label.into(); |
| 445 |
let rich = if active { |
| 446 |
egui::RichText::new(text).strong().color(theme::accent_blue()) |
| 447 |
} else { |
| 448 |
egui::RichText::new(text).color(theme::text_primary()) |
| 449 |
}; |
| 450 |
ui.selectable_label(active, rich) |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
pub fn selectable_row_secondary( |
| 459 |
ui: &mut egui::Ui, |
| 460 |
active: bool, |
| 461 |
label: impl Into<String>, |
| 462 |
) -> egui::Response { |
| 463 |
let text = label.into(); |
| 464 |
let rich = if active { |
| 465 |
egui::RichText::new(text).strong().color(theme::accent_blue()) |
| 466 |
} else { |
| 467 |
egui::RichText::new(text).color(theme::text_secondary()) |
| 468 |
}; |
| 469 |
ui.selectable_label(active, rich) |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
pub fn selectable_tag(ui: &mut egui::Ui, active: bool, label: impl Into<String>) -> egui::Response { |
| 478 |
let text = label.into(); |
| 479 |
let rich = if active { |
| 480 |
egui::RichText::new(text).color(theme::accent_blue()) |
| 481 |
} else { |
| 482 |
egui::RichText::new(text).color(theme::text_secondary()) |
| 483 |
}; |
| 484 |
ui.selectable_label(active, rich) |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
pub fn wizard_steps(ui: &mut egui::Ui, steps: &[&str], current: usize) { |
| 493 |
ui.horizontal_wrapped(|ui| { |
| 494 |
ui.spacing_mut().item_spacing.x = theme::space::SM; |
| 495 |
for (i, step) in steps.iter().enumerate() { |
| 496 |
let label = format!("{}. {}", i + 1, step); |
| 497 |
let colored = if i == current { |
| 498 |
egui::RichText::new(label).strong().color(theme::accent_blue()) |
| 499 |
} else if i < current { |
| 500 |
egui::RichText::new(label).color(theme::text_secondary()) |
| 501 |
} else { |
| 502 |
egui::RichText::new(label).color(theme::text_muted()) |
| 503 |
}; |
| 504 |
ui.label(colored); |
| 505 |
if i + 1 < steps.len() { |
| 506 |
ui.label(egui::RichText::new("\u{00B7}").color(theme::text_muted())); |
| 507 |
} |
| 508 |
} |
| 509 |
}); |
| 510 |
ui.add_space(theme::space::MD); |
| 511 |
ui.separator(); |
| 512 |
ui.add_space(theme::space::MD); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
pub fn step_number(ui: &mut egui::Ui, n: u32) { |
| 519 |
ui.label(accent_strong(format!("{n}."))); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
pub fn accent_strong(label: impl Into<String>) -> egui::RichText { |
| 527 |
egui::RichText::new(label.into()).strong().color(theme::accent_blue()) |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
pub fn classification_badge(ui: &mut egui::Ui, class: &str) { |
| 534 |
let color = theme::classification_color(class); |
| 535 |
let label = egui::RichText::new(class) |
| 536 |
.small() |
| 537 |
.color(color); |
| 538 |
ui.label(label); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
pub fn tag_chip(ui: &mut egui::Ui, tag: &str) -> egui::Response { |
| 547 |
|
| 548 |
let (rect, response) = ui.allocate_exact_size( |
| 549 |
egui::vec2(ui.fonts(|f| f.glyph_width(&egui::TextStyle::Small.resolve(ui.style()), ' ')) * tag.len() as f32 + 16.0, 20.0), |
| 550 |
egui::Sense::click(), |
| 551 |
); |
| 552 |
|
| 553 |
if ui.is_rect_visible(rect) { |
| 554 |
let bg = if response.hovered() { |
| 555 |
theme::bg_hover() |
| 556 |
} else { |
| 557 |
theme::bg_surface() |
| 558 |
}; |
| 559 |
ui.painter().rect_filled(rect, 4.0, bg); |
| 560 |
ui.painter().text( |
| 561 |
rect.center(), |
| 562 |
egui::Align2::CENTER_CENTER, |
| 563 |
tag, |
| 564 |
egui::FontId::proportional(11.0), |
| 565 |
theme::accent_blue(), |
| 566 |
); |
| 567 |
} |
| 568 |
|
| 569 |
response |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
pub fn tag_chip_removable(ui: &mut egui::Ui, tag: &str, hover_only_remove: bool) -> bool { |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
let mut removed = false; |
| 582 |
ui.horizontal(|ui| { |
| 583 |
ui.spacing_mut().item_spacing.x = 2.0; |
| 584 |
let label_resp = ui.label( |
| 585 |
egui::RichText::new(tag) |
| 586 |
.small() |
| 587 |
.color(theme::accent_blue()), |
| 588 |
); |
| 589 |
let row_hovered = label_resp.hovered() |
| 590 |
|| ui.rect_contains_pointer(label_resp.rect.expand2(egui::vec2(20.0, 0.0))); |
| 591 |
let x_color = if hover_only_remove && !row_hovered { |
| 592 |
theme::text_muted() |
| 593 |
} else { |
| 594 |
theme::accent_red() |
| 595 |
}; |
| 596 |
let btn = ui |
| 597 |
.add( |
| 598 |
egui::Button::new(egui::RichText::new("x").small().color(x_color)) |
| 599 |
.small(), |
| 600 |
) |
| 601 |
.on_hover_text("Remove tag"); |
| 602 |
if btn.clicked() { |
| 603 |
removed = true; |
| 604 |
} |
| 605 |
}); |
| 606 |
removed |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
pub fn format_duration(seconds: f64) -> String { |
| 611 |
if seconds < 60.0 { |
| 612 |
format!("{:.1}s", seconds) |
| 613 |
} else { |
| 614 |
let mins = (seconds / 60.0).floor() as u32; |
| 615 |
let secs = seconds % 60.0; |
| 616 |
format!("{}:{:04.1}", mins, secs) |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
pub fn format_bpm(bpm: f64) -> String { |
| 624 |
if (bpm - bpm.round()).abs() < 0.05 { |
| 625 |
format!("{:.0}", bpm) |
| 626 |
} else { |
| 627 |
format!("{:.1}", bpm) |
| 628 |
} |
| 629 |
} |
| 630 |
|