InputState // Rewrite documentation in Chinese.

This commit is contained in:
ShikiSuen 2022-06-15 23:17:13 +08:00
parent 305c919bec
commit d2da05a267
1 changed files with 62 additions and 50 deletions

View File

@ -28,39 +28,40 @@ import Cocoa
// InputState 使 Struct Struct // InputState 使 Struct Struct
/// Represents the states for the input method controller. /// ctlInputMethod
/// ///
/// An input method is actually a finite state machine. It receives the inputs /// Finite State Machine/
/// 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, /// InputState
/// 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 /// InputState
/// create a new state object to replace the current state instead of modifying /// InputState.Marking InputState.Inputting
/// the existing one. ///
/// ///
/// The input controller has following possible states: ///
/// ///
/// - Deactivated: The user is not using the input method yet. /// - .Deactivated: 使使
/// - Empty: The user has switched to this input method but inputted nothing yet, /// - .AssociatedPhrases:
/// or, he or she has committed text into the client apps and starts a new /// 西 .NotEmpty
/// input phase. /// - .Empty: 使
/// - Committing: The input controller is sending text to the client apps. ///
/// - Inputting: The user has inputted something and the input buffer is /// - .EmptyIgnorePreviousState: Empty
/// visible. ///
/// - Marking: The user is creating a area in the input buffer and about to /// - .Committing:
/// create a new user phrase. /// - .NotEmpty:
/// - Choosing Candidate: The candidate window is open to let the user to choose /// - .Inputting: 使Compositor
/// one among the candidates. /// - .Marking: 使
///
/// - .ChoosingCandidate: 使
/// - .SymbolTable:
class InputState { class InputState {
/// Represents that the input controller is deactivated. /// .Deactivated: 使使
class Deactivated: InputState { class Deactivated: InputState {
var description: String { var description: String {
"<InputState.Deactivated>" "<InputState.Deactivated>"
@ -69,7 +70,8 @@ class InputState {
// MARK: - // MARK: -
/// Represents that the composing buffer is empty. /// .Empty: 使
///
class Empty: InputState { class Empty: InputState {
var composingBuffer: String { var composingBuffer: String {
"" ""
@ -82,7 +84,8 @@ class InputState {
// MARK: - // MARK: -
/// Represents that the composing buffer is empty. /// .EmptyIgnorePreviousState: Empty
///
class EmptyIgnoringPreviousState: Empty { class EmptyIgnoringPreviousState: Empty {
override var description: String { override var description: String {
"<InputState.EmptyIgnoringPreviousState>" "<InputState.EmptyIgnoringPreviousState>"
@ -91,7 +94,7 @@ class InputState {
// MARK: - // MARK: -
/// Represents that the input controller is committing text into client app. /// .Committing:
class Committing: InputState { class Committing: InputState {
private(set) var poppedText: String = "" private(set) var poppedText: String = ""
@ -107,7 +110,30 @@ class InputState {
// MARK: - // MARK: -
/// Represents that the composing buffer is not empty. /// .AssociatedPhrases:
/// 西 .NotEmpty
class AssociatedPhrases: InputState {
private(set) var candidates: [String] = []
private(set) var isTypingVertical: Bool = false
init(candidates: [String], isTypingVertical: Bool) {
self.candidates = candidates
self.isTypingVertical = isTypingVertical
super.init()
}
var description: String {
"<InputState.AssociatedPhrases, candidates:\(candidates), isTypingVertical:\(isTypingVertical)>"
}
}
// MARK: -
/// .NotEmpty:
/// - .Inputting: 使Compositor
/// - .Marking: 使
///
/// - .ChoosingCandidate: 使
/// - .SymbolTable:
class NotEmpty: InputState { class NotEmpty: InputState {
private(set) var composingBuffer: String private(set) var composingBuffer: String
private(set) var cursorIndex: Int = 0 { didSet { cursorIndex = max(cursorIndex, 0) } } private(set) var cursorIndex: Int = 0 { didSet { cursorIndex = max(cursorIndex, 0) } }
@ -136,7 +162,7 @@ class InputState {
// MARK: - // MARK: -
/// Represents that the user is inputting text. /// .Inputting: 使Compositor
class Inputting: NotEmpty { class Inputting: NotEmpty {
var poppedText: String = "" var poppedText: String = ""
var tooltip: String = "" var tooltip: String = ""
@ -152,7 +178,8 @@ class InputState {
// MARK: - // MARK: -
/// Represents that the user is marking a range in the composing buffer. /// .Marking: 使
///
class Marking: NotEmpty { class Marking: NotEmpty {
private var allowedMarkRange: ClosedRange<Int> = mgrPrefs.minCandidateLength...mgrPrefs.maxCandidateLength private var allowedMarkRange: ClosedRange<Int> = mgrPrefs.minCandidateLength...mgrPrefs.maxCandidateLength
private(set) var markerIndex: Int = 0 { didSet { markerIndex = max(markerIndex, 0) } } private(set) var markerIndex: Int = 0 { didSet { markerIndex = max(markerIndex, 0) } }
@ -316,7 +343,7 @@ class InputState {
// MARK: - // MARK: -
/// Represents that the user is choosing in a candidates list. /// .ChoosingCandidate: 使
class ChoosingCandidate: NotEmpty { class ChoosingCandidate: NotEmpty {
private(set) var candidates: [String] private(set) var candidates: [String]
private(set) var isTypingVertical: Bool private(set) var isTypingVertical: Bool
@ -334,22 +361,7 @@ class InputState {
// MARK: - // MARK: -
/// Represents that the user is choosing in a candidates list /// .SymbolTable:
/// in the associated phrases mode.
class AssociatedPhrases: InputState {
private(set) var candidates: [String] = []
private(set) var isTypingVertical: Bool = false
init(candidates: [String], isTypingVertical: Bool) {
self.candidates = candidates
self.isTypingVertical = isTypingVertical
super.init()
}
var description: String {
"<InputState.AssociatedPhrases, candidates:\(candidates), isTypingVertical:\(isTypingVertical)>"
}
}
class SymbolTable: ChoosingCandidate { class SymbolTable: ChoosingCandidate {
var node: SymbolNode var node: SymbolNode