From c3d953c618ab19c52680356e404008c8ce14017d Mon Sep 17 00:00:00 2001 From: zonble Date: Sun, 30 Jan 2022 05:42:16 +0800 Subject: [PATCH] Converts input mode into a typed enum. --- Source/Engine/McBopomofoLM.h | 2 +- Source/InputMethodController.swift | 8 ++++---- Source/KeyHandler.h | 7 ++++--- Source/KeyHandler.mm | 26 +++++++++++++------------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/Source/Engine/McBopomofoLM.h b/Source/Engine/McBopomofoLM.h index de90a4a3..31bcb0f2 100644 --- a/Source/Engine/McBopomofoLM.h +++ b/Source/Engine/McBopomofoLM.h @@ -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. diff --git a/Source/InputMethodController.swift b/Source/InputMethodController.swift index c0d83dec..fa09c84e 100644 --- a/Source/InputMethodController.swift +++ b/Source/InputMethodController.swift @@ -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) diff --git a/Source/KeyHandler.h b/Source/KeyHandler.h index b0e14728..7613785c 100644 --- a/Source/KeyHandler.h +++ b/Source/KeyHandler.h @@ -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 delegate; @end diff --git a/Source/KeyHandler.mm b/Source/KeyHandler.mm index be12831d..478753ea 100644 --- a/Source/KeyHandler.mm +++ b/Source/KeyHandler.mm @@ -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);