Debian Codegraph @

podcasts-gtk/src/widgets/shows_view.rs

raw symbols SCIP
1 // shows_view.rs
2 //
3 // Copyright 2017 Jordan Petridis <[email protected]>
4 // Copyright 2024 nee <[email protected]>
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // SPDX-License-Identifier: GPL-3.0-or-later
20
21 use adw::prelude::*;
22 use adw::subclass::prelude::*;
23 use anyhow::{Result, anyhow};
24 use async_channel::Sender;
25 use glib::clone;
26 use glib::object::Object;
27 use gtk::gio;
28 use gtk::glib;
29 use std::cell::Cell;
30 use std::cell::RefCell;
31 use std::cell::RefMut;
32 use std::sync::Arc;
33
34 use crate::app::Action;
35 use crate::download_covers::load_widget_texture;
36 use crate::i18n::i18n;
37 use crate::utils::get_ignored_shows;
38 use crate::widgets::BaseView;
39 use podcasts_data::dbqueries;
40 use podcasts_data::{Show, ShowId};
41
42 #[derive(Debug, Default)]
43 pub struct ShowsViewPriv {
44 view: BaseView,
45 grid: gtk::GridView,
46 }
47
48 #[glib::object_subclass]
49 impl ObjectSubclass for ShowsViewPriv {
50 const NAME: &'static str = "PdShowsView";
51 type Type = super::ShowsView;
52 type ParentType = adw::Bin;
53 }
54
55 impl ObjectImpl for ShowsViewPriv {
56 fn constructed(&self) {
57 self.parent_constructed();
58 let missing_icon = load_missing_icon();
59 let factory = gtk::SignalListItemFactory::new();
60 factory.connect_setup(clone!(
61 #[strong]
62 missing_icon,
63 move |_factory, item| {
64 let item = item.downcast_ref::<gtk::ListItem>().unwrap();
65 // TODO: Make this a widget with completed/fetch-error info overlays
66 let picture = gtk::Picture::builder()
67 .width_request(150)
68 .height_request(150)
69 .can_focus(false)
70 .build();
71 picture.set_paintable(missing_icon.as_ref());
72 picture.add_css_class("flat");
73 picture.add_css_class("rounded-big");
74 picture.add_css_class("show-button");
75 picture.set_content_fit(gtk::ContentFit::ScaleDown);
76
77 item.set_child(Some(&picture));
78 }
79 ));
80 factory.connect_bind(move |_factory, item| {
81 let item = item.downcast_ref::<gtk::ListItem>().unwrap();
82 let data = item.item().and_downcast::<ShowCoverModel>().unwrap();
83 let child = item.child().and_downcast::<gtk::Picture>().unwrap();
84
85 let id = data.show_id();
86 let load_handle = load_widget_texture(&child, id, crate::Thumb256, true);
87 let mut load_handle_store = data.get_mut_load_handle();
88 *load_handle_store = Some(load_handle);
89 });
90 factory.connect_unbind(move |_factory, item| {
91 let item = item.downcast_ref::<gtk::ListItem>().unwrap();
92 let data = item.item().and_downcast::<ShowCoverModel>().unwrap();
93 let child = item.child().and_downcast::<gtk::Picture>().unwrap();
94 // cancel loading the picture
95 if let Some(handle) = data.get_mut_load_handle().take() {
96 handle.abort();
97 }
98 child.set_paintable(missing_icon.as_ref());
99 });
100
101 self.grid.set_factory(Some(&factory));
102 self.grid.set_single_click_activate(true);
103 self.grid.set_can_focus(true);
104 self.grid.set_vexpand(true);
105 self.grid.set_hexpand(true);
106 self.grid.set_min_columns(2);
107 self.grid.set_max_columns(7);
108 self.grid.set_valign(gtk::Align::Fill);
109 self.grid.set_halign(gtk::Align::Fill);
110 self.grid.set_height_request(500);
111 // makes tabbing down to the player widget is easier.
112 self.grid.set_tab_behavior(gtk::ListTabBehavior::Item);
113 self.grid.add_css_class("shows-grid");
114 self.grid.set_vscroll_policy(gtk::ScrollablePolicy::Natural);
115 self.grid
116 // Translators: Shows as a noun, meaning Podcast-Shows.
117 .update_property(&[gtk::accessible::Property::Label(&i18n("Shows"))]);
118
119 let clamp = adw::ClampScrollable::builder()
120 .child(&self.grid)
121 .valign(gtk::Align::Fill)
122 .halign(gtk::Align::Fill)
123 .vscroll_policy(gtk::ScrollablePolicy::Natural)
124 .orientation(gtk::Orientation::Horizontal)
125 .maximum_size((256 + 6 + 6) * 7) // picture + paddings * max_columns
126 .build();
127 self.view.set_content(&clamp);
128 self.obj().set_child(Some(&self.view));
129 }
130 }
131
132 fn load_missing_icon() -> Option<gtk::IconPaintable> {
133 let display = gtk::gdk::Display::default()?;
134 // get the max scale form any of the monitors
135 let scale = display.monitors().into_iter().fold(1, |acc, m| {
136 let m_scale = (|| Some(m.ok()?.downcast::<gtk::gdk::Monitor>().ok()?.scale_factor()))()
137 .unwrap_or(acc);
138 std::cmp::max(acc, m_scale)
139 });
140 let theme = gtk::IconTheme::for_display(&display);
141 if theme.has_icon("image-missing-symbolic") {
142 Some(theme.lookup_icon(
143 "image-missing-symbolic",
144 &[],
145 128, // 1/2 size of picture to get padding
146 scale,
147 gtk::TextDirection::Ltr,
148 gtk::IconLookupFlags::FORCE_SYMBOLIC,
149 ))
150 } else {
151 None
152 }
153 }
154
155 impl WidgetImpl for ShowsViewPriv {}
156 impl BinImpl for ShowsViewPriv {}
157
158 impl ShowsViewPriv {
159 fn set_data(&self) {
160 let this = self.downgrade();
161 crate::MAINCONTEXT.spawn_local_with_priority(
162 glib::source::Priority::DEFAULT_IDLE,
163 async move {
164 let data = gio::spawn_blocking(get_episodes).await;
165 if let Ok(Ok(podcasts)) = data {
166 let model = gio::ListStore::new::<ShowCoverModel>();
167 for pod in podcasts {
168 let item = ShowCoverModel::new(pod.id());
169 model.append(&item);
170 }
171 if let Some(this) = this.upgrade() {
172 let selection_model = gtk::NoSelection::new(Some(model));
173 this.grid.set_model(Some(&selection_model));
174 }
175 }
176 },
177 );
178 }
179 }
180
181 fn get_episodes() -> Result<Vec<Show>> {
182 let ignore = get_ignored_shows()?;
183 let podcasts = dbqueries::get_podcasts_filter(&ignore)?;
184 Ok(podcasts)
185 }
186
187 glib::wrapper! {
188 pub struct ShowsView(ObjectSubclass<ShowsViewPriv>)
189 @extends gtk::Widget, adw::Bin,
190 @implements gtk::ConstraintTarget, gtk::Buildable, gtk::Accessible;
191 }
192
193 impl ShowsView {
194 pub(crate) fn new(sender: Sender<Action>) -> Self {
195 let this: Self = glib::Object::new();
196 this.imp().set_data();
197 this.imp().grid.connect_activate(move |gridview, index| {
198 if let Err(err) = on_child_activate(gridview, index, &sender) {
199 error!("Failed to activated ShowCover {err}");
200 }
201 });
202
203 this
204 }
205
206 pub fn update_model(&self) {
207 self.imp().set_data();
208 }
209 }
210
211 fn on_child_activate(gridview: &gtk::GridView, index: u32, sender: &Sender<Action>) -> Result<()> {
212 let id = gridview
213 .model()
214 .ok_or(anyhow!("no model in gridview"))?
215 .item(index)
216 .ok_or(anyhow!("clicked show not found in gridview model"))?
217 .downcast::<ShowCoverModel>()
218 .unwrap()
219 .show_id();
220 let pd = Arc::new(dbqueries::get_podcast_from_id(id)?);
221 send_blocking!(sender, Action::GoToShow(pd));
222 Ok(())
223 }
224
225 // Model data type
226 // -------------------------------------------------------------------
227 #[derive(Debug, Default)]
228 pub struct ShowCoverModelPrivate {
229 pub show_id: Cell<i32>,
230 pub load_handle: RefCell<Option<glib::JoinHandle<()>>>,
231 }
232
233 #[glib::object_subclass]
234 impl ObjectSubclass for ShowCoverModelPrivate {
235 const NAME: &'static str = "PdShowCoverModel";
236 type Type = ShowCoverModel;
237 type ParentType = Object;
238 }
239
240 impl ObjectImpl for ShowCoverModelPrivate {}
241
242 gtk::glib::wrapper! {
243 pub struct ShowCoverModel(ObjectSubclass<ShowCoverModelPrivate>);
244 }
245
246 impl ShowCoverModel {
247 pub(crate) fn new(id: ShowId) -> Self {
248 let self_: Self = glib::Object::new();
249 self_.imp().show_id.set(id.0);
250 self_
251 }
252
253 fn show_id(&self) -> ShowId {
254 ShowId(self.imp().show_id.get())
255 }
256
257 fn get_mut_load_handle(&self) -> RefMut<'_, Option<glib::JoinHandle<()>>> {
258 self.imp().load_handle.borrow_mut()
259 }
260 }
261 // -------------------------------------------------------------------