Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var api = (function() { | 5 var api = new Object(); |
|
Dan Beam
2017/01/04 20:29:02
var api = {};
OR
cr.exportPath('api');
OR
cr.d
cjgrant
2017/01/04 21:23:17
We previously used your Option 3, but ditched it a
| |
| 6 'use strict'; | 6 |
| 7 | 7 /** |
| 8 /** | 8 * Enumeration of scene update commands. |
| 9 * Enumeration of scene update commands. | 9 * @enum {number} |
| 10 * @enum {number} | 10 * @const |
| 11 * @const | 11 */ |
| 12 */ | 12 api.Command = { |
| 13 var Command = Object.freeze({ | |
| 14 'ADD_ELEMENT': 0, | 13 'ADD_ELEMENT': 0, |
| 15 'UPDATE_ELEMENT': 1, | 14 'UPDATE_ELEMENT': 1, |
| 16 'REMOVE_ELEMENT': 2, | 15 'REMOVE_ELEMENT': 2, |
| 17 'ADD_ANIMATION': 3, | 16 'ADD_ANIMATION': 3, |
| 18 'REMOVE_ANIMATION': 4 | 17 'REMOVE_ANIMATION': 4 |
|
Dan Beam
2017/01/04 20:29:01
indent off
protip: tools/clang-format-js
cjgrant
2017/01/04 21:23:17
See the next patch set - I've been watching your c
| |
| 19 }); | 18 }; |
| 20 | 19 |
| 21 /** | 20 /** |
| 22 * Sends one or more commands to native scene management. Commands are used | 21 * Sends one or more commands to native scene management. Commands are used |
| 23 * to add, modify or remove elements and animations. For examples of how to | 22 * to add, modify or remove elements and animations. For examples of how to |
| 24 * format command parameters, refer to examples in scene.js. | 23 * format command parameters, refer to examples in scene.js. |
| 25 */ | 24 * @param {Array<Object>} commands |
| 26 function sendCommands(commands) { | 25 */ |
| 27 chrome.send('updateScene', commands); | 26 api.sendCommands = function(commands) { |
| 28 } | 27 chrome.send('updateScene', commands); |
| 29 | 28 }; |
| 30 /** | 29 |
| 31 * Returns the element ID of the browser content quad, allowing the content to | 30 /** |
| 32 * be manimulated in the scene. | 31 * Enumeration of valid Anchroing for X axis. |
| 33 */ | 32 * An element can either be anchored to the left, right, or center of the main |
| 34 function getContentElementId() { | 33 * content rect (or it can be absolutely positioned using NONE). Any |
| 35 return 0; | 34 * translations applied will be relative to this anchoring. |
| 36 } | 35 * @enum {number} |
| 37 | 36 * @const |
| 38 /** | 37 */ |
| 39 * Enumeration of valid Anchroing for X axis. | 38 api.XAnchoring = { |
| 40 * An element can either be anchored to the left, right, or center of the main | 39 'XNONE': 0, |
| 41 * content rect (or it can be absolutely positioned using NONE). Any | 40 'XLEFT': 1, |
| 42 * translations applied will be relative to this anchoring. | 41 'XRIGHT': 2 |
| 43 * @enum {number} | 42 }; |
| 44 * @const | 43 |
| 45 */ | 44 /** |
| 46 var XAnchoring = Object.freeze({ | 45 * Enumeration of valid Anchroing for Y axis. |
| 47 'XNONE': 0, | 46 * @enum {number} |
| 48 'XLEFT': 1, | 47 * @const |
| 49 'XRIGHT': 2 | 48 */ |
| 50 }); | 49 api.YAnchoring = { |
| 51 | 50 'YNONE': 0, |
| 52 /** | 51 'YTOP': 1, |
| 53 * Enumeration of valid Anchroing for Y axis. | 52 'YBOTTOM': 2 |
| 54 * @enum {number} | 53 }; |
| 55 * @const | 54 |
| 56 */ | 55 /** |
| 57 var YAnchoring = Object.freeze({ | 56 * Enumeration of actions that can be triggered by the HTML UI. |
| 58 'YNONE': 0, | 57 * @enum {number} |
| 59 'YTOP': 1, | 58 * @const |
| 60 'YBOTTOM': 2 | 59 */ |
| 61 }); | 60 api.Action = { |
| 62 | 61 'HISTORY_BACK': 0, |
| 63 /** | 62 'HISTORY_FORWARD': 1, |
| 64 * Enumeration of actions that can be triggered by the HTML UI. | 63 'RELOAD': 2, |
| 65 * @enum {number} | 64 'ZOOM_OUT': 3, |
| 66 * @const | 65 'ZOOM_IN': 4, |
| 67 */ | 66 'RELOAD_UI': 5, |
| 68 var Action = Object.freeze({ | 67 }; |
| 69 'HISTORY_BACK': 0, | 68 |
| 70 'HISTORY_FORWARD': 1, | 69 /** |
| 71 'RELOAD': 2, | 70 * Enumeration of modes that can be specified by the native side. |
| 72 'ZOOM_OUT': 3, | 71 * @enum {number} |
| 73 'ZOOM_IN': 4, | 72 * @const |
| 74 'RELOAD_UI': 5, | 73 */ |
| 75 }); | 74 api.Mode = { |
| 76 | 75 'UNKNOWN': -1, |
| 77 /** | 76 'STANDARD': 0, |
| 78 * Enumeration of modes that can be specified by the native side. | 77 'WEB_VR': 1, |
| 79 * @enum {number} | 78 }; |
| 80 * @const | 79 |
| 81 */ | 80 /** |
| 82 var Mode = Object.freeze({ | 81 * Triggers an Action. |
| 83 'UNKNOWN': -1, | 82 * @param {api.Action} action |
|
amp
2016/12/22 21:10:43
It looks a little odd to have the type include the
cjgrant
2017/01/03 17:05:39
Without the api prefix, the compiler says:
## /wo
| |
| 84 'STANDARD': 0, | 83 */ |
| 85 'WEB_VR': 1, | 84 api.doAction = function(action) { |
| 86 }); | 85 chrome.send('doAction', [action]); |
| 87 | 86 }; |
| 88 /** | 87 |
| 89 * Triggers an Action. | 88 /** |
| 90 */ | 89 * Notify native scene management that DOM loading has completed, at the |
| 91 function doAction(action) { | 90 * specified page size. |
| 92 chrome.send('doAction', [action]); | 91 */ |
| 93 } | 92 api.domLoaded = function() { |
| 94 | 93 chrome.send('domLoaded'); |
| 95 /** | 94 }; |
| 96 * Notify native scene management that DOM loading has completed, at the | 95 |
| 97 * specified page size. | 96 /** |
| 98 */ | 97 * Sets the CSS size for this page. |
| 99 function domLoaded() { | 98 * @param {number} width |
| 100 chrome.send('domLoaded'); | 99 * @param {number} height |
| 101 } | 100 * @param {number} dpr |
| 102 | 101 */ |
| 103 /** | 102 api.setUiCssSize = function(width, height, dpr) { |
| 104 * Sets the CSS size for this page. | 103 chrome.send('setUiCssSize', [width, height, dpr]); |
| 105 */ | 104 }; |
| 106 function setUiCssSize(width, height, dpr) { | 105 |
| 107 chrome.send('setUiCssSize', [width, height, dpr]); | 106 /** |
| 108 } | 107 * Represents updates to UI element properties. Any properties set on this |
| 109 | 108 * object are relayed to an underlying native element via scene command. |
| 110 /** | 109 * Properties that are not set on this object are left unchanged. |
| 111 * Represents updates to UI element properties. Any properties set on this | 110 * @struct |
|
amp
2016/12/22 21:10:43
What does tagging a class as @struct do?
cjgrant
2017/01/03 17:05:39
From the JSDoc annotation guide:
"@struct is used
| |
| 112 * object are relayed to an underlying native element via scene command. | 111 */ |
| 113 * Properties that are not set on this object are left unchanged. | 112 api.UiElementUpdate = class { |
| 114 */ | 113 |
| 115 class UiElementUpdate { | 114 constructor() { |
| 116 | 115 /** @private {!Object} */ |
| 117 setIsContentQuad() { | 116 this.properties = { |
| 118 this.contentQuad = true; | 117 'id': -1 |
| 119 } | 118 }; |
| 120 | 119 } |
| 121 /** | 120 |
| 122 * Specify a parent for this element. If set, this element is positioned | 121 /** |
| 123 * relative to its parent element, rather than absolutely. This allows | 122 * Set the id of the element to update. |
| 124 * elements to automatically move with a parent. | 123 * @param {number} id |
| 125 */ | 124 */ |
| 126 setParentId(id) { | 125 setId(id) { |
| 127 this.parentId = id; | 126 this.properties['id'] = id; |
| 128 } | 127 } |
| 129 | 128 |
| 130 /** | 129 /** |
| 131 * Specify the width and height (in meters) of an element. | 130 * Operates on an instance of MyClass and returns something. |
| 132 */ | 131 */ |
| 133 setSize(x, y) { | 132 setIsContentQuad() { |
| 134 this.size = { x: x, y: y }; | 133 this.properties['contentQuad'] = true; |
| 135 } | 134 } |
| 136 | 135 |
| 137 /** | 136 /** |
| 138 * Specify optional scaling of the element, and any children. | 137 * Specify a parent for this element. If set, this element is positioned |
| 139 */ | 138 * relative to its parent element, rather than absolutely. This allows |
| 140 setScale(x, y, z) { | 139 * elements to automatically move with a parent. |
| 141 this.scale = { x: x, y: y, z: z }; | 140 * @param {number} id |
| 142 } | 141 */ |
| 143 | 142 setParentId(id) { |
| 144 /** | 143 this.properties['parentId'] = id; |
| 145 * Specify rotation for the element. The rotation is specified in axis-angle | 144 } |
| 146 * representation (rotate around unit vector [x, y, z] by 'a' radians). | 145 |
| 147 */ | 146 /** |
| 148 setRotation(x, y, z, a) { | 147 * Specify the width and height (in meters) of an element. |
| 149 this.rotation = { x: x, y: y, z: z, a: a }; | 148 * @param {number} x |
| 150 } | 149 * @param {number} y |
| 151 | 150 */ |
| 152 /** | 151 setSize(x, y) { |
| 153 * Specify the translation of the element. If anchoring is specified, the | 152 this.properties['size'] = { x: x, y: y }; |
| 154 * offset is applied to the anchoring position rather than the origin. | 153 } |
| 155 * Translation is applied after scaling and rotation. | 154 |
| 156 */ | 155 /** |
| 157 setTranslation(x, y, z) { | 156 * Specify optional scaling of the element, and any children. |
| 158 this.translation = { x: x, y: y, z: z }; | 157 * @param {number} x |
| 159 } | 158 * @param {number} y |
| 160 | 159 * @param {number} z |
| 161 /** | 160 */ |
| 162 * Anchoring allows a rectangle to be positioned relative to the edge of | 161 setScale(x, y, z) { |
| 163 * its parent, without being concerned about the size of the parent. | 162 this.properties['scale'] = { x: x, y: y, z: z }; |
| 164 * Values should be XAnchoring and YAnchoring elements. | 163 } |
| 165 * Example: element.setAnchoring(XAnchoring.XNONE, YAnchoring.YBOTTOM); | 164 |
| 166 */ | 165 /** |
| 167 setAnchoring(x, y) { | 166 * Specify rotation for the element. The rotation is specified in axis-angle |
| 168 this.xAnchoring = x; | 167 * representation (rotate around unit vector [x, y, z] by 'a' radians). |
| 169 this.yAnchoring = y; | 168 * @param {number} x |
| 170 } | 169 * @param {number} y |
| 171 | 170 * @param {number} z |
| 172 /** | 171 * @param {number} a |
| 173 * Visibility controls whether the element is rendered. | 172 */ |
| 174 */ | 173 setRotation(x, y, z, a) { |
| 175 setVisible(visible) { | 174 this.properties['rotation'] = { x: x, y: y, z: z, a: a }; |
| 176 this.visible = !!visible; | 175 } |
| 177 } | 176 |
| 178 | 177 /** |
| 179 /** | 178 * Specify the translation of the element. If anchoring is specified, the |
| 180 * Hit-testable implies that the reticle will hit the element, if visible. | 179 * offset is applied to the anchoring position rather than the origin. |
| 181 */ | 180 * Translation is applied after scaling and rotation. |
| 182 setHitTestable(testable) { | 181 * @param {number} x |
| 183 this.hitTestable = !!testable; | 182 * @param {number} y |
| 184 } | 183 * @param {number} z |
| 185 | 184 */ |
| 186 /** | 185 setTranslation(x, y, z) { |
| 187 * Causes an element to be rendered relative to the field of view, rather | 186 this.properties['translation'] = { x: x, y: y, z: z }; |
| 188 * than the scene. Elements locked in this way should not have a parent. | 187 } |
| 189 */ | 188 |
| 190 setLockToFieldOfView(locked) { | 189 /** |
| 191 this.lockToFov = !!locked; | 190 * Anchoring allows a rectangle to be positioned relative to the edge of |
| 192 } | 191 * its parent, without being concerned about the size of the parent. |
| 193 }; | 192 * Values should be XAnchoring and YAnchoring elements. |
| 194 | 193 * Example: element.setAnchoring(XAnchoring.XNONE, YAnchoring.YBOTTOM); |
| 195 /** | 194 * @param {number} x |
| 196 * Represents a new UI element. This object builds on UiElementUpdate, | 195 * @param {number} y |
| 197 * forcing the underlying texture coordinates to be specified. | 196 */ |
| 198 */ | 197 setAnchoring(x, y) { |
| 199 class UiElement extends UiElementUpdate { | 198 this.properties['xAnchoring'] = x; |
| 200 /** | 199 this.properties['yAnchoring'] = y; |
| 201 * Constructor of UiElement. | 200 } |
| 202 * pixelX and pixelY values indicate the left upper corner; pixelWidth and | 201 |
| 203 * pixelHeight is width and height of the texture to be copied from the web | 202 /** |
| 204 * contents. | 203 * Visibility controls whether the element is rendered. |
| 205 */ | 204 * @param {boolean} visible |
| 206 constructor(pixelX, pixelY, pixelWidth, pixelHeight) { | 205 */ |
| 207 super(); | 206 setVisible(visible) { |
| 208 | 207 this.properties['visible'] = !!visible; |
| 209 this.copyRect = { | 208 } |
| 210 x: pixelX, | 209 |
| 211 y: pixelY, | 210 /** |
| 212 width: pixelWidth, | 211 * Hit-testable implies that the reticle will hit the element, if visible. |
| 213 height: pixelHeight | 212 * @param {boolean} testable |
| 214 }; | 213 */ |
| 215 } | 214 setHitTestable(testable) { |
| 216 }; | 215 this.properties['hitTestable'] = !!testable; |
| 217 | 216 } |
| 218 /** | 217 |
| 219 * Enumeration of animatable properties. | 218 /** |
| 220 * @enum {number} | 219 * Causes an element to be rendered relative to the field of view, rather |
| 221 * @const | 220 * than the scene. Elements locked in this way should not have a parent. |
| 222 */ | 221 * @param {boolean} locked |
| 223 var Property = Object.freeze({ | 222 */ |
| 224 'COPYRECT': 0, | 223 setLockToFieldOfView(locked) { |
| 225 'SIZE': 1, | 224 this.properties['lockToFov'] = !!locked; |
| 226 'TRANSLATION': 2, | 225 } |
| 227 'ORIENTATION': 3, | 226 }; |
| 228 'ROTATION': 4 | 227 |
| 229 }); | 228 /** |
| 230 | 229 * Represents a new UI element. This object builds on UiElementUpdate, |
| 231 /** | 230 * forcing the underlying texture coordinates to be specified. |
| 232 * Enumeration of easing type. | 231 * @struct |
| 233 * @enum {number} | 232 */ |
| 234 * @const | 233 api.UiElement = class extends api.UiElementUpdate { |
| 235 */ | 234 /** |
| 236 var Easing = Object.freeze({ | 235 * Constructor of UiElement. |
| 237 'LINEAR': 0, | 236 * pixelX and pixelY values indicate the left upper corner; pixelWidth and |
| 238 'CUBICBEZIER': 1, | 237 * pixelHeight is width and height of the texture to be copied from the web |
| 239 'EASEIN': 2, | 238 * contents. |
| 240 'EASEOUT': 3 | 239 * @param {number} pixelX |
| 241 }); | 240 * @param {number} pixelY |
| 242 | 241 * @param {number} pixelWidth |
| 243 /** | 242 * @param {number} pixelHeight |
| 244 * Base animation class. An animation can vary only one object property. | 243 */ |
| 245 */ | 244 constructor(pixelX, pixelY, pixelWidth, pixelHeight) { |
| 246 class Animation { | 245 super(); |
| 247 constructor(elementId, durationMs) { | 246 |
| 248 this.meshId = elementId; | 247 /** @private {Object} */ |
| 249 this.to = {}; | 248 this.properties['copyRect'] = { |
| 250 this.easing = {}; | 249 x: pixelX, |
| 251 this.easing.type = api.Easing.LINEAR; | 250 y: pixelY, |
| 252 | 251 width: pixelWidth, |
| 253 // How many milliseconds in the future to start the animation. | 252 height: pixelHeight |
| 254 this.startInMillis = 0.0; | 253 }; |
| 255 | 254 } |
| 256 // Duration of the animation (milliseconds). | 255 }; |
| 257 this.durationMillis = durationMs; | 256 |
| 258 } | 257 /** |
| 259 | 258 * Enumeration of animatable properties. |
| 260 /** | 259 * @enum {number} |
| 261 * Set the animation's final element size. | 260 * @const |
| 262 */ | 261 */ |
| 263 setSize(width, height) { | 262 api.Property = { |
| 264 this.property = api.Property.SIZE; | 263 'COPYRECT': 0, |
| 265 this.to.x = width; | 264 'SIZE': 1, |
| 266 this.to.y = height; | 265 'TRANSLATION': 2, |
| 267 } | 266 'SCALE': 3, |
| 268 | 267 'ROTATION': 4 |
| 269 /** | 268 }; |
| 270 * Set the animation's final element scale. | 269 |
| 271 */ | 270 /** |
| 272 setScale(x, y, z) { | 271 * Enumeration of easing type. |
| 273 this.property = api.Property.SCALE; | 272 * @enum {number} |
| 274 this.to.x = x; | 273 * @const |
| 275 this.to.y = y; | 274 */ |
| 276 this.to.z = z; | 275 api.Easing = { |
| 277 } | 276 'LINEAR': 0, |
| 278 | 277 'CUBICBEZIER': 1, |
| 279 /** | 278 'EASEIN': 2, |
| 280 * Set the animation's final element rotation. | 279 'EASEOUT': 3 |
| 281 */ | 280 }; |
| 282 setRotation(x, y, z, a) { | 281 |
| 283 this.property = api.Property.ROTATION; | 282 /** |
| 284 this.to.x = x; | 283 * Base animation class. An animation can vary only one object property. |
| 285 this.to.y = y; | 284 * @struct |
| 286 this.to.z = z; | 285 */ |
| 287 this.to.a = a; | 286 api.Animation = class { |
| 288 } | 287 constructor(elementId, durationMs) { |
| 289 | 288 /** @private {number} */ |
| 290 /** | 289 this.id = -1; |
| 291 * Set the animation's final element translation. | 290 /** @private {number} */ |
| 292 */ | 291 this.meshId = elementId; |
| 293 setTranslation(x, y, z) { | 292 /** @private {number} */ |
| 294 this.property = api.Property.TRANSLATION; | 293 this.property = -1; |
| 295 this.to.x = x; | 294 /** @private {Object} */ |
| 296 this.to.y = y; | 295 this.to = {}; |
| 297 this.to.z = z; | 296 /** @private {Object} */ |
| 298 } | 297 this.easing = {}; |
| 299 }; | 298 |
| 300 | 299 // How many milliseconds in the future to start the animation. |
| 301 return { | 300 /** @private {number} */ |
| 302 sendCommands: sendCommands, | 301 this.startInMillis = 0.0; |
| 303 XAnchoring: XAnchoring, | 302 |
| 304 YAnchoring: YAnchoring, | 303 // Duration of the animation (milliseconds). |
| 305 Property: Property, | 304 /** @private {number} */ |
| 306 Easing: Easing, | 305 this.durationMillis = durationMs; |
| 307 Command: Command, | 306 |
| 308 Action: Action, | 307 this.easing.type = api.Easing.LINEAR; |
| 309 Mode: Mode, | 308 } |
| 310 getContentElementId: getContentElementId, | 309 |
| 311 UiElement: UiElement, | 310 /** |
| 312 UiElementUpdate: UiElementUpdate, | 311 * Set the id of the animation. |
| 313 Animation: Animation, | 312 * @param {number} id |
| 314 doAction: doAction, | 313 */ |
| 315 domLoaded: domLoaded, | 314 setId(id) { |
| 316 setUiCssSize: setUiCssSize, | 315 this.id = id; |
| 317 }; | 316 } |
| 318 })(); | 317 |
| 318 /** | |
| 319 * Set the animation's final element size. | |
| 320 * @param {number} width | |
| 321 * @param {number} height | |
| 322 */ | |
| 323 setSize(width, height) { | |
| 324 this.property = api.Property.SIZE; | |
| 325 this.to.x = width; | |
| 326 this.to.y = height; | |
| 327 } | |
| 328 | |
| 329 /** | |
| 330 * Set the animation's final element scale. | |
| 331 * @param {number} x | |
| 332 * @param {number} y | |
| 333 * @param {number} z | |
| 334 */ | |
| 335 setScale(x, y, z) { | |
| 336 this.property = api.Property.SCALE; | |
| 337 this.to.x = x; | |
| 338 this.to.y = y; | |
| 339 this.to.z = z; | |
| 340 } | |
| 341 | |
| 342 /** | |
| 343 * Set the animation's final element rotation. | |
| 344 * @param {number} x | |
| 345 * @param {number} y | |
| 346 * @param {number} z | |
| 347 * @param {number} a | |
| 348 */ | |
| 349 setRotation(x, y, z, a) { | |
| 350 this.property = api.Property.ROTATION; | |
| 351 this.to.x = x; | |
| 352 this.to.y = y; | |
| 353 this.to.z = z; | |
| 354 this.to.a = a; | |
| 355 } | |
| 356 | |
| 357 /** | |
| 358 * Set the animation's final element translation. | |
| 359 * @param {number} x | |
| 360 * @param {number} y | |
| 361 * @param {number} z | |
| 362 */ | |
| 363 setTranslation(x, y, z) { | |
| 364 this.property = api.Property.TRANSLATION; | |
| 365 this.to.x = x; | |
| 366 this.to.y = y; | |
| 367 this.to.z = z; | |
| 368 } | |
| 369 }; | |
| OLD | NEW |