main screen: Add button to start random playback

This commit is contained in:
Elias Projahn 2022-02-08 18:18:29 +01:00
parent 487fd0a048
commit 3350c6be95
5 changed files with 57 additions and 22 deletions

View file

@ -110,18 +110,18 @@ impl Player {
result result
.mpris .mpris
.connect_play_pause(clone!(@weak result => move || { .connect_play_pause(clone!(@weak result => move || {
result.play_pause(); result.play_pause().unwrap();
})); }));
result.mpris.connect_play(clone!(@weak result => move || { result.mpris.connect_play(clone!(@weak result => move || {
if !result.is_playing() { if !result.is_playing() {
result.play_pause(); result.play_pause().unwrap();
} }
})); }));
result.mpris.connect_pause(clone!(@weak result => move || { result.mpris.connect_pause(clone!(@weak result => move || {
if result.is_playing() { if result.is_playing() {
result.play_pause(); result.play_pause().unwrap();
} }
})); }));
@ -223,7 +223,7 @@ impl Player {
Ok(()) Ok(())
} }
pub fn play_pause(&self) { pub fn play_pause(&self) -> Result<()> {
if self.is_playing() { if self.is_playing() {
self.player.pause(); self.player.pause();
self.playing.set(false); self.playing.set(false);
@ -235,6 +235,10 @@ impl Player {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
self.mpris.set_playback_status(PlaybackStatus::Paused); self.mpris.set_playback_status(PlaybackStatus::Paused);
} else { } else {
if self.current_track.get().is_none() {
self.next()?;
}
self.player.play(); self.player.play();
self.playing.set(true); self.playing.set(true);
@ -245,6 +249,8 @@ impl Player {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
self.mpris.set_playback_status(PlaybackStatus::Playing); self.mpris.set_playback_status(PlaybackStatus::Playing);
} }
Ok(())
} }
pub fn seek(&self, ms: u64) { pub fn seek(&self, ms: u64) {
@ -287,23 +293,30 @@ impl Player {
} }
pub fn next(&self) -> Result<()> { pub fn next(&self) -> Result<()> {
let mut current_track = self.current_track.get().ok_or_else(|| { let current_track = self.current_track.get();
Error::Other(String::from( let cb = self.generate_next_track_cb.borrow();
"Player tried to access non existant current track.",
))
})?;
if current_track + 1 < self.playlist.borrow().len() { if let Some(current_track) = current_track {
current_track += 1; if current_track + 1 >= self.playlist.borrow().len() {
} else if let Some(cb) = &*self.generate_next_track_cb.borrow() { if let Some(cb) = &*cb {
let new_track = cb(); let new_track = cb();
self.add_item(new_track)?; self.add_item(new_track)?;
current_track += 1;
} else { } else {
return Err(Error::Other(String::from("No existing next track."))); return Err(Error::Other(String::from("No existing next track.")));
} }
}
self.set_track(current_track) self.set_track(current_track + 1)?;
Ok(())
} else if let Some(cb) = &*cb {
let new_track = cb();
self.add_item(new_track)?;
Ok(())
} else {
Err(Error::Other(String::from("No existing next track.")))
}
} }
pub fn set_track(&self, current_track: usize) -> Result<()> { pub fn set_track(&self, current_track: usize) -> Result<()> {

View file

@ -7,7 +7,7 @@
<child> <child>
<object class="AdwHeaderBar"> <object class="AdwHeaderBar">
<property name="title-widget"> <property name="title-widget">
<object class="GtkLabel"/> <object class="GtkLabel" />
</property> </property>
</object> </object>
</child> </child>
@ -17,6 +17,18 @@
<property name="title" translatable="yes">Welcome to Musicus!</property> <property name="title" translatable="yes">Welcome to Musicus!</property>
<property name="description" translatable="yes">Get startet by selecting something from the sidebar or adding new things to your library using the button in the top left corner.</property> <property name="description" translatable="yes">Get startet by selecting something from the sidebar or adding new things to your library using the button in the top left corner.</property>
<property name="vexpand">true</property> <property name="vexpand">true</property>
<child>
<object class="GtkRevealer" id="play_button_revealer">
<property name="reveal-child">true</property>
<property name="transition-type">crossfade</property>
<child>
<object class="GtkButton" id="play_button">
<property name="halign">center</property>
<property name="label" translatable="yes">Play something</property>
</object>
</child>
</object>
</child>
</object> </object>
</child> </child>
</object> </object>
@ -40,7 +52,7 @@
<object class="GtkLabel"> <object class="GtkLabel">
<property name="label">Musicus</property> <property name="label">Musicus</property>
<style> <style>
<class name="title"/> <class name="title" />
</style> </style>
</object> </object>
</property> </property>
@ -104,7 +116,7 @@
<property name="child"> <property name="child">
<object class="GtkScrolledWindow" id="scroll"> <object class="GtkScrolledWindow" id="scroll">
<child> <child>
<placeholder/> <placeholder />
</child> </child>
</object> </object>
</property> </property>

View file

@ -32,6 +32,8 @@ impl Screen<(), ()> for MainScreen {
get_widget!(builder, gtk::Box, widget); get_widget!(builder, gtk::Box, widget);
get_widget!(builder, adw::Leaflet, leaflet); get_widget!(builder, adw::Leaflet, leaflet);
get_widget!(builder, gtk::Revealer, play_button_revealer);
get_widget!(builder, gtk::Button, play_button);
get_widget!(builder, gtk::Button, add_button); get_widget!(builder, gtk::Button, add_button);
get_widget!(builder, gtk::SearchEntry, search_entry); get_widget!(builder, gtk::SearchEntry, search_entry);
get_widget!(builder, gtk::Stack, stack); get_widget!(builder, gtk::Stack, stack);
@ -126,6 +128,14 @@ impl Screen<(), ()> for MainScreen {
search.is_empty() || title.contains(&search) search.is_empty() || title.contains(&search)
})); }));
this.handle.backend.pl().add_playlist_cb(clone!(@weak play_button_revealer => move |new_playlist| {
play_button_revealer.set_reveal_child(new_playlist.is_empty());
}));
play_button.connect_clicked(clone!(@weak this => move |_| {
this.handle.backend.pl().play_pause().unwrap();
}));
this.navigator.set_back_cb(clone!(@weak this => move || { this.navigator.set_back_cb(clone!(@weak this => move || {
this.leaflet.set_visible_child_name("sidebar"); this.leaflet.set_visible_child_name("sidebar");
})); }));

View file

@ -163,7 +163,7 @@ impl Screen<(), ()> for PlayerScreen {
this.play_button this.play_button
.connect_clicked(clone!(@weak this => move |_| { .connect_clicked(clone!(@weak this => move |_| {
this.handle.backend.pl().play_pause(); this.handle.backend.pl().play_pause().unwrap();
})); }));
this.next_button this.next_button

View file

@ -47,7 +47,7 @@ impl PlayerBar {
play_button.connect_clicked(clone!(@strong player => move |_| { play_button.connect_clicked(clone!(@strong player => move |_| {
if let Some(player) = &*player.borrow() { if let Some(player) = &*player.borrow() {
player.play_pause(); player.play_pause().unwrap();
} }
})); }));