Browse and listen to thousands of radio stations across the globe right from your terminal ๐ŸŒŽ ๐Ÿ“ป ๐ŸŽตโœจ
radio rust tokio web-radio command-line-tool tui

app: refactor: pull event actions to functions

Changed files
+64 -58
src
+64 -58
src/app.rs
··· 434 434 ) -> Result<bool, io::Error> { 435 435 let mut quit = false; 436 436 437 - let play = |graph: &mut GraphConfig| { 438 - graph.pause = false; 439 - sink_cmd_tx 440 - .send(SinkCommand::Play) 441 - .expect("receiver never dropped"); 442 - }; 443 - 444 - let pause = |graph: &mut GraphConfig| { 445 - graph.pause = true; 446 - sink_cmd_tx 447 - .send(SinkCommand::Pause) 448 - .expect("receiver never dropped"); 449 - }; 450 - 451 - let toggle_play_pause = |graph: &mut GraphConfig| { 452 - graph.pause = !graph.pause; 453 - let sink_cmd = if graph.pause { 454 - SinkCommand::Pause 455 - } else { 456 - SinkCommand::Play 457 - }; 458 - sink_cmd_tx.send(sink_cmd).expect("receiver never dropped"); 459 - }; 460 - 461 - let lower_volume = || { 462 - let mut state = state.lock().unwrap(); 463 - state.volume.change_volume(-1.0); 464 - sink_cmd_tx 465 - .send(SinkCommand::SetVolume(state.volume.volume_ratio())) 466 - .expect("receiver never dropped"); 467 - }; 468 - 469 - let raise_volume = || { 470 - let mut state = state.lock().unwrap(); 471 - state.volume.change_volume(1.0); 472 - sink_cmd_tx 473 - .send(SinkCommand::SetVolume(state.volume.volume_ratio())) 474 - .expect("receiver never dropped"); 475 - }; 476 - 477 - let mute_volume = || { 478 - let mut state = state.lock().unwrap(); 479 - state.volume.toggle_mute(); 480 - sink_cmd_tx 481 - .send(SinkCommand::SetVolume(state.volume.volume_ratio())) 482 - .expect("receiver never dropped"); 483 - }; 484 - 485 437 if let Event::Key(key) = event { 486 438 if let KeyModifiers::CONTROL = key.modifiers { 487 439 match key.code { ··· 500 452 KeyCode::Up => { 501 453 // inverted to act as zoom 502 454 update_value_f(&mut self.graph.scale, 0.01, magnitude, 0.0..10.0); 503 - raise_volume(); 455 + raise_volume(&state, sink_cmd_tx); 504 456 } 505 457 KeyCode::Down => { 506 458 // inverted to act as zoom 507 459 update_value_f(&mut self.graph.scale, -0.01, magnitude, 0.0..10.0); 508 - lower_volume(); 460 + lower_volume(&state, sink_cmd_tx); 509 461 } 510 462 KeyCode::Right => update_value_i( 511 463 &mut self.graph.samples, ··· 522 474 0..self.graph.width * 2, 523 475 ), 524 476 KeyCode::Char('q') => quit = true, 525 - KeyCode::Char(' ') => toggle_play_pause(&mut self.graph), 477 + KeyCode::Char(' ') => toggle_play_pause(&mut self.graph, sink_cmd_tx), 526 478 KeyCode::Char('s') => self.graph.scatter = !self.graph.scatter, 527 479 KeyCode::Char('h') => self.graph.show_ui = !self.graph.show_ui, 528 480 KeyCode::Char('r') => self.graph.references = !self.graph.references, 529 - KeyCode::Char('m') => mute_volume(), 481 + KeyCode::Char('m') => mute_volume(&state, sink_cmd_tx), 530 482 KeyCode::Esc => { 531 483 self.graph.samples = self.graph.width; 532 484 self.graph.scale = 1.; ··· 552 504 } 553 505 } 554 506 KeyCode::Media(media_key_code) => match media_key_code { 555 - MediaKeyCode::Play => play(&mut self.graph), 556 - MediaKeyCode::Pause => pause(&mut self.graph), 557 - MediaKeyCode::PlayPause => toggle_play_pause(&mut self.graph), 507 + MediaKeyCode::Play => play(&mut self.graph, sink_cmd_tx), 508 + MediaKeyCode::Pause => pause(&mut self.graph, sink_cmd_tx), 509 + MediaKeyCode::PlayPause => toggle_play_pause(&mut self.graph, sink_cmd_tx), 558 510 MediaKeyCode::Stop => { 559 511 quit = true; 560 512 } 561 - MediaKeyCode::LowerVolume => lower_volume(), 562 - MediaKeyCode::RaiseVolume => raise_volume(), 563 - MediaKeyCode::MuteVolume => mute_volume(), 513 + MediaKeyCode::LowerVolume => lower_volume(&state, sink_cmd_tx), 514 + MediaKeyCode::RaiseVolume => raise_volume(&state, sink_cmd_tx), 515 + MediaKeyCode::MuteVolume => mute_volume(&state, sink_cmd_tx), 564 516 MediaKeyCode::TrackNext 565 517 | MediaKeyCode::TrackPrevious 566 518 | MediaKeyCode::Reverse ··· 635 587 ) 636 588 .style(Style::default().fg(cfg.labels_color)) 637 589 } 590 + 591 + /// Play music. 592 + fn play(graph: &mut GraphConfig, sink_cmd_tx: &UnboundedSender<SinkCommand>) { 593 + graph.pause = false; 594 + sink_cmd_tx 595 + .send(SinkCommand::Play) 596 + .expect("receiver never dropped"); 597 + } 598 + 599 + /// Pause music. 600 + fn pause(graph: &mut GraphConfig, sink_cmd_tx: &UnboundedSender<SinkCommand>) { 601 + graph.pause = true; 602 + sink_cmd_tx 603 + .send(SinkCommand::Pause) 604 + .expect("receiver never dropped"); 605 + } 606 + 607 + /// Toggle between play and pause. 608 + fn toggle_play_pause(graph: &mut GraphConfig, sink_cmd_tx: &UnboundedSender<SinkCommand>) { 609 + graph.pause = !graph.pause; 610 + let sink_cmd = if graph.pause { 611 + SinkCommand::Pause 612 + } else { 613 + SinkCommand::Play 614 + }; 615 + sink_cmd_tx.send(sink_cmd).expect("receiver never dropped"); 616 + } 617 + 618 + /// Lower the volume. 619 + fn lower_volume(state: &Arc<Mutex<State>>, sink_cmd_tx: &UnboundedSender<SinkCommand>) { 620 + let mut state = state.lock().unwrap(); 621 + state.volume.change_volume(-1.0); 622 + sink_cmd_tx 623 + .send(SinkCommand::SetVolume(state.volume.volume_ratio())) 624 + .expect("receiver never dropped"); 625 + } 626 + 627 + /// Raise the volume. 628 + fn raise_volume(state: &Arc<Mutex<State>>, sink_cmd_tx: &UnboundedSender<SinkCommand>) { 629 + let mut state = state.lock().unwrap(); 630 + state.volume.change_volume(1.0); 631 + sink_cmd_tx 632 + .send(SinkCommand::SetVolume(state.volume.volume_ratio())) 633 + .expect("receiver never dropped"); 634 + } 635 + 636 + /// Mute the volume. 637 + fn mute_volume(state: &Arc<Mutex<State>>, sink_cmd_tx: &UnboundedSender<SinkCommand>) { 638 + let mut state = state.lock().unwrap(); 639 + state.volume.toggle_mute(); 640 + sink_cmd_tx 641 + .send(SinkCommand::SetVolume(state.volume.volume_ratio())) 642 + .expect("receiver never dropped"); 643 + }