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

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: address comments 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
« no previous file with comments | « no previous file | chrome/browser/resources/keyboard/layout_fr.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 evtCopy = {};
260 evtCopy.currentTarget = evt.currentTarget;
219 key.longPressTimer = setTimeout(function() { 261 key.longPressTimer = setTimeout(function() {
220 keyLongHandler(evt), 262 keyLongHandler(evtCopy),
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 * touchmove_popup events are dispatched responding to a touchmove and
350 * touchend_popup 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 dispatchCustomPopupEvents(evt) {
355 var type = null;
356 var touches = null;
357 if (evt.type == 'touchmove') {
358 type = 'touchmove_popup';
359 touches = evt.touches;
360 } else if (evt.type == 'touchend') {
361 type = 'touchend_popup';
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 dispatchCustomPopupEvents(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 dispatchCustomPopupEvents(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.
417 * @param {UIEvent} evt The UI event which triggered the touch start.
418 * @return {void}
294 */ 419 */
295 function showPopupKeyboard(name) { 420 function showPopupKeyboard(name, evt) {
296 // TODO(mazda): Implement this function. 421 var popupDiv = document.getElementById('popup');
297 console.warn('Popup keyboard is not implemented yet.'); 422 if (popupDiv.style.visibility == 'visible') {
423 return;
424 }
425
426 // Iitialize the rows of the popup keyboard
427 initRows(name, popupDiv, true);
428 currentPopupName = name;
429
430 // Set the mode of the popup keyboard
431 var popupRows = KEYBOARDS[name]['rows'];
432 for (var i = 0; i < popupRows.length; ++i) {
433 popupRows[i].showMode(currentMode);
434 }
435
436 // Calculate the size of popup keyboard based on the size of the key.
437 var keyElement = evt.currentTarget;
438 var keyboard = KEYBOARDS[name];
439 var rows = keyboard['definition'];
440 var height = keyElement.offsetHeight * rows.length;
441 var aspect = keyboard['aspect'];
442 var width = aspect * height;
443 popupDiv.style.width = width + 'px';
444 popupDiv.style.height = height + 'px';
445
446 // Place the popup keyboard above the key
447 var rect = keyElement.getBoundingClientRect();
448 var left = (rect.left + rect.right) / 2 - width / 2;
449 left = Math.min(Math.max(left, 0), window.innerWidth - width);
450 var top = rect.top - height;
451 top = Math.min(Math.max(top, 0), window.innerHeight - height);
452 popupDiv.style.left = left + 'px';
453 popupDiv.style.top = top + 'px';
454 popupDiv.style.visibility = 'visible';
455
456 keyElement.addEventListener('touchmove', trackTouchMoveForPopup);
457 keyElement.addEventListener('touchend', trackTouchEndForPopup);
298 } 458 }
299 459
300 /** 460 /**
301 * Create closure for the showPopupKeyboard function. 461 * Create closure for the showPopupKeyboard function.
302 * @param {string} name The name paramater to showPopupKeyboard. 462 * @param {string} name The name paramater to showPopupKeyboard.
303 * @return {function()} A function which calls showPopupKeyboard(name). 463 * @return {function()} A function which calls showPopupKeyboard(name, evt).
304 */ 464 */
305 function showPopupKeyboardFunction(name) { 465 function showPopupKeyboardFunction(name) {
306 return function () { 466 return function (evt) {
307 showPopupKeyboard(name); 467 showPopupKeyboard(name, evt);
308 }; 468 };
309 } 469 }
310 470
311 /** 471 /**
472 * Hide the popup keyboard.
473 * @return {void}
474 */
475 function hidePopupKeyboard() {
476 // Clean up the popup keyboard
477 var popupDiv = document.getElementById('popup');
478 popupDiv.style.visibility = 'hidden';
479 while (popupDiv.firstChild) {
480 popupDiv.removeChild(popupDiv.firstChild);
481 }
482 if (currentPopupName in KEYBOARDS) {
483 delete KEYBOARDS[currentPopupName].rows;
484 }
485 currentPopupName = '';
486 }
487
488 /**
312 * Plain-old-data class to represent a character. 489 * Plain-old-data class to represent a character.
313 * @param {string} display The HTML to be displayed. 490 * @param {string} display The HTML to be displayed.
314 * @param {string} id The key identifier for this Character. 491 * @param {string} id The key identifier for this Character.
315 * @constructor 492 * @constructor
316 */ 493 */
317 function Character(display, id) { 494 function Character(display, id) {
318 this.display = display; 495 this.display = display;
319 this.keyIdentifier = id; 496 this.keyIdentifier = id;
320 } 497 }
321 498
322 /** 499 /**
323 * Convenience function to make the keyboard data more readable. 500 * Convenience function to make the keyboard data more readable.
324 * @param {string} display Both the display and id for the created Character. 501 * @param {string} display The display for the created Character.
502 * @param {string} opt_id The id for the created Character.
503 * @return {Character} A character that contains display and opt_id. If
504 * opt_id is omitted, display is used as the id.
325 */ 505 */
326 function C(display) { 506 function C(display, opt_id) {
327 return new Character(display, display); 507 var id = opt_id || display;
508 return new Character(display, id);
328 } 509 }
329 510
330 /** 511 /**
512 * Convenience function to make the keyboard data more readable.
513 * @param {string} display The display for the created Character.
514 * @param {string} opt_id The id for the created Character.
515 * @param {string} opt_popupName The popup keyboard name for this character.
516 * @return {Object} An object that contains a Character and the popup keyboard
517 * name.
518 */
519 function CP(display, opt_id, opt_popupName) {
520 var result = { character: C(display, opt_id) };
521 if (opt_popupName) {
522 result['popupName'] = opt_popupName;
523 }
524 return result;
525 }
526
527 /**
331 * An abstract base-class for all keys on the keyboard. 528 * An abstract base-class for all keys on the keyboard.
332 * @constructor 529 * @constructor
333 */ 530 */
334 function BaseKey() {} 531 function BaseKey() {}
335 532
336 BaseKey.prototype = { 533 BaseKey.prototype = {
337 /** 534 /**
338 * The cell type of this key. Determines the background colour. 535 * The cell type of this key. Determines the background colour.
339 * @type {string} 536 * @type {string}
340 */ 537 */
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 * @param {string} mode The keyboard mode to create elements for. 586 * @param {string} mode The keyboard mode to create elements for.
390 * @return {Element} The top-level DOM Element for the key. 587 * @return {Element} The top-level DOM Element for the key.
391 */ 588 */
392 makeDOM: function(mode) { 589 makeDOM: function(mode) {
393 throw new Error('makeDOM not implemented in BaseKey'); 590 throw new Error('makeDOM not implemented in BaseKey');
394 }, 591 },
395 }; 592 };
396 593
397 /** 594 /**
398 * A simple key which displays Characters. 595 * A simple key which displays Characters.
596 * @param {Object} key The Character and the popup name for KEY_MODE.
597 * @param {Object} shift The Character and the popup name for SHIFT_MODE.
598 * @param {Object} num The Character and the popup name for NUMBER_MODE.
599 * @param {Object} symbol The Character and the popup name for SYMBOL_MODE.
600 * @constructor
601 * @extends {BaseKey}
602 */
603 function Key(key, shift, num, symbol) {
604 this.modeElements_ = {};
605 this.cellType_ = '';
606
607 this.modes_ = {};
608 this.modes_[KEY_MODE] = key ? key.character : null;
609 this.modes_[SHIFT_MODE] = shift ? shift.character : null;
610 this.modes_[NUMBER_MODE] = num ? num.character : null;
611 this.modes_[SYMBOL_MODE] = symbol ? symbol.character : null;
612
613 this.popupNames_ = {};
614 this.popupNames_[KEY_MODE] = key ? key.popupName : null;
615 this.popupNames_[SHIFT_MODE] = shift ? shift.popupName : null;
616 this.popupNames_[NUMBER_MODE] = num ? num.popupName : null;
617 this.popupNames_[SYMBOL_MODE] = symbol ? symbol.popupName : null;
618 }
619
620 Key.prototype = {
621 __proto__: BaseKey.prototype,
622
623 /** @inheritDoc */
624 makeDOM: function(mode) {
625 if (!this.modes_[mode]) {
626 return null;
627 }
628
629 this.modeElements_[mode] = document.createElement('div');
630 var element = this.modeElements_[mode];
631 element.className = 'key';
632
633 addContent(element, this.modes_[mode].display);
634
635 var longHandler = this.popupNames_[mode] ?
636 showPopupKeyboardFunction(this.popupNames_[mode]) : null;
637 setupKeyEventHandlers(this, element,
638 { 'up': sendKeyFunction(this.modes_[mode].keyIdentifier),
639 'long': longHandler });
640 return element;
641 }
642 };
643
644 /**
645 * A simple key which displays Characters on the popup keyboard.
399 * @param {Character} key The Character for KEY_MODE. 646 * @param {Character} key The Character for KEY_MODE.
400 * @param {Character} shift The Character for SHIFT_MODE. 647 * @param {Character} shift The Character for SHIFT_MODE.
401 * @param {Character} num The Character for NUMBER_MODE. 648 * @param {Character} num The Character for NUMBER_MODE.
402 * @param {Character} symbol The Character for SYMBOL_MODE. 649 * @param {Character} symbol The Character for SYMBOL_MODE.
403 * @constructor 650 * @constructor
404 * @extends {BaseKey} 651 * @extends {BaseKey}
405 */ 652 */
406 function Key(key, shift, num, symbol) { 653 function PopupKey(key, shift, num, symbol) {
407 this.modeElements_ = {}; 654 this.modeElements_ = {};
408 this.cellType_ = ''; 655 this.cellType_ = '';
409 656
410 this.modes_ = {}; 657 this.modes_ = {};
411 this.modes_[KEY_MODE] = key; 658 this.modes_[KEY_MODE] = key;
412 this.modes_[SHIFT_MODE] = shift; 659 this.modes_[SHIFT_MODE] = shift;
413 this.modes_[NUMBER_MODE] = num; 660 this.modes_[NUMBER_MODE] = num;
414 this.modes_[SYMBOL_MODE] = symbol; 661 this.modes_[SYMBOL_MODE] = symbol;
415 } 662 }
416 663
417 Key.prototype = { 664 PopupKey.prototype = {
418 __proto__: BaseKey.prototype, 665 __proto__: BaseKey.prototype,
419 666
420 /** @inheritDoc */ 667 /** @inheritDoc */
421 makeDOM: function(mode) { 668 makeDOM: function(mode) {
422 if (!this.modes_[mode]) { 669 if (!this.modes_[mode]) {
423 return null; 670 return null;
424 } 671 }
425 672
426 this.modeElements_[mode] = document.createElement('div'); 673 this.modeElements_[mode] = document.createElement('div');
427 this.modeElements_[mode].className = 'key'; 674 var element = this.modeElements_[mode];
428 addContent(this.modeElements_[mode], this.modes_[mode].display); 675 element.className = 'key popupkey';
429 676
430 // TODO(mazda): Set the long-press handler only if the key has characters 677 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 678
436 return this.modeElements_[mode]; 679 var upHandler = sendKeyFunction(this.modes_[mode].keyIdentifier);
680 element.addEventListener('touchmove_popup', function(evt) {
681 touchedKeys.push(element);
682 });
683 element.addEventListener('touchend_popup', upHandler);
684 element.addEventListener('mouseup', upHandler);
685 element.addEventListener('mouseover', function(evt) {
686 element.classList.add('highlighted');
687 });
688 element.addEventListener('mouseout', function(evt) {
689 element.classList.remove('highlighted');
690 });
691 return element;
437 } 692 }
438 }; 693 };
439 694
440 /** 695 /**
441 * A key which displays an SVG image. 696 * A key which displays an SVG image.
442 * @param {string} className The class that provides the image. 697 * @param {string} className The class that provides the image.
443 * @param {string} keyId The key identifier for the key. 698 * @param {string} keyId The key identifier for the key.
444 * @param {boolean} opt_repeat True if the key should repeat. 699 * @param {boolean} opt_repeat True if the key should repeat.
445 * @constructor 700 * @constructor
446 * @extends {BaseKey} 701 * @extends {BaseKey}
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 * Shows the given mode. 953 * Shows the given mode.
699 * @param {string} mode The mode to show. 954 * @param {string} mode The mode to show.
700 * @return {void} 955 * @return {void}
701 */ 956 */
702 showMode: function(mode) { 957 showMode: function(mode) {
703 for (var i = 0; i < MODES.length; ++i) { 958 for (var i = 0; i < MODES.length; ++i) {
704 this.modeElements_[MODES[i]].style.display = 'none'; 959 this.modeElements_[MODES[i]].style.display = 'none';
705 } 960 }
706 this.modeElements_[mode].style.display = '-webkit-box'; 961 this.modeElements_[mode].style.display = '-webkit-box';
707 }, 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 }
708 }; 971 };
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/keyboard/layout_fr.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698