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. /// Enables or disables phrase replacement.
void setPhraseReplacementEnabled(bool enabled); void setPhraseReplacementEnabled(bool enabled);
/// If phrease replacement is enabled or not. /// If phrase replacement is enabled or not.
bool phraseReplacementEnabled(); bool phraseReplacementEnabled();
/// Enables or disables the external converter. /// Enables or disables the external converter.

View File

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

View File

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

View File

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