Skip to content

Commit

Permalink
TDKCandidates // Support displaying codepoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen committed Feb 15, 2024
1 parent ccd9b39 commit 424a736
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 424a736

Please sign in to comment.