2.4.0 SP2 // Emacs key. Merge PR #125 from upd/2.4.0sp2

This commit is contained in:
ShikiSuen 2022-09-06 19:58:08 +08:00 committed by GitHub
commit 3f7d0d1bdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 83 additions and 63 deletions

View File

@ -112,7 +112,7 @@ extension KeyHandler {
// MARK: PgDn
if input.isPageDown || input.emacsKey == EmacsKey.nextPage {
if input.isPageDown {
let updated: Bool = ctlCandidateCurrent.showNextPage()
if !updated {
IME.prtDebugIntel("9B691919")
@ -150,17 +150,6 @@ extension KeyHandler {
return true
}
// MARK: EmacsKey Backward
if input.emacsKey == EmacsKey.backward {
let updated: Bool = ctlCandidateCurrent.highlightPreviousCandidate()
if !updated {
IME.prtDebugIntel("9B89308D")
errorCallback()
}
return true
}
// MARK: Right Arrow
if input.isRight {
@ -179,17 +168,6 @@ extension KeyHandler {
return true
}
// MARK: EmacsKey Forward
if input.emacsKey == EmacsKey.forward {
let updated: Bool = ctlCandidateCurrent.highlightNextCandidate()
if !updated {
IME.prtDebugIntel("9B2428D")
errorCallback()
}
return true
}
// MARK: Up Arrow
if input.isUp {
@ -228,7 +206,7 @@ extension KeyHandler {
// MARK: Home Key
if input.isHome || input.emacsKey == EmacsKey.home {
if input.isHome {
if ctlCandidateCurrent.selectedCandidateIndex == 0 {
IME.prtDebugIntel("9B6EDE8D")
errorCallback()
@ -244,7 +222,7 @@ extension KeyHandler {
if state.candidates.isEmpty {
return false
} else { // count > 0!isEmpty滿
if input.isEnd || input.emacsKey == EmacsKey.end {
if input.isEnd {
if ctlCandidateCurrent.selectedCandidateIndex == state.candidates.count - 1 {
IME.prtDebugIntel("9B69AAAD")
errorCallback()

View File

@ -71,7 +71,9 @@ extension KeyHandler {
stateCallback(IMEState.ofEmpty())
// Shift
if input.isUpperCaseASCIILetterKey {
if (input.isUpperCaseASCIILetterKey && input.isASCIIModeInput)
|| (input.isCapsLockOn && input.isShiftHold)
{
return false
}
@ -195,7 +197,7 @@ extension KeyHandler {
// MARK: Cursor backward
if input.isCursorBackward || input.emacsKey == EmacsKey.backward {
if input.isCursorBackward {
return handleBackward(
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
)
@ -203,7 +205,7 @@ extension KeyHandler {
// MARK: Cursor forward
if input.isCursorForward || input.emacsKey == EmacsKey.forward {
if input.isCursorForward {
return handleForward(
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
)
@ -211,13 +213,13 @@ extension KeyHandler {
// MARK: Home
if input.isHome || input.emacsKey == EmacsKey.home {
if input.isHome {
return handleHome(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
}
// MARK: End
if input.isEnd || input.emacsKey == EmacsKey.end {
if input.isEnd {
return handleEnd(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
}
@ -259,7 +261,7 @@ extension KeyHandler {
// MARK: Delete
if input.isDelete || input.emacsKey == EmacsKey.delete {
if input.isDelete {
return handleDelete(state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback)
}

View File

@ -190,7 +190,7 @@ extension KeyHandler {
}
// Shift + Left
if input.isCursorBackward || input.emacsKey == EmacsKey.backward, input.isShiftHold {
if input.isCursorBackward, input.isShiftHold {
if compositor.marker > 0 {
compositor.marker -= 1
if isCursorCuttingChar(isMarker: true) {
@ -213,7 +213,7 @@ extension KeyHandler {
}
// Shift + Right
if input.isCursorForward || input.emacsKey == EmacsKey.forward, input.isShiftHold {
if input.isCursorForward, input.isShiftHold {
if compositor.marker < compositor.width {
compositor.marker += 1
if isCursorCuttingChar(isMarker: true) {

View File

@ -35,6 +35,38 @@ extension NSEvent {
keyCode: keyCode ?? self.keyCode
)
}
/// Emacs NSEvent NSEvent NSEvent
/// - Parameter isVerticalTyping:
/// - Returns:
public func convertFromEmacKeyEvent(isVerticalContext: Bool) -> NSEvent {
guard isEmacsKey else { return self }
let newKeyCode: UInt16 = {
switch isVerticalContext {
case false: return IME.vChewingEmacsKey.charKeyMapHorizontal[charCode] ?? 0
case true: return IME.vChewingEmacsKey.charKeyMapVertical[charCode] ?? 0
}
}()
guard newKeyCode != 0 else { return self }
let newCharScalar: Unicode.Scalar = {
switch charCode {
case 6:
return isVerticalContext
? NSEvent.SpecialKey.downArrow.unicodeScalar : NSEvent.SpecialKey.rightArrow.unicodeScalar
case 2:
return isVerticalContext
? NSEvent.SpecialKey.upArrow.unicodeScalar : NSEvent.SpecialKey.leftArrow.unicodeScalar
case 1: return NSEvent.SpecialKey.home.unicodeScalar
case 5: return NSEvent.SpecialKey.end.unicodeScalar
case 4: return NSEvent.SpecialKey.deleteForward.unicodeScalar // Use "deleteForward" for PC delete.
case 22: return NSEvent.SpecialKey.pageDown.unicodeScalar
default: return .init(0)
}
}()
let newChar = String(newCharScalar)
return reinitiate(modifierFlags: [], characters: newChar, charactersIgnoringModifiers: newChar, keyCode: newKeyCode)
?? self
}
}
// MARK: - NSEvent Extension - InputSignalProtocol
@ -58,8 +90,9 @@ extension NSEvent: InputSignalProtocol {
public var isFlagChanged: Bool { type == .flagsChanged }
public var emacsKey: EmacsKey {
NSEvent.detectEmacsKey(charCode: charCode, flags: modifierFlags)
public var isEmacsKey: Bool {
// isControlHold
[6, 2, 1, 5, 4, 22].contains(charCode) && modifierFlags == .control
}
// Alt+Shift+ macOS
@ -150,13 +183,6 @@ extension NSEvent: InputSignalProtocol {
public var isSymbolMenuPhysicalKey: Bool {
[KeyCode.kSymbolMenuPhysicalKeyIntl, KeyCode.kSymbolMenuPhysicalKeyJIS].contains(KeyCode(rawValue: keyCode))
}
static func detectEmacsKey(charCode: UniChar, flags: NSEvent.ModifierFlags) -> EmacsKey {
if flags.contains(.control) {
return EmacsKey(rawValue: charCode) ?? .none
}
return .none
}
}
// MARK: - InputSignalProtocol
@ -169,7 +195,6 @@ public protocol InputSignalProtocol {
var charCode: UInt16 { get }
var keyCode: UInt16 { get }
var isFlagChanged: Bool { get }
var emacsKey: EmacsKey { get }
var mainAreaNumKeyChar: String? { get }
var isASCII: Bool { get }
var isInvalid: Bool { get }
@ -319,13 +344,3 @@ enum CharCode: UInt16 {
// KeyCode doesn't give a phuque about the character sent through macOS keyboard layouts ...
// ... but only focuses on which physical key is pressed.
}
public enum EmacsKey: UInt16 {
case none = 0
case forward = 6 // F
case backward = 2 // B
case home = 1 // A
case end = 5 // E
case delete = 4 // D
case nextPage = 22 // V
}

View File

@ -46,6 +46,15 @@ extension ctlInputMethod {
///
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

View File

@ -36,6 +36,8 @@ class ctlInputMethod: IMKInputController {
static var isASCIIModeSituation: Bool = false
/// ctlInputMethod
static var isVerticalTypingSituation: Bool = false
/// ctlInputMethod
static var isVerticalCandidateSituation: Bool = false
/// ctlInputMethod
var isASCIIMode: Bool = false
/// 調
@ -221,7 +223,11 @@ class ctlInputMethod: IMKInputController {
// - delegate ctlInputMethod KeyHandler
proc: if let ctlCandidateCurrent = ctlInputMethod.ctlCandidateCurrent as? ctlCandidateIMK {
guard ctlCandidateCurrent.visible else { break proc }
let event: NSEvent = ctlCandidateIMK.replaceNumPadKeyCodes(target: event) ?? event
var event: NSEvent = ctlCandidateIMK.replaceNumPadKeyCodes(target: event) ?? event
// 使 NSEvent Emacs NSEvent NSEvent
if event.isEmacsKey {
event = event.convertFromEmacKeyEvent(isVerticalContext: ctlInputMethod.isVerticalCandidateSituation)
}
// Shift+Enter delegate keyHandler
// Shift Flags
@ -247,6 +253,7 @@ class ctlInputMethod: IMKInputController {
/// commonEventHandler
/// IMK 便
/// event event var Shift
return commonEventHandler(event)
}

View File

@ -58,6 +58,8 @@ extension ctlInputMethod {
// true
}
ctlInputMethod.isVerticalCandidateSituation = (isCandidateWindowVertical || !mgrPrefs.useHorizontalCandidateList)
ctlInputMethod.ctlCandidateCurrent.delegate = nil
/// currentLayout

View File

@ -26,6 +26,13 @@ public enum IME {
fileURLWithFileSystemRepresentation: getpwuid(getuid()).pointee.pw_dir, isDirectory: true, relativeTo: nil
)
// MARK: - vChewing Emacs CharCode-KeyCode translation tables.
public enum vChewingEmacsKey {
static let charKeyMapHorizontal: [UInt16: UInt16] = [6: 124, 2: 123, 1: 115, 5: 119, 4: 117, 22: 121]
static let charKeyMapVertical: [UInt16: UInt16] = [6: 125, 2: 126, 1: 115, 5: 119, 4: 117, 22: 121]
}
// MARK: - Bundle Identifier
/// Bundle Identifier Shift

View File

@ -5,7 +5,7 @@
<key>CFBundleShortVersionString</key>
<string>2.4.0</string>
<key>CFBundleVersion</key>
<string>2400</string>
<string>2402</string>
<key>UpdateInfoEndpoint</key>
<string>https://gitee.com/vchewing/vChewing-macOS/raw/main/Update-Info.plist</string>
<key>UpdateInfoSite</key>

View File

@ -1455,7 +1455,7 @@
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2400;
CURRENT_PROJECT_VERSION = 2402;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_PREPROCESSOR_DEFINITIONS = (
@ -1494,7 +1494,7 @@
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2400;
CURRENT_PROJECT_VERSION = 2402;
ENABLE_NS_ASSERTIONS = NO;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
@ -1532,7 +1532,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2400;
CURRENT_PROJECT_VERSION = 2402;
DEAD_CODE_STRIPPING = YES;
ENABLE_HARDENED_RUNTIME = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
@ -1584,7 +1584,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2400;
CURRENT_PROJECT_VERSION = 2402;
DEAD_CODE_STRIPPING = YES;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_NS_ASSERTIONS = NO;
@ -1718,7 +1718,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2400;
CURRENT_PROJECT_VERSION = 2402;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = "";
@ -1777,7 +1777,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2400;
CURRENT_PROJECT_VERSION = 2402;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = "";
@ -1824,7 +1824,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2400;
CURRENT_PROJECT_VERSION = 2402;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
@ -1868,7 +1868,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2400;
CURRENT_PROJECT_VERSION = 2402;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;