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

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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 // at the correct time. 244 // at the correct time.
208 repeatKey.timer = setTimeout(function() { 245 repeatKey.timer = setTimeout(function() {
209 repeatKey.timer = undefined; 246 repeatKey.timer = undefined;
210 repeatKey.interval = setInterval(function() { 247 repeatKey.interval = setInterval(function() {
211 keyDownHandler(); 248 keyDownHandler();
212 }, REPEAT_INTERVAL_MSEC); 249 }, REPEAT_INTERVAL_MSEC);
213 }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC)); 250 }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC));
214 } 251 }
215 252
216 if (keyLongHandler) { 253 if (keyLongHandler) {
254 // Copy the current state of event because |evt| can be modified before
255 // |keyLongHandler| is called.
256 var evt2 = {};
257 for (var name in evt) {
258 evt2[name] = evt[name];
259 }
217 key.longPressTimer = setTimeout(function() { 260 key.longPressTimer = setTimeout(function() {
218 keyLongHandler(evt), 261 keyLongHandler(evt2),
219 clearTimeout(key.longPressTimer); 262 clearTimeout(key.longPressTimer);
220 delete key.longPressTimer; 263 delete key.longPressTimer;
221 key.pressed = false; 264 key.pressed = false;
222 }, LONGPRESS_DELAY_MSEC); 265 }, LONGPRESS_DELAY_MSEC);
223 } 266 }
224 }; 267 };
225 268
226 /** 269 /**
227 * Handle a key up event on the virtual key. 270 * Handle a key up event on the virtual key.
228 * @param {UIEvent} evt The UI event which triggered the key up. 271 * @param {UIEvent} evt The UI event which triggered the key up.
(...skipping 16 matching lines...) Expand all
245 repeatKey.cancel(); 288 repeatKey.cancel();
246 } 289 }
247 290
248 if (keyUpHandler) { 291 if (keyUpHandler) {
249 keyUpHandler(); 292 keyUpHandler();
250 } 293 }
251 evt.preventDefault(); 294 evt.preventDefault();
252 }; 295 };
253 296
254 var outHandler = function(evt) { 297 var outHandler = function(evt) {
298 if (evt.target != evt.currentTarget ||
299 evt.toElement.parentNode == evt.fromElement) {
300 return;
301 }
255 // Reset key press state if the point goes out of the element. 302 // Reset key press state if the point goes out of the element.
256 key.pressed = false; 303 key.pressed = false;
257 // Reset long-press timer. 304 // Reset long-press timer.
258 if (key.longPressTimer) { 305 if (key.longPressTimer) {
259 clearTimeout(key.longPressTimer); 306 clearTimeout(key.longPressTimer);
260 delete key.longPressTimer 307 delete key.longPressTimer
261 } 308 }
262 } 309 }
263 310
264 // Setup mouse event handlers. 311 // Setup mouse event handlers.
(...skipping 13 matching lines...) Expand all
278 * @param {string} key The key paramater to sendKey. 325 * @param {string} key The key paramater to sendKey.
279 * @return {function()} A function which calls sendKey(key). 326 * @return {function()} A function which calls sendKey(key).
280 */ 327 */
281 function sendKeyFunction(key) { 328 function sendKeyFunction(key) {
282 return function() { 329 return function() {
283 sendKey(key); 330 sendKey(key);
284 }; 331 };
285 } 332 }
286 333
287 /** 334 /**
335 * Dispatch custom events to the elements at the touch points.
336 * touchpointmove events are dispatched responding to a touchmove and
337 * touchpointend events responding to a touchend event respectively.
338 * @param {UIEvent} evt The touch event that contains touch points information.
339 * @return {void}
340 */
341 function dispatchTouchPointEvent(evt) {
342 var type = null;
343 var touches = null;
344 if (evt.type == 'touchmove') {
345 type = 'touchpointmove';
346 touches = evt.touches;
347 } else if (evt.type == 'touchend') {
348 type = 'touchpointend';
349 touches = evt.changedTouches;
350 } else {
351 return;
352 }
353
354 for (var i = 0; i < touches.length; ++i) {
355 var dispatchedEvent = document.createEvent('Event');
356 dispatchedEvent.initEvent(type, true, false);
357 var touch = touches[i];
358 var key = document.elementFromPoint(touch.screenX, touch.screenY);
359 if (key) {
360 key.dispatchEvent(dispatchedEvent);
361 }
362 }
363 }
364
365 /**
366 * Handle a touch move event on the key to make changes to the popup keyboard.
367 * @param {UIEvent} evt The UI event which triggered the touch move.
368 * @return {void}
369 */
370 function trackTouchMoveForPopup(evt) {
371 var previous = touchedKeys;
372 touchedKeys = [];
373 dispatchTouchPointEvent(evt);
374 for (var i = 0; i < previous.length; ++i) {
375 if (touchedKeys.indexOf(previous[i]) == -1) {
376 previous[i].classList.remove('highlighted');
377 }
378 }
379 for (var i = 0; i < touchedKeys.length; ++i) {
380 touchedKeys[i].classList.add('highlighted');
381 }
382 }
383
384 /**
385 * Handle a touch end event on the key to make changes to the popup keyboard.
386 * @param {UIEvent} evt The UI event which triggered the touch end.
387 * @return {void}
388 */
389 function trackTouchEndForPopup(evt) {
390 for (var i = 0; i < touchedKeys.length; ++i) {
391 touchedKeys[i].classList.remove('highlighted');
392 }
393 dispatchTouchPointEvent(evt);
394 hidePopupKeyboard();
395
396 touchedKeys = [];
397 evt.target.removeEventListener('touchmove', trackTouchMoveForPopup);
398 evt.target.removeEventListener('touchend', trackTouchEndForPopup);
399 }
400
401 /**
288 * Show the popup keyboard. 402 * Show the popup keyboard.
289 * @param {string} name The name of the popup keyboard. 403 * @param {string} name The name of the popup keyboard.
404 * @return {void}
290 */ 405 */
291 function showPopupKeyboard(name) { 406 function showPopupKeyboard(name, evt) {
292 // TODO(mazda): Implement this function. 407 var popupDiv = document.getElementById('popup');
293 console.warn('Popup keyboard is not implemented yet.'); 408 if (popupDiv.style.visibility == 'visible') {
409 return;
410 }
411
412 // Reinitialize the rows of the popup keyboard
413 if (currentPopupName != name) {
414 while (popupDiv.firstChild) {
415 popupDiv.removeChild(popupDiv.firstChild);
416 }
417 if (currentPopupName in KEYBOARDS) {
418 delete KEYBOARDS[currentPopupName].rows;
419 }
420 initRows(name, popupDiv, true);
421 currentPopupName = name;
422 }
423
424 // Set the mode of the popup keyboard
425 var popupRows = KEYBOARDS[currentPopupName]['rows'];
426 for (var i = 0; i < popupRows.length; ++i) {
427 popupRows[i].showMode(currentMode);
428 }
429
430 // Calculate the size of popup keyboard based on the size of the key.
431 var keyElement = evt.currentTarget;
432 var keyboard = KEYBOARDS[name];
433 var rows = keyboard['definition'];
434 var height = keyElement.offsetHeight * rows.length;
435 var aspect = keyboard['aspect'];
436 var width = aspect * height;
437 popupDiv.style.width = width + 'px';
438 popupDiv.style.height = height + 'px';
439
440 // Place the popup keyboard above the key
441 var rect = keyElement.getBoundingClientRect();
442 var left = (rect.left + rect.right) / 2 - width / 2;
443 left = Math.min(Math.max(left, 0), window.innerWidth - width);
444 var top = rect.top - height;
445 top = Math.min(Math.max(top, 0), window.innerHeight - height);
446 popupDiv.style.left = left + 'px';
447 popupDiv.style.top = top + 'px';
448 popupDiv.style.visibility = 'visible';
449
450 keyElement.addEventListener('touchmove', trackTouchMoveForPopup);
451 keyElement.addEventListener('touchend', trackTouchEndForPopup);
294 } 452 }
295 453
296 /** 454 /**
297 * Create closure for the showPopupKeyboard function. 455 * Create closure for the showPopupKeyboard function.
298 * @param {string} name The name paramater to showPopupKeyboard. 456 * @param {string} name The name paramater to showPopupKeyboard.
299 * @return {function()} A function which calls showPopupKeyboard(name). 457 * @return {function()} A function which calls showPopupKeyboard(name).
300 */ 458 */
301 function showPopupKeyboardFunction(name) { 459 function showPopupKeyboardFunction(name) {
302 return function () { 460 return function (evt) {
303 showPopupKeyboard(name); 461 showPopupKeyboard(name, evt);
304 }; 462 };
305 } 463 }
306 464
307 /** 465 /**
466 * Hide the popup keyboard.
467 * @return {void}
468 */
469 function hidePopupKeyboard() {
470 var popupDiv = document.getElementById('popup');
471 popupDiv.style.visibility = 'hidden';
472 }
473
474 /**
308 * Plain-old-data class to represent a character. 475 * Plain-old-data class to represent a character.
309 * @param {string} display The HTML to be displayed. 476 * @param {string} display The HTML to be displayed.
310 * @param {string} id The key identifier for this Character. 477 * @param {string} id The key identifier for this Character.
311 * @constructor 478 * @constructor
312 */ 479 */
313 function Character(display, id) { 480 function Character(display, id) {
314 this.display = display; 481 this.display = display;
315 this.keyIdentifier = id; 482 this.keyIdentifier = id;
316 } 483 }
317 484
318 /** 485 /**
319 * Convenience function to make the keyboard data more readable. 486 * Convenience function to make the keyboard data more readable.
320 * @param {string} display Both the display and id for the created Character. 487 * @param {string} display The display for the created Character.
488 * @param {string} opt_id The id for the created Character.
489 * @return {Character} A character that contains display and opt_id. If
490 * opt_id is omitted, display is used as the id.
321 */ 491 */
322 function C(display) { 492 function C(display, opt_id) {
323 return new Character(display, display); 493 var id = opt_id || display;
494 return new Character(display, id);
324 } 495 }
325 496
326 /** 497 /**
498 * Convenience function to make the keyboard data more readable.
499 * @param {string} display The display for the created Character.
500 * @param {string} opt_id The id for the created Character.
501 * @param {string} opt_popupName The popup keyboard name for this character.
502 * @return {Object} An object that contains a Character and the popup keyboard
503 * name.
504 */
505 function CP(display, opt_id, opt_popupName) {
506 var result = { character: C(display, opt_id) };
507 if (opt_popupName) {
508 result['popupName'] = opt_popupName;
509 }
510 return result;
511 }
512
513 /**
327 * An abstract base-class for all keys on the keyboard. 514 * An abstract base-class for all keys on the keyboard.
328 * @constructor 515 * @constructor
329 */ 516 */
330 function BaseKey() {} 517 function BaseKey() {}
331 518
332 BaseKey.prototype = { 519 BaseKey.prototype = {
333 /** 520 /**
334 * The cell type of this key. Determines the background colour. 521 * The cell type of this key. Determines the background colour.
335 * @type {string} 522 * @type {string}
336 */ 523 */
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 * @param {string} mode The keyboard mode to create elements for. 582 * @param {string} mode The keyboard mode to create elements for.
396 * @return {Element} The top-level DOM Element for the key. 583 * @return {Element} The top-level DOM Element for the key.
397 */ 584 */
398 makeDOM: function(mode) { 585 makeDOM: function(mode) {
399 throw new Error('makeDOM not implemented in BaseKey'); 586 throw new Error('makeDOM not implemented in BaseKey');
400 }, 587 },
401 }; 588 };
402 589
403 /** 590 /**
404 * A simple key which displays Characters. 591 * A simple key which displays Characters.
592 * @param {Object} key The Character and the popup name for KEY_MODE.
593 * @param {Object} shift The Character and the popup name for SHIFT_MODE.
594 * @param {Object} num The Character and the popup name for NUMBER_MODE.
595 * @param {Object} symbol The Character and the popup name for SYMBOL_MODE.
596 * @constructor
597 * @extends {BaseKey}
598 */
599 function Key(key, shift, num, symbol) {
600 this.modeElements_ = {};
601 this.cellType_ = '';
602
603 this.modes_ = {};
604 this.modes_[KEY_MODE] = key ? key.character : null;
605 this.modes_[SHIFT_MODE] = shift ? shift.character : null;
606 this.modes_[NUMBER_MODE] = num ? num.character : null;
607 this.modes_[SYMBOL_MODE] = symbol ? symbol.character : null;
608
609 this.popupNames_ = {};
610 this.popupNames_[KEY_MODE] = key ? key.popupName : null;
611 this.popupNames_[SHIFT_MODE] = shift ? shift.popupName : null;
612 this.popupNames_[NUMBER_MODE] = num ? num.popupName : null;
613 this.popupNames_[SYMBOL_MODE] = symbol ? symbol.popupName : null;
614 }
615
616 Key.prototype = {
617 __proto__: BaseKey.prototype,
618
619 /** @inheritDoc */
620 makeDOM: function(mode) {
621 if (!this.modes_[mode]) {
622 return null;
623 }
624
625 this.modeElements_[mode] = document.createElement('div');
626 var element = this.modeElements_[mode];
627 element.className = 'key';
628
629 addContent(element, this.modes_[mode].display);
630
631 var longHandler = this.popupNames_[mode] ?
632 showPopupKeyboardFunction(this.popupNames_[mode]) : null;
633 setupKeyEventHandlers(this, element,
634 { 'up': sendKeyFunction(this.modes_[mode].keyIdentifier),
635 'long': longHandler });
636 return element;
637 }
638 };
639
640 /**
641 * A simple key which displays Characters on the popup keyboard.
405 * @param {Character} key The Character for KEY_MODE. 642 * @param {Character} key The Character for KEY_MODE.
406 * @param {Character} shift The Character for SHIFT_MODE. 643 * @param {Character} shift The Character for SHIFT_MODE.
407 * @param {Character} num The Character for NUMBER_MODE. 644 * @param {Character} num The Character for NUMBER_MODE.
408 * @param {Character} symbol The Character for SYMBOL_MODE. 645 * @param {Character} symbol The Character for SYMBOL_MODE.
409 * @constructor 646 * @constructor
410 * @extends {BaseKey} 647 * @extends {BaseKey}
411 */ 648 */
412 function Key(key, shift, num, symbol) { 649 function PopupKey(key, shift, num, symbol) {
413 this.modeElements_ = {}; 650 this.modeElements_ = {};
414 this.cellType_ = ''; 651 this.cellType_ = '';
415 652
416 this.modes_ = {}; 653 this.modes_ = {};
417 this.modes_[KEY_MODE] = key; 654 this.modes_[KEY_MODE] = key;
418 this.modes_[SHIFT_MODE] = shift; 655 this.modes_[SHIFT_MODE] = shift;
419 this.modes_[NUMBER_MODE] = num; 656 this.modes_[NUMBER_MODE] = num;
420 this.modes_[SYMBOL_MODE] = symbol; 657 this.modes_[SYMBOL_MODE] = symbol;
421 } 658 }
422 659
423 Key.prototype = { 660 PopupKey.prototype = {
424 __proto__: BaseKey.prototype, 661 __proto__: BaseKey.prototype,
425 662
426 /** @inheritDoc */ 663 /** @inheritDoc */
427 makeDOM: function(mode) { 664 makeDOM: function(mode) {
428 if (!this.modes_[mode]) { 665 if (!this.modes_[mode]) {
429 return null; 666 return null;
430 } 667 }
431 668
432 this.modeElements_[mode] = document.createElement('div'); 669 this.modeElements_[mode] = document.createElement('div');
433 this.modeElements_[mode].className = 'key'; 670 var element = this.modeElements_[mode];
434 addContent(this.modeElements_[mode], this.modes_[mode].display); 671 element.className = 'key popupkey';
435 672
436 // TODO(mazda): Set the long-press handler only if the key has characters 673 addContent(element, this.modes_[mode].display);
437 // to show on the popup keyboard.
438 setupKeyEventHandlers(this, this.modeElements_[mode],
439 { 'up': sendKeyFunction(this.modes_[mode].keyIdentifier),
440 'long': showPopupKeyboardFunction('') });
441 674
442 return this.modeElements_[mode]; 675 var upHandler = sendKeyFunction(this.modes_[mode].keyIdentifier);
676 element.addEventListener('touchpointmove', function(evt) {
677 touchedKeys.push(element);
678 });
679 element.addEventListener('touchpointend', upHandler);
680 element.addEventListener('mouseup', upHandler);
681 element.addEventListener('mouseover', function(evt) {
682 element.classList.add('highlighted');
683 });
684 element.addEventListener('mouseout', function(evt) {
685 element.classList.remove('highlighted');
686 });
687 return element;
443 } 688 }
444 }; 689 };
445 690
446 /** 691 /**
447 * A key which displays an SVG image. 692 * A key which displays an SVG image.
448 * @param {string} className The class that provides the image. 693 * @param {string} className The class that provides the image.
449 * @param {string} keyId The key identifier for the key. 694 * @param {string} keyId The key identifier for the key.
450 * @param {boolean} opt_repeat True if the key should repeat. 695 * @param {boolean} opt_repeat True if the key should repeat.
451 * @constructor 696 * @constructor
452 * @extends {BaseKey} 697 * @extends {BaseKey}
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 * Shows the given mode. 953 * Shows the given mode.
709 * @param {string} mode The mode to show. 954 * @param {string} mode The mode to show.
710 * @return {void} 955 * @return {void}
711 */ 956 */
712 showMode: function(mode) { 957 showMode: function(mode) {
713 for (var i = 0; i < MODES.length; ++i) { 958 for (var i = 0; i < MODES.length; ++i) {
714 this.modeElements_[MODES[i]].style.display = 'none'; 959 this.modeElements_[MODES[i]].style.display = 'none';
715 } 960 }
716 this.modeElements_[mode].style.display = '-webkit-box'; 961 this.modeElements_[mode].style.display = '-webkit-box';
717 }, 962 },
963
964 /**
965 * Returns the size of keys this row contains.
966 * @return {number} The size of keys.
967 */
968 get length() {
969 return this.keys_.length;
970 }
718 }; 971 };
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