weaken that one playbin reference

This commit is contained in:
Erica Z 2024-11-01 17:31:54 +01:00
parent 8dc40ac3fb
commit 472770013e

View file

@ -2,7 +2,7 @@ mod imp {
use adw::prelude::*; use adw::prelude::*;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use glib::subclass::InitializingObject; use glib::subclass::InitializingObject;
use glib::{gformat, GString}; use glib::{gformat, GString, WeakRef};
use gtk::{gdk, glib}; use gtk::{gdk, glib};
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
@ -15,7 +15,7 @@ mod imp {
#[property(get, set)] #[property(get, set)]
playing_cover_art: RefCell<Option<gdk::Paintable>>, playing_cover_art: RefCell<Option<gdk::Paintable>>,
#[property(get, set)] #[property(get, set)]
playbin: RefCell<Option<crate::Playbin>>, // TODO: weak playbin: WeakRef<crate::Playbin>,
#[property(get, set, default = true)] #[property(get, set, default = true)]
show_cover_art: Cell<bool>, show_cover_art: Cell<bool>,
@ -102,8 +102,7 @@ mod imp {
value: f64, value: f64,
range: &gtk::Range, range: &gtk::Range,
) -> bool { ) -> bool {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade().unwrap();
let playbin = playbin.as_ref().unwrap();
if range.adjustment().lower() < range.adjustment().upper() { if range.adjustment().lower() < range.adjustment().upper() {
playbin.seek(value); playbin.seek(value);
} }
@ -112,22 +111,19 @@ mod imp {
#[template_callback] #[template_callback]
fn on_skip_forward_clicked(&self) { fn on_skip_forward_clicked(&self) {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade().unwrap();
let playbin = playbin.as_ref().unwrap();
playbin.go_to_next_track(); playbin.go_to_next_track();
} }
#[template_callback] #[template_callback]
fn on_skip_backward_clicked(&self) { fn on_skip_backward_clicked(&self) {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade().unwrap();
let playbin = playbin.as_ref().unwrap();
playbin.go_to_prev_track(); playbin.go_to_prev_track();
} }
#[template_callback] #[template_callback]
fn seek_backward(&self) { fn seek_backward(&self) {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade().unwrap();
let playbin = playbin.as_ref().unwrap();
// 10 seconds // 10 seconds
let mut new_position = playbin.position() - 10.0; let mut new_position = playbin.position() - 10.0;
if new_position < 0.0 { if new_position < 0.0 {
@ -138,8 +134,7 @@ mod imp {
#[template_callback] #[template_callback]
fn seek_forward(&self) { fn seek_forward(&self) {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade().unwrap();
let playbin = playbin.as_ref().unwrap();
// 10 seconds // 10 seconds
let mut new_position = playbin.position() + 10.0; let mut new_position = playbin.position() + 10.0;
if new_position > playbin.duration() { if new_position > playbin.duration() {
@ -150,8 +145,7 @@ mod imp {
#[template_callback] #[template_callback]
fn on_play_pause_clicked(&self, _button: &gtk::Button) { fn on_play_pause_clicked(&self, _button: &gtk::Button) {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade().unwrap();
let playbin = playbin.as_ref().unwrap();
if playbin.state() == crate::playbin::State::Playing { if playbin.state() == crate::playbin::State::Playing {
playbin.pause(); playbin.pause();
@ -162,23 +156,21 @@ mod imp {
#[template_callback] #[template_callback]
fn on_mute_toggle(&self) { fn on_mute_toggle(&self) {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade().unwrap();
let playbin = playbin.as_ref().unwrap();
playbin.set_mute(!playbin.mute()); playbin.set_mute(!playbin.mute());
} }
fn volume(&self) -> i32 { fn volume(&self) -> i32 {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade();
match playbin.as_ref() { match playbin {
None => 100, None => 100,
Some(playbin) => playbin.volume(), Some(playbin) => playbin.volume(),
} }
} }
fn set_volume(&self, value: i32) { fn set_volume(&self, value: i32) {
let playbin = self.playbin.borrow(); let playbin = self.playbin.upgrade().unwrap();
let playbin = playbin.as_ref().unwrap();
playbin.set_volume(value); playbin.set_volume(value);
} }
} }