Skip to content

Commit eed1d95

Browse files
committed
Refactor progress bar to Class
1 parent 3f41cd7 commit eed1d95

2 files changed

Lines changed: 82 additions & 41 deletions

File tree

src/explode_shape_layer.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
function explodeLayer(layer) {
77

8-
cLog('Exploding layer : ' + layer.name + '\n==================');
8+
cLog('==================\n' + 'Exploding layer : ' + layer.name);
99

1010
// Get the elements of the original shape layer
1111
var contents = layer.property("Contents");
@@ -22,15 +22,15 @@ function explodeLayer(layer) {
2222

2323
}
2424

25-
_progressBar.make(1, contents.numProperties, 1);
26-
_progressBar.showBar();
25+
var pb = new ProgressBar(1, contents.numProperties, 1);
26+
pb.start();
2727

2828
// Browse through contents array
2929
for(var i = contents.numProperties; i > 0; i--) {
3030

3131
// Get the original property
3232
var _prop = contents.property(i);
33-
_progressBar.setCurrent(contents.numProperties - i)
33+
pb.update(contents.numProperties - i)
3434

3535
// Skip the property if not enabled
3636
if (!_prop.enabled) continue;
@@ -52,7 +52,7 @@ function explodeLayer(layer) {
5252

5353
}
5454

55-
_progressBar.hideBar();
55+
pb.end();
5656

5757
for(var i = 0; i < layers.length; i++) {
5858
layers[i].enabled = true;

src/progressBar.jsx

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,83 @@
1-
_progressBar = new Window('palette', configs.title, undefined, {
2-
resizeable : false,
3-
borderless : 'not quite true',
4-
});
5-
6-
_progressBar.preferredSize = [420, 40];
7-
_progressBar.bar = _progressBar.add("progressbar", undefined, 0, 100);
8-
_progressBar.bar.value = 0;
9-
_progressBar.bar.preferredSize.width = 400;
10-
_progressBar.bar.show()
11-
12-
_progressBar.barInfos = _progressBar.add("statictext", undefined, 'Loading, please wait', {
13-
justify: 'center'
14-
});
15-
_progressBar.barInfos.preferredSize = [400, 17];
16-
17-
_progressBar.make = function (min, max, current) {
18-
this.barProps = {
19-
min : min,
20-
max : max,
21-
current : current,
1+
function ProgressBar(min, max, current) {
2+
3+
var _window,
4+
_progressBar,
5+
_infos,
6+
_real,
7+
_cursor,
8+
_isVisible;
9+
10+
this.testInfos = 'Processing element :current on :max';
11+
12+
this.constructor = function(min, max, current) {
13+
14+
_this = this;
15+
_isVisible = false;
16+
17+
_real = { min : min, max : max, current : current };
18+
_cursor = { min : 0, max : 100, current : 0 };
19+
20+
_cursor.max = (_real.max - _real.min) + 1;
21+
22+
// Instanciate the window
23+
_window = new Window('palette', configs.title, undefined, {
24+
resizeable : false,
25+
borderless : 'not quite true',
26+
});
27+
_window.preferredSize = [420, 40];
28+
29+
// Instanciate the progress bar
30+
_progressBar = _window.add("progressbar", undefined, _cursor.min, _cursor.max);
31+
_progressBar.preferredSize.width = 400;
32+
_progressBar.show();
33+
34+
// Instanciate text infos
35+
_infos = _window.add("statictext", undefined, 'Loading, please wait', {
36+
justify: 'center'
37+
});
38+
_infos.preferredSize = [400, 17];
39+
40+
this.update(current);
41+
42+
43+
return this;
44+
2245
}
23-
this.barProps.total = (this.barProps.max - this.barProps.min) + 1;
24-
this.barProps.step = (this.barProps.current - this.barProps.min) + 1;
25-
}
2646

27-
_progressBar.updateBar = function () {
28-
this.bar.value = Math.round(( (this.barProps.step) * 100) / this.barProps.max)
29-
consLog('Processing element ' + (this.barProps.step + 1) + ' on ' + this.barProps.total);
30-
this.barInfos.text = 'Processing element ' + (this.barProps.step + 1) + ' on ' + this.barProps.total;
31-
}
47+
this.start = function () {
48+
_isVisible = true;
49+
this.update(_real.current)
50+
_window.show();
51+
}
52+
53+
this.end = function () {
54+
_window.hide();
55+
}
56+
57+
this.update = function(step) {
58+
59+
_real.current = step;
60+
_cursor.current = (_real.current + 1) - _real.min;
61+
62+
var infos = this.testInfos
63+
.replace(':current', _cursor.current)
64+
.replace(':max', _cursor.max);
3265

33-
_progressBar.showBar = function () { this.show(); }
66+
_progressBar.value = _cursor.current;
67+
_infos.text = infos;
3468

35-
_progressBar.hideBar = function () { this.hide(); }
69+
cDebug(infos);
70+
71+
updateGraphics();
72+
}
73+
74+
function updateGraphics() {
75+
if(!_isVisible) return;
76+
_window.update();
77+
}
78+
79+
return this.constructor(min, max, current);
3680

37-
_progressBar.setCurrent = function (current, text) {
38-
this.barProps.current = current;
39-
this.barProps.step = (this.barProps.current - this.barProps.min) + 1;
40-
this.updateBar();
41-
this.update();
4281
}
82+
83+
// this.bar.value = Math.round(( (this.barProps.step) * 100) / this.barProps.max)

0 commit comments

Comments
 (0)