Debian Codegraph @

podcasts-gtk/src/widgets/chapters.rs

raw symbols SCIP
1 // chapters.rs
2 //
3 // Copyright 2025 nee <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use adw::prelude::*;
21 use adw::subclass::prelude::*;
22 use anyhow::Result;
23 use async_channel::Sender;
24 use glib::Properties;
25 use glib::clone;
26 use glib::subclass::InitializingObject;
27 use gtk::CompositeTemplate;
28 use gtk::glib;
29 use std::cell::{Cell, RefCell};
30
31 use crate::app::Action;
32 use crate::chapter_parser::Chapter;
33 use crate::i18n::i18n;
34 use podcasts_data::EpisodeId;
35 use podcasts_data::dbqueries;
36
37 #[derive(Debug, CompositeTemplate, Default, Properties)]
38 #[template(resource = "/org/gnome/Podcasts/gtk/chapters.ui")]
39 #[properties(wrapper_type = Chapters)]
40 pub struct ChaptersPriv {
41 #[template_child]
42 episode: TemplateChild<gtk::Label>,
43 #[template_child]
44 show: TemplateChild<gtk::Label>,
45 #[template_child]
46 listbox: TemplateChild<gtk::ListBox>,
47
48 chapters: RefCell<Vec<Chapter>>,
49 episode_id: Cell<Option<EpisodeId>>,
50 sender: RefCell<Option<Sender<Action>>>,
51 active_chapter_icon: RefCell<Option<gtk::Image>>,
52
53 #[property(get, set)]
54 progress: Cell<f64>,
55 #[property(get, set)]
56 active_chapter_index: Cell<i32>,
57 }
58
59 impl ChaptersPriv {
60 fn set_labels(&self, id: EpisodeId) -> Result<()> {
61 let ep = dbqueries::get_episode_widget_from_id(id)?;
62 self.episode.set_text(ep.title());
63
64 let show = dbqueries::get_podcast_from_id(ep.show_id())?;
65 self.show.set_text(show.title());
66 Ok(())
67 }
68
69 fn fill_chapters_list(&self, chapters: &[Chapter]) {
70 self.listbox.remove_all();
71 for c in chapters.iter() {
72 let s = c.start.num_seconds();
73 let duration = {
74 let seconds = s % 60;
75 let minutes = (s / 60) % 60;
76 let hours = (s / 60) / 60;
77 format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
78 };
79
80 let row = adw::ActionRow::new();
81 row.set_action_name(Some("jump-to-second"));
82 row.set_action_target_value(Some(&(s as i32).to_variant()));
83 row.set_activatable(true);
84 row.set_title(&duration);
85 row.set_subtitle(&c.title);
86 row.add_css_class("property");
87 if !c.description.is_empty() {
88 row.set_tooltip_text(Some(&c.description));
89 }
90 self.listbox.append(&row);
91 }
92 }
93
94 fn update_active_chapter(&self) {
95 let seconds = self.progress.get();
96 let old_index = self.active_chapter_index.get();
97 let mut new_index = 0;
98 for c in self.chapters.borrow().iter() {
99 if c.start.num_seconds() as f64 > seconds {
100 break;
101 }
102 new_index += 1;
103 }
104 new_index = (new_index - 1).max(0);
105
106 if old_index != new_index {
107 self.active_chapter_index.set(new_index);
108 self.active_chapter_changed(old_index);
109 }
110 }
111
112 fn active_chapter_changed(&self, old_index: i32) {
113 println!("CHAP {}", self.active_chapter_index.get());
114 if let Some(icon) = self.active_chapter_icon.borrow().as_ref() {
115 if let Some(row) = self
116 .listbox
117 .row_at_index(old_index)
118 .and_then(|r| r.downcast::<adw::ActionRow>().ok())
119 {
120 row.remove(icon);
121 }
122 }
123 if let Some(row) = self
124 .listbox
125 .row_at_index(self.active_chapter_index.get())
126 .and_then(|r| r.downcast::<adw::ActionRow>().ok())
127 {
128 let icon = gtk::Image::from_icon_name("media-playback-start-symbolic");
129 icon.set_pixel_size(12);
130 icon.set_tooltip_text(Some(&i18n("Currently playing chapter")));
131 row.add_suffix(&icon);
132 self.active_chapter_icon.replace(Some(icon));
133 }
134 }
135 }
136
137 #[glib::object_subclass]
138 impl ObjectSubclass for ChaptersPriv {
139 const NAME: &'static str = "PdChapters";
140 type Type = Chapters;
141 type ParentType = adw::Bin;
142
143 fn class_init(klass: &mut Self::Class) {
144 klass.bind_template();
145 klass.install_action(
146 "jump-to-second",
147 Some(glib::VariantTy::INT32),
148 move |this, _, value| {
149 if let Some(second) = value.and_then(|v| v.get::<i32>()) {
150 this.jump_to_second(second);
151 }
152 },
153 );
154 }
155
156 fn instance_init(obj: &InitializingObject<Self>) {
157 obj.init_template();
158 }
159 }
160
161 #[glib::derived_properties]
162 impl ObjectImpl for ChaptersPriv {}
163 impl WidgetImpl for ChaptersPriv {}
164 impl BinImpl for ChaptersPriv {}
165
166 glib::wrapper! {
167 pub struct Chapters(ObjectSubclass<ChaptersPriv>)
168 @extends adw::Bin, gtk::Widget,
169 @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
170 }
171
172 impl Chapters {
173 pub(crate) fn new(
174 sender: &Sender<Action>,
175 slider: &gtk::Scale,
176 ep: EpisodeId,
177 chapters: Vec<Chapter>,
178 ) -> Self {
179 let this: Self = glib::Object::new();
180 this.init(sender, slider);
181 this.set_chapters(ep, chapters);
182 this
183 }
184
185 pub(crate) fn new_page(
186 sender: &Sender<Action>,
187 slider: &gtk::Scale,
188 ep: EpisodeId,
189 chapters: Vec<Chapter>,
190 ) -> adw::NavigationPage {
191 let widget = Self::new(sender, slider, ep, chapters);
192 let view = adw::ToolbarView::builder().content(&widget).build();
193 view.add_top_bar(&adw::HeaderBar::new());
194 adw::NavigationPage::with_tag(&view, &i18n("Chapters"), "chapters")
195 }
196
197 pub(crate) fn init(&self, sender: &Sender<Action>, slider: &gtk::Scale) {
198 let imp = self.imp();
199 imp.sender.replace(Some(sender.clone()));
200
201 slider.connect_value_changed(clone!(
202 #[weak]
203 imp,
204 move |slider| {
205 let seconds = slider.value();
206 imp.progress.set(seconds);
207 imp.update_active_chapter();
208 }
209 ));
210 imp.progress.set(slider.value());
211 imp.update_active_chapter();
212 }
213
214 fn jump_to_second(&self, second: i32) {
215 let sender = self.imp().sender.borrow();
216 if let (Some(id), Some(sender)) = (self.imp().episode_id.get(), sender.clone()) {
217 send_blocking!(sender, Action::InitEpisodeAt(id, second));
218 }
219 }
220
221 pub(crate) fn set_chapters(&self, ep: EpisodeId, chapters: Vec<Chapter>) {
222 let imp = self.imp();
223 imp.episode_id.set(Some(ep));
224 let _ = imp.set_labels(ep);
225 imp.fill_chapters_list(&chapters);
226 imp.chapters.replace(chapters);
227 imp.update_active_chapter();
228 }
229 }