Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/resources/keyboard/common.js

Issue 7754019: Enable the keyboard to show the popup keyboard for inputting accented characters. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fix Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 /** 5 /**
6 * @fileoverview A simple virtual keyboard implementation. 6 * @fileoverview A simple virtual keyboard implementation.
7 */ 7 */
8 8
9 var KEY_MODE = 'key'; 9 var KEY_MODE = 'key';
10 var SHIFT_MODE = 'shift'; 10 var SHIFT_MODE = 'shift';
(...skipping 29 matching lines...) Expand all
40 var REPEAT_DELAY_MSEC = 500; 40 var REPEAT_DELAY_MSEC = 500;
41 41
42 /** 42 /**
43 * The repeat interval or number of milliseconds between subsequent keypresses. 43 * The repeat interval or number of milliseconds between subsequent keypresses.
44 * Use the same rate as Chromebook. 44 * Use the same rate as Chromebook.
45 * @type {number} 45 * @type {number}
46 */ 46 */
47 var REPEAT_INTERVAL_MSEC = 50; 47 var REPEAT_INTERVAL_MSEC = 50;
48 48
49 /** 49 /**
50 * The keyboard layout name currently in use.
51 * @type {string}
52 */
53 var currentKeyboardLayout = 'us';
54
55 /**
56 * The popup keyboard layout name currently in use.
57 * @type {string}
58 */
59 var currentPopupName = '';
60
61 /**
50 * A structure to track the currently repeating key on the keyboard. 62 * A structure to track the currently repeating key on the keyboard.
51 */ 63 */
52 var repeatKey = { 64 var repeatKey = {
53 /** 65 /**
54 * The timer for the delay before repeating behaviour begins. 66 * The timer for the delay before repeating behaviour begins.
55 * @type {number|undefined} 67 * @type {number|undefined}
56 */ 68 */
57 timer: undefined, 69 timer: undefined,
58 70
59 /** 71 /**
(...skipping 14 matching lines...) Expand all
74 cancel: function() { 86 cancel: function() {
75 clearTimeout(this.timer); 87 clearTimeout(this.timer);
76 clearInterval(this.interval); 88 clearInterval(this.interval);
77 this.timer = undefined; 89 this.timer = undefined;
78 this.interval = undefined; 90 this.interval = undefined;
79 this.key = undefined; 91 this.key = undefined;
80 } 92 }
81 }; 93 };
82 94
83 /** 95 /**
96 * An array to track the currently touched keys on the popup keyboard.
97 */
98 var touchedKeys = [];
99
100 /**
101 * Set the keyboard mode.
102 * @param {string} mode The new mode.
103 * @return {void}
104 */
105 function setMode(mode) {
106 var rows = KEYBOARDS[currentKeyboardLayout]['rows'];
107 for (var i = 0; i < rows.length; ++i) {
108 rows[i].showMode(mode);
109 }
110
111 if (!currentPopupName) {
112 return;
113 }
114 var popupRows = KEYBOARDS[currentPopupName]['rows'];
115 for (var i = 0; i < popupRows.length; ++i) {
116 popupRows[i].showMode(mode);
117 }
118 }
119
120 /**
84 * Transition the mode according to the given transition. 121 * Transition the mode according to the given transition.
85 * @param {string} transition The transition to take. 122 * @param {string} transition The transition to take.
86 * @return {void} 123 * @return {void}
87 */ 124 */
88 function transitionMode(transition) { 125 function transitionMode(transition) {
89 currentMode = MODE_TRANSITIONS[currentMode + transition]; 126 currentMode = MODE_TRANSITIONS[currentMode + transition];
90 setMode(currentMode); 127 setMode(currentMode);
91 } 128 }
92 129
93 /** 130 /**
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 // at the correct time. 246 // at the correct time.
210 repeatKey.timer = setTimeout(function() { 247 repeatKey.timer = setTimeout(function() {
211 repeatKey.timer = undefined; 248 repeatKey.timer = undefined;
212 repeatKey.interval = setInterval(function() { 249 repeatKey.interval = setInterval(function() {
213 keyDownHandler(); 250 keyDownHandler();
214 }, REPEAT_INTERVAL_MSEC); 251 }, REPEAT_INTERVAL_MSEC);
215 }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC)); 252 }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC));
216 } 253 }
217 254
218 if (keyLongHandler) { 255 if (keyLongHandler) {
256 // Copy the currentTarget of event, which is neccessary in
257 // showPopupKeyboard, because |evt| can be modified before
258 // |keyLongHandler| is called.
259 var evt2 = {};
bryeung 2011/09/20 19:30:50 nit: evtCopy instead of evt2?
mazda 2011/09/22 08:40:38 Done.
260 evt2.currentTarget = evt.currentTarget;
219 key.longPressTimer = setTimeout(function() { 261 key.longPressTimer = setTimeout(function() {
220 keyLongHandler(evt), 262 keyLongHandler(evt2),
221 clearTimeout(key.longPressTimer); 263 clearTimeout(key.longPressTimer);
222 delete key.longPressTimer; 264 delete key.longPressTimer;
223 key.pressed = false; 265 key.pressed = false;
224 }, LONGPRESS_DELAY_MSEC); 266 }, LONGPRESS_DELAY_MSEC);
225 } 267 }
226 }; 268 };
227 269
228 /** 270 /**
229 * Handle a key up event on the virtual key. 271 * Handle a key up event on the virtual key.
230 * @param {UIEvent} evt The UI event which triggered the key up. 272 * @param {UIEvent} evt The UI event which triggered the key up.
(...skipping 18 matching lines...) Expand all
249 if (repeatKey.key == key) { 291 if (repeatKey.key == key) {
250 repeatKey.cancel(); 292 repeatKey.cancel();
251 } 293 }
252 294
253 if (keyUpHandler) { 295 if (keyUpHandler) {
254 keyUpHandler(); 296 keyUpHandler();
255 } 297 }
256 }; 298 };
257 299
258 var outHandler = function(evt) { 300 var outHandler = function(evt) {
301 // Key element contains a div that holds text like this.
302 //
303 // <div class="key r1">
304 // <div class="text-key">a</div>
305 // </div>
306 //
307 // We are interested in mouseout event sent when mouse cursor moves out of
308 // the external div, but mouseout event is sent when mouse cursor moves out
309 // of the internal div or moves into the internal div, too.
310 // Filter out the last two cases here.
311 if (evt.target != evt.currentTarget ||
312 evt.toElement.parentNode == evt.fromElement) {
313 return;
314 }
259 // Reset key press state if the point goes out of the element. 315 // Reset key press state if the point goes out of the element.
260 key.pressed = false; 316 key.pressed = false;
261 // Reset long-press timer. 317 // Reset long-press timer.
262 if (key.longPressTimer) { 318 if (key.longPressTimer) {
263 clearTimeout(key.longPressTimer); 319 clearTimeout(key.longPressTimer);
264 delete key.longPressTimer 320 delete key.longPressTimer
265 } 321 }
266 } 322 }
267 323
268 // Setup mouse event handlers. 324 // Setup mouse event handlers.
(...skipping 13 matching lines...) Expand all
282 * @param {string} key The key paramater to sendKey. 338 * @param {string} key The key paramater to sendKey.
283 * @return {function()} A function which calls sendKey(key). 339 * @return {function()} A function which calls sendKey(key).
284 */ 340 */
285 function sendKeyFunction(key) { 341 function sendKeyFunction(key) {
286 return function() { 342 return function() {
287 sendKey(key); 343 sendKey(key);
288 }; 344 };
289 } 345 }
290 346
291 /** 347 /**
348 * Dispatch custom events to the elements at the touch points.
349 * touchpointmove events are dispatched responding to a touchmove and
bryeung 2011/09/20 19:30:50 nit: make the name more obvious (e.g. touchmove_po
mazda 2011/09/22 08:40:38 Done.
350 * touchpointend events responding to a touchend event respectively.
351 * @param {UIEvent} evt The touch event that contains touch points information.
352 * @return {void}
353 */
354 function dispatchTouchPointEvent(evt) {
bryeung 2011/09/20 19:30:50 nit: make the name more obvious (e.g. dispatchCust
mazda 2011/09/22 08:40:38 Done.
355 var type = null;
356 var touches = null;
357 if (evt.type == 'touchmove') {
358 type = 'touchpointmove';
359 touches = evt.touches;
360 } else if (evt.type == 'touchend') {
361 type = 'touchpointend';
362 touches = evt.changedTouches;
363 } else {
364 return;
365 }
366
367 for (var i = 0; i < touches.length; ++i) {
368 var dispatchedEvent = document.createEvent('Event');
369 dispatchedEvent.initEvent(type, true, false);
370 var touch = touches[i];
371 var key = document.elementFromPoint(touch.screenX, touch.screenY);
372 if (key) {
373 key.dispatchEvent(dispatchedEvent);
374 }
375 }
376 }
377
378 /**
379 * Handle a touch move event on the key to make changes to the popup keyboard.
380 * @param {UIEvent} evt The UI event which triggered the touch move.
381 * @return {void}
382 */
383 function trackTouchMoveForPopup(evt) {
384 var previous = touchedKeys;
385 touchedKeys = [];
386 dispatchTouchPointEvent(evt);
387 for (var i = 0; i < previous.length; ++i) {
388 if (touchedKeys.indexOf(previous[i]) == -1) {
389 previous[i].classList.remove('highlighted');
390 }
391 }
392 for (var i = 0; i < touchedKeys.length; ++i) {
393 touchedKeys[i].classList.add('highlighted');
394 }
395 }
396
397 /**
398 * Handle a touch end event on the key to make changes to the popup keyboard.
399 * @param {UIEvent} evt The UI event which triggered the touch end.
400 * @return {void}
401 */
402 function trackTouchEndForPopup(evt) {
403 for (var i = 0; i < touchedKeys.length; ++i) {
404 touchedKeys[i].classList.remove('highlighted');
405 }
406 dispatchTouchPointEvent(evt);
407 hidePopupKeyboard();
408
409 touchedKeys = [];
410 evt.target.removeEventListener('touchmove', trackTouchMoveForPopup);
411 evt.target.removeEventListener('touchend', trackTouchEndForPopup);
412 }
413
414 /**
292 * Show the popup keyboard. 415 * Show the popup keyboard.
293 * @param {string} name The name of the popup keyboard. 416 * @param {string} name The name of the popup keyboard.
bryeung 2011/09/20 19:30:50 missing evt param doc
mazda 2011/09/22 08:40:38 Done.
417 * @return {void}
294 */ 418 */
295 function showPopupKeyboard(name) { 419 function showPopupKeyboard(name, evt) {
296 // TODO(mazda): Implement this function. 420 var popupDiv = document.getElementById('popup');
297 console.warn('Popup keyboard is not implemented yet.'); 421 if (popupDiv.style.visibility == 'visible') {
422 return;
423 }
424
425 // Reinitialize the rows of the popup keyboard
426 if (currentPopupName != name) {
427 while (popupDiv.firstChild) {
428 popupDiv.removeChild(popupDiv.firstChild);
429 }
430 if (currentPopupName in KEYBOARDS) {
431 delete KEYBOARDS[currentPopupName].rows;
432 }
433 initRows(name, popupDiv, true);
434 currentPopupName = name;
435 }
436
437 // Set the mode of the popup keyboard
438 var popupRows = KEYBOARDS[currentPopupName]['rows'];
439 for (var i = 0; i < popupRows.length; ++i) {
440 popupRows[i].showMode(currentMode);
441 }
442
443 // Calculate the size of popup keyboard based on the size of the key.
444 var keyElement = evt.currentTarget;
445 var keyboard = KEYBOARDS[name];
446 var rows = keyboard['definition'];
447 var height = keyElement.offsetHeight * rows.length;
448 var aspect = keyboard['aspect'];
449 var width = aspect * height;
450 popupDiv.style.width = width + 'px';
451 popupDiv.style.height = height + 'px';
452
453 // Place the popup keyboard above the key
454 var rect = keyElement.getBoundingClientRect();
455 var left = (rect.left + rect.right) / 2 - width / 2;
456 left = Math.min(Math.max(left, 0), window.innerWidth - width);
457 var top = rect.top - height;
458 top = Math.min(Math.max(top, 0), window.innerHeight - height);
459 popupDiv.style.left = left + 'px';
460 popupDiv.style.top = top + 'px';
461 popupDiv.style.visibility = 'visible';
462
463 keyElement.addEventListener('touchmove', trackTouchMoveForPopup);
464 keyElement.addEventListener('touchend', trackTouchEndForPopup);
298 } 465 }
299 466
300 /** 467 /**
301 * Create closure for the showPopupKeyboard function. 468 * Create closure for the showPopupKeyboard function.
302 * @param {string} name The name paramater to showPopupKeyboard. 469 * @param {string} name The name paramater to showPopupKeyboard.
303 * @return {function()} A function which calls showPopupKeyboard(name). 470 * @return {function()} A function which calls showPopupKeyboard(name).
304 */ 471 */
305 function showPopupKeyboardFunction(name) { 472 function showPopupKeyboardFunction(name) {
306 return function () { 473 return function (evt) {
307 showPopupKeyboard(name); 474 showPopupKeyboard(name, evt);
308 }; 475 };
309 } 476 }
310 477
311 /** 478 /**
479 * Hide the popup keyboard.
480 * @return {void}
481 */
482 function hidePopupKeyboard() {
bryeung 2011/09/20 19:30:50 Is it worth moving the clean-up code (that removes
mazda 2011/09/22 08:40:38 That would provide better performance if the popup
483 var popupDiv = document.getElementById('popup');
484 popupDiv.style.visibility = 'hidden';
485 }
486
487 /**
312 * Plain-old-data class to represent a character. 488 * Plain-old-data class to represent a character.
313 * @param {string} display The HTML to be displayed. 489 * @param {string} display The HTML to be displayed.
314 * @param {string} id The key identifier for this Character. 490 * @param {string} id The key identifier for this Character.
315 * @constructor 491 * @constructor
316 */ 492 */
317 function Character(display, id) { 493 function Character(display, id) {
318 this.display = display; 494 this.display = display;
319 this.keyIdentifier = id; 495 this.keyIdentifier = id;
320 } 496 }
321 497
322 /** 498 /**
323 * Convenience function to make the keyboard data more readable. 499 * Convenience function to make the keyboard data more readable.
324 * @param {string} display Both the display and id for the created Character. 500 * @param {string} display The display for the created Character.
501 * @param {string} opt_id The id for the created Character.
502 * @return {Character} A character that contains display and opt_id. If
503 * opt_id is omitted, display is used as the id.
325 */ 504 */
326 function C(display) { 505 function C(display, opt_id) {
327 return new Character(display, display); 506 var id = opt_id || display;
507 return new Character(display, id);
328 } 508 }
329 509
330 /** 510 /**
511 * Convenience function to make the keyboard data more readable.
512 * @param {string} display The display for the created Character.
513 * @param {string} opt_id The id for the created Character.
514 * @param {string} opt_popupName The popup keyboard name for this character.
515 * @return {Object} An object that contains a Character and the popup keyboard
516 * name.
517 */
518 function CP(display, opt_id, opt_popupName) {
519 var result = { character: C(display, opt_id) };
520 if (opt_popupName) {
521 result['popupName'] = opt_popupName;
522 }
523 return result;
524 }
525
526 /**
331 * An abstract base-class for all keys on the keyboard. 527 * An abstract base-class for all keys on the keyboard.
332 * @constructor 528 * @constructor
333 */ 529 */
334 function BaseKey() {} 530 function BaseKey() {}
335 531
336 BaseKey.prototype = { 532 BaseKey.prototype = {
337 /** 533 /**
338 * The cell type of this key. Determines the background colour. 534 * The cell type of this key. Determines the background colour.
339 * @type {string} 535 * @type {string}
340 */ 536 */
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 * @param {string} mode The keyboard mode to create elements for. 585 * @param {string} mode The keyboard mode to create elements for.
390 * @return {Element} The top-level DOM Element for the key. 586 * @return {Element} The top-level DOM Element for the key.
391 */ 587 */
392 makeDOM: function(mode) { 588 makeDOM: function(mode) {
393 throw new Error('makeDOM not implemented in BaseKey'); 589 throw new Error('makeDOM not implemented in BaseKey');
394 }, 590 },
395 }; 591 };
396 592
397 /** 593 /**
398 * A simple key which displays Characters. 594 * A simple key which displays Characters.
595 * @param {Object} key The Character and the popup name for KEY_MODE.
596 * @param {Object} shift The Character and the popup name for SHIFT_MODE.
597 * @param {Object} num The Character and the popup name for NUMBER_MODE.
598 * @param {Object} symbol The Character and the popup name for SYMBOL_MODE.
599 * @constructor
600 * @extends {BaseKey}
601 */
602 function Key(key, shift, num, symbol) {
603 this.modeElements_ = {};
604 this.cellType_ = '';
605
606 this.modes_ = {};
607 this.modes_[KEY_MODE] = key ? key.character : null;
608 this.modes_[SHIFT_MODE] = shift ? shift.character : null;
609 this.modes_[NUMBER_MODE] = num ? num.character : null;
610 this.modes_[SYMBOL_MODE] = symbol ? symbol.character : null;
611
612 this.popupNames_ = {};
613 this.popupNames_[KEY_MODE] = key ? key.popupName : null;
614 this.popupNames_[SHIFT_MODE] = shift ? shift.popupName : null;
615 this.popupNames_[NUMBER_MODE] = num ? num.popupName : null;
616 this.popupNames_[SYMBOL_MODE] = symbol ? symbol.popupName : null;
617 }
618
619 Key.prototype = {
620 __proto__: BaseKey.prototype,
621
622 /** @inheritDoc */
623 makeDOM: function(mode) {
624 if (!this.modes_[mode]) {
625 return null;
626 }
627
628 this.modeElements_[mode] = document.createElement('div');
629 var element = this.modeElements_[mode];
630 element.className = 'key';
631
632 addContent(element, this.modes_[mode].display);
633
634 var longHandler = this.popupNames_[mode] ?
635 showPopupKeyboardFunction(this.popupNames_[mode]) : null;
636 setupKeyEventHandlers(this, element,
637 { 'up': sendKeyFunction(this.modes_[mode].keyIdentifier),
638 'long': longHandler });
639 return element;
640 }
641 };
642
643 /**
644 * A simple key which displays Characters on the popup keyboard.
399 * @param {Character} key The Character for KEY_MODE. 645 * @param {Character} key The Character for KEY_MODE.
400 * @param {Character} shift The Character for SHIFT_MODE. 646 * @param {Character} shift The Character for SHIFT_MODE.
401 * @param {Character} num The Character for NUMBER_MODE. 647 * @param {Character} num The Character for NUMBER_MODE.
402 * @param {Character} symbol The Character for SYMBOL_MODE. 648 * @param {Character} symbol The Character for SYMBOL_MODE.
403 * @constructor 649 * @constructor
404 * @extends {BaseKey} 650 * @extends {BaseKey}
405 */ 651 */
406 function Key(key, shift, num, symbol) { 652 function PopupKey(key, shift, num, symbol) {
407 this.modeElements_ = {}; 653 this.modeElements_ = {};
408 this.cellType_ = ''; 654 this.cellType_ = '';
409 655
410 this.modes_ = {}; 656 this.modes_ = {};
411 this.modes_[KEY_MODE] = key; 657 this.modes_[KEY_MODE] = key;
412 this.modes_[SHIFT_MODE] = shift; 658 this.modes_[SHIFT_MODE] = shift;
413 this.modes_[NUMBER_MODE] = num; 659 this.modes_[NUMBER_MODE] = num;
414 this.modes_[SYMBOL_MODE] = symbol; 660 this.modes_[SYMBOL_MODE] = symbol;
415 } 661 }
416 662
417 Key.prototype = { 663 PopupKey.prototype = {
418 __proto__: BaseKey.prototype, 664 __proto__: BaseKey.prototype,
419 665
420 /** @inheritDoc */ 666 /** @inheritDoc */
421 makeDOM: function(mode) { 667 makeDOM: function(mode) {
422 if (!this.modes_[mode]) { 668 if (!this.modes_[mode]) {
423 return null; 669 return null;
424 } 670 }
425 671
426 this.modeElements_[mode] = document.createElement('div'); 672 this.modeElements_[mode] = document.createElement('div');
427 this.modeElements_[mode].className = 'key'; 673 var element = this.modeElements_[mode];
428 addContent(this.modeElements_[mode], this.modes_[mode].display); 674 element.className = 'key popupkey';
429 675
430 // TODO(mazda): Set the long-press handler only if the key has characters 676 addContent(element, this.modes_[mode].display);
431 // to show on the popup keyboard.
432 setupKeyEventHandlers(this, this.modeElements_[mode],
433 { 'up': sendKeyFunction(this.modes_[mode].keyIdentifier),
434 'long': showPopupKeyboardFunction('') });
435 677
436 return this.modeElements_[mode]; 678 var upHandler = sendKeyFunction(this.modes_[mode].keyIdentifier);
679 element.addEventListener('touchpointmove', function(evt) {
680 touchedKeys.push(element);
681 });
682 element.addEventListener('touchpointend', upHandler);
683 element.addEventListener('mouseup', upHandler);
684 element.addEventListener('mouseover', function(evt) {
685 element.classList.add('highlighted');
686 });
687 element.addEventListener('mouseout', function(evt) {
688 element.classList.remove('highlighted');
689 });
690 return element;
437 } 691 }
438 }; 692 };
439 693
440 /** 694 /**
441 * A key which displays an SVG image. 695 * A key which displays an SVG image.
442 * @param {string} className The class that provides the image. 696 * @param {string} className The class that provides the image.
443 * @param {string} keyId The key identifier for the key. 697 * @param {string} keyId The key identifier for the key.
444 * @param {boolean} opt_repeat True if the key should repeat. 698 * @param {boolean} opt_repeat True if the key should repeat.
445 * @constructor 699 * @constructor
446 * @extends {BaseKey} 700 * @extends {BaseKey}
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 * Shows the given mode. 952 * Shows the given mode.
699 * @param {string} mode The mode to show. 953 * @param {string} mode The mode to show.
700 * @return {void} 954 * @return {void}
701 */ 955 */
702 showMode: function(mode) { 956 showMode: function(mode) {
703 for (var i = 0; i < MODES.length; ++i) { 957 for (var i = 0; i < MODES.length; ++i) {
704 this.modeElements_[MODES[i]].style.display = 'none'; 958 this.modeElements_[MODES[i]].style.display = 'none';
705 } 959 }
706 this.modeElements_[mode].style.display = '-webkit-box'; 960 this.modeElements_[mode].style.display = '-webkit-box';
707 }, 961 },
962
963 /**
964 * Returns the size of keys this row contains.
965 * @return {number} The size of keys.
966 */
967 get length() {
968 return this.keys_.length;
969 }
708 }; 970 };
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/keyboard/index.html » ('j') | chrome/browser/resources/keyboard/main.css » ('J')

Powered by Google App Engine
This is Rietveld 408576698