feat: prevent screen sleep during generation

This commit is contained in:
YSH 2024-06-11 16:50:00 -07:00
parent feee37d75f
commit 1f8f3a6e8b
2 changed files with 26 additions and 0 deletions

View File

@ -76,6 +76,28 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
var dateStart = new Date(); var dateStart = new Date();
var wasEverActive = false; var wasEverActive = false;
var parentProgressbar = progressbarContainer.parentNode; var parentProgressbar = progressbarContainer.parentNode;
var wakeLock = null;
var requestWakeLock = async function() {
if (!opts.prevent_screen_sleep_during_generation) return;
try {
wakeLock = await navigator.wakeLock.request('screen');
console.log('Wake Lock is active.');
} catch (err) {
console.log('Wake Lock is not supported.');
}
};
var releaseWakeLock = async function() {
if (!opts.prevent_screen_sleep_during_generation || !wakeLock) return;
try {
await wakeLock.release();
console.log('Wake Lock is released.');
wakeLock = null;
} catch (err) {
console.error('Wake Lock release failed', err);
}
};
var divProgress = document.createElement('div'); var divProgress = document.createElement('div');
divProgress.className = 'progressDiv'; divProgress.className = 'progressDiv';
@ -89,6 +111,7 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
var livePreview = null; var livePreview = null;
var removeProgressBar = function() { var removeProgressBar = function() {
releaseWakeLock();
if (!divProgress) return; if (!divProgress) return;
setTitle(""); setTitle("");
@ -100,6 +123,8 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
}; };
var funProgress = function(id_task) { var funProgress = function(id_task) {
// Request the wake lock at the start of the progress
requestWakeLock();
request("./internal/progress", {id_task: id_task, live_preview: false}, function(res) { request("./internal/progress", {id_task: id_task, live_preview: false}, function(res) {
if (res.completed) { if (res.completed) {
removeProgressBar(); removeProgressBar();

View File

@ -359,6 +359,7 @@ options_templates.update(options_section(('ui', "Live previews", "ui"), {
"live_preview_refresh_period": OptionInfo(1000, "Progressbar and preview update period").info("in milliseconds"), "live_preview_refresh_period": OptionInfo(1000, "Progressbar and preview update period").info("in milliseconds"),
"live_preview_fast_interrupt": OptionInfo(False, "Return image with chosen live preview method on interrupt").info("makes interrupts faster"), "live_preview_fast_interrupt": OptionInfo(False, "Return image with chosen live preview method on interrupt").info("makes interrupts faster"),
"js_live_preview_in_modal_lightbox": OptionInfo(False, "Show Live preview in full page image viewer"), "js_live_preview_in_modal_lightbox": OptionInfo(False, "Show Live preview in full page image viewer"),
"prevent_screen_sleep_during_generation": OptionInfo(True, "Prevent screen sleep during generation"),
})) }))
options_templates.update(options_section(('sampler-params', "Sampler parameters", "sd"), { options_templates.update(options_section(('sampler-params', "Sampler parameters", "sd"), {