| 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 |
use std::collections::BTreeMap; |
| 69 |
use std::fmt::Write as _; |
| 70 |
|
| 71 |
use quasi_router::{Node, Screen, layout}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 80 |
pub(super) enum Role { |
| 81 |
|
| 82 |
Button, |
| 83 |
|
| 84 |
Text, |
| 85 |
|
| 86 |
Check, |
| 87 |
|
| 88 |
Choice, |
| 89 |
|
| 90 |
Number, |
| 91 |
} |
| 92 |
|
| 93 |
impl Role { |
| 94 |
const fn show(self) -> &'static str { |
| 95 |
match self { |
| 96 |
Self::Button => "button", |
| 97 |
Self::Text => "text", |
| 98 |
Self::Check => "check", |
| 99 |
Self::Choice => "choice", |
| 100 |
Self::Number => "number", |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 107 |
pub(super) struct Offer { |
| 108 |
|
| 109 |
pub(super) role: Role, |
| 110 |
|
| 111 |
pub(super) label: String, |
| 112 |
|
| 113 |
pub(super) dead: bool, |
| 114 |
} |
| 115 |
|
| 116 |
impl Offer { |
| 117 |
fn show(&self) -> String { |
| 118 |
let dead = if self.dead { " (dead)" } else { "" }; |
| 119 |
format!("{} {:?}{dead}", self.role.show(), self.label) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
#[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 125 |
pub(super) struct Offering { |
| 126 |
offers: Vec<Offer>, |
| 127 |
|
| 128 |
|
| 129 |
addresses: Vec<String>, |
| 130 |
} |
| 131 |
|
| 132 |
impl Offering { |
| 133 |
fn push(&mut self, role: Role, label: impl Into<String>, dead: bool) { |
| 134 |
let label = label.into(); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if label.trim().is_empty() { |
| 139 |
return; |
| 140 |
} |
| 141 |
self.offers.push(Offer { role, label, dead }); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
fn sorted(&self) -> Vec<Offer> { |
| 146 |
let mut offers = self.offers.clone(); |
| 147 |
offers.sort(); |
| 148 |
offers |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
fn addresses_resolve(&self) { |
| 165 |
let router = super::router(); |
| 166 |
let patterns: Vec<String> = router.routes().map(|(_, path)| path.to_owned()).collect(); |
| 167 |
for address in &self.addresses { |
| 168 |
assert!( |
| 169 |
patterns.iter().any(|pattern| matches(pattern, address)), |
| 170 |
"the screen offers a control addressed {address:?}, \ |
| 171 |
and the router has no route that answers it" |
| 172 |
); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
fn show(&self) -> String { |
| 178 |
let mut out = String::new(); |
| 179 |
for offer in self.sorted() { |
| 180 |
let _ = writeln!(out, " {}", offer.show()); |
| 181 |
} |
| 182 |
out |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
pub(super) fn described(screen: &Screen) -> Offering { |
| 191 |
let mut out = Offering::default(); |
| 192 |
for slot in &screen.slots { |
| 193 |
for placed in &slot.body { |
| 194 |
walk(&placed.node, &mut out); |
| 195 |
} |
| 196 |
} |
| 197 |
out |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
fn field_offers(field: &quasi_router::Field, out: &mut Offering) { |
| 208 |
if field.kind == layout::FieldKind::Radio { |
| 209 |
for choice in &field.options { |
| 210 |
out.push(Role::Choice, choice.label.clone(), false); |
| 211 |
} |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
let ends = if field.kind == layout::FieldKind::Interval { |
| 219 |
2 |
| 220 |
} else { |
| 221 |
1 |
| 222 |
}; |
| 223 |
for _ in 0..ends { |
| 224 |
out.push(field_role(field.kind), field.label.clone(), false); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
fn field_role(kind: layout::FieldKind) -> Role { |
| 230 |
use layout::FieldKind as K; |
| 231 |
match kind { |
| 232 |
K::Checkbox => Role::Check, |
| 233 |
K::Select | K::Radio => Role::Choice, |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
K::Range | K::Interval => Role::Number, |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
_ => Role::Text, |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
fn walk(node: &Node, out: &mut Offering) { |
| 250 |
match node { |
| 251 |
Node::Act(act) => { |
| 252 |
out.push( |
| 253 |
Role::Button, |
| 254 |
act.label.clone(), |
| 255 |
act.state == Some(layout::State::Disabled), |
| 256 |
); |
| 257 |
out.addresses |
| 258 |
.push(act.action.destination.as_str().to_owned()); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
for field in &act.asks { |
| 263 |
field_offers(field, out); |
| 264 |
} |
| 265 |
} |
| 266 |
Node::Link { text, action } => { |
| 267 |
out.push(Role::Button, text.clone(), false); |
| 268 |
out.addresses.push(action.destination.as_str().to_owned()); |
| 269 |
} |
| 270 |
Node::Field(field) => field_offers(field, out), |
| 271 |
Node::Form { |
| 272 |
submit, |
| 273 |
action, |
| 274 |
fields, |
| 275 |
} => { |
| 276 |
out.push(Role::Button, submit.clone(), false); |
| 277 |
out.addresses.push(action.destination.as_str().to_owned()); |
| 278 |
for field in fields { |
| 279 |
field_offers(field, out); |
| 280 |
} |
| 281 |
} |
| 282 |
Node::Select { |
| 283 |
options, action, .. |
| 284 |
} => { |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
for (choice, own) in options { |
| 299 |
out.push(Role::Button, choice.label.clone(), false); |
| 300 |
if let Some(action) = own.as_ref().or(action.as_ref()) { |
| 301 |
out.addresses.push(action.destination.as_str().to_owned()); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
Node::Table { columns, rows, .. } => { |
| 306 |
for column in columns { |
| 307 |
|
| 308 |
|
| 309 |
if let Some(reorder) = &column.reorder { |
| 310 |
out.push(Role::Button, column.name.clone(), false); |
| 311 |
out.addresses.push(reorder.destination.as_str().to_owned()); |
| 312 |
} |
| 313 |
} |
| 314 |
for cells in rows { |
| 315 |
if let Some(activate) = &cells.activate { |
| 316 |
out.push(Role::Button, first_words(&cells.values), false); |
| 317 |
out.addresses.push(activate.destination.as_str().to_owned()); |
| 318 |
} |
| 319 |
for cell in &cells.values { |
| 320 |
for part in &cell.parts { |
| 321 |
walk(part, out); |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
Node::List { rows, .. } => { |
| 327 |
for row in rows { |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
if row.activate.is_some() || !row.menu.is_empty() { |
| 335 |
let named: Vec<_> = row.parts.iter().map(|part| part.node.clone()).collect(); |
| 336 |
out.push(Role::Button, first_text(&named), false); |
| 337 |
} |
| 338 |
if let Some(activate) = &row.activate { |
| 339 |
out.addresses.push(activate.destination.as_str().to_owned()); |
| 340 |
} |
| 341 |
for part in &row.parts { |
| 342 |
walk(&part.node, out); |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
Node::Region(slot) => { |
| 347 |
for placed in &slot.body { |
| 348 |
walk(&placed.node, out); |
| 349 |
} |
| 350 |
} |
| 351 |
Node::Stats { figures } => { |
| 352 |
for (figure, action) in figures { |
| 353 |
if let Some(action) = action { |
| 354 |
out.push(Role::Button, figure.caption.clone(), false); |
| 355 |
out.addresses.push(action.destination.as_str().to_owned()); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
Node::StandIn { act: Some(act), .. } => walk(&Node::Act(act.clone()), out), |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
Node::Token(tag) => { |
| 370 |
if let Some(action) = &tag.action { |
| 371 |
out.push(Role::Button, tag.label.clone(), false); |
| 372 |
out.addresses.push(action.destination.as_str().to_owned()); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
_ => {} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
fn matches(pattern: &str, address: &str) -> bool { |
| 387 |
let address = address.split('?').next().unwrap_or(address); |
| 388 |
let pattern: Vec<&str> = pattern.trim_matches('/').split('/').collect(); |
| 389 |
let address: Vec<&str> = address.trim_matches('/').split('/').collect(); |
| 390 |
pattern.len() == address.len() |
| 391 |
&& pattern |
| 392 |
.iter() |
| 393 |
.zip(&address) |
| 394 |
.all(|(want, got)| want.starts_with('{') || want == got) |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
fn first_words(cells: &[quasi_router::Cell]) -> String { |
| 404 |
cells |
| 405 |
.iter() |
| 406 |
.find_map(|cell| { |
| 407 |
let said = first_text(&cell.parts); |
| 408 |
(!said.is_empty()).then_some(said) |
| 409 |
}) |
| 410 |
.unwrap_or_default() |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
fn first_text(parts: &[Node]) -> String { |
| 415 |
parts |
| 416 |
.iter() |
| 417 |
.find_map(|part| match part { |
| 418 |
Node::Text { text, .. } | Node::Heading { text, .. } | Node::Link { text, .. } => { |
| 419 |
Some(text.clone()) |
| 420 |
} |
| 421 |
_ => None, |
| 422 |
}) |
| 423 |
.unwrap_or_default() |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
pub(super) fn shipped(mut paint: impl FnMut(&mut egui::Ui)) -> Offering { |
| 437 |
let ctx = egui::Context::default(); |
| 438 |
ctx.enable_accesskit(); |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
for theme in [egui::Theme::Light, egui::Theme::Dark] { |
| 446 |
ctx.style_mut_of(theme, |style| { |
| 447 |
style.interaction.selectable_labels = false; |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
style.animation_time = 0.0; |
| 454 |
}); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
let input = || egui::RawInput { |
| 459 |
screen_rect: Some(egui::Rect::from_min_size( |
| 460 |
egui::Pos2::ZERO, |
| 461 |
egui::vec2(1440.0, 900.0), |
| 462 |
)), |
| 463 |
..Default::default() |
| 464 |
}; |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
let _ = ctx.run_ui(input(), &mut paint); |
| 472 |
let output = ctx.run_ui(input(), &mut paint); |
| 473 |
|
| 474 |
let mut out = Offering::default(); |
| 475 |
let Some(update) = output.platform_output.accesskit_update else { |
| 476 |
panic!("accesskit produced no tree: the panel drew nothing at all"); |
| 477 |
}; |
| 478 |
let by_id: std::collections::HashMap<_, _> = update.nodes.iter().cloned().collect(); |
| 479 |
for (_, node) in &update.nodes { |
| 480 |
let Some(role) = role_of(node) else { |
| 481 |
continue; |
| 482 |
}; |
| 483 |
let said = named(node, &by_id); |
| 484 |
out.push(role, undecorated(&said), node.is_disabled()); |
| 485 |
} |
| 486 |
out |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
fn named( |
| 500 |
node: &egui::accesskit::Node, |
| 501 |
by_id: &std::collections::HashMap<egui::accesskit::NodeId, egui::accesskit::Node>, |
| 502 |
) -> String { |
| 503 |
if let Some(label) = node.label() { |
| 504 |
return label.to_owned(); |
| 505 |
} |
| 506 |
if let Some(said) = node |
| 507 |
.labelled_by() |
| 508 |
.iter() |
| 509 |
.find_map(|id| by_id.get(id)) |
| 510 |
.and_then(|by| by.label().or_else(|| by.value())) |
| 511 |
{ |
| 512 |
return said.to_owned(); |
| 513 |
} |
| 514 |
node.value().unwrap_or_default().to_owned() |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
fn undecorated(said: &str) -> String { |
| 528 |
let mut said = said.trim(); |
| 529 |
for direction in [layout::Sort::Ascending, layout::Sort::Descending] { |
| 530 |
if let Some(stripped) = said.strip_suffix(direction.glyph()) { |
| 531 |
said = stripped.trim_end(); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
if let Some(stripped) = said.strip_suffix('*') { |
| 539 |
said = stripped.trim_end(); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
if said.ends_with(')') |
| 549 |
&& let Some((label, _)) = said.rsplit_once(" (") |
| 550 |
{ |
| 551 |
said = label.trim_end(); |
| 552 |
} |
| 553 |
said.to_owned() |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
fn role_of(node: &egui::accesskit::Node) -> Option<Role> { |
| 571 |
use egui::accesskit::{Action, Role as R}; |
| 572 |
match node.role() { |
| 573 |
R::Button | R::Link => Some(Role::Button), |
| 574 |
R::TextInput | R::MultilineTextInput => Some(Role::Text), |
| 575 |
R::CheckBox | R::Switch => Some(Role::Check), |
| 576 |
R::RadioButton | R::ComboBox | R::ListBox => Some(Role::Choice), |
| 577 |
R::Slider | R::SpinButton => Some(Role::Number), |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
_ if node.supports_action(Action::Click) => Some(Role::Button), |
| 582 |
_ => None, |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
#[derive(Debug, Clone, Default)] |
| 598 |
pub(super) struct Parity { |
| 599 |
dropped: Vec<String>, |
| 600 |
gained: Vec<String>, |
| 601 |
} |
| 602 |
|
| 603 |
impl Parity { |
| 604 |
|
| 605 |
pub(super) fn strict() -> Self { |
| 606 |
Self::default() |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
#[must_use] |
| 615 |
pub(super) fn dropping(mut self, label: &str) -> Self { |
| 616 |
self.dropped.push(label.to_owned()); |
| 617 |
self |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
#[must_use] |
| 625 |
pub(super) fn gaining(mut self, label: &str) -> Self { |
| 626 |
self.gained.push(label.to_owned()); |
| 627 |
self |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
#[must_use] |
| 639 |
pub(super) fn in_a_window(self, title: &str) -> Self { |
| 640 |
self.dropping(title) |
| 641 |
.dropping("Hide") |
| 642 |
.dropping("Close window") |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
#[must_use] |
| 657 |
pub(super) fn slider_readouts(mut self, shown: &[&str]) -> Self { |
| 658 |
for value in shown { |
| 659 |
self = self.dropping(value); |
| 660 |
} |
| 661 |
self |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
pub(super) fn assert(&self, described: &Offering, shipped: &Offering) { |
| 666 |
let mut want: BTreeMap<Offer, isize> = BTreeMap::new(); |
| 667 |
for offer in shipped.sorted() { |
| 668 |
if self.dropped.contains(&offer.label) { |
| 669 |
continue; |
| 670 |
} |
| 671 |
*want.entry(offer).or_default() += 1; |
| 672 |
} |
| 673 |
for offer in described.sorted() { |
| 674 |
if self.gained.contains(&offer.label) { |
| 675 |
continue; |
| 676 |
} |
| 677 |
*want.entry(offer).or_default() -= 1; |
| 678 |
} |
| 679 |
|
| 680 |
let mut missing = Vec::new(); |
| 681 |
let mut extra = Vec::new(); |
| 682 |
for (offer, count) in want { |
| 683 |
for _ in 0..count.max(0) { |
| 684 |
missing.push(offer.show()); |
| 685 |
} |
| 686 |
for _ in 0..(-count).max(0) { |
| 687 |
extra.push(offer.show()); |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
assert!( |
| 692 |
missing.is_empty() && extra.is_empty(), |
| 693 |
"the described screen does not offer what the shipped one offers.\n\ |
| 694 |
\nthe shipped screen offers and the described one does not:\n{}\ |
| 695 |
\nthe described screen offers and the shipped one does not:\n{}\ |
| 696 |
\nall of the shipped screen's offers:\n{}\ |
| 697 |
\nall of the described screen's offers:\n{}", |
| 698 |
show_all(&missing), |
| 699 |
show_all(&extra), |
| 700 |
shipped.show(), |
| 701 |
described.show(), |
| 702 |
); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
fn show_all(lines: &[String]) -> String { |
| 707 |
if lines.is_empty() { |
| 708 |
return " (none)\n".to_owned(); |
| 709 |
} |
| 710 |
let mut out = String::new(); |
| 711 |
for line in lines { |
| 712 |
let _ = writeln!(out, " {line}"); |
| 713 |
} |
| 714 |
out |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
fn fixture() -> (crate::state::BrowserState, tempfile::TempDir) { |
| 724 |
use std::sync::Arc; |
| 725 |
|
| 726 |
let dir = tempfile::TempDir::new().unwrap(); |
| 727 |
let shared = Arc::new(crate::state::SharedState::new()); |
| 728 |
let mut state = crate::state::BrowserState::new(dir.path(), shared, 44_100.0, "Vault").unwrap(); |
| 729 |
|
| 730 |
let vfs = state.current_vfs_id().unwrap(); |
| 731 |
let parent = state.nav.current_dir; |
| 732 |
let db = audiofiles_core::db::Database::open(state.data_dir.join("audiofiles.db")).unwrap(); |
| 733 |
for (hash, name) in [("aaa111", "kick.wav"), ("bbb222", "snare.wav")] { |
| 734 |
db.conn() |
| 735 |
.execute( |
| 736 |
"INSERT OR IGNORE INTO samples \ |
| 737 |
(hash, original_name, file_extension, file_size, import_date, last_modified) \ |
| 738 |
VALUES (?1, ?2, 'wav', 100, 0, 0)", |
| 739 |
rusqlite::params![hash, format!("{hash}.wav")], |
| 740 |
) |
| 741 |
.unwrap(); |
| 742 |
state |
| 743 |
.backend |
| 744 |
.create_sample_link(vfs, parent, name, hash) |
| 745 |
.unwrap(); |
| 746 |
} |
| 747 |
state.refresh_contents(); |
| 748 |
(state, dir) |
| 749 |
} |
| 750 |
|
| 751 |
#[test] |
| 752 |
fn the_file_list_offers_what_the_shipped_one_offers() { |
| 753 |
let (mut state, _dir) = fixture(); |
| 754 |
|
| 755 |
let described = described(&super::panel::described_screen(&state, "/files")); |
| 756 |
let shipped = shipped(|ui| { |
| 757 |
crate::ui::file_list::draw_file_list(ui, &mut state, None); |
| 758 |
}); |
| 759 |
|
| 760 |
described.addresses_resolve(); |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
Parity::strict() |
| 768 |
.dropping("Dur") |
| 769 |
.gaining("Duration") |
| 770 |
.assert(&described, &shipped); |
| 771 |
} |
| 772 |
|
| 773 |
#[test] |
| 774 |
fn the_detail_panel_serves_what_it_describes() { |
| 775 |
let (mut state, _dir) = fixture(); |
| 776 |
|
| 777 |
|
| 778 |
state.nav.selection.set_single(0); |
| 779 |
|
| 780 |
let described = described(&super::panel::described_screen(&state, "/detail")); |
| 781 |
let drawn = shipped(|ui| { |
| 782 |
super::panel::draw_detail(ui, &mut state); |
| 783 |
}); |
| 784 |
|
| 785 |
described.addresses_resolve(); |
| 786 |
|
| 787 |
Parity::strict().assert(&described, &drawn); |
| 788 |
} |
| 789 |
|
| 790 |
#[test] |
| 791 |
fn settings_offers_what_the_four_described_sections_offer() { |
| 792 |
let (mut state, _dir) = fixture(); |
| 793 |
|
| 794 |
let described = described(&super::panel::described_screen(&state, "/settings")); |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
let shipped = shipped(|ui| { |
| 812 |
crate::ui::settings_panel::appearance_body(ui, &mut state); |
| 813 |
crate::ui::settings_panel::preview_body(ui, &mut state); |
| 814 |
crate::ui::settings_panel::forge_body(ui, &mut state); |
| 815 |
crate::ui::settings_panel::display_body(ui, &mut state); |
| 816 |
}); |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
let themes = crate::ui::theme::list_themes(); |
| 823 |
let active = crate::ui::theme::active_id(); |
| 824 |
let active_name = themes |
| 825 |
.iter() |
| 826 |
.find(|theme| theme.id == active) |
| 827 |
.map_or(active.as_str(), |theme| theme.name.as_str()); |
| 828 |
let announced = match &state.theme_selection { |
| 829 |
crate::ui::theme::ThemeSelection::Follow => format!("System ({active_name})"), |
| 830 |
crate::ui::theme::ThemeSelection::Fixed(_) => active_name.to_owned(), |
| 831 |
}; |
| 832 |
|
| 833 |
described.addresses_resolve(); |
| 834 |
Parity::strict() |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
.dropping(&announced) |
| 842 |
.gaining("Theme") |
| 843 |
.gaining("Row height") |
| 844 |
.assert(&described, &shipped); |
| 845 |
} |
| 846 |
|
| 847 |
#[test] |
| 848 |
fn the_flipped_warning_serves_what_it_describes() { |
| 849 |
let (mut state, _dir) = fixture(); |
| 850 |
state.loose_files.loose_files_missing_count = 3; |
| 851 |
state.loose_files.show_loose_files_warning = true; |
| 852 |
|
| 853 |
let described = described(&super::panel::described_screen( |
| 854 |
&state, |
| 855 |
"/library/loose-files", |
| 856 |
)); |
| 857 |
let drawn = shipped(|ui| { |
| 858 |
super::panel::draw_integrity(ui.ctx(), &mut state); |
| 859 |
}); |
| 860 |
|
| 861 |
Parity::strict() |
| 862 |
.in_a_window("Loose-files mode warning") |
| 863 |
.assert(&described, &drawn); |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
#[test] |
| 871 |
fn the_four_name_modals_serve_what_they_describe() { |
| 872 |
type Show = fn(&mut crate::state::BrowserState); |
| 873 |
|
| 874 |
let modals: [(&str, &str, Show); 4] = [ |
| 875 |
("New Vault", "/vaults/new", |state| { |
| 876 |
state.vfs_modal.show_vfs_create = true; |
| 877 |
}), |
| 878 |
("Rename Vault", "/vaults/{id}/rename", |state| { |
| 879 |
let vault = state.nav.vfs_list[0].clone(); |
| 880 |
state.vfs_modal.vfs_rename_target = Some((vault.id, vault.name)); |
| 881 |
}), |
| 882 |
("New Folder", "/folders/new", |state| { |
| 883 |
state.vfs_modal.show_dir_create = true; |
| 884 |
}), |
| 885 |
("Rename", "/folders/{id}/rename", |state| { |
| 886 |
let folder = state.nav.contents[0].node.clone(); |
| 887 |
state.vfs_modal.dir_rename_target = Some((folder.id, folder.name)); |
| 888 |
}), |
| 889 |
]; |
| 890 |
|
| 891 |
for (title, address, show) in modals { |
| 892 |
let (mut state, _dir) = fixture(); |
| 893 |
|
| 894 |
let vault = state.current_vfs_id().unwrap(); |
| 895 |
let parent = state.nav.current_dir; |
| 896 |
state |
| 897 |
.backend |
| 898 |
.create_directory(vault, parent, "drums") |
| 899 |
.unwrap(); |
| 900 |
state.refresh_contents(); |
| 901 |
show(&mut state); |
| 902 |
|
| 903 |
let address = address.replace("{id}", &real_id(&state, address).to_string()); |
| 904 |
let described = described(&super::panel::described_screen(&state, &address)); |
| 905 |
let drawn = shipped(|ui| { |
| 906 |
super::panel::draw_naming(ui.ctx(), &mut state, title, &address); |
| 907 |
}); |
| 908 |
|
| 909 |
described.addresses_resolve(); |
| 910 |
Parity::strict() |
| 911 |
.in_a_window(title) |
| 912 |
.assert(&described, &drawn); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
fn real_id(state: &crate::state::BrowserState, address: &str) -> i64 { |
| 918 |
if address.starts_with("/vaults/") { |
| 919 |
state.nav.vfs_list[0].id.as_i64() |
| 920 |
} else { |
| 921 |
state |
| 922 |
.nav |
| 923 |
.contents |
| 924 |
.iter() |
| 925 |
.map(|node| &node.node) |
| 926 |
.find(|node| node.sample_hash.is_none()) |
| 927 |
.expect("the fixture makes a folder") |
| 928 |
.id |
| 929 |
.as_i64() |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
fn with_a_review_queue(state: &mut crate::state::BrowserState) { |
| 935 |
use crate::state::{ReviewCandidate, ReviewGroup, ReviewQueue}; |
| 936 |
|
| 937 |
state.classifier.review = Some(ReviewQueue { |
| 938 |
groups: vec![ReviewGroup { |
| 939 |
tag: "instrument.drum.kick".to_owned(), |
| 940 |
candidates: vec![ |
| 941 |
ReviewCandidate { |
| 942 |
hash: "aaa111".to_owned(), |
| 943 |
name: Some("kick.wav".to_owned()), |
| 944 |
score: 0.95, |
| 945 |
confident: true, |
| 946 |
accepted: false, |
| 947 |
}, |
| 948 |
ReviewCandidate { |
| 949 |
hash: "bbb222".to_owned(), |
| 950 |
name: Some("snare.wav".to_owned()), |
| 951 |
score: 0.42, |
| 952 |
confident: false, |
| 953 |
accepted: false, |
| 954 |
}, |
| 955 |
], |
| 956 |
names_loaded: true, |
| 957 |
}], |
| 958 |
samples_considered: 2, |
| 959 |
samples_with_suggestions: 2, |
| 960 |
}); |
| 961 |
state.open_review_screen(); |
| 962 |
} |
| 963 |
|
| 964 |
#[test] |
| 965 |
fn the_tag_queue_serves_what_it_describes() { |
| 966 |
let (mut state, _dir) = fixture(); |
| 967 |
with_a_review_queue(&mut state); |
| 968 |
|
| 969 |
let described = described(&super::panel::described_screen(&state, "/review")); |
| 970 |
let drawn = shipped(|ui| { |
| 971 |
super::panel::draw_queue(ui, &mut state); |
| 972 |
}); |
| 973 |
|
| 974 |
described.addresses_resolve(); |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
Parity::strict().assert(&described, &drawn); |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
fn with_the_forge_open(state: &mut crate::state::BrowserState) { |
| 983 |
state.nav.selection.set_single(0); |
| 984 |
state.open_forge_window("aaa111"); |
| 985 |
} |
| 986 |
|
| 987 |
#[test] |
| 988 |
fn the_forge_serves_what_it_describes() { |
| 989 |
let (mut state, _dir) = fixture(); |
| 990 |
with_the_forge_open(&mut state); |
| 991 |
|
| 992 |
let described = described(&super::panel::described_screen(&state, "/forge")); |
| 993 |
let drawn = shipped(|ui| { |
| 994 |
super::panel::draw_forge(ui.ctx(), &mut state); |
| 995 |
}); |
| 996 |
|
| 997 |
described.addresses_resolve(); |
| 998 |
Parity::strict() |
| 999 |
.in_a_window("Sample Forge") |
| 1000 |
.assert(&described, &drawn); |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
fn with_the_editor_open(state: &mut crate::state::BrowserState) { |
| 1005 |
state.nav.selection.set_single(0); |
| 1006 |
state.open_edit_window("aaa111"); |
| 1007 |
} |
| 1008 |
|
| 1009 |
#[test] |
| 1010 |
fn the_editor_serves_what_it_describes() { |
| 1011 |
let (mut state, _dir) = fixture(); |
| 1012 |
with_the_editor_open(&mut state); |
| 1013 |
|
| 1014 |
let described = described(&super::panel::described_screen(&state, "/edit")); |
| 1015 |
let drawn = shipped(|ui| { |
| 1016 |
super::panel::draw_edit(ui.ctx(), &mut state); |
| 1017 |
}); |
| 1018 |
|
| 1019 |
described.addresses_resolve(); |
| 1020 |
Parity::strict() |
| 1021 |
.in_a_window("Sample Editor") |
| 1022 |
.slider_readouts(&["-1.0", "0.0", "0.000", "1.000", "100"]) |
| 1023 |
.assert(&described, &drawn); |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
fn with_every_filter_narrowed(state: &mut crate::state::BrowserState) { |
| 1034 |
let f = &mut state.search.search_filter; |
| 1035 |
f.bpm_min = Some(90.0); |
| 1036 |
f.duration_min = Some(1.0); |
| 1037 |
f.peak_db_min = Some(-12.0); |
| 1038 |
f.centroid_min = Some(500.0); |
| 1039 |
f.flatness_min = Some(0.2); |
| 1040 |
f.attack_min = Some(5.0); |
| 1041 |
f.keys.push("Am".to_owned()); |
| 1042 |
f.required_tags.push("drums".to_owned()); |
| 1043 |
state.search.filter_panel_open = true; |
| 1044 |
} |
| 1045 |
|
| 1046 |
#[test] |
| 1047 |
fn the_filter_panel_serves_what_it_describes() { |
| 1048 |
let (mut state, _dir) = fixture(); |
| 1049 |
with_every_filter_narrowed(&mut state); |
| 1050 |
|
| 1051 |
let described = described(&super::panel::described_screen(&state, "/filters")); |
| 1052 |
let drawn = shipped(|ui| { |
| 1053 |
super::panel::draw_filters(ui, &mut state); |
| 1054 |
}); |
| 1055 |
|
| 1056 |
described.addresses_resolve(); |
| 1057 |
|
| 1058 |
|
| 1059 |
Parity::strict().assert(&described, &drawn); |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
fn with_two_chosen(state: &mut crate::state::BrowserState) { |
| 1064 |
state.nav.selection.select_all(state.nav.contents.len()); |
| 1065 |
} |
| 1066 |
|
| 1067 |
#[test] |
| 1068 |
fn the_three_bulk_modals_serve_what_they_describe() { |
| 1069 |
type Open = fn(&mut crate::state::BrowserState); |
| 1070 |
|
| 1071 |
let modals: [(&str, &str, Open); 3] = [ |
| 1072 |
("Bulk Tag", "/bulk/tag", |state| state.open_bulk_tag_modal()), |
| 1073 |
("Bulk Move", "/bulk/move", |state| { |
| 1074 |
state.open_bulk_move_modal(); |
| 1075 |
}), |
| 1076 |
("Bulk Rename", "/bulk/rename", |state| { |
| 1077 |
state.open_bulk_rename_modal(); |
| 1078 |
}), |
| 1079 |
]; |
| 1080 |
|
| 1081 |
for (title, address, open) in modals { |
| 1082 |
let (mut state, _dir) = fixture(); |
| 1083 |
with_two_chosen(&mut state); |
| 1084 |
open(&mut state); |
| 1085 |
|
| 1086 |
let described = described(&super::panel::described_screen(&state, address)); |
| 1087 |
let drawn = shipped(|ui| { |
| 1088 |
super::panel::draw_bulk(ui.ctx(), &mut state, title, address); |
| 1089 |
}); |
| 1090 |
|
| 1091 |
described.addresses_resolve(); |
| 1092 |
Parity::strict() |
| 1093 |
.in_a_window(title) |
| 1094 |
.assert(&described, &drawn); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
#[test] |
| 1099 |
fn the_unconfigured_sync_screen_serves_what_it_describes() { |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
let (mut state, _dir) = fixture(); |
| 1104 |
state.sync.show_panel = true; |
| 1105 |
|
| 1106 |
let described = described(&super::panel::described_screen(&state, "/sync")); |
| 1107 |
let drawn = shipped(|ui| { |
| 1108 |
super::panel::draw_sync(ui.ctx(), &mut state, None); |
| 1109 |
}); |
| 1110 |
|
| 1111 |
described.addresses_resolve(); |
| 1112 |
Parity::strict() |
| 1113 |
.in_a_window("Cloud Sync") |
| 1114 |
.assert(&described, &drawn); |
| 1115 |
} |
| 1116 |
|
| 1117 |
#[test] |
| 1118 |
fn the_import_preflight_serves_what_it_describes() { |
| 1119 |
let (mut state, _dir) = fixture(); |
| 1120 |
state.import_wf.pending_import_preflight = |
| 1121 |
Some(crate::state::import_workflow::ImportPreflight { |
| 1122 |
source: std::path::PathBuf::from("/music/samples"), |
| 1123 |
file_count: 4_200, |
| 1124 |
total_bytes: 9_000_000_000, |
| 1125 |
}); |
| 1126 |
|
| 1127 |
let described = described(&super::panel::described_screen(&state, "/import/preflight")); |
| 1128 |
let drawn = shipped(|ui| { |
| 1129 |
super::panel::draw_preflight(ui.ctx(), &mut state); |
| 1130 |
}); |
| 1131 |
|
| 1132 |
described.addresses_resolve(); |
| 1133 |
Parity::strict() |
| 1134 |
.in_a_window("Import folder") |
| 1135 |
.assert(&described, &drawn); |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
#[test] |
| 1145 |
fn the_import_flow_serves_what_it_describes_at_every_stage() { |
| 1146 |
type Reach = fn(&mut crate::state::BrowserState); |
| 1147 |
|
| 1148 |
let stages: [(&str, Reach); 4] = [ |
| 1149 |
("idle", |_state| {}), |
| 1150 |
("configuring", |state| { |
| 1151 |
state.import_wf.import_mode = crate::state::ImportMode::ConfigureImport { |
| 1152 |
source: std::path::PathBuf::from("/music/kits"), |
| 1153 |
source_name: "kits".to_owned(), |
| 1154 |
strategy: crate::import::ImportStrategy::NewVfs { |
| 1155 |
vfs_name: "kits".to_owned(), |
| 1156 |
}, |
| 1157 |
available_vfs: state.nav.vfs_list.to_vec(), |
| 1158 |
selected_merge_vfs_idx: 0, |
| 1159 |
new_vfs_name: "kits".to_owned(), |
| 1160 |
audio_file_count: 42, |
| 1161 |
}; |
| 1162 |
}), |
| 1163 |
("copying", |state| { |
| 1164 |
state.import_wf.import_mode = crate::state::ImportMode::Importing { |
| 1165 |
total: 42, |
| 1166 |
completed: 7, |
| 1167 |
current_name: "kick.wav".to_owned(), |
| 1168 |
walking: false, |
| 1169 |
walking_count: 0, |
| 1170 |
total_bytes: 9_000_000, |
| 1171 |
loose_files: false, |
| 1172 |
}; |
| 1173 |
}), |
| 1174 |
("stopped", |state| { |
| 1175 |
state.import_wf.import_mode = crate::state::ImportMode::OperationCancelled { |
| 1176 |
kind: crate::state::CancelKind::Import, |
| 1177 |
completed: 7, |
| 1178 |
total: 42, |
| 1179 |
destination: None, |
| 1180 |
}; |
| 1181 |
}), |
| 1182 |
]; |
| 1183 |
|
| 1184 |
for (stage, reach) in stages { |
| 1185 |
let (mut state, _dir) = fixture(); |
| 1186 |
reach(&mut state); |
| 1187 |
|
| 1188 |
let described = described(&super::panel::described_screen(&state, "/import")); |
| 1189 |
let drawn = shipped(|ui| { |
| 1190 |
super::panel::draw_import(ui, &mut state); |
| 1191 |
}); |
| 1192 |
|
| 1193 |
described.addresses_resolve(); |
| 1194 |
|
| 1195 |
|
| 1196 |
Parity::strict().assert(&described, &drawn); |
| 1197 |
println!(" {stage}: ok"); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
#[test] |
| 1202 |
fn the_sweep_serves_what_it_describes() { |
| 1203 |
let (mut state, _dir) = fixture(); |
| 1204 |
state.import_wf.import_mode = crate::state::ImportMode::Cleaning { |
| 1205 |
completed: 3, |
| 1206 |
total: 9, |
| 1207 |
current_name: "kick.wav".to_owned(), |
| 1208 |
}; |
| 1209 |
|
| 1210 |
let described = described(&super::panel::described_screen(&state, "/cleanup")); |
| 1211 |
let drawn = shipped(|ui| { |
| 1212 |
super::panel::draw_sweep(ui, &mut state); |
| 1213 |
}); |
| 1214 |
|
| 1215 |
described.addresses_resolve(); |
| 1216 |
|
| 1217 |
Parity::strict().assert(&described, &drawn); |
| 1218 |
} |
| 1219 |
|