OLD | NEW |
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 /** | 9 /** |
10 * The keyboard layout name currently in use. | |
11 * @type {string} | |
12 */ | |
13 var currentKeyboardLayout = 'us'; | |
14 | |
15 /** | |
16 * The ratio of the row height to the font size. | 10 * The ratio of the row height to the font size. |
17 * @type {number} | 11 * @type {number} |
18 */ | 12 */ |
19 const kFontSizeRatio = 3.5; | 13 const kFontSizeRatio = 3.5; |
20 | 14 |
21 /** | 15 /** |
22 * Return the id attribute of the keyboard element for the given layout. | 16 * Return the id attribute of the keyboard element for the given layout. |
23 * @param {string} layout The keyboard layout. | 17 * @param {string} layout The keyboard layout. |
24 * @return {string} The id attribute of the keyboard element. | 18 * @return {string} The id attribute of the keyboard element. |
25 */ | 19 */ |
(...skipping 15 matching lines...) Expand all Loading... |
41 * @return {number} The height of the keyboard in pixels. | 35 * @return {number} The height of the keyboard in pixels. |
42 */ | 36 */ |
43 function getKeyboardHeight() { | 37 function getKeyboardHeight() { |
44 var x = window.innerWidth; | 38 var x = window.innerWidth; |
45 var y = window.innerHeight - ((imeui && imeui.visible) ? IME_HEIGHT : 0); | 39 var y = window.innerHeight - ((imeui && imeui.visible) ? IME_HEIGHT : 0); |
46 return (x > getKeyboardAspect() * y) ? | 40 return (x > getKeyboardAspect() * y) ? |
47 y : Math.floor(x / getKeyboardAspect()); | 41 y : Math.floor(x / getKeyboardAspect()); |
48 } | 42 } |
49 | 43 |
50 /** | 44 /** |
51 * Set the keyboard mode. | |
52 * @param {string} mode The new mode. | |
53 * @return {void} | |
54 */ | |
55 function setMode(mode) { | |
56 var rows = KEYBOARDS[currentKeyboardLayout]['rows']; | |
57 for (var i = 0; i < rows.length; ++i) { | |
58 rows[i].showMode(mode); | |
59 } | |
60 } | |
61 | |
62 /** | |
63 * Create a DOM of the keyboard rows for the given keyboard layout. | 45 * Create a DOM of the keyboard rows for the given keyboard layout. |
64 * Do nothing if the DOM is already created. | 46 * Do nothing if the DOM is already created. |
65 * @param {string} layout The keyboard layout for which rows are created. | 47 * @param {string} layout The keyboard layout for which rows are created. |
66 * @param {Element} The DOM Element to which rows are appended. | 48 * @param {Element} element The DOM Element to which rows are appended. |
| 49 * @param {boolean} autoPadding True if padding needs to be added to both side |
| 50 * of the rows that have less keys. |
67 * @return {void} | 51 * @return {void} |
68 */ | 52 */ |
69 function initRows(layout, element) { | 53 function initRows(layout, element, autoPadding) { |
70 var keyboard = KEYBOARDS[layout]; | 54 var keyboard = KEYBOARDS[layout]; |
71 if ('rows' in keyboard) { | 55 if ('rows' in keyboard) { |
72 return; | 56 return; |
73 } | 57 } |
74 var def = keyboard['definition']; | 58 var def = keyboard['definition']; |
75 var rows = []; | 59 var rows = []; |
76 for (var i = 0; i < def.length; ++i) { | 60 for (var i = 0; i < def.length; ++i) { |
77 rows.push(new Row(i, def[i])); | 61 rows.push(new Row(i, def[i])); |
78 } | 62 } |
79 keyboard['rows'] = rows; | 63 keyboard['rows'] = rows; |
80 | 64 |
| 65 var maxRowLength = -1; |
| 66 for (var i = 0; i < rows.length; ++i) { |
| 67 if (rows[i].length > maxRowLength) { |
| 68 maxRowLength = rows[i].length; |
| 69 } |
| 70 } |
| 71 |
81 // A div element which holds rows for the layout. | 72 // A div element which holds rows for the layout. |
82 var rowsDiv = document.createElement('div'); | 73 var rowsDiv = document.createElement('div'); |
83 rowsDiv.className = 'rows'; | 74 rowsDiv.className = 'rows'; |
84 for (var i = 0; i < rows.length; ++i) { | 75 for (var i = 0; i < rows.length; ++i) { |
85 rowsDiv.appendChild(rows[i].makeDOM()); | 76 var rowDiv = rows[i].makeDOM(); |
| 77 if (autoPadding && rows[i].length < maxRowLength) { |
| 78 var padding = 50 * (maxRowLength - rows[i].length) / maxRowLength; |
| 79 rowDiv.style.paddingLeft = padding + '%'; |
| 80 rowDiv.style.paddingRight = padding + '%'; |
| 81 } |
| 82 rowsDiv.appendChild(rowDiv); |
86 rows[i].showMode(currentMode); | 83 rows[i].showMode(currentMode); |
87 } | 84 } |
88 keyboard['rowsDiv'] = rowsDiv; | 85 keyboard['rowsDiv'] = rowsDiv; |
89 element.appendChild(rowsDiv); | 86 element.appendChild(rowsDiv); |
90 } | 87 } |
91 | 88 |
92 /** | 89 /** |
93 * Create a DOM of the handwriting canvas for the given keyboard layout. | 90 * Create a DOM of the handwriting canvas for the given keyboard layout. |
94 * Do nothing if the DOM is already created or the layout doesn't have canvas. | 91 * Do nothing if the DOM is already created or the layout doesn't have canvas. |
95 * @param {string} layout The keyboard layout for which canvas is created. | 92 * @param {string} layout The keyboard layout for which canvas is created. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 keyboardDiv.id = getKeyboardId(layout); | 124 keyboardDiv.id = getKeyboardId(layout); |
128 keyboardDiv.className = 'keyboard'; | 125 keyboardDiv.className = 'keyboard'; |
129 initRows(layout, keyboardDiv); | 126 initRows(layout, keyboardDiv); |
130 initHandwritingCanvas(layout, keyboardDiv); | 127 initHandwritingCanvas(layout, keyboardDiv); |
131 keyboard['keyboardDiv'] = keyboardDiv; | 128 keyboard['keyboardDiv'] = keyboardDiv; |
132 window.onresize(); | 129 window.onresize(); |
133 element.appendChild(keyboardDiv); | 130 element.appendChild(keyboardDiv); |
134 } | 131 } |
135 | 132 |
136 /** | 133 /** |
| 134 * Create a DOM of the popup keyboard. |
| 135 * @param {Element} The DOM Element to which the popup keyboard is appended. |
| 136 * @return {void} |
| 137 */ |
| 138 function initPopupKeyboard(element) { |
| 139 var popupDiv = document.createElement('div'); |
| 140 popupDiv.id = 'popup'; |
| 141 popupDiv.className = 'keyboard popup'; |
| 142 popupDiv.style.visibility = 'hidden'; |
| 143 element.appendChild(popupDiv); |
| 144 element.addEventListener('mouseup', function(evt) { |
| 145 hidePopupKeyboard(evt); |
| 146 }); |
| 147 } |
| 148 |
| 149 /** |
137 * Resize the keyboard according to the new window size. | 150 * Resize the keyboard according to the new window size. |
138 * @return {void} | 151 * @return {void} |
139 */ | 152 */ |
140 window.onresize = function() { | 153 window.onresize = function() { |
141 var keyboardDiv = KEYBOARDS[currentKeyboardLayout]['keyboardDiv']; | 154 var keyboardDiv = KEYBOARDS[currentKeyboardLayout]['keyboardDiv']; |
142 var height = getKeyboardHeight(); | 155 var height = getKeyboardHeight(); |
143 keyboardDiv.style.height = height + 'px'; | 156 keyboardDiv.style.height = height + 'px'; |
144 var mainDiv = document.getElementById('main'); | 157 var mainDiv = document.getElementById('main'); |
145 mainDiv.style.width = Math.floor(getKeyboardAspect() * height) + 'px'; | 158 mainDiv.style.width = Math.floor(getKeyboardAspect() * height) + 'px'; |
146 var rowsLength = KEYBOARDS[currentKeyboardLayout]['rows'].length; | 159 var rowsLength = KEYBOARDS[currentKeyboardLayout]['rows'].length; |
(...skipping 17 matching lines...) Expand all Loading... |
164 body.addEventListener('touchmove', disableGestures); | 177 body.addEventListener('touchmove', disableGestures); |
165 body.addEventListener('touchend', disableGestures); | 178 body.addEventListener('touchend', disableGestures); |
166 | 179 |
167 var mainDiv = document.createElement('div'); | 180 var mainDiv = document.createElement('div'); |
168 mainDiv.className = 'main'; | 181 mainDiv.className = 'main'; |
169 mainDiv.id = 'main'; | 182 mainDiv.id = 'main'; |
170 body.appendChild(mainDiv); | 183 body.appendChild(mainDiv); |
171 | 184 |
172 initIme(mainDiv); | 185 initIme(mainDiv); |
173 initKeyboard(currentKeyboardLayout, mainDiv); | 186 initKeyboard(currentKeyboardLayout, mainDiv); |
| 187 initPopupKeyboard(body); |
174 | 188 |
175 window.onhashchange(); | 189 window.onhashchange(); |
176 | 190 |
177 chrome.experimental.input.onTextInputTypeChanged.addListener(function(type) { | 191 chrome.experimental.input.onTextInputTypeChanged.addListener(function(type) { |
178 switch(type) { | 192 switch(type) { |
179 case "text": | 193 case "text": |
180 currentMode = SHIFT_MODE; | 194 currentMode = SHIFT_MODE; |
181 break; | 195 break; |
182 case "email": | 196 case "email": |
183 case "password": | 197 case "password": |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 if (canvas !== undefined) { | 241 if (canvas !== undefined) { |
228 if (!visible) { | 242 if (!visible) { |
229 canvas.clear(); | 243 canvas.clear(); |
230 } | 244 } |
231 } | 245 } |
232 if (visible) { | 246 if (visible) { |
233 window.onresize(); | 247 window.onresize(); |
234 } | 248 } |
235 }); | 249 }); |
236 } | 250 } |
OLD | NEW |