TDKCandidates // Support displaying codepoints.

This commit is contained in:
ShikiSuen 2024-02-15 04:09:16 +08:00
parent ccd9b391e4
commit 424a736c8e
5 changed files with 27 additions and 9 deletions

View File

@ -200,14 +200,14 @@ public class CandidateCellData: Hashable {
return attrStrCandidate
}
public var charDescriptions: [String] {
public func charDescriptions(shortened: Bool = false) -> [String] {
var result = displayedText
if displayedText.contains("("), displayedText.count > 2 {
result = displayedText.replacingOccurrences(of: "(", with: "").replacingOccurrences(of: ")", with: "")
}
return result.flatMap(\.unicodeScalars).compactMap {
let theName: String = $0.properties.name ?? ""
return String(format: "U+%02X %@", $0.value, theName)
return shortened ? String(format: "U+%02X", $0.value) : String(format: "U+%02X %@", $0.value, theName)
}
}

View File

@ -301,10 +301,12 @@ extension CandidatePool {
.font: Self.blankCell.phraseFont(size: reverseLookupTextSize),
]
let result = NSMutableAttributedString(string: "", attributes: attrReverseLookupSpacer)
var addedCounter = 0
for neta in reverseLookupResult {
result.append(NSAttributedString(string: " ", attributes: attrReverseLookupSpacer))
result.append(NSAttributedString(string: " \(neta) ", attributes: attrReverseLookup))
if maxLinesPerPage == 1 { break }
addedCounter += 1
if maxLinesPerPage == 1, addedCounter == 2 { break }
}
return result
}

View File

@ -108,16 +108,30 @@ public class CtlCandidateTDK: CtlCandidate, NSWindowDelegate {
override open func updateDisplay() {
guard let window = window else { return }
if let currentCandidateText = Self.thePool.currentSelectedCandidateText {
reverseLookupResult = delegate?.reverseLookup(for: currentCandidateText) ?? []
Self.thePool.reverseLookupResult = reverseLookupResult
Self.thePool.tooltip = delegate?.candidateToolTip(shortened: !Self.thePool.isMatrix) ?? ""
}
delegate?.candidatePairHighlightChanged(at: highlightedIndex)
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.updateNSWindowModern(window)
}
if let currentCandidate = Self.thePool.currentCandidate {
let displayedText = currentCandidate.displayedText
var lookupResult: [String?] = delegate?.reverseLookup(for: displayedText) ?? []
if displayedText.count == 1, delegate?.showCodePointForCurrentCandidate ?? false {
if lookupResult.isEmpty {
lookupResult.append(currentCandidate.charDescriptions(shortened: !Self.thePool.isMatrix).first)
} else {
lookupResult.insert(currentCandidate.charDescriptions(shortened: true).first, at: lookupResult.startIndex)
}
reverseLookupResult = lookupResult.compactMap { $0 }
} else {
// UNICODE
if !Self.thePool.isMatrix {
reverseLookupResult = [lookupResult.compactMap { $0 }.first].compactMap { $0 }
}
}
Self.thePool.reverseLookupResult = reverseLookupResult
}
Self.thePool.tooltip = delegate?.candidateToolTip(shortened: !Self.thePool.isMatrix) ?? ""
delegate?.candidatePairHighlightChanged(at: highlightedIndex)
}
func updateNSWindowModern(_ window: NSWindow) {

View File

@ -80,6 +80,7 @@ extension SessionCtl: InputHandlerDelegate {
extension SessionCtl: CtlCandidateDelegate {
public var isCandidateState: Bool { state.isCandidateContainer }
public var showCodePointForCurrentCandidate: Bool { PrefMgr.shared.showCodePointInCandidateUI }
public var clientAccentColor: NSColor? {
var nullResponse = !PrefMgr.shared.respectClientAccentColor

View File

@ -19,6 +19,7 @@ public protocol CtlCandidateDelegate {
var selectionKeys: String { get }
var isVerticalTyping: Bool { get }
var isCandidateState: Bool { get }
var showCodePointForCurrentCandidate: Bool { get }
var shouldAutoExpandCandidates: Bool { get }
var isCandidateContextMenuEnabled: Bool { get }
var showReverseLookupResult: Bool { get }