use GStrings

This commit is contained in:
Erica Z 2024-10-31 08:15:35 +01:00
parent ab354a35c0
commit 0dab31137c
2 changed files with 17 additions and 41 deletions

View file

@ -25,6 +25,8 @@ mod ffi {
} }
} }
use glib::translate::{from_glib_none, ToGlibPtr};
use glib::GString;
use gtk::glib; use gtk::glib;
glib::wrapper! { glib::wrapper! {
@ -36,33 +38,15 @@ glib::wrapper! {
} }
impl Song { impl Song {
pub fn title(&self) -> std::borrow::Cow<'_, str> { pub fn title(&self) -> GString {
use glib::translate::ToGlibPtr; unsafe { from_glib_none(ffi::audrey_playbin_song_get_title(self.to_glib_none().0)) }
// TODO: memory management....
String::from_utf8_lossy(unsafe {
std::ffi::CStr::from_ptr(ffi::audrey_playbin_song_get_title(self.to_glib_none().0))
.to_bytes()
})
} }
pub fn artist(&self) -> std::borrow::Cow<'_, str> { pub fn artist(&self) -> GString {
use glib::translate::ToGlibPtr; unsafe { from_glib_none(ffi::audrey_playbin_song_get_artist(self.to_glib_none().0)) }
// TODO: memory management....
String::from_utf8_lossy(unsafe {
std::ffi::CStr::from_ptr(ffi::audrey_playbin_song_get_artist(self.to_glib_none().0))
.to_bytes()
})
} }
pub fn album(&self) -> std::borrow::Cow<'_, str> { pub fn album(&self) -> GString {
use glib::translate::ToGlibPtr; unsafe { from_glib_none(ffi::audrey_playbin_song_get_album(self.to_glib_none().0)) }
// TODO: memory management....
String::from_utf8_lossy(unsafe {
std::ffi::CStr::from_ptr(ffi::audrey_playbin_song_get_album(self.to_glib_none().0))
.to_bytes()
})
} }
} }

View file

@ -2,6 +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, gstr, GString};
use gtk::{gdk, glib}; use gtk::{gdk, glib};
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
@ -48,32 +49,23 @@ mod imp {
#[gtk::template_callbacks] #[gtk::template_callbacks]
impl Playbar { impl Playbar {
#[template_callback] #[template_callback]
fn song_title(&self, song: Option<&crate::playbin::Song>) -> String { fn song_title(&self, song: Option<&crate::playbin::Song>) -> Option<GString> {
match song { song.map(|song| song.title())
None => "".to_owned(),
Some(song) => song.title().to_string(),
}
} }
#[template_callback] #[template_callback]
fn song_artist(&self, song: Option<&crate::playbin::Song>) -> String { fn song_artist(&self, song: Option<&crate::playbin::Song>) -> Option<GString> {
match song { song.map(|song| song.artist())
None => "".to_owned(),
Some(song) => song.artist().to_string(),
}
} }
#[template_callback] #[template_callback]
fn song_album(&self, song: Option<&crate::playbin::Song>) -> String { fn song_album(&self, song: Option<&crate::playbin::Song>) -> Option<GString> {
match song { song.map(|song| song.album())
None => "".to_owned(),
Some(song) => song.album().to_string(),
}
} }
#[template_callback] #[template_callback]
fn format_timestamp(&self, s: f64) -> String { fn format_timestamp(&self, s: f64) -> GString {
format!("{:02}:{:02}", (s as i64) / 64, (s as i64) % 60) gformat!("{:02}:{:02}", (s as i64) / 64, (s as i64) % 60)
} }
#[template_callback] #[template_callback]