-
Notifications
You must be signed in to change notification settings - Fork 0
/
piecesbar.js
277 lines (227 loc) · 10.1 KB
/
piecesbar.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Jesse Smick <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
'use strict';
if (window.qBittorrent === undefined) {
window.qBittorrent = {};
}
window.qBittorrent.PiecesBar = (() => {
const exports = () => {
return {
PiecesBar: PiecesBar
};
};
const STATUS_DOWNLOADING = 1;
const STATUS_DOWNLOADED = 2;
// absolute max width of 4096
// this is to support all browsers for size of canvas elements
// see https://github.com/jhildenbiddle/canvas-size#test-results
const MAX_CANVAS_WIDTH = 4096;
let piecesBarUniqueId = 0;
const PiecesBar = new Class({
initialize(pieces, parameters) {
const vals = {
'id': 'piecesbar_' + (piecesBarUniqueId++),
'width': 0,
'height': 0,
'downloadingColor': 'green',
'haveColor': 'blue',
'borderSize': 1,
'borderColor': '#999'
};
if (parameters && ($type(parameters) === 'object'))
$extend(vals, parameters);
vals.height = Math.max(vals.height, 12);
const obj = new Element('div', {
'id': vals.id,
'class': 'piecesbarWrapper',
'styles': {
'border': vals.borderSize.toString() + 'px solid ' + vals.borderColor,
'height': vals.height.toString() + 'px',
}
});
obj.vals = vals;
obj.vals.pieces = $pick(pieces, []);
obj.vals.canvas = new Element('canvas', {
'id': vals.id + '_canvas',
'class': 'piecesbarCanvas',
'width': (vals.width - (2 * vals.borderSize)).toString(),
'height': '1' // will stretch vertically to take up the height of the parent
});
obj.appendChild(obj.vals.canvas);
obj.setPieces = setPieces;
obj.refresh = refresh;
obj.clear = setPieces.bind(obj, []);
obj._drawStatus = drawStatus;
if (vals.width > 0)
obj.setPieces(vals.pieces);
else
setTimeout(() => { checkForParent(obj.id); }, 1);
return obj;
}
});
function setPieces(pieces) {
if (!Array.isArray(pieces))
pieces = [];
this.vals.pieces = pieces;
this.refresh(true);
}
function refresh(force) {
if (!this.parentNode)
return;
const pieces = this.vals.pieces;
// if the number of pieces is small, use that for the width,
// and have it stretch horizontally.
// this also limits the ratio below to >= 1
const width = Math.min(this.offsetWidth, pieces.length, MAX_CANVAS_WIDTH);
if ((this.vals.width === width) && !force)
return;
this.vals.width = width;
// change canvas size to fit exactly in the space
this.vals.canvas.width = width - (2 * this.vals.borderSize);
const canvas = this.vals.canvas;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
const imageWidth = canvas.width;
if (imageWidth.length === 0)
return;
let minStatus = Infinity;
let maxStatus = 0;
for (const status of pieces) {
if (status > maxStatus) {
maxStatus = status;
}
if (status < minStatus) {
minStatus = status;
}
}
// if no progress then don't do anything
if (maxStatus === 0)
return;
// if all pieces are downloaded, fill entire image at once
if (minStatus === STATUS_DOWNLOADED) {
ctx.fillStyle = this.vals.haveColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
return;
}
/* Linear transformation from pieces to pixels.
*
* The canvas size can vary in width so this figures out what to draw at each pixel.
* Inspired by the GUI code here https://github.com/qbittorrent/qBittorrent/blob/25b3f2d1a6b14f0fe098fb79a3d034607e52deae/src/gui/properties/downloadedpiecesbar.cpp#L54
*
* example ratio > 1 (at least 2 pieces per pixel)
* +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
* pieces | 2 | 1 | 2 | 0 | 2 | 0 | 1 | 0 | 1 | 2 |
* +---------+---------+---------+---------+---------+---------+
* pixels | | | | | | |
* +---------+---------+---------+---------+---------+---------+
*
* example ratio < 1 (at most 2 pieces per pixel)
* This case shouldn't happen since the max pixels are limited to the number of pieces
* +---------+---------+---------+---------+----------+--------+
* pieces | 2 | 1 | 1 | 0 | 2 | 2 |
* +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
* pixels | | | | | | | | | | |
* +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
*/
const ratio = pieces.length / imageWidth;
let lastValue = null;
let rectangleStart = 0;
// for each pixel compute its status based on the pieces
for (let x = 0; x < imageWidth; ++x) {
// find positions in the pieces array
const piecesFrom = x * ratio;
const piecesTo = (x + 1) * ratio;
const piecesToInt = Math.ceil(piecesTo);
const statusValues = {
[STATUS_DOWNLOADING]: 0,
[STATUS_DOWNLOADED]: 0
};
// aggregate the status of each piece that contributes to this pixel
for (let p = piecesFrom; p < piecesToInt; ++p) {
const piece = Math.floor(p);
const pieceStart = Math.max(piecesFrom, piece);
const pieceEnd = Math.min(piece + 1, piecesTo);
const amount = pieceEnd - pieceStart;
const status = pieces[piece];
if (status in statusValues)
statusValues[status] += amount;
}
// normalize to interval [0, 1]
statusValues[STATUS_DOWNLOADING] /= ratio;
statusValues[STATUS_DOWNLOADED] /= ratio;
// floats accumulate small errors, so smooth it out by rounding to hundredths place
// this effectively limits each status to a value 1 in 100
statusValues[STATUS_DOWNLOADING] = Math.round(statusValues[STATUS_DOWNLOADING] * 100) / 100;
statusValues[STATUS_DOWNLOADED] = Math.round(statusValues[STATUS_DOWNLOADED] * 100) / 100;
// float precision sometimes _still_ gives > 1
statusValues[STATUS_DOWNLOADING] = Math.min(statusValues[STATUS_DOWNLOADING], 1);
statusValues[STATUS_DOWNLOADED] = Math.min(statusValues[STATUS_DOWNLOADED], 1);
if (!lastValue) {
lastValue = statusValues;
}
// group contiguous colors together and draw as a single rectangle
if ((lastValue[STATUS_DOWNLOADING] === statusValues[STATUS_DOWNLOADING])
&& (lastValue[STATUS_DOWNLOADED] === statusValues[STATUS_DOWNLOADED])) {
continue;
}
const rectangleWidth = x - rectangleStart;
this._drawStatus(ctx, rectangleStart, rectangleWidth, lastValue);
lastValue = statusValues;
rectangleStart = x;
}
// fill a rect at the end of the canvas
if (rectangleStart < imageWidth) {
const rectangleWidth = imageWidth - rectangleStart;
this._drawStatus(ctx, rectangleStart, rectangleWidth, lastValue);
}
}
function drawStatus(ctx, start, width, statusValues) {
// mix the colors by using transparency and a composite mode
ctx.globalCompositeOperation = 'lighten';
if (statusValues[STATUS_DOWNLOADING]) {
ctx.globalAlpha = statusValues[STATUS_DOWNLOADING];
ctx.fillStyle = this.vals.downloadingColor;
ctx.fillRect(start, 0, width, ctx.canvas.height);
}
if (statusValues[STATUS_DOWNLOADED]) {
ctx.globalAlpha = statusValues[STATUS_DOWNLOADED];
ctx.fillStyle = this.vals.haveColor;
ctx.fillRect(start, 0, width, ctx.canvas.height);
}
}
function checkForParent(id) {
const obj = $(id);
if (!obj)
return;
if (!obj.parentNode)
return setTimeout(function() { checkForParent(id); }, 1);
obj.refresh();
}
return exports();
})();
Object.freeze(window.qBittorrent.PiecesBar);