split off album carousel album into its own class

This commit is contained in:
Erica Z 2024-12-01 00:24:38 +01:00
parent 8b748115e5
commit afa27dbb6a
6 changed files with 138 additions and 52 deletions

View file

@ -80,58 +80,10 @@ template $AudreyUiAlbumCarousel: Adw.Bin {
model: bind template.model;
};
factory: BuilderListItemFactory {
template ListItem {
child: Box {
styles [
"album"
]
orientation: vertical;
margin-bottom: 6;
spacing: 6;
Image {
icon-name: "media-optical-cd";
pixel-size: 160;
halign: center;
hexpand: false;
}
Box {
styles [
"labels"
]
orientation: vertical;
homogeneous: true;
Label {
single-line-mode: true;
label: bind template.item as <$AudreyModelAlbum>.name;
ellipsize: end;
xalign: 0;
margin-start: 6;
styles [
"heading"
]
}
Label {
single-line-mode: true;
label: bind template.item as <$AudreyModelAlbum>.artist;
ellipsize: end;
xalign: 0;
margin-start: 6;
styles [
"caption"
]
}
}
};
}
factory: SignalListItemFactory {
setup => $on_setup() swapped;
bind => $on_bind() swapped;
unbind => $on_unbind() swapped;
};
};
}

View file

@ -0,0 +1,54 @@
using Gtk 4.0;
using Adw 1;
template $AudreyUiAlbumCarouselAlbum: Adw.Bin {
child: Box {
styles [
"album"
]
orientation: vertical;
margin-bottom: 6;
spacing: 6;
Image {
icon-name: "media-optical-cd";
pixel-size: 160;
halign: center;
hexpand: false;
}
Box {
styles [
"labels"
]
orientation: vertical;
homogeneous: true;
Label {
single-line-mode: true;
label: bind template.item as <$AudreyModelAlbum>.name;
ellipsize: end;
xalign: 0;
margin-start: 6;
styles [
"heading"
]
}
Label {
single-line-mode: true;
label: bind template.item as <$AudreyModelAlbum>.artist;
ellipsize: end;
xalign: 0;
margin-start: 6;
styles [
"caption"
]
}
}
};
}

View file

@ -8,6 +8,7 @@
<file>style.css</file>
<file preprocess="xml-stripblanks">gtk/help-overlay.ui</file>
<file preprocess="xml-stripblanks">album_carousel.ui</file>
<file preprocess="xml-stripblanks">album_carousel_album.ui</file>
<file preprocess="xml-stripblanks">play_queue.ui</file>
<file preprocess="xml-stripblanks">play_queue_song.ui</file>
<file preprocess="xml-stripblanks">playbar.ui</file>

View file

@ -2,6 +2,7 @@ blueprints = custom_target(
'blueprints',
input: files(
'album_carousel.blp',
'album_carousel_album.blp',
'play_queue.blp',
'play_queue_song.blp',
'playbar.blp',
@ -10,6 +11,7 @@ blueprints = custom_target(
),
output: [
'album_carousel.ui',
'album_carousel_album.ui',
'play_queue.ui',
'play_queue_song.ui',
'playbar.ui',

View file

@ -1,3 +1,7 @@
mod album;
pub use album::Album;
use crate::model;
use crate::subsonic::AlbumListType;
use adw::prelude::*;
use adw::subclass::prelude::*;
@ -142,6 +146,25 @@ mod imp {
}
});
}
#[template_callback]
pub fn on_setup(&self, item: &gtk::ListItem, _factory: &gtk::SignalListItemFactory) {
let child: super::Album = glib::Object::new();
item.set_child(Some(&child));
}
#[template_callback]
pub fn on_bind(&self, item: &gtk::ListItem, _factory: &gtk::SignalListItemFactory) {
let child: super::Album = item.child().and_downcast().unwrap();
let album: model::Album = item.item().and_downcast().unwrap();
child.bind(&album);
}
#[template_callback]
pub fn on_unbind(&self, item: &gtk::ListItem, _factory: &gtk::SignalListItemFactory) {
let child: super::Album = item.child().and_downcast().unwrap();
child.unbind();
}
}
}

View file

@ -0,0 +1,54 @@
use crate::model;
use adw::{prelude::*, subclass::prelude::*};
use glib::subclass::InitializingObject;
use std::cell::RefCell;
mod imp {
use super::*;
#[derive(gtk::CompositeTemplate, glib::Properties, Default)]
#[template(resource = "/eu/callcc/audrey/album_carousel_album.ui")]
#[properties(wrapper_type = super::Album)]
pub struct Album {
#[property(get, set, nullable)]
item: RefCell<Option<model::Album>>,
}
#[glib::object_subclass]
impl ObjectSubclass for Album {
const NAME: &'static str = "AudreyUiAlbumCarouselAlbum";
type Type = super::Album;
type ParentType = adw::Bin;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for Album {}
impl WidgetImpl for Album {}
impl BinImpl for Album {}
}
glib::wrapper! {
pub struct Album(ObjectSubclass<imp::Album>)
@extends adw::Bin, gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
}
impl Album {
pub fn bind(&self, album: &model::Album) {
self.set_item(Some(album));
}
pub fn unbind(&self) {
self.set_item(None::<model::Album>);
}
}