SVGHMI: add ScreenSaver feature. Automatically jumps to a page named "ScreenSaver" after timeout defined in page arguments.
authorEdouard Tisserant
Fri, 04 Nov 2022 10:51:45 +0100
changeset 3653 d5ff60e906b0
parent 3651 e3a29c5b74c4
child 3654 6b7f15089703
SVGHMI: add ScreenSaver feature. Automatically jumps to a page named "ScreenSaver" after timeout defined in page arguments.
svghmi/detachable_pages.ysl2
svghmi/svghmi.js
svghmi/widget_back.ysl2
--- a/svghmi/detachable_pages.ysl2	Fri Oct 28 10:42:35 2022 +0200
+++ b/svghmi/detachable_pages.ysl2	Fri Nov 04 10:51:45 2022 +0100
@@ -25,6 +25,17 @@
 emit "preamble:default-page" {
     |
     | var default_page = "«$default_page»";
+    const "screensaverpage", "$hmi_pages_descs[arg[1]/@value = 'ScreenSaver']";
+    const "delay" choose {
+        when "$screensaverpage" {
+            const "delaystr", "$screensaverpage/arg[2]/@value";
+            if "not(regexp:test($delaystr,'^[0-9]+$'))"
+                error > ScreenSaver page has missing or malformed delay argument.
+            value "$delaystr";
+        }
+        otherwise > null
+    }
+    | var screensaver_delay = «$delay»;
 }
 
 const "keypads_descs", "$parsed_widgets/widget[@type = 'Keypad']";
--- a/svghmi/svghmi.js	Fri Oct 28 10:42:35 2022 +0200
+++ b/svghmi/svghmi.js	Fri Nov 04 10:51:45 2022 +0100
@@ -428,6 +428,19 @@
     e.preventDefault();
 });
 
+var screensaver_timer = null;
+function reset_screensaver_timer() {
+    if(screensaver_timer){
+        window.clearTimeout(screensaver_timer);
+    }
+    screensaver_timer = window.setTimeout(() => {
+        switch_page("ScreenSaver");
+        screensaver_timer = null;
+    }, screensaver_delay*1000);
+}
+if(screensaver_delay)
+    document.body.addEventListener('pointerdown', reset_screensaver_timer);
+
 function detach_detachables() {
 
     for(let eltid in detachable_elements){
@@ -497,9 +510,12 @@
     jumps_need_update = true;
 
     requestHMIAnimation();
-    jump_history.push([page_name, page_index]);
-    if(jump_history.length > 42)
-        jump_history.shift();
+    let [last_page_name, last_page_index] = jump_history[jump_history.length-1];
+    if(last_page_name != page_name || last_page_index != page_index){
+        jump_history.push([page_name, page_index]);
+        if(jump_history.length > 42)
+            jump_history.shift();
+    }
 
     apply_hmi_value(current_page_var_index, page_index == undefined
         ? page_name
@@ -585,6 +601,9 @@
 // show main page
 switch_page(default_page);
 
+// initialize screensaver
+reset_screensaver_timer();
+
 var reconnect_delay = 0;
 var periodic_reconnect_timer;
 
--- a/svghmi/widget_back.ysl2	Fri Oct 28 10:42:35 2022 +0200
+++ b/svghmi/widget_back.ysl2	Fri Nov 04 10:51:45 2022 +0100
@@ -14,8 +14,12 @@
     ||
         on_click(evt) {
             if(jump_history.length > 1){
-               jump_history.pop();
-               let [page_name, index] = jump_history.pop();
+               let page_name, index;
+               do {
+                   jump_history.pop(); // forget current page
+                   if(jump_history.length == 0) return;
+                   [page_name, index] = jump_history[jump_history.length-1];
+               } while(page_name == "ScreenSaver") // never go back to ScreenSaver
                switch_page(page_name, index);
             }
         }