Converts input mode into a typed enum.

This commit is contained in:
zonble 2022-01-30 05:42:16 +08:00
parent 0b1975bfeb
commit c3d953c618
4 changed files with 22 additions and 21 deletions

View File

@ -84,7 +84,7 @@ public:
/// Enables or disables phrase replacement.
void setPhraseReplacementEnabled(bool enabled);
/// If phrease replacement is enabled or not.
/// If phrase replacement is enabled or not.
bool phraseReplacementEnabled();
/// Enables or disables the external converter.

View File

@ -76,7 +76,7 @@ class McBopomofoInputMethodController: IMKInputController {
let inputMode = keyHandler.inputMode
let optionKeyPressed = NSEvent.modifierFlags.contains(.option)
if inputMode == kBopomofoModeIdentifier && optionKeyPressed {
if inputMode == .bopomofo && optionKeyPressed {
let phaseReplacementItem = menu.addItem(withTitle: NSLocalizedString("Use Phrase Replacement", comment: ""), action: #selector(togglePhraseReplacement(_:)), keyEquivalent: "")
phaseReplacementItem.state = Preferences.phraseReplacementEnabled.state
}
@ -84,7 +84,7 @@ class McBopomofoInputMethodController: IMKInputController {
menu.addItem(NSMenuItem.separator())
menu.addItem(withTitle: NSLocalizedString("User Phrases", comment: ""), action: nil, keyEquivalent: "")
if inputMode == kPlainBopomofoModeIdentifier {
if inputMode == .plainBopomofo {
menu.addItem(withTitle: NSLocalizedString("Edit Excluded Phrases", comment: ""), action: #selector(openExcludedPhrasesPlainBopomofo(_:)), keyEquivalent: "")
} else {
menu.addItem(withTitle: NSLocalizedString("Edit User Phrases", comment: ""), action: #selector(openUserPhrases(_:)), keyEquivalent: "")
@ -126,7 +126,7 @@ class McBopomofoInputMethodController: IMKInputController {
}
override func setValue(_ value: Any!, forTag tag: Int, client: Any!) {
let newInputMode = value as? String ?? kBopomofoModeIdentifier
let newInputMode = InputMode(rawValue: value as? String ?? InputMode.bopomofo.rawValue)
if keyHandler.inputMode != newInputMode {
UserDefaults.standard.synchronize()
// Remember to override the keyboard layout again -- treat this as an activate event.
@ -535,7 +535,7 @@ extension McBopomofoInputMethodController: CandidateControllerDelegate {
return
}
if keyHandler.inputMode == kPlainBopomofoModeIdentifier {
if keyHandler.inputMode == .plainBopomofo {
keyHandler.clear()
handle(state: .Committing(poppedText: inputting.composingBuffer), client: currentCandidateClient)
handle(state: .Empty(), client: currentDeferredClient)

View File

@ -28,8 +28,9 @@
NS_ASSUME_NONNULL_BEGIN
extern NSString *const kBopomofoModeIdentifier;
extern NSString *const kPlainBopomofoModeIdentifier;
typedef NSString *const InputMode NS_TYPED_ENUM;
extern InputMode InputModeBopomofo;
extern InputMode InputModePlainBopomofo;
@class KeyHandler;
@ -53,7 +54,7 @@ candidateSelectionCallback:(void (^)(void))candidateSelectionCallback
- (InputState *)buildInputtingState;
@property (strong, nonatomic) NSString *inputMode;
@property (strong, nonatomic) InputMode inputMode;
@property (weak, nonatomic) id <KeyHandlerDelegate> delegate;
@end

View File

@ -40,8 +40,8 @@ using namespace Formosa::Gramambular;
using namespace McBopomofo;
using namespace OpenVanilla;
NSString *const kBopomofoModeIdentifier = @"org.openvanilla.inputmethod.McBopomofo.Bopomofo";
NSString *const kPlainBopomofoModeIdentifier = @"org.openvanilla.inputmethod.McBopomofo.PlainBopomofo";
InputMode InputModeBopomofo = @"org.openvanilla.inputmethod.McBopomofo.Bopomofo";
InputMode InputModePlainBopomofo = @"org.openvanilla.inputmethod.McBopomofo.PlainBopomofo";
static const double kEpsilon = 0.000001;
@ -105,12 +105,12 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
NSString *newInputMode;
McBopomofoLM *newLanguageModel;
if ([value isKindOfClass:[NSString class]] && [value isEqual:kPlainBopomofoModeIdentifier]) {
newInputMode = kPlainBopomofoModeIdentifier;
if ([value isKindOfClass:[NSString class]] && [value isEqual:InputModePlainBopomofo]) {
newInputMode = InputModePlainBopomofo;
newLanguageModel = [LanguageModelManager languageModelPlainBopomofo];
newLanguageModel->setPhraseReplacementEnabled(false);
} else {
newInputMode = kBopomofoModeIdentifier;
newInputMode = InputModeBopomofo;
newLanguageModel = [LanguageModelManager languageModelMcBopomofo];
newLanguageModel->setPhraseReplacementEnabled(Preferences.phraseReplacementEnabled);
}
@ -160,7 +160,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// each Mandarin syllable is separated by a hyphen
_builder->setJoinSeparator("-");
_inputMode = kBopomofoModeIdentifier;
_inputMode = InputModeBopomofo;
}
return self;
}
@ -199,7 +199,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
size_t cursorIndex = [self _actualCandidateCursorIndex];
string stringValue = [value UTF8String];
_builder->grid().fixNodeSelectedCandidate(cursorIndex, stringValue);
if (_inputMode != kPlainBopomofoModeIdentifier) {
if (_inputMode != InputModePlainBopomofo) {
_userOverrideModel->observe(_walkedNodes, cursorIndex, stringValue, [[NSDate date] timeIntervalSince1970]);
}
[self _walk];
@ -329,7 +329,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
NSString *poppedText = [self _popOverflowComposingTextAndWalk];
// get user override model suggestion
string overrideValue = (_inputMode == kPlainBopomofoModeIdentifier) ? "" :
string overrideValue = (_inputMode == InputModePlainBopomofo) ? "" :
_userOverrideModel->suggest(_walkedNodes, _builder->cursorIndex(), [[NSDate date] timeIntervalSince1970]);
if (!overrideValue.empty()) {
@ -346,7 +346,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
inputting.poppedText = poppedText;
stateCallback(inputting);
if (_inputMode == kPlainBopomofoModeIdentifier) {
if (_inputMode == InputModePlainBopomofo) {
InputStateChoosingCandidate *choosingCandidates = [self _buildCandidateState:inputting useVerticalMode:input.useVerticalMode];
if (choosingCandidates.candidates.count == 1) {
[self clear];
@ -712,7 +712,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
- (BOOL)_handleEnterWithState:(InputState *)state stateCallback:(void (^)(InputState *))stateCallback errorCallback:(void (^)(void))errorCallback
{
if ([state isKindOfClass:[InputStateInputting class]]) {
if (_inputMode == kPlainBopomofoModeIdentifier) {
if (_inputMode == InputModePlainBopomofo) {
if (!_bpmfReadingBuffer->isEmpty()) {
errorCallback();
}
@ -753,7 +753,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
inputting.poppedText = poppedText;
stateCallback(inputting);
if (_inputMode == kPlainBopomofoModeIdentifier && _bpmfReadingBuffer->isEmpty()) {
if (_inputMode == InputModePlainBopomofo && _bpmfReadingBuffer->isEmpty()) {
InputStateChoosingCandidate *candidateState = [self _buildCandidateState:inputting useVerticalMode:useVerticalMode];
if ([candidateState.candidates count] == 1) {
@ -841,7 +841,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
BOOL cancelCandidateKey = (charCode == 27) || (charCode == 8) || [input isDelete];
if (cancelCandidateKey) {
if (_inputMode == kPlainBopomofoModeIdentifier) {
if (_inputMode == InputModePlainBopomofo) {
[self clear];
InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init];
stateCallback(empty);
@ -997,7 +997,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
}
}
if (_inputMode == kPlainBopomofoModeIdentifier) {
if (_inputMode == InputModePlainBopomofo) {
string layout = [self _currentLayout];
string punctuationNamePrefix = Preferences.halfWidthPunctuationEnabled ? string("_half_punctuation_") : string("_punctuation_");
string customPunctuation = punctuationNamePrefix + layout + string(1, (char) charCode);