diff --git a/data/res/style.css b/data/res/style.css index a4345b5..ad68c52 100644 --- a/data/res/style.css +++ b/data/res/style.css @@ -7,4 +7,17 @@ min-width: 24px; min-height: 24px; margin: 0px; +} + +.tile { + min-height: 50px; + min-width: 200px; +} + +.tile > box { + margin: 6px 12px; +} + +.tile .subtitle { + font-size: smaller; } \ No newline at end of file diff --git a/data/ui/tile.blp b/data/ui/tile.blp index 531490c..64022bb 100644 --- a/data/ui/tile.blp +++ b/data/ui/tile.blp @@ -2,24 +2,21 @@ using Gtk 4.0; using Adw 1; template $MusicusTile : Gtk.FlowBoxChild { - styles ["card", "activatable"] - width-request: 200; + styles ["card", "activatable", "tile"] Gtk.Box { orientation: vertical; - margin-start: 6; - margin-end: 6; - margin-top: 6; - margin-bottom: 6; + valign: center; Gtk.Label title_label { - styles ["caption-heading"] + styles ["title"] halign: start; label: _("Title"); } Gtk.Label subtitle_label { - styles ["caption"] + visible: false; + styles ["subtitle", "dim-label"] halign: start; label: _("Subtitle"); } diff --git a/src/tile.rs b/src/tile.rs index 3ee36cc..e862bca 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,11 +1,24 @@ -use gtk::{glib, subclass::prelude::*}; +use gtk::{glib, glib::Properties, prelude::*, subclass::prelude::*}; +use std::cell::RefCell; mod imp { use super::*; - #[derive(Debug, Default, gtk::CompositeTemplate)] + #[derive(Properties, Debug, Default, gtk::CompositeTemplate)] + #[properties(wrapper_type = super::MusicusTile)] #[template(file = "data/ui/tile.blp")] - pub struct MusicusTile {} + pub struct MusicusTile { + #[property(get, set)] + pub title: RefCell, + + #[property(get, set)] + pub subtitle: RefCell>, + + #[template_child] + pub title_label: TemplateChild, + #[template_child] + pub subtitle_label: TemplateChild, + } #[glib::object_subclass] impl ObjectSubclass for MusicusTile { @@ -22,7 +35,29 @@ mod imp { } } - impl ObjectImpl for MusicusTile {} + #[glib::derived_properties] + impl ObjectImpl for MusicusTile { + fn constructed(&self) { + self.parent_constructed(); + + self.obj() + .bind_property("title", &self.title_label.get(), "label") + .sync_create() + .build(); + + self.obj() + .bind_property("subtitle", &self.subtitle_label.get(), "visible") + .sync_create() + .transform_to(|_, s: Option| Some(s.is_some())) + .build(); + + self.obj() + .bind_property("subtitle", &self.subtitle_label.get(), "label") + .sync_create() + .build(); + } + } + impl WidgetImpl for MusicusTile {} impl FlowBoxChildImpl for MusicusTile {} } @@ -33,7 +68,14 @@ glib::wrapper! { } impl MusicusTile { - pub fn new() -> Self { - glib::Object::new() + pub fn with_title(title: &str) -> Self { + glib::Object::builder().property("title", title).build() + } + + pub fn with_subtitle(title: &str, subtitle: &str) -> Self { + glib::Object::builder() + .property("title", title) + .property("subtitle", subtitle) + .build() } }