Improve tile widget

This commit is contained in:
Elias Projahn 2023-10-07 20:52:15 +02:00
parent 54f0088e61
commit bcb4b84b19
3 changed files with 66 additions and 14 deletions

View file

@ -7,4 +7,17 @@
min-width: 24px; min-width: 24px;
min-height: 24px; min-height: 24px;
margin: 0px; margin: 0px;
}
.tile {
min-height: 50px;
min-width: 200px;
}
.tile > box {
margin: 6px 12px;
}
.tile .subtitle {
font-size: smaller;
} }

View file

@ -2,24 +2,21 @@ using Gtk 4.0;
using Adw 1; using Adw 1;
template $MusicusTile : Gtk.FlowBoxChild { template $MusicusTile : Gtk.FlowBoxChild {
styles ["card", "activatable"] styles ["card", "activatable", "tile"]
width-request: 200;
Gtk.Box { Gtk.Box {
orientation: vertical; orientation: vertical;
margin-start: 6; valign: center;
margin-end: 6;
margin-top: 6;
margin-bottom: 6;
Gtk.Label title_label { Gtk.Label title_label {
styles ["caption-heading"] styles ["title"]
halign: start; halign: start;
label: _("Title"); label: _("Title");
} }
Gtk.Label subtitle_label { Gtk.Label subtitle_label {
styles ["caption"] visible: false;
styles ["subtitle", "dim-label"]
halign: start; halign: start;
label: _("Subtitle"); label: _("Subtitle");
} }

View file

@ -1,11 +1,24 @@
use gtk::{glib, subclass::prelude::*}; use gtk::{glib, glib::Properties, prelude::*, subclass::prelude::*};
use std::cell::RefCell;
mod imp { mod imp {
use super::*; use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)] #[derive(Properties, Debug, Default, gtk::CompositeTemplate)]
#[properties(wrapper_type = super::MusicusTile)]
#[template(file = "data/ui/tile.blp")] #[template(file = "data/ui/tile.blp")]
pub struct MusicusTile {} pub struct MusicusTile {
#[property(get, set)]
pub title: RefCell<String>,
#[property(get, set)]
pub subtitle: RefCell<Option<String>>,
#[template_child]
pub title_label: TemplateChild<gtk::Label>,
#[template_child]
pub subtitle_label: TemplateChild<gtk::Label>,
}
#[glib::object_subclass] #[glib::object_subclass]
impl ObjectSubclass for MusicusTile { 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<String>| Some(s.is_some()))
.build();
self.obj()
.bind_property("subtitle", &self.subtitle_label.get(), "label")
.sync_create()
.build();
}
}
impl WidgetImpl for MusicusTile {} impl WidgetImpl for MusicusTile {}
impl FlowBoxChildImpl for MusicusTile {} impl FlowBoxChildImpl for MusicusTile {}
} }
@ -33,7 +68,14 @@ glib::wrapper! {
} }
impl MusicusTile { impl MusicusTile {
pub fn new() -> Self { pub fn with_title(title: &str) -> Self {
glib::Object::new() 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()
} }
} }