From 2a39db799a4353d1a91a4d643ee9d7bf1d87a562 Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Mon, 5 Feb 2024 00:46:44 +0200 Subject: [PATCH] auto-pause Talkinghead when ST tab is hidden to save GPU resources --- .../scripts/extensions/expressions/index.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/public/scripts/extensions/expressions/index.js b/public/scripts/extensions/expressions/index.js index 2359ace5c..cfbb87894 100644 --- a/public/scripts/extensions/expressions/index.js +++ b/public/scripts/extensions/expressions/index.js @@ -1678,6 +1678,34 @@ async function fetchImagesNoCache() { $('#expression_custom_remove').on('click', onClickExpressionRemoveCustom); } + // Pause Talkinghead to save resources when the ST tab is not visible or the window is minimized. + // We currently do this via loading/unloading. Could be improved by adding new pause/unpause endpoints to Extras. + document.addEventListener("visibilitychange", function (event) { + let pageIsVisible; + if (document.hidden) { + console.debug('expressions: SillyTavern is now hidden'); + pageIsVisible = false; + } else { + console.debug('expressions: SillyTavern is now visible'); + pageIsVisible = true; + } + + if (isTalkingHeadEnabled() && modules.includes('talkinghead')) { + isTalkingHeadAvailable().then(result => { + if (result) { + if (pageIsVisible) { + loadTalkingHead(); + } else { + unloadTalkingHead(); + } + handleImageChange(); // Change image as needed + } else { + //console.log("talkinghead does not exist."); + } + }); + } + }); + addExpressionImage(); addVisualNovelMode(); addSettings();