| 1 |
// Fragments - folder_page_contents.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::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!( |
| 85 |
#[weak(rename_to = this)] |
| 86 |
self, |
| 87 |
move || { |
| 88 |
this.scrolled_window |
| 89 |
.vadjustment() |
| 90 |
.set_value(*this.scroll_pos.get().unwrap()); |
| 91 |
} |
| 92 |
)); |
| 93 |
|
| 94 |
// Search |
| •95 |
self.searchbar.connect_search_mode_enabled_notify(clone!( |
| 96 |
#[weak(rename_to = this)] |
| 97 |
self, |
| 98 |
move |sb| { |
| 99 |
let file = this.obj().file(); |
| 100 |
|
| 101 |
let model: gio::ListModel = if sb.is_search_mode() { |
| 102 |
this.filter.set_folder(Some(&file)); |
| 103 |
file.torrent().files().upcast() |
| 104 |
} else { |
| 105 |
this.filter.set_folder(None::<&TrFile>); |
| 106 |
file.related().upcast() |
| 107 |
}; |
| 108 |
|
| 109 |
this.sort_model.set_model(Some(&model)); |
| •110 |
} |
| 111 |
)); |
| 112 |
self.searchentry |
| 113 |
.bind_property("text", &self.filter.get(), "search") |
| 114 |
.build(); |
| 115 |
|
| 116 |
// Those actions are page specific, unlike those which are defined in |
| 117 |
// FrgTorrentDialog |
| 118 |
self.obj() |
| 119 |
.insert_action_group("view", Some(&self.view_actions)); |
| 120 |
let mut action; |
| 121 |
|
| 122 |
// view.select-all |
| 123 |
action = SimpleAction::new("select-all", None); |
| •124 |
action.connect_activate(clone!( |
| 125 |
#[weak(rename_to = this)] |
| 126 |
self, |
| 127 |
move |_, _| { |
| 128 |
this.obj().file().set_wanted(true); |
| 129 |
} |
| 130 |
)); |
| 131 |
self.view_actions.add_action(&action); |
| 132 |
|
| 133 |
// view.deselect-all |
| 134 |
action = SimpleAction::new("deselect-all", None); |
| •135 |
action.connect_activate(clone!( |
| 136 |
#[weak(rename_to = this)] |
| 137 |
self, |
| 138 |
move |_, _| { |
| 139 |
this.obj().file().set_wanted(false); |
| 140 |
} |
| 141 |
)); |
| 142 |
self.view_actions.add_action(&action); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl WidgetImpl for FrgFolderPageContents {} |
| 147 |
|
| 148 |
impl BinImpl for FrgFolderPageContents {} |
| 149 |
|
| 150 |
#[gtk::template_callbacks] |
| 151 |
impl FrgFolderPageContents { |
| 152 |
fn scroll_pos(&self) -> f64 { |
| 153 |
self.scrolled_window.vadjustment().value() |
| 154 |
} |
| 155 |
|
| 156 |
#[template_callback] |
| 157 |
fn row_activated(&self, pos: u32) { |
| 158 |
let model = self.listview.model().unwrap(); |
| 159 |
let file = model.item(pos).unwrap().downcast::<TrFile>().unwrap(); |
| 160 |
|
| 161 |
if file.is_folder() { |
| 162 |
self.obj() |
| 163 |
.activate_action("view.show-folder", (&file.name().to_variant()).into()) |
| 164 |
.unwrap(); |
| 165 |
} else { |
| 166 |
self.obj() |
| 167 |
.activate_action("view.show-file", (&file.name().to_variant()).into()) |
| 168 |
.unwrap(); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
glib::wrapper! { |
| 175 |
pub struct FrgFolderPageContents(ObjectSubclass<imp::FrgFolderPageContents>) |
| •176 |
@extends gtk::Widget, adw::Bin, |
| 177 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 178 |
} |
| 179 |
|
| 180 |
impl FrgFolderPageContents { |
| 181 |
pub fn new(file: &TrFile, sorter: &FrgFileSorter, scroll_pos: f64) -> Self { |
| 182 |
glib::Object::builder() |
| 183 |
.property("file", file) |
| 184 |
.property("sorter", sorter) |
| 185 |
.property("scroll-pos", scroll_pos) |
| 186 |
.build() |
| 187 |
} |
| 188 |
} |