musicus/src/playlist_item.rs

96 lines
2.4 KiB
Rust
Raw Normal View History

2023-10-25 17:45:32 +02:00
use std::{
2023-10-26 11:48:42 +02:00
cell::{Cell, OnceCell},
2023-10-25 17:45:32 +02:00
path::{Path, PathBuf},
};
2025-03-01 09:57:01 +01:00
use gtk::{glib, glib::Properties, prelude::*, subclass::prelude::*};
2023-10-25 17:45:32 +02:00
mod imp {
use super::*;
#[derive(Properties, Default)]
#[properties(wrapper_type = super::PlaylistItem)]
pub struct PlaylistItem {
2023-10-26 11:48:42 +02:00
#[property(get, set)]
pub is_playing: Cell<bool>,
2023-10-25 17:45:32 +02:00
#[property(get, construct_only)]
pub is_title: OnceCell<bool>,
2023-10-27 14:15:05 +02:00
#[property(get, construct_only)]
2024-07-13 12:20:39 +02:00
pub composers: OnceCell<Option<String>>,
#[property(get, construct_only)]
pub work: OnceCell<String>,
2023-10-25 17:45:32 +02:00
#[property(get, construct_only, nullable)]
pub performers: OnceCell<Option<String>>,
#[property(get, construct_only, nullable)]
pub part_title: OnceCell<Option<String>>,
#[property(get, construct_only)]
pub path: OnceCell<PathBuf>,
2024-06-10 20:53:15 +02:00
#[property(get, construct_only)]
pub track_id: OnceCell<String>,
2023-10-25 17:45:32 +02:00
}
#[glib::object_subclass]
impl ObjectSubclass for PlaylistItem {
const NAME: &'static str = "MusicusPlaylistItem";
type Type = super::PlaylistItem;
}
#[glib::derived_properties]
impl ObjectImpl for PlaylistItem {}
}
glib::wrapper! {
pub struct PlaylistItem(ObjectSubclass<imp::PlaylistItem>);
}
impl PlaylistItem {
pub fn new(
2023-10-27 14:15:05 +02:00
is_title: bool,
composers: Option<String>,
2024-07-13 12:20:39 +02:00
work: &str,
2023-10-25 17:45:32 +02:00
performers: Option<&str>,
part_title: Option<&str>,
path: impl AsRef<Path>,
2024-06-10 20:53:15 +02:00
track_id: &str,
2023-10-25 17:45:32 +02:00
) -> Self {
glib::Object::builder()
2023-10-27 14:15:05 +02:00
.property("is-title", is_title)
2024-07-13 12:20:39 +02:00
.property("composers", composers)
.property("work", work)
2023-10-25 17:45:32 +02:00
.property("performers", performers)
.property("part-title", part_title)
.property("path", path.as_ref())
2024-06-10 20:53:15 +02:00
.property("track-id", track_id)
2023-10-25 17:45:32 +02:00
.build()
}
2023-11-03 18:20:41 +01:00
pub fn make_title(&self) -> String {
2024-07-13 12:20:39 +02:00
let mut title = String::new();
if let Some(composers) = self.composers() {
title.push_str(&composers);
title.push_str(": ");
}
title.push_str(&self.work());
2023-11-03 18:20:41 +01:00
if let Some(part_title) = self.part_title() {
title.push_str(": ");
title.push_str(&part_title);
}
title
}
pub fn make_subtitle(&self) -> Option<String> {
self.performers()
}
2023-10-25 17:45:32 +02:00
}