OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 (function(exports) { | 4 (function(exports) { |
5 /** | 5 /** |
6 * Alignment options for a keyset. | 6 * Alignment options for a keyset. |
7 * @param {Object=} opt_keyset The keyset to calculate the dimensions for. | 7 * @param {Object=} opt_keyset The keyset to calculate the dimensions for. |
8 * Defaults to the current active keyset. | 8 * Defaults to the current active keyset. |
9 */ | 9 */ |
10 var AlignmentOptions = function(opt_keyset) { | 10 var AlignmentOptions = function(opt_keyset) { |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 return 0; | 287 return 0; |
288 return x >= high? 1 : -1; | 288 return x >= high? 1 : -1; |
289 } | 289 } |
290 | 290 |
291 return binarySearch(0, allKeys.length -1, testFn); | 291 return binarySearch(0, allKeys.length -1, testFn); |
292 } | 292 } |
293 | 293 |
294 /** | 294 /** |
295 * Redistributes the total width amongst the keys in the range provided. | 295 * Redistributes the total width amongst the keys in the range provided. |
296 * @param {Array.<kb-key>} allKeys Ordered list of keys to stretch. | 296 * @param {Array.<kb-key>} allKeys Ordered list of keys to stretch. |
297 * @param {number} pitch The space between two keys. | 297 * @param {AlignmentOptions} params Options for aligning the keyset. |
298 * @param {number} xOffset The x-coordinate of the key who's index is start. | 298 * @param {number} xOffset The x-coordinate of the key who's index is start. |
299 * @param {number} width The total extraneous width to distribute. | 299 * @param {number} width The total extraneous width to distribute. |
300 * @param {number} keyHeight The height of each key. | 300 * @param {number} keyHeight The height of each key. |
301 * @param {number} yOffset The y-coordinate of the top edge of the row. | 301 * @param {number} yOffset The y-coordinate of the top edge of the row. |
302 */ | 302 */ |
303 function redistribute(allKeys, pitch, xOffset, width, keyHeight, yOffset) { | 303 function redistribute(allKeys, params, xOffset, width, keyHeight, yOffset) { |
304 var weight = 0; | 304 var availableWidth = width - (allKeys.length - 1) * params.pitchX; |
| 305 var stretchWeight = 0; |
| 306 var nStretch = 0; |
305 for (var i = 0; i < allKeys.length; i++) { | 307 for (var i = 0; i < allKeys.length; i++) { |
306 var key = allKeys[i]; | 308 var key = allKeys[i]; |
307 weight += key.weight; | 309 if (key.stretch) { |
| 310 stretchWeight += key.weight; |
| 311 nStretch++; |
| 312 } else if (key.weight == DEFAULT_KEY_WEIGHT_X) { |
| 313 availableWidth -= params.keyWidth; |
| 314 } else { |
| 315 availableWidth -= |
| 316 Math.floor(key.weight/DEFAULT_KEY_WEIGHT_X * params.keyWidth); |
| 317 } |
308 } | 318 } |
309 var availableWidth = width - (allKeys.length - 1) * pitch; | 319 if (stretchWeight <= 0) |
310 var pixelsPerWeight = width / weight; | 320 console.error("Cannot stretch row without a stretchable key"); |
| 321 // Rounding error to distribute. |
| 322 var pixelsPerWeight = availableWidth / stretchWeight; |
311 for (var i = 0; i < allKeys.length; i++) { | 323 for (var i = 0; i < allKeys.length; i++) { |
312 var key = allKeys[i] | 324 var key = allKeys[i]; |
313 var keyWidth = Math.floor(key.weight * pixelsPerWeight); | 325 var keyWidth = params.keyWidth; |
314 if (i == allKeys.length -1) { | 326 if (key.weight != DEFAULT_KEY_WEIGHT_X) { |
315 keyWidth = availableWidth; | 327 keyWidth = |
| 328 Math.floor(key.weight/DEFAULT_KEY_WEIGHT_X * params.keyWidth); |
316 } | 329 } |
317 updateKey(allKeys[i], keyWidth, keyHeight, xOffset, yOffset) | 330 if (key.stretch) { |
318 availableWidth -= keyWidth; | 331 nStretch--; |
319 xOffset += keyWidth + pitch; | 332 if (nStretch > 0) { |
| 333 keyWidth = Math.floor(key.weight * pixelsPerWeight); |
| 334 availableWidth -= keyWidth; |
| 335 } else { |
| 336 keyWidth = availableWidth; |
| 337 } |
| 338 } |
| 339 updateKey(key, keyWidth, keyHeight, xOffset, yOffset) |
| 340 xOffset += keyWidth + params.pitchX; |
320 } | 341 } |
321 } | 342 } |
322 | 343 |
323 /** | 344 /** |
324 * Aligns a row such that the spacebar is perfectly aligned with the row above | 345 * Aligns a row such that the spacebar is perfectly aligned with the row above |
325 * it. A precondition is that all keys in this row can be stretched as needed. | 346 * it. A precondition is that all keys in this row can be stretched as needed. |
326 * @param {!kb-row} row The current row to be aligned. | 347 * @param {!kb-row} row The current row to be aligned. |
327 * @param {!kb-row} prevRow The row above the current row. | 348 * @param {!kb-row} prevRow The row above the current row. |
328 * @param {!AlignmentOptions} params The parameters used to align the keyset. | 349 * @param {!AlignmentOptions} params Options for aligning the keyset. |
329 * @param {number} keyHeight The height of the keys in this row. | 350 * @param {number} keyHeight The height of the keys in this row. |
330 * @param {number} heightOffset The height offset caused by the rows above. | 351 * @param {number} heightOffset The height offset caused by the rows above. |
331 */ | 352 */ |
332 function realignSpacebarRow(row, prevRow, params, keyHeight, heightOffset) { | 353 function realignSpacebarRow(row, prevRow, params, keyHeight, heightOffset) { |
333 var allKeys = row.children; | 354 var allKeys = row.children; |
334 var stretchWeightBeforeSpace = 0; | 355 var stretchWeightBeforeSpace = 0; |
335 var stretchBefore = 0; | 356 var stretchBefore = 0; |
336 var stretchWeightAfterSpace = 0; | 357 var stretchWeightAfterSpace = 0; |
337 var stretchAfter = 0; | 358 var stretchAfter = 0; |
338 var spaceIndex = -1; | 359 var spaceIndex = -1; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 397 |
377 var rightKey = | 398 var rightKey = |
378 findClosestKey(prevRowKeys, spacePredictedRight, params.pitchX, false); | 399 findClosestKey(prevRowKeys, spacePredictedRight, params.pitchX, false); |
379 | 400 |
380 var yOffset = params.offsetTop + heightOffset; | 401 var yOffset = params.offsetTop + heightOffset; |
381 // Fix left side. | 402 // Fix left side. |
382 var leftEdge = parseFloat(leftKey.style.left); | 403 var leftEdge = parseFloat(leftKey.style.left); |
383 var leftWidth = leftEdge - params.offsetLeft - params.pitchX; | 404 var leftWidth = leftEdge - params.offsetLeft - params.pitchX; |
384 var leftKeys = allKeys.array().slice(0, spaceIndex); | 405 var leftKeys = allKeys.array().slice(0, spaceIndex); |
385 redistribute(leftKeys, | 406 redistribute(leftKeys, |
386 params.pitchX, | 407 params, |
387 params.offsetLeft, | 408 params.offsetLeft, |
388 leftWidth, | 409 leftWidth, |
389 keyHeight, | 410 keyHeight, |
390 yOffset); | 411 yOffset); |
391 // Fix right side. | 412 // Fix right side. |
392 var rightEdge = parseFloat(rightKey.style.left) + | 413 var rightEdge = parseFloat(rightKey.style.left) + |
393 parseFloat(rightKey.style.width); | 414 parseFloat(rightKey.style.width); |
394 var spacebarWidth = rightEdge - leftEdge; | 415 var spacebarWidth = rightEdge - leftEdge; |
395 updateKey(allKeys[spaceIndex], | 416 updateKey(allKeys[spaceIndex], |
396 spacebarWidth, | 417 spacebarWidth, |
397 keyHeight, | 418 keyHeight, |
398 leftEdge, | 419 leftEdge, |
399 yOffset); | 420 yOffset); |
400 var rightWidth = | 421 var rightWidth = |
401 params.availableWidth - (rightEdge - params.offsetLeft + params.pitchX); | 422 params.availableWidth - (rightEdge - params.offsetLeft + params.pitchX); |
402 var rightKeys = allKeys.array().slice(spaceIndex + 1); | 423 var rightKeys = allKeys.array().slice(spaceIndex + 1); |
403 redistribute(rightKeys, | 424 redistribute(rightKeys, |
404 params.pitchX, | 425 params, |
405 rightEdge + params.pitchX,//xOffset. | 426 rightEdge + params.pitchX,//xOffset. |
406 rightWidth, | 427 rightWidth, |
407 keyHeight, | 428 keyHeight, |
408 yOffset); | 429 yOffset); |
409 } | 430 } |
410 | 431 |
411 /** | 432 /** |
412 * Realigns a given row based on the parameters provided. | 433 * Realigns a given row based on the parameters provided. |
413 * @param {!kb-row} row The row to realign. | 434 * @param {!kb-row} row The row to realign. |
414 * @param {!AlignmentOptions} params The parameters used to align the keyset. | 435 * @param {!AlignmentOptions} params The parameters used to align the keyset. |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 var importedContent = importHTML(content); | 618 var importedContent = importHTML(content); |
598 expandHTML(importedContent); | 619 expandHTML(importedContent); |
599 return importedContent; | 620 return importedContent; |
600 } | 621 } |
601 | 622 |
602 // Prevents all default actions of touch. Keyboard should use its own gesture | 623 // Prevents all default actions of touch. Keyboard should use its own gesture |
603 // recognizer. | 624 // recognizer. |
604 addEventListener('touchstart', function(e) { e.preventDefault() }); | 625 addEventListener('touchstart', function(e) { e.preventDefault() }); |
605 addEventListener('touchend', function(e) { e.preventDefault() }); | 626 addEventListener('touchend', function(e) { e.preventDefault() }); |
606 addEventListener('touchmove', function(e) { e.preventDefault() }); | 627 addEventListener('touchmove', function(e) { e.preventDefault() }); |
OLD | NEW |