Skip to content

Commit d18b8ba

Browse files
committed
Updated lyrics UI, added convenience method for when a view is entered
1 parent b9cecf5 commit d18b8ba

File tree

5 files changed

+89
-27
lines changed

5 files changed

+89
-27
lines changed

src/lyrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl LyricsManager {
4141
}
4242

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

4747
match playable {

src/lyrics_fetcher.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use log::trace;
2+
13
use crate::model::track::Track;
24

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

1518
format!("Sample Lyrics for {}\n", track.title)
1619
}

src/traits.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub trait ViewExt: View {
7171
}
7272

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

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

94+
fn on_enter(&mut self) {
95+
self.with_view_mut(|v| v.on_enter());
96+
}
97+
9398
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
9499
self.with_view_mut(move |v| v.on_command(s, cmd)).unwrap()
95100
}

src/ui/layout.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ impl Layout {
100100
self.cmdline_focus = false;
101101
self.screenchange = true;
102102

103+
self.screens
104+
.get_mut(self.focus.as_ref().unwrap())
105+
.unwrap()
106+
.on_enter();
107+
103108
// trigger a redraw
104109
self.ev.trigger();
105110
}
@@ -125,12 +130,13 @@ impl Layout {
125130
self.result.clone()
126131
}
127132

128-
pub fn push_view(&mut self, view: Box<dyn ViewExt>) {
133+
pub fn push_view(&mut self, mut view: Box<dyn ViewExt>) {
129134
if let Some(view) = self.get_top_view() {
130135
view.on_leave();
131136
}
132137

133138
if let Some(stack) = self.get_focussed_stack_mut() {
139+
view.on_enter();
134140
stack.push(view)
135141
}
136142
}
@@ -395,4 +401,10 @@ impl ViewExt for Layout {
395401
}
396402
}
397403
}
404+
405+
fn on_enter(&mut self) {
406+
if let Some(view) = self.get_current_view_mut() {
407+
view.on_enter()
408+
}
409+
}
398410
}

src/ui/lyrics.rs

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,85 @@ use std::sync::Arc;
22

33
use cursive::{
44
theme::Effect,
5-
utils::markup::StyledString,
65
view::ViewWrapper,
7-
views::{DummyView, LinearLayout, ScrollView, TextView},
6+
views::{DummyView, LinearLayout, ResizedView, ScrollView, TextContent, TextView},
87
};
98

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

1211
pub struct LyricsView {
1312
manager: Arc<LyricsManager>,
1413
view: LinearLayout,
14+
track_lyrics: TextContent,
15+
track_title: TextContent,
16+
track_authors: TextContent,
17+
track_album: TextContent,
1518
}
1619

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

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

26-
// TODO: fixme
27-
let content = String::from("");
31+
let view = LinearLayout::vertical()
32+
.child(ResizedView::with_full_width(
33+
ResizedView::with_fixed_height(5, DummyView),
34+
))
35+
.child(
36+
TextView::new_with_content(track_title.clone())
37+
.center()
38+
.style(Effect::Bold),
39+
)
40+
.child(TextView::new_with_content(track_authors.clone()).center())
41+
.child(
42+
TextView::new_with_content(track_album.clone())
43+
.center()
44+
.style(Effect::Italic),
45+
)
46+
.child(DummyView)
47+
.child(lyrics_view);
2848

29-
text.append(StyledString::styled(note, Effect::Italic));
30-
text.append(content);
49+
let lyrics_view = LyricsView {
50+
manager,
51+
view,
52+
track_lyrics,
53+
track_album,
54+
track_authors,
55+
track_title,
56+
};
3157

32-
text.append("\n\n");
33-
text.append(StyledString::styled(
34-
manager.get_lyrics_for_current(),
35-
Effect::Simple,
36-
));
58+
lyrics_view.update_lyrics();
3759

38-
let lyrics_view = ScrollView::new(TextView::new(text).center());
60+
lyrics_view
61+
}
3962

40-
let view = LinearLayout::vertical()
41-
.child(TextView::new("Title").center())
42-
.child(TextView::new("Authors").center())
43-
.child(TextView::new("Album").center())
44-
.child(DummyView)
45-
.child(lyrics_view);
63+
// needs to be made public in order to be updated from main's event loop
64+
pub fn update_lyrics(&self) {
65+
let current_track = self.manager.get_current_track();
4666

47-
LyricsView { manager, view }
67+
if let Some(track) = current_track {
68+
let track_title_str = track.clone().title;
69+
70+
let track_authors_str = track.clone().artists.join(", ");
71+
72+
let track_album_str = match track.clone().album {
73+
None => String::default(),
74+
Some(album_name) => album_name,
75+
};
76+
77+
let track_lyrics_str = self.manager.get_lyrics(track);
78+
79+
self.track_title.set_content(track_title_str);
80+
self.track_authors.set_content(track_authors_str);
81+
self.track_album.set_content(track_album_str);
82+
self.track_lyrics.set_content(track_lyrics_str);
83+
}
4884
}
4985

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

73-
fn on_leave(&self) {}
109+
fn on_enter(&mut self) {
110+
self.update_lyrics();
111+
}
74112

75113
fn on_command(
76114
&mut self,
@@ -79,6 +117,10 @@ impl ViewExt for LyricsView {
79117
) -> Result<CommandResult, String> {
80118
match cmd {
81119
Command::Save => self.save_lyrics(),
120+
Command::Next | Command::Previous => {
121+
// still does not work
122+
Ok(CommandResult::Ignored) // return ignored so it is processed by the default command handler
123+
}
82124
_ => Ok(CommandResult::Ignored),
83125
}
84126
}

0 commit comments

Comments
 (0)