KeyHandler // _handleCandidateState: Add comments.

This commit is contained in:
ShikiSuen 2022-04-18 14:31:26 +08:00
parent 79f7299fd0
commit c632654672
1 changed files with 61 additions and 34 deletions

View File

@ -2,27 +2,27 @@
// Refactored from the ObjCpp-version of this class by: // Refactored from the ObjCpp-version of this class by:
// (c) 2011 and onwards The OpenVanilla Project (MIT License). // (c) 2011 and onwards The OpenVanilla Project (MIT License).
/* /*
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions: subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in 1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
2. No trademark license is granted to use the trade names, trademarks, service 2. No trademark license is granted to use the trade names, trademarks, service
marks, or product names of Contributor, except as required to fulfill notice marks, or product names of Contributor, except as required to fulfill notice
requirements above. requirements above.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import Cocoa import Cocoa
@ -39,38 +39,35 @@ import Cocoa
let charCode: UniChar = input.charCode let charCode: UniChar = input.charCode
let ctlCandidateCurrent = delegate!.ctlCandidate(for: self) as! ctlCandidate let ctlCandidateCurrent = delegate!.ctlCandidate(for: self) as! ctlCandidate
// MARK: Cancel Candidate
let cancelCandidateKey = let cancelCandidateKey =
input.isBackSpace || input.isESC || input.isDelete input.isBackSpace || input.isESC || input.isDelete
|| ((input.isCursorBackward || input.isCursorForward) && input.isShiftHold) || ((input.isCursorBackward || input.isCursorForward) && input.isShiftHold)
if cancelCandidateKey { if cancelCandidateKey {
if state is InputState.AssociatedPhrases { if (state is InputState.AssociatedPhrases)
clear() || mgrPrefs.useSCPCTypingMode
let empty = InputState.EmptyIgnoringPreviousState() || isBuilderEmpty()
stateCallback(empty) {
} else if mgrPrefs.useSCPCTypingMode {
clear()
let empty = InputState.EmptyIgnoringPreviousState()
stateCallback(empty)
} else if isBuilderEmpty() {
// //
// //
// 使 BackSpace // 使 BackSpace
// isBuilderEmpty()
clear() clear()
let empty = InputState.EmptyIgnoringPreviousState() stateCallback(InputState.EmptyIgnoringPreviousState())
stateCallback(empty)
} else { } else {
let inputting = buildInputtingState() stateCallback(buildInputtingState())
stateCallback(inputting)
} }
return true return true
} }
// MARK: Enter
if input.isEnter { if input.isEnter {
if state is InputState.AssociatedPhrases { if state is InputState.AssociatedPhrases {
clear() clear()
let empty = InputState.EmptyIgnoringPreviousState() stateCallback(InputState.EmptyIgnoringPreviousState())
stateCallback(empty)
return true return true
} }
delegate!.keyHandler( delegate!.keyHandler(
@ -81,10 +78,14 @@ import Cocoa
return true return true
} }
// MARK: Tab
if input.isTab { if input.isTab {
let updated: Bool = let updated: Bool =
mgrPrefs.specifyShiftTabKeyBehavior mgrPrefs.specifyShiftTabKeyBehavior
? (input.isShiftHold ? ctlCandidateCurrent.showPreviousPage() : ctlCandidateCurrent.showNextPage()) ? (input.isShiftHold
? ctlCandidateCurrent.showPreviousPage()
: ctlCandidateCurrent.showNextPage())
: (input.isShiftHold : (input.isShiftHold
? ctlCandidateCurrent.highlightPreviousCandidate() ? ctlCandidateCurrent.highlightPreviousCandidate()
: ctlCandidateCurrent.highlightNextCandidate()) : ctlCandidateCurrent.highlightNextCandidate())
@ -95,6 +96,8 @@ import Cocoa
return true return true
} }
// MARK: Space
if input.isSpace { if input.isSpace {
let updated: Bool = let updated: Bool =
mgrPrefs.specifyShiftSpaceKeyBehavior mgrPrefs.specifyShiftSpaceKeyBehavior
@ -111,6 +114,8 @@ import Cocoa
return true return true
} }
// MARK: PgDn
if input.isPageDown || input.emacsKey == vChewingEmacsKey.nextPage { if input.isPageDown || input.emacsKey == vChewingEmacsKey.nextPage {
let updated: Bool = ctlCandidateCurrent.showNextPage() let updated: Bool = ctlCandidateCurrent.showNextPage()
if !updated { if !updated {
@ -120,6 +125,8 @@ import Cocoa
return true return true
} }
// MARK: PgUp
if input.isPageUp { if input.isPageUp {
let updated: Bool = ctlCandidateCurrent.showPreviousPage() let updated: Bool = ctlCandidateCurrent.showPreviousPage()
if !updated { if !updated {
@ -129,6 +136,8 @@ import Cocoa
return true return true
} }
// MARK: Left Arrow
if input.isLeft { if input.isLeft {
if ctlCandidateCurrent is ctlCandidateHorizontal { if ctlCandidateCurrent is ctlCandidateHorizontal {
let updated: Bool = ctlCandidateCurrent.highlightPreviousCandidate() let updated: Bool = ctlCandidateCurrent.highlightPreviousCandidate()
@ -146,6 +155,8 @@ import Cocoa
return true return true
} }
// MARK: EmacsKey Backward
if input.emacsKey == vChewingEmacsKey.backward { if input.emacsKey == vChewingEmacsKey.backward {
let updated: Bool = ctlCandidateCurrent.highlightPreviousCandidate() let updated: Bool = ctlCandidateCurrent.highlightPreviousCandidate()
if !updated { if !updated {
@ -155,6 +166,8 @@ import Cocoa
return true return true
} }
// MARK: Right Arrow
if input.isRight { if input.isRight {
if ctlCandidateCurrent is ctlCandidateHorizontal { if ctlCandidateCurrent is ctlCandidateHorizontal {
let updated: Bool = ctlCandidateCurrent.highlightNextCandidate() let updated: Bool = ctlCandidateCurrent.highlightNextCandidate()
@ -172,6 +185,8 @@ import Cocoa
return true return true
} }
// MARK: EmacsKey Forward
if input.emacsKey == vChewingEmacsKey.forward { if input.emacsKey == vChewingEmacsKey.forward {
let updated: Bool = ctlCandidateCurrent.highlightNextCandidate() let updated: Bool = ctlCandidateCurrent.highlightNextCandidate()
if !updated { if !updated {
@ -181,6 +196,8 @@ import Cocoa
return true return true
} }
// MARK: Up Arrow
if input.isUp { if input.isUp {
if ctlCandidateCurrent is ctlCandidateHorizontal { if ctlCandidateCurrent is ctlCandidateHorizontal {
let updated: Bool = ctlCandidateCurrent.showPreviousPage() let updated: Bool = ctlCandidateCurrent.showPreviousPage()
@ -198,6 +215,8 @@ import Cocoa
return true return true
} }
// MARK: Down Arrow
if input.isDown { if input.isDown {
if ctlCandidateCurrent is ctlCandidateHorizontal { if ctlCandidateCurrent is ctlCandidateHorizontal {
let updated: Bool = ctlCandidateCurrent.showNextPage() let updated: Bool = ctlCandidateCurrent.showNextPage()
@ -215,6 +234,8 @@ import Cocoa
return true return true
} }
// MARK: Home Key
if input.isHome || input.emacsKey == vChewingEmacsKey.home { if input.isHome || input.emacsKey == vChewingEmacsKey.home {
if ctlCandidateCurrent.selectedCandidateIndex == 0 { if ctlCandidateCurrent.selectedCandidateIndex == 0 {
IME.prtDebugIntel("9B6EDE8D") IME.prtDebugIntel("9B6EDE8D")
@ -234,6 +255,8 @@ import Cocoa
candidates = (state as! InputState.AssociatedPhrases).candidates candidates = (state as! InputState.AssociatedPhrases).candidates
} }
// MARK: End Key
if candidates.isEmpty { if candidates.isEmpty {
return false return false
} else { // count > 0!isEmpty滿 } else { // count > 0!isEmpty滿
@ -248,6 +271,8 @@ import Cocoa
} }
} }
// MARK: - Associated Phrases
if state is InputState.AssociatedPhrases { if state is InputState.AssociatedPhrases {
if !input.isShiftHold { return false } if !input.isShiftHold { return false }
} }
@ -276,6 +301,8 @@ import Cocoa
if state is InputState.AssociatedPhrases { return false } if state is InputState.AssociatedPhrases { return false }
// MARK: SCPC Mode Processing
if mgrPrefs.useSCPCTypingMode { if mgrPrefs.useSCPCTypingMode {
var punctuationNamePrefix = "" var punctuationNamePrefix = ""