IMKCandidates // Move its key handling process to ctlIME.

This commit is contained in:
ShikiSuen 2022-09-24 11:30:40 +08:00
parent 7c3d90dc80
commit 0ce0604c16
5 changed files with 69 additions and 90 deletions

View File

@ -25,8 +25,6 @@ public class CandidateKeyLabel: NSObject {
}
public protocol ctlCandidateDelegate: AnyObject {
var isAssociatedPhrasesState: Bool { get }
func sharedEventHandler(_ event: NSEvent) -> Bool
func candidateCountForController(_ controller: ctlCandidateProtocol) -> Int
func candidatesForController(_ controller: ctlCandidateProtocol) -> [(String, String)]
func ctlCandidate(_ controller: ctlCandidateProtocol, candidateAtIndex index: Int)

View File

@ -185,74 +185,6 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
setCandidateFrameTopLeft(adjustedPoint)
}
override public func interpretKeyEvents(_ eventArray: [NSEvent]) {
//
// Objective-C nil
guard !eventArray.isEmpty else { return }
let event = eventArray[0]
guard let delegate = delegate else { return }
if event.isEsc || event.isBackSpace || event.isDelete || (event.isShiftHold && !event.isSpace) {
_ = delegate.sharedEventHandler(event)
} else if event.isSymbolMenuPhysicalKey {
//
switch currentLayout {
case .horizontal: event.isShiftHold ? moveUp(self) : moveDown(self)
case .vertical: event.isShiftHold ? moveLeft(self) : moveRight(self)
}
} else if event.isSpace {
switch PrefMgr.shared.specifyShiftSpaceKeyBehavior {
case true: _ = event.isShiftHold ? highlightNextCandidate() : showNextPage()
case false: _ = event.isShiftHold ? showNextPage() : highlightNextCandidate()
}
} else if event.isTab {
switch PrefMgr.shared.specifyShiftTabKeyBehavior {
case true: _ = event.isShiftHold ? showPreviousPage() : showNextPage()
case false: _ = event.isShiftHold ? highlightPreviousCandidate() : highlightNextCandidate()
}
} else {
if let newChar = Self.defaultIMKSelectionKey[event.keyCode] {
/// KeyCode NSEvent Character
/// IMK
let newEvent = event.reinitiate(characters: newChar)
if let newEvent = newEvent {
if PrefMgr.shared.useSCPCTypingMode, delegate.isAssociatedPhrasesState {
// input.isShiftHold ctlInputMethod.handle()
if !event.isShiftHold {
_ = delegate.sharedEventHandler(event)
return
}
} else {
if #available(macOS 10.14, *) {
handleKeyboardEvent(newEvent)
} else {
super.interpretKeyEvents([newEvent])
}
return
}
}
}
if PrefMgr.shared.useSCPCTypingMode, !event.isReservedKey {
_ = delegate.sharedEventHandler(event)
return
}
if delegate.isAssociatedPhrasesState,
!event.isPageUp, !event.isPageDown, !event.isCursorForward, !event.isCursorBackward,
!event.isCursorClockLeft, !event.isCursorClockRight, !event.isSpace,
!event.isEnter || !PrefMgr.shared.alsoConfirmAssociatedCandidatesByEnter
{
_ = delegate.sharedEventHandler(event)
return
}
super.interpretKeyEvents(eventArray)
}
}
public func superInterpretKeyEvents(_ eventArray: [NSEvent]) {
super.interpretKeyEvents(eventArray)
}
}
// MARK: - Generate TISInputSource Object

View File

@ -322,7 +322,7 @@ class ctlInputMethod: IMKInputController {
Self.areWeNerfing = eventToDeal.modifierFlags.contains([.shift, .command])
// IMK IMK
if let result = imkCandidatesEventHandler(event: eventToDeal) {
if let result = imkCandidatesEventPreHandler(event: eventToDeal) {
if shouldUseShiftToggleHandle {
rencentKeyHandledByKeyHandlerEtc = result
}

View File

@ -50,17 +50,6 @@ extension ctlInputMethod: KeyHandlerDelegate {
// MARK: - Candidate Controller Delegate
extension ctlInputMethod: ctlCandidateDelegate {
var isAssociatedPhrasesState: Bool { state.type == .ofAssociates }
/// handle() IMK
/// handle()
/// IMK commonEventHandler()
/// - Parameter event: IMK
/// - Returns: `true` IMK`false`
@discardableResult func sharedEventHandler(_ event: NSEvent) -> Bool {
commonEventHandler(event)
}
func candidateCountForController(_ controller: ctlCandidateProtocol) -> Int {
_ = controller //
if state.isCandidateContainer {

View File

@ -37,10 +37,10 @@ extension ctlInputMethod {
/// handle()
/// - Parameter event: IMK
/// - Returns: `true` IMK`false`
func imkCandidatesEventHandler(event eventToDeal: NSEvent) -> Bool? {
func imkCandidatesEventPreHandler(event eventToDeal: NSEvent) -> Bool? {
// IMK IMK
// interpretKeyEvents()
// - super.interpretKeyEvents()
// - imkCandidates.interpretKeyEvents()
// - delegate ctlInputMethod KeyHandler
if let imkCandidates = ctlInputMethod.ctlCandidateCurrent as? ctlCandidateIMK, imkCandidates.visible {
let event: NSEvent = ctlCandidateIMK.replaceNumPadKeyCodes(target: eventToDeal) ?? eventToDeal
@ -52,25 +52,85 @@ extension ctlInputMethod {
NSSound.beep()
return true
}
imkCandidates.interpretKeyEvents([newEvent])
return true
return imkCandidatesEventSubHandler(event: newEvent)
}
//
if let newChar = ctlCandidateIMK.defaultIMKSelectionKey[event.keyCode],
event.isShiftHold, isAssociatedPhrasesState,
event.isShiftHold, state.type == .ofAssociates,
let newEvent = event.reinitiate(modifierFlags: [], characters: newChar)
{
if #available(macOS 10.14, *) {
imkCandidates.handleKeyboardEvent(newEvent)
} else {
imkCandidates.superInterpretKeyEvents([newEvent])
imkCandidates.interpretKeyEvents([newEvent])
}
return true
}
imkCandidates.interpretKeyEvents([event])
return true
return imkCandidatesEventSubHandler(event: event)
}
return nil
}
func imkCandidatesEventSubHandler(event: NSEvent) -> Bool {
let eventArray = [event]
guard let imkC = Self.ctlCandidateCurrent as? ctlCandidateIMK else { return false }
if event.isEsc || event.isBackSpace || event.isDelete || (event.isShiftHold && !event.isSpace) {
return commonEventHandler(event)
} else if event.isSymbolMenuPhysicalKey {
//
switch imkC.currentLayout {
case .horizontal: _ = event.isShiftHold ? imkC.moveUp(self) : imkC.moveDown(self)
case .vertical: _ = event.isShiftHold ? imkC.moveLeft(self) : imkC.moveRight(self)
}
return true
} else if event.isSpace {
switch PrefMgr.shared.specifyShiftSpaceKeyBehavior {
case true: _ = event.isShiftHold ? imkC.highlightNextCandidate() : imkC.showNextPage()
case false: _ = event.isShiftHold ? imkC.showNextPage() : imkC.highlightNextCandidate()
}
return true
} else if event.isTab {
switch PrefMgr.shared.specifyShiftTabKeyBehavior {
case true: _ = event.isShiftHold ? imkC.showPreviousPage() : imkC.showNextPage()
case false: _ = event.isShiftHold ? imkC.highlightPreviousCandidate() : imkC.highlightNextCandidate()
}
return true
} else {
if let newChar = ctlCandidateIMK.defaultIMKSelectionKey[event.keyCode] {
/// KeyCode NSEvent Character
/// IMK
let newEvent = event.reinitiate(characters: newChar)
if let newEvent = newEvent {
if PrefMgr.shared.useSCPCTypingMode, state.type == .ofAssociates {
// input.isShiftHold ctlInputMethod.handle()
return event.isShiftHold ? true : commonEventHandler(event)
} else {
if #available(macOS 10.14, *) {
imkC.handleKeyboardEvent(newEvent)
} else {
imkC.interpretKeyEvents([newEvent])
}
return true
}
}
}
if PrefMgr.shared.useSCPCTypingMode, !event.isReservedKey {
return commonEventHandler(event)
}
if state.type == .ofAssociates,
!event.isPageUp, !event.isPageDown, !event.isCursorForward, !event.isCursorBackward,
!event.isCursorClockLeft, !event.isCursorClockRight, !event.isSpace,
!event.isEnter || !PrefMgr.shared.alsoConfirmAssociatedCandidatesByEnter
{
return commonEventHandler(event)
}
imkC.interpretKeyEvents(eventArray)
return true
}
}
}