libchewing
libchewing copied to clipboard
Enclose all state transition in a function
Currently state transition is handled by each function which is error prone. The idea is to define two functions, getState() and setState() to handle the state transition like English Mode -> Chinese Mode.
My purpose is to add the following code:
enum State {
STATE_CHINESE,
STATE_ENGLISH_HALFSHAPE,
STATE_ENGLISH_FULLSHAPE,
STATE_SELET_CANDIDATE,
STATE_ENTERING_BOPOMOFO,
STATE_SHIFT_SELECT_PHRASE,
STATE_EASY_SYMBOL,
};
enum ChangeStateAction {
ENTER_CHINESE,
ENTER_ENGLISH,
ENTER_HALFSHAPE,
ENTER_FULLSHAPE,
ENTER_EASY_SYMBOL,
LEAVE_EASY_SYMBOL,
ENTER_BOPOMOFO,
LEAVE_BOPOMOFO,
ENTER_SELECT_CANDIDATE,
LEAVE_SELECT_CANDIDATE,
ENTER_SELECT_PHRASE,
LEAVE_SELECT_PHRASE,
};
State get_state( ChewingContext *ctx );
int change_state( ChewingContext *ctx, ChangeStateAction action );
// The following is an example how to use these two functions.
int chewing_handle_Default( ... )
{
....
switch ( get_state( ctx ) ) {
case STATE_SELET_CANDIDATE:
if ( /* key is select key */ ) {
....
change_state( ctx, LEAVE_SELECT_CANDIDATE );
}
break;
....
}
}
Please help to see if this approach is suitable or not, thanks.
Now we have a state machine in the Rust version!