Chromium Code Reviews| 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 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 * @param {number} width The total extraneous width to distribute. | 301 * @param {number} width The total extraneous width to distribute. |
| 302 * @param {number} keyHeight The height of each key. | 302 * @param {number} keyHeight The height of each key. |
| 303 * @param {number} yOffset The y-coordinate of the top edge of the row. | 303 * @param {number} yOffset The y-coordinate of the top edge of the row. |
| 304 */ | 304 */ |
| 305 function redistribute(allKeys, pitch, xOffset, width, keyHeight, yOffset) { | 305 function redistribute(allKeys, pitch, xOffset, width, keyHeight, yOffset) { |
| 306 var weight = 0; | 306 var weight = 0; |
| 307 for (var i = 0; i < allKeys.length; i++) { | 307 for (var i = 0; i < allKeys.length; i++) { |
| 308 weight += allKeys[i].weight; | 308 weight += allKeys[i].weight; |
| 309 } | 309 } |
| 310 var availableWidth = width - (allKeys.length - 1) * pitch; | 310 var availableWidth = width - (allKeys.length - 1) * pitch; |
| 311 var pixelsPerWeight = width / weight; | 311 var pixelsPerWeight = availableWidth / weight; |
| 312 for (var i = 0; i < allKeys.length; i++) { | 312 for (var i = 0; i < allKeys.length; i++) { |
| 313 var keyWidth = Math.floor(allKeys[i].weight * pixelsPerWeight); | 313 var keyWidth = Math.floor(allKeys[i].weight * pixelsPerWeight); |
| 314 if (i == allKeys.length -1) { | 314 if (i == allKeys.length -1) { |
| 315 keyWidth = availableWidth; | 315 keyWidth = availableWidth; |
| 316 } | 316 } |
| 317 updateKey(allKeys[i], keyWidth, keyHeight, xOffset, yOffset) | 317 updateKey(allKeys[i], keyWidth, keyHeight, xOffset, yOffset) |
| 318 availableWidth -= keyWidth; | 318 availableWidth -= keyWidth; |
| 319 xOffset += keyWidth + pitch; | 319 xOffset += keyWidth + pitch; |
| 320 } | 320 } |
| 321 } | 321 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 } | 408 } |
| 409 | 409 |
| 410 /** | 410 /** |
| 411 * Realigns a given row based on the parameters provided. | 411 * Realigns a given row based on the parameters provided. |
| 412 * @param {!kb-row} row The row to realign. | 412 * @param {!kb-row} row The row to realign. |
| 413 * @param {!AlignmentOptions} params The parameters used to align the keyset. | 413 * @param {!AlignmentOptions} params The parameters used to align the keyset. |
| 414 * @param {number} heightOffset The offset caused by rows above it. | 414 * @param {number} heightOffset The offset caused by rows above it. |
| 415 */ | 415 */ |
| 416 function realignRow(row, params, heightOffset) { | 416 function realignRow(row, params, heightOffset) { |
| 417 var all = row.children; | 417 var all = row.children; |
| 418 var stretch = []; | 418 var nStretch = 0; |
| 419 var stretchWeightSum = 0; | 419 var stretchWeightSum = 0; |
| 420 var allSum = 0; | 420 var allSum = 0; |
| 421 | 421 // Keeps track of where to distribute pixels caused by round off errors. |
| 422 var deltaWidth = []; | |
| 422 for (var i = 0; i < all.length; i++) { | 423 for (var i = 0; i < all.length; i++) { |
| 424 deltaWidth.push(0) | |
| 423 var key = all[i]; | 425 var key = all[i]; |
| 424 if (key.weight == DEFAULT_KEY_WEIGHT_X){ | 426 if (key.weight == DEFAULT_KEY_WEIGHT_X){ |
| 425 allSum += params.keyWidth; | 427 allSum += params.keyWidth; |
| 426 continue; | 428 continue; |
| 427 } | 429 } |
| 428 stretch.push(key) | 430 nStretch++; |
| 429 var width = | 431 var width = |
| 430 Math.floor((params.keyWidth/DEFAULT_KEY_WEIGHT_X) * key.weight); | 432 Math.floor((params.keyWidth/DEFAULT_KEY_WEIGHT_X) * key.weight); |
| 431 allSum += width; | 433 allSum += width; |
| 432 stretchWeightSum += key.weight; | 434 stretchWeightSum += key.weight; |
| 433 } | 435 } |
| 434 var nStretch = stretch.length; | |
| 435 var nRegular = all.length - nStretch; | 436 var nRegular = all.length - nStretch; |
| 436 // Extra space. | 437 // Extra space. |
| 437 var extra = params.availableWidth - | 438 var extra = params.availableWidth - |
| 438 allSum - | 439 allSum - |
| 439 (params.pitchX * (all.length -1)); | 440 (params.pitchX * (all.length -1)); |
| 440 var xOffset = params.offsetLeft; | 441 var xOffset = params.offsetLeft; |
| 442 | |
| 441 var alignment = row.align; | 443 var alignment = row.align; |
| 442 switch (alignment) { | 444 switch (alignment) { |
| 443 case RowAlignment.STRETCH: | 445 case RowAlignment.STRETCH: |
| 444 var extraPerWeight = extra/stretchWeightSum; | 446 var extraPerWeight = extra/stretchWeightSum; |
| 445 for (var i = 0; i < stretch.length; i++) { | 447 var stretchSoFar = 0; |
|
kevers
2014/02/03 19:01:00
stretchTally ... or decrement nStretch and check f
rsadam
2014/02/03 19:36:21
Done.
| |
| 446 var bonus = Math.floor(stretch[i].weight * extraPerWeight); | 448 for (var i = 0; i < all.length; i++) { |
| 447 extra -= bonus; | 449 if (all[i].weight == DEFAULT_KEY_WEIGHT_X) |
| 450 continue; | |
| 451 var delta = Math.floor(all[i].weight * extraPerWeight); | |
| 452 extra -= delta; | |
| 453 deltaWidth[i] = delta; | |
| 448 // All left-over pixels assigned to right most key. | 454 // All left-over pixels assigned to right most key. |
| 449 if (i == (stretch.length - 1)) | 455 if (stretchSoFar == (nStretch - 1)) |
| 450 stretch[i].setAttribute('bonus', bonus + extra); | 456 deltaWidth[i] += extra; |
| 451 else | 457 stretchSoFar++; |
| 452 stretch[i].setAttribute('bonus', bonus); | |
| 453 } | 458 } |
| 454 break; | 459 break; |
| 455 case RowAlignment.CENTER: | 460 case RowAlignment.CENTER: |
| 456 xOffset += Math.floor(extra/2) | 461 xOffset += Math.floor(extra/2) |
| 457 break; | 462 break; |
| 458 case RowAlignment.JUSTIFY: | 463 case RowAlignment.RIGHT: |
| 459 xOffset += extra; | 464 xOffset += extra; |
| 460 break; | 465 break; |
| 466 case RowAlignment.STRETCHRIGHT: | |
| 467 deltaWidth[all.length - 1] = extra; | |
| 461 default: | 468 default: |
| 462 break; | 469 break; |
| 463 }; | 470 }; |
| 464 | 471 |
| 465 var yOffset = params.offsetTop + heightOffset; | 472 var yOffset = params.offsetTop + heightOffset; |
| 466 var left = xOffset; | 473 var left = xOffset; |
| 467 for (var i = 0; i < all.length; i++) { | 474 for (var i = 0; i < all.length; i++) { |
| 468 var key = all[i]; | 475 var key = all[i]; |
| 469 var width = params.keyWidth; | 476 var width = params.keyWidth; |
| 470 if (key.weight != DEFAULT_KEY_WEIGHT_X) { | 477 if (key.weight != DEFAULT_KEY_WEIGHT_X) |
| 471 width = Math.floor((params.keyWidth/DEFAULT_KEY_WEIGHT_X) * key.weight) | 478 width = Math.floor((params.keyWidth/DEFAULT_KEY_WEIGHT_X) * key.weight) |
| 472 var bonus = key.getAttribute('bonus') | 479 width += deltaWidth[i]; |
| 473 if (bonus) | |
| 474 width += parseInt(bonus) | |
| 475 } | |
| 476 updateKey(key, width, params.keyHeight, left, yOffset) | 480 updateKey(key, width, params.keyHeight, left, yOffset) |
| 477 left += (width + params.pitchX); | 481 left += (width + params.pitchX); |
| 478 } | 482 } |
| 479 } | 483 } |
| 480 | 484 |
| 481 /** | 485 /** |
| 482 * Realigns the keysets in all layouts of the keyboard. | 486 * Realigns the keysets in all layouts of the keyboard. |
| 483 */ | 487 */ |
| 484 function realignAll() { | 488 function realignAll() { |
| 485 var keyboard = $('keyboard'); | 489 var keyboard = $('keyboard'); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 518 keyboard.stale = false; | 522 keyboard.stale = false; |
| 519 } | 523 } |
| 520 | 524 |
| 521 /* | 525 /* |
| 522 * Realigns a given keyset. | 526 * Realigns a given keyset. |
| 523 * @param {Object} keyset The keyset to realign. | 527 * @param {Object} keyset The keyset to realign. |
| 524 * @param {!AlignmentOptions} params The parameters used to align the keyset. | 528 * @param {!AlignmentOptions} params The parameters used to align the keyset. |
| 525 */ | 529 */ |
| 526 function realignKeyset(keyset, params) { | 530 function realignKeyset(keyset, params) { |
| 527 var rows = keyset.querySelectorAll('kb-row').array(); | 531 var rows = keyset.querySelectorAll('kb-row').array(); |
| 532 var maxSize = getKeyboardBounds(); | |
| 533 var height = (maxSize.width > ASPECT_RATIO * maxSize.height) ? | |
| 534 maxSize.height : Math.floor(maxSize.width / ASPECT_RATIO); | |
| 535 keyset.style.fontSize = (height / FONT_SIZE_RATIO / rows.length) + 'px'; | |
| 536 | |
| 528 var heightOffset = 0; | 537 var heightOffset = 0; |
| 529 for (var i = 0; i < rows.length; i++) { | 538 for (var i = 0; i < rows.length; i++) { |
| 530 var row = rows[i]; | 539 var row = rows[i]; |
| 531 if (row.querySelector('.space') && (i > 1)) { | 540 if (row.querySelector('.space') && (i > 1)) { |
| 532 realignSpacebarRow(row, rows[i-1], params, heightOffset) | 541 realignSpacebarRow(row, rows[i-1], params, heightOffset) |
| 533 } else { | 542 } else { |
| 534 realignRow(row, params, heightOffset); | 543 realignRow(row, params, heightOffset); |
| 535 } | 544 } |
| 536 heightOffset += (params.keyHeight + params.pitchY); | 545 heightOffset += (params.keyHeight + params.pitchY); |
| 537 } | 546 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 var importedContent = importHTML(content); | 596 var importedContent = importHTML(content); |
| 588 expandHTML(importedContent); | 597 expandHTML(importedContent); |
| 589 return importedContent; | 598 return importedContent; |
| 590 } | 599 } |
| 591 | 600 |
| 592 // Prevents all default actions of touch. Keyboard should use its own gesture | 601 // Prevents all default actions of touch. Keyboard should use its own gesture |
| 593 // recognizer. | 602 // recognizer. |
| 594 addEventListener('touchstart', function(e) { e.preventDefault() }); | 603 addEventListener('touchstart', function(e) { e.preventDefault() }); |
| 595 addEventListener('touchend', function(e) { e.preventDefault() }); | 604 addEventListener('touchend', function(e) { e.preventDefault() }); |
| 596 addEventListener('touchmove', function(e) { e.preventDefault() }); | 605 addEventListener('touchmove', function(e) { e.preventDefault() }); |
| OLD | NEW |