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;
glib::wrapper! {
@ -36,33 +38,15 @@ glib::wrapper! {
}
impl Song {
pub fn title(&self) -> std::borrow::Cow<'_, str> {
use glib::translate::ToGlibPtr;
// 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 title(&self) -> GString {
unsafe { from_glib_none(ffi::audrey_playbin_song_get_title(self.to_glib_none().0)) }
}
pub fn artist(&self) -> std::borrow::Cow<'_, str> {
use glib::translate::ToGlibPtr;
// 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 artist(&self) -> GString {
unsafe { from_glib_none(ffi::audrey_playbin_song_get_artist(self.to_glib_none().0)) }
}
pub fn album(&self) -> std::borrow::Cow<'_, str> {
use glib::translate::ToGlibPtr;
// 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()
})
pub fn album(&self) -> GString {
unsafe { from_glib_none(ffi::audrey_playbin_song_get_album(self.to_glib_none().0)) }
}
}

View file

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