Repo // Handle NSInternalInconsistencyException.

This commit is contained in:
ShikiSuen 2022-09-11 08:51:42 +08:00
parent 738aec4260
commit 3987cad35e
3 changed files with 69 additions and 64 deletions

View File

@ -22,15 +22,19 @@ extension NSEvent {
isARepeat: Bool? = nil,
keyCode: UInt16? = nil
) -> NSEvent? {
NSEvent.keyEvent(
let oldChars: String = {
if self.type == .flagsChanged { return "" }
return self.characters ?? ""
}()
return NSEvent.keyEvent(
with: type ?? self.type,
location: location ?? locationInWindow,
modifierFlags: modifierFlags ?? self.modifierFlags,
timestamp: timestamp ?? self.timestamp,
windowNumber: windowNumber ?? self.windowNumber,
context: nil,
characters: characters ?? self.characters ?? "",
charactersIgnoringModifiers: charactersIgnoringModifiers ?? self.characters ?? "",
characters: characters ?? oldChars,
charactersIgnoringModifiers: charactersIgnoringModifiers ?? characters ?? oldChars,
isARepeat: isARepeat ?? self.isARepeat,
keyCode: keyCode ?? self.keyCode
)
@ -81,6 +85,8 @@ extension NSEvent: InputSignalProtocol {
}
public var charCode: UInt16 {
guard type != .flagsChanged else { return 0 }
guard characters != nil else { return 0 }
// count > 0!isEmpty滿
guard !text.isEmpty else { return 0 }
let scalars = text.unicodeScalars

View File

@ -16,59 +16,9 @@ extension ctlInputMethod {
/// - Parameter event: IMK
/// - Returns: `true` IMK`false`
func commonEventHandler(_ event: NSEvent) -> Bool {
// Shift macOS 10.15 macOS
let shouldUseHandle: Bool = {
switch mgrPrefs.shiftKeyAccommodationBehavior {
case 0: return false
case 1: return IME.arrClientShiftHandlingExceptionList.contains(clientBundleIdentifier)
case 2: return true
default: return false
}
}()
if #available(macOS 10.15, *) {
if ShiftKeyUpChecker.check(event), !mgrPrefs.disableShiftTogglingAlphanumericalMode {
if !shouldUseHandle || (!rencentKeyHandledByKeyHandler && shouldUseHandle) {
NotifierController.notify(
message: NSLocalizedString("Alphanumerical Mode", comment: "") + "\n"
+ (toggleASCIIMode()
? NSLocalizedString("NotificationSwitchON", comment: "")
: NSLocalizedString("NotificationSwitchOFF", comment: ""))
)
}
if shouldUseHandle {
rencentKeyHandledByKeyHandler = false
}
return false
}
}
///
guard client() != nil else { return false }
var event = event
// 使 NSEvent Emacs NSEvent NSEvent
if event.isEmacsKey {
let verticalProcessing =
(state.isCandidateContainer)
? ctlInputMethod.isVerticalCandidateSituation : ctlInputMethod.isVerticalTypingSituation
event = event.convertFromEmacKeyEvent(isVerticalContext: verticalProcessing)
}
/// flags使 KeyHandler
/// flags
/// event.type == .flagsChanged return false
/// NSInternalInconsistencyException
if event.type == .flagsChanged { return false }
//
ctlInputMethod.areWeNerfing = event.modifierFlags.contains([.shift, .command])
//
// KeyHandler
if !event.charCode.isPrintable {
return false
}
if !event.charCode.isPrintable { return false }
/// 調
/// result bool IMK
@ -78,9 +28,6 @@ extension ctlInputMethod {
} errorCallback: {
clsSFX.beep()
}
if shouldUseHandle {
rencentKeyHandledByKeyHandler = result
}
return result
}
}

View File

@ -57,7 +57,7 @@ class ctlInputMethod: IMKInputController {
}
/// `handle(event:)` Shift
var rencentKeyHandledByKeyHandler = false
var rencentKeyHandledByKeyHandlerEtc = false
// MARK: -
@ -207,6 +207,8 @@ class ctlInputMethod: IMKInputController {
@objc(handleEvent:client:) override func handle(_ event: NSEvent!, client sender: Any!) -> Bool {
_ = sender //
// MARK:
//
ctlInputMethod.isASCIIModeSituation = isASCIIMode
ctlInputMethod.isVerticalTypingSituation = isVerticalTyping
@ -218,17 +220,63 @@ class ctlInputMethod: IMKInputController {
return false
}
// Shift macOS 10.15 macOS
let shouldUseShiftToggleHandle: Bool = {
switch mgrPrefs.shiftKeyAccommodationBehavior {
case 0: return false
case 1: return IME.arrClientShiftHandlingExceptionList.contains(clientBundleIdentifier)
case 2: return true
default: return false
}
}()
if #available(macOS 10.15, *) {
if ShiftKeyUpChecker.check(event), !mgrPrefs.disableShiftTogglingAlphanumericalMode {
if !shouldUseShiftToggleHandle || (!rencentKeyHandledByKeyHandlerEtc && shouldUseShiftToggleHandle) {
NotifierController.notify(
message: NSLocalizedString("Alphanumerical Mode", comment: "") + "\n"
+ (toggleASCIIMode()
? NSLocalizedString("NotificationSwitchON", comment: "")
: NSLocalizedString("NotificationSwitchOFF", comment: ""))
)
}
if shouldUseShiftToggleHandle {
rencentKeyHandledByKeyHandlerEtc = false
}
return false
}
}
// MARK:
/// flags使 KeyHandler
/// flags
/// event.type == .flagsChanged return false
/// NSInternalInconsistencyException
if event.type == .flagsChanged { return false }
///
guard client() != nil else { return false }
var eventToDeal = event
// 使 NSEvent Emacs NSEvent NSEvent
if eventToDeal.isEmacsKey {
let verticalProcessing =
(state.isCandidateContainer)
? ctlInputMethod.isVerticalCandidateSituation : ctlInputMethod.isVerticalTypingSituation
eventToDeal = eventToDeal.convertFromEmacKeyEvent(isVerticalContext: verticalProcessing)
}
//
ctlInputMethod.areWeNerfing = eventToDeal.modifierFlags.contains([.shift, .command])
// IMK IMK
// interpretKeyEvents()
// - super.interpretKeyEvents()
// - delegate ctlInputMethod KeyHandler
proc: if let ctlCandidateCurrent = ctlInputMethod.ctlCandidateCurrent as? ctlCandidateIMK {
guard ctlCandidateCurrent.visible else { break proc }
var event: NSEvent = ctlCandidateIMK.replaceNumPadKeyCodes(target: event) ?? event
// 使 NSEvent Emacs NSEvent NSEvent
if event.isEmacsKey {
event = event.convertFromEmacKeyEvent(isVerticalContext: ctlInputMethod.isVerticalCandidateSituation)
}
var event: NSEvent = ctlCandidateIMK.replaceNumPadKeyCodes(target: eventToDeal) ?? eventToDeal
// Shift+Enter delegate keyHandler
// Shift Flags
@ -255,7 +303,11 @@ class ctlInputMethod: IMKInputController {
/// commonEventHandler
/// IMK 便
/// event event var Shift
return commonEventHandler(event)
let result = commonEventHandler(eventToDeal)
if shouldUseShiftToggleHandle {
rencentKeyHandledByKeyHandlerEtc = result
}
return result
}
/// App Ctrl+Enter / Shift+Enter