Code clean-up.

This commit is contained in:
zonble 2022-01-28 16:16:52 +08:00
parent 0bc9468ba2
commit b13da10ec0
6 changed files with 145 additions and 160 deletions

View File

@ -76,7 +76,6 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
@implementation McBopomofoInputMethodController @implementation McBopomofoInputMethodController
- (id)initWithServer:(IMKServer *)server delegate:(id)delegate client:(id)client - (id)initWithServer:(IMKServer *)server delegate:(id)delegate client:(id)client
{ {
// an instance is initialized whenever a text input client (a Mac app) requires // an instance is initialized whenever a text input client (a Mac app) requires
@ -96,7 +95,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
{ {
// a menu instance (autoreleased) is requested every time the user click on the input menu // a menu instance (autoreleased) is requested every time the user click on the input menu
NSMenu *menu = [[NSMenu alloc] initWithTitle:LocalizationNotNeeded(@"Input Method Menu")]; NSMenu *menu = [[NSMenu alloc] initWithTitle:LocalizationNotNeeded(@"Input Method Menu")];
NSString *inputMode = [_keyHandler inputMode]; NSString *inputMode = _keyHandler.inputMode;
[menu addItemWithTitle:NSLocalizedString(@"McBopomofo Preferences", @"") action:@selector(showPreferences:) keyEquivalent:@""]; [menu addItemWithTitle:NSLocalizedString(@"McBopomofo Preferences", @"") action:@selector(showPreferences:) keyEquivalent:@""];
@ -147,12 +146,14 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
// reset the state // reset the state
_currentDeferredClient = nil; _currentDeferredClient = nil;
_currentCandidateClient = nil; _currentCandidateClient = nil;
InputStateEmpty *newState = [[InputStateEmpty alloc] init];
[self handleState:newState client:client]; [_keyHandler clear];
InputStateEmpty *empty = [[InputStateEmpty alloc] init];
[self handleState:empty client:client];
// checks and populates the default settings // checks and populates the default settings
[_keyHandler synchWithPrefereneces]; [_keyHandler syncWithPreferences];
[(AppDelegate * )[NSApp delegate] checkForUpdate]; [(AppDelegate *) NSApp.delegate checkForUpdate];
} }
- (void)deactivateServer:(id)client - (void)deactivateServer:(id)client
@ -162,8 +163,8 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
InputStateEmpty *empty = [[InputStateEmpty alloc] init]; InputStateEmpty *empty = [[InputStateEmpty alloc] init];
[self handleState:empty client:client]; [self handleState:empty client:client];
InputStateDeactive *deactive = [[InputStateDeactive alloc] init]; InputStateDeactivated *inactive = [[InputStateDeactivated alloc] init];
[self handleState:deactive client:client]; [self handleState:inactive client:client];
} }
- (void)setValue:(id)value forTag:(long)tag client:(id)sender - (void)setValue:(id)value forTag:(long)tag client:(id)sender
@ -177,10 +178,10 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
} }
// Only apply the changes if the value is changed // Only apply the changes if the value is changed
if (![ [_keyHandler inputMode] isEqualToString:newInputMode]) { if (![_keyHandler.inputMode isEqualToString:newInputMode]) {
[[NSUserDefaults standardUserDefaults] synchronize]; [[NSUserDefaults standardUserDefaults] synchronize];
// Remember to override the keyboard layout again -- treat this as an activate eventy // Remember to override the keyboard layout again -- treat this as an activate event.
NSString *basisKeyboardLayoutID = Preferences.basisKeyboardLayout; NSString *basisKeyboardLayoutID = Preferences.basisKeyboardLayout;
[sender overrideKeyboardWithKeyboardNamed:basisKeyboardLayoutID]; [sender overrideKeyboardWithKeyboardNamed:basisKeyboardLayoutID];
[_keyHandler clear]; [_keyHandler clear];
@ -194,12 +195,12 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
- (NSUInteger)recognizedEvents:(id)sender - (NSUInteger)recognizedEvents:(id)sender
{ {
return NSKeyDownMask | NSFlagsChangedMask; return NSEventMaskKeyDown | NSEventMaskFlagsChanged;
} }
- (BOOL)handleEvent:(NSEvent *)event client:(id)client - (BOOL)handleEvent:(NSEvent *)event client:(id)client
{ {
if ([event type] == NSFlagsChanged) { if ([event type] == NSEventMaskFlagsChanged) {
NSString *functionKeyKeyboardLayoutID = Preferences.functionKeyboardLayout; NSString *functionKeyKeyboardLayoutID = Preferences.functionKeyboardLayout;
NSString *basisKeyboardLayoutID = Preferences.basisKeyboardLayout; NSString *basisKeyboardLayoutID = Preferences.basisKeyboardLayout;
@ -210,13 +211,13 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
// Function key pressed. // Function key pressed.
BOOL includeShift = Preferences.functionKeyKeyboardLayoutOverrideIncludeShiftKey; BOOL includeShift = Preferences.functionKeyKeyboardLayoutOverrideIncludeShiftKey;
if (([event modifierFlags] & ~NSEventModifierFlagShift) || (([event modifierFlags] & NSEventModifierFlagShift) && includeShift)) { if ((event.modifierFlags & ~NSEventModifierFlagShift) || ((event.modifierFlags & NSEventModifierFlagShift) && includeShift)) {
// Override the keyboard layout and let the OS do its thing // Override the keyboard layout and let the OS do its thing
[client overrideKeyboardWithKeyboardNamed:functionKeyKeyboardLayoutID]; [client overrideKeyboardWithKeyboardNamed:functionKeyKeyboardLayoutID];
return NO; return NO;
} }
// Revert back to the basis layout when the function key is released // Revert to the basis layout when the function key is released
[client overrideKeyboardWithKeyboardNamed:basisKeyboardLayoutID]; [client overrideKeyboardWithKeyboardNamed:basisKeyboardLayoutID];
return NO; return NO;
} }
@ -227,7 +228,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
@try { @try {
attributes = [client attributesForCharacterIndex:0 lineHeightRectangle:&textFrame]; attributes = [client attributesForCharacterIndex:0 lineHeightRectangle:&textFrame];
useVerticalMode = [attributes objectForKey:@"IMKTextOrientation"] && [[attributes objectForKey:@"IMKTextOrientation"] integerValue] == 0; useVerticalMode = attributes[@"IMKTextOrientation"] && [attributes[@"IMKTextOrientation"] integerValue] == 0;
} }
@catch (NSException *e) { @catch (NSException *e) {
// exception may raise while using Twitter.app's search filed. // exception may raise while using Twitter.app's search filed.
@ -241,10 +242,9 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
KeyHandlerInput *input = [[KeyHandlerInput alloc] initWithEvent:event isVerticalMode:useVerticalMode]; KeyHandlerInput *input = [[KeyHandlerInput alloc] initWithEvent:event isVerticalMode:useVerticalMode];
BOOL result = [_keyHandler handleInput:input state:_state stateCallback:^(InputState *state) { BOOL result = [_keyHandler handleInput:input state:_state stateCallback:^(InputState *state) {
[self handleState:state client:client]; [self handleState:state client:client];
} candidateSelectionCallback:^{ } candidateSelectionCallback:^{
NSLog(@"candidateSelectionCallback "); NSLog(@"candidate window updated.");
// [self handleState:self->_state client:(self->_currentCandidateClient ? self->_currentCandidateClient : client)]; } errorCallback:^{
} errorCallback:^{
NSBeep(); NSBeep();
}]; }];
@ -284,7 +284,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
if ([[client bundleIdentifier] isEqualToString:@"com.apple.Terminal"] && ![NSStringFromClass([client class]) isEqualToString:@"IPMDServerClientWrapper"]) { if ([[client bundleIdentifier] isEqualToString:@"com.apple.Terminal"] && ![NSStringFromClass([client class]) isEqualToString:@"IPMDServerClientWrapper"]) {
if (_currentDeferredClient) { if (_currentDeferredClient) {
id currentDeferredClient = _currentDeferredClient; id currentDeferredClient = _currentDeferredClient;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[currentDeferredClient insertText:buffer replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; [currentDeferredClient insertText:buffer replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
}); });
} }
@ -297,33 +297,33 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
{ {
// NSLog(@"current state: %@ new state: %@", _state, newState ); // NSLog(@"current state: %@ new state: %@", _state, newState );
// We need to set the state to the member varible since the candidate // We need to set the state to the member variable since the candidate
// window need to read the candidates from it. // window need to read the candidates from it.
InputState *previous = _state; InputState *previous = _state;
_state = newState; _state = newState;
if ([newState isKindOfClass:[InputStateDeactive class]]) { if ([newState isKindOfClass:[InputStateDeactivated class]]) {
[self _handleInputStateDeactive:(InputStateDeactive *) newState previous:previous client:client]; [self _handleDeactivated:(InputStateDeactivated *) newState previous:previous client:client];
} else if ([newState isKindOfClass:[InputStateEmpty class]]) { } else if ([newState isKindOfClass:[InputStateEmpty class]]) {
[self _handleInputStateEmpty:(InputStateEmpty *) newState previous:previous client:client]; [self _handleEmpty:(InputStateEmpty *) newState previous:previous client:client];
} else if ([newState isKindOfClass:[InputStateEmptyIgnoringPreviousState class]]) { } else if ([newState isKindOfClass:[InputStateEmptyIgnoringPreviousState class]]) {
[self _handleInputStateEmptyIgnoringPrevious:(InputStateEmptyIgnoringPreviousState *) newState previous:previous client:client]; [self _handleEmptyIgnoringPrevious:(InputStateEmptyIgnoringPreviousState *) newState previous:previous client:client];
} else if ([newState isKindOfClass:[InputStateCommitting class]]) { } else if ([newState isKindOfClass:[InputStateCommitting class]]) {
[self _handleInputStateCommitting:(InputStateCommitting *) newState previous:previous client:client]; [self _handleCommitting:(InputStateCommitting *) newState previous:previous client:client];
} else if ([newState isKindOfClass:[InputStateInputting class]]) { } else if ([newState isKindOfClass:[InputStateInputting class]]) {
[self _handleInputStateInputting:(InputStateInputting *) newState previous:previous client:client]; [self _handleInputting:(InputStateInputting *) newState previous:previous client:client];
} else if ([newState isKindOfClass:[InputStateMarking class]]) { } else if ([newState isKindOfClass:[InputStateMarking class]]) {
[self _handleInputStateMarking:(InputStateMarking *) newState previous:previous client:client]; [self _handleMarking:(InputStateMarking *) newState previous:previous client:client];
} else if ([newState isKindOfClass:[InputStateChoosingCandidate class]]) { } else if ([newState isKindOfClass:[InputStateChoosingCandidate class]]) {
[self _handleInputStateChoosingCandidate:(InputStateChoosingCandidate *)newState previous:previous client:client]; [self _handleChoosingCandidate:(InputStateChoosingCandidate *) newState previous:previous client:client];
} }
} }
- (void)_handleInputStateDeactive:(InputStateDeactive *)state previous:(InputState *)previous client:(id)client - (void)_handleDeactivated:(InputStateDeactivated *)state previous:(InputState *)previous client:(id)client
{ {
// commit any residue in the composing buffer // commit any residue in the composing buffer
if ([previous isKindOfClass:[InputStateInputting class]]) { if ([previous isKindOfClass:[InputStateInputting class]]) {
NSString *buffer = [(InputStateInputting *) previous composingBuffer]; NSString *buffer = ((InputStateInputting *) previous).composingBuffer;
[self _commitText:buffer client:client]; [self _commitText:buffer client:client];
} }
[client setMarkedText:@"" selectionRange:NSMakeRange(0, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; [client setMarkedText:@"" selectionRange:NSMakeRange(0, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
@ -336,11 +336,11 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
[self _hideTooltip]; [self _hideTooltip];
} }
- (void)_handleInputStateEmpty:(InputStateEmpty *)state previous:(InputState *)previous client:(id)client - (void)_handleEmpty:(InputStateEmpty *)state previous:(InputState *)previous client:(id)client
{ {
// commit any residue in the composing buffer // commit any residue in the composing buffer
if ([previous isKindOfClass:[InputStateInputting class]]) { if ([previous isKindOfClass:[InputStateInputting class]]) {
NSString *buffer = [(InputStateInputting *) previous composingBuffer]; NSString *buffer = ((InputStateInputting *) previous).composingBuffer;
[self _commitText:buffer client:client]; [self _commitText:buffer client:client];
} }
@ -349,31 +349,30 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
[self _hideTooltip]; [self _hideTooltip];
} }
- (void)_handleInputStateEmptyIgnoringPrevious:(InputStateEmptyIgnoringPreviousState *)state previous:(InputState *)previous client:(id)client - (void)_handleEmptyIgnoringPrevious:(InputStateEmptyIgnoringPreviousState *)state previous:(InputState *)previous client:(id)client
{ {
// [client insertText:@"" replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
[client setMarkedText:@"" selectionRange:NSMakeRange(0, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; [client setMarkedText:@"" selectionRange:NSMakeRange(0, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
gCurrentCandidateController.visible = NO; gCurrentCandidateController.visible = NO;
[self _hideTooltip]; [self _hideTooltip];
} }
- (void)_handleInputStateCommitting:(InputStateCommitting *)state previous:(InputState *)previous client:(id)client - (void)_handleCommitting:(InputStateCommitting *)state previous:(InputState *)previous client:(id)client
{ {
NSString *poppedText = [state poppedText]; NSString *poppedText = state.poppedText;
[self _commitText:poppedText client:client]; [self _commitText:poppedText client:client];
gCurrentCandidateController.visible = NO; gCurrentCandidateController.visible = NO;
[self _hideTooltip]; [self _hideTooltip];
} }
- (void)_handleInputStateInputting:(InputStateInputting *)state previous:(InputState *)previous client:(id)client - (void)_handleInputting:(InputStateInputting *)state previous:(InputState *)previous client:(id)client
{ {
NSString *poppedText = state.poppedText; NSString *poppedText = state.poppedText;
if (poppedText.length) { if (poppedText.length) {
[self _commitText:poppedText client:client]; [self _commitText:poppedText client:client];
} }
NSUInteger cursorIndex = [state cursorIndex]; NSUInteger cursorIndex = state.cursorIndex;
NSAttributedString *attrString = [state attributedString]; NSAttributedString *attrString = state.attributedString;
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound, // the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put ths composing buffer // i.e. the client app needs to take care of where to put ths composing buffer
@ -383,10 +382,10 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
[self _hideTooltip]; [self _hideTooltip];
} }
- (void)_handleInputStateMarking:(InputStateMarking *)state previous:(InputState *)previous client:(id)client - (void)_handleMarking:(InputStateMarking *)state previous:(InputState *)previous client:(id)client
{ {
NSUInteger cursorIndex = [state cursorIndex]; NSUInteger cursorIndex = state.cursorIndex;
NSAttributedString *attrString = [state attributedString]; NSAttributedString *attrString = state.attributedString;
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound, // the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put ths composing buffer // i.e. the client app needs to take care of where to put ths composing buffer
@ -400,16 +399,16 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
} }
} }
- (void)_handleInputStateChoosingCandidate:(InputStateChoosingCandidate *)state previous:(InputState *)previous client:(id)client - (void)_handleChoosingCandidate:(InputStateChoosingCandidate *)state previous:(InputState *)previous client:(id)client
{ {
NSUInteger cursorIndex = [state cursorIndex]; NSUInteger cursorIndex = state.cursorIndex;
NSAttributedString *attrString = [state attributedString]; NSAttributedString *attrString = state.attributedString;
// the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound, // the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound,
// i.e. the client app needs to take care of where to put ths composing buffer // i.e. the client app needs to take care of where to put ths composing buffer
[client setMarkedText:attrString selectionRange:NSMakeRange(cursorIndex, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; [client setMarkedText:attrString selectionRange:NSMakeRange(cursorIndex, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
if ([_keyHandler inputMode] == kPlainBopomofoModeIdentifier && [state.candidates count] == 1) { if (_keyHandler.inputMode == kPlainBopomofoModeIdentifier && state.candidates.count == 1) {
NSString *buffer = [self _convertToSimplifiedChineseIfRequired:state.candidates.firstObject]; NSString *buffer = [self _convertToSimplifiedChineseIfRequired:state.candidates.firstObject];
[client insertText:buffer replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; [client insertText:buffer replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
InputStateEmpty *empty = [[InputStateEmpty alloc] init]; InputStateEmpty *empty = [[InputStateEmpty alloc] init];
@ -444,17 +443,17 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
NSString *ctFontName = Preferences.candidateTextFontName; NSString *ctFontName = Preferences.candidateTextFontName;
NSString *klFontName = Preferences.candidateKeyLabelFontName; NSString *klFontName = Preferences.candidateKeyLabelFontName;
NSString *ckeys = Preferences.candidateKeys; NSString *candidateKeys = Preferences.candidateKeys;
gCurrentCandidateController.keyLabelFont = klFontName ? [NSFont fontWithName:klFontName size:keyLabelSize] : [NSFont systemFontOfSize:keyLabelSize]; gCurrentCandidateController.keyLabelFont = klFontName ? [NSFont fontWithName:klFontName size:keyLabelSize] : [NSFont systemFontOfSize:keyLabelSize];
gCurrentCandidateController.candidateFont = ctFontName ? [NSFont fontWithName:ctFontName size:textSize] : [NSFont systemFontOfSize:textSize]; gCurrentCandidateController.candidateFont = ctFontName ? [NSFont fontWithName:ctFontName size:textSize] : [NSFont systemFontOfSize:textSize];
NSMutableArray *keyLabels = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil]; NSMutableArray *keyLabels = [@[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"] mutableCopy];
if ([ckeys length] > 1) { if (candidateKeys.length > 1) {
[keyLabels removeAllObjects]; [keyLabels removeAllObjects];
for (NSUInteger i = 0, c = [ckeys length]; i < c; i++) { for (NSUInteger i = 0, c = candidateKeys.length; i < c; i++) {
[keyLabels addObject:[ckeys substringWithRange:NSMakeRange(i, 1)]]; [keyLabels addObject:[candidateKeys substringWithRange:NSMakeRange(i, 1)]];
} }
} }
@ -465,7 +464,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
NSRect lineHeightRect = NSMakeRect(0.0, 0.0, 16.0, 16.0); NSRect lineHeightRect = NSMakeRect(0.0, 0.0, 16.0, 16.0);
NSInteger cursor = state.cursorIndex; NSInteger cursor = state.cursorIndex;
if (cursor == [state.composingBuffer length] && cursor != 0) { if (cursor == state.composingBuffer.length && cursor != 0) {
cursor--; cursor--;
} }
@ -494,9 +493,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
if ([IMKInputController instancesRespondToSelector:@selector(showPreferences:)]) { if ([IMKInputController instancesRespondToSelector:@selector(showPreferences:)]) {
[super showPreferences:sender]; [super showPreferences:sender];
} else { } else {
[(AppDelegate * )[ [(AppDelegate *) NSApp.delegate showPreferences];
NSApp
delegate] showPreferences];
} }
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
} }
@ -504,10 +501,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
- (void)toggleChineseConverter:(id)sender - (void)toggleChineseConverter:(id)sender
{ {
BOOL chineseConversionEnabled = [Preferences toggleChineseConversionEnabled]; BOOL chineseConversionEnabled = [Preferences toggleChineseConversionEnabled];
[NotifierController notifyWithMessage: [NotifierController notifyWithMessage:chineseConversionEnabled ? NSLocalizedString(@"Chinese conversion on", @"") : NSLocalizedString(@"Chinese conversion off", @"") stay:NO];
chineseConversionEnabled ?
NSLocalizedString(@"Chinese conversion on", @"") :
NSLocalizedString(@"Chinese conversion off", @"") stay:NO];
} }
- (void)toggleHalfWidthPunctuation:(id)sender - (void)toggleHalfWidthPunctuation:(id)sender
@ -527,7 +521,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
- (void)checkForUpdate:(id)sender - (void)checkForUpdate:(id)sender
{ {
[(AppDelegate * )[[NSApplication sharedApplication] delegate] checkForUpdateForced:YES]; [(AppDelegate *) NSApp.delegate checkForUpdateForced:YES];
} }
- (BOOL)_checkUserFiles - (BOOL)_checkUserFiles
@ -614,11 +608,11 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
InputStateChoosingCandidate *state = (InputStateChoosingCandidate *) _state; InputStateChoosingCandidate *state = (InputStateChoosingCandidate *) _state;
// candidate selected, override the node with selection // candidate selected, override the node with selection
string selectedValue = [[state.candidates objectAtIndex:index] UTF8String]; string selectedValue = [state.candidates[index] UTF8String];
[_keyHandler fixNodeWithvalue:selectedValue]; [_keyHandler fixNodeWithValue:selectedValue];
InputStateInputting *inputting = [_keyHandler _buildInputtingState]; InputStateInputting *inputting = [_keyHandler _buildInputtingState];
if ([_keyHandler inputMode] == kPlainBopomofoModeIdentifier) { if (_keyHandler.inputMode == kPlainBopomofoModeIdentifier) {
[_keyHandler clear]; [_keyHandler clear];
InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:inputting.composingBuffer]; InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:inputting.composingBuffer];
[self handleState:committing client:_currentCandidateClient]; [self handleState:committing client:_currentCandidateClient];
@ -644,8 +638,8 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
if (!state.validToWrite) { if (!state.validToWrite) {
return NO; return NO;
} }
NSString *userphrase = state.userPhrase; NSString *userPhrase = state.userPhrase;
[LanguageModelManager writeUserPhrase:userphrase]; [LanguageModelManager writeUserPhrase:userPhrase];
return YES; return YES;
} }
@ -693,8 +687,8 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
{ {
NSRect lineHeightRect = NSMakeRect(0.0, 0.0, 16.0, 16.0); NSRect lineHeightRect = NSMakeRect(0.0, 0.0, 16.0, 16.0);
NSInteger cursor = cursorIndex; NSUInteger cursor = (NSUInteger) cursorIndex;
if (cursor == [composingBuffer length] && cursor != 0) { if (cursor == composingBuffer.length && cursor != 0) {
cursor--; cursor--;
} }
@ -717,4 +711,3 @@ static inline NSString *LocalizationNotNeeded(NSString *s) {
} }
@end @end

View File

@ -27,23 +27,23 @@ import Cocoa
class InputState: NSObject { class InputState: NSObject {
} }
/// Represents that the input controller is deactive. /// Represents that the input controller is deactivated.
class InputStateDeactive: InputState { class InputStateDeactivated: InputState {
override var description: String { override var description: String {
return "<InputStateDeactive>" "<InputStateDeactivated>"
} }
} }
/// Represents that the composing buffer is empty. /// Represents that the composing buffer is empty.
class InputStateEmpty: InputState { class InputStateEmpty: InputState {
@objc var composingBuffer: String { @objc var composingBuffer: String {
"" ""
} }
} }
/// Represents that the composing buffer is empty. /// Represents that the composing buffer is empty.
class InputStateEmptyIgnoringPreviousState: InputState { class InputStateEmptyIgnoringPreviousState: InputState {
@objc var composingBuffer: String { @objc var composingBuffer: String {
"" ""
} }
} }
@ -58,7 +58,7 @@ class InputStateCommitting: InputState {
} }
override var description: String { override var description: String {
return "<InputStateCommitting poppedText:\(poppedText)>" "<InputStateCommitting poppedText:\(poppedText)>"
} }
} }
@ -73,14 +73,14 @@ class InputStateNotEmpty: InputState {
} }
override var description: String { override var description: String {
return "<InputStateNotEmpty, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex)>" "<InputStateNotEmpty, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex)>"
} }
} }
/// Represents that the user is inputting text. /// Represents that the user is inputting text.
class InputStateInputting: InputStateNotEmpty { class InputStateInputting: InputStateNotEmpty {
@objc var bpmfReading: String = "" @objc var bpmfReading: String = ""
@objc var bpmfReadingCursotIndex: UInt8 = 0 @objc var bpmfReadingCursorIndex: UInt8 = 0
@objc var poppedText: String = "" @objc var poppedText: String = ""
@objc override init(composingBuffer: String, cursorIndex: UInt) { @objc override init(composingBuffer: String, cursorIndex: UInt) {
@ -88,7 +88,7 @@ class InputStateInputting: InputStateNotEmpty {
} }
@objc var attributedString: NSAttributedString { @objc var attributedString: NSAttributedString {
let attributedSting = NSAttributedString(string: composingBuffer, attributes: [ let attributedSting = NSAttributedString(string: composingBuffer, attributes: [
.underlineStyle: NSUnderlineStyle.single.rawValue, .underlineStyle: NSUnderlineStyle.single.rawValue,
.markedClauseSegment: 0 .markedClauseSegment: 0
]) ])
@ -96,7 +96,7 @@ class InputStateInputting: InputStateNotEmpty {
} }
override var description: String { override var description: String {
return "<InputStateInputting, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex), poppedText:\(poppedText)>" "<InputStateInputting, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex), poppedText:\(poppedText)>"
} }
} }
@ -134,7 +134,7 @@ class InputStateMarking: InputStateNotEmpty {
self.markerIndex = markerIndex self.markerIndex = markerIndex
let begin = min(cursorIndex, markerIndex) let begin = min(cursorIndex, markerIndex)
let end = max(cursorIndex, markerIndex) let end = max(cursorIndex, markerIndex)
self.markedRange = NSMakeRange(Int(begin), Int(end - begin)) markedRange = NSMakeRange(Int(begin), Int(end - begin))
super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex)
} }
@ -154,12 +154,12 @@ class InputStateMarking: InputStateNotEmpty {
.underlineStyle: NSUnderlineStyle.single.rawValue, .underlineStyle: NSUnderlineStyle.single.rawValue,
.markedClauseSegment: 2 .markedClauseSegment: 2
], range: NSRange(location: end, ], range: NSRange(location: end,
length: composingBuffer.count - end)) length: composingBuffer.count - end))
return attributedSting return attributedSting
} }
override var description: String { override var description: String {
return "<InputStateMarking, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex), markedRange:\(markedRange), readings:\(readings)>" "<InputStateMarking, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex), markedRange:\(markedRange), readings:\(readings)>"
} }
@objc func convertToInputting() -> InputStateInputting { @objc func convertToInputting() -> InputStateInputting {
@ -168,7 +168,7 @@ class InputStateMarking: InputStateNotEmpty {
} }
@objc var validToWrite: Bool { @objc var validToWrite: Bool {
return self.markedRange.length >= kMinMarkRangeLength && self.markedRange.length <= kMaxMarkRangeLength markedRange.length >= kMinMarkRangeLength && markedRange.length <= kMaxMarkRangeLength
} }
@objc var userPhrase: String { @objc var userPhrase: String {
@ -192,7 +192,7 @@ class InputStateChoosingCandidate: InputStateNotEmpty {
} }
@objc var attributedString: NSAttributedString { @objc var attributedString: NSAttributedString {
let attributedSting = NSAttributedString(string: composingBuffer, attributes: [ let attributedSting = NSAttributedString(string: composingBuffer, attributes: [
.underlineStyle: NSUnderlineStyle.single.rawValue, .underlineStyle: NSUnderlineStyle.single.rawValue,
.markedClauseSegment: 0 .markedClauseSegment: 0
]) ])
@ -200,6 +200,6 @@ class InputStateChoosingCandidate: InputStateNotEmpty {
} }
override var description: String { override var description: String {
return "<InputStateChoosingCandidate, candidates:\(candidates), useVerticalMode:\(useVerticalMode), composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex)>" "<InputStateChoosingCandidate, candidates:\(candidates), useVerticalMode:\(useVerticalMode), composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex)>"
} }
} }

View File

@ -46,8 +46,8 @@ extern NSString *const kPlainBopomofoModeIdentifier;
candidateSelectionCallback:(void (^)(void))candidateSelectionCallback candidateSelectionCallback:(void (^)(void))candidateSelectionCallback
errorCallback:(void (^)(void))errorCallback; errorCallback:(void (^)(void))errorCallback;
- (void)synchWithPrefereneces; - (void)syncWithPreferences;
- (void)fixNodeWithvalue:(std::string)value; - (void)fixNodeWithValue:(std::string)value;
- (void)clear; - (void)clear;
- (InputStateInputting *)_buildInputtingState; - (InputStateInputting *)_buildInputtingState;

View File

@ -161,7 +161,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
return self; return self;
} }
- (void)synchWithPrefereneces - (void)syncWithPreferences
{ {
NSInteger layout = Preferences.keyboardLayout; NSInteger layout = Preferences.keyboardLayout;
switch (layout) { switch (layout) {
@ -190,7 +190,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
_languageModel->setExternalConverterEnabled(Preferences.chineseConversionStyle == 1); _languageModel->setExternalConverterEnabled(Preferences.chineseConversionStyle == 1);
} }
- (void)fixNodeWithvalue:(std::string)value - (void)fixNodeWithValue:(std::string)value
{ {
size_t cursorIndex = [self _actualCandidateCursorIndex]; size_t cursorIndex = [self _actualCandidateCursorIndex];
_builder->grid().fixNodeSelectedCandidate(cursorIndex, value); _builder->grid().fixNodeSelectedCandidate(cursorIndex, value);
@ -226,7 +226,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
} }
// if the composing buffer is empty and there's no reading, and there is some function key combination, we ignore it // if the composing buffer is empty and there's no reading, and there is some function key combination, we ignore it
BOOL isFunctionKey = ([input isCommandHold] || [input isControlHold] || [input isOptionlHold] || [input isNumericPad]); BOOL isFunctionKey = ([input isCommandHold] || [input isControlHold] || [input isOptionHold] || [input isNumericPad]);
if (![state isKindOfClass:[InputStateNotEmpty class]] && isFunctionKey) { if (![state isKindOfClass:[InputStateNotEmpty class]] && isFunctionKey) {
return NO; return NO;
} }
@ -258,7 +258,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
} }
if ([input isNumericPad]) { if ([input isNumericPad]) {
if (![input isLeft] && ![input isRight] && ![input isDown] && ![input isUp] && charCode != 32 && isprint(charCode)) { if (![input isLeft] && ![input isRight] && ![input isDown] && ![input isUp] && charCode != 32 && isprint(charCode)) {
[self clear]; [self clear];
InputStateEmpty *emptyState = [[InputStateEmpty alloc] init]; InputStateEmpty *emptyState = [[InputStateEmpty alloc] init];
stateCallback(emptyState); stateCallback(emptyState);
@ -276,8 +276,8 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// MARK: Handle Marking // MARK: Handle Marking
if ([state isKindOfClass:[InputStateMarking class]]) { if ([state isKindOfClass:[InputStateMarking class]]) {
InputStateMarking *marking = (InputStateMarking *)state; InputStateMarking *marking = (InputStateMarking *) state;
if ([self _handleMarkingState:(InputStateMarking *)state input:input stateCallback:stateCallback candidateSelectionCallback:candidateSelectionCallback errorCallback:errorCallback]) { if ([self _handleMarkingState:(InputStateMarking *) state input:input stateCallback:stateCallback candidateSelectionCallback:candidateSelectionCallback errorCallback:errorCallback]) {
return YES; return YES;
} }
state = [marking convertToInputting]; state = [marking convertToInputting];
@ -325,7 +325,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// get user override model suggestion // get user override model suggestion
string overrideValue = (_inputMode == kPlainBopomofoModeIdentifier) ? "" : string overrideValue = (_inputMode == kPlainBopomofoModeIdentifier) ? "" :
_userOverrideModel->suggest(_walkedNodes, _builder->cursorIndex(), [[NSDate date] timeIntervalSince1970]); _userOverrideModel->suggest(_walkedNodes, _builder->cursorIndex(), [[NSDate date] timeIntervalSince1970]);
if (!overrideValue.empty()) { if (!overrideValue.empty()) {
size_t cursorIndex = [self _actualCandidateCursorIndex]; size_t cursorIndex = [self _actualCandidateCursorIndex];
@ -353,15 +353,15 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// MARK: Space and Down // MARK: Space and Down
// keyCode 125 = Down, charCode 32 = Space // keyCode 125 = Down, charCode 32 = Space
if (_bpmfReadingBuffer->isEmpty() && if (_bpmfReadingBuffer->isEmpty() &&
[state isKindOfClass:[InputStateNotEmpty class]] && [state isKindOfClass:[InputStateNotEmpty class]] &&
([input isExtraChooseCandidateKey] || charCode == 32 || (input.useVerticalMode && ([input isVerticalModeOnlyChooseCandidateKey])))) { ([input isExtraChooseCandidateKey] || charCode == 32 || (input.useVerticalMode && ([input isVerticalModeOnlyChooseCandidateKey])))) {
if (charCode == 32) { if (charCode == 32) {
// if the spacebar is NOT set to be a selection key // if the spacebar is NOT set to be a selection key
if ([input isShiftHold] || !Preferences.chooseCandidateUsingSpace) { if ([input isShiftHold] || !Preferences.chooseCandidateUsingSpace) {
if (_builder->cursorIndex() >= _builder->length()) { if (_builder->cursorIndex() >= _builder->length()) {
[self clear]; [self clear];
InputStateCommitting *commiting = [[InputStateCommitting alloc] initWithPoppedText:@" "]; InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:@" "];
stateCallback(commiting); stateCallback(committing);
InputStateEmpty *empty = [[InputStateEmpty alloc] init]; InputStateEmpty *empty = [[InputStateEmpty alloc] init];
stateCallback(empty); stateCallback(empty);
} else if (_languageModel->hasUnigramsForKey(" ")) { } else if (_languageModel->hasUnigramsForKey(" ")) {
@ -436,8 +436,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
stateCallback(inputting); stateCallback(inputting);
InputStateChoosingCandidate *choosingCandidate = [self _buildCandidateState:inputting useVerticalMode:input.useVerticalMode]; InputStateChoosingCandidate *choosingCandidate = [self _buildCandidateState:inputting useVerticalMode:input.useVerticalMode];
stateCallback(choosingCandidate); stateCallback(choosingCandidate);
} } else { // If there is still unfinished bpmf reading, ignore the punctuation
else { // If there is still unfinished bpmf reading, ignore the punctuation
errorCallback(); errorCallback();
} }
return YES; return YES;
@ -483,7 +482,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
BOOL escToClearInputBufferEnabled = Preferences.escToCleanInputBuffer; BOOL escToClearInputBufferEnabled = Preferences.escToCleanInputBuffer;
if (escToClearInputBufferEnabled) { if (escToClearInputBufferEnabled) {
// if the optioon is enabled, we clear everythiong including the composing // if the option is enabled, we clear everything including the composing
// buffer, walked nodes and the reading. // buffer, walked nodes and the reading.
[self clear]; [self clear];
InputStateEmpty *empty = [[InputStateEmpty alloc] init]; InputStateEmpty *empty = [[InputStateEmpty alloc] init];
@ -712,7 +711,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
return NO; return NO;
} }
NSString *poppedText = @""; NSString *poppedText;
if (_bpmfReadingBuffer->isEmpty()) { if (_bpmfReadingBuffer->isEmpty()) {
_builder->insertReadingAtCursor(customPunctuation); _builder->insertReadingAtCursor(customPunctuation);
poppedText = [self _popOverflowComposingTextAndWalk]; poppedText = [self _popOverflowComposingTextAndWalk];
@ -721,6 +720,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
stateCallback(state); stateCallback(state);
return YES; return YES;
} }
InputStateInputting *inputting = [self _buildInputtingState]; InputStateInputting *inputting = [self _buildInputtingState];
inputting.poppedText = poppedText; inputting.poppedText = poppedText;
stateCallback(inputting); stateCallback(inputting);
@ -750,7 +750,6 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
{ {
UniChar charCode = input.charCode; UniChar charCode = input.charCode;
if (charCode == 27) { if (charCode == 27) {
InputStateInputting *inputting = [self _buildInputtingState]; InputStateInputting *inputting = [self _buildInputtingState];
stateCallback(inputting); stateCallback(inputting);
@ -770,7 +769,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// Shift + left // Shift + left
if (([input isCursorBackward] || input.emacsKey == McBopomofoEmacsKeyBackward) if (([input isCursorBackward] || input.emacsKey == McBopomofoEmacsKeyBackward)
&& ([input isShiftHold])) { && ([input isShiftHold])) {
NSUInteger index = state.markerIndex; NSUInteger index = state.markerIndex;
if (index > 0) { if (index > 0) {
index -= 1; index -= 1;
@ -786,7 +785,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// Shift + Right // Shift + Right
if (([input isCursorForward] || input.emacsKey == McBopomofoEmacsKeyForward) if (([input isCursorForward] || input.emacsKey == McBopomofoEmacsKeyForward)
&& ([input isShiftHold])) { && ([input isShiftHold])) {
NSUInteger index = state.markerIndex; NSUInteger index = state.markerIndex;
if (index < state.composingBuffer.length) { if (index < state.composingBuffer.length) {
index += 1; index += 1;
@ -814,7 +813,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
VTCandidateController *gCurrentCandidateController = [self.delegate candidateControllerForKeyHanlder:self]; VTCandidateController *gCurrentCandidateController = [self.delegate candidateControllerForKeyHanlder:self];
BOOL cancelCandidateKey = (charCode == 27) || [input isDelete] || BOOL cancelCandidateKey = (charCode == 27) || [input isDelete] ||
((_inputMode == kPlainBopomofoModeIdentifier) && (charCode == 8)); ((_inputMode == kPlainBopomofoModeIdentifier) && (charCode == 8));
if (cancelCandidateKey) { if (cancelCandidateKey) {
if (_inputMode == kPlainBopomofoModeIdentifier) { if (_inputMode == kPlainBopomofoModeIdentifier) {
@ -979,7 +978,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
string punctuation = string("_punctuation_") + string(1, (char) charCode); string punctuation = string("_punctuation_") + string(1, (char) charCode);
BOOL shouldAutoSelectCandidate = _bpmfReadingBuffer->isValidKey((char) charCode) || _languageModel->hasUnigramsForKey(customPunctuation) || BOOL shouldAutoSelectCandidate = _bpmfReadingBuffer->isValidKey((char) charCode) || _languageModel->hasUnigramsForKey(customPunctuation) ||
_languageModel->hasUnigramsForKey(punctuation); _languageModel->hasUnigramsForKey(punctuation);
if (shouldAutoSelectCandidate) { if (shouldAutoSelectCandidate) {
NSUInteger candidateIndex = [gCurrentCandidateController candidateIndexAtKeyLabelIndex:0]; NSUInteger candidateIndex = [gCurrentCandidateController candidateIndexAtKeyLabelIndex:0];
@ -1026,7 +1025,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// (the actual cursor on the screen) with the builder's logical // (the actual cursor on the screen) with the builder's logical
// cursor (reading) cursor; each built node has a "spanning length" // cursor (reading) cursor; each built node has a "spanning length"
// (e.g. two reading blocks has a spanning length of 2), and we // (e.g. two reading blocks has a spanning length of 2), and we
// accumulate those lengthes to calculate the displayed cursor // accumulate those lengths to calculate the displayed cursor
// index // index
size_t spanningLength = (*wi).spanningLength; size_t spanningLength = (*wi).spanningLength;
if (readingCursorIndex + spanningLength <= builderCursorIndex) { if (readingCursorIndex + spanningLength <= builderCursorIndex) {
@ -1083,7 +1082,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// but because the Viterbi algorithm has a complexity of O(N^2), // but because the Viterbi algorithm has a complexity of O(N^2),
// the walk will become slower as the number of nodes increase, // the walk will become slower as the number of nodes increase,
// therefore we need to "pop out" overflown text -- they usually // therefore we need to "pop out" overflown text -- they usually
// lose their influence over the whole MLE anyway -- so tht when // lose their influence over the whole MLE anyway -- so that when
// the user type along, the already composed text at front will // the user type along, the already composed text at front will
// be popped out // be popped out
@ -1145,7 +1144,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
{ {
NSMutableArray *readingsArray = [[NSMutableArray alloc] init]; NSMutableArray *readingsArray = [[NSMutableArray alloc] init];
vector<std::string> v = _builder->readings(); vector<std::string> v = _builder->readings();
for(vector<std::string>::iterator it_i=v.begin(); it_i!=v.end(); ++it_i) { for (vector<std::string>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
[readingsArray addObject:[NSString stringWithUTF8String:it_i->c_str()]]; [readingsArray addObject:[NSString stringWithUTF8String:it_i->c_str()]];
} }
return readingsArray; return readingsArray;

View File

@ -37,7 +37,7 @@ enum KeyCode: UInt16 {
case delete = 117 case delete = 117
} }
@objc class KeyHandlerInput: NSObject { class KeyHandlerInput: NSObject {
@objc private (set) var useVerticalMode: Bool @objc private (set) var useVerticalMode: Bool
@objc private (set) var inputText: String? @objc private (set) var inputText: String?
@objc private (set) var charCode: UInt16 @objc private (set) var charCode: UInt16
@ -55,21 +55,21 @@ enum KeyCode: UInt16 {
self.keyCode = keyCode self.keyCode = keyCode
self.charCode = charCode self.charCode = charCode
self.flags = flags self.flags = flags
self.useVerticalMode = isVerticalMode useVerticalMode = isVerticalMode
self.emacsKey = EmacsKeyHelper.detect(charCode: charCode, flags: flags) emacsKey = EmacsKeyHelper.detect(charCode: charCode, flags: flags)
self.cursorForwardKey = useVerticalMode ? .down : .right cursorForwardKey = useVerticalMode ? .down : .right
self.cursorBackwardKey = useVerticalMode ? .up : .left cursorBackwardKey = useVerticalMode ? .up : .left
self.extraChooseCandidateKey = useVerticalMode ? .left : .down extraChooseCandidateKey = useVerticalMode ? .left : .down
self.absorbedArrowKey = useVerticalMode ? .right : .up absorbedArrowKey = useVerticalMode ? .right : .up
self.verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none
super.init() super.init()
} }
@objc init(event: NSEvent, isVerticalMode: Bool) { @objc init(event: NSEvent, isVerticalMode: Bool) {
self.inputText = event.characters inputText = event.characters
self.keyCode = event.keyCode keyCode = event.keyCode
self.flags = event.modifierFlags flags = event.modifierFlags
self.useVerticalMode = isVerticalMode useVerticalMode = isVerticalMode
let charCode: UInt16 = { let charCode: UInt16 = {
guard let inputText = event.characters, inputText.count > 0 else { guard let inputText = event.characters, inputText.count > 0 else {
return 0 return 0
@ -78,98 +78,98 @@ enum KeyCode: UInt16 {
return first return first
}() }()
self.charCode = charCode self.charCode = charCode
self.emacsKey = EmacsKeyHelper.detect(charCode: charCode, flags: event.modifierFlags) emacsKey = EmacsKeyHelper.detect(charCode: charCode, flags: event.modifierFlags)
self.cursorForwardKey = useVerticalMode ? .down : .right cursorForwardKey = useVerticalMode ? .down : .right
self.cursorBackwardKey = useVerticalMode ? .up : .left cursorBackwardKey = useVerticalMode ? .up : .left
self.extraChooseCandidateKey = useVerticalMode ? .left : .down extraChooseCandidateKey = useVerticalMode ? .left : .down
self.absorbedArrowKey = useVerticalMode ? .right : .up absorbedArrowKey = useVerticalMode ? .right : .up
self.verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none
super.init() super.init()
NSLog("self.keyCode \(self.keyCode)") NSLog("self.keyCode \(keyCode)")
} }
@objc var isShiftHold: Bool { @objc var isShiftHold: Bool {
self.flags.contains([.shift]) flags.contains([.shift])
} }
@objc var isCommandHold: Bool { @objc var isCommandHold: Bool {
self.flags.contains([.command]) flags.contains([.command])
} }
@objc var isControlHold: Bool { @objc var isControlHold: Bool {
self.flags.contains([.control]) flags.contains([.control])
} }
@objc var isOptionlHold: Bool { @objc var isOptionHold: Bool {
self.flags.contains([.option]) flags.contains([.option])
} }
@objc var isCapsLockOn: Bool { @objc var isCapsLockOn: Bool {
self.flags.contains([.capsLock]) flags.contains([.capsLock])
} }
@objc var isNumericPad: Bool { @objc var isNumericPad: Bool {
self.flags.contains([.numericPad]) flags.contains([.numericPad])
} }
@objc var isEnter: Bool { @objc var isEnter: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.enter KeyCode(rawValue: keyCode) == KeyCode.enter
} }
@objc var isUp: Bool { @objc var isUp: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.up KeyCode(rawValue: keyCode) == KeyCode.up
} }
@objc var isDown: Bool { @objc var isDown: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.down KeyCode(rawValue: keyCode) == KeyCode.down
} }
@objc var isLeft: Bool { @objc var isLeft: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.left KeyCode(rawValue: keyCode) == KeyCode.left
} }
@objc var isRight: Bool { @objc var isRight: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.right KeyCode(rawValue: keyCode) == KeyCode.right
} }
@objc var isPageUp: Bool { @objc var isPageUp: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.pageUp KeyCode(rawValue: keyCode) == KeyCode.pageUp
} }
@objc var isPageDown: Bool { @objc var isPageDown: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.pageDown KeyCode(rawValue: keyCode) == KeyCode.pageDown
} }
@objc var isHome: Bool { @objc var isHome: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.home KeyCode(rawValue: keyCode) == KeyCode.home
} }
@objc var isEnd: Bool { @objc var isEnd: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.end KeyCode(rawValue: keyCode) == KeyCode.end
} }
@objc var isDelete: Bool { @objc var isDelete: Bool {
KeyCode(rawValue: self.keyCode) == KeyCode.delete KeyCode(rawValue: keyCode) == KeyCode.delete
} }
@objc var isCursorBackward: Bool { @objc var isCursorBackward: Bool {
KeyCode(rawValue: self.keyCode) == cursorBackwardKey KeyCode(rawValue: keyCode) == cursorBackwardKey
} }
@objc var isCursorForward: Bool { @objc var isCursorForward: Bool {
KeyCode(rawValue: self.keyCode) == cursorForwardKey KeyCode(rawValue: keyCode) == cursorForwardKey
} }
@objc var isAbsorbedArrowKey: Bool { @objc var isAbsorbedArrowKey: Bool {
KeyCode(rawValue: self.keyCode) == absorbedArrowKey KeyCode(rawValue: keyCode) == absorbedArrowKey
} }
@objc var isExtraChooseCandidateKey: Bool { @objc var isExtraChooseCandidateKey: Bool {
KeyCode(rawValue: self.keyCode) == extraChooseCandidateKey KeyCode(rawValue: keyCode) == extraChooseCandidateKey
} }
@objc var isVerticalModeOnlyChooseCandidateKey: Bool { @objc var isVerticalModeOnlyChooseCandidateKey: Bool {
KeyCode(rawValue: self.keyCode) == verticalModeOnlyChooseCandidateKey KeyCode(rawValue: keyCode) == verticalModeOnlyChooseCandidateKey
} }
} }

View File

@ -22,20 +22,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#import "LanguageModelManager.h" #import "LanguageModelManager.h"
#import <fstream>
#import <iostream>
#import <set>
#import "OVStringHelper.h"
#import "OVUTF8Helper.h"
#import "McBopomofo-Swift.h" #import "McBopomofo-Swift.h"
@import VXHanConvert; @import VXHanConvert;
@import OpenCCBridge; @import OpenCCBridge;
using namespace std; using namespace std;
using namespace Formosa::Gramambular;
using namespace McBopomofo; using namespace McBopomofo;
using namespace OpenVanilla;
static const int kUserOverrideModelCapacity = 500; static const int kUserOverrideModelCapacity = 500;
static const double kObservedOverrideHalflife = 5400.0; // 1.5 hr. static const double kObservedOverrideHalflife = 5400.0; // 1.5 hr.
@ -218,7 +211,7 @@ static void LTLoadLanguageModelFile(NSString *filenameWithoutExtension, McBopomo
+ (NSString *)dataFolderPath + (NSString *)dataFolderPath
{ {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDirectory, YES); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDirectory, YES);
NSString *appSupportPath = [paths objectAtIndex:0]; NSString *appSupportPath = paths[0];
NSString *userDictPath = [appSupportPath stringByAppendingPathComponent:@"McBopomofo"]; NSString *userDictPath = [appSupportPath stringByAppendingPathComponent:@"McBopomofo"];
return userDictPath; return userDictPath;
} }