mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 11:47:25 +01:00
main screen: Add button to start random playback
This commit is contained in:
parent
487fd0a048
commit
3350c6be95
5 changed files with 57 additions and 22 deletions
|
|
@ -110,18 +110,18 @@ impl Player {
|
|||
result
|
||||
.mpris
|
||||
.connect_play_pause(clone!(@weak result => move || {
|
||||
result.play_pause();
|
||||
result.play_pause().unwrap();
|
||||
}));
|
||||
|
||||
result.mpris.connect_play(clone!(@weak result => move || {
|
||||
if !result.is_playing() {
|
||||
result.play_pause();
|
||||
result.play_pause().unwrap();
|
||||
}
|
||||
}));
|
||||
|
||||
result.mpris.connect_pause(clone!(@weak result => move || {
|
||||
if result.is_playing() {
|
||||
result.play_pause();
|
||||
result.play_pause().unwrap();
|
||||
}
|
||||
}));
|
||||
|
||||
|
|
@ -223,7 +223,7 @@ impl Player {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn play_pause(&self) {
|
||||
pub fn play_pause(&self) -> Result<()> {
|
||||
if self.is_playing() {
|
||||
self.player.pause();
|
||||
self.playing.set(false);
|
||||
|
|
@ -235,6 +235,10 @@ impl Player {
|
|||
#[cfg(target_os = "linux")]
|
||||
self.mpris.set_playback_status(PlaybackStatus::Paused);
|
||||
} else {
|
||||
if self.current_track.get().is_none() {
|
||||
self.next()?;
|
||||
}
|
||||
|
||||
self.player.play();
|
||||
self.playing.set(true);
|
||||
|
||||
|
|
@ -245,6 +249,8 @@ impl Player {
|
|||
#[cfg(target_os = "linux")]
|
||||
self.mpris.set_playback_status(PlaybackStatus::Playing);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn seek(&self, ms: u64) {
|
||||
|
|
@ -287,23 +293,30 @@ impl Player {
|
|||
}
|
||||
|
||||
pub fn next(&self) -> Result<()> {
|
||||
let mut current_track = self.current_track.get().ok_or_else(|| {
|
||||
Error::Other(String::from(
|
||||
"Player tried to access non existant current track.",
|
||||
))
|
||||
})?;
|
||||
let current_track = self.current_track.get();
|
||||
let cb = self.generate_next_track_cb.borrow();
|
||||
|
||||
if current_track + 1 < self.playlist.borrow().len() {
|
||||
current_track += 1;
|
||||
} else if let Some(cb) = &*self.generate_next_track_cb.borrow() {
|
||||
if let Some(current_track) = current_track {
|
||||
if current_track + 1 >= self.playlist.borrow().len() {
|
||||
if let Some(cb) = &*cb {
|
||||
let new_track = cb();
|
||||
self.add_item(new_track)?;
|
||||
} else {
|
||||
return Err(Error::Other(String::from("No existing next track.")));
|
||||
}
|
||||
}
|
||||
|
||||
self.set_track(current_track + 1)?;
|
||||
|
||||
Ok(())
|
||||
} else if let Some(cb) = &*cb {
|
||||
let new_track = cb();
|
||||
self.add_item(new_track)?;
|
||||
current_track += 1;
|
||||
} else {
|
||||
return Err(Error::Other(String::from("No existing next track.")));
|
||||
}
|
||||
|
||||
self.set_track(current_track)
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::Other(String::from("No existing next track.")))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_track(&self, current_track: usize) -> Result<()> {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
<child>
|
||||
<object class="AdwHeaderBar">
|
||||
<property name="title-widget">
|
||||
<object class="GtkLabel"/>
|
||||
<object class="GtkLabel" />
|
||||
</property>
|
||||
</object>
|
||||
</child>
|
||||
|
|
@ -17,6 +17,18 @@
|
|||
<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="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>
|
||||
</child>
|
||||
</object>
|
||||
|
|
@ -40,7 +52,7 @@
|
|||
<object class="GtkLabel">
|
||||
<property name="label">Musicus</property>
|
||||
<style>
|
||||
<class name="title"/>
|
||||
<class name="title" />
|
||||
</style>
|
||||
</object>
|
||||
</property>
|
||||
|
|
@ -104,7 +116,7 @@
|
|||
<property name="child">
|
||||
<object class="GtkScrolledWindow" id="scroll">
|
||||
<child>
|
||||
<placeholder/>
|
||||
<placeholder />
|
||||
</child>
|
||||
</object>
|
||||
</property>
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ impl Screen<(), ()> for MainScreen {
|
|||
|
||||
get_widget!(builder, gtk::Box, widget);
|
||||
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::SearchEntry, search_entry);
|
||||
get_widget!(builder, gtk::Stack, stack);
|
||||
|
|
@ -126,6 +128,14 @@ impl Screen<(), ()> for MainScreen {
|
|||
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.leaflet.set_visible_child_name("sidebar");
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -163,7 +163,7 @@ impl Screen<(), ()> for PlayerScreen {
|
|||
|
||||
this.play_button
|
||||
.connect_clicked(clone!(@weak this => move |_| {
|
||||
this.handle.backend.pl().play_pause();
|
||||
this.handle.backend.pl().play_pause().unwrap();
|
||||
}));
|
||||
|
||||
this.next_button
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ impl PlayerBar {
|
|||
|
||||
play_button.connect_clicked(clone!(@strong player => move |_| {
|
||||
if let Some(player) = &*player.borrow() {
|
||||
player.play_pause();
|
||||
player.play_pause().unwrap();
|
||||
}
|
||||
}));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue