Implement the Plain Bopomofo mode.

This commit is contained in:
Lukhnos Liu 2012-09-09 01:58:15 -07:00
parent 6a5cd2a38a
commit 31f796f620
2 changed files with 87 additions and 5 deletions

View File

@ -61,6 +61,9 @@
// currently available candidates
NSMutableArray *_candidates;
// current input mode
NSString *_inputMode;
}
@end

View File

@ -79,6 +79,10 @@ static NSString *const kCandidateTextFontName = @"CandidateTextFontName";
static NSString *const kCandidateKeyLabelFontName = @"CandidateKeyLabelFontName";
static NSString *const kCandidateKeys = @"CandidateKeys";
// input modes
static NSString *const kBopomofoModeIdentifier = @"org.openvanilla.inputmethod.McBopomofo.Bopomofo";
static NSString *const kPlainBopomofoModeIdentifier = @"org.openvanilla.inputmethod.McBopomofo.PlainBopomofo";
// key code enums
enum {
kUpKeyCode = 126,
@ -179,6 +183,8 @@ public:
if (![[NSUserDefaults standardUserDefaults] objectForKey:kDisableUserCandidateSelectionLearning]) {
[[NSUserDefaults standardUserDefaults] setObject:(id)kCFBooleanTrue forKey:kDisableUserCandidateSelectionLearning];
}
_inputMode = kBopomofoModeIdentifier;
}
return self;
@ -262,9 +268,6 @@ public:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::StandardLayout());
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kKeyboardLayoutPreferenceKey];
}
if (keyboardLayout < 4) {
[client overrideKeyboardWithKeyboardNamed:@"com.apple.keylayout.US"];
}
// set the size
NSInteger textSize = [[NSUserDefaults standardUserDefaults] integerForKey:kCandidateListTextSizeKey];
@ -301,6 +304,27 @@ public:
[_candidates removeAllObjects];
}
- (void)setValue:(id)value forTag:(long)tag client:(id)sender
{
if ([value isKindOfClass:[NSString class]] && [value isEqual:kPlainBopomofoModeIdentifier]) {
_inputMode = kPlainBopomofoModeIdentifier;
}
else {
_inputMode = kBopomofoModeIdentifier;
}
[sender overrideKeyboardWithKeyboardNamed:@"com.apple.keylayout.US"];
if (!_bpmfReadingBuffer->isEmpty()) {
_bpmfReadingBuffer->clear();
[self updateClientComposingBuffer:sender];
}
if ([_composingBuffer length] > 0) {
[self commitComposition:sender];
}
}
#pragma mark - IMKServerInput protocol methods
- (void)commitComposition:(id)client
@ -462,9 +486,25 @@ public:
- (BOOL)handleCandidateEventWithInputText:(NSString *)inputText charCode:(UniChar)charCode keyCode:(NSUInteger)keyCode
{
if (_inputMode == kPlainBopomofoModeIdentifier) {
if (charCode == '<') {
keyCode = kPageUpKeyCode;
}
else if (charCode == '>') {
keyCode = kPageDownKeyCode;
}
}
if (charCode == 27) {
gCurrentCandidateController.visible = NO;
[_candidates removeAllObjects];
if (_inputMode == kPlainBopomofoModeIdentifier) {
_builder->clear();
_walkedNodes.clear();
[_composingBuffer setString:@""];
}
[self updateClientComposingBuffer:_currentCandidateClient];
return YES;
}
else if (charCode == 13 || keyCode == 127) {
@ -564,8 +604,7 @@ public:
return YES;
}
else {
else {
NSInteger index = NSNotFound;
for (NSUInteger j = 0, c = [gCurrentCandidateController.keyLabels count]; j < c; j++) {
if ([inputText compare:[gCurrentCandidateController.keyLabels objectAtIndex:j] options:NSCaseInsensitiveSearch] == NSOrderedSame) {
@ -582,6 +621,16 @@ public:
return YES;
}
}
if (_inputMode == kPlainBopomofoModeIdentifier) {
if (_bpmfReadingBuffer->isValidKey((char)charCode)) {
NSUInteger candidateIndex = [gCurrentCandidateController candidateIndexAtKeyLabelIndex:0];
if (candidateIndex != NSUIntegerMax) {
[self candidateController:gCurrentCandidateController didSelectCandidateAtIndex:candidateIndex];
return [self inputText:inputText key:keyCode modifiers:0 client:_currentCandidateClient];
}
}
}
[self beep];
return YES;
@ -716,6 +765,10 @@ public:
_bpmfReadingBuffer->clear();
[self updateClientComposingBuffer:client];
if (_inputMode == kPlainBopomofoModeIdentifier) {
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
// and tells the client that the key is consumed
return YES;
}
@ -895,6 +948,11 @@ public:
[self beep];
}
[self updateClientComposingBuffer:client];
if (_inputMode == kPlainBopomofoModeIdentifier) {
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
return YES;
}
@ -909,6 +967,17 @@ public:
[self beep];
}
[self updateClientComposingBuffer:client];
if (_inputMode == kPlainBopomofoModeIdentifier) {
[self collectCandidates];
if ([_candidates count] == 1) {
[self commitComposition:client];
}
else {
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
}
return YES;
}
@ -1104,6 +1173,11 @@ public:
gCurrentCandidateController.keyLabels = keyLabels;
[self collectCandidates];
if (_inputMode == kPlainBopomofoModeIdentifier && [_candidates count] == 1) {
[self commitComposition:client];
return;
}
gCurrentCandidateController.delegate = self;
[gCurrentCandidateController reloadData];
@ -1213,6 +1287,11 @@ public:
[self walk];
[self updateClientComposingBuffer:_currentCandidateClient];
if (_inputMode == kPlainBopomofoModeIdentifier) {
[self commitComposition:_currentCandidateClient];
return;
}
}
@end