From a548e0d1f6030b3dd7d78dea9e05bcd2f52dda08 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Sun, 7 May 2023 15:13:43 +0200 Subject: [PATCH] `foreground-service`: Listen to tab visibility change event (#1878) Fixes #1872. This makes the foreground service also listen to the `visibilitychange` event to stop and resume foreground activity when the tab is changed or the browser is closed. See https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event. -- Signed-off-by: Florian Hotze --- .../src/components/widgets/widget-foreground-service.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bundles/org.openhab.ui/web/src/components/widgets/widget-foreground-service.js b/bundles/org.openhab.ui/web/src/components/widgets/widget-foreground-service.js index b50104b803..cd3d2bbe11 100644 --- a/bundles/org.openhab.ui/web/src/components/widgets/widget-foreground-service.js +++ b/bundles/org.openhab.ui/web/src/components/widgets/widget-foreground-service.js @@ -21,6 +21,14 @@ export default { this.$f7.on('pageAfterIn', this.onPageAfterIn) this.$f7.on('pageBeforeOut', this.onPageBeforeOut) + + document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'visible') { + this.startForegroundActivity() + } else if (document.visibilityState === 'hidden') { + this.stopForegroundActivity() + } + }) }, beforeDestroy () { this.$f7.off('pageAfterIn', this.onPageAfterIn)