blob: 5285e0dd8da6c95679465d80272e9eccb154185c (
plain) (
blame)
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
|
using Gee;
using Gdk;
using Gtk;
using Xmpp;
using Dino.Entities;
namespace Dino.Ui {
public class FileTransmissionProgress : Adw.Bin {
public enum State {
UNKNOWN_SOURCE,
DOWNLOAD_NOT_STARTED,
DOWNLOAD_NOT_STARTED_FAILED_BEFORE,
DOWNLOADING,
UPLOADING
}
public int64 file_size { get; set; }
public int64 transferred_size { get; set; }
public State state { get; set; }
private CssProvider css_provider = new CssProvider();
private Button button = new Button();
private uint64 next_update_time = 0;
private int64 last_progress_percent = 0;
private uint update_progress_timeout_id = -1;
construct {
add_css_class("circular-loading-indicator");
button.add_css_class("circular");
Adw.Bin holder = new Adw.Bin();
holder.set_child(button);
this.set_child(holder);
this.button.clicked.connect(on_button_clicked);
this.notify["transferred-size"].connect(on_transferred_size_update);
this.notify["state"].connect(on_state_changed);
on_state_changed();
}
private void on_transferred_size_update() {
if (update_progress_timeout_id == -1) {
int64 progress_percent = transferred_size * 100 / file_size;
if (progress_percent != last_progress_percent) {
uint64 time_now = get_monotonic_time() / 1000;
if (next_update_time > time_now) {
update_progress_timeout_id = Timeout.add((uint) (next_update_time - time_now), () => {
update_progress();
update_progress_timeout_id = -1;
return Source.REMOVE;
});
} else {
update_progress();
}
}
}
}
private void on_state_changed() {
sensitive = state != UNKNOWN_SOURCE;
switch (this.state) {
case UNKNOWN_SOURCE:
case DOWNLOAD_NOT_STARTED:
button.icon_name = "document-save-symbolic";
break;
case DOWNLOADING:
case UPLOADING:
button.icon_name = "small-x-symbolic";
break;
case DOWNLOAD_NOT_STARTED_FAILED_BEFORE:
button.icon_name = "dialog-warning-symbolic";
break;
}
}
private void update_progress() {
this.get_style_context().remove_provider(css_provider);
int64 progress_percent = transferred_size * 100 / file_size;
css_provider = Util.force_css(this, @"
.circular-loading-indicator {
background-image: conic-gradient(@accent_color $(progress_percent)%, transparent $(progress_percent)%);
}
");
next_update_time = get_monotonic_time() / 1000 + 500;
last_progress_percent = progress_percent;
}
private void on_button_clicked() {
switch (this.state) {
case UNKNOWN_SOURCE:
break;
case DOWNLOAD_NOT_STARTED_FAILED_BEFORE:
case DOWNLOAD_NOT_STARTED:
this.activate_action("file.download", null);
break;
case DOWNLOADING:
case UPLOADING:
this.activate_action("file.cancel", null);
break;
}
}
public override void dispose() {
if (update_progress_timeout_id != -1) {
Source.remove(update_progress_timeout_id);
update_progress_timeout_id = -1;
}
}
}
}
|