Skip to content

Commit

Permalink
Updated lyrics UI, added convenience method for when a view is entered
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Jun 26, 2024
1 parent fd8abd4 commit d4c109b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/lyrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl LyricsManager {
}

/// Returns the track being played currently, or nothing if the user is listening to a podcast episodes
fn get_current_track(&self) -> Option<Track> {
pub fn get_current_track(&self) -> Option<Track> {
let playable = self.queue.get_current().unwrap();

match playable {
Expand Down
3 changes: 3 additions & 0 deletions src/lyrics_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use log::trace;

use crate::model::track::Track;

#[derive(Clone)]
Expand All @@ -11,6 +13,7 @@ impl LyricsFetcher {
/// Fetches the lyrics of the given song using the specified lyrics source
pub fn fetch(&self, track: &Track) -> String {
// std::thread::sleep(std::time::Duration::from_secs(2));
trace!("Fetching lyrics for {track}");

format!("Sample Lyrics for {}\n", track.title)
}
Expand Down
5 changes: 5 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub trait ViewExt: View {
}

fn on_leave(&self) {}
fn on_enter(&mut self) {} // TODO: see if there are args that should go here

fn on_command(&mut self, _s: &mut Cursive, _cmd: &Command) -> Result<CommandResult, String> {
Ok(CommandResult::Ignored)
Expand All @@ -91,6 +92,10 @@ impl<V: ViewExt> ViewExt for NamedView<V> {
self.with_view(|v| v.on_leave());
}

fn on_enter(&mut self) {
self.with_view_mut(|v| v.on_enter());
}

fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
self.with_view_mut(move |v| v.on_command(s, cmd)).unwrap()
}
Expand Down
14 changes: 13 additions & 1 deletion src/ui/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ impl Layout {
self.focus = Some(s);
self.cmdline_focus = false;

self.screens
.get_mut(self.focus.as_ref().unwrap())
.unwrap()
.on_enter();

// trigger a redraw
self.ev.trigger();
}
Expand All @@ -173,12 +178,13 @@ impl Layout {
self.result.clone()
}

pub fn push_view(&mut self, view: Box<dyn ViewExt>) {
pub fn push_view(&mut self, mut view: Box<dyn ViewExt>) {
if let Some(view) = self.get_top_view() {
view.on_leave();
}

if let Some(stack) = self.get_focussed_stack_mut() {
view.on_enter();
stack.push(view)
}
}
Expand Down Expand Up @@ -495,4 +501,10 @@ impl ViewExt for Layout {
}
}
}

fn on_enter(&mut self) {
if let Some(view) = self.get_current_view_mut() {
view.on_enter()
}
}
}
92 changes: 67 additions & 25 deletions src/ui/lyrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,85 @@ use std::sync::Arc;

use cursive::{
theme::Effect,
utils::markup::StyledString,
view::ViewWrapper,
views::{DummyView, LinearLayout, ScrollView, TextView},
views::{DummyView, LinearLayout, ResizedView, ScrollView, TextContent, TextView},
};

use crate::{commands::CommandResult, lyrics::LyricsManager, traits::ViewExt, command::Command};

pub struct LyricsView {
manager: Arc<LyricsManager>,
view: LinearLayout,
track_lyrics: TextContent,
track_title: TextContent,
track_authors: TextContent,
track_album: TextContent,
}

impl LyricsView {
pub fn new(manager: Arc<LyricsManager>) -> LyricsView {
let mut text = StyledString::styled("Keybindings\n\n", Effect::Bold);
// INITIALIZE THESE WITH "TRASHY" VALUE THAT IS GOING TO BE REPLACED AFTER
let track_title = TextContent::new("No track being played");
let track_authors = TextContent::new("No track being played");
let track_album = TextContent::new("No track being played");
let track_lyrics = TextContent::new("No track being played");

let note = format!(
"Custom bindings can be set in the {} file within the [keybindings] section.\n\n",
"test"
);
let lyrics_view =
ScrollView::new(TextView::new_with_content(track_lyrics.clone()).center());

// TODO: fixme
let content = String::from("");
let view = LinearLayout::vertical()
.child(ResizedView::with_full_width(
ResizedView::with_fixed_height(5, DummyView),
))
.child(
TextView::new_with_content(track_title.clone())
.center()
.style(Effect::Bold),
)
.child(TextView::new_with_content(track_authors.clone()).center())
.child(
TextView::new_with_content(track_album.clone())
.center()
.style(Effect::Italic),
)
.child(DummyView)
.child(lyrics_view);

text.append(StyledString::styled(note, Effect::Italic));
text.append(content);
let lyrics_view = LyricsView {
manager,
view,
track_lyrics,
track_album,
track_authors,
track_title,
};

text.append("\n\n");
text.append(StyledString::styled(
manager.get_lyrics_for_current(),
Effect::Simple,
));
lyrics_view.update_lyrics();

let lyrics_view = ScrollView::new(TextView::new(text).center());
lyrics_view
}

let view = LinearLayout::vertical()
.child(TextView::new("Title").center())
.child(TextView::new("Authors").center())
.child(TextView::new("Album").center())
.child(DummyView)
.child(lyrics_view);
// needs to be made public in order to be updated from main's event loop
pub fn update_lyrics(&self) {
let current_track = self.manager.get_current_track();

LyricsView { manager, view }
if let Some(track) = current_track {
let track_title_str = track.clone().title;

let track_authors_str = track.clone().artists.join(", ");

let track_album_str = match track.clone().album {
None => String::default(),
Some(album_name) => album_name,
};

let track_lyrics_str = self.manager.get_lyrics(track);

self.track_title.set_content(track_title_str);
self.track_authors.set_content(track_authors_str);
self.track_album.set_content(track_album_str);
self.track_lyrics.set_content(track_lyrics_str);
}
}

/// Saves the lyrics of the current song
Expand All @@ -70,7 +106,9 @@ impl ViewExt for LyricsView {
"".to_string()
}

fn on_leave(&self) {}
fn on_enter(&mut self) {
self.update_lyrics();
}

fn on_command(
&mut self,
Expand All @@ -79,6 +117,10 @@ impl ViewExt for LyricsView {
) -> Result<CommandResult, String> {
match cmd {
Command::Save => self.save_lyrics(),
Command::Next | Command::Previous => {
// still does not work
Ok(CommandResult::Ignored) // return ignored so it is processed by the default command handler
}
_ => Ok(CommandResult::Ignored),
}
}
Expand Down

0 comments on commit d4c109b

Please sign in to comment.