Skip to content

Commit

Permalink
(InputMethod) Improve EventHandler code structures
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Oct 23, 2023
1 parent 6d64eab commit a061df2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
16 changes: 8 additions & 8 deletions Jyutping/InputMethod/EventHandler.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SwiftUI
import InputMethodKit
import CoreIME
import CommonExtensions

private struct ShiftKey {
private(set) static var isBuffering: Bool = false
Expand Down Expand Up @@ -45,10 +46,7 @@ extension JyutpingInputController {

override func handle(_ event: NSEvent!, client sender: Any!) -> Bool {
guard let event = event else { return false }
let modifiers = event.modifierFlags
let shouldIgnoreCurrentEvent: Bool = modifiers.contains(.command) || modifiers.contains(.option)
guard !shouldIgnoreCurrentEvent else { return false }
if shouldSwitchInputMethodMode(with: event) {
guard !shouldSwitchInputMethodMode(with: event) else {
switch appContext.inputForm {
case .cantonese:
passBuffer()
Expand All @@ -65,6 +63,9 @@ extension JyutpingInputController {
}
}
guard event.type == .keyDown else { return false }
let modifiers = event.modifierFlags
let shouldIgnoreCurrentEvent: Bool = modifiers.contains(.command) || modifiers.contains(.option)
guard !shouldIgnoreCurrentEvent else { return false }
let client: IMKTextInput? = (sender as? IMKTextInput) ?? currentClient
currentOrigin = client?.position
let currentClientID = currentClient?.uniqueClientIdentifierString()
Expand Down Expand Up @@ -306,7 +307,7 @@ extension JyutpingInputController {
handleOptions(-1)
return true
}
case .escapeClear:
case .escape, .clear:
switch currentInputForm {
case .cantonese:
guard inputStage.isBuffering else { return false }
Expand All @@ -332,14 +333,13 @@ extension JyutpingInputController {
if candidates.isEmpty {
passBuffer()
let shouldInsertFullWidthSpace: Bool = isShifting || (Options.characterForm == .fullWidth)
let text: String = shouldInsertFullWidthSpace ? " " : " "
let text: String = shouldInsertFullWidthSpace ? String.fullWidthSpace : String.space
client?.insert(text)
return true
} else {
let index = appContext.highlightedIndex
guard let selectedItem = appContext.displayCandidates.fetch(index) else { return true }
let text = selectedItem.text
client?.insert(text)
client?.insert(selectedItem.text)
aftercareSelection(selectedItem)
return true
}
Expand Down
11 changes: 6 additions & 5 deletions Jyutping/InputMethod/KeyCode/Representative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ enum Representative: Hashable {
case separator
case `return`
case backspace
case escapeClear
case escape
case clear
case space
case tab
case previousPage
Expand Down Expand Up @@ -153,8 +154,10 @@ extension UInt16 {
return .punctuation(.equal)
case KeyCode.Symbol.VK_QUOTE:
return .separator
case KeyCode.Special.VK_ESCAPE, KeyCode.Keypad.VK_KEYPAD_CLEAR:
return .escapeClear
case KeyCode.Special.VK_ESCAPE:
return .escape
case KeyCode.Keypad.VK_KEYPAD_CLEAR:
return .clear
case KeyCode.Special.VK_TAB:
return .tab
case KeyCode.Special.VK_PAGEUP:
Expand All @@ -166,5 +169,3 @@ extension UInt16 {
}
}
}


0 comments on commit a061df2

Please sign in to comment.