vChewing-macOS/Source/Modules/ctlInputMethod_Core.swift

274 lines
13 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 IMKUtils
import PopupCompositionBuffer
import Shared
import ShiftKeyUpChecker
import TooltipUI
///
///
/// IMKInputController
///
/// / IMKInputController
/// 調
/// - Remark: IMKServer
/// IMKInputController
@objc(ctlInputMethod) // ObjC IMK ObjC
class ctlInputMethod: IMKInputController {
///
static var areWeNerfing = false
///
static var ctlCandidateCurrent: ctlCandidateProtocol =
PrefMgr.shared.useIMKCandidateWindow ? ctlCandidateIMK.init(.horizontal) : ctlCandidateUniversal.init(.horizontal)
///
static var tooltipInstance = TooltipUI()
///
static var popupCompositionBuffer = PopupCompositionBuffer()
// MARK: -
/// ctlInputMethod
var isASCIIMode = false {
didSet {
resetKeyHandler()
setKeyLayout()
}
}
/// 調
var keyHandler = KeyHandler(lm: LMMgr.currentLM(), uom: LMMgr.currentUOM(), pref: PrefMgr.shared)
///
var state: IMEStateProtocol = IMEState.ofEmpty() {
didSet {
vCLog("Current State: \(state.type.rawValue)")
}
}
/// Shift
/// - Remark: Struct CapsLock Shift
static var theShiftKeyDetector = ShiftKeyUpChecker(useLShift: PrefMgr.shared.togglingAlphanumericalModeWithLShift)
/// `handle(event:)` Shift
var rencentKeyHandledByKeyHandlerEtc = false
///
public static var isVerticalTyping: Bool = false
public var isVerticalTyping: Bool {
guard let client = client() else { return false }
var textFrame = NSRect.seniorTheBeast
let attributes: [AnyHashable: Any]? = client.attributes(
forCharacterIndex: 0, lineHeightRectangle: &textFrame
)
let result = (attributes?["IMKTextOrientation"] as? NSNumber)?.intValue == 0 || false
Self.isVerticalTyping = result
return result
}
/// InputMode
/// IME UserPrefs
var inputMode: Shared.InputMode = IMEApp.currentInputMode {
willSet {
/// Prefs IME
IMEApp.currentInputMode = newValue
PrefMgr.shared.mostRecentInputMode = IMEApp.currentInputMode.rawValue
}
didSet {
///
keyHandler.currentLM = LMMgr.currentLM() //
keyHandler.currentUOM = LMMgr.currentUOM()
///
keyHandler.ensureKeyboardParser()
///
syncBaseLMPrefs()
}
}
///
///
/// 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
syncBaseLMPrefs()
//
resetKeyHandler()
activateServer(inputClient)
}
}
// MARK: -
extension ctlInputMethod {
///
func setKeyLayout() {
guard let client = client() else { return }
if isASCIIMode, IMKHelper.isDynamicBasicKeyboardLayoutEnabled {
client.overrideKeyboard(withKeyboardNamed: PrefMgr.shared.alphanumericalKeyboardLayout)
return
}
client.overrideKeyboard(withKeyboardNamed: PrefMgr.shared.basicKeyboardLayout)
}
/// 調
func resetKeyHandler() {
//
if state.type == .ofInputting, PrefMgr.shared.trimUnfinishedReadingsOnCommit {
keyHandler.composer.clear()
handle(state: keyHandler.buildInputtingState)
}
let isSecureMode = PrefMgr.shared.clientsIMKTextInputIncapable.contains(clientBundleIdentifier)
if state.hasComposition, !isSecureMode {
/// 調
handle(state: IMEState.ofCommitting(textToCommit: state.displayedText))
}
handle(state: isSecureMode ? IMEState.ofAbortion() : IMEState.ofEmpty())
}
}
// MARK: - IMKStateSetting
extension ctlInputMethod {
///
/// - Parameter sender: 使
override func activateServer(_ sender: Any!) {
_ = sender //
UserDefaults.standard.synchronize()
// activateServer nil
//
if keyHandler.delegate == nil { keyHandler.delegate = self }
// setValue() IMK activateServer() setValue()
keyHandler.clear() // handle State.Empty()
keyHandler.ensureKeyboardParser()
if isASCIIMode, PrefMgr.shared.disableShiftTogglingAlphanumericalMode { isASCIIMode = false }
///
/// macOS
if let client = client() {
Self.isVerticalTyping = isVerticalTyping
if client.bundleIdentifier() != Bundle.main.bundleIdentifier {
// 使
setKeyLayout()
handle(state: IMEState.ofEmpty())
}
} //
(NSApp.delegate as? AppDelegate)?.updateSputnik.checkForUpdate(forced: false, url: kUpdateInfoSourceURL)
}
///
/// - 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 = Shared.InputMode(rawValue: value as? String ?? "") ?? Shared.InputMode.imeModeNULL
switch newInputMode {
case .imeModeCHS:
newInputMode = .imeModeCHS
case .imeModeCHT:
newInputMode = .imeModeCHT
default:
newInputMode = .imeModeNULL
}
if PrefMgr.shared.onlyLoadFactoryLangModelsIfNeeded { LMMgr.loadDataModel(newInputMode) }
if inputMode != newInputMode {
UserDefaults.standard.synchronize()
keyHandler.clear() // handle State.Empty()
inputMode = newInputMode
///
/// macOS
if let client = client(), client.bundleIdentifier() != Bundle.main.bundleIdentifier {
// 使
setKeyLayout()
handle(state: IMEState.ofEmpty())
} //
}
}
///
func syncBaseLMPrefs() {
LMMgr.currentLM().isPhraseReplacementEnabled = PrefMgr.shared.phraseReplacementEnabled
LMMgr.currentLM().isCNSEnabled = PrefMgr.shared.cns11643Enabled
LMMgr.currentLM().isSymbolEnabled = PrefMgr.shared.symbolInputEnabled
LMMgr.currentLM().isSCPCEnabled = PrefMgr.shared.useSCPCTypingMode
LMMgr.currentLM().deltaOfCalendarYears = PrefMgr.shared.deltaOfCalendarYears
}
}
// MARK: - IMKServerInput
// handle(_ event:) ctlInputMethod_HandleEvent.swift
extension ctlInputMethod {
///
///
///
/// 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)
}
/// App Ctrl+Enter / Shift+Enter
/// handle(event:) Event
/// commitComposition
/// - Parameter sender: 使
override func commitComposition(_ sender: Any!) {
_ = sender //
resetKeyHandler()
// super.commitComposition(sender) //
}
/// InputMethodKit
/// - Parameter sender: 使
/// - Returns: nil
override func composedString(_ sender: Any!) -> Any! {
_ = sender //
guard state.hasComposition else { return "" }
return state.displayedTextConverted
}
///
/// IMK Bug
override func inputControllerWillClose() {
//
resetKeyHandler()
super.inputControllerWillClose()
}
}