Cleans-up InputMethodController.mm.

This commit is contained in:
zonble 2011-10-18 22:26:55 +08:00
parent a230705bbd
commit c9d0378cd5
1 changed files with 104 additions and 101 deletions

View File

@ -94,6 +94,7 @@ SimpleLM LTLanguageModel;
- (void)_performDeferredSaveUserCandidatesDictionary;
- (void)saveUserCandidatesDictionary;
- (void)_showCandidateWindowUsingVerticalMode:(BOOL)useVerticalMode client:(id)client;
@end
// sort helper
@ -195,14 +196,7 @@ public:
return menu;
}
#pragma mark IMKStateSetting protocol methods
- (void)showPreferences:(id)sender
{
// show the preferences panel, and also make the IME app itself the focus
[super showPreferences:sender];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
#pragma mark - IMKStateSetting protocol methods
- (void)activateServer:(id)client
{
@ -270,6 +264,8 @@ public:
_currentCandidateClient = nil;
}
#pragma mark - IMKServerInput protocol methods
- (void)commitComposition:(id)client
{
// if it's Terminal, we don't commit at the first call (the client of which will not be IPMDServerClientWrapper)
@ -422,42 +418,6 @@ public:
NSBeep();
}
- (void)_showCandidateWindowUsingVerticalMode:(BOOL)useVerticalMode client:(id)client
{
// candidate
[LTSharedCandidates setDismissesAutomatically:YES];
// wrap NSNumber; we only allow number keys 1-9 as selection keys in this project
#define LTUIntObj(x) ([NSNumber numberWithInteger:x])
[LTSharedCandidates setSelectionKeys:[NSArray arrayWithObjects:LTUIntObj(18), LTUIntObj(19), LTUIntObj(20), LTUIntObj(21), LTUIntObj(23), LTUIntObj(22), LTUIntObj(26), LTUIntObj(28), LTUIntObj(25), nil]];
#undef LTUIntObj
// set the candidate panel style
BOOL useHorizontalCandidateList = [[NSUserDefaults standardUserDefaults] boolForKey:kUseHorizontalCandidateListPreferenceKey];
if (useVerticalMode) {
[LTSharedCandidates setPanelType:kIMKSingleColumnScrollingCandidatePanel];
}
else if (useHorizontalCandidateList) {
[LTSharedCandidates setPanelType:kIMKSingleRowSteppingCandidatePanel];
}
else {
[LTSharedCandidates setPanelType:kIMKSingleColumnScrollingCandidatePanel];
}
// set the attributes for the candidate panel (which uses NSAttributedString)
NSInteger textSize = [[NSUserDefaults standardUserDefaults] integerForKey:kCandidateListTextSizeKey];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont systemFontOfSize:textSize], NSFontAttributeName, nil];
[LTSharedCandidates setAttributes:attributes];
[LTSharedCandidates updateCandidates];
[LTSharedCandidates show:useVerticalMode ? kIMKLocateCandidatesLeftHint : kIMKLocateCandidatesBelowHint];
// update the composing text, set the client
[self updateClientComposingBuffer:client];
_currentCandidateClient = client;
}
- (BOOL)inputText:(NSString*)inputText key:(NSInteger)keyCode modifiers:(NSUInteger)flags client:(id)client
{
NSRect textFrame = NSZeroRect;
@ -790,6 +750,69 @@ public:
return results;
}
- (void)candidateSelected:(NSAttributedString *)candidateString
{
// candidate selected, override the node with selection
string selectedValue;
if ([candidateString isKindOfClass:[NSString class]]) {
selectedValue = [(NSString *)candidateString UTF8String];
}
else {
selectedValue = [[candidateString string] UTF8String];
}
if (![[NSUserDefaults standardUserDefaults] boolForKey:kDisableUserCandidateSelectionLearning]) {
NSString *trigram = [self neighborTrigramString];
NSString *selectedNSString = [NSString stringWithUTF8String:selectedValue.c_str()];
[TLCandidateLearningDictionary setObject:selectedNSString forKey:trigram];
[self saveUserCandidatesDictionary];
}
size_t cursorIndex = [self actualCandidateCursorIndex];
vector<NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
for (vector<NodeAnchor>::iterator ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
const vector<KeyValuePair>& candidates = (*ni).node->candidates();
for (size_t i = 0, c = candidates.size(); i < c; ++i) {
if (candidates[i].value == selectedValue) {
// found our node
const_cast<Node*>((*ni).node)->selectCandidateAtIndex(i);
break;
}
}
}
[self walk];
[self updateClientComposingBuffer:_currentCandidateClient];
_currentCandidateClient = nil;
}
#pragma mark - Private methods
- (size_t)actualCandidateCursorIndex
{
size_t cursorIndex = _builder->cursorIndex();
BOOL candidatePhraseLocatedAfterCursor = [[NSUserDefaults standardUserDefaults] boolForKey:kSelectPhraseAfterCursorAsCandidatePreferenceKey];
if (candidatePhraseLocatedAfterCursor) {
// MS Phonetics IME style, phrase is *after* the cursor, i.e. cursor is always *before* the phrase
if (cursorIndex < _builder->length()) {
++cursorIndex;
}
}
else {
if (!cursorIndex) {
++cursorIndex;
}
}
return cursorIndex;
}
- (NSString *)neighborTrigramString
{
// gather the "trigram" for user candidate selection learning
@ -845,69 +868,6 @@ public:
return [termArray componentsJoinedByString:@"-"];
}
- (void)candidateSelected:(NSAttributedString *)candidateString
{
// candidate selected, override the node with selection
string selectedValue;
if ([candidateString isKindOfClass:[NSString class]]) {
selectedValue = [(NSString *)candidateString UTF8String];
}
else {
selectedValue = [[candidateString string] UTF8String];
}
if (![[NSUserDefaults standardUserDefaults] boolForKey:kDisableUserCandidateSelectionLearning]) {
NSString *trigram = [self neighborTrigramString];
NSString *selectedNSString = [NSString stringWithUTF8String:selectedValue.c_str()];
[TLCandidateLearningDictionary setObject:selectedNSString forKey:trigram];
[self saveUserCandidatesDictionary];
}
size_t cursorIndex = [self actualCandidateCursorIndex];
vector<NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
for (vector<NodeAnchor>::iterator ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
const vector<KeyValuePair>& candidates = (*ni).node->candidates();
for (size_t i = 0, c = candidates.size(); i < c; ++i) {
if (candidates[i].value == selectedValue) {
// found our node
const_cast<Node*>((*ni).node)->selectCandidateAtIndex(i);
break;
}
}
}
[self walk];
[self updateClientComposingBuffer:_currentCandidateClient];
_currentCandidateClient = nil;
}
#pragma mark Private methods
- (size_t)actualCandidateCursorIndex
{
size_t cursorIndex = _builder->cursorIndex();
BOOL candidatePhraseLocatedAfterCursor = [[NSUserDefaults standardUserDefaults] boolForKey:kSelectPhraseAfterCursorAsCandidatePreferenceKey];
if (candidatePhraseLocatedAfterCursor) {
// MS Phonetics IME style, phrase is *after* the cursor, i.e. cursor is always *before* the phrase
if (cursorIndex < _builder->length()) {
++cursorIndex;
}
}
else {
if (!cursorIndex) {
++cursorIndex;
}
}
return cursorIndex;
}
- (void)_performDeferredSaveUserCandidatesDictionary
{
BOOL __unused success = [TLCandidateLearningDictionary writeToFile:TLUserCandidatesDictionaryPath atomically:YES];
@ -925,7 +885,50 @@ public:
[self performSelector:@selector(_performDeferredSaveUserCandidatesDictionary) withObject:nil afterDelay:5.0];
}
#pragma Misc menu items
- (void)_showCandidateWindowUsingVerticalMode:(BOOL)useVerticalMode client:(id)client
{
// candidate
[LTSharedCandidates setDismissesAutomatically:YES];
// wrap NSNumber; we only allow number keys 1-9 as selection keys in this project
#define LTUIntObj(x) ([NSNumber numberWithInteger:x])
[LTSharedCandidates setSelectionKeys:[NSArray arrayWithObjects:LTUIntObj(18), LTUIntObj(19), LTUIntObj(20), LTUIntObj(21), LTUIntObj(23), LTUIntObj(22), LTUIntObj(26), LTUIntObj(28), LTUIntObj(25), nil]];
#undef LTUIntObj
// set the candidate panel style
BOOL useHorizontalCandidateList = [[NSUserDefaults standardUserDefaults] boolForKey:kUseHorizontalCandidateListPreferenceKey];
if (useVerticalMode) {
[LTSharedCandidates setPanelType:kIMKSingleColumnScrollingCandidatePanel];
}
else if (useHorizontalCandidateList) {
[LTSharedCandidates setPanelType:kIMKSingleRowSteppingCandidatePanel];
}
else {
[LTSharedCandidates setPanelType:kIMKSingleColumnScrollingCandidatePanel];
}
// set the attributes for the candidate panel (which uses NSAttributedString)
NSInteger textSize = [[NSUserDefaults standardUserDefaults] integerForKey:kCandidateListTextSizeKey];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont systemFontOfSize:textSize], NSFontAttributeName, nil];
[LTSharedCandidates setAttributes:attributes];
[LTSharedCandidates updateCandidates];
[LTSharedCandidates show:useVerticalMode ? kIMKLocateCandidatesLeftHint : kIMKLocateCandidatesBelowHint];
// update the composing text, set the client
[self updateClientComposingBuffer:client];
_currentCandidateClient = client;
}
#pragma mark - Misc menu items
- (void)showPreferences:(id)sender
{
// show the preferences panel, and also make the IME app itself the focus
[super showPreferences:sender];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
- (void)showAbout:(id)sender
{