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

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: 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.
bryeung 2011/09/08 19:22:35 Where is the event being modified? Also, can we c
mazda 2011/09/12 14:33:48 I'm not sure where it is modified, but evt.current
bryeung 2011/09/13 23:53:04 Sorry. I think my use of the word "keys" was a po
mazda 2011/09/16 13:16:37 Yes, that's fine. But if we use keyLongHandler for
bryeung 2011/09/19 15:05:09 I don't see that change in this patch. Do you nee
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) {
bryeung 2011/09/08 19:22:35 Why is this needed now?
mazda 2011/09/12 14:33:48 This kind of check should have been in the CL that
bryeung 2011/09/13 23:53:04 Thank you for the explanation. I think maybe we s
mazda 2011/09/16 13:16:37 Added a comment.
299 return;
300 }
255 // Reset key press state if the point goes out of the element. 301 // Reset key press state if the point goes out of the element.
256 key.pressed = false; 302 key.pressed = false;
257 // Reset long-press timer. 303 // Reset long-press timer.
258 if (key.longPressTimer) { 304 if (key.longPressTimer) {
259 clearTimeout(key.longPressTimer); 305 clearTimeout(key.longPressTimer);
260 delete key.longPressTimer 306 delete key.longPressTimer
261 } 307 }
262 } 308 }
263 309
264 // Setup mouse event handlers. 310 // Setup mouse event handlers.
(...skipping 13 matching lines...) Expand all
278 * @param {string} key The key paramater to sendKey. 324 * @param {string} key The key paramater to sendKey.
279 * @return {function()} A function which calls sendKey(key). 325 * @return {function()} A function which calls sendKey(key).
280 */ 326 */
281 function sendKeyFunction(key) { 327 function sendKeyFunction(key) {
282 return function() { 328 return function() {
283 sendKey(key); 329 sendKey(key);
284 }; 330 };
285 } 331 }
286 332
287 /** 333 /**
334 * Dispatch custom events to the elements at the touch points.
bryeung 2011/09/08 19:22:35 It's not clear to me why we need to make custom ev
mazda 2011/09/12 14:33:48 touchmove and touchend events are not sent to the
bryeung 2011/09/13 23:53:04 Is there a reason why we can't simulate regular ev
mazda 2011/09/16 13:16:37 touchmove and touchend events are sent to the elem
335 * touchpointmove events are dispatched responding to a touchmove and
336 * touchpointend events responding to a touchend event respectively.
337 * @param {UIEvent} evt The touch event that contains touch points information.
338 * @return {void}
339 */
340 function dispatchTouchPointEvent(evt) {
341 var type = null;
342 var touches = null;
343 if (evt.type == 'touchmove') {
344 type = 'touchpointmove';
345 touches = evt.touches;
346 } else if (evt.type == 'touchend') {
347 type = 'touchpointend';
348 touches = evt.changedTouches;
349 } else {
350 return;
351 }
352
353 for (var i = 0; i < touches.length; ++i) {
354 var dispatchedEvent = document.createEvent('Event');
355 dispatchedEvent.initEvent(type, true, false);
356 var touch = touches[i];
357 var key = document.elementFromPoint(touch.screenX, touch.screenY);
358 if (key) {
359 key.dispatchEvent(dispatchedEvent);
360 }
361 }
362 }
363
364 /**
365 * Handle a touch move event on the key to make changes to the popup keyboard.
366 * @param {UIEvent} evt The UI event which triggered the touch move.
367 * @return {void}
368 */
369 function trackTouchMoveForPopup(evt) {
370 var previous = touchedKeys;
371 touchedKeys = [];
372 dispatchTouchPointEvent(evt);
373 for (var i = 0; i < previous.length; ++i) {
374 if (touchedKeys.indexOf(previous[i]) == -1) {
375 previous[i].classList.remove('highlighted');
376 }
377 }
378 for (var i = 0; i < touchedKeys.length; ++i) {
379 touchedKeys[i].classList.add('highlighted');
380 }
381 }
382
383 /**
384 * Handle a touch end event on the key to make changes to the popup keyboard.
385 * @param {UIEvent} evt The UI event which triggered the touch end.
386 * @return {void}
387 */
388 function trackTouchEndForPopup(evt) {
389 for (var i = 0; i < touchedKeys.length; ++i) {
390 touchedKeys[i].classList.remove('highlighted');
391 }
392 dispatchTouchPointEvent(evt);
393 hidePopupKeyboard();
394
395 touchedKeys = [];
396 evt.target.removeEventListener('touchmove', trackTouchMoveForPopup);
397 evt.target.removeEventListener('touchend', trackTouchEndForPopup);
398 }
399
400 /**
288 * Show the popup keyboard. 401 * Show the popup keyboard.
289 * @param {string} name The name of the popup keyboard. 402 * @param {string} name The name of the popup keyboard.
403 * @return {void}
290 */ 404 */
291 function showPopupKeyboard(name) { 405 function showPopupKeyboard(name, evt) {
292 // TODO(mazda): Implement this function. 406 var popupDiv = document.getElementById('popup');
293 console.warn('Popup keyboard is not implemented yet.'); 407 if (popupDiv.style.visibility == 'visible') {
408 return;
409 }
410
411 // Reinitialize the rows of the popup keyboard
412 if (currentPopupName != name) {
413 while (popupDiv.firstChild) {
414 popupDiv.removeChild(popupDiv.firstChild);
415 }
416 if (currentPopupName in KEYBOARDS) {
417 delete KEYBOARDS[currentPopupName].rows;
418 }
419 initRows(name, popupDiv, true);
420 currentPopupName = name;
421 }
422
423 // Set the mode of the popup keyboard
424 var popupRows = KEYBOARDS[currentPopupName]['rows'];
425 for (var i = 0; i < popupRows.length; ++i) {
426 popupRows[i].showMode(currentMode);
427 }
428
429 // Calculate the size of popup keyboard based on the size of the key.
430 var keyElement = evt.currentTarget;
431 var keyboard = KEYBOARDS[name];
432 var rows = keyboard['definition'];
433 var height = keyElement.offsetHeight * rows.length;
434 var aspect = keyboard['aspect'];
435 var width = aspect * height;
436 popupDiv.style.width = width + 'px';
437 popupDiv.style.height = height + 'px';
438
439 // Place the popup keyboard above the key
440 var rect = keyElement.getBoundingClientRect();
441 var left = (rect.left + rect.right) / 2 - width / 2;
442 left = Math.min(Math.max(left, 0), window.innerWidth - width);
443 var top = rect.top - height;
444 top = Math.min(Math.max(top, 0), window.innerHeight - height);
445 popupDiv.style.left = left + 'px';
446 popupDiv.style.top = top + 'px';
447 popupDiv.style.visibility = 'visible';
448
449 keyElement.addEventListener('touchmove', trackTouchMoveForPopup);
450 keyElement.addEventListener('touchend', trackTouchEndForPopup);
294 } 451 }
295 452
296 /** 453 /**
297 * Create closure for the showPopupKeyboard function. 454 * Create closure for the showPopupKeyboard function.
298 * @param {string} name The name paramater to showPopupKeyboard. 455 * @param {string} name The name paramater to showPopupKeyboard.
299 * @return {function()} A function which calls showPopupKeyboard(name). 456 * @return {function()} A function which calls showPopupKeyboard(name).
300 */ 457 */
301 function showPopupKeyboardFunction(name) { 458 function showPopupKeyboardFunction(name) {
302 return function () { 459 return function (evt) {
303 showPopupKeyboard(name); 460 showPopupKeyboard(name, evt);
304 }; 461 };
305 } 462 }
306 463
307 /** 464 /**
465 * Hide the popup keyboard.
466 * @return {void}
467 */
468 function hidePopupKeyboard() {
469 var popupDiv = document.getElementById('popup');
470 popupDiv.style.visibility = 'hidden';
471 }
472
473 /**
308 * Plain-old-data class to represent a character. 474 * Plain-old-data class to represent a character.
309 * @param {string} display The HTML to be displayed. 475 * @param {string} display The HTML to be displayed.
310 * @param {string} id The key identifier for this Character. 476 * @param {string} id The key identifier for this Character.
311 * @constructor 477 * @constructor
312 */ 478 */
313 function Character(display, id) { 479 function Character(display, id) {
314 this.display = display; 480 this.display = display;
315 this.keyIdentifier = id; 481 this.keyIdentifier = id;
316 } 482 }
317 483
318 /** 484 /**
319 * Convenience function to make the keyboard data more readable. 485 * Convenience function to make the keyboard data more readable.
320 * @param {string} display Both the display and id for the created Character. 486 * If |opt_id| is omitted, |display| is used as the id.
487 * @param {string} display The display for the created Character.
488 * @param {string} opt_id The id for the created Character.
489 * @param {string} opt_popupName The popup keyboard name for this character.
490 * @return {Object} An object that contains a Character and the popup keyboard
491 * name.
321 */ 492 */
322 function C(display) { 493 function C(display, opt_id, opt_popupName) {
323 return new Character(display, display); 494 var id = opt_id || display;
495 var result = { character: new Character(display, id)};
496 if (opt_popupName) {
497 result['popupName'] = opt_popupName;
498 }
499 return result;
324 } 500 }
325 501
326 /** 502 /**
327 * An abstract base-class for all keys on the keyboard. 503 * An abstract base-class for all keys on the keyboard.
328 * @constructor 504 * @constructor
329 */ 505 */
330 function BaseKey() {} 506 function BaseKey() {}
331 507
332 BaseKey.prototype = { 508 BaseKey.prototype = {
333 /** 509 /**
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 578
403 /** 579 /**
404 * A simple key which displays Characters. 580 * A simple key which displays Characters.
405 * @param {Character} key The Character for KEY_MODE. 581 * @param {Character} key The Character for KEY_MODE.
406 * @param {Character} shift The Character for SHIFT_MODE. 582 * @param {Character} shift The Character for SHIFT_MODE.
407 * @param {Character} num The Character for NUMBER_MODE. 583 * @param {Character} num The Character for NUMBER_MODE.
408 * @param {Character} symbol The Character for SYMBOL_MODE. 584 * @param {Character} symbol The Character for SYMBOL_MODE.
409 * @constructor 585 * @constructor
410 * @extends {BaseKey} 586 * @extends {BaseKey}
411 */ 587 */
412 function Key(key, shift, num, symbol) { 588 function Key(key, shift, num, symbol, opt_className) {
413 this.modeElements_ = {}; 589 this.modeElements_ = {};
414 this.cellType_ = ''; 590 this.cellType_ = '';
591 this.className_ = opt_className || "";
415 592
416 this.modes_ = {}; 593 this.modes_ = {};
417 this.modes_[KEY_MODE] = key; 594 this.modes_[KEY_MODE] = key ? key.character : null;
418 this.modes_[SHIFT_MODE] = shift; 595 this.modes_[SHIFT_MODE] = shift ? shift.character : null;
419 this.modes_[NUMBER_MODE] = num; 596 this.modes_[NUMBER_MODE] = num ? num.character : null;
420 this.modes_[SYMBOL_MODE] = symbol; 597 this.modes_[SYMBOL_MODE] = symbol ? symbol.character : null;
598
599 this.popupNames_ = {};
600 this.popupNames_[KEY_MODE] = key ? key.popupName : null;
601 this.popupNames_[SHIFT_MODE] = shift ? shift.popupName : null;
602 this.popupNames_[NUMBER_MODE] = num ? num.popupName : null;
603 this.popupNames_[SYMBOL_MODE] = symbol ? symbol.popupName : null;
421 } 604 }
422 605
423 Key.prototype = { 606 Key.prototype = {
424 __proto__: BaseKey.prototype, 607 __proto__: BaseKey.prototype,
425 608
426 /** @inheritDoc */ 609 /** @inheritDoc */
427 makeDOM: function(mode) { 610 makeDOM: function(mode) {
428 if (!this.modes_[mode]) { 611 if (!this.modes_[mode]) {
429 return null; 612 return null;
430 } 613 }
431 614
432 this.modeElements_[mode] = document.createElement('div'); 615 this.modeElements_[mode] = document.createElement('div');
433 this.modeElements_[mode].className = 'key'; 616 var element = this.modeElements_[mode];
434 addContent(this.modeElements_[mode], this.modes_[mode].display); 617 element.className = 'key';
618 if (this.className_) {
619 element.classList.add(this.className_);
620 }
435 621
436 // TODO(mazda): Set the long-press handler only if the key has characters 622 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 623
442 return this.modeElements_[mode]; 624 var upHandler = sendKeyFunction(this.modes_[mode].keyIdentifier);
625 if (this.className_ == 'popupkey') {
bryeung 2011/09/08 19:22:35 I don't like giving the class name this semantic v
mazda 2011/09/12 14:33:48 popupNames are added for the keys that invokes the
bryeung 2011/09/13 23:53:04 I like this much better. Thank you.
626 // Add handlers for keys on the popup keyboard
627 element.addEventListener('touchpointmove', function(evt) {
628 touchedKeys.push(element);
629 });
630 element.addEventListener('touchpointend', upHandler);
631 element.addEventListener('mouseup', upHandler);
632 element.addEventListener('mouseover', function(evt) {
633 element.classList.add('highlighted');
634 });
635 element.addEventListener('mouseout', function(evt) {
636 element.classList.remove('highlighted');
637 });
638 } else {
639 var longHandler = this.popupNames_[mode] ?
640 showPopupKeyboardFunction(this.popupNames_[mode]) : null;
641 setupKeyEventHandlers(this, element,
642 { 'up': upHandler,
643 'long': longHandler });
644 }
645 return element;
443 } 646 }
444 }; 647 };
445 648
446 /** 649 /**
447 * A key which displays an SVG image. 650 * A key which displays an SVG image.
448 * @param {string} className The class that provides the image. 651 * @param {string} className The class that provides the image.
449 * @param {string} keyId The key identifier for the key. 652 * @param {string} keyId The key identifier for the key.
450 * @param {boolean} opt_repeat True if the key should repeat. 653 * @param {boolean} opt_repeat True if the key should repeat.
451 * @constructor 654 * @constructor
452 * @extends {BaseKey} 655 * @extends {BaseKey}
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 * Shows the given mode. 911 * Shows the given mode.
709 * @param {string} mode The mode to show. 912 * @param {string} mode The mode to show.
710 * @return {void} 913 * @return {void}
711 */ 914 */
712 showMode: function(mode) { 915 showMode: function(mode) {
713 for (var i = 0; i < MODES.length; ++i) { 916 for (var i = 0; i < MODES.length; ++i) {
714 this.modeElements_[MODES[i]].style.display = 'none'; 917 this.modeElements_[MODES[i]].style.display = 'none';
715 } 918 }
716 this.modeElements_[mode].style.display = '-webkit-box'; 919 this.modeElements_[mode].style.display = '-webkit-box';
717 }, 920 },
921
922 /**
923 * Returns the size of keys this row contains.
924 * @return {number} The size of keys.
925 */
926 get length() {
927 return this.keys_.length;
928 }
718 }; 929 };
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/keyboard/index.html » ('j') | chrome/browser/resources/keyboard/layout_fr.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698