1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::cell::{Cell, RefCell};
use std::time::Duration;
use adw::prelude::*;
use adw::subclass::prelude::*;
use adw::{PropertyAnimationTarget, TimedAnimation};
use glib::{clone, ParamSpec, Properties};
use gtk::glib;
use once_cell::unsync::OnceCell;
use crate::main::task::SkTask;
mod imp {
use super::*;
#[derive(Debug, Default, Properties)]
#[properties(wrapper_type = super::SkTaskProgressBar)]
pub struct SkTaskProgressBar {
#[property(get, set = Self::set_task)]
task: RefCell<Option<SkTask>>,
progressbar: gtk::ProgressBar,
animation: OnceCell<TimedAnimation>,
is_pulsing: Cell<bool>,
watches: RefCell<Vec<gtk::ExpressionWatch>>,
}
#[glib::object_subclass]
impl ObjectSubclass for SkTaskProgressBar {
const NAME: &'static str = "SkTaskProgressBar";
type ParentType = adw::Bin;
type Type = super::SkTaskProgressBar;
}
impl ObjectImpl for SkTaskProgressBar {
fn properties() -> &'static [ParamSpec] {
Self::derived_properties()
}
fn property(&self, id: usize, pspec: &ParamSpec) -> glib::Value {
Self::derived_property(self, id, pspec)
}
fn set_property(&self, id: usize, value: &glib::Value, pspec: &ParamSpec) {
Self::derived_set_property(self, id, value, pspec)
}
fn constructed(&self) {
self.parent_constructed();
self.progressbar.set_pulse_step(1.0);
self.progressbar.set_valign(gtk::Align::Center);
self.obj().set_child(Some(&self.progressbar));
let target = PropertyAnimationTarget::new(&self.progressbar, "fraction");
let animation = TimedAnimation::new(&self.progressbar, 0.0, 0.0, 1000, target);
self.animation.set(animation).unwrap();
}
fn dispose(&self) {
let animation = self.animation.get().unwrap();
animation.set_target(&adw::CallbackAnimationTarget::new(|_| ()));
}
}
impl WidgetImpl for SkTaskProgressBar {}
impl BinImpl for SkTaskProgressBar {}
impl SkTaskProgressBar {
fn set_task(&self, task: Option<&SkTask>) {
while let Some(watch) = self.watches.borrow_mut().pop() {
watch.unwatch();
}
if let Some(task) = task {
let watch = task.property_expression("progress").watch(
glib::Object::NONE,
clone!(@weak self as this => move|| this.update_fraction()),
);
self.watches.borrow_mut().push(watch);
let watch = task.property_expression("status").watch(
glib::Object::NONE,
clone!(@weak self as this => move|| this.update()),
);
self.watches.borrow_mut().push(watch);
}
*self.task.borrow_mut() = task.cloned();
self.obj().notify("task");
self.update_fraction();
}
fn update(&self) {
if let Some(task) = self.obj().task() {
if task.status().has_no_detailed_progress() && !self.is_pulsing.get() {
self.is_pulsing.set(true);
glib::timeout_add_local(
Duration::from_secs(1),
clone!(@weak self as this => @default-return Continue(false), move || {
let is_pulsing = this.is_pulsing.get();
if is_pulsing {
this.progressbar.pulse();
} else {
this.update_fraction();
}
Continue(is_pulsing)
}),
);
} else {
self.is_pulsing.set(false);
}
}
}
fn update_fraction(&self) {
let animation = self.animation.get().unwrap();
if let Some(task) = self.obj().task() {
animation.skip();
let current_value = animation.value();
animation.reset();
animation.set_value_from(current_value);
animation.set_value_to(task.progress() as f64);
animation.play();
}
}
}
}
glib::wrapper! {
pub struct SkTaskProgressBar(
ObjectSubclass<imp::SkTaskProgressBar>)
@extends gtk::Widget, adw::Bin;
}
impl SkTaskProgressBar {
pub fn new() -> Self {
glib::Object::new()
}
}
impl Default for SkTaskProgressBar {
fn default() -> Self {
Self::new()
}
}