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

210 lines
9.1 KiB
Swift
Raw Permalink 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 AppKit
import Foundation
import Shared
import Tekkon
// MARK: - IMKCandidates
public extension SessionCtl {
private var initialCharForQuickCandidates: String {
PrefMgr.shared.useHorizontalCandidateList ? "" : "🗲"
}
/// IMK
/// - Parameter sender: 使
/// - Returns: IMK
override func candidates(_ sender: Any!) -> [Any]! {
_ = sender //
var arrResult = [String]()
// 便 IMEState
func handleIMKCandidatesPrepared(
_ candidates: [(keyArray: [String], value: String)], prefix: String = ""
) {
guard let separator = inputHandler?.keySeparator else { return }
for theCandidate in candidates {
let theConverted = ChineseConverter.kanjiConversionIfRequired(theCandidate.value)
var result = (theCandidate.value == theConverted) ? theCandidate.value : "\(theConverted)\u{1A}(\(theCandidate.value))"
if arrResult.contains(result) {
let reading: String =
PrefMgr.shared.cassetteEnabled
? theCandidate.keyArray.joined(separator: separator)
: (PrefMgr.shared.showHanyuPinyinInCompositionBuffer
? Tekkon.cnvPhonaToHanyuPinyin(
targetJoined: {
var arr = [String]()
theCandidate.keyArray.forEach { key in
arr.append(Tekkon.restoreToneOneInPhona(target: key))
}
return arr.joined(separator: "-")
}()
)
: theCandidate.keyArray.joined(separator: separator))
result = "\(result)\u{17}(\(reading))"
}
arrResult.append(prefix + result)
}
}
switch state.type {
case .ofDeactivated, .ofEmpty, .ofAbortion, .ofCommitting, .ofMarking: break
case .ofAssociates:
handleIMKCandidatesPrepared(state.candidates, prefix: "")
case .ofInputting where state.isCandidateContainer:
handleIMKCandidatesPrepared(state.candidates, prefix: initialCharForQuickCandidates)
case .ofCandidates:
guard !state.candidates.isEmpty else { return .init() }
if state.candidates[0].keyArray.joined(separator: "-").contains("_punctuation") {
arrResult = state.candidates.map(\.value) //
} else {
handleIMKCandidatesPrepared(state.candidates)
}
case .ofSymbolTable:
// / JIS 使
arrResult = state.candidates.map(\.value)
default: break
}
return arrResult
}
/// IMK
/// - Parameter currentSelection:
override func candidateSelectionChanged(_ currentSelection: NSAttributedString!) {
guard state.isCandidateContainer else { return }
guard let candidateString = currentSelection?.string, !candidateString.isEmpty else { return }
// Handle candidatePairHighlightChanged().
let indexDeducted = deductCandidateIndex(from: candidateString)
candidatePairHighlightChanged(at: indexDeducted)
let realCandidateString = state.candidates[indexDeducted].value
// Handle IMK Annotation... We just use this to tell Apple that this never works in IMKCandidates.
DispatchQueue.main.async { [self] in
let annotation = reverseLookup(for: candidateString).joined(separator: "\n")
guard !annotation.isEmpty else { return }
vCLog("Current Annotation: \(annotation)")
guard let imkCandidates = candidateUI as? CtlCandidateIMK else { return }
annotationSelected(.init(string: annotation), forCandidate: .init(string: realCandidateString))
imkCandidates.showAnnotation(.init(string: annotation))
}
}
/// IMK
/// - Remark: IMK API Confirm Selection
/// - Parameter candidateString:
override func candidateSelected(_ candidateString: NSAttributedString!) {
guard state.isCandidateContainer else { return }
let candidateString: String = candidateString?.string ?? ""
if state.type == .ofAssociates {
// Shift+
let isShiftHold = NSEvent.keyModifierFlags.contains(.shift)
if !(isShiftHold || PrefMgr.shared.alsoConfirmAssociatedCandidatesByEnter) {
switchState(IMEState.ofAbortion())
return
}
}
let indexDeducted = deductCandidateIndex(from: candidateString)
candidatePairSelectionConfirmed(at: indexDeducted)
}
func deductCandidateIndex(from candidateString: String) -> Int {
var indexDeducted = 0
// / JIS 使
func fixSymbolIndexForIMKCandidates() {
for (i, neta) in state.candidates.enumerated() {
if candidateString == neta.value {
indexDeducted = min(i, state.candidates.count - 1)
break
}
}
}
switch state.type {
case .ofAssociates:
fixIndexForIMKCandidates(&indexDeducted, prefix: "", source: candidateString)
case .ofInputting where state.isCandidateContainer:
fixIndexForIMKCandidates(&indexDeducted, prefix: initialCharForQuickCandidates, source: candidateString)
case .ofSymbolTable:
fixSymbolIndexForIMKCandidates()
case .ofCandidates:
guard !state.candidates.isEmpty else { break }
if state.candidates[0].keyArray.description.contains("_punctuation") {
fixSymbolIndexForIMKCandidates() //
} else {
fixIndexForIMKCandidates(&indexDeducted, source: candidateString)
}
default: break
}
return indexDeducted
}
/// IMKCandidates
/// - Remark: `\u{1A}`便 IMEState
/// - Parameters:
/// - prefix:
/// - indexToFix:
/// - candidateString: IMKCandidates
private func fixIndexForIMKCandidates(
_ indexDeducted: inout Int, prefix: String = "", source candidateString: String
) {
guard state.isCandidateContainer else { return }
guard let separator = inputHandler?.keySeparator else { return }
let candidates = state.candidates
let maxIndex = candidates.count - 1
for (i, neta) in candidates.enumerated() {
let theConverted = ChineseConverter.kanjiConversionIfRequired(neta.value)
let netaShown = (neta.value == theConverted)
? neta.value
: "\(theConverted)\u{1A}(\(neta.value))"
let reading: String =
PrefMgr.shared.cassetteEnabled
? neta.keyArray.joined(separator: separator)
: (PrefMgr.shared.showHanyuPinyinInCompositionBuffer
? Tekkon.cnvPhonaToHanyuPinyin(
targetJoined: {
var arr = [String]()
neta.keyArray.forEach { key in
arr.append(Tekkon.restoreToneOneInPhona(target: key))
}
return arr.joined(separator: "-")
}()
)
: neta.keyArray.joined(separator: separator))
let netaShownWithPronunciation = "\(netaShown)\u{17}(\(reading))"
if candidateString == prefix + netaShownWithPronunciation {
indexDeducted = min(i, maxIndex)
break
}
if candidateString == prefix + netaShown {
indexDeducted = min(i, maxIndex)
break
}
}
}
/// deactivateServer() activateServer()
///
/// IMKCandidates
internal func keepIMKCandidatesShownUp() {
guard let imkC = candidateUI as? CtlCandidateIMK else { return }
var i: Double = 0
while i < 1 {
DispatchQueue.main.asyncAfter(deadline: .now() + i) { [self] in
if state.isCandidateContainer, !imkC.visible {
imkC.visible = true
}
}
i += 0.3
}
}
}