| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use adw::prelude::*; |
| 21 |
use adw::subclass::prelude::*; |
| 22 |
use async_channel::Sender; |
| 23 |
use glib::clone; |
| 24 |
use glib::subclass::InitializingObject; |
| 25 |
use gtk::CompositeTemplate; |
| 26 |
use gtk::gio; |
| 27 |
use gtk::glib; |
| 28 |
use std::cell::RefCell; |
| 29 |
use std::rc::Rc; |
| 30 |
|
| 31 |
use crate::app::Action; |
| 32 |
use crate::chapter_parser::Chapter; |
| 33 |
use crate::widgets::player::{PlayerRate, PlayerTimes, PlayerWidget}; |
| 34 |
use crate::widgets::{Chapters, SheetDescription, SheetPlayer}; |
| 35 |
use podcasts_data::{Episode, EpisodeId, ShowCoverModel}; |
| 36 |
|
| 37 |
#[derive(Debug, CompositeTemplate, Default)] |
| 38 |
#[template(resource = "/org/gnome/Podcasts/gtk/sheet_base.ui")] |
| 39 |
pub(crate) struct SheetBasePriv { |
| 40 |
#[template_child] |
| 41 |
pub(crate) rate_container: TemplateChild<adw::Bin>, |
| 42 |
#[template_child] |
| 43 |
pub(crate) stack: TemplateChild<adw::ViewStack>, |
| 44 |
#[template_child] |
| 45 |
pub(crate) toggle_group: TemplateChild<adw::ToggleGroup>, |
| 46 |
#[template_child] |
| 47 |
pub(crate) player: TemplateChild<SheetPlayer>, |
| 48 |
#[template_child] |
| 49 |
pub(crate) description: TemplateChild<SheetDescription>, |
| 50 |
#[template_child] |
| 51 |
pub(crate) chapters: TemplateChild<Chapters>, |
| 52 |
#[template_child] |
| 53 |
pub(crate) chapters_toggle: TemplateChild<adw::Toggle>, |
| 54 |
|
| 55 |
rate: RefCell<Option<PlayerRate>>, |
| 56 |
} |
| 57 |
|
| 58 |
#[glib::object_subclass] |
| 59 |
impl ObjectSubclass for SheetBasePriv { |
| 60 |
const NAME: &'static str = "PdSheetBase"; |
| 61 |
type Type = SheetBase; |
| 62 |
type ParentType = adw::Bin; |
| 63 |
|
| 64 |
fn class_init(klass: &mut Self::Class) { |
| 65 |
SheetPlayer::ensure_type(); |
| 66 |
SheetDescription::ensure_type(); |
| 67 |
Chapters::ensure_type(); |
| 68 |
klass.bind_template(); |
| 69 |
} |
| 70 |
|
| 71 |
fn instance_init(obj: &InitializingObject<Self>) { |
| 72 |
obj.init_template(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
impl WidgetImpl for SheetBasePriv {} |
| 77 |
impl ObjectImpl for SheetBasePriv {} |
| 78 |
impl BinImpl for SheetBasePriv {} |
| 79 |
|
| 80 |
glib::wrapper! { |
| 81 |
pub(crate) struct SheetBase(ObjectSubclass<SheetBasePriv>) |
| 82 |
@extends adw::Bin, gtk::Widget, |
| 83 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 84 |
} |
| 85 |
|
| 86 |
impl SheetBase { |
| 87 |
pub(crate) fn new(rate: PlayerRate) -> Self { |
| 88 |
let this: Self = glib::Object::new(); |
| 89 |
let imp = this.imp(); |
| 90 |
imp.rate_container.set_child(Some(&rate.btn)); |
| 91 |
imp.rate.replace(Some(rate)); |
| 92 |
|
| 93 |
imp.toggle_group.connect_active_name_notify(clone!( |
| 94 |
#[weak] |
| 95 |
this, |
| 96 |
move |group| if let Some(name) = group.active_name() { |
| 97 |
this.imp().stack.set_visible_child_name(&name) |
| 98 |
}, |
| 99 |
)); |
| 100 |
|
| 101 |
this |
| 102 |
} |
| 103 |
|
| 104 |
pub(crate) fn init(&self, sender: &Sender<Action>, progress: >k::Scale) { |
| 105 |
self.imp().description.init(sender); |
| 106 |
self.imp().chapters.init(sender, progress); |
| 107 |
} |
| 108 |
|
| 109 |
pub(crate) fn initialize_episode(&self, episode: &Episode, show: &ShowCoverModel) { |
| 110 |
let imp = self.imp(); |
| 111 |
|
| 112 |
imp.chapters_toggle.set_enabled(false); |
| 113 |
imp.player.initialize_episode(episode, show); |
| 114 |
imp.description.initialize_episode(episode, show); |
| 115 |
imp.toggle_group.set_active_name(Some("player")); |
| 116 |
} |
| 117 |
|
| 118 |
pub(crate) fn on_rate_changed(&self, rate: f64) { |
| 119 |
self.rate().btn.set_label(&format!("{rate:.2}×")); |
| 120 |
} |
| 121 |
|
| 122 |
pub(crate) fn on_open_changed(&self, is_open: bool) { |
| 123 |
if !is_open { |
| 124 |
self.imp().toggle_group.set_active_name(Some("player")); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
pub(crate) fn chapters_available(&self, id: EpisodeId, chapters: Vec<Chapter>) { |
| 129 |
if chapters.is_empty() { |
| 130 |
return; |
| 131 |
} |
| 132 |
let imp = self.imp(); |
| 133 |
imp.chapters_toggle.set_enabled(true); |
| 134 |
imp.chapters.set_chapters(id, chapters); |
| 135 |
} |
| 136 |
|
| 137 |
pub(crate) fn on_play(&self) { |
| 138 |
self.imp().player.on_play(); |
| 139 |
} |
| 140 |
|
| 141 |
pub(crate) fn on_pause(&self) { |
| 142 |
self.imp().player.on_pause(); |
| 143 |
} |
| 144 |
|
| 145 |
pub(crate) fn on_stop(&self) { |
| 146 |
self.imp().player.on_stop(); |
| 147 |
} |
| 148 |
|
| 149 |
pub(crate) fn connect(&self, timer: &PlayerTimes) { |
| 150 |
self.imp().player.connect(timer); |
| 151 |
} |
| 152 |
|
| 153 |
pub(crate) fn connect_rate_actions(&self, group: &gio::SimpleActionGroup) { |
| 154 |
self.insert_action_group("rate", Some(group)); |
| 155 |
} |
| 156 |
|
| 157 |
pub(crate) fn connect_control_buttons(&self, player: &Rc<RefCell<PlayerWidget>>) { |
| 158 |
self.imp().player.connect_control_buttons(player); |
| 159 |
} |
| 160 |
|
| 161 |
pub(crate) fn rate(&self) -> PlayerRate { |
| 162 |
self.imp().rate.borrow().as_ref().unwrap().clone() |
| 163 |
} |
| 164 |
} |