Debian Codegraph @

src/ui/torrent_group.rs

raw ◀ ▶
1 Debian patch touch this file
1 // Fragments - torrent_group.rs
•2 // Copyright (C) 2022-2025 Felix Häcker <[email protected]>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 use std::cell::RefCell;
18
19 use adw::subclass::prelude::*;
20 use glib::subclass;
21 use gtk::prelude::*;
22 use gtk::{glib, CompositeTemplate};
23 use transmission_gobject::TrTorrent;
24
25 use crate::ui::FrgTorrentRow;
26
27 mod imp {
28 use super::*;
29
30 #[derive(Default, Debug, CompositeTemplate)]
31 #[template(resource = "/de/haeckerfelix/Fragments/gtk/torrent_group.ui")]
32 pub struct FrgTorrentGroup {
33 #[template_child]
34 pub listbox_box: TemplateChild<gtk::Box>,
35 pub models: RefCell<Vec<gtk::SortListModel>>,
36 }
37
38 #[glib::object_subclass]
39 impl ObjectSubclass for FrgTorrentGroup {
40 const NAME: &'static str = "FrgTorrentGroup";
41 type ParentType = adw::PreferencesGroup;
42 type Type = super::FrgTorrentGroup;
43
44 fn class_init(klass: &mut Self::Class) {
45 klass.bind_template();
46 }
47
48 fn instance_init(obj: &subclass::InitializingObject<Self>) {
49 obj.init_template();
50 }
51 }
52
53 impl ObjectImpl for FrgTorrentGroup {}
54
55 impl WidgetImpl for FrgTorrentGroup {}
56
57 impl PreferencesGroupImpl for FrgTorrentGroup {}
58 }
59
60 glib::wrapper! {
61 pub struct FrgTorrentGroup(
62 ObjectSubclass<imp::FrgTorrentGroup>)
•63 @extends gtk::Widget, adw::PreferencesGroup,
64 @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
65 }
66
67 impl FrgTorrentGroup {
68 pub fn add_model(&self, model: gtk::SortListModel) {
69 let imp = self.imp();
70
71 let listbox = gtk::ListBox::new();
72 listbox.add_css_class("content");
73 listbox.set_selection_mode(gtk::SelectionMode::None);
74 imp.listbox_box.append(&listbox);
75
76 listbox.bind_model(Some(&model), |object| {
77 let torrent = object.downcast_ref::<TrTorrent>().unwrap();
78 FrgTorrentRow::new(torrent).upcast::<gtk::Widget>()
79 });
80
81 listbox.connect_row_activated(move |_, row| {
82 row.activate_action("torrent.show-dialog", None).unwrap();
83 });
84
•85 model.connect_items_changed(glib::clone!(
86 #[weak(rename_to = this)]
87 self,
88 move |_, _, _, _| {
89 let imp = this.imp();
90
91 // Hide this group if all models are empty
92 let mut is_empty = true;
93 for m in &*imp.models.borrow() {
94 if m.n_items() != 0 {
95 is_empty = false;
96 }
97 }
98 this.set_visible(!is_empty);
99
100 // Hide empty listboxes
•101 let mut child = imp.listbox_box.first_child();
102 while let Some(widget) = child {
103 let listbox = widget.downcast_ref::<gtk::ListBox>().unwrap();
104 listbox.set_visible(listbox.row_at_index(0).is_some());
105 child = listbox.next_sibling();
106 }
•107 }
108 ));
109
110 imp.models.borrow_mut().insert(0, model);
111 }
112 }