Debian Codegraph @

src/ui/status_header.rs

raw ◀ ▶
1 Debian patch touch this file
1 // Fragments - status_header.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::{Cell, RefCell};
18
19 use adw::subclass::prelude::*;
20 use glib::{subclass, Properties};
21 use gtk::prelude::*;
22 use gtk::{glib, CompositeTemplate};
23
24 use crate::utils;
25
26 mod imp {
27 use super::*;
28
29 #[derive(Debug, Default, CompositeTemplate, Properties)]
30 #[template(resource = "/de/haeckerfelix/Fragments/gtk/status_header.ui")]
31 #[properties(wrapper_type = super::FrgStatusHeader)]
32 pub struct FrgStatusHeader {
33 #[property(get, set = Self::set_mimetype)]
34 mimetype: RefCell<String>,
35 #[property(get, set)]
36 title: RefCell<String>,
37 #[property(get, set = Self::set_downloaded_bytes)]
38 downloaded_bytes: Cell<u64>,
39 #[property(get, set = Self::set_total_bytes)]
40 total_bytes: Cell<u64>,
41 #[property(get, set)]
42 eta: RefCell<String>,
43
44 #[template_child]
45 mimetype_image: TemplateChild<gtk::Image>,
46 #[template_child]
47 title_label: TemplateChild<gtk::Label>,
48 #[template_child]
49 downloaded_label: TemplateChild<gtk::Label>,
50 #[template_child]
51 total_label: TemplateChild<gtk::Label>,
52 #[template_child]
53 eta_label: TemplateChild<gtk::Label>,
54 #[template_child]
55 progressbar: TemplateChild<gtk::ProgressBar>,
56 }
57
58 #[glib::object_subclass]
59 impl ObjectSubclass for FrgStatusHeader {
60 const NAME: &'static str = "FrgStatusHeader";
61 type ParentType = adw::Bin;
62 type Type = super::FrgStatusHeader;
63
64 fn class_init(klass: &mut Self::Class) {
65 klass.bind_template();
66 }
67
68 fn instance_init(obj: &subclass::InitializingObject<Self>) {
69 obj.init_template();
70 }
71 }
72
73 #[glib::derived_properties]
74 impl ObjectImpl for FrgStatusHeader {
75 fn constructed(&self) {
76 self.parent_constructed();
77
78 self.obj()
79 .bind_property("title", &self.title_label.get(), "label")
80 .sync_create()
81 .build();
82 self.obj()
83 .bind_property("eta", &self.eta_label.get(), "label")
84 .sync_create()
85 .build();
86 }
87 }
88
89 impl WidgetImpl for FrgStatusHeader {}
90
91 impl BinImpl for FrgStatusHeader {}
92
93 impl FrgStatusHeader {
94 pub fn set_downloaded_bytes(&self, bytes: u64) {
95 let downloaded = utils::format_size(bytes.try_into().unwrap());
96 self.downloaded_label.set_text(&downloaded);
97
98 self.downloaded_bytes.set(bytes);
99 self.update_progressbar();
100 }
101
102 pub fn set_total_bytes(&self, bytes: u64) {
103 let total = utils::format_size(bytes.try_into().unwrap());
104 self.total_label.set_text(&total);
105
106 self.total_bytes.set(bytes);
107 self.update_progressbar();
108 }
109
110 pub fn set_mimetype(&self, mimetype: &str) {
111 utils::set_mimetype_image(mimetype, &self.mimetype_image.get());
112 }
113
114 fn update_progressbar(&self) {
115 let f = self.obj().downloaded_bytes() as f64 / self.obj().total_bytes() as f64;
116 self.progressbar.set_fraction(f);
117 }
118 }
119 }
120
121 glib::wrapper! {
122 pub struct FrgStatusHeader(ObjectSubclass<imp::FrgStatusHeader>)
•123 @extends gtk::Widget, adw::Bin,
124 @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
125 }
126
127 impl FrgStatusHeader {
128 pub fn new() -> Self {
129 glib::Object::new()
130 }
131 }
132
133 impl Default for FrgStatusHeader {
134 fn default() -> Self {
135 Self::new()
136 }
137 }