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) ?? [] reverseLookupResult = delegate?.reverseLookup(for: currentCandidateText) ?? []
Self.thePool.reverseLookupResult = reverseLookupResult Self.thePool.reverseLookupResult = reverseLookupResult
} }
delegate?.candidatePairHighlightChanged(at: highlightedIndex)
DispatchQueue.main.async { [self] in DispatchQueue.main.async { [self] in
window.isOpaque = false window.isOpaque = false
window.backgroundColor = .clear window.backgroundColor = .clear

View File

@ -69,14 +69,20 @@ public extension SessionCtl {
/// IMK /// IMK
/// - Parameter currentSelection: /// - Parameter currentSelection:
override func candidateSelectionChanged(_ currentSelection: NSAttributedString!) { override func candidateSelectionChanged(_ currentSelection: NSAttributedString!) {
guard let currentCandidate = currentSelection?.string, !currentCandidate.isEmpty else { return } guard let candidateString = currentSelection?.string, !candidateString.isEmpty else { return }
let annotation = reverseLookup(for: currentCandidate).joined(separator: "\n") // Handle IMK Annotation... We just use this to tell Apple that this never works in IMKCandidates.
guard !annotation.isEmpty else { return }
vCLog("Current Annotation: \(annotation)")
DispatchQueue.main.async { [self] in 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 } guard let imkCandidates = candidateUI as? CtlCandidateIMK else { return }
imkCandidates.showAnnotation(.init(string: annotation)) imkCandidates.showAnnotation(.init(string: annotation))
} }
// Handle candidatePairHighlightChanged().
guard state.type == .ofCandidates else { return }
var indexDeducted = 0
fixIndexForIMKCandidates(&indexDeducted, source: candidateString)
candidatePairHighlightChanged(at: indexDeducted)
} }
/// IMK /// IMK
@ -85,6 +91,7 @@ public extension SessionCtl {
override func candidateSelected(_ candidateString: NSAttributedString!) { override func candidateSelected(_ candidateString: NSAttributedString!) {
let candidateString: String = candidateString?.string ?? "" let candidateString: String = candidateString?.string ?? ""
if state.type == .ofAssociates { if state.type == .ofAssociates {
// Shift+
if !PrefMgr.shared.alsoConfirmAssociatedCandidatesByEnter { if !PrefMgr.shared.alsoConfirmAssociatedCandidatesByEnter {
switchState(IMEState.ofAbortion()) switchState(IMEState.ofAbortion())
return return
@ -93,45 +100,9 @@ public extension SessionCtl {
var indexDeducted = 0 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 使 // / JIS 使
func handleSymbolCandidatesSelected(_ candidates: [(keyArray: [String], value: String)]) { func fixSymbolIndexForIMKCandidates() {
for (i, neta) in candidates.enumerated() { for (i, neta) in state.candidates.enumerated() {
if candidateString == neta.value { if candidateString == neta.value {
indexDeducted = i indexDeducted = i
break break
@ -140,17 +111,59 @@ public extension SessionCtl {
} }
if state.type == .ofAssociates { if state.type == .ofAssociates {
handleIMKCandidatesSelected(state.candidates, prefix: "") fixIndexForIMKCandidates(&indexDeducted, prefix: "", source: candidateString)
} else if state.type == .ofSymbolTable { } else if state.type == .ofSymbolTable {
handleSymbolCandidatesSelected(state.candidates) fixSymbolIndexForIMKCandidates()
} else if state.type == .ofCandidates { } else if state.type == .ofCandidates {
guard !state.candidates.isEmpty else { return } guard !state.candidates.isEmpty else { return }
if state.candidates[0].keyArray.description.contains("_punctuation") { if state.candidates[0].keyArray.description.contains("_punctuation") {
handleSymbolCandidatesSelected(state.candidates) // fixSymbolIndexForIMKCandidates() //
} else { } else {
handleIMKCandidatesSelected(state.candidates) fixIndexForIMKCandidates(&indexDeducted, source: candidateString)
} }
} }
candidatePairSelectionConfirmed(at: indexDeducted) 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
}
}
}
} }