From 472770013e1a3d7376a9c286bf459aa5e0e28eb4 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Fri, 1 Nov 2024 17:31:54 +0100 Subject: [PATCH] weaken that one playbin reference --- src/ui/playbar.rs | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/ui/playbar.rs b/src/ui/playbar.rs index 3b802b4..4c1864d 100644 --- a/src/ui/playbar.rs +++ b/src/ui/playbar.rs @@ -2,7 +2,7 @@ mod imp { use adw::prelude::*; use adw::subclass::prelude::*; use glib::subclass::InitializingObject; - use glib::{gformat, GString}; + use glib::{gformat, GString, WeakRef}; use gtk::{gdk, glib}; use std::cell::{Cell, RefCell}; @@ -15,7 +15,7 @@ mod imp { #[property(get, set)] playing_cover_art: RefCell>, #[property(get, set)] - playbin: RefCell>, // TODO: weak + playbin: WeakRef, #[property(get, set, default = true)] show_cover_art: Cell, @@ -102,8 +102,7 @@ mod imp { value: f64, range: >k::Range, ) -> bool { - let playbin = self.playbin.borrow(); - let playbin = playbin.as_ref().unwrap(); + let playbin = self.playbin.upgrade().unwrap(); if range.adjustment().lower() < range.adjustment().upper() { playbin.seek(value); } @@ -112,22 +111,19 @@ mod imp { #[template_callback] fn on_skip_forward_clicked(&self) { - let playbin = self.playbin.borrow(); - let playbin = playbin.as_ref().unwrap(); + let playbin = self.playbin.upgrade().unwrap(); playbin.go_to_next_track(); } #[template_callback] fn on_skip_backward_clicked(&self) { - let playbin = self.playbin.borrow(); - let playbin = playbin.as_ref().unwrap(); + let playbin = self.playbin.upgrade().unwrap(); playbin.go_to_prev_track(); } #[template_callback] fn seek_backward(&self) { - let playbin = self.playbin.borrow(); - let playbin = playbin.as_ref().unwrap(); + let playbin = self.playbin.upgrade().unwrap(); // 10 seconds let mut new_position = playbin.position() - 10.0; if new_position < 0.0 { @@ -138,8 +134,7 @@ mod imp { #[template_callback] fn seek_forward(&self) { - let playbin = self.playbin.borrow(); - let playbin = playbin.as_ref().unwrap(); + let playbin = self.playbin.upgrade().unwrap(); // 10 seconds let mut new_position = playbin.position() + 10.0; if new_position > playbin.duration() { @@ -150,8 +145,7 @@ mod imp { #[template_callback] fn on_play_pause_clicked(&self, _button: >k::Button) { - let playbin = self.playbin.borrow(); - let playbin = playbin.as_ref().unwrap(); + let playbin = self.playbin.upgrade().unwrap(); if playbin.state() == crate::playbin::State::Playing { playbin.pause(); @@ -162,23 +156,21 @@ mod imp { #[template_callback] fn on_mute_toggle(&self) { - let playbin = self.playbin.borrow(); - let playbin = playbin.as_ref().unwrap(); + let playbin = self.playbin.upgrade().unwrap(); playbin.set_mute(!playbin.mute()); } fn volume(&self) -> i32 { - let playbin = self.playbin.borrow(); + let playbin = self.playbin.upgrade(); - match playbin.as_ref() { + match playbin { None => 100, Some(playbin) => playbin.volume(), } } fn set_volume(&self, value: i32) { - let playbin = self.playbin.borrow(); - let playbin = playbin.as_ref().unwrap(); + let playbin = self.playbin.upgrade().unwrap(); playbin.set_volume(value); } }