Skip to content

Commit

Permalink
adds spatialInterface APIs for communicating from footer to other ifr…
Browse files Browse the repository at this point in the history
…ames; adds onWindowOrViewportResized API
  • Loading branch information
benptc committed Sep 20, 2024
1 parent e8cad61 commit 2464c0c
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions libraries/objectDefaultFiles/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,11 @@
this.onViewportToSidebarMessage = makeSendStub('onViewportToSidebarMessage');
this.onSidebarToViewportMessage = makeSendStub('onSidebarToViewportMessage');

this.sendMessageFooterToViewport = makeSendStub('sendMessageFooterToViewport');
this.sendMessageViewportToFooter = makeSendStub('sendMessageViewportToFooter');
this.onViewportToFooterMessage = makeSendStub('onViewportToFooterMessage');
this.onFooterToViewportMessage = makeSendStub('onFooterToViewportMessage');

// deprecated methods
this.sendToBackground = makeSendStub('sendToBackground');
}
Expand Down Expand Up @@ -2208,9 +2213,29 @@
});
};

this.sendMessageFooterToViewport = function(messageType, messagePayload) {
let footerToViewport = {};
footerToViewport[messageType] = messagePayload;

postDataToParent({
footerToViewport: footerToViewport
});
};

this.sendMessageViewportToFooter = function(messageType, messagePayload) {
let viewportToFooter = {};
viewportToFooter[messageType] = messagePayload;

postDataToParent({
viewportToFooter: viewportToFooter
});
};

let messageSubscriptionCounters = {
onViewportToSidebarMessage: 0,
onSidebarToViewportMessage: 0,
onViewportToFooterMessage: 0,
onFooterToViewportMessage: 0
};

this.onViewportToSidebarMessage = function(callback) {
Expand All @@ -2235,6 +2260,61 @@
};
};

this.onViewportToFooterMessage = function(callback) {
messageSubscriptionCounters.onViewportToFooterMessage++;
let callbackId = `onViewportToFooterMessageCall_${messageSubscriptionCounters.onViewportToFooterMessage}`;
spatialObject.messageCallBacks[callbackId] = function (msgContent) {
if (spatialObject.visibility !== 'visible') return;
if (typeof msgContent.viewportToFooter !== 'undefined') {
callback(msgContent.viewportToFooter);
}
};
};

this.onFooterToViewportMessage = function(callback) {
messageSubscriptionCounters.onFooterToViewportMessage++;
let callbackId = `onFooterToViewportMessageCall_${messageSubscriptionCounters.onFooterToViewportMessage}`;
spatialObject.messageCallBacks[callbackId] = function (msgContent) {
if (spatialObject.visibility !== 'visible') return;
if (typeof msgContent.footerToViewport !== 'undefined') {
callback(msgContent.footerToViewport);
}
};
};

// the userinterface will combine all of the limitations and use the strictest combination
this.addViewportMarginLimitation = function({left, top, right, bottom}) {
let margins = {};
if (typeof left === 'number') {
margins.left = left;
}
if (typeof top === 'number') {
margins.top = top;
}
if (typeof right === 'number') {
margins.right = right;
}
if (typeof bottom === 'number') {
margins.bottom = bottom;
}

postDataToParent({
addViewportMarginLimitation: {
left,
top,
right,
bottom
}
});
};

// the userinterface will combine all of the limitations and use the strictest combination
this.removeViewportMarginLimitation = function() {
postDataToParent({
removeViewportMarginLimitation: true
});
};

/**
* Broadcasts a standardized message when a video plays, that will automatically pause videos in all other frames
* The event that is sent
Expand Down Expand Up @@ -2295,6 +2375,11 @@
this.changeToolSize = this.changeFrameSize;

let windowResizedCallbackCount = 0;
/**
* Gives the viewport width and height that this iframe is rendered within
* @param callback
* @deprecated - use onWindowOrViewportResized instead
*/
this.onWindowResized = function(callback) {
windowResizedCallbackCount++;
spatialObject.messageCallBacks[`onWindowResizedCall${windowResizedCallbackCount}`] = (msgContent) => {
Expand All @@ -2310,8 +2395,43 @@
});
};

/**
* @param {function} callback
*/
this.onWindowOrViewportResized = function(callback) {
windowResizedCallbackCount++;
spatialObject.messageCallBacks[`onWindowOrViewportResizedCall${windowResizedCallbackCount}`] = (msgContent) => {
if (typeof msgContent.onWindowOrViewportResized === 'undefined') return;
callback({
fullWindow: {
width: msgContent.onWindowOrViewportResized.fullWindow.width,
height: msgContent.onWindowOrViewportResized.fullWindow.height,
top: msgContent.onWindowOrViewportResized.fullWindow.top,
left: msgContent.onWindowOrViewportResized.fullWindow.left,
},
viewport: {
width: msgContent.onWindowOrViewportResized.viewport.width,
height: msgContent.onWindowOrViewportResized.viewport.height,
top: msgContent.onWindowOrViewportResized.viewport.top,
left: msgContent.onWindowOrViewportResized.viewport.left,
},
safeWindowBounds: {
width: msgContent.onWindowOrViewportResized.viewport.width, // account for title bars, notch insets, etc
height: msgContent.onWindowOrViewportResized.viewport.height,
top: msgContent.onWindowOrViewportResized.viewport.top,
left: msgContent.onWindowOrViewportResized.viewport.left,
}
});
};

postDataToParent({
sendWindowResize: true
});
};

/**
* Asynchronously query the screen width and height from the parent application, as the iframe itself can't access that
* This has been updated to return the viewport width and height that the iframe is rendered within, not the entire screen.
* @param {function} callback
*/
this.getScreenDimensions = function(callback) {
Expand Down

0 comments on commit 2464c0c

Please sign in to comment.