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 + '%'; | |
bryeung
2011/09/08 19:22:35
Why is this necsesary? And why is it being done w
mazda
2011/09/12 14:33:48
Keys occupy the entire row without these paddings
| |
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) { | |
bryeung
2011/09/08 19:22:35
Is there a reason there is not also a touchend han
mazda
2011/09/12 14:33:48
This mouseup handler deals with the case that the
| |
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; |
147 keyboardDiv.style.fontSize = (height / kFontSizeRatio / rowsLength) + 'px'; | 160 keyboardDiv.style.fontSize = (height / kFontSizeRatio / rowsLength) + 'px'; |
148 updateIme(); | 161 updateIme(); |
149 } | 162 } |
150 | 163 |
151 /** | 164 /** |
152 * Init the keyboard. | 165 * Init the keyboard. |
153 * @return {void} | 166 * @return {void} |
154 */ | 167 */ |
155 window.onload = function() { | 168 window.onload = function() { |
156 var body = document.getElementById('b'); | 169 var body = document.getElementById('b'); |
157 | 170 |
158 var mainDiv = document.createElement('div'); | 171 var mainDiv = document.createElement('div'); |
159 mainDiv.className = 'main'; | 172 mainDiv.className = 'main'; |
160 mainDiv.id = 'main'; | 173 mainDiv.id = 'main'; |
161 body.appendChild(mainDiv); | 174 body.appendChild(mainDiv); |
162 | 175 |
163 initIme(mainDiv); | 176 initIme(mainDiv); |
164 initKeyboard(currentKeyboardLayout, mainDiv); | 177 initKeyboard(currentKeyboardLayout, mainDiv); |
178 initPopupKeyboard(body); | |
165 | 179 |
166 window.onhashchange(); | 180 window.onhashchange(); |
167 | 181 |
168 chrome.experimental.input.onTextInputTypeChanged.addListener(function(type) { | 182 chrome.experimental.input.onTextInputTypeChanged.addListener(function(type) { |
169 switch(type) { | 183 switch(type) { |
170 case "text": | 184 case "text": |
171 currentMode = SHIFT_MODE; | 185 currentMode = SHIFT_MODE; |
172 break; | 186 break; |
173 case "email": | 187 case "email": |
174 case "password": | 188 case "password": |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 if (canvas !== undefined) { | 232 if (canvas !== undefined) { |
219 if (!visible) { | 233 if (!visible) { |
220 canvas.clear(); | 234 canvas.clear(); |
221 } | 235 } |
222 } | 236 } |
223 if (visible) { | 237 if (visible) { |
224 window.onresize(); | 238 window.onresize(); |
225 } | 239 } |
226 }); | 240 }); |
227 } | 241 } |
OLD | NEW |