Code clean-up.

This commit is contained in:
zonble 2022-01-28 16:51:25 +08:00
parent b13da10ec0
commit 9612aa6ba0
4 changed files with 42 additions and 15 deletions

View File

@ -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;
}

View File

@ -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)
}

View File

@ -33,7 +33,7 @@ extern NSString *const kPlainBopomofoModeIdentifier;
@class KeyHandler;
@protocol KeyHandlerDelegate <NSObject>
- (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

View File

@ -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));