Browse Source

webgl: Improve handling of canvas focus

Can now forcibly focus to the canvas with a foreground request, and now
focuses automatically on window open (except if requested not to)
rdb 10 months ago
parent
commit
8114971c56
1 changed files with 23 additions and 0 deletions
  1. 23 0
      panda/src/webgldisplay/webGLGraphicsWindow.cxx

+ 23 - 0
panda/src/webgldisplay/webGLGraphicsWindow.cxx

@@ -238,6 +238,16 @@ set_properties_now(WindowProperties &properties) {
     // though, we can't hide the cursor.
     properties.clear_cursor_hidden();
   }
+
+  if (properties.get_foreground()) {
+    EM_ASM_({
+      var canvas = document.getElementById('canvas');
+      if (canvas) {
+        canvas.focus();
+      }
+    });
+    properties.clear_foreground();
+  }
 }
 
 /**
@@ -369,6 +379,19 @@ open_window() {
 
   emscripten_set_wheel_callback(target, user_data, false, &on_wheel_event);
 
+  if (!_properties.has_foreground() || _properties.get_foreground()) {
+    _properties.set_foreground(EM_ASM_INT({
+      var canvas = document.getElementById('canvas');
+      if (canvas) {
+        canvas.focus();
+
+        return document.activeElement === canvas;
+      } else {
+        return false;
+      }
+    }));
+  }
+
   return true;
 }