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

171 lines
6.3 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 Cocoa
// MARK: - KeyHandler Delegate
extension ctlInputMethod: KeyHandlerDelegate {
///
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
)
return (attributes?["IMKTextOrientation"] as? NSNumber)?.intValue == 0 || false
}
var clientBundleIdentifier: String {
guard let client = client() else { return "" }
return client.bundleIdentifier() ?? ""
}
func ctlCandidate() -> ctlCandidateProtocol { ctlInputMethod.ctlCandidateCurrent }
func keyHandler(
_: KeyHandler, didSelectCandidateAt index: Int,
ctlCandidate controller: ctlCandidateProtocol
) {
ctlCandidate(controller, didSelectCandidateAtIndex: index)
}
func keyHandler(_ keyHandler: KeyHandler, didRequestWriteUserPhraseWith state: IMEStateProtocol, addToFilter: Bool)
-> Bool
{
guard state.type == .ofMarking else { return false }
let refInputModeReversed: InputMode =
(keyHandler.inputMode == InputMode.imeModeCHT)
? InputMode.imeModeCHS : InputMode.imeModeCHT
if !mgrLangModel.writeUserPhrase(
state.data.userPhrase, inputMode: keyHandler.inputMode,
areWeDuplicating: state.data.chkIfUserPhraseExists,
areWeDeleting: addToFilter
)
|| !mgrLangModel.writeUserPhrase(
state.data.userPhraseConverted, inputMode: refInputModeReversed,
areWeDuplicating: false,
areWeDeleting: addToFilter
)
{
return false
}
return true
}
}
// MARK: - Candidate Controller Delegate
extension ctlInputMethod: ctlCandidateDelegate {
var isAssociatedPhrasesState: Bool { state.type == .ofAssociates }
/// handle() IMK
/// handle()
/// IMK commonEventHandler()
/// - Parameter event: IMK
/// - Returns: `true` IMK`false`
@discardableResult func sharedEventHandler(_ event: NSEvent) -> Bool {
commonEventHandler(event)
}
func candidateCountForController(_ controller: ctlCandidateProtocol) -> Int {
_ = controller //
if state.isCandidateContainer {
return state.candidates.count
}
return 0
}
///
/// - Parameter controller:
/// - Returns:
func candidatesForController(_ controller: ctlCandidateProtocol) -> [(String, String)] {
_ = controller //
if state.isCandidateContainer {
return state.candidates
}
return .init()
}
func ctlCandidate(_ controller: ctlCandidateProtocol, candidateAtIndex index: Int)
-> (String, String)
{
_ = controller //
if state.isCandidateContainer {
return state.candidates[index]
}
return ("", "")
}
func ctlCandidate(_ controller: ctlCandidateProtocol, didSelectCandidateAtIndex index: Int) {
_ = controller //
if state.type == .ofSymbolTable,
let node = state.node.children?[index]
{
if let children = node.children, !children.isEmpty {
handle(state: IMEState.ofEmpty()) //
handle(state: IMEState.ofSymbolTable(node: node))
} else {
handle(state: IMEState.ofCommitting(textToCommit: node.title))
handle(state: IMEState.ofEmpty())
}
return
}
if [.ofCandidates, .ofSymbolTable].contains(state.type) {
let selectedValue = state.candidates[index]
keyHandler.fixNode(
candidate: selectedValue, respectCursorPushing: true,
preConsolidate: mgrPrefs.consolidateContextOnCandidateSelection
)
let inputting = keyHandler.buildInputtingState
if mgrPrefs.useSCPCTypingMode {
handle(state: IMEState.ofCommitting(textToCommit: inputting.displayedText))
// selectedValue.1
if mgrPrefs.associatedPhrasesEnabled {
let associates = keyHandler.buildAssociatePhraseState(
withPair: .init(key: selectedValue.0, value: selectedValue.1)
)
handle(state: associates.candidates.isEmpty ? IMEState.ofEmpty() : associates)
} else {
handle(state: IMEState.ofEmpty())
}
} else {
handle(state: inputting)
}
return
}
if state.type == .ofAssociates {
let selectedValue = state.candidates[index]
handle(state: IMEState.ofCommitting(textToCommit: selectedValue.1))
// selectedValue.1
//
guard let valueKept = selectedValue.1.last else {
handle(state: IMEState.ofEmpty())
return
}
if mgrPrefs.associatedPhrasesEnabled {
let associates = keyHandler.buildAssociatePhraseState(
withPair: .init(key: selectedValue.0, value: String(valueKept))
)
if !associates.candidates.isEmpty {
handle(state: associates)
return
}
}
handle(state: IMEState.ofEmpty())
}
}
}