From 9612aa6ba03703eb958ed1ff5236c167299b1608 Mon Sep 17 00:00:00 2001 From: zonble Date: Fri, 28 Jan 2022 16:51:25 +0800 Subject: [PATCH] Code clean-up. --- Source/InputMethodController.mm | 2 +- Source/InputState.swift | 37 ++++++++++++++++++++++++++++++--- Source/KeyHandler.h | 2 +- Source/KeyHandler.mm | 16 ++++++-------- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index 9c76008f..2ed9fe72 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -628,7 +628,7 @@ static inline NSString *LocalizationNotNeeded(NSString *s) { @implementation McBopomofoInputMethodController (KeyHandlerDelegate) -- (nonnull VTCandidateController *)candidateControllerForKeyHanlder:(nonnull KeyHandler *)keyHandler +- (nonnull VTCandidateController *)candidateControllerForKeyHandler:(nonnull KeyHandler *)keyHandler { return gCurrentCandidateController; } diff --git a/Source/InputState.swift b/Source/InputState.swift index 506178b2..4266f562 100644 --- a/Source/InputState.swift +++ b/Source/InputState.swift @@ -23,7 +23,37 @@ import Cocoa -/// Represents the states for the input controller. +/// Represents the states for the input method controller. +/// +/// An input method is actually a finite state machine. It receives the inputs +/// from hardware like keyboard and mouse, changes its state, updates user +/// interface by the state, and finally produces the text output and then them +/// to the client apps. It should be a one-way data flow, and the user interface +/// and text output should follow unconditionally one single data source. +/// +/// The InputState class is for representing what the input controller is doing, +/// and the place to store the variables that could be used. For example, the +/// array for the candidate list is useful only when the user is choosing a +/// candidate, and the array should not exist when the input controller is in +/// another state. +/// +/// They are immutable objects. When the state changes, the controller should +/// create a new state object to replace the current state instead of modifying +/// the existing one. +/// +/// McBopomofo's input controller has following possible states: +/// +/// - Deactivated: The user is not using McBopomofo yet. +/// - Empty: The user has switched to McBopomofo but did not input anything yet, +/// or, he or she has committed text into the client apps and starts a new +/// input phase. +/// - Committing: The input controller is sending text to the client apps. +/// - Inputting: The user has inputted something and the input buffer is +/// visible. +/// - Marking: The user is creating a area in the input buffer and about to +/// create a new user phrase. +/// - Choosing Candidate: The candidate window is open to let the user to choose +/// one among the candidates. class InputState: NSObject { } @@ -128,13 +158,14 @@ class InputStateMarking: InputStateNotEmpty { return String(format: NSLocalizedString("You are now selecting \"%@\". Press enter to add a new phrase.", comment: ""), text) } - @objc var readings: [String] = [] + @objc private(set) var readings: [String] = [] - @objc init(composingBuffer: String, cursorIndex: UInt, markerIndex: UInt) { + @objc init(composingBuffer: String, cursorIndex: UInt, markerIndex: UInt, readings: [String]) { self.markerIndex = markerIndex let begin = min(cursorIndex, markerIndex) let end = max(cursorIndex, markerIndex) markedRange = NSMakeRange(Int(begin), Int(end - begin)) + self.readings = readings super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) } diff --git a/Source/KeyHandler.h b/Source/KeyHandler.h index ac0e85e8..cb51bbab 100644 --- a/Source/KeyHandler.h +++ b/Source/KeyHandler.h @@ -33,7 +33,7 @@ extern NSString *const kPlainBopomofoModeIdentifier; @class KeyHandler; @protocol KeyHandlerDelegate -- (VTCandidateController *)candidateControllerForKeyHanlder:(KeyHandler *)keyHandler; +- (VTCandidateController *)candidateControllerForKeyHandler:(KeyHandler *)keyHandler; - (void)keyHandler:(KeyHandler *)keyHandler didSelectCandidateAtIndex:(NSInteger)index candidateController:(VTCandidateController *)controller; - (BOOL)keyHandler:(KeyHandler *)keyHandler didRequestWriteUserPhraseWithState:(InputStateMarking *)state; @end diff --git a/Source/KeyHandler.mm b/Source/KeyHandler.mm index bdfb137d..4e1983e1 100644 --- a/Source/KeyHandler.mm +++ b/Source/KeyHandler.mm @@ -62,7 +62,7 @@ public: }; // if DEBUG is defined, a DOT file (GraphViz format) will be written to the -// specified path everytime the grid is walked +// specified path every time the grid is walked #if DEBUG static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot"; #endif @@ -525,8 +525,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot if ([input isShiftHold]) { // Shift + left if (_builder->cursorIndex() > 0) { - InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:currentState.cursorIndex - 1]; - marking.readings = [self _currentReadings]; + InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:currentState.cursorIndex - 1 readings: [self _currentReadings]]; stateCallback(marking); } else { errorCallback(); @@ -562,8 +561,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot if ([input isShiftHold]) { // Shift + Right if (_builder->cursorIndex() < _builder->length()) { - InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:currentState.cursorIndex + 1]; - marking.readings = [self _currentReadings]; + InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:currentState.cursorIndex + 1 readings: [self _currentReadings]]; stateCallback(marking); } else { errorCallback(); @@ -773,8 +771,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot NSUInteger index = state.markerIndex; if (index > 0) { index -= 1; - InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:state.composingBuffer cursorIndex:state.cursorIndex markerIndex:index]; - marking.readings = state.readings; + InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:state.composingBuffer cursorIndex:state.cursorIndex markerIndex:index readings:state.readings]; stateCallback(marking); } else { errorCallback(); @@ -789,8 +786,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot NSUInteger index = state.markerIndex; if (index < state.composingBuffer.length) { index += 1; - InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:state.composingBuffer cursorIndex:state.cursorIndex markerIndex:index]; - marking.readings = state.readings; + InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:state.composingBuffer cursorIndex:state.cursorIndex markerIndex:index readings:state.readings]; stateCallback(marking); } else { errorCallback(); @@ -810,7 +806,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot { NSString *inputText = input.inputText; UniChar charCode = input.charCode; - VTCandidateController *gCurrentCandidateController = [self.delegate candidateControllerForKeyHanlder:self]; + VTCandidateController *gCurrentCandidateController = [self.delegate candidateControllerForKeyHandler:self]; BOOL cancelCandidateKey = (charCode == 27) || [input isDelete] || ((_inputMode == kPlainBopomofoModeIdentifier) && (charCode == 8));