Repo // Call candidatePairHighlightChanged() when appropriate.

This commit is contained in:
ShikiSuen 2023-03-09 16:32:48 +08:00
parent fc49bebc5d
commit 625897a744
2 changed files with 60 additions and 46 deletions

View File

@ -88,6 +88,7 @@ public class CtlCandidateTDK: CtlCandidate, NSWindowDelegate {
reverseLookupResult = delegate?.reverseLookup(for: currentCandidateText) ?? []
Self.thePool.reverseLookupResult = reverseLookupResult
}
delegate?.candidatePairHighlightChanged(at: highlightedIndex)
DispatchQueue.main.async { [self] in
window.isOpaque = false
window.backgroundColor = .clear

View File

@ -69,14 +69,20 @@ public extension SessionCtl {
/// 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)")
guard let candidateString = currentSelection?.string, !candidateString.isEmpty else { return }
// 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 }
imkCandidates.showAnnotation(.init(string: annotation))
}
// Handle candidatePairHighlightChanged().
guard state.type == .ofCandidates else { return }
var indexDeducted = 0
fixIndexForIMKCandidates(&indexDeducted, source: candidateString)
candidatePairHighlightChanged(at: indexDeducted)
}
/// IMK
@ -85,6 +91,7 @@ public extension SessionCtl {
override func candidateSelected(_ candidateString: NSAttributedString!) {
let candidateString: String = candidateString?.string ?? ""
if state.type == .ofAssociates {
// Shift+
if !PrefMgr.shared.alsoConfirmAssociatedCandidatesByEnter {
switchState(IMEState.ofAbortion())
return
@ -93,45 +100,9 @@ public extension SessionCtl {
var indexDeducted = 0
// 便 IMEState
func handleIMKCandidatesSelected(
_ candidates: [(keyArray: [String], value: String)], prefix: String = ""
) {
guard let separator = inputHandler?.keySeparator else { return }
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 = i
break
}
if candidateString == prefix + netaShown {
indexDeducted = i
break
}
}
}
// / JIS 使
func handleSymbolCandidatesSelected(_ candidates: [(keyArray: [String], value: String)]) {
for (i, neta) in candidates.enumerated() {
func fixSymbolIndexForIMKCandidates() {
for (i, neta) in state.candidates.enumerated() {
if candidateString == neta.value {
indexDeducted = i
break
@ -140,17 +111,59 @@ public extension SessionCtl {
}
if state.type == .ofAssociates {
handleIMKCandidatesSelected(state.candidates, prefix: "")
fixIndexForIMKCandidates(&indexDeducted, prefix: "", source: candidateString)
} else if state.type == .ofSymbolTable {
handleSymbolCandidatesSelected(state.candidates)
fixSymbolIndexForIMKCandidates()
} else if state.type == .ofCandidates {
guard !state.candidates.isEmpty else { return }
if state.candidates[0].keyArray.description.contains("_punctuation") {
handleSymbolCandidatesSelected(state.candidates) //
fixSymbolIndexForIMKCandidates() //
} else {
handleIMKCandidatesSelected(state.candidates)
fixIndexForIMKCandidates(&indexDeducted, source: candidateString)
}
}
candidatePairSelectionConfirmed(at: indexDeducted)
}
/// IMKCandidates
/// - Remark: `\u{1A`便 IMEState
/// - Parameters:
/// - prefix:
/// - indexToFix:
/// - candidateString: IMKCandidates
private func fixIndexForIMKCandidates(
_ indexDeducted: inout Int, prefix: String = "", source candidateString: String
) {
guard let separator = inputHandler?.keySeparator else { return }
let candidates = state.candidates
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 = i
break
}
if candidateString == prefix + netaShown {
indexDeducted = i
break
}
}
}
}