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); }); };