| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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) |
| 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 |
|
| 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, |
| 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: >k::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 |
|
| 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 |
|