Debian Codegraph @

src/ui/torrent_group.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Fragments - torrent_group.rs
2 // Copyright (C) 2022-2023 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 }
65
66 impl FrgTorrentGroup {
67 pub fn add_model(&self, model: gtk::SortListModel) {
68 let imp = self.imp();
69
70 let listbox = gtk::ListBox::new();
71 listbox.add_css_class("content");
72 listbox.set_selection_mode(gtk::SelectionMode::None);
73 imp.listbox_box.append(&listbox);
74
75 listbox.bind_model(Some(&model), |object| {
76 let torrent = object.downcast_ref::<TrTorrent>().unwrap();
77 FrgTorrentRow::new(torrent).upcast::<gtk::Widget>()
78 });
79
80 listbox.connect_row_activated(move |_, row| {
81 row.activate_action("torrent.show-dialog", None).unwrap();
82 });
83
84 model.connect_items_changed(
85 glib::clone!(@weak self as this, @weak listbox => move |_, _, _, _| {
86 let imp = this.imp();
87
88 // Hide this group if all models are empty
89 let mut is_empty = true;
90 for m in &*imp.models.borrow() {
91 if m.n_items() != 0 {
92 is_empty = false;
93 }
94 }
95 this.set_visible(!is_empty);
96
97 // Hide empty listboxes
98 let mut child = imp.listbox_box.first_child ();
99 while let Some(widget) = child {
100 let listbox = widget.downcast_ref::<gtk::ListBox>().unwrap();
101 listbox.set_visible(listbox.row_at_index(0).is_some());
102 child = listbox.next_sibling();
103 }
104 }),
105 );
106
107 imp.models.borrow_mut().insert(0, model);
108 }
109 }