vChewing-macOS/Source/Modules/SessionCtl_IMKCandidatesDat...

150 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) 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 Foundation
import Shared
import Tekkon
// MARK: - IMKCandidates
public extension SessionCtl {
/// IMK
/// - Parameter sender: 使
/// - Returns: IMK
override func candidates(_ sender: Any!) -> [Any]! {
_ = sender //
var arrResult = [String]()
// 便 IMEState
func handleIMKCandidatesPrepared(_ candidates: [([String], String)], prefix: String = "") {
guard let separator = inputHandler?.keySeparator else { return }
for theCandidate in candidates {
let theConverted = ChineseConverter.kanjiConversionIfRequired(theCandidate.1)
var result = (theCandidate.1 == theConverted) ? theCandidate.1 : "\(theConverted)\u{1A}(\(theCandidate.1))"
if arrResult.contains(result) {
let reading: String =
PrefMgr.shared.cassetteEnabled
? theCandidate.0.joined(separator: separator)
: (PrefMgr.shared.showHanyuPinyinInCompositionBuffer
? Tekkon.cnvPhonaToHanyuPinyin(
targetJoined: {
var arr = [String]()
theCandidate.0.forEach { key in
arr.append(Tekkon.restoreToneOneInPhona(target: key))
}
return arr.joined(separator: "-")
}()
)
: theCandidate.0.joined(separator: separator))
result = "\(result)\u{17}(\(reading))"
}
arrResult.append(prefix + result)
}
}
if state.type == .ofAssociates {
handleIMKCandidatesPrepared(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 {
handleIMKCandidatesPrepared(state.candidates)
}
}
return arrResult
}
/// IMK
/// - Parameter currentSelection:
override func candidateSelectionChanged(_ currentSelection: NSAttributedString!) {
guard let currentCandidate = currentSelection?.string, !currentCandidate.isEmpty else { return }
let annotation = reverseLookup(for: currentCandidate).joined(separator: "\n")
guard !annotation.isEmpty else { return }
vCLog("Current Annotation: \(annotation)")
DispatchQueue.main.async { [self] in
guard let imkCandidates = candidateUI as? CtlCandidateIMK else { return }
imkCandidates.showAnnotation(.init(string: annotation))
}
}
/// IMK
/// - Parameter candidateString:
override func candidateSelected(_ candidateString: NSAttributedString!) {
let candidateString: String = candidateString?.string ?? ""
if state.type == .ofAssociates {
if !PrefMgr.shared.alsoConfirmAssociatedCandidatesByEnter {
switchState(IMEState.ofAbortion())
return
}
}
var indexDeducted = 0
// 便 IMEState
func handleIMKCandidatesSelected(_ candidates: [([String], String)], prefix: String = "") {
guard let separator = inputHandler?.keySeparator else { return }
for (i, neta) in candidates.enumerated() {
let theConverted = ChineseConverter.kanjiConversionIfRequired(neta.1)
let netaShown = (neta.1 == theConverted) ? neta.1 : "\(theConverted)\u{1A}(\(neta.1))"
let reading: String =
PrefMgr.shared.cassetteEnabled
? neta.0.joined(separator: separator)
: (PrefMgr.shared.showHanyuPinyinInCompositionBuffer
? Tekkon.cnvPhonaToHanyuPinyin(
targetJoined: {
var arr = [String]()
neta.0.forEach { key in
arr.append(Tekkon.restoreToneOneInPhona(target: key))
}
return arr.joined(separator: "-")
}()
)
: neta.0.joined(separator: separator))
let netaShownWithPronunciation = "\(netaShown)\u{17}(\(reading))"
if candidateString == prefix + netaShownWithPronunciation {
indexDeducted = i
break
}
if candidateString == prefix + netaShown {
indexDeducted = i
break
}
}
}
// / JIS 使
func handleSymbolCandidatesSelected(_ candidates: [([String], String)]) {
for (i, neta) in candidates.enumerated() {
if candidateString == neta.1 {
indexDeducted = i
break
}
}
}
if state.type == .ofAssociates {
handleIMKCandidatesSelected(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 {
handleIMKCandidatesSelected(state.candidates)
}
}
candidatePairSelected(at: indexDeducted)
}
}