Skip to content

Commit

Permalink
Extract screen-specific actions into their own update()
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoRiether committed Feb 8, 2024
1 parent 7aa75c1 commit 24d6376
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 154 deletions.
3 changes: 3 additions & 0 deletions tori/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub struct State<'n> {
pub visualizer: Visualizer,
}

#[derive(Default)]
pub enum Screen {
#[default]
None,
BrowseScreen(BrowseScreen),
}

Expand Down
1 change: 1 addition & 0 deletions tori/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn ui(state: &mut State, area: Rect, buf: &mut Buffer) {
state.now_playing.render(bottom, buf, &mut l);

match &mut state.screen {
Screen::None => unreachable!(),
Screen::BrowseScreen(screen) => browse_screen(screen, screen_area, buf, &mut l),
}

Expand Down
148 changes: 148 additions & 0 deletions tori/src/update/browse_screen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use std::mem;

use crate::{
error::Result,
events::{channel::Tx, Action, Command},
input::Input,
player::Player,
state::{
browse_screen::{BrowseScreen, Focus},
State,
},
util::copy_to_clipboard,
};

pub fn browse_screen_action(
state: &mut State<'_>,
screen: &mut BrowseScreen,
tx: Tx,
act: Action,
) -> Result<Option<Action>> {
use Action::*;
match act {
Action::Command(cmd) => return browse_screen_command(state, screen, tx, cmd),

ScrollDown => match &screen.focus {
Focus::Playlists => screen.shown_playlists.select_next(),
Focus::Songs => screen.shown_songs.select_next(),
Focus::PlaylistsFilter(_) | Focus::SongsFilter(_) => {}
},
ScrollUp => match &screen.focus {
Focus::Playlists => screen.shown_playlists.select_prev(),
Focus::Songs => screen.shown_songs.select_prev(),
Focus::PlaylistsFilter(_) | Focus::SongsFilter(_) => {}
},

SongAdded {
playlist,
song: _song,
} => {
if screen.selected_playlist() == Some(playlist.as_str()) {
screen.refresh_songs()?;
screen
.shown_songs
.select(Some(screen.shown_songs.items.len() - 1));
}
}
RefreshSongs => screen.refresh_songs()?,
RefreshPlaylists => screen.refresh_playlists()?,
SelectSong(i) => screen.shown_songs.select(Some(i)),
SelectPlaylist(i) => {
screen.shown_playlists.select(Some(i));
screen.refresh_songs()?;
}

_ => {}
}

Ok(None)
}

fn browse_screen_command(
state: &mut State<'_>,
screen: &mut BrowseScreen,
_tx: Tx,
cmd: Command,
) -> Result<Option<Action>> {
use Command::*;
match cmd {
Esc => {
let focus = mem::take(&mut screen.focus);
screen.focus = match focus {
Focus::PlaylistsFilter(_) => Focus::Playlists,
Focus::SongsFilter(_) => Focus::Songs,
_ => focus,
};
return Ok(Some(Action::RefreshPlaylists));
}

Play => {
if let Some(song) = screen.selected_song() {
state.player.play(&song.path)?;
}
}
QueueSong => {
if let Some(song) = screen.selected_song() {
state.player.queue(&song.path)?;
}
}
QueueShown => {
for &i in screen.shown_songs.iter() {
let path = screen.songs[i].path.as_str();
state.player.queue(path)?;
}
}

OpenInBrowser => {
if let Some(song) = screen.selected_song() {
match webbrowser::open(&song.path) {
Ok(_) => state.notify_ok(format!("Opening {} in your browser", song.path)),
Err(e) => state.notify_err(format!("Failed to open song path: {}", e)),
}
}
}
CopyUrl => {
if let Some(song) = screen.selected_song() {
copy_to_clipboard(song.path.clone());
state.notify_ok(format!("Copied {} to the clipboard", song.path));
}
}
CopyTitle => {
if let Some(song) = screen.selected_song() {
copy_to_clipboard(song.title.clone());
state.notify_ok(format!("Copied {} to the clipboard", song.title));
}
}

NextSortingMode => screen.next_sorting_mode(),

SelectNext => screen.select_next()?,
SelectPrev => screen.select_prev()?,
SelectLeft => screen.focus = Focus::Playlists,
SelectRight => screen.focus = Focus::Songs,

Search => {
let focus = mem::take(&mut screen.focus);
let new_focus = match focus {
Focus::Playlists => Focus::PlaylistsFilter(Input::default()),
Focus::Songs => Focus::SongsFilter(Input::default()),
_ => focus,
};
screen.focus = new_focus;
}

GotoStart => match &screen.focus {
Focus::Playlists => screen.shown_playlists.select_first(),
Focus::Songs => screen.shown_songs.select_first(),
Focus::PlaylistsFilter(_) | Focus::SongsFilter(_) => {}
},
GotoEnd => match &screen.focus {
Focus::Playlists => screen.shown_playlists.select_last(),
Focus::Songs => screen.shown_songs.select_last(),
Focus::PlaylistsFilter(_) | Focus::SongsFilter(_) => {}
},
_ => {}
}

Ok(None)
}
Loading

0 comments on commit 24d6376

Please sign in to comment.