1 // Copyright 2014 The ChromeOS IME Authors. All Rights Reserved.
2 // limitations under the License.
3 // See the License for the specific language governing permissions and
4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5 // distributed under the License is distributed on an "AS-IS" BASIS,
6 // Unless required by applicable law or agreed to in writing, software
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // You may obtain a copy of the License at
11 // you may not use this file except in compliance with the License.
12 // Licensed under the Apache License, Version 2.0 (the "License");
14 goog
.provide('i18n.input.chrome.inputview.StateManager');
16 goog
.require('i18n.input.chrome.inputview.Covariance');
18 goog
.scope(function() {
23 * The state for the input view keyboard.
27 i18n
.input
.chrome
.inputview
.StateManager = function() {
28 /** @type {!i18n.input.chrome.inputview.Covariance} */
29 this.covariance
= new i18n
.input
.chrome
.inputview
.Covariance();
31 var StateManager
= i18n
.input
.chrome
.inputview
.StateManager
;
35 StateManager
.prototype.contextType
= '';
39 * The state of the keyboard.
44 StateManager
.prototype.state_
= 0;
53 StateManager
.prototype.sticky_
= 0;
57 * Bits to indicate which state key is down.
62 StateManager
.prototype.stateKeyDown_
= 0;
66 * Bits to track which state is in chording.
71 StateManager
.prototype.chording_
= 0;
75 * A flag to temporary compatible with the current modifier key state
76 * managerment, this bit indicates the sticky is set by double_click or long
77 * press which won't be canceled by keyup or text commit.
81 StateManager
.prototype.finalSticky_
= 0;
85 * Whether the current keyset is in English mode.
89 StateManager
.prototype.isEnMode
= false;
93 * Sets a state to keydown.
95 * @param {!i18n.input.chrome.inputview.StateType} stateType The state type.
96 * @param {boolean} isKeyDown True if the state key is down.
98 i18n
.input
.chrome
.inputview
.StateManager
.prototype.setKeyDown = function(
99 stateType
, isKeyDown
) {
101 this.stateKeyDown_
|= stateType
;
103 this.stateKeyDown_
&= ~stateType
;
104 this.chording_
&= ~stateType
;
110 * True if the key is down.
112 * @param {!i18n.input.chrome.inputview.StateType} stateType .
113 * @return {boolean} .
115 i18n
.input
.chrome
.inputview
.StateManager
.prototype.isKeyDown = function(
117 return (this.stateKeyDown_
& stateType
) != 0;
122 * Triggers chording and record it for each key-downed state.
124 i18n
.input
.chrome
.inputview
.StateManager
.prototype.triggerChording
=
126 this.chording_
|= this.stateKeyDown_
;
131 * True if the state is chording now.
133 * @param {!i18n.input.chrome.inputview.StateType} stateType The state type.
134 * @return {boolean} True if the state is chording.
136 i18n
.input
.chrome
.inputview
.StateManager
.prototype.isChording = function(
138 return (this.chording_
& stateType
) != 0;
143 * Is a state in final sticky.
145 * @param {i18n.input.chrome.inputview.StateType} stateType .
146 * @return {boolean} .
148 i18n
.input
.chrome
.inputview
.StateManager
.prototype.isFinalSticky = function(
150 return (this.finalSticky_
& stateType
) != 0;
155 * Sets a specific state to be final sticky.
157 * @param {i18n.input.chrome.inputview.StateType} stateType .
158 * @param {boolean} isFinalSticky .
160 i18n
.input
.chrome
.inputview
.StateManager
.prototype.setFinalSticky = function(
161 stateType
, isFinalSticky
) {
163 this.finalSticky_
|= stateType
;
165 this.finalSticky_
&= ~stateType
;
171 * Sets a state to be sticky.
173 * @param {!i18n.input.chrome.inputview.StateType} stateType The state type.
174 * @param {boolean} isSticky True to set it sticky.
176 i18n
.input
.chrome
.inputview
.StateManager
.prototype.setSticky = function(
177 stateType
, isSticky
) {
179 this.sticky_
|= stateType
;
181 this.sticky_
&= ~stateType
;
189 * @param {!i18n.input.chrome.inputview.StateType} stateType The state
191 * @return {boolean} True if it is sticky.
193 i18n
.input
.chrome
.inputview
.StateManager
.prototype.isSticky = function(
195 return (this.sticky_
& stateType
) != 0;
202 * @param {!i18n.input.chrome.inputview.StateType} stateType The state
204 * @param {boolean} enable True to enable the state.
206 i18n
.input
.chrome
.inputview
.StateManager
.prototype.setState = function(
209 this.state_
= this.state_
| stateType
;
211 this.state_
= this.state_
& ~stateType
;
219 * @param {!i18n.input.chrome.inputview.StateType} stateType The state
221 * @param {boolean} isSticky True to set it sticky.
223 i18n
.input
.chrome
.inputview
.StateManager
.prototype.toggleState = function(
224 stateType
, isSticky
) {
225 var enable
= !this.hasState(stateType
);
226 this.setState(stateType
, enable
);
227 isSticky
= enable
? isSticky
: false;
228 this.setSticky(stateType
, isSticky
);
235 * @param {!i18n.input.chrome.inputview.StateType} stateType The state
237 * @return {boolean} True if the state is on.
239 i18n
.input
.chrome
.inputview
.StateManager
.prototype.hasState = function(
241 return (this.state_
& stateType
) != 0;
248 * @return {number} The state.
250 i18n
.input
.chrome
.inputview
.StateManager
.prototype.getState
=
257 * Clears unsticky state.
260 i18n
.input
.chrome
.inputview
.StateManager
.prototype.clearUnstickyState
=
262 this.state_
= this.state_
& this.sticky_
;
267 * True if there is unsticky state.
269 * @return {boolean} True if there is unsticky state.
271 i18n
.input
.chrome
.inputview
.StateManager
.prototype.hasUnStickyState
=
273 return this.state_
!= this.sticky_
;
281 i18n
.input
.chrome
.inputview
.StateManager
.prototype.reset = function() {