Splits Input Method controller into two classes.

This commit is contained in:
zonble 2022-01-28 15:02:00 +08:00
parent dd803b6c31
commit 0bc9468ba2
7 changed files with 1308 additions and 1127 deletions

View File

@ -59,6 +59,7 @@
D47F7DD3278C1263002F9DD7 /* UserOverrideModel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DD2278C1263002F9DD7 /* UserOverrideModel.cpp */; };
D485D3B92796A8A000657FF3 /* PreferencesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D485D3B82796A8A000657FF3 /* PreferencesTests.swift */; };
D485D3C02796CE3200657FF3 /* VersionUpdateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D485D3BF2796CE3200657FF3 /* VersionUpdateTests.swift */; };
D4E569DC27A34D0E00AC2CEF /* KeyHandler.mm in Sources */ = {isa = PBXBuildFile; fileRef = D4E569DB27A34CC100AC2CEF /* KeyHandler.mm */; };
D4F0BBDF279AF1AF0071253C /* ArchiveUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4F0BBDE279AF1AF0071253C /* ArchiveUtil.swift */; };
D4F0BBE1279AF8B30071253C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4F0BBE0279AF8B30071253C /* AppDelegate.swift */; };
D4F0BBE4279B08900071253C /* BundleTranslocate.m in Sources */ = {isa = PBXBuildFile; fileRef = D4F0BBE3279B08900071253C /* BundleTranslocate.m */; };
@ -214,6 +215,8 @@
D485D3B62796A8A000657FF3 /* McBopomofoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = McBopomofoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D485D3B82796A8A000657FF3 /* PreferencesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesTests.swift; sourceTree = "<group>"; };
D485D3BF2796CE3200657FF3 /* VersionUpdateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionUpdateTests.swift; sourceTree = "<group>"; };
D4E569DA27A34CC100AC2CEF /* KeyHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeyHandler.h; sourceTree = "<group>"; };
D4E569DB27A34CC100AC2CEF /* KeyHandler.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = KeyHandler.mm; sourceTree = "<group>"; };
D4F0BBDE279AF1AF0071253C /* ArchiveUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArchiveUtil.swift; sourceTree = "<group>"; };
D4F0BBE0279AF8B30071253C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D4F0BBE2279B08900071253C /* BundleTranslocate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BundleTranslocate.h; sourceTree = "<group>"; };
@ -301,8 +304,10 @@
6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */,
D41355D6278D7409005E5CBD /* LanguageModelManager.h */,
D41355D7278D7409005E5CBD /* LanguageModelManager.mm */,
D461B791279DAC010070E734 /* InputState.swift */,
D4E569DA27A34CC100AC2CEF /* KeyHandler.h */,
D4E569DB27A34CC100AC2CEF /* KeyHandler.mm */,
D456576D279E4F7B00DF6BC9 /* KeyHandlerInput.swift */,
D461B791279DAC010070E734 /* InputState.swift */,
D427F76B278CA1BA004A2160 /* AppDelegate.swift */,
D44FB74427915555003C80A6 /* Preferences.swift */,
D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */,
@ -703,6 +708,7 @@
D47B92C027972AD100458394 /* main.swift in Sources */,
D44FB74D2792189A003C80A6 /* PhraseReplacementMap.cpp in Sources */,
D44FB74527915565003C80A6 /* Preferences.swift in Sources */,
D4E569DC27A34D0E00AC2CEF /* KeyHandler.mm in Sources */,
D47F7DD0278C0897002F9DD7 /* NonModalAlertWindowController.swift in Sources */,
D456576E279E4F7B00DF6BC9 /* KeyHandlerInput.swift in Sources */,
D47F7DCE278BFB57002F9DD7 /* PreferencesWindowController.swift in Sources */,

View File

@ -23,35 +23,9 @@
#import <Cocoa/Cocoa.h>
#import <InputMethodKit/InputMethodKit.h>
#import "Mandarin.h"
#import "Gramambular.h"
#import "McBopomofoLM.h"
#import "UserOverrideModel.h"
#import "McBopomofo-Swift.h"
@interface McBopomofoInputMethodController : IMKInputController {
@private
// the reading buffer that takes user input
Formosa::Mandarin::BopomofoReadingBuffer *_bpmfReadingBuffer;
// language model
McBopomofo::McBopomofoLM *_languageModel;
// user override model
McBopomofo::UserOverrideModel *_userOverrideModel;
// the grid (lattice) builder for the unigrams (and bigrams)
Formosa::Gramambular::BlockReadingBuilder *_builder;
// latest walked path (trellis) using the Viterbi algorithm
std::vector<Formosa::Gramambular::NodeAnchor> _walkedNodes;
}
- (BOOL)handleInput:(KeyHandlerInput *)input
state:(InputState *)state
stateCallback:(void (^)(InputState *))stateCallback
candidateSelectionCallback:(void (^)(void))candidateSelectionCallback
errorCallback:(void (^)(void))errorCallback;
@interface McBopomofoInputMethodController : IMKInputController
- (void)handleState:(InputState *)newState client:(id)client;

File diff suppressed because it is too large Load Diff

View File

@ -41,6 +41,13 @@ class InputStateEmpty: InputState {
}
}
/// Represents that the composing buffer is empty.
class InputStateEmptyIgnoringPreviousState: InputState {
@objc var composingBuffer: String {
""
}
}
/// Represents that the input controller is committing text into client app.
class InputStateCommitting: InputState {
@objc private(set) var poppedText: String = ""
@ -159,6 +166,18 @@ class InputStateMarking: InputStateNotEmpty {
let state = InputStateInputting(composingBuffer: composingBuffer, cursorIndex: cursorIndex)
return state
}
@objc var validToWrite: Bool {
return self.markedRange.length >= kMinMarkRangeLength && self.markedRange.length <= kMaxMarkRangeLength
}
@objc var userPhrase: String {
let text = (composingBuffer as NSString).substring(with: markedRange)
let end = markedRange.location + markedRange.length
let readings = readings[markedRange.location..<end]
let joined = readings.joined(separator: "-")
return "\(text) \(joined)"
}
}
/// Represents that the user is choosing in a candidates list.

59
Source/KeyHandler.h Normal file
View File

@ -0,0 +1,59 @@
// Copyright (c) 2011 and onwards The McBopomofo Authors.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import "McBopomofo-Swift.h"
@import CandidateUI;
NS_ASSUME_NONNULL_BEGIN
extern NSString *const kBopomofoModeIdentifier;
extern NSString *const kPlainBopomofoModeIdentifier;
@class KeyHandler;
@protocol KeyHandlerDelegate <NSObject>
- (VTCandidateController *)candidateControllerForKeyHanlder:(KeyHandler *)keyHandler;
- (void)keyHandler:(KeyHandler *)keyHandler didSelectCandidateAtIndex:(NSInteger)index candidateController:(VTCandidateController *)controller;
- (BOOL)keyHandler:(KeyHandler *)keyHandler didRequestWriteUserPhraseWithState:(InputStateMarking *)state;
@end
@interface KeyHandler : NSObject
- (BOOL)handleInput:(KeyHandlerInput *)input
state:(InputState *)state
stateCallback:(void (^)(InputState *))stateCallback
candidateSelectionCallback:(void (^)(void))candidateSelectionCallback
errorCallback:(void (^)(void))errorCallback;
- (void)synchWithPrefereneces;
- (void)fixNodeWithvalue:(std::string)value;
- (void)clear;
- (InputStateInputting *)_buildInputtingState;
@property (strong, nonatomic) NSString *inputMode;
@property (weak, nonatomic) id <KeyHandlerDelegate> delegate;
@end
NS_ASSUME_NONNULL_END

1154
Source/KeyHandler.mm Normal file

File diff suppressed because it is too large Load Diff

View File

@ -174,7 +174,7 @@ static void LTLoadLanguageModelFile(NSString *filenameWithoutExtension, McBopomo
return NO;
}
BOOL shuoldAddLineBreakAtFront = NO;
BOOL addLineBreakAtFront = NO;
NSString *path = [self userPhrasesDataPathMcBopomofo];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
@ -188,7 +188,7 @@ static void LTLoadLanguageModelFile(NSString *filenameWithoutExtension, McBopomo
NSData *data = [readFile readDataToEndOfFile];
const void *bytes = [data bytes];
if (*(char *)bytes != '\n') {
shuoldAddLineBreakAtFront = YES;
addLineBreakAtFront = YES;
}
[readFile closeFile];
}
@ -196,7 +196,7 @@ static void LTLoadLanguageModelFile(NSString *filenameWithoutExtension, McBopomo
}
NSMutableString *currentMarkedPhrase = [NSMutableString string];
if (shuoldAddLineBreakAtFront) {
if (addLineBreakAtFront) {
[currentMarkedPhrase appendString:@"\n"];
}
[currentMarkedPhrase appendString:userPhrase];