Skip to content

Commit

Permalink
Fire 'vrdisplayconnect' event when populating VRDisplays. Fixes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
jsantell committed May 30, 2017
1 parent cf1d123 commit 4bdb770
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ VRDisplay.prototype.fireVRDisplayPresentChange_ = function() {
window.dispatchEvent(event);
};

VRDisplay.prototype.fireVRDisplayConnect_ = function() {
// Important: unfortunately we cannot have full spec compliance here.
// CustomEvent custom fields all go under e.detail (so the VRDisplay ends up
// being e.detail.display, instead of e.display as per WebVR spec).
var event = new CustomEvent('vrdisplayconnect', {detail: {display: this}});
window.dispatchEvent(event);
};

VRDisplay.prototype.addFullscreenListeners_ = function(element, changeHandler, errorHandler) {
this.removeFullscreenListeners_();

Expand Down
10 changes: 8 additions & 2 deletions src/webvr-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ WebVRPolyfill.prototype.isDeprecatedWebVRAvailable = function() {
return ('getVRDevices' in navigator) || ('mozGetVRDevices' in navigator);
};

WebVRPolyfill.prototype.connectDisplay = function(vrDisplay) {
vrDisplay.fireVRDisplayConnect_();
this.displays.push(vrDisplay);
};

WebVRPolyfill.prototype.populateDevices = function() {
if (this.devicesPopulated) {
return;
Expand All @@ -65,7 +70,8 @@ WebVRPolyfill.prototype.populateDevices = function() {
// Add a Cardboard VRDisplay on compatible mobile devices
if (this.isCardboardCompatible()) {
vrDisplay = new CardboardVRDisplay();
this.displays.push(vrDisplay);

this.connectDisplay(vrDisplay);

// For backwards compatibility
if (window.WebVRConfig.ENABLE_DEPRECATED_API) {
Expand All @@ -77,7 +83,7 @@ WebVRPolyfill.prototype.populateDevices = function() {
// Add a Mouse and Keyboard driven VRDisplay for desktops/laptops
if (!this.isMobile() && !window.WebVRConfig.MOUSE_KEYBOARD_CONTROLS_DISABLED) {
vrDisplay = new MouseKeyboardVRDisplay();
this.displays.push(vrDisplay);
this.connectDisplay(vrDisplay);

// For backwards compatibility
if (window.WebVRConfig.ENABLE_DEPRECATED_API) {
Expand Down

0 comments on commit 4bdb770

Please sign in to comment.