Debian Codegraph @

podcasts-gtk/src/widgets/read_more_label.rs

raw symbols SCIP
1 use gtk::glib;
2 use gtk::prelude::*;
3 use gtk::subclass::prelude::*;
4
5 use std::cell::Cell;
6
7 use crate::i18n::i18n;
8
9 #[derive(Debug, Default)]
10 pub struct ReadMoreLabelPriv {
11 pub short_label: gtk::Label,
12 pub short_desc: gtk::Box,
13 pub long_label: gtk::Label,
14 pub button: gtk::Button,
15
16 pub expanded: Cell<bool>,
17 }
18
19 #[glib::object_subclass]
20 impl ObjectSubclass for ReadMoreLabelPriv {
21 const NAME: &'static str = "PdReadMoreLabel";
22 type Type = ReadMoreLabel;
23 type ParentType = gtk::Widget;
24 }
25
26 impl ObjectImpl for ReadMoreLabelPriv {
27 fn constructed(&self) {
28 self.parent_constructed();
29
30 let obj = self.obj();
31
32 self.button
33 .update_property(&[gtk::accessible::Property::Description(&i18n(
34 "Visually expands this description",
35 ))]);
36 self.button.set_label(&i18n("Read More"));
37 self.button.set_halign(gtk::Align::Center);
38 self.button.connect_clicked(glib::clone!(
39 #[weak]
40 obj,
41 move |_| {
42 obj.set_expanded(true);
43 }
44 ));
45
46 self.short_label.set_halign(gtk::Align::Center);
47 self.short_label.set_valign(gtk::Align::Center);
48 self.short_label.set_use_markup(true);
49 self.short_label.set_wrap(true);
50 // See https://gitlab.gnome.org/GNOME/gtk/-/issues/4714.
51 self.short_label.set_lines(4);
52 self.short_label
53 .set_wrap_mode(gtk::pango::WrapMode::WordChar);
54 self.short_label.set_justify(gtk::Justification::Center);
55 self.short_label
56 .set_ellipsize(gtk::pango::EllipsizeMode::End);
57
58 self.short_desc.set_orientation(gtk::Orientation::Vertical);
59 self.short_desc.set_spacing(6);
60 self.short_desc.append(&self.short_label);
61 self.short_desc.append(&self.button);
62 self.short_desc.set_child_visible(true);
63
64 self.long_label.set_use_markup(true);
65 self.long_label.set_justify(gtk::Justification::Center);
66 self.long_label.set_wrap(true);
67 self.long_label
68 .set_wrap_mode(gtk::pango::WrapMode::WordChar);
69 self.long_label.set_valign(gtk::Align::Center);
70 self.long_label.set_halign(gtk::Align::Center);
71 self.long_label.set_child_visible(false);
72
73 self.long_label.set_parent(obj.upcast_ref::<gtk::Widget>());
74 self.short_desc.set_parent(obj.upcast_ref::<gtk::Widget>());
75 }
76
77 fn dispose(&self) {
78 self.short_desc.unparent();
79 self.long_label.unparent();
80 }
81 }
82
83 impl WidgetImpl for ReadMoreLabelPriv {
84 fn measure(&self, orientation: gtk::Orientation, for_size: i32) -> (i32, i32, i32, i32) {
85 if self.expanded.get() {
86 let (min_h, nat_h, min_b, nat_b) = self.long_label.measure(orientation, for_size);
87 if orientation == gtk::Orientation::Vertical {
88 (nat_h, nat_h, min_b, nat_b)
89 } else {
90 (min_h, nat_h, min_b, nat_b)
91 }
92 } else {
93 self.short_desc.measure(orientation, for_size)
94 }
95 }
96
97 fn request_mode(&self) -> gtk::SizeRequestMode {
98 gtk::SizeRequestMode::WidthForHeight
99 }
100
101 fn size_allocate(&self, width: i32, height: i32, baseline: i32) {
102 let long_nat_h = self.long_label.measure(gtk::Orientation::Vertical, width).1;
103
104 // If we have enough space to allocate the long label, we directly
105 // allocate it.
106 if long_nat_h < height {
107 self.obj().set_expanded_inner(true);
108 }
109
110 if self.expanded.get() {
111 self.long_label.allocate(width, height, baseline, None);
112 } else {
113 self.short_desc.allocate(width, height, baseline, None);
114 }
115 }
116 }
117
118 glib::wrapper! {
119 pub struct ReadMoreLabel(ObjectSubclass<ReadMoreLabelPriv>)
120 @extends gtk::Widget,
121 @implements gtk::ConstraintTarget, gtk::Buildable, gtk::Accessible;
122 }
123
124 impl Default for ReadMoreLabel {
125 fn default() -> Self {
126 glib::Object::new()
127 }
128 }
129
130 impl ReadMoreLabel {
131 pub(crate) fn init(&self) {
132 self.set_focusable(true);
133 self.set_accessible_role(gtk::AccessibleRole::Label);
134 }
135
136 fn set_expanded_inner(&self, expanded: bool) {
137 let imp = self.imp();
138 if expanded == imp.expanded.replace(expanded) {
139 return;
140 }
141
142 // This should be only set once.
143 if !expanded {
144 return;
145 }
146
147 imp.long_label.set_child_visible(expanded);
148 imp.short_desc.set_child_visible(!expanded);
149 }
150
151 fn set_expanded(&self, expanded: bool) {
152 self.set_expanded_inner(expanded);
153
154 self.queue_resize();
155 }
156
157 pub fn set_label(&self, label: &str) {
158 let imp = self.imp();
159 let markup = glib::markup_escape_text(label);
160
161 let lines: Vec<&str> = markup.lines().collect();
162 if !lines.is_empty() {
163 imp.short_label.set_markup(lines[0]);
164 }
165
166 imp.long_label.set_markup(&markup);
167 self.update_property(&[gtk::accessible::Property::Label(label)]);
168 }
169 }