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 // currently available candidates
NSMutableArray *_candidates; NSMutableArray *_candidates;
// current input mode
NSString *_inputMode;
} }
@end @end

View File

@ -79,6 +79,10 @@ static NSString *const kCandidateTextFontName = @"CandidateTextFontName";
static NSString *const kCandidateKeyLabelFontName = @"CandidateKeyLabelFontName"; static NSString *const kCandidateKeyLabelFontName = @"CandidateKeyLabelFontName";
static NSString *const kCandidateKeys = @"CandidateKeys"; 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 // key code enums
enum { enum {
kUpKeyCode = 126, kUpKeyCode = 126,
@ -179,6 +183,8 @@ public:
if (![[NSUserDefaults standardUserDefaults] objectForKey:kDisableUserCandidateSelectionLearning]) { if (![[NSUserDefaults standardUserDefaults] objectForKey:kDisableUserCandidateSelectionLearning]) {
[[NSUserDefaults standardUserDefaults] setObject:(id)kCFBooleanTrue forKey:kDisableUserCandidateSelectionLearning]; [[NSUserDefaults standardUserDefaults] setObject:(id)kCFBooleanTrue forKey:kDisableUserCandidateSelectionLearning];
} }
_inputMode = kBopomofoModeIdentifier;
} }
return self; return self;
@ -262,9 +268,6 @@ public:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::StandardLayout()); _bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::StandardLayout());
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kKeyboardLayoutPreferenceKey]; [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kKeyboardLayoutPreferenceKey];
} }
if (keyboardLayout < 4) {
[client overrideKeyboardWithKeyboardNamed:@"com.apple.keylayout.US"];
}
// set the size // set the size
NSInteger textSize = [[NSUserDefaults standardUserDefaults] integerForKey:kCandidateListTextSizeKey]; NSInteger textSize = [[NSUserDefaults standardUserDefaults] integerForKey:kCandidateListTextSizeKey];
@ -301,6 +304,27 @@ public:
[_candidates removeAllObjects]; [_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 #pragma mark - IMKServerInput protocol methods
- (void)commitComposition:(id)client - (void)commitComposition:(id)client
@ -462,9 +486,25 @@ public:
- (BOOL)handleCandidateEventWithInputText:(NSString *)inputText charCode:(UniChar)charCode keyCode:(NSUInteger)keyCode - (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) { if (charCode == 27) {
gCurrentCandidateController.visible = NO; gCurrentCandidateController.visible = NO;
[_candidates removeAllObjects]; [_candidates removeAllObjects];
if (_inputMode == kPlainBopomofoModeIdentifier) {
_builder->clear();
_walkedNodes.clear();
[_composingBuffer setString:@""];
}
[self updateClientComposingBuffer:_currentCandidateClient];
return YES; return YES;
} }
else if (charCode == 13 || keyCode == 127) { else if (charCode == 13 || keyCode == 127) {
@ -565,7 +605,6 @@ public:
return YES; return YES;
} }
else { else {
NSInteger index = NSNotFound; NSInteger index = NSNotFound;
for (NSUInteger j = 0, c = [gCurrentCandidateController.keyLabels count]; j < c; j++) { for (NSUInteger j = 0, c = [gCurrentCandidateController.keyLabels count]; j < c; j++) {
if ([inputText compare:[gCurrentCandidateController.keyLabels objectAtIndex:j] options:NSCaseInsensitiveSearch] == NSOrderedSame) { if ([inputText compare:[gCurrentCandidateController.keyLabels objectAtIndex:j] options:NSCaseInsensitiveSearch] == NSOrderedSame) {
@ -583,6 +622,16 @@ public:
} }
} }
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]; [self beep];
return YES; return YES;
} }
@ -716,6 +765,10 @@ public:
_bpmfReadingBuffer->clear(); _bpmfReadingBuffer->clear();
[self updateClientComposingBuffer:client]; [self updateClientComposingBuffer:client];
if (_inputMode == kPlainBopomofoModeIdentifier) {
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
// and tells the client that the key is consumed // and tells the client that the key is consumed
return YES; return YES;
} }
@ -895,6 +948,11 @@ public:
[self beep]; [self beep];
} }
[self updateClientComposingBuffer:client]; [self updateClientComposingBuffer:client];
if (_inputMode == kPlainBopomofoModeIdentifier) {
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
return YES; return YES;
} }
@ -909,6 +967,17 @@ public:
[self beep]; [self beep];
} }
[self updateClientComposingBuffer:client]; [self updateClientComposingBuffer:client];
if (_inputMode == kPlainBopomofoModeIdentifier) {
[self collectCandidates];
if ([_candidates count] == 1) {
[self commitComposition:client];
}
else {
[self _showCandidateWindowUsingVerticalMode:useVerticalMode client:client];
}
}
return YES; return YES;
} }
@ -1105,6 +1174,11 @@ public:
gCurrentCandidateController.keyLabels = keyLabels; gCurrentCandidateController.keyLabels = keyLabels;
[self collectCandidates]; [self collectCandidates];
if (_inputMode == kPlainBopomofoModeIdentifier && [_candidates count] == 1) {
[self commitComposition:client];
return;
}
gCurrentCandidateController.delegate = self; gCurrentCandidateController.delegate = self;
[gCurrentCandidateController reloadData]; [gCurrentCandidateController reloadData];
@ -1213,6 +1287,11 @@ public:
[self walk]; [self walk];
[self updateClientComposingBuffer:_currentCandidateClient]; [self updateClientComposingBuffer:_currentCandidateClient];
if (_inputMode == kPlainBopomofoModeIdentifier) {
[self commitComposition:_currentCandidateClient];
return;
}
} }
@end @end