Debian Codegraph @

src/ui/folder_page_contents.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Fragments - folder_page_contents.rs
2 // Copyright (C) 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::OnceCell;
18
19 use adw::prelude::*;
20 use adw::subclass::prelude::*;
21 use gio::SimpleAction;
22 use glib::{clone, subclass, Properties};
23 use gtk::{gio, glib, CompositeTemplate};
24 use transmission_gobject::TrFile;
25
26 use crate::model::{FrgFileFilter, FrgFileSorter};
27
28 mod imp {
29 use super::*;
30
31 #[derive(Debug, Default, Properties, CompositeTemplate)]
32 #[template(resource = "/de/haeckerfelix/Fragments/gtk/folder_page_contents.ui")]
33 #[properties(wrapper_type = super::FrgFolderPageContents)]
34 pub struct FrgFolderPageContents {
35 #[property(get, set, construct_only)]
36 file: OnceCell<TrFile>,
37 #[property(get, set, construct_only)]
38 sorter: OnceCell<FrgFileSorter>,
39 #[property(get = Self::scroll_pos, set, construct_only, type = f64)]
40 scroll_pos: OnceCell<f64>,
41
42 #[template_child]
43 scrolled_window: TemplateChild<gtk::ScrolledWindow>,
44 #[template_child]
45 searchbar: TemplateChild<gtk::SearchBar>,
46 #[template_child]
47 searchentry: TemplateChild<gtk::SearchEntry>,
48 #[template_child]
49 listview: TemplateChild<gtk::ListView>,
50 #[template_child]
51 filter: TemplateChild<FrgFileFilter>,
52 #[template_child]
53 sort_model: TemplateChild<gtk::SortListModel>,
54
55 view_actions: gio::SimpleActionGroup,
56 }
57
58 #[glib::object_subclass]
59 impl ObjectSubclass for FrgFolderPageContents {
60 const NAME: &'static str = "FrgFolderPageContents";
61 type ParentType = adw::Bin;
62 type Type = super::FrgFolderPageContents;
63
64 fn class_init(klass: &mut Self::Class) {
65 klass.bind_template();
66 klass.bind_template_callbacks();
67 }
68
69 fn instance_init(obj: &subclass::InitializingObject<Self>) {
70 obj.init_template();
71 }
72 }
73
74 #[glib::derived_properties]
75 impl ObjectImpl for FrgFolderPageContents {
76 fn constructed(&self) {
77 self.parent_constructed();
78
79 self.sort_model.set_sorter(Some(&self.obj().sorter()));
80 self.sort_model
81 .set_model(Some(&self.obj().file().related()));
82
83 // Needed, since listview needs a moment to populate the list
84 glib::idle_add_local_once(clone!(@weak self as this => move|| {
85 this.scrolled_window.vadjustment().set_value(*this.scroll_pos.get().unwrap());
86 }));
87
88 // Search
89 self.searchbar.connect_search_mode_enabled_notify(
90 clone!(@weak self as this => move |sb| {
91 let file = this.obj().file();
92
93 let model: gio::ListModel = if sb.is_search_mode() {
94 this.filter.set_folder(Some(&file));
95 file.torrent().files().upcast()
96 } else {
97 this.filter.set_folder(None::<&TrFile>);
98 file.related().upcast()
99 };
100
101 this.sort_model.set_model(Some(&model));
102 }),
103 );
104 self.searchentry
105 .bind_property("text", &self.filter.get(), "search")
106 .build();
107
108 // Those actions are page specific, unlike those which are defined in
109 // FrgTorrentDialog
110 self.obj()
111 .insert_action_group("view", Some(&self.view_actions));
112 let mut action;
113
114 // view.select-all
115 action = SimpleAction::new("select-all", None);
116 action.connect_activate(clone!(@weak self as this => move |_, _| {
117 this.obj().file().set_wanted(true);
118 }));
119 self.view_actions.add_action(&action);
120
121 // view.deselect-all
122 action = SimpleAction::new("deselect-all", None);
123 action.connect_activate(clone!(@weak self as this => move |_, _| {
124 this.obj().file().set_wanted(false);
125 }));
126 self.view_actions.add_action(&action);
127 }
128 }
129
130 impl WidgetImpl for FrgFolderPageContents {}
131
132 impl BinImpl for FrgFolderPageContents {}
133
134 #[gtk::template_callbacks]
135 impl FrgFolderPageContents {
136 fn scroll_pos(&self) -> f64 {
137 self.scrolled_window.vadjustment().value()
138 }
139
140 #[template_callback]
141 fn row_activated(&self, pos: u32) {
142 let model = self.listview.model().unwrap();
143 let file = model.item(pos).unwrap().downcast::<TrFile>().unwrap();
144
145 if file.is_folder() {
146 self.obj()
147 .activate_action("view.show-folder", (&file.name().to_variant()).into())
148 .unwrap();
149 } else {
150 self.obj()
151 .activate_action("view.show-file", (&file.name().to_variant()).into())
152 .unwrap();
153 }
154 }
155 }
156 }
157
158 glib::wrapper! {
159 pub struct FrgFolderPageContents(ObjectSubclass<imp::FrgFolderPageContents>)
160 @extends gtk::Widget, adw::Bin;
161 }
162
163 impl FrgFolderPageContents {
164 pub fn new(file: &TrFile, sorter: &FrgFileSorter, scroll_pos: f64) -> Self {
165 glib::Object::builder()
166 .property("file", file)
167 .property("sorter", sorter)
168 .property("scroll-pos", scroll_pos)
169 .build()
170 }
171 }