♻️ Use webcam container for intersection instead of controls (#4759)

* 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
This commit is contained in:
Christian Würthner authored and Gina Häußge committed 2023-03-13 17:10:51 +01:00
1 parent 65f53f3d86
commit 4cc41b05ea
2 files changed
+15 -11

No files matched your search

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