vChewing-macOS/Source/Modules/ControllerModules/ctlInputMethod_Core.swift

434 lines
21 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// (c) 2011 and onwards The OpenVanilla Project (MIT License).
// All possible vChewing-specific modifications are of:
// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import InputMethodKit
///
///
/// IMKInputController
///
/// / IMKInputController
/// 調
/// - Remark: IMKServer
/// IMKInputController
@objc(ctlInputMethod) // ObjC IMK ObjC
class ctlInputMethod: IMKInputController {
///
static var areWeNerfing = false
///
static var ctlCandidateCurrent: ctlCandidateProtocol =
mgrPrefs.useIMKCandidateWindow ? ctlCandidateIMK.init(.horizontal) : ctlCandidateUniversal.init(.horizontal)
///
static var tooltipInstance = ctlTooltip()
///
static var popupCompositionBuffer = ctlPopupCompositionBuffer()
// MARK: -
/// ctlInputMethod
static var isASCIIModeSituation: Bool = false
/// ctlInputMethod
static var isVerticalTypingSituation: Bool = false
/// ctlInputMethod
static var isVerticalCandidateSituation: Bool = false
/// ctlInputMethod
var isASCIIMode: Bool = false { didSet { setKeyLayout() } }
/// 調
var keyHandler: KeyHandler = .init()
///
var state: IMEStateProtocol = IMEState.ofEmpty() {
didSet {
IME.prtDebugIntel("Current State: \(state.type.rawValue)")
}
}
/// ctlInputMethod
func toggleASCIIMode() -> Bool {
resetKeyHandler()
isASCIIMode = !isASCIIMode
return isASCIIMode
}
/// `handle(event:)` Shift
var rencentKeyHandledByKeyHandlerEtc = false
// MARK: -
///
func setKeyLayout() {
guard let client = client() else { return }
if isASCIIMode, AppleKeyboardConverter.isDynamicBasicKeyboardLayoutEnabled {
client.overrideKeyboard(withKeyboardNamed: mgrPrefs.alphanumericalKeyboardLayout)
return
}
client.overrideKeyboard(withKeyboardNamed: mgrPrefs.basicKeyboardLayout)
}
/// 調
func resetKeyHandler() {
//
if state.type == .ofInputting, mgrPrefs.trimUnfinishedReadingsOnCommit {
keyHandler.composer.clear()
handle(state: keyHandler.buildInputtingState)
}
let isSecureMode = mgrPrefs.clientsIMKTextInputIncapable.contains(clientBundleIdentifier)
if state.hasComposition, !isSecureMode {
/// 調
handle(state: IMEState.ofCommitting(textToCommit: state.displayedText))
}
handle(state: isSecureMode ? IMEState.ofAbortion() : IMEState.ofEmpty())
}
// MARK: - IMKInputController
///
///
/// inputClient IMKServer IMKTextInput
/// - Remark: IMKInputController client()
/// - Parameters:
/// - server: IMKServer
/// - delegate:
/// - inputClient:
override init!(server: IMKServer!, delegate: Any!, client inputClient: Any!) {
super.init(server: server, delegate: delegate, client: inputClient)
keyHandler.delegate = self
//
resetKeyHandler()
activateServer(inputClient)
}
// MARK: - IMKStateSetting
///
/// - Parameter sender: 使
override func activateServer(_ sender: Any!) {
_ = sender //
UserDefaults.standard.synchronize()
// activateServer nil
//
if keyHandler.delegate == nil { keyHandler.delegate = self }
setValue(IME.currentInputMode.rawValue, forTag: 114_514, client: client())
keyHandler.clear() // handle State.Empty()
keyHandler.ensureParser()
if isASCIIMode, mgrPrefs.disableShiftTogglingAlphanumericalMode { isASCIIMode = false }
///
/// macOS
if let client = client(), client.bundleIdentifier() != Bundle.main.bundleIdentifier {
// 使
setKeyLayout()
handle(state: IMEState.ofEmpty())
} //
UpdateSputnik.shared.checkForUpdate()
}
///
/// - Parameter sender: 使
override func deactivateServer(_ sender: Any!) {
_ = sender //
resetKeyHandler() // Empty
handle(state: IMEState.ofDeactivated())
}
///
/// - Parameters:
/// - value: identifier bundle identifier info.plist
/// - tag: 使
/// - sender: 使
override func setValue(_ value: Any!, forTag tag: Int, client sender: Any!) {
_ = tag //
_ = sender //
var newInputMode = InputMode(rawValue: value as? String ?? "") ?? InputMode.imeModeNULL
switch newInputMode {
case InputMode.imeModeCHS:
newInputMode = InputMode.imeModeCHS
case InputMode.imeModeCHT:
newInputMode = InputMode.imeModeCHT
default:
newInputMode = InputMode.imeModeNULL
}
mgrLangModel.loadDataModel(newInputMode)
if keyHandler.inputMode != newInputMode {
UserDefaults.standard.synchronize()
keyHandler.clear() // handle State.Empty()
keyHandler.inputMode = newInputMode
///
/// macOS
if let client = client(), client.bundleIdentifier() != Bundle.main.bundleIdentifier {
// 使
setKeyLayout()
handle(state: IMEState.ofEmpty())
} //
}
//
IME.currentInputMode = keyHandler.inputMode
}
// MARK: - IMKServerInput
///
///
///
/// Swift `NSEvent.EventTypeMask = [.keyDown]` ObjC `NSKeyDownMask`
/// IMK
/// 使
/// `commitComposition(_ message)`
/// - Parameter sender: 使
/// - Returns: uint NSEvent NSEvent.h
override func recognizedEvents(_ sender: Any!) -> Int {
_ = sender //
let events: NSEvent.EventTypeMask = [.keyDown, .flagsChanged]
return Int(events.rawValue)
}
/// NSEvent
/// - Parameters:
/// - event: nil
/// - sender: 使
/// - Returns: `true` IMK`false`
@objc(handleEvent:client:) override func handle(_ event: NSEvent!, client sender: Any!) -> Bool {
_ = sender //
// MARK:
//
ctlInputMethod.isASCIIModeSituation = isASCIIMode
ctlInputMethod.isVerticalTypingSituation = isVerticalTyping
// NSEvent nilApple InputMethodKit
// client()
guard let event = event, sender is IMKTextInput else {
resetKeyHandler()
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
}
}()
/// event event var Shift
if #available(macOS 10.15, *) {
if ShiftKeyUpChecker.check(event), !mgrPrefs.disableShiftTogglingAlphanumericalMode {
if !shouldUseShiftToggleHandle || (!rencentKeyHandledByKeyHandlerEtc && shouldUseShiftToggleHandle) {
NotifierController.notify(
message: toggleASCIIMode()
? NSLocalizedString("Alphanumerical Input Mode", comment: "")
: NSLocalizedString("Chinese Input Mode", 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
if let result = imkCandidatesEventHandler(event: eventToDeal) {
if shouldUseShiftToggleHandle {
rencentKeyHandledByKeyHandlerEtc = result
}
return result
}
/// NSEvent commonEventHandler
/// IMK 便
let result = commonEventHandler(eventToDeal)
if shouldUseShiftToggleHandle {
rencentKeyHandledByKeyHandlerEtc = result
}
return result
}
/// App Ctrl+Enter / Shift+Enter
/// handle(event:) Event
/// commitComposition
/// - Parameter sender: 使
override func commitComposition(_ sender: Any!) {
_ = sender //
resetKeyHandler()
// super.commitComposition(sender) //
}
///
/// - Parameter sender: 使
/// - Returns: nil
override func composedString(_ sender: Any!) -> Any! {
_ = sender //
guard state.hasComposition else { return "" }
return state.displayedText
}
///
/// IMK Bug
override func inputControllerWillClose() {
//
resetKeyHandler()
super.inputControllerWillClose()
}
// MARK: - IMKCandidates
/// IMK
/// - Parameter sender: 使
/// - Returns: IMK
override func candidates(_ sender: Any!) -> [Any]! {
_ = sender //
var arrResult = [String]()
// 便 IMEState
func handleCandidatesPrepared(_ candidates: [(String, String)], prefix: String = "") {
for theCandidate in candidates {
let theConverted = IME.kanjiConversionIfRequired(theCandidate.1)
var result = (theCandidate.1 == theConverted) ? theCandidate.1 : "\(theConverted)\u{1A}(\(theCandidate.1))"
if arrResult.contains(result) {
let reading: String =
mgrPrefs.showHanyuPinyinInCompositionBuffer
? Tekkon.cnvPhonaToHanyuPinyin(target: Tekkon.restoreToneOneInZhuyinKey(target: theCandidate.0))
: theCandidate.0
result = "\(result)\u{17}(\(reading))"
}
arrResult.append(prefix + result)
}
}
if state.type == .ofAssociates {
handleCandidatesPrepared(state.candidates, prefix: "")
} else if state.type == .ofSymbolTable {
// / JIS 使
arrResult = state.candidates.map(\.1)
} else if state.type == .ofCandidates {
guard !state.candidates.isEmpty else { return .init() }
if state.candidates[0].0.contains("_punctuation") {
arrResult = state.candidates.map(\.1) //
} else {
handleCandidatesPrepared(state.candidates)
}
}
return arrResult
}
/// IMK
/// - Parameter _:
override open func candidateSelectionChanged(_: NSAttributedString!) {
//
// IMKServer.commitCompositionWithReply() commitComposition()
// keyHandler
//
//
// ctlCandidateIMK identifier
// NSNotFound NSLog identifier
// console ips
// candidateSelected() identifier NSNotFound
// IMK 西
}
/// IMK
/// - Parameter candidateString:
override open func candidateSelected(_ candidateString: NSAttributedString!) {
let candidateString: NSAttributedString = candidateString ?? .init(string: "")
if state.type == .ofAssociates {
if !mgrPrefs.alsoConfirmAssociatedCandidatesByEnter {
handle(state: IMEState.ofAbortion())
return
}
}
var indexDeducted = 0
// 便 IMEState
func handleCandidatesSelected(_ candidates: [(String, String)], prefix: String = "") {
for (i, neta) in candidates.enumerated() {
let theConverted = IME.kanjiConversionIfRequired(neta.1)
let netaShown = (neta.1 == theConverted) ? neta.1 : "\(theConverted)\u{1A}(\(neta.1))"
let reading: String =
mgrPrefs.showHanyuPinyinInCompositionBuffer
? Tekkon.cnvPhonaToHanyuPinyin(target: Tekkon.restoreToneOneInZhuyinKey(target: neta.0)) : neta.0
let netaShownWithPronunciation = "\(netaShown)\u{17}(\(reading))"
if candidateString.string == prefix + netaShownWithPronunciation {
indexDeducted = i
break
}
if candidateString.string == prefix + netaShown {
indexDeducted = i
break
}
}
}
// / JIS 使
func handleSymbolCandidatesSelected(_ candidates: [(String, String)]) {
for (i, neta) in candidates.enumerated() {
if candidateString.string == neta.1 {
indexDeducted = i
break
}
}
}
if state.type == .ofAssociates {
handleCandidatesSelected(state.candidates, prefix: "")
} else if state.type == .ofSymbolTable {
handleSymbolCandidatesSelected(state.candidates)
} else if state.type == .ofCandidates {
guard !state.candidates.isEmpty else { return }
if state.candidates[0].0.contains("_punctuation") {
handleSymbolCandidatesSelected(state.candidates) //
} else {
handleCandidatesSelected(state.candidates)
}
}
keyHandler(
keyHandler,
didSelectCandidateAt: indexDeducted,
ctlCandidate: ctlInputMethod.ctlCandidateCurrent
)
}
}