From 4cc41b05eabe11dc49998f67d79b4d41833d1e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20W=C3=BCrthner?= Date: Mon, 13 Mar 2023 17:09:57 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Use=20webcam=20containe?= =?UTF-8?q?r=20for=20intersection=20instead=20of=20controls=20(#4759)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use webcam container for intersection instead of controls * Removed check for tab visibility, fully rely on control.js logic * Allow intersection observers to be recreated * Use onStartupComplete as callback to defer intersection initialization --- .../classicwebcam/static/js/classicwebcam.js | 11 ++--------- src/octoprint/static/js/app/viewmodels/control.js | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js b/src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js index 5e4d82446..f1b99522a 100644 --- a/src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js +++ b/src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js @@ -117,18 +117,11 @@ $(function () { }; self._enableWebcam = function () { - if ( - OctoPrint.coreui.selectedTab != "#control" || - !OctoPrint.coreui.browserTabVisible - ) { - return; - } - if (self.webcamDisableTimeout != undefined) { clearTimeout(self.webcamDisableTimeout); } - // IF disabled then we dont need to do anything + // If disabled then we dont need to do anything if (self.settings.webcamEnabled() == false) { console.log("Webcam not enabled"); return; @@ -156,7 +149,7 @@ $(function () { }; self.onWebcamErrored = function () { - log.debug("Webcam stream failed to load/disabled"); + log.debug("Webcam stream failed to load/was unloaded"); self.webcamLoaded(false); self.webcamError(true); }; diff --git a/src/octoprint/static/js/app/viewmodels/control.js b/src/octoprint/static/js/app/viewmodels/control.js index 2e3de6d19..84e7c3615 100644 --- a/src/octoprint/static/js/app/viewmodels/control.js +++ b/src/octoprint/static/js/app/viewmodels/control.js @@ -37,6 +37,7 @@ $(function () { self.controlsFromServer = []; self.additionalControls = []; + self.intersectionObservers = []; self.keycontrolActive = ko.observable(false); self.keycontrolHelpActive = ko.observable(false); @@ -74,18 +75,27 @@ $(function () { } }; - self.onAfterBinding = function () { + self.onStartupComplete = function () { + self.recreateIntersectionObservers(); + }; + + self.recreateIntersectionObservers = function () { // We are using the IntersectionObserver API to determine whether a webcam is visible or not. // A webcam will not intersect with the control tab if the control tab is invisible because another tab // is selected or if the webcam isn't shown because another webcam is active. // // Whenever the webacam changes visibility we will call onWebcamVisbilityChange() which the webcam's // VM can use to start or stop the stream. + self.intersectionObservers.forEach(function (observer) { + observer.disconnect(); + }); + self.intersectionObservers = []; + document .querySelectorAll("#webcam-group .tab-pane") .forEach(function (target) { var options = { - root: document.querySelector("#control"), + root: document.querySelector("#webcam_plugins_container"), rootMargin: "0px", threshold: 1.0 }; @@ -103,6 +113,7 @@ $(function () { var observer = new IntersectionObserver(callback, options); observer.observe(target); + self.intersectionObservers.push(observer); }); };