Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/javascript/progressbar.js
3055 views
1
// code related to showing and updating progressbar shown as the image is being made
2
3
function rememberGallerySelection() {
4
5
}
6
7
function getGallerySelectedIndex() {
8
9
}
10
11
function request(url, data, handler, errorHandler) {
12
var xhr = new XMLHttpRequest();
13
xhr.open("POST", url, true);
14
xhr.setRequestHeader("Content-Type", "application/json");
15
xhr.onreadystatechange = function() {
16
if (xhr.readyState === 4) {
17
if (xhr.status === 200) {
18
try {
19
var js = JSON.parse(xhr.responseText);
20
handler(js);
21
} catch (error) {
22
console.error(error);
23
errorHandler();
24
}
25
} else {
26
errorHandler();
27
}
28
}
29
};
30
var js = JSON.stringify(data);
31
xhr.send(js);
32
}
33
34
function pad2(x) {
35
return x < 10 ? '0' + x : x;
36
}
37
38
function formatTime(secs) {
39
if (secs > 3600) {
40
return pad2(Math.floor(secs / 60 / 60)) + ":" + pad2(Math.floor(secs / 60) % 60) + ":" + pad2(Math.floor(secs) % 60);
41
} else if (secs > 60) {
42
return pad2(Math.floor(secs / 60)) + ":" + pad2(Math.floor(secs) % 60);
43
} else {
44
return Math.floor(secs) + "s";
45
}
46
}
47
48
49
var originalAppTitle = undefined;
50
51
onUiLoaded(function() {
52
originalAppTitle = document.title;
53
});
54
55
function setTitle(progress) {
56
var title = originalAppTitle;
57
58
if (opts.show_progress_in_title && progress) {
59
title = '[' + progress.trim() + '] ' + title;
60
}
61
62
if (document.title != title) {
63
document.title = title;
64
}
65
}
66
67
68
function randomId() {
69
return "task(" + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + ")";
70
}
71
72
// starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and
73
// preview inside gallery element. Cleans up all created stuff when the task is over and calls atEnd.
74
// calls onProgress every time there is a progress update
75
function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgress, inactivityTimeout = 40) {
76
var dateStart = new Date();
77
var wasEverActive = false;
78
var parentProgressbar = progressbarContainer.parentNode;
79
var wakeLock = null;
80
81
var requestWakeLock = async function() {
82
if (!opts.prevent_screen_sleep_during_generation || wakeLock) return;
83
try {
84
wakeLock = await navigator.wakeLock.request('screen');
85
} catch (err) {
86
console.error('Wake Lock is not supported.');
87
}
88
};
89
90
var releaseWakeLock = async function() {
91
if (!opts.prevent_screen_sleep_during_generation || !wakeLock) return;
92
try {
93
await wakeLock.release();
94
wakeLock = null;
95
} catch (err) {
96
console.error('Wake Lock release failed', err);
97
}
98
};
99
100
var divProgress = document.createElement('div');
101
divProgress.className = 'progressDiv';
102
divProgress.style.display = opts.show_progressbar ? "block" : "none";
103
var divInner = document.createElement('div');
104
divInner.className = 'progress';
105
106
divProgress.appendChild(divInner);
107
parentProgressbar.insertBefore(divProgress, progressbarContainer);
108
109
var livePreview = null;
110
111
var removeProgressBar = function() {
112
releaseWakeLock();
113
if (!divProgress) return;
114
115
setTitle("");
116
parentProgressbar.removeChild(divProgress);
117
if (gallery && livePreview) gallery.removeChild(livePreview);
118
atEnd();
119
120
divProgress = null;
121
};
122
123
var funProgress = function(id_task) {
124
requestWakeLock();
125
request("./internal/progress", {id_task: id_task, live_preview: false}, function(res) {
126
if (res.completed) {
127
removeProgressBar();
128
return;
129
}
130
131
let progressText = "";
132
133
divInner.style.width = ((res.progress || 0) * 100.0) + '%';
134
divInner.style.background = res.progress ? "" : "transparent";
135
136
if (res.progress > 0) {
137
progressText = ((res.progress || 0) * 100.0).toFixed(0) + '%';
138
}
139
140
if (res.eta) {
141
progressText += " ETA: " + formatTime(res.eta);
142
}
143
144
setTitle(progressText);
145
146
if (res.textinfo && res.textinfo.indexOf("\n") == -1) {
147
progressText = res.textinfo + " " + progressText;
148
}
149
150
divInner.textContent = progressText;
151
152
var elapsedFromStart = (new Date() - dateStart) / 1000;
153
154
if (res.active) wasEverActive = true;
155
156
if (!res.active && wasEverActive) {
157
removeProgressBar();
158
return;
159
}
160
161
if (elapsedFromStart > inactivityTimeout && !res.queued && !res.active) {
162
removeProgressBar();
163
return;
164
}
165
166
if (onProgress) {
167
onProgress(res);
168
}
169
170
setTimeout(() => {
171
funProgress(id_task, res.id_live_preview);
172
}, opts.live_preview_refresh_period || 500);
173
}, function() {
174
removeProgressBar();
175
});
176
};
177
178
var funLivePreview = function(id_task, id_live_preview) {
179
request("./internal/progress", {id_task: id_task, id_live_preview: id_live_preview}, function(res) {
180
if (!divProgress) {
181
return;
182
}
183
184
if (res.live_preview && gallery) {
185
var img = new Image();
186
img.onload = function() {
187
if (!livePreview) {
188
livePreview = document.createElement('div');
189
livePreview.className = 'livePreview';
190
gallery.insertBefore(livePreview, gallery.firstElementChild);
191
}
192
193
livePreview.appendChild(img);
194
if (livePreview.childElementCount > 2) {
195
livePreview.removeChild(livePreview.firstElementChild);
196
}
197
};
198
img.src = res.live_preview;
199
}
200
201
setTimeout(() => {
202
funLivePreview(id_task, res.id_live_preview);
203
}, opts.live_preview_refresh_period || 500);
204
}, function() {
205
removeProgressBar();
206
});
207
};
208
209
funProgress(id_task, 0);
210
211
if (gallery) {
212
funLivePreview(id_task, 0);
213
}
214
215
}
216
217